WordPress.org

Make WordPress Core

Changeset 45811


Ignore:
Timestamp:
08/15/2019 09:08:05 PM (5 weeks ago)
Author:
kadamwhite
Message:

REST API: Cache results of get_item_schema on controller instances for performance.

Caches the output of get_item_schema() to avoid redundant recomputation of translatable strings and other computed values. This method is called many times per item in each REST request, and the results of the method should not vary between calls.
Additional schema fields are not cached.

Props kadamwhite, joehoyle, TimothyBlynJacobs.
Fixes #47871.

Location:
trunk
Files:
18 edited

Legend:

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

    r44934 r45811  
    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();
     
    514517        unset( $schema['properties']['password'] );
    515518
    516         return $schema;
     519        $this->schema = $schema;
     520        return $this->add_additional_fields_schema( $this->schema );
    517521    }
    518522
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

    r45444 r45811  
    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
     
    307311        );
    308312
    309         return $schema;
     313        $this->schema = $schema;
     314        return $this->add_additional_fields_schema( $this->schema );
    310315    }
    311316
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php

    r44269 r45811  
    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',
     
    175179            ),
    176180        );
     181        return $this->schema;
    177182    }
    178183}
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-blocks-controller.php

    r44268 r45811  
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

    r45632 r45811  
    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#',
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    r45810 r45811  
    3030     */
    3131    protected $rest_base;
     32
     33    /**
     34     * Cached results of get_item_schema.
     35     *
     36     * @since 5.3.0
     37     * @var array
     38     */
     39    protected $schema;
    3240
    3341    /**
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-statuses-controller.php

    r43571 r45811  
    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#',
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

    r43571 r45811  
    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#',
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r45705 r45811  
    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(
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

    r45267 r45811  
    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#',
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-search-controller.php

    r45267 r45811  
    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();
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-settings-controller.php

    r43571 r45811  
    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
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-taxonomies-controller.php

    r43571 r45811  
    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#',
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php

    r45723 r45811  
    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#',
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-themes-controller.php

    r44138 r45811  
    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#',
     
    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
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php

    r45632 r45811  
    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#',
     
    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
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r45607 r45811  
    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();
  • trunk/tests/phpunit/tests/rest-api/rest-users-controller.php

    r45424 r45811  
    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 );
Note: See TracChangeset for help on using the changeset viewer.