Ejemplo n.º 1
0
 /**
  * @codeCoverageIgnore
  */
 public function setup()
 {
     $url = get_site_url();
     $this->index = str_replace('/', '_', preg_replace('#^.*?//(.*?)/?$#', '$1', $url));
     $this->host = SP_Config()->get_setting('host');
     $this->request_defaults = array('sslverify' => false, 'timeout' => 10, 'user-agent' => 'SearchPress 0.1 for WordPress', 'reject_unsafe_urls' => false);
     # Increase the timeout for bulk indexing
     if (defined('DOING_CRON') && DOING_CRON || defined('WP_CLI') && WP_CLI) {
         $this->request_defaults['timeout'] = 60;
     }
 }
Ejemplo n.º 2
0
 /**
  * Flush the current index. !!Warning!! This empties your elasticsearch index for the entire site.
  *
  */
 public function flush()
 {
     WP_CLI::line("Flushing current index...");
     $result = SP_Config()->flush();
     if ('200' == SP_API()->last_request['response_code'] || '404' == SP_API()->last_request['response_code']) {
         WP_CLI::success("Successfully flushed Post index\n");
     } else {
         print_r(SP_API()->last_request);
         print_r($result);
         WP_CLI::error("Could not flush existing data!");
     }
 }
 function es_wp_query_index_test_data()
 {
     // If your ES server is not at localhost:9200, you need to set $_ENV['searchpress_host'].
     $host = !empty($_ENV['searchpress_host']) ? $_ENV['searchpress_host'] : 'http://localhost:9200';
     SP_Config()->update_settings(array('active' => false, 'host' => $host));
     SP_API()->index = 'es-wp-query-tests';
     SP_Config()->flush();
     SP_Config()->create_mapping();
     $posts = get_posts(array('posts_per_page' => -1, 'post_type' => 'any', 'post_status' => array_values(get_post_stati()), 'orderby' => 'ID', 'order' => 'ASC'));
     $sp_posts = array();
     foreach ($posts as $post) {
         $sp_posts[] = new SP_Post($post);
     }
     $response = SP_API()->index_posts($sp_posts);
     if ('200' != SP_API()->last_request['response_code']) {
         echo "ES response not 200!\n" . print_r($response, 1);
     } elseif (!is_object($response) || !is_array($response->items)) {
         echo "Error indexing data! Response:\n" . print_r($response, 1);
     }
     SP_Config()->update_settings(array('active' => true, 'must_init' => false));
     SP_API()->post('_refresh');
 }
Ejemplo n.º 4
0
 function test_settings()
 {
     $host = getenv('SEARCHPRESS_HOST');
     if (empty($host)) {
         $host = 'http://localhost:9200';
     }
     SP_Config()->settings = false;
     delete_option('sp_settings');
     $this->assertEquals('http://localhost:9200', SP_Config()->host());
     $this->assertTrue(SP_Config()->must_init());
     $this->assertFalse(SP_Config()->active());
     $this->assertFalse(SP_Config()->last_beat());
     SP_Config()->update_settings(array('active' => true, 'must_init' => false, 'host' => $host));
     $this->assertEquals($host, SP_Config()->host());
     $this->assertFalse(SP_Config()->must_init());
     $this->assertTrue(SP_Config()->active());
     SP_Config()->settings = false;
     delete_option('sp_settings');
     SP_Config()->update_settings(array('active' => true, 'must_init' => false, 'host' => $host));
     $this->assertEquals($host, SP_Config()->host());
     $this->assertFalse(SP_Config()->must_init());
     $this->assertTrue(SP_Config()->active());
 }
Ejemplo n.º 5
0
 /**
  * @codeCoverageIgnore
  */
 public function setup()
 {
     if (!is_admin() && SP_Config()->active()) {
         $this->init_hooks();
     }
 }
Ejemplo n.º 6
0
function sp_index_flush_data()
{
    SP_Config()->flush();
    SP_Config()->create_mapping();
}
Ejemplo n.º 7
0
 function test_singular_index_non_200()
 {
     // This domain is used in unit tests, and we'll get a 404 from trying to use it with ES
     SP_Config()->update_settings(array('host' => 'http://asdftestblog1.files.wordpress.com', 'active' => true));
     // Because we changed the host, we have to re-init SP_API
     SP_API()->setup();
     $posts = array($this->factory->post->create(array('post_title' => 'searchpress')));
     $this->assertNotEmpty(SP_Sync_Meta()->messages['error']);
     $this->assertTrue(SP_Sync_Meta()->has_errors());
 }
