WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/11/2020 04:26:56 PM (8 months ago)
Author:
kadamwhite
Message:

REST API: Introduce rest_{$this->post_type}_item_schema filter to enable manipulation of schema values.

register_rest_field can be used to add properties to a schema, but no mechanism existed to alter existing properties like "content".
Running the schema through this filter lets plugins append additional sub-properties to existing schema definitions.

Props luisherranz, TimothyBlynJacobs, swissspidy, westonruter, kadamwhite.
Fixes #47779.

File:
1 edited

Legend:

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

    r47122 r47265  
    48424842    }
    48434843
     4844    /**
     4845     * @ticket 47779
     4846     */
     4847    public function test_rest_post_type_item_schema_filter_change_property() {
     4848        add_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema' ) );
     4849
     4850        // Re-initialize the controller to cache-bust schemas from prior test runs.
     4851        $GLOBALS['wp_rest_server']->override_by_default = true;
     4852        $controller                                     = new WP_REST_Posts_Controller( 'post' );
     4853        $controller->register_routes();
     4854        $GLOBALS['wp_rest_server']->override_by_default = false;
     4855
     4856        $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
     4857        $response   = rest_get_server()->dispatch( $request );
     4858        $data       = $response->get_data();
     4859        $properties = $data['schema']['properties']['content']['properties'];
     4860
     4861        $this->assertArrayHasKey( 'new_prop', $properties );
     4862        $this->assertEquals( array( 'new_context' ), $properties['new_prop']['context'] );
     4863    }
     4864
     4865    /**
     4866     * @ticket 47779
     4867     */
     4868    public function test_rest_post_type_item_schema_filter_add_property_triggers_doing_it_wrong() {
     4869        add_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema_add_property' ) );
     4870        $this->setExpectedIncorrectUsage( 'WP_REST_Posts_Controller::get_item_schema' );
     4871
     4872        // Re-initialize the controller to cache-bust schemas from prior test runs.
     4873        $GLOBALS['wp_rest_server']->override_by_default = true;
     4874        $controller                                     = new WP_REST_Posts_Controller( 'post' );
     4875        $controller->register_routes();
     4876        $GLOBALS['wp_rest_server']->override_by_default = false;
     4877    }
     4878
    48444879    public function tearDown() {
    48454880        _unregister_post_type( 'private-post' );
     
    48694904        );
    48704905    }
     4906
     4907    public function filter_post_item_schema( $schema ) {
     4908        $schema['properties']['content']['properties']['new_prop'] = array(
     4909            'description' => __( 'A new prop added with a the rest_post_item_schema filter.' ),
     4910            'type'        => 'string',
     4911            'context'     => array( 'new_context' ),
     4912        );
     4913        return $schema;
     4914    }
     4915
     4916    public function filter_post_item_schema_add_property( $schema ) {
     4917        $schema['properties']['something_entirely_new'] = array(
     4918            'description' => __( 'A new prop added with a the rest_post_item_schema filter.' ),
     4919            'type'        => 'string',
     4920            'context'     => array( 'new_context' ),
     4921        );
     4922        return $schema;
     4923    }
    48714924}
Note: See TracChangeset for help on using the changeset viewer.