/** * Queues up a post for bulk indexing * * @since 0.9.2 * * @param $post_id * @param $bulk_trigger * @param bool $show_bulk_errors true to show individual post error messages for bulk errors * * @return bool */ private function queue_post($post_id, $bulk_trigger, $show_bulk_errors = false) { static $post_count = 0; $post_args = ep_prepare_post($post_id); // Mimic EP_Sync_Manager::sync_post( $post_id ), otherwise posts can slip // through the kill filter... that would be bad! if (apply_filters('ep_post_sync_kill', false, $post_args, $post_id)) { return true; } // put the post into the queue $this->posts[$post_id][] = '{ "index": { "_id": "' . absint($post_id) . '" } }'; $this->posts[$post_id][] = addcslashes(json_encode($post_args), "\n"); // augment the counter ++$post_count; // if we have hit the trigger, initiate the bulk request if ($post_count === absint($bulk_trigger)) { $this->bulk_index($show_bulk_errors); // reset the post count $post_count = 0; // reset the posts $this->posts = array(); } return true; }
/** * Sync a post for a specific site or globally. * * @param int $post_id * @since 0.1.0 * @return bool|array */ public function sync_post($post_id) { $post_args = ep_prepare_post($post_id); if (apply_filters('ep_post_sync_kill', false, $post_args, $post_id)) { return; } $response = ep_index_post($post_args); return $response; }
/** * Queues up a post for bulk indexing * * @since 0.9.2 * * @param $post_id * @param $bulk_trigger * @param bool $show_bulk_errors true to show individual post error messages for bulk errors * * @return bool|int true if successfully synced, false if not or 2 if post was killed before sync */ private function queue_post($post_id, $bulk_trigger, $show_bulk_errors = false) { static $post_count = 0; static $killed_post_count = 0; $killed_post = false; $post_args = ep_prepare_post($post_id); // Mimic EP_Sync_Manager::sync_post( $post_id ), otherwise posts can slip // through the kill filter... that would be bad! if (apply_filters('ep_post_sync_kill', false, $post_args, $post_id)) { $killed_post_count++; $killed_post = true; // Save status for return. } else { // Post wasn't killed so process it. // put the post into the queue $this->posts[$post_id][] = '{ "index": { "_id": "' . absint($post_id) . '" } }'; if (function_exists('wp_json_encode')) { $this->posts[$post_id][] = addcslashes(wp_json_encode($post_args), "\n"); } else { $this->posts[$post_id][] = addcslashes(json_encode($post_args), "\n"); } // augment the counter ++$post_count; } // If we have hit the trigger, initiate the bulk request. if ($post_count + $killed_post_count === absint($bulk_trigger)) { // Don't waste time if we've killed all the posts. if (!empty($this->posts)) { $this->bulk_index($show_bulk_errors); } // reset the post count $post_count = 0; $killed_post_count = 0; // reset the posts $this->posts = array(); } if (true === $killed_post) { return 2; } return true; }
/** * Continue index * * @since 2.1 */ public function action_wp_ajax_ep_index() { if (!check_ajax_referer('ep_nonce', 'nonce', false)) { wp_send_json_error(); exit; } if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { $index_meta = get_site_option('ep_index_meta', false); } else { $index_meta = get_option('ep_index_meta', false); } $status = false; // No current index going on. Let's start over if (false === $index_meta) { $status = 'start'; $index_meta = array('offset' => 0, 'start' => true); if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { $sites = ep_get_sites(); $index_meta['site_stack'] = array(); foreach ($sites as $site) { $index_meta['site_stack'][] = array('url' => untrailingslashit($site['domain'] . $site['path']), 'id' => (int) $site['blog_id']); } $index_meta['current_site'] = array_shift($index_meta['site_stack']); } else { if (!apply_filters('ep_skip_index_reset', false, $index_meta)) { ep_delete_index(); ep_put_mapping(); } } if (!empty($_POST['module_sync'])) { $index_meta['module_sync'] = esc_attr($_POST['module_sync']); } } else { if (!empty($index_meta['site_stack']) && $index_meta['offset'] >= $index_meta['found_posts']) { $status = 'start'; $index_meta['start'] = true; $index_meta['offset'] = 0; $index_meta['current_site'] = array_shift($index_meta['site_stack']); } else { $index_meta['start'] = false; } } $index_meta = apply_filters('ep_index_meta', $index_meta); if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { switch_to_blog($index_meta['current_site']['id']); if (!empty($index_meta['start'])) { if (!apply_filters('ep_skip_index_reset', false, $index_meta)) { ep_delete_index(); ep_put_mapping(); } } } $posts_per_page = apply_filters('ep_index_posts_per_page', 350); do_action('ep_pre_dashboard_index', $index_meta, $status); $args = apply_filters('ep_index_posts_args', array('posts_per_page' => $posts_per_page, 'post_type' => ep_get_indexable_post_types(), 'post_status' => ep_get_indexable_post_status(), 'offset' => $index_meta['offset'], 'ignore_sticky_posts' => true, 'orderby' => 'ID', 'order' => 'DESC', 'fields' => 'all')); $query = new WP_Query($args); $index_meta['found_posts'] = $query->found_posts; if ($status !== 'start') { if ($query->have_posts()) { $queued_posts = array(); while ($query->have_posts()) { $query->the_post(); $killed_post_count = 0; $post_args = ep_prepare_post(get_the_ID()); if (apply_filters('ep_post_sync_kill', false, $post_args, get_the_ID())) { $killed_post_count++; } else { // Post wasn't killed so process it. $queued_posts[get_the_ID()][] = '{ "index": { "_id": "' . absint(get_the_ID()) . '" } }'; if (function_exists('wp_json_encode')) { $queued_posts[get_the_ID()][] = addcslashes(wp_json_encode($post_args), "\n"); } else { $queued_posts[get_the_ID()][] = addcslashes(json_encode($post_args), "\n"); } } } if (!empty($queued_posts)) { $flatten = array(); foreach ($queued_posts as $post) { $flatten[] = $post[0]; $flatten[] = $post[1]; } // make sure to add a new line at the end or the request will fail $body = rtrim(implode("\n", $flatten)) . "\n"; ep_bulk_index_posts($body); } $index_meta['offset'] = absint($index_meta['offset'] + $posts_per_page); if ($index_meta['offset'] >= $index_meta['found_posts']) { $index_meta['offset'] = $index_meta['found_posts']; } if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { update_site_option('ep_index_meta', $index_meta); } else { update_option('ep_index_meta', $index_meta); } } else { // We are done (with this site) if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { if (empty($index_meta['site_stack'])) { delete_site_option('ep_index_meta'); $sites = ep_get_sites(); $indexes = array(); foreach ($sites as $site) { switch_to_blog($site['blog_id']); $indexes[] = ep_get_index_name(); restore_current_blog(); } ep_create_network_alias($indexes); } else { $index_meta['offset'] = (int) $query->found_posts; } } else { $index_meta['offset'] = (int) $query->found_posts; delete_option('ep_index_meta'); } } } else { if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { update_site_option('ep_index_meta', $index_meta); } else { update_option('ep_index_meta', $index_meta); } } if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { restore_current_blog(); } wp_send_json_success($index_meta); }
/** * Queues up a post for bulk indexing * * @since 0.9.2 * * @param $post_id * @param $bulk_trigger * * @return bool */ private function queue_post($post_id, $bulk_trigger) { static $post_count = 0; // put the post into the queue $this->posts[$post_id][] = '{ "index": { "_id": "' . absint($post_id) . '" } }'; $this->posts[$post_id][] = addcslashes(json_encode(ep_prepare_post($post_id)), "\n"); // augment the counter ++$post_count; // if we have hit the trigger, initiate the bulk request if ($post_count === absint($bulk_trigger)) { $this->bulk_index(); // reset the post count $post_count = 0; // reset the posts $this->posts = array(); } return true; }
/** * Sync a post for a specific site or globally. * * @param int $post_id * @since 0.1.0 * @return bool|array */ public function sync_post($post_id) { $post_args = apply_filters('ep_post_sync_args', ep_prepare_post($post_id), $post_id); $response = ep_index_post($post_args); return $response; }