Changeset 57494
- Timestamp:
- 01/31/2024 10:39:04 AM (12 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php
r56272 r57494 48 48 * 49 49 * @since 6.3.0 50 * @since 6.5.0 Added route to fetch individual global styles revisions. 50 51 */ 51 52 public function register_routes() { … … 65 66 'permission_callback' => array( $this, 'get_item_permissions_check' ), 66 67 'args' => $this->get_collection_params(), 68 ), 69 'schema' => array( $this, 'get_public_item_schema' ), 70 ) 71 ); 72 73 register_rest_route( 74 $this->namespace, 75 '/' . $this->parent_base . '/(?P<parent>[\d]+)/' . $this->rest_base . '/(?P<id>[\d]+)', 76 array( 77 'args' => array( 78 'parent' => array( 79 'description' => __( 'The ID for the parent of the global styles revision.' ), 80 'type' => 'integer', 81 ), 82 'id' => array( 83 'description' => __( 'Unique identifier for the global styles revision.' ), 84 'type' => 'integer', 85 ), 86 ), 87 array( 88 'methods' => WP_REST_Server::READABLE, 89 'callback' => array( $this, 'get_item' ), 90 'permission_callback' => array( $this, 'get_item_permissions_check' ), 91 'args' => array( 92 'context' => $this->get_context_param( array( 'default' => 'view' ) ), 93 ), 67 94 ), 68 95 'schema' => array( $this, 'get_public_item_schema' ), … … 243 270 244 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 /** 245 322 * Checks the post_date_gmt or modified_gmt and prepare any post or 246 323 * modified date for single post output. -
trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php
r56714 r57494 227 227 /** 228 228 * @ticket 58524 229 * @ticket 59810 229 230 * 230 231 * @covers WP_REST_Global_Styles_Controller::register_routes … … 236 237 $routes, 237 238 'Global style revisions based on the given parentID route does not exist.' 239 ); 240 $this->assertArrayHasKey( 241 '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)', 242 $routes, 243 'Single global style revisions based on the given parentID and revision ID route does not exist.' 238 244 ); 239 245 } … … 303 309 $this->assertSame( $this->revision_1_id, $data[2]['id'] ); 304 310 $this->check_get_revision_response( $data[2], $this->revision_1 ); 311 } 312 313 /** 314 * @ticket 59810 315 * 316 * @covers WP_REST_Global_Styles_Controller::get_item 317 */ 318 public function test_get_item() { 319 wp_set_current_user( self::$admin_id ); 320 321 $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/' . $this->revision_1_id ); 322 $response = rest_get_server()->dispatch( $request ); 323 $data = $response->get_data(); 324 325 $this->assertSame( 200, $response->get_status(), 'Response status is 200.' ); 326 $this->check_get_revision_response( $data, $this->revision_1 ); 327 } 328 329 /** 330 * @ticket 59810 331 * 332 * @covers WP_REST_Global_Styles_Controller::get_revision 333 */ 334 public function test_get_item_invalid_revision_id_should_error() { 335 wp_set_current_user( self::$admin_id ); 336 337 $expected_error = 'rest_post_invalid_id'; 338 $expected_status = 404; 339 $request = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id . '/revisions/20000001' ); 340 $response = rest_get_server()->dispatch( $request ); 341 342 $this->assertErrorResponse( $expected_error, $response, $expected_status ); 305 343 } 306 344 … … 798 836 * @doesNotPerformAssertions 799 837 */ 800 public function test_get_item() {801 // Controller does not implement get_item().802 }803 804 /**805 * @doesNotPerformAssertions806 */807 838 public function test_create_item() { 808 839 // Controller does not implement create_item(). -
trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php
r56819 r57494 136 136 '/wp/v2/global-styles/(?P<id>[\/\w-]+)', 137 137 '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions', 138 '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)', 138 139 '/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations', 139 140 '/wp/v2/global-styles/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)', -
trunk/tests/qunit/fixtures/wp-api-generated.js
r57044 r57494 10045 10045 ] 10046 10046 }, 10047 "/wp/v2/global-styles/(?P<parent>[\\d]+)/revisions/(?P<id>[\\d]+)": { 10048 "namespace": "wp/v2", 10049 "methods": [ 10050 "GET" 10051 ], 10052 "endpoints": [ 10053 { 10054 "methods": [ 10055 "GET" 10056 ], 10057 "args": { 10058 "parent": { 10059 "description": "The ID for the parent of the global styles revision.", 10060 "type": "integer", 10061 "required": false 10062 }, 10063 "id": { 10064 "description": "Unique identifier for the global styles revision.", 10065 "type": "integer", 10066 "required": false 10067 }, 10068 "context": { 10069 "description": "Scope under which the request is made; determines fields present in response.", 10070 "type": "string", 10071 "enum": [ 10072 "view", 10073 "embed", 10074 "edit" 10075 ], 10076 "default": "view", 10077 "required": false 10078 } 10079 } 10080 } 10081 ] 10082 }, 10047 10083 "/wp/v2/global-styles/themes/(?P<stylesheet>[\\/\\s%\\w\\.\\(\\)\\[\\]\\@_\\-]+)/variations": { 10048 10084 "namespace": "wp/v2",
Note: See TracChangeset
for help on using the changeset viewer.