Example #1
0
function xfac_search_indexPost($config, $postId, $latestSyncDate = 0)
{
    $post = get_post($postId);
    if ($post->post_type !== 'post' || $post->post_status !== 'publish') {
        return false;
    }
    $date = mysql2date('U', $post->post_date_gmt);
    if ($date <= $latestSyncDate) {
        return false;
    }
    $accessToken = xfac_user_getAccessToken($post->post_author);
    if (empty($accessToken)) {
        $accessToken = xfac_user_getSystemAccessToken($config, true);
    }
    $body = _xfac_syncPost_getPostBody($post, false);
    $link = get_permalink($post);
    return xfac_api_postSearchIndexing($config, $accessToken, 'post', $post->ID, $post->post_title, $body, $date, $link);
}
Example #2
0
function xfac_save_post($postId, WP_Post $post, $update)
{
    if (!empty($GLOBALS['XFAC_SKIP_xfac_save_post'])) {
        return;
    }
    if ($post->post_type != 'post') {
        return;
    }
    if ($post->post_status == 'publish') {
        $tagForumMappings = get_option('xfac_tag_forum_mappings');
        if (empty($tagForumMappings)) {
            return;
        }
        $forumIds = array();
        if (!empty($_POST['xfac_forum_id'])) {
            $forumIds[] = $_POST['xfac_forum_id'];
        }
        foreach ($tagForumMappings as $tagForumMapping) {
            if (!empty($tagForumMapping['term_id'])) {
                if (is_object_in_term($post->ID, 'post_tag', $tagForumMapping['term_id'])) {
                    $forumIds[] = $tagForumMapping['forum_id'];
                }
            }
        }
        if (!empty($forumIds)) {
            $accessToken = xfac_user_getAccessToken($post->post_author);
            if (!empty($accessToken)) {
                $config = xfac_option_getConfig();
                if (!empty($config)) {
                    $existingSyncRecords = xfac_sync_getRecordsByProviderTypeAndSyncId('', 'thread', $post->ID);
                    foreach ($existingSyncRecords as $existingSyncRecord) {
                        foreach (array_keys($forumIds) as $key) {
                            if (!empty($existingSyncRecord->syncData['thread']['forum_id']) and $existingSyncRecord->syncData['thread']['forum_id'] == $forumIds[$key]) {
                                unset($forumIds[$key]);
                                break;
                            }
                        }
                    }
                    $postBody = _xfac_syncPost_getPostBody($post);
                    foreach ($forumIds as $forumId) {
                        $thread = xfac_api_postThread($config, $accessToken, $forumId, $post->post_title, $postBody);
                        if (!empty($thread['thread']['thread_id'])) {
                            $subscribed = array();
                            if (intval(get_option('xfac_sync_comment_xf_wp')) > 0) {
                                $xfPosts = xfac_api_getPostsInThread($config, $thread['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']['thread_id'], $post->ID, 0, array('forumId' => $forumId, 'thread' => $thread['thread'], 'direction' => 'push', 'subscribed' => $subscribed));
                            xfac_log('xfac_save_post pushed to $forum (#%d) as $xfThread (#%d)', $forumId, $thread['thread']['thread_id']);
                        } else {
                            xfac_log('xfac_save_post failed pushing to $forum (#%d)', $forumId);
                        }
                    }
                    foreach ($existingSyncRecords as $existingSyncRecord) {
                        if (!empty($_POST['xfac_delete_sync']) and in_array($existingSyncRecord->provider_content_id, $_POST['xfac_delete_sync'])) {
                            // user chose to delete this sync record
                            xfac_sync_deleteRecord($existingSyncRecord);
                            if (!empty($existingSyncRecord->syncData['subscribed']['hub'])) {
                                xfac_api_postSubscription($config, $accessToken, $existingSyncRecord->syncData['subscribed']['hub'], 'unsubscribe');
                            }
                            continue;
                        }
                        if (empty($existingSyncRecord->syncData['thread']['first_post']['post_id'])) {
                            // no information about first post to update
                            continue;
                        }
                        $xfPost = xfac_api_putPost($config, $accessToken, $existingSyncRecord->syncData['thread']['first_post']['post_id'], $postBody, array('thread_title' => $post->post_title));
                        if (!empty($xfPost['post']['post_id'])) {
                            $syncData = $existingSyncRecord->syncData;
                            $syncData['direction'] = 'push';
                            $syncData['thread']['first_post'] = $xfPost['post'];
                            xfac_sync_updateRecord('', 'thread', $xfPost['post']['thread_id'], $post->ID, 0, $syncData);
                            xfac_log('xfac_save_post pushed an update for $xfPost (#%d)', $xfPost['post']['post_id']);
                        }
                    }
                }
            }
        }
    }
}