WordPress.org

Make WordPress Core

Ticket #47779: 47779.3.diff

File 47779.3.diff, 3.0 KB (added by luisherranz, 4 months ago)

I've added the first test, but it's not working yet. Somehow the same code works perfectly fine in the regular site.

  • src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
    index 4a00336e34..306afe4161 100644
    a b class WP_REST_Posts_Controller extends WP_REST_Controller { 
    22612261                        $schema['links'] = $schema_links;
    22622262                }
    22632263
     2264                $schema = $this->add_additional_fields_schema( $schema );
     2265
     2266                /**
     2267                 * Filter the post's schema.
     2268                 *
     2269                 * The dynamic part of the filter `$this->post_type` refers to the post
     2270                 * type slug for the controller.
     2271                 *
     2272                 * @since 5.3.0
     2273                 *
     2274                 * @param array  $schema          Item schema data.
     2275                 * @param string $this->post_type Post type slug.
     2276                 */
     2277
     2278                $schema_fields = array_keys( $schema['properties'] );
     2279
     2280                $schema = apply_filters( "rest_{$this->post_type}_item_schema", $schema );
     2281
     2282                $new_fields = array_diff( array_keys( $schema['properties']), $schema_fields );
     2283
     2284                // Emit a _doing_it_wrong warning if user tries to add new properties using this filter.
     2285                if ( count($new_fields) > 0 ) {
     2286                        _doing_it_wrong(__METHOD__, __( 'Please use register_rest_field to add new properties.' ), '5.3.0' );
     2287                }
     2288
    22642289                $this->schema = $schema;
    2265                 return $this->add_additional_fields_schema( $this->schema );
     2290
     2291                return $schema;
    22662292        }
    22672293
    22682294        /**
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index fa7396afcc..ab8b7c398c 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    46164616                );
    46174617        }
    46184618
     4619        /**
     4620         * @ticket 47779
     4621         */
     4622        public function test_rest_post_type_item_schema_filter_change_property() {
     4623                add_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema' ) );
     4624               
     4625                $request    = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
     4626                $response   = rest_get_server()->dispatch( $request );
     4627                $data       = $response->get_data();
     4628                $properties = $data['schema']['properties']['content']['properties'];
     4629               
     4630                remove_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema' ) );
     4631               
     4632                $this->assertArrayHasKey( 'new_prop', $properties );
     4633        }
     4634
    46194635        public function tearDown() {
    46204636                _unregister_post_type( 'private-post' );
    46214637                _unregister_post_type( 'youseeme' );
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    46434659                        'post-my-test-template.php' => 'My Test Template',
    46444660                );
    46454661        }
     4662
     4663        public function filter_post_item_schema( $schema ) {
     4664                $schema['properties']['content']['properties']['new_prop'] = array(
     4665                        'description' => __( 'A new prop added with a the rest_post_item_schema filter.' ),
     4666                        'type'        => 'string',
     4667                        'context'     => array( 'view', 'edit' ),
     4668                );
     4669                return $schema;
     4670        }
    46464671}