REST API: Introduce rest_{$this->post_type}_item_schema filter to enable manipulation of schema values.

register_rest_field can be used to add properties to a schema, but no mechanism existed to alter existing properties like "content".
Running the schema through this filter lets plugins append additional sub-properties to existing schema definitions.

Props luisherranz, TimothyBlynJacobs, swissspidy, westonruter, kadamwhite.
Fixes #47779.



git-svn-id: https://develop.svn.wordpress.org/trunk@47265 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
K. Adam White
2020-02-11 16:26:56 +00:00
parent 72e7742446
commit b6dcb779ed
2 changed files with 74 additions and 0 deletions

View File

@@ -2406,6 +2406,27 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$schema['links'] = $schema_links;
}
// Take a snapshot of which fields are in the schema pre-filtering.
$schema_fields = array_keys( $schema['properties'] );
/**
* Filter the post's schema.
*
* The dynamic portion of the filter, `$this->post_type`, refers to the
* post type slug for the controller.
*
* @since 5.4.0
*
* @param array $schema Item schema data.
*/
$schema = apply_filters( "rest_{$this->post_type}_item_schema", $schema );
// Emit a _doing_it_wrong warning if user tries to add new properties using this filter.
$new_fields = array_diff( array_keys( $schema['properties'] ), $schema_fields );
if ( count( $new_fields ) > 0 ) {
_doing_it_wrong( __METHOD__, __( 'Please use register_rest_field to add new schema properties.' ), '5.4.0' );
}
$this->schema = $schema;
return $this->add_additional_fields_schema( $this->schema );