Make WordPress Core

Changeset 57494


Ignore:
Timestamp:
01/31/2024 10:39:04 AM (12 months ago)
Author:
youknowriad
Message:

REST API: Add route for single styles revisions.

Adds a route for single global styles revisions: /wp/v2/global-styles/${ parentId }/revisions/${ revisionsId }
This fixes the getRevision actions in the core-data package.

Props ramonopoly, get_dave.
Fixes #59810.

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  
    4848     *
    4949     * @since 6.3.0
     50     * @since 6.5.0 Added route to fetch individual global styles revisions.
    5051     */
    5152    public function register_routes() {
     
    6566                    'permission_callback' => array( $this, 'get_item_permissions_check' ),
    6667                    '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                    ),
    6794                ),
    6895                'schema' => array( $this, 'get_public_item_schema' ),
     
    243270
    244271    /**
     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    /**
    245322     * Checks the post_date_gmt or modified_gmt and prepare any post or
    246323     * modified date for single post output.
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-revisions-controller.php

    r56714 r57494  
    227227    /**
    228228     * @ticket 58524
     229     * @ticket 59810
    229230     *
    230231     * @covers WP_REST_Global_Styles_Controller::register_routes
     
    236237            $routes,
    237238            '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.'
    238244        );
    239245    }
     
    303309        $this->assertSame( $this->revision_1_id, $data[2]['id'] );
    304310        $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 );
    305343    }
    306344
     
    798836     * @doesNotPerformAssertions
    799837     */
    800     public function test_get_item() {
    801         // Controller does not implement get_item().
    802     }
    803 
    804     /**
    805      * @doesNotPerformAssertions
    806      */
    807838    public function test_create_item() {
    808839        // Controller does not implement create_item().
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r56819 r57494  
    136136            '/wp/v2/global-styles/(?P<id>[\/\w-]+)',
    137137            '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions',
     138            '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
    138139            '/wp/v2/global-styles/themes/(?P<stylesheet>[\/\s%\w\.\(\)\[\]\@_\-]+)/variations',
    139140            '/wp/v2/global-styles/themes/(?P<stylesheet>[^\/:<>\*\?"\|]+(?:\/[^\/:<>\*\?"\|]+)?)',
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r57044 r57494  
    1004510045            ]
    1004610046        },
     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        },
    1004710083        "/wp/v2/global-styles/themes/(?P<stylesheet>[\\/\\s%\\w\\.\\(\\)\\[\\]\\@_\\-]+)/variations": {
    1004810084            "namespace": "wp/v2",
Note: See TracChangeset for help on using the changeset viewer.