Sin descripción

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. $response->add_links( $this->prepare_links( $theme ) );
  288. if ( $theme->get_stylesheet() === wp_get_theme()->get_stylesheet() ) {
  289. // This creates a record for the active theme if not existent.
  290. $id = WP_Theme_JSON_Resolver::get_user_global_styles_post_id();
  291. } else {
  292. $user_cpt = WP_Theme_JSON_Resolver::get_user_data_from_wp_global_styles( $theme );
  293. $id = isset( $user_cpt['ID'] ) ? $user_cpt['ID'] : null;
  294. }
  295. if ( $id ) {
  296. $response->add_link(
  297. 'https://api.w.org/user-global-styles',
  298. rest_url( 'wp/v2/global-styles/' . $id )
  299. );
  300. }
  301. /**
  302. * Filters theme data returned from the REST API.
  303. *
  304. * @since 5.0.0
  305. *
  306. * @param WP_REST_Response $response The response object.
  307. * @param WP_Theme $theme Theme object used to create response.
  308. * @param WP_REST_Request $request Request object.
  309. */
  310. return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
  311. }
  312. /**
  313. * Prepares links for the request.
  314. *
  315. * @since 5.7.0
  316. *
  317. * @param WP_Theme $theme Theme data.
  318. * @return array Links for the given block type.
  319. */
  320. protected function prepare_links( $theme ) {
  321. return array(
  322. 'self' => array(
  323. 'href' => rest_url( sprintf( '%s/%s/%s', $this->namespace, $this->rest_base, $theme->get_stylesheet() ) ),
  324. ),
  325. 'collection' => array(
  326. 'href' => rest_url( sprintf( '%s/%s', $this->namespace, $this->rest_base ) ),
  327. ),
  328. );
  329. }
  330. /**
  331. * Helper function to compare two themes.
  332. *
  333. * @since 5.7.0
  334. *
  335. * @param WP_Theme $theme_a First theme to compare.
  336. * @param WP_Theme $theme_b Second theme to compare.
  337. * @return bool
  338. */
  339. protected function is_same_theme( $theme_a, $theme_b ) {
  340. return $theme_a->get_stylesheet() === $theme_b->get_stylesheet();
  341. }
  342. /**
  343. * Prepares the theme support value for inclusion in the REST API response.
  344. *
  345. * @since 5.5.0
  346. *
  347. * @param mixed $support The raw value from get_theme_support().
  348. * @param array $args The feature's registration args.
  349. * @param string $feature The feature name.
  350. * @param WP_REST_Request $request The request object.
  351. * @return mixed The prepared support value.
  352. */
  353. protected function prepare_theme_support( $support, $args, $feature, $request ) {
  354. $schema = $args['show_in_rest']['schema'];
  355. if ( 'boolean' === $schema['type'] ) {
  356. return true;
  357. }
  358. if ( is_array( $support ) && ! $args['variadic'] ) {
  359. $support = $support[0];
  360. }
  361. return rest_sanitize_value_from_schema( $support, $schema );
  362. }
  363. /**
  364. * Retrieves the theme's schema, conforming to JSON Schema.
  365. *
  366. * @since 5.0.0
  367. *
  368. * @return array Item schema data.
  369. */
  370. public function get_item_schema() {
  371. if ( $this->schema ) {
  372. return $this->add_additional_fields_schema( $this->schema );
  373. }
  374. $schema = array(
  375. '$schema' => 'http://json-schema.org/draft-04/schema#',
  376. 'title' => 'theme',
  377. 'type' => 'object',
  378. 'properties' => array(
  379. 'stylesheet' => array(
  380. 'description' => __( 'The theme\'s stylesheet. This uniquely identifies the theme.' ),
  381. 'type' => 'string',
  382. 'readonly' => true,
  383. ),
  384. 'template' => array(
  385. '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.' ),
  386. 'type' => 'string',
  387. 'readonly' => true,
  388. ),
  389. 'author' => array(
  390. 'description' => __( 'The theme author.' ),
  391. 'type' => 'object',
  392. 'readonly' => true,
  393. 'properties' => array(
  394. 'raw' => array(
  395. 'description' => __( 'The theme author\'s name, as found in the theme header.' ),
  396. 'type' => 'string',
  397. ),
  398. 'rendered' => array(
  399. 'description' => __( 'HTML for the theme author, transformed for display.' ),
  400. 'type' => 'string',
  401. ),
  402. ),
  403. ),
  404. 'author_uri' => array(
  405. 'description' => __( 'The website of the theme author.' ),
  406. 'type' => 'object',
  407. 'readonly' => true,
  408. 'properties' => array(
  409. 'raw' => array(
  410. 'description' => __( 'The website of the theme author, as found in the theme header.' ),
  411. 'type' => 'string',
  412. 'format' => 'uri',
  413. ),
  414. 'rendered' => array(
  415. 'description' => __( 'The website of the theme author, transformed for display.' ),
  416. 'type' => 'string',
  417. 'format' => 'uri',
  418. ),
  419. ),
  420. ),
  421. 'description' => array(
  422. 'description' => __( 'A description of the theme.' ),
  423. 'type' => 'object',
  424. 'readonly' => true,
  425. 'properties' => array(
  426. 'raw' => array(
  427. 'description' => __( 'The theme description, as found in the theme header.' ),
  428. 'type' => 'string',
  429. ),
  430. 'rendered' => array(
  431. 'description' => __( 'The theme description, transformed for display.' ),
  432. 'type' => 'string',
  433. ),
  434. ),
  435. ),
  436. 'name' => array(
  437. 'description' => __( 'The name of the theme.' ),
  438. 'type' => 'object',
  439. 'readonly' => true,
  440. 'properties' => array(
  441. 'raw' => array(
  442. 'description' => __( 'The theme name, as found in the theme header.' ),
  443. 'type' => 'string',
  444. ),
  445. 'rendered' => array(
  446. 'description' => __( 'The theme name, transformed for display.' ),
  447. 'type' => 'string',
  448. ),
  449. ),
  450. ),
  451. 'requires_php' => array(
  452. 'description' => __( 'The minimum PHP version required for the theme to work.' ),
  453. 'type' => 'string',
  454. 'readonly' => true,
  455. ),
  456. 'requires_wp' => array(
  457. 'description' => __( 'The minimum WordPress version required for the theme to work.' ),
  458. 'type' => 'string',
  459. 'readonly' => true,
  460. ),
  461. 'screenshot' => array(
  462. 'description' => __( 'The theme\'s screenshot URL.' ),
  463. 'type' => 'string',
  464. 'format' => 'uri',
  465. 'readonly' => true,
  466. ),
  467. 'tags' => array(
  468. 'description' => __( 'Tags indicating styles and features of the theme.' ),
  469. 'type' => 'object',
  470. 'readonly' => true,
  471. 'properties' => array(
  472. 'raw' => array(
  473. 'description' => __( 'The theme tags, as found in the theme header.' ),
  474. 'type' => 'array',
  475. 'items' => array(
  476. 'type' => 'string',
  477. ),
  478. ),
  479. 'rendered' => array(
  480. 'description' => __( 'The theme tags, transformed for display.' ),
  481. 'type' => 'string',
  482. ),
  483. ),
  484. ),
  485. 'textdomain' => array(
  486. 'description' => __( 'The theme\'s text domain.' ),
  487. 'type' => 'string',
  488. 'readonly' => true,
  489. ),
  490. 'theme_supports' => array(
  491. 'description' => __( 'Features supported by this theme.' ),
  492. 'type' => 'object',
  493. 'readonly' => true,
  494. 'properties' => array(),
  495. ),
  496. 'theme_uri' => array(
  497. 'description' => __( 'The URI of the theme\'s webpage.' ),
  498. 'type' => 'object',
  499. 'readonly' => true,
  500. 'properties' => array(
  501. 'raw' => array(
  502. 'description' => __( 'The URI of the theme\'s webpage, as found in the theme header.' ),
  503. 'type' => 'string',
  504. 'format' => 'uri',
  505. ),
  506. 'rendered' => array(
  507. 'description' => __( 'The URI of the theme\'s webpage, transformed for display.' ),
  508. 'type' => 'string',
  509. 'format' => 'uri',
  510. ),
  511. ),
  512. ),
  513. 'version' => array(
  514. 'description' => __( 'The theme\'s current version.' ),
  515. 'type' => 'string',
  516. 'readonly' => true,
  517. ),
  518. 'status' => array(
  519. 'description' => __( 'A named status for the theme.' ),
  520. 'type' => 'string',
  521. 'enum' => array( 'inactive', 'active' ),
  522. ),
  523. ),
  524. );
  525. foreach ( get_registered_theme_features() as $feature => $config ) {
  526. if ( ! is_array( $config['show_in_rest'] ) ) {
  527. continue;
  528. }
  529. $name = $config['show_in_rest']['name'];
  530. $schema['properties']['theme_supports']['properties'][ $name ] = $config['show_in_rest']['schema'];
  531. }
  532. $this->schema = $schema;
  533. return $this->add_additional_fields_schema( $this->schema );
  534. }
  535. /**
  536. * Retrieves the search params for the themes collection.
  537. *
  538. * @since 5.0.0
  539. *
  540. * @return array Collection parameters.
  541. */
  542. public function get_collection_params() {
  543. $query_params = array(
  544. 'status' => array(
  545. 'description' => __( 'Limit result set to themes assigned one or more statuses.' ),
  546. 'type' => 'array',
  547. 'items' => array(
  548. 'enum' => array( 'active', 'inactive' ),
  549. 'type' => 'string',
  550. ),
  551. ),
  552. );
  553. /**
  554. * Filters REST API collection parameters for the themes controller.
  555. *
  556. * @since 5.0.0
  557. *
  558. * @param array $query_params JSON Schema-formatted collection parameters.
  559. */
  560. return apply_filters( 'rest_themes_collection_params', $query_params );
  561. }
  562. /**
  563. * Sanitizes and validates the list of theme status.
  564. *
  565. * @since 5.0.0
  566. * @deprecated 5.7.0
  567. *
  568. * @param string|array $statuses One or more theme statuses.
  569. * @param WP_REST_Request $request Full details about the request.
  570. * @param string $parameter Additional parameter to pass to validation.
  571. * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
  572. */
  573. public function sanitize_theme_status( $statuses, $request, $parameter ) {
  574. _deprecated_function( __METHOD__, '5.7.0' );
  575. $statuses = wp_parse_slug_list( $statuses );
  576. foreach ( $statuses as $status ) {
  577. $result = rest_validate_request_arg( $status, $request, $parameter );
  578. if ( is_wp_error( $result ) ) {
  579. return $result;
  580. }
  581. }
  582. return $statuses;
  583. }
  584. }