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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 the objects of the controller.
  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. }
  48. /**
  49. * Checks if a given request has access to read the theme.
  50. *
  51. * @since 5.0.0
  52. *
  53. * @param WP_REST_Request $request Full details about the request.
  54. * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
  55. */
  56. public function get_items_permissions_check( $request ) {
  57. if ( ! is_user_logged_in() || ! current_user_can( 'edit_posts' ) ) {
  58. return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to view themes.' ), array( 'status' => rest_authorization_required_code() ) );
  59. }
  60. return true;
  61. }
  62. /**
  63. * Retrieves a collection of themes.
  64. *
  65. * @since 5.0.0
  66. *
  67. * @param WP_REST_Request $request Full details about the request.
  68. * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
  69. */
  70. public function get_items( $request ) {
  71. // Retrieve the list of registered collection query parameters.
  72. $registered = $this->get_collection_params();
  73. $themes = array();
  74. if ( isset( $registered['status'], $request['status'] ) && in_array( 'active', $request['status'], true ) ) {
  75. $active_theme = wp_get_theme();
  76. $active_theme = $this->prepare_item_for_response( $active_theme, $request );
  77. $themes[] = $this->prepare_response_for_collection( $active_theme );
  78. }
  79. $response = rest_ensure_response( $themes );
  80. $response->header( 'X-WP-Total', count( $themes ) );
  81. $response->header( 'X-WP-TotalPages', count( $themes ) );
  82. return $response;
  83. }
  84. /**
  85. * Prepares a single theme output for response.
  86. *
  87. * @since 5.0.0
  88. *
  89. * @param WP_Theme $theme Theme object.
  90. * @param WP_REST_Request $request Request object.
  91. * @return WP_REST_Response Response object.
  92. */
  93. public function prepare_item_for_response( $theme, $request ) {
  94. $data = array();
  95. $fields = $this->get_fields_for_response( $request );
  96. if ( in_array( 'theme_supports', $fields, true ) ) {
  97. $formats = get_theme_support( 'post-formats' );
  98. $formats = is_array( $formats ) ? array_values( $formats[0] ) : array();
  99. $formats = array_merge( array( 'standard' ), $formats );
  100. $data['theme_supports']['formats'] = $formats;
  101. $data['theme_supports']['post-thumbnails'] = false;
  102. $data['theme_supports']['responsive-embeds'] = (bool) get_theme_support( 'responsive-embeds' );
  103. $post_thumbnails = get_theme_support( 'post-thumbnails' );
  104. if ( $post_thumbnails ) {
  105. // $post_thumbnails can contain a nested array of post types.
  106. // e.g. array( array( 'post', 'page' ) ).
  107. $data['theme_supports']['post-thumbnails'] = is_array( $post_thumbnails ) ? $post_thumbnails[0] : true;
  108. }
  109. }
  110. $data = $this->add_additional_fields_to_object( $data, $request );
  111. // Wrap the data in a response object.
  112. $response = rest_ensure_response( $data );
  113. /**
  114. * Filters theme data returned from the REST API.
  115. *
  116. * @since 5.0.0
  117. *
  118. * @param WP_REST_Response $response The response object.
  119. * @param WP_Theme $theme Theme object used to create response.
  120. * @param WP_REST_Request $request Request object.
  121. */
  122. return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
  123. }
  124. /**
  125. * Retrieves the theme's schema, conforming to JSON Schema.
  126. *
  127. * @since 5.0.0
  128. *
  129. * @return array Item schema data.
  130. */
  131. public function get_item_schema() {
  132. if ( $this->schema ) {
  133. return $this->add_additional_fields_schema( $this->schema );
  134. }
  135. $schema = array(
  136. '$schema' => 'http://json-schema.org/draft-04/schema#',
  137. 'title' => 'theme',
  138. 'type' => 'object',
  139. 'properties' => array(
  140. 'theme_supports' => array(
  141. 'description' => __( 'Features supported by this theme.' ),
  142. 'type' => 'array',
  143. 'readonly' => true,
  144. 'properties' => array(
  145. 'formats' => array(
  146. 'description' => __( 'Post formats supported.' ),
  147. 'type' => 'array',
  148. 'readonly' => true,
  149. ),
  150. 'post-thumbnails' => array(
  151. 'description' => __( 'Whether the theme supports post thumbnails.' ),
  152. 'type' => array( 'array', 'bool' ),
  153. 'readonly' => true,
  154. ),
  155. 'responsive-embeds' => array(
  156. 'description' => __( 'Whether the theme supports responsive embedded content.' ),
  157. 'type' => 'bool',
  158. 'readonly' => true,
  159. ),
  160. ),
  161. ),
  162. ),
  163. );
  164. $this->schema = $schema;
  165. return $this->add_additional_fields_schema( $this->schema );
  166. }
  167. /**
  168. * Retrieves the search params for the themes collection.
  169. *
  170. * @since 5.0.0
  171. *
  172. * @return array Collection parameters.
  173. */
  174. public function get_collection_params() {
  175. $query_params = parent::get_collection_params();
  176. $query_params['status'] = array(
  177. 'description' => __( 'Limit result set to themes assigned one or more statuses.' ),
  178. 'type' => 'array',
  179. 'items' => array(
  180. 'enum' => array( 'active' ),
  181. 'type' => 'string',
  182. ),
  183. 'required' => true,
  184. 'sanitize_callback' => array( $this, 'sanitize_theme_status' ),
  185. );
  186. /**
  187. * Filter collection parameters for the themes controller.
  188. *
  189. * @since 5.0.0
  190. *
  191. * @param array $query_params JSON Schema-formatted collection parameters.
  192. */
  193. return apply_filters( 'rest_themes_collection_params', $query_params );
  194. }
  195. /**
  196. * Sanitizes and validates the list of theme status.
  197. *
  198. * @since 5.0.0
  199. *
  200. * @param string|array $statuses One or more theme statuses.
  201. * @param WP_REST_Request $request Full details about the request.
  202. * @param string $parameter Additional parameter to pass to validation.
  203. * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
  204. */
  205. public function sanitize_theme_status( $statuses, $request, $parameter ) {
  206. $statuses = wp_parse_slug_list( $statuses );
  207. foreach ( $statuses as $status ) {
  208. $result = rest_validate_request_arg( $status, $request, $parameter );
  209. if ( is_wp_error( $result ) ) {
  210. return $result;
  211. }
  212. }
  213. return $statuses;
  214. }
  215. }