Changeset 57628 for trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php
- Timestamp:
- 02/13/2024 03:10:37 PM (10 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php
r57624 r57628 15 15 * @see WP_REST_Controller 16 16 */ 17 class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_ Revisions_Controller {18 /** 19 * Parent controller.20 * 21 * @since 6. 5.022 * @var WP_REST_Controller23 */ 24 pr ivate $parent_controller;17 class WP_REST_Global_Styles_Revisions_Controller extends WP_REST_Controller { 18 /** 19 * Parent post type. 20 * 21 * @since 6.3.0 22 * @var string 23 */ 24 protected $parent_post_type; 25 25 26 26 /** … … 36 36 * 37 37 * @since 6.3.0 38 * @since 6.5.0 Extends class from WP_REST_Revisions_Controller. 39 * 40 * @param string $parent_post_type Post type of the parent. 41 */ 42 public function __construct( $parent_post_type ) { 43 parent::__construct( $parent_post_type ); 44 $post_type_object = get_post_type_object( $parent_post_type ); 45 $parent_controller = $post_type_object->get_rest_controller(); 46 47 if ( ! $parent_controller ) { 48 $parent_controller = new WP_REST_Global_Styles_Controller( $parent_post_type ); 49 } 50 51 $this->parent_controller = $parent_controller; 52 $this->rest_base = 'revisions'; 53 $this->parent_base = ! empty( $post_type_object->rest_base ) ? $post_type_object->rest_base : $post_type_object->name; 54 $this->namespace = ! empty( $post_type_object->rest_namespace ) ? $post_type_object->rest_namespace : 'wp/v2'; 38 */ 39 public function __construct() { 40 $this->parent_post_type = 'wp_global_styles'; 41 $this->rest_base = 'revisions'; 42 $this->parent_base = 'global-styles'; 43 $this->namespace = 'wp/v2'; 55 44 } 56 45 … … 75 64 'methods' => WP_REST_Server::READABLE, 76 65 'callback' => array( $this, 'get_items' ), 77 'permission_callback' => array( $this, 'get_item s_permissions_check' ),66 'permission_callback' => array( $this, 'get_item_permissions_check' ), 78 67 'args' => $this->get_collection_params(), 79 68 ), … … 107 96 ) 108 97 ); 98 } 99 100 /** 101 * Retrieves the query params for collections. 102 * 103 * Inherits from WP_REST_Controller::get_collection_params(), 104 * also reflects changes to return value WP_REST_Revisions_Controller::get_collection_params(). 105 * 106 * @since 6.3.0 107 * 108 * @return array Collection parameters. 109 */ 110 public function get_collection_params() { 111 $collection_params = parent::get_collection_params(); 112 $collection_params['context']['default'] = 'view'; 113 $collection_params['offset'] = array( 114 'description' => __( 'Offset the result set by a specific number of items.' ), 115 'type' => 'integer', 116 ); 117 unset( $collection_params['search'] ); 118 unset( $collection_params['per_page']['default'] ); 119 120 return $collection_params; 109 121 } 110 122 … … 258 270 259 271 /** 272 * Retrieves one global styles revision from the collection. 273 * 274 * @since 6.5.0 275 * 276 * @param WP_REST_Request $request Full details about the request. 277 * @return WP_REST_Response|WP_Error Response object on success, or WP_Error object on failure. 278 */ 279 public function get_item( $request ) { 280 $parent = $this->get_parent( $request['parent'] ); 281 if ( is_wp_error( $parent ) ) { 282 return $parent; 283 } 284 285 $revision = $this->get_revision( $request['id'] ); 286 if ( is_wp_error( $revision ) ) { 287 return $revision; 288 } 289 290 $response = $this->prepare_item_for_response( $revision, $request ); 291 return rest_ensure_response( $response ); 292 } 293 294 /** 295 * Gets the global styles revision, if the ID is valid. 296 * 297 * @since 6.5.0 298 * 299 * @param int $id Supplied ID. 300 * @return WP_Post|WP_Error Revision post object if ID is valid, WP_Error otherwise. 301 */ 302 protected function get_revision( $id ) { 303 $error = new WP_Error( 304 'rest_post_invalid_id', 305 __( 'Invalid global styles revision ID.' ), 306 array( 'status' => 404 ) 307 ); 308 309 if ( (int) $id <= 0 ) { 310 return $error; 311 } 312 313 $revision = get_post( (int) $id ); 314 if ( empty( $revision ) || empty( $revision->ID ) || 'revision' !== $revision->post_type ) { 315 return $error; 316 } 317 318 return $revision; 319 } 320 321 /** 322 * Checks the post_date_gmt or modified_gmt and prepare any post or 323 * modified date for single post output. 324 * 325 * Duplicate of WP_REST_Revisions_Controller::prepare_date_response. 326 * 327 * @since 6.3.0 328 * 329 * @param string $date_gmt GMT publication time. 330 * @param string|null $date Optional. Local publication time. Default null. 331 * @return string|null ISO8601/RFC3339 formatted datetime, otherwise null. 332 */ 333 protected function prepare_date_response( $date_gmt, $date = null ) { 334 if ( '0000-00-00 00:00:00' === $date_gmt ) { 335 return null; 336 } 337 338 if ( isset( $date ) ) { 339 return mysql_to_rfc3339( $date ); 340 } 341 342 return mysql_to_rfc3339( $date_gmt ); 343 } 344 345 /** 260 346 * Prepares the revision for the REST response. 261 347 * … … 326 412 * 327 413 * @since 6.3.0 328 * @since 6.5.0 Merged parent and parent controller schema data.329 414 * 330 415 * @return array Item schema data. … … 335 420 } 336 421 337 $schema = parent::get_item_schema(); 338 $parent_schema = $this->parent_controller->get_item_schema(); 339 $schema['properties'] = array_merge( $schema['properties'], $parent_schema['properties'] ); 340 341 unset( $schema['properties']['guid'] ); 342 unset( $schema['properties']['slug'] ); 343 unset( $schema['properties']['meta'] ); 344 unset( $schema['properties']['content'] ); 345 unset( $schema['properties']['title'] ); 422 $schema = array( 423 '$schema' => 'http://json-schema.org/draft-04/schema#', 424 'title' => "{$this->parent_post_type}-revision", 425 'type' => 'object', 426 // Base properties for every revision. 427 'properties' => array( 428 429 /* 430 * Adds settings and styles from the WP_REST_Revisions_Controller item fields. 431 * Leaves out GUID as global styles shouldn't be accessible via URL. 432 */ 433 'author' => array( 434 'description' => __( 'The ID for the author of the revision.' ), 435 'type' => 'integer', 436 'context' => array( 'view', 'edit', 'embed' ), 437 ), 438 'date' => array( 439 'description' => __( "The date the revision was published, in the site's timezone." ), 440 'type' => 'string', 441 'format' => 'date-time', 442 'context' => array( 'view', 'edit', 'embed' ), 443 ), 444 'date_gmt' => array( 445 'description' => __( 'The date the revision was published, as GMT.' ), 446 'type' => 'string', 447 'format' => 'date-time', 448 'context' => array( 'view', 'edit' ), 449 ), 450 'id' => array( 451 'description' => __( 'Unique identifier for the revision.' ), 452 'type' => 'integer', 453 'context' => array( 'view', 'edit', 'embed' ), 454 ), 455 'modified' => array( 456 'description' => __( "The date the revision was last modified, in the site's timezone." ), 457 'type' => 'string', 458 'format' => 'date-time', 459 'context' => array( 'view', 'edit' ), 460 ), 461 'modified_gmt' => array( 462 'description' => __( 'The date the revision was last modified, as GMT.' ), 463 'type' => 'string', 464 'format' => 'date-time', 465 'context' => array( 'view', 'edit' ), 466 ), 467 'parent' => array( 468 'description' => __( 'The ID for the parent of the revision.' ), 469 'type' => 'integer', 470 'context' => array( 'view', 'edit', 'embed' ), 471 ), 472 473 // Adds settings and styles from the WP_REST_Global_Styles_Controller parent schema. 474 'styles' => array( 475 'description' => __( 'Global styles.' ), 476 'type' => array( 'object' ), 477 'context' => array( 'view', 'edit' ), 478 ), 479 'settings' => array( 480 'description' => __( 'Global settings.' ), 481 'type' => array( 'object' ), 482 'context' => array( 'view', 'edit' ), 483 ), 484 ), 485 ); 346 486 347 487 $this->schema = $schema; … … 351 491 352 492 /** 353 * Retrieves the query params for collections. 354 * Removes params that are not supported by global styles revisions. 355 * 356 * @since 6.5.0 357 * 358 * @return array Collection parameters. 359 */ 360 public function get_collection_params() { 361 $query_params = parent::get_collection_params(); 362 unset( $query_params['exclude'] ); 363 unset( $query_params['include'] ); 364 unset( $query_params['search'] ); 365 unset( $query_params['order'] ); 366 unset( $query_params['orderby'] ); 367 return $query_params; 493 * Checks if a given request has access to read a single global style. 494 * 495 * @since 6.3.0 496 * 497 * @param WP_REST_Request $request Full details about the request. 498 * @return true|WP_Error True if the request has read access, WP_Error object otherwise. 499 */ 500 public function get_item_permissions_check( $request ) { 501 $post = $this->get_parent( $request['parent'] ); 502 if ( is_wp_error( $post ) ) { 503 return $post; 504 } 505 506 /* 507 * The same check as WP_REST_Global_Styles_Controller::get_item_permissions_check. 508 */ 509 if ( ! current_user_can( 'read_post', $post->ID ) ) { 510 return new WP_Error( 511 'rest_cannot_view', 512 __( 'Sorry, you are not allowed to view revisions for this global style.' ), 513 array( 'status' => rest_authorization_required_code() ) 514 ); 515 } 516 517 return true; 518 } 519 520 /** 521 * Gets the parent post, if the ID is valid. 522 * 523 * Duplicate of WP_REST_Revisions_Controller::get_parent. 524 * 525 * @since 6.3.0 526 * 527 * @param int $parent_post_id Supplied ID. 528 * @return WP_Post|WP_Error Post object if ID is valid, WP_Error otherwise. 529 */ 530 protected function get_parent( $parent_post_id ) { 531 $error = new WP_Error( 532 'rest_post_invalid_parent', 533 __( 'Invalid post parent ID.' ), 534 array( 'status' => 404 ) 535 ); 536 537 if ( (int) $parent_post_id <= 0 ) { 538 return $error; 539 } 540 541 $parent_post = get_post( (int) $parent_post_id ); 542 543 if ( empty( $parent_post ) || empty( $parent_post->ID ) 544 || $this->parent_post_type !== $parent_post->post_type 545 ) { 546 return $error; 547 } 548 549 return $parent_post; 368 550 } 369 551 }
Note: See TracChangeset
for help on using the changeset viewer.