Sin descripción

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

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