No Description

class-wp-rest-themes-controller.php 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. <?php
  2. /**
  3. * REST API: WP_REST_Themes_Controller class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Core class used to manage themes via the REST API.
  11. *
  12. * @since 5.0.0
  13. *
  14. * @see WP_REST_Controller
  15. */
  16. class WP_REST_Themes_Controller extends WP_REST_Controller {
  17. /**
  18. * Constructor.
  19. *
  20. * @since 5.0.0
  21. */
  22. public function __construct() {
  23. $this->namespace = 'wp/v2';
  24. $this->rest_base = 'themes';
  25. }
  26. /**
  27. * Registers the routes for themes.
  28. *
  29. * @since 5.0.0
  30. *
  31. * @see register_rest_route()
  32. */
  33. public function register_routes() {
  34. register_rest_route(
  35. $this->namespace,
  36. '/' . $this->rest_base,
  37. array(
  38. array(
  39. 'methods' => WP_REST_Server::READABLE,
  40. 'callback' => array( $this, 'get_items' ),
  41. 'permission_callback' => array( $this, 'get_items_permissions_check' ),
  42. 'args' => $this->get_collection_params(),
  43. ),
  44. 'schema' => array( $this, 'get_item_schema' ),
  45. )
  46. );
  47. register_rest_route(
  48. $this->namespace,
  49. '/' . $this->rest_base . '/(?P<stylesheet>[\w-]+)',
  50. array(
  51. 'args' => array(
  52. 'stylesheet' => array(
  53. 'description' => __( "The theme's stylesheet. This uniquely identifies the theme." ),
  54. 'type' => 'string',
  55. ),
  56. ),
  57. array(
  58. 'methods' => WP_REST_Server::READABLE,
  59. 'callback' => array( $this, 'get_item' ),
  60. 'permission_callback' => array( $this, 'get_item_permissions_check' ),
  61. ),
  62. 'schema' => array( $this, 'get_public_item_schema' ),
  63. )
  64. );
  65. }
  66. /**
  67. * Checks if a given request has access to read the theme.
  68. *
  69. * @since 5.0.0
  70. *
  71. * @param WP_REST_Request $request Full details about the request.
  72. * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
  73. */
  74. public function get_items_permissions_check( $request ) {
  75. if ( current_user_can( 'switch_themes' ) || current_user_can( 'manage_network_themes' ) ) {
  76. return true;
  77. }
  78. $registered = $this->get_collection_params();
  79. if ( isset( $registered['status'], $request['status'] ) && is_array( $request['status'] ) && array( 'active' ) === $request['status'] ) {
  80. return $this->check_read_active_theme_permission();
  81. }
  82. return new WP_Error(
  83. 'rest_cannot_view_themes',
  84. __( 'Sorry, you are not allowed to view themes.' ),
  85. array( 'status' => rest_authorization_required_code() )
  86. );
  87. }
  88. /**
  89. * Checks if a given request has access to read the theme.
  90. *
  91. * @since 5.7.0
  92. *
  93. * @param WP_REST_Request $request Full details about the request.
  94. * @return bool|WP_Error True if the request has read access for the item, otherwise WP_Error object.
  95. */
  96. public function get_item_permissions_check( $request ) {
  97. if ( current_user_can( 'switch_themes' ) || current_user_can( 'manage_network_themes' ) ) {
  98. return true;
  99. }
  100. $wp_theme = wp_get_theme( $request['stylesheet'] );
  101. $current_theme = wp_get_theme();
  102. if ( $this->is_same_theme( $wp_theme, $current_theme ) ) {
  103. return $this->check_read_active_theme_permission();
  104. }
  105. return new WP_Error(
  106. 'rest_cannot_view_themes',
  107. __( 'Sorry, you are not allowed to view themes.' ),
  108. array( 'status' => rest_authorization_required_code() )
  109. );
  110. }
  111. /**
  112. * Checks if a theme can be read.
  113. *
  114. * @since 5.7.0
  115. *
  116. * @return bool|WP_Error Whether the theme can be read.
  117. */
  118. protected function check_read_active_theme_permission() {
  119. if ( current_user_can( 'edit_posts' ) ) {
  120. return true;
  121. }
  122. foreach ( get_post_types( array( 'show_in_rest' => true ), 'objects' ) as $post_type ) {
  123. if ( current_user_can( $post_type->cap->edit_posts ) ) {
  124. return true;
  125. }
  126. }
  127. return new WP_Error(
  128. 'rest_cannot_view_active_theme',
  129. __( 'Sorry, you are not allowed to view the active theme.' ),
  130. array( 'status' => rest_authorization_required_code() )
  131. );
  132. }
  133. /**
  134. * Retrieves a single theme.
  135. *
  136. * @since 5.7.0
  137. *
  138. * @param WP_REST_Request $request Full details about the request.
  139. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  140. */
  141. public function get_item( $request ) {
  142. $wp_theme = wp_get_theme( $request['stylesheet'] );
  143. if ( ! $wp_theme->exists() ) {
  144. return new WP_Error(
  145. 'rest_theme_not_found',
  146. __( 'Theme not found.' ),
  147. array( 'status' => 404 )
  148. );
  149. }
  150. $data = $this->prepare_item_for_response( $wp_theme, $request );
  151. return rest_ensure_response( $data );
  152. }
  153. /**
  154. * Retrieves a collection of themes.
  155. *
  156. * @since 5.0.0
  157. *
  158. * @param WP_REST_Request $request Full details about the request.
  159. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  160. */
  161. public function get_items( $request ) {
  162. $themes = array();
  163. $active_themes = wp_get_themes();
  164. $current_theme = wp_get_theme();
  165. $status = $request['status'];
  166. foreach ( $active_themes as $theme_name => $theme ) {
  167. $theme_status = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
  168. if ( is_array( $status ) && ! in_array( $theme_status, $status, true ) ) {
  169. continue;
  170. }
  171. $prepared = $this->prepare_item_for_response( $theme, $request );
  172. $themes[] = $this->prepare_response_for_collection( $prepared );
  173. }
  174. $response = rest_ensure_response( $themes );
  175. $response->header( 'X-WP-Total', count( $themes ) );
  176. $response->header( 'X-WP-TotalPages', 1 );
  177. return $response;
  178. }
  179. /**
  180. * Prepares a single theme output for response.
  181. *
  182. * @since 5.0.0
  183. *
  184. * @param WP_Theme $theme Theme object.
  185. * @param WP_REST_Request $request Request object.
  186. * @return WP_REST_Response Response object.
  187. */
  188. public function prepare_item_for_response( $theme, $request ) {
  189. $data = array();
  190. $fields = $this->get_fields_for_response( $request );
  191. if ( rest_is_field_included( 'stylesheet', $fields ) ) {
  192. $data['stylesheet'] = $theme->get_stylesheet();
  193. }
  194. if ( rest_is_field_included( 'template', $fields ) ) {
  195. /**
  196. * Use the get_template() method, not the 'Template' header, for finding the template.
  197. * The 'Template' header is only good for what was written in the style.css, while
  198. * get_template() takes into account where WordPress actually located the theme and
  199. * whether it is actually valid.
  200. */
  201. $data['template'] = $theme->get_template();
  202. }
  203. $plain_field_mappings = array(
  204. 'requires_php' => 'RequiresPHP',
  205. 'requires_wp' => 'RequiresWP',
  206. 'textdomain' => 'TextDomain',
  207. 'version' => 'Version',
  208. );
  209. foreach ( $plain_field_mappings as $field => $header ) {
  210. if ( rest_is_field_included( $field, $fields ) ) {
  211. $data[ $field ] = $theme->get( $header );
  212. }
  213. }
  214. if ( rest_is_field_included( 'screenshot', $fields ) ) {
  215. // Using $theme->get_screenshot() with no args to get absolute URL.
  216. $data['screenshot'] = $theme->get_screenshot() ? $theme->get_screenshot() : '';
  217. }
  218. $rich_field_mappings = array(
  219. 'author' => 'Author',
  220. 'author_uri' => 'AuthorURI',
  221. 'description' => 'Description',
  222. 'name' => 'Name',
  223. 'tags' => 'Tags',
  224. 'theme_uri' => 'ThemeURI',
  225. );
  226. foreach ( $rich_field_mappings as $field => $header ) {
  227. if ( rest_is_field_included( "{$field}.raw", $fields ) ) {
  228. $data[ $field ]['raw'] = $theme->display( $header, false, true );
  229. }
  230. if ( rest_is_field_included( "{$field}.rendered", $fields ) ) {
  231. $data[ $field ]['rendered'] = $theme->display( $header );
  232. }
  233. }
  234. $current_theme = wp_get_theme();
  235. if ( rest_is_field_included( 'status', $fields ) ) {
  236. $data['status'] = ( $this->is_same_theme( $theme, $current_theme ) ) ? 'active' : 'inactive';
  237. }
  238. if ( rest_is_field_included( 'theme_supports', $fields ) && $this->is_same_theme( $theme, $current_theme ) ) {
  239. foreach ( get_registered_theme_features() as $feature => $config ) {
  240. if ( ! is_array( $config['show_in_rest'] ) ) {
  241. continue;
  242. }
  243. $name = $config['show_in_rest']['name'];
  244. if ( ! rest_is_field_included( "theme_supports.{$name}", $fields ) ) {
  245. continue;
  246. }
  247. if ( ! current_theme_supports( $feature ) ) {
  248. $data['theme_supports'][ $name ] = $config['show_in_rest']['schema']['default'];
  249. continue;
  250. }
  251. $support = get_theme_support( $feature );
  252. if ( isset( $config['show_in_rest']['prepare_callback'] ) ) {
  253. $prepare = $config['show_in_rest']['prepare_callback'];
  254. } else {
  255. $prepare = array( $this, 'prepare_theme_support' );
  256. }
  257. $prepared = $prepare( $support, $config, $feature, $request );
  258. if ( is_wp_error( $prepared ) ) {
  259. continue;
  260. }
  261. $data['theme_supports'][ $name ] = $prepared;
  262. }
  263. }
  264. $data = $this->add_additional_fields_to_object( $data, $request );
  265. // Wrap the data in a response object.
  266. $response = rest_ensure_response( $data );
  267. $response->add_links( $this->prepare_links( $theme ) );
  268. /**
  269. * Filters theme data returned from the REST API.
  270. *
  271. * @since 5.0.0
  272. *
  273. * @param WP_REST_Response $response The response object.
  274. * @param WP_Theme $theme Theme object used to create response.
  275. * @param WP_REST_Request $request Request object.
  276. */
  277. return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
  278. }
  279. /**
  280. * Prepares links for the request.
  281. *
  282. * @since 5.7.0
  283. *
  284. * @param WP_Theme $theme Theme data.
  285. * @return array Links for the given block type.
  286. */
  287. protected function prepare_links( $theme ) {
  288. return array(
  289. 'self' => array(
  290. 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $theme->get_stylesheet() ) ),
  291. ),
  292. 'collection' => array(
  293. 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  294. ),
  295. );
  296. }
  297. /**
  298. * Helper function to compare two themes.
  299. *
  300. * @since 5.7.0
  301. *
  302. * @param WP_Theme $theme_a First theme to compare.
  303. * @param WP_Theme $theme_b Second theme to compare.
  304. * @return bool
  305. */
  306. protected function is_same_theme( $theme_a, $theme_b ) {
  307. return $theme_a->get_stylesheet() === $theme_b->get_stylesheet();
  308. }
  309. /**
  310. * Prepares the theme support value for inclusion in the REST API response.
  311. *
  312. * @since 5.5.0
  313. *
  314. * @param mixed $support The raw value from get_theme_support().
  315. * @param array $args The feature's registration args.
  316. * @param string $feature The feature name.
  317. * @param WP_REST_Request $request The request object.
  318. * @return mixed The prepared support value.
  319. */
  320. protected function prepare_theme_support( $support, $args, $feature, $request ) {
  321. $schema = $args['show_in_rest']['schema'];
  322. if ( 'boolean' === $schema['type'] ) {
  323. return true;
  324. }
  325. if ( is_array( $support ) && ! $args['variadic'] ) {
  326. $support = $support[0];
  327. }
  328. return rest_sanitize_value_from_schema( $support, $schema );
  329. }
  330. /**
  331. * Retrieves the theme's schema, conforming to JSON Schema.
  332. *
  333. * @since 5.0.0
  334. *
  335. * @return array Item schema data.
  336. */
  337. public function get_item_schema() {
  338. if ( $this->schema ) {
  339. return $this->add_additional_fields_schema( $this->schema );
  340. }
  341. $schema = array(
  342. '$schema' => 'http://json-schema.org/draft-04/schema#',
  343. 'title' => 'theme',
  344. 'type' => 'object',
  345. 'properties' => array(
  346. 'stylesheet' => array(
  347. 'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ),
  348. 'type' => 'string',
  349. 'readonly' => true,
  350. ),
  351. 'template' => array(
  352. 'description' => __( 'The theme\'s template. If this is a child theme, this refers to the parent theme, otherwise this is the same as the theme\'s stylesheet.' ),
  353. 'type' => 'string',
  354. 'readonly' => true,
  355. ),
  356. 'author' => array(
  357. 'description' => __( 'The theme author.' ),
  358. 'type' => 'object',
  359. 'readonly' => true,
  360. 'properties' => array(
  361. 'raw' => array(
  362. 'description' => __( 'The theme author\'s name, as found in the theme header.' ),
  363. 'type' => 'string',
  364. ),
  365. 'rendered' => array(
  366. 'description' => __( 'HTML for the theme author, transformed for display.' ),
  367. 'type' => 'string',
  368. ),
  369. ),
  370. ),
  371. 'author_uri' => array(
  372. 'description' => __( 'The website of the theme author.' ),
  373. 'type' => 'object',
  374. 'readonly' => true,
  375. 'properties' => array(
  376. 'raw' => array(
  377. 'description' => __( 'The website of the theme author, as found in the theme header.' ),
  378. 'type' => 'string',
  379. 'format' => 'uri',
  380. ),
  381. 'rendered' => array(
  382. 'description' => __( 'The website of the theme author, transformed for display.' ),
  383. 'type' => 'string',
  384. 'format' => 'uri',
  385. ),
  386. ),
  387. ),
  388. 'description' => array(
  389. 'description' => __( 'A description of the theme.' ),
  390. 'type' => 'object',
  391. 'readonly' => true,
  392. 'properties' => array(
  393. 'raw' => array(
  394. 'description' => __( 'The theme description, as found in the theme header.' ),
  395. 'type' => 'string',
  396. ),
  397. 'rendered' => array(
  398. 'description' => __( 'The theme description, transformed for display.' ),
  399. 'type' => 'string',
  400. ),
  401. ),
  402. ),
  403. 'name' => array(
  404. 'description' => __( 'The name of the theme.' ),
  405. 'type' => 'object',
  406. 'readonly' => true,
  407. 'properties' => array(
  408. 'raw' => array(
  409. 'description' => __( 'The theme name, as found in the theme header.' ),
  410. 'type' => 'string',
  411. ),
  412. 'rendered' => array(
  413. 'description' => __( 'The theme name, transformed for display.' ),
  414. 'type' => 'string',
  415. ),
  416. ),
  417. ),
  418. 'requires_php' => array(
  419. 'description' => __( 'The minimum PHP version required for the theme to work.' ),
  420. 'type' => 'string',
  421. 'readonly' => true,
  422. ),
  423. 'requires_wp' => array(
  424. 'description' => __( 'The minimum WordPress version required for the theme to work.' ),
  425. 'type' => 'string',
  426. 'readonly' => true,
  427. ),
  428. 'screenshot' => array(
  429. 'description' => __( 'The theme\'s screenshot URL.' ),
  430. 'type' => 'string',
  431. 'format' => 'uri',
  432. 'readonly' => true,
  433. ),
  434. 'tags' => array(
  435. 'description' => __( 'Tags indicating styles and features of the theme.' ),
  436. 'type' => 'object',
  437. 'readonly' => true,
  438. 'properties' => array(
  439. 'raw' => array(
  440. 'description' => __( 'The theme tags, as found in the theme header.' ),
  441. 'type' => 'array',
  442. 'items' => array(
  443. 'type' => 'string',
  444. ),
  445. ),
  446. 'rendered' => array(
  447. 'description' => __( 'The theme tags, transformed for display.' ),
  448. 'type' => 'string',
  449. ),
  450. ),
  451. ),
  452. 'textdomain' => array(
  453. 'description' => __( 'The theme\'s text domain.' ),
  454. 'type' => 'string',
  455. 'readonly' => true,
  456. ),
  457. 'theme_supports' => array(
  458. 'description' => __( 'Features supported by this theme.' ),
  459. 'type' => 'object',
  460. 'readonly' => true,
  461. 'properties' => array(),
  462. ),
  463. 'theme_uri' => array(
  464. 'description' => __( 'The URI of the theme\'s webpage.' ),
  465. 'type' => 'object',
  466. 'readonly' => true,
  467. 'properties' => array(
  468. 'raw' => array(
  469. 'description' => __( 'The URI of the theme\'s webpage, as found in the theme header.' ),
  470. 'type' => 'string',
  471. 'format' => 'uri',
  472. ),
  473. 'rendered' => array(
  474. 'description' => __( 'The URI of the theme\'s webpage, transformed for display.' ),
  475. 'type' => 'string',
  476. 'format' => 'uri',
  477. ),
  478. ),
  479. ),
  480. 'version' => array(
  481. 'description' => __( 'The theme\'s current version.' ),
  482. 'type' => 'string',
  483. 'readonly' => true,
  484. ),
  485. 'status' => array(
  486. 'description' => __( 'A named status for the theme.' ),
  487. 'type' => 'string',
  488. 'enum' => array( 'inactive', 'active' ),
  489. ),
  490. ),
  491. );
  492. foreach ( get_registered_theme_features() as $feature => $config ) {
  493. if ( ! is_array( $config['show_in_rest'] ) ) {
  494. continue;
  495. }
  496. $name = $config['show_in_rest']['name'];
  497. $schema['properties']['theme_supports']['properties'][ $name ] = $config['show_in_rest']['schema'];
  498. }
  499. $this->schema = $schema;
  500. return $this->add_additional_fields_schema( $this->schema );
  501. }
  502. /**
  503. * Retrieves the search params for the themes collection.
  504. *
  505. * @since 5.0.0
  506. *
  507. * @return array Collection parameters.
  508. */
  509. public function get_collection_params() {
  510. $query_params = array(
  511. 'status' => array(
  512. 'description' => __( 'Limit result set to themes assigned one or more statuses.' ),
  513. 'type' => 'array',
  514. 'items' => array(
  515. 'enum' => array( 'active', 'inactive' ),
  516. 'type' => 'string',
  517. ),
  518. ),
  519. );
  520. /**
  521. * Filters REST API collection parameters for the themes controller.
  522. *
  523. * @since 5.0.0
  524. *
  525. * @param array $query_params JSON Schema-formatted collection parameters.
  526. */
  527. return apply_filters( 'rest_themes_collection_params', $query_params );
  528. }
  529. /**
  530. * Sanitizes and validates the list of theme status.
  531. *
  532. * @since 5.0.0
  533. * @deprecated 5.7.0
  534. *
  535. * @param string|array $statuses One or more theme statuses.
  536. * @param WP_REST_Request $request Full details about the request.
  537. * @param string $parameter Additional parameter to pass to validation.
  538. * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
  539. */
  540. public function sanitize_theme_status( $statuses, $request, $parameter ) {
  541. _deprecated_function( __METHOD__, '5.7.0' );
  542. $statuses = wp_parse_slug_list( $statuses );
  543. foreach ( $statuses as $status ) {
  544. $result = rest_validate_request_arg( $status, $request, $parameter );
  545. if ( is_wp_error( $result ) ) {
  546. return $result;
  547. }
  548. }
  549. return $statuses;
  550. }
  551. }