/** * Provide better error messaging for common connection errors * * @since 0.9.3 */ private function _connect_check() { $host = ep_get_host(); if (empty($host)) { WP_CLI::error(__('There is no Elasticsearch host set up. Either add one through the dashboard or define one in wp-config.php', 'elasticpress')); } elseif (!ep_elasticsearch_can_connect()) { WP_CLI::error(__('Unable to reach Elasticsearch Server! Check that service is running.', 'elasticpress')); } }
/** * 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.'); } } }
/** * Test get hosts method */ public function testGetHost() { global $ep_backup_host; //Check host constant $host_1 = ep_get_host(true); //Test only host in array $ep_backup_host = array('http://127.0.0.1:9200'); $host_2 = ep_get_host(true, true); //Test no good hosts $ep_backup_host = array('bad host 1', 'bad host 2'); $host_3 = ep_get_host(true, true); //Test good host 1st array item $ep_backup_host = array('http://127.0.0.1:9200', 'bad host 2'); $host_4 = ep_get_host(true, true); //Test good host last array item $ep_backup_host = array('bad host 1', 'http://127.0.0.1:9200'); $host_5 = ep_get_host(true, true); $this->assertInternalType('string', $host_1); $this->assertInternalType('string', $host_2); $this->assertWPError($host_3); $this->assertInternalType('string', $host_4); $this->assertInternalType('string', $host_5); }
/** * Wrapper for wp_remote_request * * This is a wrapper function for wp_remote_request that will switch to a backup server * (if present) should the primary EP host fail. * * @since 1.6 * * @param string $path Site URL to retrieve. * @param array $args Optional. Request arguments. Default empty array. * * @return WP_Error|array The response or WP_Error on failure. */ public function remote_request($path, $args = array()) { //The allowance of these variables makes testing easier. $force = false; $use_backups = false; //Add the API Header $args['headers'] = $this->format_request_headers(); if (defined('EP_FORCE_HOST_REFRESH') && true === EP_FORCE_HOST_REFRESH) { $force = true; } if (defined('EP_HOST_USE_ONLY_BACKUPS') && true === EP_HOST_USE_ONLY_BACKUPS) { $use_backups = true; } $host = ep_get_host($force, $use_backups); $request = false; if (!is_wp_error($host)) { // probably only reachable in testing but just to be safe $request = wp_remote_request(esc_url(trailingslashit($host) . $path), $args); //try the existing host to avoid unnecessary calls } //If we have a failure we'll try it again with a backup host if (false === $request || is_wp_error($request) || isset($request['response']['code']) && 200 !== $request['response']['code']) { $host = ep_get_host(true, $use_backups); if (is_wp_error($host)) { return $host; } return wp_remote_request(esc_url(trailingslashit($host) . $path), $args); } return $request; }
/** * Conditionally show dashboard or intro * * @since 2.1 */ public function intro_or_dashboard() { global $pagenow; if ('admin.php' !== $pagenow || empty($_GET['page']) || 'elasticpress' !== $_GET['page']) { return; } $host = ep_get_host(); if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { $intro_shown = get_site_option('ep_intro_shown', false); } else { $intro_shown = get_option('ep_intro_shown', false); } if (!$intro_shown) { if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { wp_redirect(admin_url('network/admin.php?page=elasticpress-intro')); } else { wp_redirect(admin_url('admin.php?page=elasticpress-intro')); } exit; } else { if (empty($host)) { if (defined('EP_IS_NETWORK') && EP_IS_NETWORK) { wp_redirect(admin_url('network/admin.php?page=elasticpress-intro')); } else { wp_redirect(admin_url('admin.php?page=elasticpress-intro')); } exit; } } }
<table class="form-table"> <tbody> <tr> <th scope="row"><label for="ep_host"><?php esc_html_e('Elasticsearch Host', 'elasticpress'); ?> </label></th> <td> <input <?php if (defined('EP_HOST') && EP_HOST) { ?> disabled<?php } ?> placeholder="http://" type="text" value="<?php echo esc_url(ep_get_host()); ?> " name="ep_host" id="ep_host"> <?php if (defined('EP_HOST') && EP_HOST) { ?> <span class="description"><?php esc_html_e('Your Elasticsearch host is set in wp-config.php', 'elasticpress'); ?> </span> <?php } else { ?> <span class="description"><?php _e('Plug in your Elasticsearch server here!', 'elasticpress'); ?>
/** * Retrieves search stats from Elasticsearch. * * Retrieves various search statistics from the ES server. * * @since 1.9 * * @param int $blog_id Id of blog to get stats. * * @return array Contains the status message or the returned statistics. */ public function get_search_status($blog_id = null) { if (is_wp_error(ep_get_host())) { return array('status' => false, 'msg' => esc_html__('Elasticsearch Host is not available.', 'elasticpress')); } else { if (is_multisite() && null === $blog_id) { $path = ep_get_network_alias() . '/_stats/search/'; } else { $path = ep_get_index_name($blog_id) . '/_stats/search/'; } $request = ep_remote_request($path, array('method' => 'GET')); } if (!is_wp_error($request)) { $stats = json_decode(wp_remote_retrieve_body($request)); if (isset($stats->_all)) { return $stats->_all->primaries->search; } return false; } return array('status' => false, 'msg' => $request->get_error_message()); }