WordPress.org

Make WordPress Core

Ticket #47871: 47871.1.diff

File 47871.1.diff, 16.6 KB (added by kadamwhite, 6 months ago)

Add caching to default schema properties for all REST controllers

  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
    index fe7bd06ee0..bc1a280492 100644
    class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    417417         * @return array Item schema as an array.
    418418         */
    419419        public function get_item_schema() {
     420                if ( $this->schema ) {
     421                        return $this->add_additional_fields_schema( $this->schema );
     422                }
    420423
    421424                $schema = parent::get_item_schema();
    422425
    class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    513516
    514517                unset( $schema['properties']['password'] );
    515518
    516                 return $schema;
     519                $this->schema = $schema;
     520                return $this->add_additional_fields_schema( $this->schema );
    517521        }
    518522
    519523        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php
    index e55d3a3729..0d015519d3 100644
    class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { 
    296296         * @return array Item schema data.
    297297         */
    298298        public function get_item_schema() {
     299                if ( $this->schema ) {
     300                        return $this->add_additional_fields_schema( $this->schema );
     301                }
     302
    299303                $schema = $this->revisions_controller->get_item_schema();
    300304
    301305                $schema['properties']['preview_link'] = array(
    class WP_REST_Autosaves_Controller extends WP_REST_Revisions_Controller { 
    306310                        'readonly'    => true,
    307311                );
    308312
    309                 return $schema;
     313                $this->schema = $schema;
     314                return $this->add_additional_fields_schema( $this->schema );
    310315        }
    311316
    312317        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php
    index 681e519313..18fbe1e036 100644
    class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { 
    161161         * @return array Item schema data.
    162162         */
    163163        public function get_item_schema() {
    164                 return array(
     164                if ( $this->schema ) {
     165                        return $this->schema;
     166                }
     167
     168                $this->schema = array(
    165169                        '$schema'    => 'http://json-schema.org/schema#',
    166170                        'title'      => 'rendered-block',
    167171                        'type'       => 'object',
    class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { 
    174178                                ),
    175179                        ),
    176180                );
     181                return $this->schema;
    177182        }
    178183}
  • src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php
    index a523764dc5..36d1573a9a 100644
    class WP_REST_Blocks_Controller extends WP_REST_Posts_Controller { 
    6868         * @return array Item schema data.
    6969         */
    7070        public function get_item_schema() {
     71                // Do not cache this schema because all properties are derived from parent controller.
    7172                $schema = parent::get_item_schema();
    7273
    7374                /*
  • src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php
    index b730844fde..8bce803ea9 100644
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    12151215         * @return array
    12161216         */
    12171217        public function get_item_schema() {
     1218                if ( $this->schema ) {
     1219                        return $this->add_additional_fields_schema( $this->schema );
     1220                }
     1221
    12181222                $schema = array(
    12191223                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    12201224                        'title'      => 'comment',
    class WP_REST_Comments_Controller extends WP_REST_Controller { 
    13641368
    13651369                $schema['properties']['meta'] = $this->meta->get_field_schema();
    13661370
    1367                 return $this->add_additional_fields_schema( $schema );
     1371                $this->schema = $schema;
     1372                return $this->add_additional_fields_schema( $this->schema );
    13681373        }
    13691374
    13701375        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
    index cd976f3357..f6991e0d67 100644
    abstract class WP_REST_Controller { 
    3030         */
    3131        protected $rest_base;
    3232
     33        /**
     34         * Cached results of get_item_schema.
     35         *
     36         * @since 5.3.0
     37         * @var array
     38         */
     39        protected $schema;
     40
    3341        /**
    3442         * Registers the routes for the objects of the controller.
    3543         *
  • src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php
    index 699e8fd2ce..155c8aaa96 100644
    class WP_REST_Post_Statuses_Controller extends WP_REST_Controller { 
    268268         * @return array Item schema data.
    269269         */
    270270        public function get_item_schema() {
     271                if ( $this->schema ) {
     272                        return $this->add_additional_fields_schema( $this->schema );
     273                }
     274
    271275                $schema = array(
    272276                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    273277                        'title'      => 'status',
    class WP_REST_Post_Statuses_Controller extends WP_REST_Controller { 
    318322                        ),
    319323                );
    320324
    321                 return $this->add_additional_fields_schema( $schema );
     325                $this->schema = $schema;
     326                return $this->add_additional_fields_schema( $this->schema );
    322327        }
    323328
    324329        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php
    index c718a3dfa6..99a3c77071 100644
    class WP_REST_Post_Types_Controller extends WP_REST_Controller { 
    242242         * @return array Item schema data.
    243243         */
    244244        public function get_item_schema() {
     245                if ( $this->schema ) {
     246                        return $this->add_additional_fields_schema( $this->schema );
     247                }
     248
    245249                $schema = array(
    246250                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    247251                        'title'      => 'type',
    class WP_REST_Post_Types_Controller extends WP_REST_Controller { 
    312316                                ),
    313317                        ),
    314318                );
    315                 return $this->add_additional_fields_schema( $schema );
     319
     320                $this->schema = $schema;
     321                return $this->add_additional_fields_schema( $this->schema );
    316322        }
    317323
    318324        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 090ca2a5da..ad53ea6a1e 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    18701870         * @return array Item schema data.
    18711871         */
    18721872        public function get_item_schema() {
     1873                if ( $this->schema ) {
     1874                        return $this->add_additional_fields_schema( $this->schema );
     1875                }
    18731876
    18741877                $schema = array(
    18751878                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    class WP_REST_Posts_Controller extends WP_REST_Controller { 
    22292232                        $schema['links'] = $schema_links;
    22302233                }
    22312234
    2232                 return $this->add_additional_fields_schema( $schema );
     2235                $this->schema = $schema;
     2236                return $this->add_additional_fields_schema( $this->schema );
    22332237        }
    22342238
    22352239        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php
    index 52df0f2206..1b34619130 100644
    class WP_REST_Revisions_Controller extends WP_REST_Controller { 
    606606         * @return array Item schema data.
    607607         */
    608608        public function get_item_schema() {
     609                if ( $this->schema ) {
     610                        return $this->add_additional_fields_schema( $this->schema );
     611                }
     612
    609613                $schema = array(
    610614                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    611615                        'title'      => "{$this->parent_post_type}-revision",
    class WP_REST_Revisions_Controller extends WP_REST_Controller { 
    682686                        $schema['properties']['guid'] = $parent_schema['properties']['guid'];
    683687                }
    684688
    685                 return $this->add_additional_fields_schema( $schema );
     689                $this->schema = $schema;
     690                return $this->add_additional_fields_schema( $this->schema );
    686691        }
    687692
    688693        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php
    index 299029b1c3..c464e0d1b4 100644
    class WP_REST_Search_Controller extends WP_REST_Controller { 
    211211         * @return array Item schema data.
    212212         */
    213213        public function get_item_schema() {
     214                if ( $this->schema ) {
     215                        return $this->add_additional_fields_schema( $this->schema );
     216                }
     217
    214218                $types    = array();
    215219                $subtypes = array();
    216220                foreach ( $this->search_handlers as $search_handler ) {
    class WP_REST_Search_Controller extends WP_REST_Controller { 
    262266                        ),
    263267                );
    264268
    265                 return $this->add_additional_fields_schema( $schema );
     269                $this->schema = $schema;
     270                return $this->add_additional_fields_schema( $this->schema );
    266271        }
    267272
    268273        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php
    index 95752f8236..b11e4fdca8 100644
    class WP_REST_Settings_Controller extends WP_REST_Controller { 
    270270         * @return array Item schema data.
    271271         */
    272272        public function get_item_schema() {
     273                if ( $this->schema ) {
     274                        return $this->add_additional_fields_schema( $this->schema );
     275                }
     276
    273277                $options = $this->get_registered_options();
    274278
    275279                $schema = array(
    class WP_REST_Settings_Controller extends WP_REST_Controller { 
    286290                        );
    287291                }
    288292
    289                 return $this->add_additional_fields_schema( $schema );
     293                $this->schema = $schema;
     294                return $this->add_additional_fields_schema( $this->schema );
    290295        }
    291296
    292297        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php
    index 6dc78cb6d0..58ad748132 100644
    class WP_REST_Taxonomies_Controller extends WP_REST_Controller { 
    276276         * @return array Item schema data.
    277277         */
    278278        public function get_item_schema() {
     279                if ( $this->schema ) {
     280                        return $this->add_additional_fields_schema( $this->schema );
     281                }
     282
    279283                $schema = array(
    280284                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    281285                        'title'      => 'taxonomy',
    class WP_REST_Taxonomies_Controller extends WP_REST_Controller { 
    373377                                ),
    374378                        ),
    375379                );
    376                 return $this->add_additional_fields_schema( $schema );
     380
     381                $this->schema = $schema;
     382                return $this->add_additional_fields_schema( $this->schema );
    377383        }
    378384
    379385        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
    index 881afcd41f..52bc9d2b0a 100644
    class WP_REST_Terms_Controller extends WP_REST_Controller { 
    856856         * @return array Item schema data.
    857857         */
    858858        public function get_item_schema() {
     859                if ( $this->schema ) {
     860                        return $this->add_additional_fields_schema( $this->schema );
     861                }
     862
    859863                $schema = array(
    860864                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    861865                        'title'      => 'post_tag' === $this->taxonomy ? 'tag' : $this->taxonomy,
    class WP_REST_Terms_Controller extends WP_REST_Controller { 
    924928
    925929                $schema['properties']['meta'] = $this->meta->get_field_schema();
    926930
    927                 return $this->add_additional_fields_schema( $schema );
     931                $this->schema = $schema;
     932                return $this->add_additional_fields_schema( $this->schema );
    928933        }
    929934
    930935        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php
    index dbfaa839cb..e04b23594d 100644
    class WP_REST_Themes_Controller extends WP_REST_Controller { 
    147147         * @return array Item schema data.
    148148         */
    149149        public function get_item_schema() {
     150                if ( $this->schema ) {
     151                        return $this->add_additional_fields_schema( $this->schema );
     152                }
     153
    150154                $schema = array(
    151155                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    152156                        'title'      => 'theme',
    class WP_REST_Themes_Controller extends WP_REST_Controller { 
    177181                        ),
    178182                );
    179183
    180                 return $this->add_additional_fields_schema( $schema );
     184                $this->schema = $schema;
     185                return $this->add_additional_fields_schema( $this->schema );
    181186        }
    182187
    183188        /**
  • src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    diff --git src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php
    index bd09b70b32..ec29c66974 100644
    class WP_REST_Users_Controller extends WP_REST_Controller { 
    11781178         * @return array Item schema data.
    11791179         */
    11801180        public function get_item_schema() {
     1181                if ( $this->schema ) {
     1182                        return $this->add_additional_fields_schema( $this->schema );
     1183                }
     1184
    11811185                $schema = array(
    11821186                        '$schema'    => 'http://json-schema.org/draft-04/schema#',
    11831187                        'title'      => 'user',
    class WP_REST_Users_Controller extends WP_REST_Controller { 
    13341338
    13351339                $schema['properties']['meta'] = $this->meta->get_field_schema();
    13361340
    1337                 return $this->add_additional_fields_schema( $schema );
     1341                $this->schema = $schema;
     1342                return $this->add_additional_fields_schema( $this->schema );
    13381343        }
    13391344
    13401345        /**
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php
    index f03eee8e83..ba174c448e 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    39193919
    39203920                remove_post_type_support( 'post', 'author' );
    39213921
     3922                // Re-initialize the controller to cache-bust schemas from prior test runs.
     3923                $GLOBALS['wp_rest_server']->override_by_default = true;
     3924                $controller                                     = new WP_REST_Posts_Controller( 'post' );
     3925                $controller->register_routes();
     3926                $GLOBALS['wp_rest_server']->override_by_default = false;
     3927
    39223928                $response = rest_get_server()->dispatch( new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ) );
    39233929                $data     = $response->get_data();
    39243930                $schema   = $data['schema'];
  • tests/phpunit/tests/rest-api/rest-users-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-users-controller.php tests/phpunit/tests/rest-api/rest-users-controller.php
    index 02ef87c04b..28755a97d1 100644
    class WP_Test_REST_Users_Controller extends WP_Test_REST_Controller_Testcase { 
    25222522
    25232523        public function test_get_item_schema_show_avatar() {
    25242524                update_option( 'show_avatars', false );
     2525
     2526                // Re-initialize the controller to cache-bust schemas from prior test runs.
     2527                $GLOBALS['wp_rest_server']->override_by_default = true;
     2528                $controller                                     = new WP_REST_Users_Controller();
     2529                $controller->register_routes();
     2530                $GLOBALS['wp_rest_server']->override_by_default = false;
     2531
    25252532                $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/users' );
    25262533                $response   = rest_get_server()->dispatch( $request );
    25272534                $data       = $response->get_data();