/** * Get all sites, count indexes * * @return array total index count with last blog id to manipulate blog with an index */ function ep_count_indexes() { $sites = ep_get_sites(); $count_indexes = 0; foreach ($sites as $site) { if ($index_name = ep_get_index_name($site['blog_id'])) { if (ep_index_exists($index_name)) { $count_indexes++; $last_blog_id_with_index = $site['blog_id']; } } } return array('total_indexes' => $count_indexes, 'last_blog_id_with_index' => $last_blog_id_with_index); }
/** * Test a simple post sync * * @since 0.9 */ public function testPostSync() { $sites = ep_get_sites(); foreach ($sites as $site) { switch_to_blog($site['blog_id']); add_action('ep_sync_on_transition', array($this, 'action_sync_on_transition'), 10, 0); $post_id = epa_create_and_sync_post(); ep_refresh_index(); // $this->assertTrue( ! empty( $this->fired_actions['ep_sync_on_transition'] ) ); $post = ep_get_post($post_id); $this->assertTrue(!empty($post)); $this->fired_actions = array(); restore_current_blog(); } }
/** * Get stats on the current index. * * @since 0.9.2 */ public function stats() { $this->_connect_check(); $request_args = array('headers' => ep_format_request_headers()); $request = wp_remote_get(trailingslashit(ep_get_host(true)) . '_stats/', $request_args); if (is_wp_error($request)) { WP_CLI::error(implode("\n", $request->get_error_messages())); } $body = json_decode(wp_remote_retrieve_body($request), true); $sites = is_multisite() ? ep_get_sites() : array('blog_id' => get_current_blog_id()); foreach ($sites as $site) { $current_index = ep_get_index_name($site['blog_id']); if (isset($body['indices'][$current_index])) { WP_CLI::log('====== Stats for: ' . $current_index . " ======"); WP_CLI::log('Documents: ' . $body['indices'][$current_index]['total']['docs']['count']); WP_CLI::log('Index Size: ' . size_format($body['indices'][$current_index]['total']['store']['size_in_bytes'], 2)); WP_CLI::log('====== End Stats ======'); } else { WP_CLI::warning($current_index . ' is not currently indexed.'); } } }
/** * 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); }
/** * Test index_exists helper function */ public function testIndexExists() { $sites = ep_get_sites(); $first_site_index = ep_get_index_name($sites[0]['blog_id']); $index_should_exist = ep_index_exists($first_site_index); $index_should_not_exist = ep_index_exists($first_site_index . 2); $this->assertTrue($index_should_exist); $this->assertFalse($index_should_not_exist); }
/** * Get stats on the current index. * * @since 0.9.2 */ public function stats() { $this->_connect_check(); $sites = is_multisite() ? ep_get_sites() : array('blog_id' => get_current_blog_id()); foreach ($sites as $site) { $stats_map = ep_stats($site['blog_id']); $current_index = ep_get_index_name($site['blog_id']); if (isset($stats_map)) { \WP_CLI::log('====== Stats for: ' . $current_index . " ======"); \WP_CLI::log('Documents: ' . $stats_map['total']['docs']['count']); \WP_CLI::log('Index Size: ' . size_format($stats_map['total']['store']['size_in_bytes'], 2)); \WP_CLI::log('====== End Stats ======'); } else { \WP_CLI::warning($current_index . ' is not currently indexed.'); } } }
/** * Test filter for skipping query integration * * @since 1.2 */ public function testQueryIntegrationSkip() { $main_post_id = $this->factory->post->create(); query_posts(array('p' => $main_post_id)); $GLOBALS['wp_the_query'] = $GLOBALS['wp_query']; $sites = ep_get_sites(); $i = 0; foreach ($sites as $site) { switch_to_blog($site['blog_id']); ep_create_and_sync_post(array('post_title' => 'findme')); ep_create_and_sync_post(array('post_title' => 'findme')); if ($i > 0) { ep_create_and_sync_post(array('post_title' => 'notfirstblog')); } elseif ($i === 0) { ep_create_and_sync_post(array('post_title' => 'firstblog')); } ep_refresh_index(); restore_current_blog(); $i++; } add_filter('ep_skip_query_integration', '__return_true'); $args = array('s' => 'notfirstblog', 'sites' => 'all'); $query = new WP_Query($args); $this->assertTrue(empty($query->posts)); }