WordPress.org

Make WordPress Core


Ignore:
Timestamp:
02/11/2020 04:26:56 PM (10 months ago)
Author:
kadamwhite
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r47233 r47265  
    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 filter, `$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
Note: See TracChangeset for help on using the changeset viewer.