/** * Add the document mapping * * @synopsis [--network-wide] * @subcommand put-mapping * @since 0.9 * * @param array $args * @param array $assoc_args */ public function put_mapping($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(__('Adding mapping for site %d...', 'elasticpress'), (int) $site['blog_id'])); // Deletes index first ep_delete_index(); $result = ep_put_mapping(); if ($result) { WP_CLI::success(__('Mapping sent', 'elasticpress')); } else { WP_CLI::error(__('Mapping failed', 'elasticpress')); } restore_current_blog(); } } else { WP_CLI::line(__('Adding mapping...', 'elasticpress')); // Deletes index first $this->delete_index($args, $assoc_args); $result = ep_put_mapping(); if ($result) { WP_CLI::success(__('Mapping sent', 'elasticpress')); } else { WP_CLI::error(__('Mapping failed', 'elasticpress')); } } }
/** * 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')); $this->factory->blog->create_many(2, array('user_id' => $admin_id)); $sites = ep_get_sites(); $indexes = array(); foreach ($sites as $site) { switch_to_blog($site['blog_id']); ep_delete_index(); ep_put_mapping(); $indexes[] = ep_get_index_name(); restore_current_blog(); } ep_delete_network_alias(); ep_create_network_alias($indexes); wp_set_current_user($admin_id); EP_WP_Query_Integration::factory()->setup(); $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(); }
/** * 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(); }
/** * 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); }