class-wp-rest-site-health-controller.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * REST API: WP_REST_Site_Health_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.6.0
  8. */
  9. /**
  10. * Core class for interacting with Site Health tests.
  11. *
  12. * @since 5.6.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Site_Health_Controller extends WP_REST_Controller {
  17. /**
  18. * An instance of the site health class.
  19. *
  20. * @since 5.6.0
  21. *
  22. * @var WP_Site_Health
  23. */
  24. private $site_health;
  25. /**
  26. * Site Health controller constructor.
  27. *
  28. * @since 5.6.0
  29. *
  30. * @param WP_Site_Health $site_health An instance of the site health class.
  31. */
  32. public function __construct( $site_health ) {
  33. $this->namespace = 'wp-site-health/v1';
  34. $this->rest_base = 'tests';
  35. $this->site_health = $site_health;
  36. }
  37. /**
  38. * Registers API routes.
  39. *
  40. * @since 5.6.0
  41. * @since 6.1.0 Adds page-cache async test.
  42. *
  43. * @see register_rest_route()
  44. */
  45. public function register_routes() {
  46. register_rest_route(
  47. $this->namespace,
  48. sprintf(
  49. '/%s/%s',
  50. $this->rest_base,
  51. 'background-updates'
  52. ),
  53. array(
  54. array(
  55. 'methods' => 'GET',
  56. 'callback' => array( $this, 'test_background_updates' ),
  57. 'permission_callback' => function () {
  58. return $this->validate_request_permission( 'background_updates' );
  59. },
  60. ),
  61. 'schema' => array( $this, 'get_public_item_schema' ),
  62. )
  63. );
  64. register_rest_route(
  65. $this->namespace,
  66. sprintf(
  67. '/%s/%s',
  68. $this->rest_base,
  69. 'loopback-requests'
  70. ),
  71. array(
  72. array(
  73. 'methods' => 'GET',
  74. 'callback' => array( $this, 'test_loopback_requests' ),
  75. 'permission_callback' => function () {
  76. return $this->validate_request_permission( 'loopback_requests' );
  77. },
  78. ),
  79. 'schema' => array( $this, 'get_public_item_schema' ),
  80. )
  81. );
  82. register_rest_route(
  83. $this->namespace,
  84. sprintf(
  85. '/%s/%s',
  86. $this->rest_base,
  87. 'https-status'
  88. ),
  89. array(
  90. array(
  91. 'methods' => 'GET',
  92. 'callback' => array( $this, 'test_https_status' ),
  93. 'permission_callback' => function () {
  94. return $this->validate_request_permission( 'https_status' );
  95. },
  96. ),
  97. 'schema' => array( $this, 'get_public_item_schema' ),
  98. )
  99. );
  100. register_rest_route(
  101. $this->namespace,
  102. sprintf(
  103. '/%s/%s',
  104. $this->rest_base,
  105. 'dotorg-communication'
  106. ),
  107. array(
  108. array(
  109. 'methods' => 'GET',
  110. 'callback' => array( $this, 'test_dotorg_communication' ),
  111. 'permission_callback' => function () {
  112. return $this->validate_request_permission( 'dotorg_communication' );
  113. },
  114. ),
  115. 'schema' => array( $this, 'get_public_item_schema' ),
  116. )
  117. );
  118. register_rest_route(
  119. $this->namespace,
  120. sprintf(
  121. '/%s/%s',
  122. $this->rest_base,
  123. 'authorization-header'
  124. ),
  125. array(
  126. array(
  127. 'methods' => 'GET',
  128. 'callback' => array( $this, 'test_authorization_header' ),
  129. 'permission_callback' => function () {
  130. return $this->validate_request_permission( 'authorization_header' );
  131. },
  132. ),
  133. 'schema' => array( $this, 'get_public_item_schema' ),
  134. )
  135. );
  136. register_rest_route(
  137. $this->namespace,
  138. sprintf(
  139. '/%s',
  140. 'directory-sizes'
  141. ),
  142. array(
  143. 'methods' => 'GET',
  144. 'callback' => array( $this, 'get_directory_sizes' ),
  145. 'permission_callback' => function() {
  146. return $this->validate_request_permission( 'debug_enabled' ) && ! is_multisite();
  147. },
  148. )
  149. );
  150. register_rest_route(
  151. $this->namespace,
  152. sprintf(
  153. '/%s/%s',
  154. $this->rest_base,
  155. 'page-cache'
  156. ),
  157. array(
  158. array(
  159. 'methods' => 'GET',
  160. 'callback' => array( $this, 'test_page_cache' ),
  161. 'permission_callback' => function () {
  162. return $this->validate_request_permission( 'view_site_health_checks' );
  163. },
  164. ),
  165. )
  166. );
  167. }
  168. /**
  169. * Validates if the current user can request this REST endpoint.
  170. *
  171. * @since 5.6.0
  172. *
  173. * @param string $check The endpoint check being ran.
  174. * @return bool
  175. */
  176. protected function validate_request_permission( $check ) {
  177. $default_capability = 'view_site_health_checks';
  178. /**
  179. * Filters the capability needed to run a given Site Health check.
  180. *
  181. * @since 5.6.0
  182. *
  183. * @param string $default_capability The default capability required for this check.
  184. * @param string $check The Site Health check being performed.
  185. */
  186. $capability = apply_filters( "site_health_test_rest_capability_{$check}", $default_capability, $check );
  187. return current_user_can( $capability );
  188. }
  189. /**
  190. * Checks if background updates work as expected.
  191. *
  192. * @since 5.6.0
  193. *
  194. * @return array
  195. */
  196. public function test_background_updates() {
  197. $this->load_admin_textdomain();
  198. return $this->site_health->get_test_background_updates();
  199. }
  200. /**
  201. * Checks that the site can reach the WordPress.org API.
  202. *
  203. * @since 5.6.0
  204. *
  205. * @return array
  206. */
  207. public function test_dotorg_communication() {
  208. $this->load_admin_textdomain();
  209. return $this->site_health->get_test_dotorg_communication();
  210. }
  211. /**
  212. * Checks that loopbacks can be performed.
  213. *
  214. * @since 5.6.0
  215. *
  216. * @return array
  217. */
  218. public function test_loopback_requests() {
  219. $this->load_admin_textdomain();
  220. return $this->site_health->get_test_loopback_requests();
  221. }
  222. /**
  223. * Checks that the site's frontend can be accessed over HTTPS.
  224. *
  225. * @since 5.7.0
  226. *
  227. * @return array
  228. */
  229. public function test_https_status() {
  230. $this->load_admin_textdomain();
  231. return $this->site_health->get_test_https_status();
  232. }
  233. /**
  234. * Checks that the authorization header is valid.
  235. *
  236. * @since 5.6.0
  237. *
  238. * @return array
  239. */
  240. public function test_authorization_header() {
  241. $this->load_admin_textdomain();
  242. return $this->site_health->get_test_authorization_header();
  243. }
  244. /**
  245. * Checks that full page cache is active.
  246. *
  247. * @since 6.1.0
  248. *
  249. * @return array The test result.
  250. */
  251. public function test_page_cache() {
  252. $this->load_admin_textdomain();
  253. return $this->site_health->get_test_page_cache();
  254. }
  255. /**
  256. * Gets the current directory sizes for this install.
  257. *
  258. * @since 5.6.0
  259. *
  260. * @return array|WP_Error
  261. */
  262. public function get_directory_sizes() {
  263. if ( ! class_exists( 'WP_Debug_Data' ) ) {
  264. require_once ABSPATH . 'wp-admin/includes/class-wp-debug-data.php';
  265. }
  266. $this->load_admin_textdomain();
  267. $sizes_data = WP_Debug_Data::get_sizes();
  268. $all_sizes = array( 'raw' => 0 );
  269. foreach ( $sizes_data as $name => $value ) {
  270. $name = sanitize_text_field( $name );
  271. $data = array();
  272. if ( isset( $value['size'] ) ) {
  273. if ( is_string( $value['size'] ) ) {
  274. $data['size'] = sanitize_text_field( $value['size'] );
  275. } else {
  276. $data['size'] = (int) $value['size'];
  277. }
  278. }
  279. if ( isset( $value['debug'] ) ) {
  280. if ( is_string( $value['debug'] ) ) {
  281. $data['debug'] = sanitize_text_field( $value['debug'] );
  282. } else {
  283. $data['debug'] = (int) $value['debug'];
  284. }
  285. }
  286. if ( ! empty( $value['raw'] ) ) {
  287. $data['raw'] = (int) $value['raw'];
  288. }
  289. $all_sizes[ $name ] = $data;
  290. }
  291. if ( isset( $all_sizes['total_size']['debug'] ) && 'not available' === $all_sizes['total_size']['debug'] ) {
  292. return new WP_Error( 'not_available', __( 'Directory sizes could not be returned.' ), array( 'status' => 500 ) );
  293. }
  294. return $all_sizes;
  295. }
  296. /**
  297. * Loads the admin textdomain for Site Health tests.
  298. *
  299. * The {@see WP_Site_Health} class is defined in WP-Admin, while the REST API operates in a front-end context.
  300. * This means that the translations for Site Health won't be loaded by default in {@see load_default_textdomain()}.
  301. *
  302. * @since 5.6.0
  303. */
  304. protected function load_admin_textdomain() {
  305. // Accounts for inner REST API requests in the admin.
  306. if ( ! is_admin() ) {
  307. $locale = determine_locale();
  308. load_textdomain( 'default', WP_LANG_DIR . "/admin-$locale.mo", $locale );
  309. }
  310. }
  311. /**
  312. * Gets the schema for each site health test.
  313. *
  314. * @since 5.6.0
  315. *
  316. * @return array The test schema.
  317. */
  318. public function get_item_schema() {
  319. if ( $this->schema ) {
  320. return $this->schema;
  321. }
  322. $this->schema = array(
  323. '$schema' => 'http://json-schema.org/draft-04/schema#',
  324. 'title' => 'wp-site-health-test',
  325. 'type' => 'object',
  326. 'properties' => array(
  327. 'test' => array(
  328. 'type' => 'string',
  329. 'description' => __( 'The name of the test being run.' ),
  330. 'readonly' => true,
  331. ),
  332. 'label' => array(
  333. 'type' => 'string',
  334. 'description' => __( 'A label describing the test.' ),
  335. 'readonly' => true,
  336. ),
  337. 'status' => array(
  338. 'type' => 'string',
  339. 'description' => __( 'The status of the test.' ),
  340. 'enum' => array( 'good', 'recommended', 'critical' ),
  341. 'readonly' => true,
  342. ),
  343. 'badge' => array(
  344. 'type' => 'object',
  345. 'description' => __( 'The category this test is grouped in.' ),
  346. 'properties' => array(
  347. 'label' => array(
  348. 'type' => 'string',
  349. 'readonly' => true,
  350. ),
  351. 'color' => array(
  352. 'type' => 'string',
  353. 'enum' => array( 'blue', 'orange', 'red', 'green', 'purple', 'gray' ),
  354. 'readonly' => true,
  355. ),
  356. ),
  357. 'readonly' => true,
  358. ),
  359. 'description' => array(
  360. 'type' => 'string',
  361. 'description' => __( 'A more descriptive explanation of what the test looks for, and why it is important for the user.' ),
  362. 'readonly' => true,
  363. ),
  364. 'actions' => array(
  365. 'type' => 'string',
  366. 'description' => __( 'HTML containing an action to direct the user to where they can resolve the issue.' ),
  367. 'readonly' => true,
  368. ),
  369. ),
  370. );
  371. return $this->schema;
  372. }
  373. }