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 { |
2261 | 2261 | $schema['links'] = $schema_links; |
2262 | 2262 | } |
2263 | 2263 | |
| 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 | |
2264 | 2289 | $this->schema = $schema; |
2265 | | return $this->add_additional_fields_schema( $this->schema ); |
| 2290 | |
| 2291 | return $schema; |
2266 | 2292 | } |
2267 | 2293 | |
2268 | 2294 | /** |
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 |
4616 | 4616 | ); |
4617 | 4617 | } |
4618 | 4618 | |
| 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 | |
4619 | 4635 | public function tearDown() { |
4620 | 4636 | _unregister_post_type( 'private-post' ); |
4621 | 4637 | _unregister_post_type( 'youseeme' ); |
… |
… |
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te |
4643 | 4659 | 'post-my-test-template.php' => 'My Test Template', |
4644 | 4660 | ); |
4645 | 4661 | } |
| 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 | } |
4646 | 4671 | } |