function _xfac_save_comment($comment) { if (!empty($GLOBALS['XFAC_SKIP_xfac_save_comment'])) { return array(); } $postSyncRecords = xfac_sync_getRecordsByProviderTypeAndSyncId('', 'thread', $comment->comment_post_ID); $commentSyncRecords = xfac_sync_getRecordsByProviderTypeAndSyncId('', 'post', $comment->comment_ID); $xfPosts = array(); foreach ($postSyncRecords as $postSyncRecord) { $commentSyncRecord = null; foreach ($commentSyncRecords as $_commentSyncRecord) { if (!empty($_commentSyncRecord->syncData['post']['thread_id']) and $_commentSyncRecord->syncData['post']['thread_id'] == $postSyncRecord->provider_content_id) { $commentSyncRecord = $_commentSyncRecord; } } $xfPost = xfac_syncComment_pushComment($comment, $postSyncRecord, $commentSyncRecord); if (!empty($xfPost)) { $xfPosts[] = $xfPost; } } if (empty($xfPosts)) { // not pushed yet $config = xfac_option_getConfig(); if (!empty($config)) { xfac_search_indexComment($config, $comment); } } return $xfPosts; }
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)))); }