| 1 |
<?php |
| 2 |
/** |
| 3 |
* Elasticsearch health check |
| 4 |
* |
| 5 |
* @since 3.6.0 |
| 6 |
* @package elasticpress |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ElasticPress\HealthCheck; |
| 10 |
|
| 11 |
use ElasticPress\Elasticsearch; |
| 12 |
use ElasticPress\HealthCheck; |
| 13 |
use ElasticPress\Utils; |
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
// @codeCoverageIgnoreStart |
| 17 |
exit; // Exit if accessed directly. |
| 18 |
// @codeCoverageIgnoreEnd |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* HealthCheckElasticsearch class |
| 23 |
*/ |
| 24 |
class HealthCheckElasticsearch extends HealthCheck { |
| 25 |
|
| 26 |
/** |
| 27 |
* Create Elasticsearch health check. |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
$this->test_name = 'elasticpress-health-check-elasticsearch'; |
| 31 |
$this->async = true; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Runs the test. |
| 36 |
* |
| 37 |
* @return array Data about the result of the test. |
| 38 |
*/ |
| 39 |
public function run() { |
| 40 |
$result = [ |
| 41 |
'label' => esc_html__( 'Your site can connect to Elasticsearch.', 'elasticpress' ), |
| 42 |
'status' => 'good', |
| 43 |
'badge' => [ |
| 44 |
'label' => esc_html__( 'ElasticPress', 'elasticpress' ), |
| 45 |
'color' => 'green', |
| 46 |
], |
| 47 |
'description' => esc_html__( 'You can have a fast and flexible search and query engine for WordPress using ElasticPress.', 'elasticpress' ), |
| 48 |
'actions' => '', |
| 49 |
'test' => $this->test_name, |
| 50 |
]; |
| 51 |
|
| 52 |
$host = Utils\get_host(); |
| 53 |
|
| 54 |
$elasticpress_settings_url = defined( 'EP_IS_NETWORK' ) && EP_IS_NETWORK ? admin_url( 'network/admin.php?page=elasticpress-settings' ) : admin_url( 'admin.php?page=elasticpress-settings' ); |
| 55 |
|
| 56 |
if ( empty( $host ) ) { |
| 57 |
$result['label'] = esc_html__( 'Your site could not connect to Elasticsearch', 'elasticpress' ); |
| 58 |
$result['status'] = 'critical'; |
| 59 |
$result['badge']['color'] = 'red'; |
| 60 |
$result['description'] = esc_html__( 'The Elasticsearch host is not set.', 'elasticpress' ); |
| 61 |
$result['actions'] = sprintf( |
| 62 |
'<p><a href="%s">%s</a></p>', |
| 63 |
esc_url( $elasticpress_settings_url ), |
| 64 |
esc_html__( 'Add a host', 'elasticpress' ) |
| 65 |
); |
| 66 |
} elseif ( ! Elasticsearch::factory()->get_elasticsearch_version( true ) ) { |
| 67 |
$result['label'] = esc_html__( 'Your site could not connect to Elasticsearch', 'elasticpress' ); |
| 68 |
$result['status'] = 'critical'; |
| 69 |
$result['badge']['color'] = 'red'; |
| 70 |
$result['actions'] = sprintf( |
| 71 |
'<p><a href="%s">%s</a></p>', |
| 72 |
esc_url( $elasticpress_settings_url ), |
| 73 |
esc_html__( 'Update your settings', 'elasticpress' ) |
| 74 |
); |
| 75 |
|
| 76 |
if ( Utils\is_epio() ) { |
| 77 |
$result['description'] = esc_html__( 'Check if your credentials to ElasticPress.io host are correct.', 'elasticpress' ); |
| 78 |
} else { |
| 79 |
$result['description'] = esc_html__( 'Check if your Elasticsearch host URL is correct and you have the right access to the host.', 'elasticpress' ); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return $result; |
| 84 |
} |
| 85 |
} |
| 86 |
|