function xfac_syncPost_pullPost($config, $thread, $tags, $direction = 'pull') { if (empty($thread['creator_user_id'])) { return 0; } $wpUserData = xfac_user_getUserDataByApiData($config['root'], $thread['creator_user_id']); if (empty($wpUserData)) { return 0; } $postAuthor = $wpUserData->ID; $wpUser = new WP_User($wpUserData); $postTypeObj = get_post_type_object('post'); if (empty($postTypeObj)) { // no post type object?! return 0; } if (!$wpUser->has_cap($postTypeObj->cap->create_posts)) { // no permission to create posts xfac_log('xfac_syncPost_pullPost skipped pulling post because of lack of create_posts capability (user #%d)', $wpUser->ID); return 0; } $postDateGmt = gmdate('Y-m-d H:i:s', $thread['thread_create_date']); $postDate = get_date_from_gmt($postDateGmt); $postStatus = 'draft'; if (intval(get_option('xfac_sync_post_xf_wp_publish')) > 0) { $postStatus = 'publish'; } $postContent = xfac_api_filterHtmlFromXenForo($thread['first_post']['post_body_html']); $wpPost = array('post_author' => $postAuthor, 'post_content' => $postContent, 'post_date' => $postDate, 'post_date_gmt' => $postDateGmt, 'post_status' => $postStatus, 'post_title' => $thread['thread_title'], 'post_type' => 'post', 'tags_input' => implode(', ', $tags)); $XFAC_SKIP_xfac_save_post_before = !empty($GLOBALS['XFAC_SKIP_xfac_save_post']); $GLOBALS['XFAC_SKIP_xfac_save_post'] = true; $wpPostId = wp_insert_post($wpPost); $GLOBALS['XFAC_SKIP_xfac_save_post'] = $XFAC_SKIP_xfac_save_post_before; if ($wpPostId > 0) { $subscribed = array(); if (intval(get_option('xfac_sync_comment_xf_wp')) > 0) { $accessToken = xfac_user_getAccessToken($wpUser->ID); if (!empty($accessToken)) { $xfPosts = xfac_api_getPostsInThread($config, $thread['thread_id'], $accessToken); if (empty($xfPosts['subscription_callback']) and !empty($xfPosts['_headerLinkHub'])) { if (xfac_api_postSubscription($config, $accessToken, $xfPosts['_headerLinkHub'])) { $subscribed = array('hub' => $xfPosts['_headerLinkHub'], 'time' => time()); } } } } xfac_sync_updateRecord('', 'thread', $thread['thread_id'], $wpPostId, $thread['thread_create_date'], array('forumId' => $thread['forum_id'], 'thread' => $thread, 'direction' => $direction, 'sticky' => !empty($thread['thread_is_sticky']), 'subscribed' => $subscribed)); xfac_log('xfac_syncPost_pullPost pulled $xfThread (#%d) as $wpPost (#%d)', $thread['thread_id'], $wpPostId); } else { xfac_log('xfac_syncPost_pullPost failed pulling $xfThread (#%d)'); } return $wpPostId; }
function xfac_syncComment_pullComment($config, $xfPost, $wpPostId, $direction = 'pull') { $wpUserUrl = ''; $wpUserId = 0; $wpUserData = xfac_user_getUserDataByApiData($config['root'], $xfPost['poster_user_id']); if (empty($wpUserData)) { if (intval(get_option('xfac_sync_comment_xf_wp_as_guest')) == 0) { return 0; } // no wordpress user found but as_guest option is enabled // sync as guest now $wpDisplayName = $xfPost['poster_username']; $wpUserEmail = sprintf('*****@*****.**', $xfPost['poster_username'], $xfPost['poster_user_id']); } else { $wpDisplayName = $wpUserData->display_name; $wpUserEmail = $wpUserData->user_email; $wpUserUrl = $wpUserData->user_url; $wpUserId = $wpUserData->ID; } $commentDateGmt = gmdate('Y-m-d H:i:s', $xfPost['post_create_date']); $commentDate = get_date_from_gmt($commentDateGmt); $commentContent = xfac_api_filterHtmlFromXenForo($xfPost['post_body_html']); $comment = array('comment_post_ID' => $wpPostId, 'comment_author' => $wpDisplayName, 'comment_author_email' => $wpUserEmail, 'comment_author_url' => $wpUserUrl, 'comment_content' => $commentContent, 'user_id' => $wpUserId, 'comment_date_gmt' => $commentDateGmt, 'comment_date' => $commentDate, 'comment_approved' => 1); $XFAC_SKIP_xfac_save_comment_before = !empty($GLOBALS['XFAC_SKIP_xfac_save_comment']); $GLOBALS['XFAC_SKIP_xfac_save_comment'] = true; $commentId = wp_insert_comment($comment); $GLOBALS['XFAC_SKIP_xfac_save_comment'] = $XFAC_SKIP_xfac_save_comment_before; if ($commentId > 0) { xfac_sync_updateRecord('', 'post', $xfPost['post_id'], $commentId, 0, array('post' => $xfPost, 'direction' => $direction)); xfac_log('xfac_syncComment_pullComment pulled $xfPost (#%d) -> $wpComment (#%d)', $xfPost['post_id'], $commentId); } else { xfac_log('xfac_syncComment_pullComment failed pulling $xfPost (#%d)', $xfPost['post_id']); } return $commentId; }
function _xfac_subscription_handleCallback_threadPost($config, $ping, $postSyncRecord, $commentSyncRecord) { $wpUserData = xfac_user_getUserDataByApiData($config['root'], $postSyncRecord->syncData['thread']['creator_user_id']); $optionSyncPost = intval(get_option('xfac_sync_post_xf_wp')) > 0; $optionSyncComment = intval(get_option('xfac_sync_comment_xf_wp')) > 0; $accessToken = xfac_user_getAccessToken($wpUserData->ID); $xfPost = xfac_api_getPost($config, $ping['object_data'], $accessToken); $xfPostIsDeleted = (empty($xfPost['post']) or !empty($xfPost['post']['post_is_deleted'])); if (empty($commentSyncRecord)) { if (!$xfPostIsDeleted) { if (empty($xfPost['post']['post_is_first_post'])) { // create a new comment if ($optionSyncComment && xfac_syncComment_pullComment($config, $xfPost['post'], $postSyncRecord->sync_id, 'subscription') > 0) { return 'created new comment'; } } else { $recordPostUpdateDate = 0; $xfPostUpdateDate = 0; if (isset($postSyncRecord->syncData['thread']['first_post']['post_update_date'])) { $recordPostUpdateDate = $postSyncRecord->syncData['thread']['first_post']['post_update_date']; } if (isset($xfPost['post']['post_update_date'])) { $xfPostUpdateDate = $xfPost['post']['post_update_date']; } if ($recordPostUpdateDate > 0 && $xfPostUpdateDate > 0) { if ($xfPostUpdateDate <= $recordPostUpdateDate) { // the new post is not newer than the post in record // we used XenForo server time (which uses GMT) so it should be correct return false; } } // update the WordPress post $postContent = xfac_api_filterHtmlFromXenForo($xfPost['post']['post_body_html']); // remove the link back, if any $wfPostLink = get_permalink($postSyncRecord->sync_id); $postContent = preg_replace('#<a href="' . preg_quote($wfPostLink, '#') . '"[^>]*>[^<]+</a>$#', '', $postContent); $XFAC_SKIP_xfac_save_post_before = !empty($GLOBALS['XFAC_SKIP_xfac_save_post']); $GLOBALS['XFAC_SKIP_xfac_save_post'] = true; $postUpdated = 0; if ($optionSyncPost) { $postUpdated = wp_update_post(array('ID' => $postSyncRecord->sync_id, 'post_content' => $postContent)); } $GLOBALS['XFAC_SKIP_xfac_save_post'] = $XFAC_SKIP_xfac_save_post_before; if (is_int($postUpdated) and $postUpdated > 0) { return 'updated post'; } } } } else { if (!$xfPostIsDeleted) { // update comment content and approve it automatically $commentContent = xfac_api_filterHtmlFromXenForo($xfPost['post']['post_body_html']); $XFAC_SKIP_xfac_save_comment_before = !empty($GLOBALS['XFAC_SKIP_xfac_save_comment']); $GLOBALS['XFAC_SKIP_xfac_save_comment'] = true; $commentUpdated = 0; if ($optionSyncComment) { $commentUpdated = wp_update_comment(array('comment_ID' => $commentSyncRecord->sync_id, 'comment_content' => $commentContent, 'comment_approved' => 1)); } $GLOBALS['XFAC_SKIP_xfac_save_comment'] = $XFAC_SKIP_xfac_save_comment_before; if ($commentUpdated > 0) { return 'updated comment'; } } else { // check for comment current status and unapprove it $wpComment = get_comment($commentSyncRecord->sync_id); if (!empty($wpComment->comment_approved)) { $XFAC_SKIP_xfac_save_comment_before = !empty($GLOBALS['XFAC_SKIP_xfac_save_comment']); $GLOBALS['XFAC_SKIP_xfac_save_comment'] = true; $commentUpdated = 0; if ($optionSyncComment) { $commentUpdated = wp_update_comment(array('comment_ID' => $commentSyncRecord->sync_id, 'comment_approved' => 0)); } $GLOBALS['XFAC_SKIP_xfac_save_comment'] = $XFAC_SKIP_xfac_save_comment_before; if ($commentUpdated > 0) { return 'unapproved comment'; } } else { return 'comment is unapproved'; } } } return false; }