| 1 |
<?php |
| 2 |
|
| 3 |
namespace Yoast\WP\SEO\Integrations\Admin; |
| 4 |
|
| 5 |
use Yoast\WP\SEO\Integrations\Integration_Interface; |
| 6 |
use Yoast\WP\SEO\Services\Health_Check\Health_Check; |
| 7 |
use Yoast\WP\SEO\Conditionals\Admin_Conditional; |
| 8 |
|
| 9 |
/** |
| 10 |
* Integrates health checks with WordPress' Site Health. |
| 11 |
*/ |
| 12 |
class Health_Check_Integration implements Integration_Interface { |
| 13 |
|
| 14 |
/** |
| 15 |
* Contains all the health check implementations. |
| 16 |
* |
| 17 |
* @var Health_Check[] |
| 18 |
*/ |
| 19 |
private $health_checks = []; |
| 20 |
|
| 21 |
/** |
| 22 |
* Uses the dependency injection container to obtain all available implementations of the Health_Check interface. |
| 23 |
* |
| 24 |
* @param Health_Check ...$health_checks The available health checks implementations. |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function __construct( Health_Check ...$health_checks ) { |
| 28 |
$this->health_checks = $health_checks; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Hooks the health checks into WordPress' site status tests. |
| 33 |
* |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public function register_hooks() { |
| 37 |
add_filter( 'site_status_tests', [ $this, 'add_health_checks' ] ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns the conditionals based on which this loadable should be active. |
| 42 |
* |
| 43 |
* In this case: only when on an admin page. |
| 44 |
* |
| 45 |
* @return array The conditionals. |
| 46 |
*/ |
| 47 |
public static function get_conditionals() { |
| 48 |
return [ Admin_Conditional::class ]; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Checks if the input is a WordPress site status tests array, and adds Yoast's health checks if it is. |
| 53 |
* |
| 54 |
* @param string[] $tests Array containing WordPress site status tests. |
| 55 |
* @return string[] Array containing WordPress site status tests with Yoast's health checks. |
| 56 |
*/ |
| 57 |
public function add_health_checks( $tests ) { |
| 58 |
if ( ! $this->is_valid_site_status_tests_array( $tests ) ) { |
| 59 |
return $tests; |
| 60 |
} |
| 61 |
|
| 62 |
return $this->add_health_checks_to_site_status_tests( $tests ); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Checks if the input array is a WordPress site status tests array. |
| 67 |
* |
| 68 |
* @param mixed $tests Array to check. |
| 69 |
* @return bool Returns true if the input array is a WordPress site status tests array. |
| 70 |
*/ |
| 71 |
private function is_valid_site_status_tests_array( $tests ) { |
| 72 |
if ( ! is_array( $tests ) ) { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
if ( ! array_key_exists( 'direct', $tests ) ) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
|
| 80 |
if ( ! is_array( $tests['direct'] ) ) { |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Adds the health checks to WordPress' site status tests. |
| 89 |
* |
| 90 |
* @param string[] $tests Array containing WordPress site status tests. |
| 91 |
* @return string[] Array containing WordPress site status tests with Yoast's health checks. |
| 92 |
*/ |
| 93 |
private function add_health_checks_to_site_status_tests( $tests ) { |
| 94 |
foreach ( $this->health_checks as $health_check ) { |
| 95 |
$tests['direct'][ $health_check->get_test_identifier() ] = [ |
| 96 |
'test' => [ $health_check, 'run_and_get_result' ], |
| 97 |
]; |
| 98 |
} |
| 99 |
|
| 100 |
return $tests; |
| 101 |
} |
| 102 |
} |
| 103 |
|