Ejemplo n.º 1
0
 /**
  * Helper method for indexing posts
  *
  * @param bool $no_bulk disable bulk indexing
  * @param int $posts_per_page
  * @param int $offset
  * @param bool $show_bulk_errors whether or not to show bulk error messages.
  *
  * @since 0.9
  * @return array
  */
 private function _index_helper($no_bulk = false, $posts_per_page, $offset = 0, $show_bulk_errors = false)
 {
     global $wpdb, $wp_object_cache;
     $synced = 0;
     $errors = array();
     while (true) {
         $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' => $offset, 'ignore_sticky_posts' => true));
         $query = new WP_Query($args);
         if ($query->have_posts()) {
             while ($query->have_posts()) {
                 $query->the_post();
                 if ($no_bulk) {
                     // index the posts one-by-one. not sure why someone may want to do this.
                     $result = ep_sync_post(get_the_ID());
                 } else {
                     $result = $this->queue_post(get_the_ID(), $query->post_count, $show_bulk_errors);
                 }
                 if (!$result) {
                     $errors[] = get_the_ID();
                 } else {
                     $synced++;
                 }
             }
         } else {
             break;
         }
         WP_CLI::log('Indexed ' . ($query->post_count + $offset) . '/' . $query->found_posts . ' entries. . .');
         $offset += $posts_per_page;
         usleep(500);
         // Avoid running out of memory
         $wpdb->queries = array();
         if (is_object($wp_object_cache)) {
             $wp_object_cache->group_ops = array();
             $wp_object_cache->stats = array();
             $wp_object_cache->memcache_debug = array();
             $wp_object_cache->cache = array();
             if (is_callable($wp_object_cache, '__remoteset')) {
                 call_user_func(array($wp_object_cache, '__remoteset'));
                 // important
             }
         }
     }
     if (!$no_bulk) {
         $this->send_bulk_errors();
     }
     wp_reset_postdata();
     return array('synced' => $synced, 'errors' => $errors);
 }
Ejemplo n.º 2
0
 /**
  * Test invalid post date time
  *
  * @param   array $post_statuses
  * @return  array
  */
 public function testPostInvalidDateTime()
 {
     add_filter('ep_indexable_post_status', array($this, 'mock_indexable_post_status'), 10, 1);
     $post_id = ep_create_and_sync_post(array('post_status' => 'draft'));
     ep_refresh_index();
     ep_sync_post($post_id);
     wp_cache_flush();
     $wp_post = get_post($post_id);
     $post = ep_get_post($post_id);
     $invalid_datetime = "0000-00-00 00:00:00";
     if ($wp_post->post_date_gmt == $invalid_datetime) {
         $this->assertNull($post['post_date_gmt']);
     }
     if ($wp_post->post_modified_gmt == $invalid_datetime) {
         $this->assertNull($post['post_modified_gmt']);
     }
     $this->assertNotNull($post);
     remove_filter('ep_indexable_post_status', array($this, 'mock_indexable_post_status'), 10);
 }
Ejemplo n.º 3
0
 /**
  * Helper method for indexing posts
  *
  * @param array $args
  *
  * @since 0.9
  * @return array
  */
 private function _index_helper($args)
 {
     $synced = 0;
     $errors = array();
     $no_bulk = false;
     if (isset($args['nobulk'])) {
         $no_bulk = true;
     }
     $show_bulk_errors = false;
     if (isset($args['show-bulk-errors'])) {
         $show_bulk_errors = true;
     }
     $posts_per_page = 350;
     if (!empty($args['posts-per-page'])) {
         $posts_per_page = absint($args['posts-per-page']);
     }
     $offset = 0;
     if (!empty($args['offset'])) {
         $offset = absint($args['offset']);
     }
     $post_type = ep_get_indexable_post_types();
     if (!empty($args['post-type'])) {
         $post_type = explode(',', $args['post-type']);
         $post_type = array_map('trim', $post_type);
     }
     if (is_array($post_type)) {
         $post_type = array_values($post_type);
     }
     /**
      * Create WP_Query here and reuse it in the loop to avoid high memory consumption.
      */
     $query = new WP_Query();
     while (true) {
         $args = apply_filters('ep_index_posts_args', array('posts_per_page' => $posts_per_page, 'post_type' => $post_type, 'post_status' => ep_get_indexable_post_status(), 'offset' => $offset, 'ignore_sticky_posts' => true, 'orderby' => 'ID', 'order' => 'DESC'));
         $query->query($args);
         if ($query->have_posts()) {
             while ($query->have_posts()) {
                 $query->the_post();
                 if ($no_bulk) {
                     // index the posts one-by-one. not sure why someone may want to do this.
                     $result = ep_sync_post(get_the_ID());
                     do_action('ep_cli_post_index', get_the_ID());
                 } else {
                     $result = $this->queue_post(get_the_ID(), $query->post_count, $show_bulk_errors);
                 }
                 if (!$result) {
                     $errors[] = get_the_ID();
                 } elseif (true === $result) {
                     $synced++;
                 }
             }
         } else {
             break;
         }
         WP_CLI::log('Processed ' . ($query->post_count + $offset) . '/' . $query->found_posts . ' entries. . .');
         $offset += $posts_per_page;
         usleep(500);
         // Avoid running out of memory
         $this->stop_the_insanity();
     }
     if (!$no_bulk) {
         $this->send_bulk_errors();
     }
     wp_reset_postdata();
     return array('synced' => $synced, 'errors' => $errors);
 }