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_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; }