Ejemplo n.º 8
0
 public function admin_notices()
 {
     if (isset($_GET['page']) && 'searchpress' == $_GET['page']) {
         return;
     } elseif (SP_Sync_Meta()->running) {
         printf('<div class="updated"><p>%s <a href="%s">%s</a></p></div>', __('SearchPress sync is currently running.', 'searchpress'), admin_url('tools.php?page=searchpress'), __('View status', 'searchpress'));
     } elseif (SP_Config()->must_init()) {
         printf('<div class="updated error"><p>%s <a href="%s">%s</a></p></div>', __('SearchPress needs to be configured and synced before you can use it.', 'searchpress'), admin_url('tools.php?page=searchpress'), __('Go to SearchPress Settings', 'searchpress'));
     } elseif (SP_Sync_Meta()->has_errors()) {
         printf('<div class="updated error"><p>%s <a href="%s">%s</a></p></div>', __('SearchPress encountered an error while syncing.', 'searchpress'), admin_url('tools.php?page=searchpress#sp-log'), __('Go to Log', 'searchpress'));
     }
 }
Ejemplo n.º 9
0
 public function should_be_indexed()
 {
     # Check post type
     if (!in_array($this->data['post_type'], SP_Config()->sync_post_types())) {
         return false;
     }
     # Check post status
     if (!in_array($this->data['post_status'], SP_Config()->sync_statuses())) {
         return false;
     }
     return apply_filters('sp_post_should_be_indexed', true, $this);
 }
Ejemplo n.º 10
0
 /**
  * Do an indexing loop. This is the meat of the process.
  *
  * @return bool
  */
 public function do_index_loop()
 {
     $sync_meta = SP_Sync_Meta();
     $start = $sync_meta->page * $sync_meta->bulk;
     do_action('sp_debug', '[SP_Sync_Manager] Getting Range');
     $posts = $this->get_range($start, $sync_meta->bulk);
     // Reload the sync meta to ensure it hasn't been canceled while we were getting those posts
     $sync_meta->reload();
     if (!$posts || is_wp_error($posts) || !$sync_meta->running) {
         return false;
     }
     $response = SP_API()->index_posts($posts);
     do_action('sp_debug', sprintf('[SP_Sync_Manager] Indexed %d Posts', count($posts)), $response);
     $sync_meta->reload();
     if (!$sync_meta->running) {
         return false;
     }
     $sync_meta->processed += count($posts);
     if ('200' != SP_API()->last_request['response_code']) {
         # Should probably throw an error here or something
         $sync_meta->log(new WP_Error('error', __('ES response failed', 'searchpress'), SP_API()->last_request));
         $sync_meta->save();
         $this->cancel_reindex();
         return false;
     } elseif (!is_object($response) || !isset($response->items) || !is_array($response->items)) {
         $sync_meta->log(new WP_Error('error', __('Error indexing data', 'searchpress'), $response));
         $sync_meta->save();
         $this->cancel_reindex();
         return false;
     } else {
         foreach ($response->items as $post) {
             // Status should be 200 or 201, depending on if we're updating or creating respectively
             if (!isset($post->index->status)) {
                 $sync_meta->log(new WP_Error('warning', __("Error indexing post {$post->index->_id}; Response: " . json_encode($post), 'searchpress'), $post));
             } elseif (!in_array($post->index->status, array(200, 201))) {
                 $sync_meta->log(new WP_Error('warning', __("Error indexing post {$post->index->_id}; HTTP response code: {$post->index->status}", 'searchpress'), $post));
             } else {
                 $sync_meta->success++;
             }
         }
     }
     $this->total_pages = ceil($this->published_posts / $sync_meta->bulk);
     $sync_meta->page++;
     if (!defined('WP_CLI') || !WP_CLI) {
         if ($sync_meta->processed >= $sync_meta->total || $sync_meta->page > $this->total_pages) {
             SP_Config()->update_settings(array('active' => true));
             $this->cancel_reindex();
         } else {
             $sync_meta->save();
         }
     }
     return true;
 }