/**
  * Perform the bulk index operation
  *
  * @param bool $show_bulk_errors true to show individual post error messages for bulk errors
  *
  * @since 0.9.2
  */
 private function bulk_index($show_bulk_errors = false)
 {
     // monitor how many times we attempt to add this particular bulk request
     static $attempts = 0;
     // augment the attempts
     ++$attempts;
     // make sure we actually have something to index
     if (empty($this->posts)) {
         WP_CLI::error('There are no posts to index.');
     }
     $flatten = array();
     foreach ($this->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";
     // show the content length in bytes if in debug
     if (defined('WP_DEBUG') && WP_DEBUG) {
         WP_CLI::log('Request string length: ' . size_format(mb_strlen($body, '8bit'), 2));
     }
     // decode the response
     $response = ep_bulk_index_posts($body);
     if (is_wp_error($response)) {
         WP_CLI::error(implode("\n", $response->get_error_messages()));
     }
     // if we did have errors, try to add the documents again
     if (isset($response['errors']) && $response['errors'] === true) {
         if ($attempts < 5) {
             foreach ($response['items'] as $item) {
                 if (empty($item['index']['error'])) {
                     unset($this->posts[$item['index']['_id']]);
                 }
             }
             $this->bulk_index($show_bulk_errors);
         } else {
             foreach ($response['items'] as $item) {
                 if (!empty($item['index']['_id'])) {
                     $this->failed_posts[] = $item['index']['_id'];
                     if ($show_bulk_errors) {
                         $this->failed_posts_message[$item['index']['_id']] = $item['index']['error'];
                     }
                 }
             }
             $attempts = 0;
         }
     } else {
         // there were no errors, all the posts were added
         $attempts = 0;
     }
 }
 /**
  * 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);
 }