PluginProbe
ElasticPress / 5.3.5
ElasticPress v5.3.5
5.3.5 5.3.4 3.6.5 3.6.6 4.0.0 4.0.1 4.1.0 4.2.0 4.2.1 4.2.2 4.3.0 4.3.1 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.7.0 4.7.1 4.7.2 5.0.0 5.0.1 5.0.2 All 108 releases
elasticpress / includes / classes / HealthCheck.php

HealthCheck.php in ElasticPress 5.3.5, at includes/classes/HealthCheck.php

118 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * HealthCheck class.
4 *
5 * All health checkers extend this class.
6 *
7 * @since 3.6.0
8 * @package elasticpress
9 */
10
11 namespace ElasticPress;
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit; // Exit if accessed directly.
15 }
16
17 /**
18 * HealthCheck abstract class
19 */
20 abstract class HealthCheck {
21 /**
22 * The name of the test.
23 *
24 * @var string
25 */
26 protected $test_name = '';
27
28 /**
29 * Test should run via Ajax calls after page load.
30 *
31 * @var bool True when is async, default false.
32 */
33 protected $async = false;
34
35 /**
36 * Runs the test and returns the result.
37 */
38 abstract public function run();
39
40 /**
41 * Gets the test name.
42 *
43 * @return string The test name.
44 */
45 protected function get_test_name() {
46 return $this->test_name;
47 }
48
49 /**
50 * Checks if the health check is async.
51 *
52 * @return bool True when check is async.
53 */
54 protected function is_async() {
55 return ! empty( $this->async );
56 }
57
58 /**
59 * Registers the test to WordPress.
60 */
61 public function register_test() {
62 if ( $this->is_async() ) {
63 add_filter( 'site_status_tests', [ $this, 'add_async_test' ] );
64
65 add_action( 'wp_ajax_health-check-' . $this->get_test_name(), [ $this, 'get_test_result' ] );
66
67 return;
68 }
69
70 add_filter( 'site_status_tests', [ $this, 'add_direct_test' ] );
71 }
72
73 /**
74 * Adds to the direct tests list.
75 *
76 * @param array $tests Array with the current tests.
77 *
78 * @return array
79 */
80 public function add_direct_test( $tests ) {
81 $tests['direct'][ $this->get_test_name() ] = [
82 'test' => [ $this, 'get_test_result' ],
83 ];
84
85 return $tests;
86 }
87
88 /**
89 * Adds to the async tests list.
90 *
91 * @param array $tests Array with the current tests.
92 *
93 * @return array
94 */
95 public function add_async_test( $tests ) {
96 $tests['async'][ $this->get_test_name() ] = [
97 'test' => $this->get_test_name(),
98 ];
99
100 return $tests;
101 }
102
103 /**
104 * Gets the result of test.
105 *
106 * @return array|void
107 */
108 public function get_test_result() {
109 $result = $this->run();
110
111 if ( $this->is_async() ) {
112 wp_send_json_success( $result );
113 } else {
114 return $result;
115 }
116 }
117 }
118