Make WordPress Core

Changeset 60359


Ignore:
Timestamp:
06/27/2025 08:45:08 PM (14 months ago)
Author:
johnbillion
Message:

Posts, Post Types: Correct the schema for the id property of the global styles REST API endpoint.

This property is an integer as it corresponds to a post ID.

Props narenin, TimothyBlynJacobs, audrasjb, johnbillion, mikinc860, abcd95

Fixes #61911

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-controller.php

    r59048 r60359  
    8989                register_rest_route(
    9090                        $this->namespace,
    91                         '/' . $this->rest_base . '/(?P<id>[\/\w-]+)',
     91                        '/' . $this->rest_base . '/(?P<id>[\/\d+]+)',
    9292                        array(
    9393                                array(
     
    9797                                        'args'                => array(
    9898                                                'id' => array(
    99                                                         'description'       => __( 'The id of a template' ),
    100                                                         'type'              => 'string',
    101                                                         'sanitize_callback' => array( $this, '_sanitize_global_styles_callback' ),
     99                                                        'description' => __( 'ID of global styles config.' ),
     100                                                        'type'        => 'integer',
    102101                                                ),
    103102                                        ),
     
    116115
    117116        /**
    118          * Sanitize the global styles ID or stylesheet to decode endpoint.
     117         * Sanitize the global styles stylesheet to decode endpoint.
    119118         * For example, `wp/v2/global-styles/twentytwentytwo%200.4.0`
    120119         * would be decoded to `twentytwentytwo 0.4.0`.
     
    122121         * @since 5.9.0
    123122         *
    124          * @param string $id_or_stylesheet Global styles ID or stylesheet.
    125          * @return string Sanitized global styles ID or stylesheet.
    126          */
    127         public function _sanitize_global_styles_callback( $id_or_stylesheet ) {
    128                 return urldecode( $id_or_stylesheet );
     123         * @param string $stylesheet Global styles stylesheet.
     124         * @return string Sanitized global styles stylesheet.
     125         */
     126        public function _sanitize_global_styles_callback( $stylesheet ) {
     127                return urldecode( $stylesheet );
    129128        }
    130129
     
    140139                $error = new WP_Error(
    141140                        'rest_global_styles_not_found',
    142                         __( 'No global styles config exist with that id.' ),
     141                        __( 'No global styles config exists with that ID.' ),
    143142                        array( 'status' => 404 )
    144143                );
     
    465464                                'id'       => array(
    466465                                        'description' => __( 'ID of global styles config.' ),
    467                                         'type'        => 'string',
     466                                        'type'        => 'integer',
    468467                                        'context'     => array( 'embed', 'view', 'edit' ),
    469468                                        'readonly'    => true,
  • trunk/tests/phpunit/tests/rest-api/rest-global-styles-controller.php

    r59048 r60359  
    134134                $routes = rest_get_server()->get_routes();
    135135                $this->assertArrayHasKey(
    136                         '/wp/v2/global-styles/(?P<id>[\/\w-]+)',
     136                        '/wp/v2/global-styles/(?P<id>[\/\d+]+)',
    137137                        $routes,
    138138                        'Single global style based on the given ID route does not exist'
     
    140140                $this->assertCount(
    141141                        2,
    142                         $routes['/wp/v2/global-styles/(?P<id>[\/\w-]+)'],
     142                        $routes['/wp/v2/global-styles/(?P<id>[\/\d+]+)'],
    143143                        'Single global style based on the given ID route does not have exactly two elements'
    144144                );
     
    409409                        '2 subdirectories deep'  => array(
    410410                                'theme_dirname' => 'subdir/subsubdir/mytheme',
    411                                 'expected'      => 'rest_global_styles_not_found',
     411                                'expected'      => 'rest_no_route',
    412412                        ),
    413413                );
     
    777777                }
    778778        }
     779
     780        /**
     781         * Test that the route accepts integer IDs.
     782         *
     783         * @ticket 61911
     784         */
     785        public function test_global_styles_route_accepts_integer_id() {
     786                wp_set_current_user( self::$admin_id );
     787                $request  = new WP_REST_Request( 'GET', '/wp/v2/global-styles/' . self::$global_styles_id );
     788                $response = rest_get_server()->dispatch( $request );
     789
     790                $this->assertEquals( 200, $response->get_status() );
     791
     792                $data = $response->get_data();
     793                $this->assertIsInt( $data['id'] );
     794                $this->assertSame( self::$global_styles_id, $data['id'] );
     795        }
     796
     797        /**
     798         * Test that the schema defines ID as an integer.
     799         *
     800         * @ticket 61911
     801         */
     802        public function test_global_styles_schema_id_type() {
     803                $request  = new WP_REST_Request( 'OPTIONS', '/wp/v2/global-styles/' . self::$global_styles_id );
     804                $response = rest_get_server()->dispatch( $request );
     805
     806                $data   = $response->get_data();
     807                $schema = $data['schema'];
     808
     809                $this->assertArrayHasKey( 'properties', $schema );
     810                $this->assertArrayHasKey( 'id', $schema['properties'] );
     811                $this->assertArrayHasKey( 'type', $schema['properties']['id'] );
     812                $this->assertSame( 'integer', $schema['properties']['id']['type'] );
     813        }
     814
     815        /**
     816         * Test that the route argument schema defines ID as an integer.
     817         *
     818         * @ticket 61911
     819         */
     820        public function test_global_styles_route_args_schema() {
     821                $routes     = rest_get_server()->get_routes();
     822                $route_data = $routes['/wp/v2/global-styles/(?P<id>[\/\d+]+)'];
     823
     824                $this->assertArrayHasKey( 'args', $route_data[0] );
     825                $this->assertArrayHasKey( 'id', $route_data[0]['args'] );
     826                $this->assertArrayHasKey( 'type', $route_data[0]['args']['id'] );
     827                $this->assertSame( 'integer', $route_data[0]['args']['id']['type'] );
     828        }
    779829}
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r59532 r60359  
    134134                        '/wp/v2/comments',
    135135                        '/wp/v2/comments/(?P<id>[\\d]+)',
    136                         '/wp/v2/global-styles/(?P<id>[\/\w-]+)',
     136                        '/wp/v2/global-styles/(?P<id>[\/\d+]+)',
    137137                        '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions',
    138138                        '/wp/v2/global-styles/(?P<parent>[\d]+)/revisions/(?P<id>[\d]+)',
  • trunk/tests/qunit/fixtures/wp-api-generated.js

    r60197 r60359  
    68976897            ]
    68986898        },
    6899         "/wp/v2/global-styles/(?P<id>[\\/\\w-]+)": {
     6899        "/wp/v2/global-styles/(?P<id>[\\/\\d+]+)": {
    69006900            "namespace": "wp/v2",
    69016901            "methods": [
     
    69156915                    "args": {
    69166916                        "id": {
    6917                             "description": "The id of a template",
    6918                             "type": "string",
     6917                            "description": "ID of global styles config.",
     6918                            "type": "integer",
    69196919                            "required": false
    69206920                        }
Note: See TracChangeset for help on using the changeset viewer.