function xfac_tools_search_index() { /** @var wpdb $wpdb */ global $wpdb; $config = xfac_option_getConfig(); if (empty($config)) { wp_die(__('XenForo API configuration is missing.', 'xenforo-api-consumer')); } if (!xfac_api_hasModuleVersion($config, 'search/indexing', 2015091501)) { wp_die(__('Please update XenForo API to run this tool.', 'xenforo-api-consumer')); } $optionFilters = array('type' => array('filter' => FILTER_DEFAULT, 'default' => ''), 'position' => array('filter' => FILTER_VALIDATE_INT, 'default' => 0), 'limit' => array('filter' => FILTER_VALIDATE_INT, 'default' => 10)); $options = array(); foreach ($optionFilters as $optionKey => $optionFilter) { $optionValue = filter_input(INPUT_GET, $optionKey, $optionFilter['filter']); if (!empty($optionValue)) { $options[$optionKey] = $optionValue; } else { $options[$optionKey] = $optionFilter['default']; } } $contentTypes = preg_split('#[,\\s]#', $options['type'], -1, PREG_SPLIT_NO_EMPTY); $contentType = ''; $contentTable = ''; $contentIdField = ''; $syncProviderType = ''; while (true) { if (empty($contentTypes)) { die(__('Done.', 'xenforo-api-consumer')); } $contentType = reset($contentTypes); switch ($contentType) { case 'post': $contentTable = 'posts'; $contentIdField = 'ID'; $syncProviderType = 'thread'; break; case 'comment': $contentTable = 'comments'; $contentIdField = 'comment_ID'; $syncProviderType = 'post'; break; } $maxContentId = $wpdb->get_var("SELECT MAX({$contentIdField}) FROM {$wpdb->prefix}{$contentTable}"); if ($options['position'] < $maxContentId) { // position is good, break the while(true) and start working break; } $options['position'] = 0; array_shift($contentTypes); $options['type'] = implode(',', $contentTypes); } $contents = $wpdb->get_results($wpdb->prepare("\n SELECT {$contentIdField} AS ID\n FROM {$wpdb->prefix}{$contentTable}\n WHERE {$contentIdField} > %d\n LIMIT %d", array($options['position'], $options['limit']))); $contentIds = array(); foreach ($contents as $content) { $contentIds[] = $content->ID; } $syncRecords = xfac_sync_getRecordsByProviderTypeAndSyncIds('', $syncProviderType, $contentIds); foreach ($contents as $content) { $options['position'] = max($options['position'], $content->ID); $latestSyncDate = 0; foreach ($syncRecords as $syncRecord) { if ($syncRecord->sync_id == $content->ID) { $latestSyncDate = max($latestSyncDate, $syncRecord->sync_date); } } switch ($contentType) { case 'post': xfac_search_indexPost($config, $content->ID, $latestSyncDate); break; case 'comment': xfac_search_indexComment($config, $content->ID, $latestSyncDate); break; } } $optionsStr = ''; foreach ($options as $optionKey => $optionValue) { if ($optionValue !== $optionFilters[$optionKey]['default']) { $optionsStr .= sprintf('&%s=%s', $optionKey, rawurlencode($optionValue)); } } die(sprintf('<script>window.location = "%s";</script>', admin_url(sprintf('tools.php?action=xfac_tools_search_index%s', $optionsStr)))); }
function xfac_save_post($postId, WP_Post $post) { if (!empty($GLOBALS['XFAC_SKIP_xfac_save_post'])) { return; } if (empty($postId) || $post->post_type != 'post' || $post->post_status != 'publish') { return; } $config = xfac_option_getConfig(); if (empty($config)) { return; } $tagForumMappings = get_option('xfac_tag_forum_mappings'); $forumIds = array(); $pushed = false; if (!empty($_POST['xfac_forum_id'])) { $forumIds[] = $_POST['xfac_forum_id']; } if (!empty($tagForumMappings)) { 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)) { $existingSyncRecords = xfac_sync_getRecordsByProviderTypeAndSyncId('', 'thread', $post->ID); foreach ($existingSyncRecords as $existingSyncRecord) { foreach (array_keys($forumIds) as $key) { if (!empty($existingSyncRecord->syncData['thread']['forum_id']) && $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'])) { $pushed = true; $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'])) { $pushed = true; $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']); } } } } if (!$pushed) { xfac_search_indexPost($config, $post); } }