コード例 #1
0
ファイル: comment.php プロジェクト: burtay/bdApi
function xfac_syncComment_pushComment($wpComment, $postSyncRecord, $commentSyncRecord = null)
{
    $config = xfac_option_getConfig();
    if (empty($config)) {
        return null;
    }
    $accessToken = false;
    $extraParams = array();
    if ($wpComment->user_id > 0) {
        $accessToken = xfac_user_getAccessToken($wpComment->user_id);
    }
    if (empty($accessToken) and intval(get_option('xfac_sync_comment_wp_xf_as_guest')) > 0) {
        if (intval(get_option('xfac_xf_guest_account')) > 0) {
            // use pre-configured guest account
            $accessToken = xfac_user_getAccessToken(0);
        } else {
            // use one time token for guest
            $accessToken = xfac_api_generateOneTimeToken($config);
            $extraParams['guestUsername'] = $wpComment->comment_author;
        }
    }
    if (empty($accessToken)) {
        xfac_log('xfac_syncComment_pushComment skipped pushing $wpComment (#%d) because of missing $accessToken (user #%d)', $wpComment->comment_ID, $wpComment->user_id);
        return null;
    }
    $commentContent = $wpComment->comment_content;
    if (empty($commentSyncRecord)) {
        $xfPost = xfac_api_postPost($config, $accessToken, $postSyncRecord->provider_content_id, $commentContent, $extraParams);
        xfac_log('xfac_syncComment_pushComment pushed $wpComment (#%d)', $wpComment->comment_ID);
    } elseif ($wpComment->comment_approved) {
        $xfPost = xfac_api_putPost($config, $accessToken, $commentSyncRecord->provider_content_id, $commentContent, $extraParams);
        xfac_log('xfac_syncComment_pushComment pushed $wpComment (#%d) as an update', $wpComment->comment_ID);
    } else {
        $xfPost = xfac_api_deletePost($config, $accessToken, $commentSyncRecord->provider_content_id);
        xfac_log('xfac_syncComment_pushComment pushed $wpComment (#%d) as a delete', $wpComment->comment_ID);
    }
    if (!empty($xfPost['post']['post_id'])) {
        if (!empty($commentSyncRecord)) {
            xfac_sync_updateRecordDate($commentSyncRecord);
        } else {
            xfac_sync_updateRecord('', 'post', $xfPost['post']['post_id'], $wpComment->comment_ID, 0, array('post' => $xfPost['post'], 'direction' => 'push'));
        }
    } else {
        if (!empty($commentSyncRecord)) {
            xfac_sync_deleteRecord($commentSyncRecord);
        }
    }
    xfac_sync_updateRecordDate($postSyncRecord);
    return $xfPost;
}
コード例 #2
0
ファイル: post.php プロジェクト: burtay/bdApi
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']);
                        }
                    }
                }
            }
        }
    }
}