/** * Clean up after each test. Reset our mocks * * @since 0.1.0 */ public function tearDown() { parent::tearDown(); $this->fired_actions = array(); $sites = ep_get_sites(); $indexes = array(); foreach ($sites as $site) { switch_to_blog($site['blog_id']); ep_delete_index(); restore_current_blog(); } ep_delete_network_alias(); }
/** * Setup each test. * * @since 0.1.0 */ public function setUp() { global $wpdb; parent::setUp(); $wpdb->suppress_errors(); $admin_id = $this->factory->user->create(array('role' => 'administrator')); wp_set_current_user($admin_id); ep_delete_index(); ep_put_mapping(); ep_activate(); EP_WP_Query_Integration::factory()->setup(); $this->setup_test_post_type(); }
/** * Setup each test. * * @since 0.1.0 */ public function setUp() { global $wpdb; parent::setUp(); $wpdb->suppress_errors(); $admin_id = $this->factory->user->create(array('role' => 'administrator')); wp_set_current_user($admin_id); ep_delete_index(); ep_put_mapping(); EP_WP_Query_Integration::factory()->setup(); EP_Sync_Manager::factory()->setup(); EP_Sync_Manager::factory()->sync_post_queue = array(); $this->setup_test_post_type(); /** * Most of our search test are bundled into core tests for legacy reasons */ ep_activate_module('search'); EP_Modules::factory()->setup_modules(); }
/** * Test case for when index is deleted, request for Elasticsearch should fall back to WP Query */ public function testSearchIndexDeleted() { global $wpdb; ep_activate_module('search'); EP_Modules::factory()->setup_modules(); $post_ids = array(); ep_create_and_sync_post(); ep_create_and_sync_post(); ep_create_and_sync_post(array('post_content' => 'findme')); ep_refresh_index(); add_action('ep_wp_query_search', array($this, 'action_wp_query_search'), 10, 0); $args = array('s' => 'findme'); $query = new WP_Query($args); $this->assertTrue("SELECT * FROM {$wpdb->posts} WHERE 1=0" == $query->request); ep_delete_index(); $query = new WP_Query($args); $this->assertTrue("SELECT * FROM {$wpdb->posts} WHERE 1=0" != $query->request); }
/** * Delete the current index. !!Warning!! This removes your elasticsearch index for the entire site. * * @todo replace this function with one that updates all rows with a --force option * @synopsis [--network-wide] * @subcommand delete-index * @since 0.9 * * @param array $args * @param array $assoc_args */ public function delete_index($args, $assoc_args) { $this->_connect_check(); if (isset($assoc_args['network-wide']) && is_multisite()) { if (!is_numeric($assoc_args['network-wide'])) { $assoc_args['network-wide'] = 0; } $sites = ep_get_sites($assoc_args['network-wide']); foreach ($sites as $site) { switch_to_blog($site['blog_id']); WP_CLI::line(sprintf(__('Deleting index for site %d...', 'elasticpress'), (int) $site['blog_id'])); $result = ep_delete_index(); if ($result) { WP_CLI::success(__('Index deleted', 'elasticpress')); } else { WP_CLI::error(__('Delete index failed', 'elasticpress')); } restore_current_blog(); } } else { WP_CLI::line(__('Deleting index...', 'elasticpress')); $result = ep_delete_index(); if ($result) { WP_CLI::success(__('Index deleted', 'elasticpress')); } else { WP_CLI::error(__('Index delete failed', 'elasticpress')); } } }
public function action_delete_blog_from_index($blog_id) { if (ep_index_exists(ep_get_index_name($blog_id)) && !apply_filters('ep_keep_index', false)) { ep_delete_index(ep_get_index_name($blog_id)); } }
/** * 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); }
/** * Cluster status * * Test cluster status. * * @since 0.1.0 * * @group BBPE-251 * * @return void */ function testGetClusterStatus() { $status_indexed = ep_get_cluster_status(); ep_delete_index(); $status_unindexed = ep_get_cluster_status(); $this->setUp(); if (is_array($status_indexed)) { $this->assertTrue($status_indexed['status']); } else { $this->assertTrue(isset($status_indexed->cluster_name)); } if (is_array($status_unindexed)) { $this->assertTrue($status_unindexed['status']); } else { $this->assertTrue(isset($status_unindexed->cluster_name)); } }