WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/17/2020 03:20:02 AM (10 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Only register one block renderer route.

Every block has a different set of attributes. These attributes are specified as a JSON Schema object. Previously, every block registered its own block renderer route using its attributes for the schema. This allowed for the attributes to be validated using the built in endpoint validation rules. It had the unfortunate side effect, however, of creating a large number of nearly identical REST API routes, one for each dynamic block. Each registered route has a performance impact. As the number of server side blocks goes up, this becomes more and more of an issue.

Now, we register a single block renderer route and dynamically validate the attributes based on the selected block.

Fixes #48079.
Props gziolo, TimothyBlynJacobs.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php

    r47756 r48069  
    4848
    4949    /**
     50     * Non dynamic block name.
     51     *
     52     * @since 5.5.0
     53     *
     54     * @var string
     55     */
     56    protected static $non_dynamic_block_name = 'core/non-dynamic';
     57
     58    /**
    5059     * Test API user's ID.
    5160     *
     
    118127        $this->register_test_block();
    119128        $this->register_post_context_test_block();
     129        $this->register_non_dynamic_block();
    120130        parent::setUp();
    121131    }
     
    129139        WP_Block_Type_Registry::get_instance()->unregister( self::$block_name );
    130140        WP_Block_Type_Registry::get_instance()->unregister( self::$context_block_name );
     141        WP_Block_Type_Registry::get_instance()->unregister( self::$non_dynamic_block_name );
    131142        parent::tearDown();
    132143    }
     
    177188
    178189    /**
     190     * Registers the non-dynamic block name.
     191     *
     192     * @since 5.5.0
     193     */
     194    protected function register_non_dynamic_block() {
     195        register_block_type( self::$non_dynamic_block_name );
     196    }
     197
     198    /**
    179199     * Test render callback.
    180200     *
     
    211231
    212232        $routes = rest_get_server()->get_routes();
    213         foreach ( $dynamic_block_names as $dynamic_block_name ) {
    214             $this->assertArrayHasKey( self::$rest_api_route . "(?P<name>$dynamic_block_name)", $routes );
    215         }
     233        $this->assertArrayHasKey( self::$rest_api_route . '(?P<name>[a-z0-9-]+/[a-z0-9-]+)', $routes );
    216234    }
    217235
     
    262280        $response = rest_get_server()->dispatch( $request );
    263281
    264         $this->assertErrorResponse( 'rest_no_route', $response, 404 );
     282        $this->assertErrorResponse( 'block_invalid', $response, 404 );
    265283    }
    266284
     
    512530
    513531    /**
     532     * @ticket 48079
     533     */
     534    public function test_get_item_non_dynamic_block() {
     535        wp_set_current_user( self::$user_id );
     536        $request = new WP_REST_Request( 'GET', self::$rest_api_route . self::$non_dynamic_block_name );
     537
     538        $request->set_param( 'context', 'edit' );
     539        $response = rest_get_server()->dispatch( $request );
     540
     541        $this->assertErrorResponse( 'block_invalid', $response, 404 );
     542    }
     543
     544    /**
    514545     * Get item schema.
    515546     *
Note: See TracChangeset for help on using the changeset viewer.