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 d0333c61c4..4dfd280e8b 100644
|
a
|
b
|
class WP_REST_Posts_Controller extends WP_REST_Controller {
|
| 2260 | 2260 | $schema['links'] = $schema_links; |
| 2261 | 2261 | } |
| 2262 | 2262 | |
| | 2263 | $schema = $this->add_additional_fields_schema( $schema ); |
| | 2264 | |
| | 2265 | /** |
| | 2266 | * Filter the post's schema. |
| | 2267 | * |
| | 2268 | * The dynamic part of the filter `$this->post_type` refers to the post |
| | 2269 | * type slug for the controller. |
| | 2270 | * |
| | 2271 | * @since 5.3.0 |
| | 2272 | * |
| | 2273 | * @param array $schema Item schema data. |
| | 2274 | */ |
| | 2275 | |
| | 2276 | $schema_fields = array_keys( $schema['properties'] ); |
| | 2277 | |
| | 2278 | $schema = apply_filters( "rest_{$this->post_type}_item_schema", $schema ); |
| | 2279 | |
| | 2280 | $new_fields = array_diff( array_keys( $schema['properties'] ), $schema_fields ); |
| | 2281 | |
| | 2282 | // Emit a _doing_it_wrong warning if user tries to add new properties using this filter. |
| | 2283 | if ( count( $new_fields ) > 0 ) { |
| | 2284 | _doing_it_wrong( __METHOD__, __( 'Please use register_rest_field to add new properties.' ), '5.3.0' ); |
| | 2285 | } |
| | 2286 | |
| 2263 | 2287 | $this->schema = $schema; |
| 2264 | | return $this->add_additional_fields_schema( $this->schema ); |
| | 2288 | |
| | 2289 | return $schema; |
| 2265 | 2290 | } |
| 2266 | 2291 | |
| 2267 | 2292 | /** |
diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
index 2aca849347..d935d985c6 100644
|
a
|
b
|
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
|
| 4827 | 4827 | $this->assertNull( get_post_type_object( 'test' )->get_rest_controller() ); |
| 4828 | 4828 | } |
| 4829 | 4829 | |
| | 4830 | |
| | 4831 | /** |
| | 4832 | * @ticket 47779 |
| | 4833 | */ |
| | 4834 | public function test_rest_post_type_item_schema_filter_change_property() { |
| | 4835 | add_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema' ) ); |
| | 4836 | |
| | 4837 | // Re-initialize the controller to cache-bust schemas from prior test runs. |
| | 4838 | $GLOBALS['wp_rest_server']->override_by_default = true; |
| | 4839 | $controller = new WP_REST_Posts_Controller( 'post' ); |
| | 4840 | $controller->register_routes(); |
| | 4841 | $GLOBALS['wp_rest_server']->override_by_default = false; |
| | 4842 | |
| | 4843 | $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' ); |
| | 4844 | $response = rest_get_server()->dispatch( $request ); |
| | 4845 | $data = $response->get_data(); |
| | 4846 | $properties = $data['schema']['properties']['content']['properties']; |
| | 4847 | |
| | 4848 | remove_filter( 'rest_post_item_schema', array( $this, 'filter_post_item_schema' ) ); |
| | 4849 | |
| | 4850 | $this->assertArrayHasKey( 'new_prop', $properties ); |
| | 4851 | $this->assertEquals( array( 'new_context' ), $properties['new_prop']['context'] ); |
| | 4852 | } |
| | 4853 | |
| | 4854 | |
| 4830 | 4855 | public function tearDown() { |
| 4831 | 4856 | _unregister_post_type( 'private-post' ); |
| 4832 | 4857 | _unregister_post_type( 'youseeme' ); |
| … |
… |
class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te
|
| 4854 | 4879 | 'post-my-test-template.php' => 'My Test Template', |
| 4855 | 4880 | ); |
| 4856 | 4881 | } |
| | 4882 | |
| | 4883 | public function filter_post_item_schema( $schema ) { |
| | 4884 | $schema['properties']['content']['properties']['new_prop'] = array( |
| | 4885 | 'description' => __( 'A new prop added with a the rest_post_item_schema filter.' ), |
| | 4886 | 'type' => 'string', |
| | 4887 | 'context' => array( 'new_context' ), |
| | 4888 | ); |
| | 4889 | return $schema; |
| | 4890 | } |
| 4857 | 4891 | } |