function xfac_syncComment_processPostSyncRecord($config, $postSyncRecord) { $page = 1; $pulledSomething = false; if (time() - $postSyncRecord->sync_date < 60) { // do not try to sync every minute... return false; } if (!empty($postSyncRecord->syncData['subscribed'])) { if (time() - $postSyncRecord->sync_date < 86400) { // do not try to sync every day with subscribed thread return false; } } $wpUserData = xfac_user_getUserDataByApiData($config['root'], $postSyncRecord->syncData['thread']['creator_user_id']); $accessToken = xfac_user_getAccessToken($wpUserData->ID); $xfPosts = xfac_api_getPostsInThread($config, $postSyncRecord->provider_content_id, $accessToken); if (empty($xfPosts['subscription_callback']) and !empty($xfPosts['_headerLinkHub'])) { if (xfac_api_postSubscription($config, $accessToken, $xfPosts['_headerLinkHub'])) { $postSyncRecord->syncData['subscribed'] = array('hub' => $xfPosts['_headerLinkHub'], 'time' => time()); xfac_sync_updateRecord('', $postSyncRecord->provider_content_type, $postSyncRecord->provider_content_id, $postSyncRecord->sync_id, 0, $postSyncRecord->syncData); xfac_log('xfac_syncComment_processPostSyncRecord subscribed for posts in thread (#%d)', $postSyncRecord->provider_content_id); } else { xfac_log('xfac_syncComment_processPostSyncRecord failed subscribing for posts in thread (#%d)', $postSyncRecord->provider_content_id); } } if (empty($xfPosts['posts'])) { return false; } $xfPostIds = array(); foreach ($xfPosts['posts'] as $xfPost) { $xfPostIds[] = $xfPost['post_id']; } $commentSyncRecords = xfac_sync_getRecordsByProviderTypeAndIds('', 'post', $xfPostIds); foreach ($xfPosts['posts'] as $xfPost) { if (!empty($xfPost['post_is_first_post'])) { // do not pull first post continue; } $synced = false; foreach ($commentSyncRecords as $commentSyncRecord) { if ($commentSyncRecord->provider_content_id == $xfPost['post_id']) { $synced = true; } } if (!$synced) { $commentId = xfac_syncComment_pullComment($config, $xfPost, $postSyncRecord->sync_id); if ($commentId > 0) { $pulledSomething = true; } } } if ($pulledSomething) { xfac_sync_updateRecordDate($postSyncRecord); } return $pulledSomething; }
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; }