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

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