[wp-trac] [WordPress Trac] #47779: Add a filter to `get_item_schema()` to be able to extend the schema of existing properties

WordPress Trac noreply at wordpress.org
Tue Oct 8 09:48:47 UTC 2019


#47779: Add a filter to `get_item_schema()` to be able to extend the schema of
existing properties
-------------------------------------------------+-------------------------
 Reporter:  luisherranz                          |       Owner:  (none)
     Type:  enhancement                          |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
                                                 |  Review
Component:  REST API                             |     Version:  4.7
 Severity:  trivial                              |  Resolution:
 Keywords:  has-patch dev-feedback needs-unit-   |     Focuses:  rest-api
  tests                                          |
-------------------------------------------------+-------------------------

Comment (by luisherranz):

 This filter works fine in the regular site:

 {{{#!php
 function filter_post_item_schema( $schema ) {
         $schema['properties']['content']['properties']['new_prop'] =
 array(
                 'description' => __( 'A new prop added with a the
 rest_post_item_schema filter.' ),
                 'type'        => 'string',
                 'context'     => array( 'view', 'edit' ),
         );
         return $schema;
 }

 add_filter( 'rest_post_item_schema', 'filter_post_item_schema' );
 }}}

 However, in the testing environment, the filter doesn't seem to work:

 {{{#!php
 public function test_rest_post_type_item_schema_filter_change_property() {
                 add_filter( 'rest_post_item_schema', array( $this,
 'filter_post_item_schema' ) );

                 $request    = new WP_REST_Request( 'OPTIONS',
 '/wp/v2/posts' );
                 $response   = rest_get_server()->dispatch( $request );
                 $data       = $response->get_data();
                 $properties =
 $data['schema']['properties']['content']['properties'];

                 remove_filter( 'rest_post_item_schema', array( $this,
 'filter_post_item_schema' ) );

                 $this->assertArrayHasKey( 'new_prop', $properties );
         }

 ...

 public function filter_post_item_schema( $schema ) {
                 $schema['properties']['content']['properties']['new_prop']
 = array(
                         'description' => __( 'A new prop added with a the
 rest_post_item_schema filter.' ),
                         'type'        => 'string',
                         'context'     => array( 'view', 'edit' ),
                 );
                 return $schema;
         }

 }}}

 Any help is appreciated.

 Once this is solved I'll add more use cases to the test.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47779#comment:5>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list