PluginProbe
Gutenberg / 4.6.1
Gutenberg v4.6.1
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / lib / class-wp-rest-themes-controller.php

class-wp-rest-themes-controller.php in Gutenberg 4.6.1, at lib/class-wp-rest-themes-controller.php

238 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
11 * Core class used to manage themes via the REST API.
12 *
13 * @since 5.0.0
14 *
15 * @see WP_REST_Controller
16 */
17 class WP_REST_Themes_Controller extends WP_REST_Controller {
18
19 /**
20 * Constructor.
21 *
22 * @since 5.0.0
23 */
24 public function __construct() {
25 $this->namespace = 'wp/v2';
26 $this->rest_base = 'themes';
27 }
28
29 /**
30 * Registers the routes for the objects of the controller.
31 *
32 * @since 5.0.0
33 *
34 * @see register_rest_route()
35 */
36 public function register_routes() {
37 register_rest_route(
38 $this->namespace,
39 '/' . $this->rest_base,
40 array(
41 array(
42 'methods' => WP_REST_Server::READABLE,
43 'callback' => array( $this, 'get_items' ),
44 'permission_callback' => array( $this, 'get_items_permissions_check' ),
45 'args' => $this->get_collection_params(),
46 ),
47 'schema' => array( $this, 'get_item_schema' ),
48 )
49 );
50 }
51
52 /**
53 * Checks if a given request has access to read the theme.
54 *
55 * @since 5.0.0
56 *
57 * @param WP_REST_Request $request Full details about the request.
58 * @return true|WP_Error True if the request has read access for the item, otherwise WP_Error object.
59 */
60 public function get_items_permissions_check( $request ) {
61 if ( ! is_user_logged_in() || ! current_user_can( 'edit_posts' ) ) {
62 return new WP_Error( 'rest_user_cannot_view', __( 'Sorry, you are not allowed to view themes.', 'gutenberg' ), array( 'status' => rest_authorization_required_code() ) );
63 }
64
65 return true;
66 }
67
68 /**
69 * Retrieves a collection of themes.
70 *
71 * @since 5.0.0
72 *
73 * @param WP_REST_Request $request Full details about the request.
74 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure.
75 */
76 public function get_items( $request ) {
77 // Retrieve the list of registered collection query parameters.
78 $registered = $this->get_collection_params();
79 $themes = array();
80
81 if ( isset( $registered['status'], $request['status'] ) && in_array( 'active', $request['status'], true ) ) {
82 $active_theme = wp_get_theme();
83 $active_theme = $this->prepare_item_for_response( $active_theme, $request );
84 $themes[] = $this->prepare_response_for_collection( $active_theme );
85 }
86
87 $response = rest_ensure_response( $themes );
88
89 $response->header( 'X-WP-Total', count( $themes ) );
90 $response->header( 'X-WP-TotalPages', count( $themes ) );
91
92 return $response;
93 }
94
95 /**
96 * Prepares a single theme output for response.
97 *
98 * @since 5.0.0
99 *
100 * @param WP_Theme $theme Theme object.
101 * @param WP_REST_Request $request Request object.
102 * @return WP_REST_Response Response object.
103 */
104 public function prepare_item_for_response( $theme, $request ) {
105 $data = array();
106 $fields = $this->get_fields_for_response( $request );
107
108 if ( in_array( 'theme_supports', $fields, true ) ) {
109 $formats = get_theme_support( 'post-formats' );
110 $formats = is_array( $formats ) ? array_values( $formats[0] ) : array();
111 $formats = array_merge( array( 'standard' ), $formats );
112 $data['theme_supports']['formats'] = $formats;
113
114 $data['theme_supports']['post-thumbnails'] = false;
115 $data['theme_supports']['responsive-embeds'] = (bool) get_theme_support( 'responsive-embeds' );
116 $post_thumbnails = get_theme_support( 'post-thumbnails' );
117
118 if ( $post_thumbnails ) {
119 // $post_thumbnails can contain a nested array of post types.
120 // e.g. array( array( 'post', 'page' ) ).
121 $data['theme_supports']['post-thumbnails'] = is_array( $post_thumbnails ) ? $post_thumbnails[0] : true;
122 }
123 }
124
125 $data = $this->add_additional_fields_to_object( $data, $request );
126
127 // Wrap the data in a response object.
128 $response = rest_ensure_response( $data );
129
130 /**
131 * Filters theme data returned from the REST API.
132 *
133 * @since 5.0.0
134 *
135 * @param WP_REST_Response $response The response object.
136 * @param WP_Theme $theme Theme object used to create response.
137 * @param WP_REST_Request $request Request object.
138 */
139 return apply_filters( 'rest_prepare_theme', $response, $theme, $request );
140 }
141
142 /**
143 * Retrieves the theme's schema, conforming to JSON Schema.
144 *
145 * @since 5.0.0
146 *
147 * @return array Item schema data.
148 */
149 public function get_item_schema() {
150 $schema = array(
151 '$schema' => 'http://json-schema.org/draft-04/schema#',
152 'title' => 'theme',
153 'type' => 'object',
154 'properties' => array(
155 'theme_supports' => array(
156 'description' => __( 'Features supported by this theme.', 'gutenberg' ),
157 'type' => 'array',
158 'readonly' => true,
159 'properties' => array(
160 'formats' => array(
161 'description' => __( 'Post formats supported.', 'gutenberg' ),
162 'type' => 'array',
163 'readonly' => true,
164 ),
165 'post-thumbnails' => array(
166 'description' => __( 'Whether the theme supports post thumbnails.', 'gutenberg' ),
167 'type' => array( 'array', 'bool' ),
168 'readonly' => true,
169 ),
170 'responsive-embeds' => array(
171 'description' => __( 'Whether the theme supports responsive embedded content.', 'gutenberg' ),
172 'type' => 'bool',
173 'readonly' => true,
174 ),
175 ),
176 ),
177 ),
178 );
179
180 return $this->add_additional_fields_schema( $schema );
181 }
182
183 /**
184 * Retrieves the search params for the themes collection.
185 *
186 * @since 5.0.0
187 *
188 * @return array Collection parameters.
189 */
190 public function get_collection_params() {
191 $query_params = parent::get_collection_params();
192
193 $query_params['status'] = array(
194 'description' => __( 'Limit result set to themes assigned one or more statuses.', 'gutenberg' ),
195 'type' => 'array',
196 'items' => array(
197 'enum' => array( 'active' ),
198 'type' => 'string',
199 ),
200 'required' => true,
201 'sanitize_callback' => array( $this, 'sanitize_theme_status' ),
202 );
203
204 /**
205 * Filter collection parameters for the themes controller.
206 *
207 * @since 5.0.0
208 *
209 * @param array $query_params JSON Schema-formatted collection parameters.
210 */
211 return apply_filters( 'rest_themes_collection_params', $query_params );
212 }
213
214 /**
215 * Sanitizes and validates the list of theme status.
216 *
217 * @since 5.0.0
218 *
219 * @param string|array $statuses One or more theme statuses.
220 * @param WP_REST_Request $request Full details about the request.
221 * @param string $parameter Additional parameter to pass to validation.
222 * @return array|WP_Error A list of valid statuses, otherwise WP_Error object.
223 */
224 public function sanitize_theme_status( $statuses, $request, $parameter ) {
225 $statuses = wp_parse_slug_list( $statuses );
226
227 foreach ( $statuses as $status ) {
228 $result = rest_validate_request_arg( $status, $request, $parameter );
229
230 if ( is_wp_error( $result ) ) {
231 return $result;
232 }
233 }
234
235 return $statuses;
236 }
237 }
238