Make WordPress Core

Ticket #47779: 47779.6.diff

File 47779.6.diff, 3.3 KB (added by kadamwhite, 7 years ago)

Fix wording in hook doc block

  • 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 c251b57dad..694185300b 100644
    class WP_REST_Posts_Controller extends WP_REST_Controller {  
    24062406                        $schema['links'] = $schema_links;
    24072407                }
    24082408
     2409                // Take a snapshot of which fields are in the schema pre-filtering.
     2410                $schema_fields = array_keys( $schema['properties'] );
     2411
     2412                /**
     2413                 * Filter the post's schema.
     2414                 *
     2415                 * The dynamic portion of the hook, `$this->post_type`, refers to the
     2416                 * post type slug for the controller.
     2417                 *
     2418                 * @since 5.4.0
     2419                 *
     2420                 * @param array $schema Item schema data.
     2421                 */
     2422                $schema = apply_filters( "rest_{$this->post_type}_item_schema", $schema );
     2423
     2424                // Emit a _doing_it_wrong warning if user tries to add new properties using this filter.
     2425                $new_fields = array_diff( array_keys( $schema['properties'] ), $schema_fields );
     2426                if ( count( $new_fields ) > 0 ) {
     2427                        _doing_it_wrong( __METHOD__, __( 'Please use register_rest_field to add new schema properties.' ), '5.4.0' );
     2428                }
     2429
    24092430                $this->schema = $schema;
    24102431
    24112432                return $this->add_additional_fields_schema( $this->schema );
  • 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 3735d1146a..e9bc42d3bf 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te  
    48414841                $this->assertNull( get_post_type_object( 'test' )->get_rest_controller() );
    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                remove_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema' ) );
     4862
     4863                $this->assertArrayHasKey( 'new_prop', $properties );
     4864                $this->assertEquals( array( 'new_context' ), $properties['new_prop']['context'] );
     4865        }
     4866
    48444867        public function tearDown() {
    48454868                _unregister_post_type( 'private-post' );
    48464869                _unregister_post_type( 'youseeme' );
    … … class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te  
    48684891                        'post-my-test-template.php' => 'My Test Template',
    48694892                );
    48704893        }
     4894
     4895        public function filter_post_item_schema( $schema ) {
     4896                $schema['properties']['content']['properties']['new_prop'] = array(
     4897                        'description' => __( 'A new prop added with a the rest_post_item_schema filter.' ),
     4898                        'type'        => 'string',
     4899                        'context'     => array( 'new_context' ),
     4900                );
     4901                return $schema;
     4902        }
    48714903}