| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * REST API: WP_REST_Global_Styles_Revisions_Controller class 4 * 5 * @package WordPress 6 * @subpackage REST_API 7 * @since 6.3.0 8 */ 9 10 /** 11 * Core class used to access global styles revisions via the REST API. 12 * 13 * @since 6.3.0 14 * 15 * @see WP_REST_Controller 16 */ 17 class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Revisions_Controller { 18 /** 19 * Parent controller. 20 * 21 * @since 6.6.0 22 * @var WP_REST_Controller 23 */ 24 private $parent_controller; 25 26 /** 27 * The base of the parent controller's route. 28 * 29 * @since 6.3.0 30 * @var string 31 */ 32 protected $parent_base; 33 34 /** 35 * Parent post type. 36 * 37 * @since 6.6.0 38 * @var string 39 */ 40 protected $parent_post_type; 41 42 /** 43 * Constructor. 44 * 45 * @since 6.3.0 46 * @since 6.6.0 Extends class from WP_REST_Revisions_Controller. 47 * 48 * @param string $parent_post_type Post type of the parent. 49 */ 50 public function __construct( $parent_post_type = 'wp_global_styles' ) { 51 parent::__construct( $parent_post_type ); 52 $post_type_object = get_post_type_object( $parent_post_type ); 53 $parent_controller = $post_type_object->get_rest_controller(); 54 55 if ( ! $parent_controller ) { 56 $parent_controller = new WP_REST_Global_Styles_Controller( $parent_post_type ); 57 } 58 59 $this->parent_controller = $parent_controller; 60 $this->rest_base = 'revisions'; 61 $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; 62 $this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2'; 63 } 64 65 /** 66 * Registers the controller's routes. 67 * 68 * @since 6.3.0 69 * @since 6.6.0 Added route to fetch individual global styles revisions. 70 */ 71 public function register_routes() { 72 register_rest_route( 73 $this->namespace, 74 '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base, 75 array( 76 'args' => array( 77 'parent' => array( 78 'description' => __( 'The ID for the parent of the revision.' ), 79 'type' => 'integer', 80 ), 81 ), 82 array( 83 'methods' => WP_REST_Server::READABLE, 84 'callback' => array( $this, 'get_items' ), 85 'permission_callback' => array( $this, 'get_items_permissions_check' ), 86 'args' => $this->get_collection_params(), 87 ), 88 'schema' => array( $this, 'get_public_item_schema' ), 89 ) 90 ); 91 92 register_rest_route( 93 $this->namespace, 94 '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)', 95 array( 96 'args' => array( 97 'parent' => array( 98 'description' => __( 'The ID for the parent of the global styles revision.' ), 99 'type' => 'integer', 100 ), 101 'id' => array( 102 'description' => __( 'Unique identifier for the global styles revision.' ), 103 'type' => 'integer', 104 ), 105 ), 106 array( 107 'methods' => WP_REST_Server::READABLE, 108 'callback' => array( $this, 'get_item' ), 109 'permission_callback' => array( $this, 'get_item_permissions_check' ), 110 'args' => array( 111 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 112 ), 113 ), 114 'schema' => array( $this, 'get_public_item_schema' ), 115 ) 116 ); 117 } 118 119 /** 120 * Returns decoded JSON from post content string, 121 * or a 404 if not found. 122 * 123 * @since 6.3.0 124 * 125 * @param string $raw_json Encoded JSON from global styles custom post content. 126 * @return Array|WP_Error 127 */ 128 protected function get_decoded_global_styles_json( $raw_json ) { 129 $decoded_json = json_decode( $raw_json, true ); 130 131 if ( is_array( $decoded_json ) && isset( $decoded_json['isGlobalStylesUserThemeJSON'] ) && true === $decoded_json['isGlobalStylesUserThemeJSON'] ) { 132 return $decoded_json; 133 } 134 135 return new WP_Error( 136 'rest_global_styles_not_found', 137 __( 'Cannot find user global styles revisions.' ), 138 array( 'status' => 404 ) 139 ); 140 } 141 142 /** 143 * Returns paginated revisions of the given global styles config custom post type. 144 * 145 * The bulk of the body is taken from WP_REST_Revisions_Controller->get_items, 146 * but global styles does not require as many parameters. 147 * 148 * @since 6.3.0 149 * 150 * @param WP_REST_Request $request The request instance. 151 * @return WP_REST_Response|WP_Error 152 */ 153 public function get_items( $request ) { 154 $parent = $this->get_parent( $request['parent'] ); 155 156 if ( is_wp_error( $parent ) ) { 157 return $parent; 158 } 159 160 $global_styles_config = $this->get_decoded_global_styles_json( $parent->post_content ); 161 162 if ( is_wp_error( $global_styles_config ) ) { 163 return $global_styles_config; 164 } 165 166 $is_head_request = $request->is_method( 'HEAD' ); 167 168 if ( wp_revisions_enabled( $parent ) ) { 169 $registered = $this->get_collection_params(); 170 $query_args = array( 171 'post_parent' => $parent->ID, 172 'post_type' => 'revision', 173 'post_status' => 'inherit', 174 'posts_per_page' => -1, 175 'orderby' => 'date ID', 176 'order' => 'DESC', 177 ); 178 179 $parameter_mappings = array( 180 'offset' => 'offset', 181 'page' => 'paged', 182 'per_page' => 'posts_per_page', 183 ); 184 185 foreach ( $parameter_mappings as $api_param => $wp_param ) { 186 if ( isset( $registered[ $api_param ], $request[ $api_param ] ) ) { 187 $query_args[ $wp_param ] = $request[ $api_param ]; 188 } 189 } 190 191 if ( $is_head_request ) { 192 // Force the 'fields' argument. For HEAD requests, only post IDs are required to calculate pagination. 193 $query_args['fields'] = 'ids'; 194 // Disable priming post meta for HEAD requests to improve performance. 195 $query_args['update_post_term_cache'] = false; 196 $query_args['update_post_meta_cache'] = false; 197 } 198 199 $revisions_query = new WP_Query(); 200 $revisions = $revisions_query->query( $query_args ); 201 $offset = isset( $query_args['offset'] ) ? (int) $query_args['offset'] : 0; 202 $page = isset( $query_args['paged'] ) ? (int) $query_args['paged'] : 0; 203 $total_revisions = $revisions_query->found_posts; 204 205 if ( $total_revisions < 1 ) { 206 // Out-of-bounds, run the query without pagination/offset to get the total count. 207 unset( $query_args['paged'], $query_args['offset'] ); 208 209 $count_query = new WP_Query(); 210 $query_args['fields'] = 'ids'; 211 $query_args['posts_per_page'] = 1; 212 $query_args['update_post_meta_cache'] = false; 213 $query_args['update_post_term_cache'] = false; 214 215 $count_query->query( $query_args ); 216 217 $total_revisions = $count_query->found_posts; 218 } 219 220 if ( $revisions_query->query_vars['posts_per_page'] > 0 ) { 221 $max_pages = (int) ceil( $total_revisions / (int) $revisions_query->query_vars['posts_per_page'] ); 222 } else { 223 $max_pages = $total_revisions > 0 ? 1 : 0; 224 } 225 if ( $total_revisions > 0 ) { 226 if ( $offset >= $total_revisions ) { 227 return new WP_Error( 228 'rest_revision_invalid_offset_number', 229 __( 'The offset number requested is larger than or equal to the number of available revisions.' ), 230 array( 'status' => 400 ) 231 ); 232 } elseif ( ! $offset && $page > $max_pages ) { 233 return new WP_Error( 234 'rest_revision_invalid_page_number', 235 __( 'The page number requested is larger than the number of pages available.' ), 236 array( 'status' => 400 ) 237 ); 238 } 239 } 240 } else { 241 $revisions = array(); 242 $total_revisions = 0; 243 $max_pages = 0; 244 $page = (int) $request['page']; 245 } 246 247 if ( ! $is_head_request ) { 248 $response = array(); 249 250 foreach ( $revisions as $revision ) { 251 $data = $this->prepare_item_for_response( $revision, $request ); 252 $response[] = $this->prepare_response_for_collection( $data ); 253 } 254 255 $response = rest_ensure_response( $response ); 256 } else { 257 $response = new WP_REST_Response( array() ); 258 } 259 260 $response->header( 'X-WP-Total', (int) $total_revisions ); 261 $response->header( 'X-WP-TotalPages', (int) $max_pages ); 262 263 $request_params = $request->get_query_params(); 264 $base_path = rest_url( sprintf( '%s/%s/%d/%s', $this->namespace, $this->parent_base, $request['parent'], $this->rest_base ) ); 265 $base = add_query_arg( urlencode_deep( $request_params ), $base_path ); 266 267 if ( $page > 1 ) { 268 $prev_page = $page - 1; 269 270 if ( $prev_page > $max_pages ) { 271 $prev_page = $max_pages; 272 } 273 274 $prev_link = add_query_arg( 'page', $prev_page, $base ); 275 $response->link_header( 'prev', $prev_link ); 276 } 277 if ( $max_pages > $page ) { 278 $next_page = $page + 1; 279 $next_link = add_query_arg( 'page', $next_page, $base ); 280 281 $response->link_header( 'next', $next_link ); 282 } 283 284 return $response; 285 } 286 287 /** 288 * Prepares the revision for the REST response. 289 * 290 * @since 6.3.0 291 * @since 6.6.0 Added resolved URI links to the response. 292 * 293 * @param WP_Post $post Post revision object. 294 * @param WP_REST_Request $request Request object. 295 * @return WP_REST_Response|WP_Error Response object. 296 */ 297 public function prepare_item_for_response( $post, $request ) { 298 // Don't prepare the response body for HEAD requests. 299 if ( $request->is_method( 'HEAD' ) ) { 300 return new WP_REST_Response( array() ); 301 } 302 303 $parent = $this->get_parent( $request['parent'] ); 304 $global_styles_config = $this->get_decoded_global_styles_json( $post->post_content ); 305 306 if ( is_wp_error( $global_styles_config ) ) { 307 return $global_styles_config; 308 } 309 310 $fields = $this->get_fields_for_response( $request ); 311 $data = array(); 312 $theme_json = null; 313 314 if ( ! empty( $global_styles_config['styles'] ) || ! empty( $global_styles_config['settings'] ) ) { 315 $theme_json = new WP_Theme_JSON( $global_styles_config, 'custom' ); 316 $global_styles_config = $theme_json->get_raw_data(); 317 if ( rest_is_field_included( 'settings', $fields ) ) { 318 $data['settings'] = ! empty( $global_styles_config['settings'] ) ? $global_styles_config['settings'] : new stdClass(); 319 } 320 if ( rest_is_field_included( 'styles', $fields ) ) { 321 $data['styles'] = ! empty( $global_styles_config['styles'] ) ? $global_styles_config['styles'] : new stdClass(); 322 } 323 } 324 325 if ( rest_is_field_included( 'author', $fields ) ) { 326 $data['author'] = (int) $post->post_author; 327 } 328 329 if ( rest_is_field_included( 'date', $fields ) ) { 330 $data['date'] = $this->prepare_date_response( $post->post_date_gmt, $post->post_date ); 331 } 332 333 if ( rest_is_field_included( 'date_gmt', $fields ) ) { 334 $data['date_gmt'] = $this->prepare_date_response( $post->post_date_gmt ); 335 } 336 337 if ( rest_is_field_included( 'id', $fields ) ) { 338 $data['id'] = (int) $post->ID; 339 } 340 341 if ( rest_is_field_included( 'modified', $fields ) ) { 342 $data['modified'] = $this->prepare_date_response( $post->post_modified_gmt, $post->post_modified ); 343 } 344 345 if ( rest_is_field_included( 'modified_gmt', $fields ) ) { 346 $data['modified_gmt'] = $this->prepare_date_response( $post->post_modified_gmt ); 347 } 348 349 if ( rest_is_field_included( 'parent', $fields ) ) { 350 $data['parent'] = (int) $parent->ID; 351 } 352 353 $context = ! empty( $request['context'] ) ? $request['context'] : 'view'; 354 $data = $this->add_additional_fields_to_object( $data, $request ); 355 $data = $this->filter_response_by_context( $data, $context ); 356 $response = rest_ensure_response( $data ); 357 $resolved_theme_uris = WP_Theme_JSON_Resolver::get_resolved_theme_uris( $theme_json ); 358 359 if ( ! empty( $resolved_theme_uris ) ) { 360 $response->add_links( 361 array( 362 'https://api.w.org/theme-file' => $resolved_theme_uris, 363 ) 364 ); 365 } 366 367 return $response; 368 } 369 370 /** 371 * Retrieves the revision's schema, conforming to JSON Schema. 372 * 373 * @since 6.3.0 374 * @since 6.6.0 Merged parent and parent controller schema data. 375 * 376 * @return array Item schema data. 377 */ 378 public function get_item_schema() { 379 if ( $this->schema ) { 380 return $this->add_additional_fields_schema( $this->schema ); 381 } 382 383 $schema = parent::get_item_schema(); 384 $parent_schema = $this->parent_controller->get_item_schema(); 385 $schema['properties'] = array_merge( $schema['properties'], $parent_schema['properties'] ); 386 387 unset( 388 $schema['properties']['guid'], 389 $schema['properties']['slug'], 390 $schema['properties']['meta'], 391 $schema['properties']['content'], 392 $schema['properties']['title'] 393 ); 394 395 $this->schema = $schema; 396 397 return $this->add_additional_fields_schema( $this->schema ); 398 } 399 400 /** 401 * Retrieves the query params for collections. 402 * Removes params that are not supported by global styles revisions. 403 * 404 * @since 6.6.0 405 * 406 * @return array Collection parameters. 407 */ 408 public function get_collection_params() { 409 $query_params = parent::get_collection_params(); 410 unset( 411 $query_params['exclude'], 412 $query_params['include'], 413 $query_params['search'], 414 $query_params['order'], 415 $query_params['orderby'] 416 ); 417 return $query_params; 418 } 419 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Thu Oct 30 08:20:06 2025 | Cross-referenced by PHPXref |