Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to site health #2084

Merged
merged 8 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions elasticpress.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function( $class ) {

require_once __DIR__ . '/includes/compat.php';
require_once __DIR__ . '/includes/utils.php';
require_once __DIR__ . '/includes/health-check.php';

// Define a constant if we're network activated to allow plugin to respond accordingly.
$network_activated = Utils\is_network_activated( EP_FILE );
Expand Down
117 changes: 117 additions & 0 deletions includes/classes/HealthCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* HealthCheck class.
*
* All health checkers extend this class.
*
* @since 3.6.0
* @package elasticpress
*/

namespace ElasticPress;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* HealthCheck abstract class
*/
abstract class HealthCheck {
/**
* The name of the test.
*
* @var string
*/
protected $test_name = '';

/**
* Test should run via Ajax calls after page load.
*
* @var bool True when is async, default false.
*/
protected $async = false;

/**
* Runs the test and returns the result.
*/
abstract public function run();

/**
* Gets the test name.
*
* @return string The test name.
*/
protected function get_test_name() {
return $this->test_name;
}

/**
* Checks if the health check is async.
*
* @return bool True when check is async.
*/
protected function is_async() {
return ! empty( $this->async );
}

/**
* Registers the test to WordPress.
*/
public function register_test() {
if ( $this->is_async() ) {
add_filter( 'site_status_tests', [ $this, 'add_async_test' ] );

add_action( 'wp_ajax_health-check-' . $this->get_test_name(), [ $this, 'get_test_result' ] );

return;
}

add_filter( 'site_status_tests', [ $this, 'add_direct_test' ] );
}

/**
* Adds to the direct tests list.
*
* @param array $tests Array with the current tests.
*
* @return array
*/
public function add_direct_test( $tests ) {
$tests['direct'][ $this->get_test_name() ] = [
'test' => [ $this, 'get_test_result' ],
];

return $tests;
}

/**
* Adds to the async tests list.
*
* @param array $tests Array with the current tests.
*
* @return array
*/
public function add_async_test( $tests ) {
$tests['async'][ $this->get_test_name() ] = [
'test' => $this->get_test_name(),
];

return $tests;
}

/**
* Gets the result of test.
*
* @return array|void
*/
public function get_test_result() {
$result = $this->run();

if ( $this->is_async() ) {
wp_send_json_success( $result );
} else {
return $result;
}
}
}
83 changes: 83 additions & 0 deletions includes/classes/HealthCheck/HealthCheckElasticsearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Elasticsearch health check
*
* @since 3.6.0
* @package elasticpress
*/

namespace ElasticPress\HealthCheck;

use ElasticPress\HealthCheck as HealthCheck;
use ElasticPress\Utils as Utils;
use ElasticPress\Elasticsearch as Elasticsearch;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* HealthCheckElasticsearch class
*/
class HealthCheckElasticsearch extends HealthCheck {

/**
* Create Elasticsearch health check.
*/
public function __construct() {
$this->test_name = 'elasticpress-health-check-elasticsearch';
$this->async = true;
}

/**
* Runs the test.
*
* @return array Data about the result of the test.
*/
public function run() {
$result = [
'label' => esc_html__( 'Your site can connect to Elasticsearch.', 'elasticpress' ),
'status' => 'good',
'badge' => [
'label' => esc_html__( 'ElasticPress', 'elasticpress' ),
'color' => 'green',
],
'description' => esc_html__( 'You can have a fast and flexible search and query engine for WordPress using ElasticPress.', 'elasticpress' ),
'actions' => '',
'test' => $this->test_name,
];

$host = Utils\get_host();

$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' );

if ( empty( $host ) ) {
$result['label'] = esc_html__( 'Your site could not connect to Elasticsearch', 'elasticpress' );
$result['status'] = 'critical';
$result['badge']['color'] = 'red';
$result['description'] = esc_html__( 'The Elasticsearch host is not set.', 'elasticpress' );
$result['actions'] = sprintf(
'<p><a href="%s">%s</a></p>',
esc_url( $elasticpress_settings_url ),
esc_html__( 'Add a host', 'elasticpress' )
);
} elseif ( ! Elasticsearch::factory()->get_elasticsearch_version( true ) ) {
$result['label'] = esc_html__( 'Your site could not connect to Elasticsearch', 'elasticpress' );
$result['status'] = 'critical';
$result['badge']['color'] = 'red';
$result['actions'] = sprintf(
'<p><a href="%s">%s</a></p>',
esc_url( $elasticpress_settings_url ),
esc_html__( 'Update your settings', 'elasticpress' )
);

if ( Utils\is_epio() ) {
$result['description'] = esc_html__( 'Check if your credentials to ElasticPress.io host are correct.', 'elasticpress' );
} else {
$result['description'] = esc_html__( 'Check if your Elasticsearch host URL is correct and you have the right access to the host.', 'elasticpress' );
}
}

return $result;
}
}
21 changes: 21 additions & 0 deletions includes/health-check.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Health check
*
* @package elasticpress
* @since 3.6.0
*/

namespace ElasticPress;

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

$health_checks = [
new HealthCheck\HealthCheckElasticsearch(),
];

foreach ( $health_checks as $health_check ) {
$health_check->register_test();
}