WordPress.org

Make WordPress Core

Ticket #47779: 47779.2.diff

File 47779.2.diff, 1.4 KB (added by luisherranz, 4 months ago)

Emit a _doing_it_wrong warning if user tries to add new properties with this filter instead of register_rest_field.

  • 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        /**