health-check.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * WPSEO plugin file.
  4. *
  5. * @package WPSEO\Internals
  6. */
  7. /**
  8. * Represents the abstract class for the health check.
  9. */
  10. abstract class WPSEO_Health_Check {
  11. /**
  12. * @var string
  13. */
  14. const STATUS_GOOD = 'good';
  15. /**
  16. * @var string
  17. */
  18. const STATUS_RECOMMENDED = 'recommended';
  19. /**
  20. * @var string
  21. */
  22. const STATUS_CRITICAL = 'critical';
  23. /**
  24. * Name of the test.
  25. *
  26. * @var string
  27. */
  28. protected $name = '';
  29. /**
  30. * The value of the section header in the Health check.
  31. *
  32. * @var string
  33. */
  34. protected $label = '';
  35. /**
  36. * Section the result should be displayed in.
  37. *
  38. * @var string
  39. */
  40. protected $status = '';
  41. /**
  42. * What the badge should say with a color.
  43. *
  44. * @var array
  45. */
  46. protected $badge = [
  47. 'label' => '',
  48. 'color' => '',
  49. ];
  50. /**
  51. * Additional details about the results of the test.
  52. *
  53. * @var string
  54. */
  55. protected $description = '';
  56. /**
  57. * A link or button to allow the end user to take action on the result.
  58. *
  59. * @var string
  60. */
  61. protected $actions = '';
  62. /**
  63. * The name of the test.
  64. *
  65. * @var string
  66. */
  67. protected $test = '';
  68. /**
  69. * Whether or not the test should be ran on AJAX as well.
  70. *
  71. * @var bool True when is async, default false.
  72. */
  73. protected $async = false;
  74. /**
  75. * Runs the test and returns the result.
  76. *
  77. * @return array The result.
  78. */
  79. abstract public function run();
  80. /**
  81. * Registers the test to WordPress.
  82. */
  83. public function register_test() {
  84. if ( $this->async ) {
  85. add_filter( 'site_status_tests', [ $this, 'add_async_test' ] );
  86. add_action( 'wp_ajax_health-check-' . $this->get_test_name(), [ $this, 'get_async_test_result' ] );
  87. return;
  88. }
  89. add_filter( 'site_status_tests', [ $this, 'add_test' ] );
  90. }
  91. /**
  92. * Runs the test.
  93. *
  94. * @param array $tests Array with the current tests.
  95. *
  96. * @return array The extended array.
  97. */
  98. public function add_test( $tests ) {
  99. $tests['direct'][ $this->name ] = [
  100. 'test' => [ $this, 'get_test_result' ],
  101. 'name' => $this->name,
  102. ];
  103. return $tests;
  104. }
  105. /**
  106. * Runs the test in async mode.
  107. *
  108. * @param array $tests Array with the current tests.
  109. *
  110. * @return array The extended array.
  111. */
  112. public function add_async_test( $tests ) {
  113. $tests['async'][ $this->name ] = [
  114. 'test' => $this->get_test_name(),
  115. 'name' => $this->name,
  116. ];
  117. return $tests;
  118. }
  119. /**
  120. * Formats the test result as an array.
  121. *
  122. * @return array The formatted test result.
  123. */
  124. public function get_test_result() {
  125. $this->run();
  126. return [
  127. 'label' => $this->label,
  128. 'status' => $this->status,
  129. 'badge' => $this->get_badge(),
  130. 'description' => $this->description,
  131. 'actions' => $this->actions,
  132. ];
  133. }
  134. /**
  135. * Formats the test result as an array.
  136. */
  137. public function get_async_test_result() {
  138. wp_send_json_success( $this->get_test_result() );
  139. }
  140. /**
  141. * Retrieves the badge and ensure usable values are set.
  142. *
  143. * @return array The proper formatted badge.
  144. */
  145. protected function get_badge() {
  146. if ( ! is_array( $this->badge ) ) {
  147. $this->badge = [];
  148. }
  149. if ( empty( $this->badge['label'] ) ) {
  150. $this->badge['label'] = __( 'SEO', 'wordpress-seo' );
  151. }
  152. if ( empty( $this->badge['color'] ) ) {
  153. $this->badge['color'] = 'green';
  154. }
  155. return $this->badge;
  156. }
  157. /**
  158. * WordPress converts the underscores to dashes. To prevent issues we have
  159. * to do it as well.
  160. *
  161. * @return string The formatted testname.
  162. */
  163. protected function get_test_name() {
  164. return str_replace( '_', '-', $this->test );
  165. }
  166. }