Make WordPress Core

Ticket #46238: 46238a.diff

File 46238a.diff, 1.9 KB (added by dmsnell, 6 years ago)

add-the-sentinel approach

  • wp-includes/rest-api/endpoints/class-wp-rest-controller.php

    diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
    index d610504692..a2b1565f6e 100644
    a b abstract class WP_REST_Controller { 
    371371         * Adds the values from additional fields to a data object.
    372372         *
    373373         * @since 4.7.0
     374         * @since 5.1.0 includes $skip_field value to return in order to omit the field from the response
    374375         *
    375376         * @param array           $object  Data object.
    376377         * @param WP_REST_Request $request Full details about the request.
    abstract class WP_REST_Controller { 
    392393                                continue;
    393394                        }
    394395
    395                         $object[ $field_name ] = call_user_func( $field_options['get_callback'], $object, $field_name, $request, $this->get_object_type() );
     396                        /**
     397                         * Sentinel value which the callback can return in order
     398                         * to indicate that this field should not be added
     399                         *
     400                         * We're relying on the fact that object identity returns
     401                         * whether two objects are the same instance of the same
     402                         * class. That means it will be impossible to confuse a
     403                         * value created inside the callback with this value.
     404                         *
     405                         * @example
     406                         * function my_get_callback( $object, $field_name, $request, $object_type, $skip_field ) {
     407                         *   // only attach video meta to video attachments
     408                         *   if ( 0 !== strpos( $object->post_mime_type, 'video/' ) {
     409                         *     return $skip_field;
     410                         *   }
     411                         *
     412                         *   return calculate_expensive_metadata( $object->ID );
     413                         * }
     414                         *
     415                         */
     416                        $skip_field  = new stdClass;
     417                        $field_value = call_user_func( $field_options['get_callback'], $object, $field_name, $request, $this->get_object_type(), $skip_field );
     418
     419                        if ( $field_value === $skip_field ) {
     420                                continue;
     421                        }
     422
     423                        $object[ $field_name ] = $field_value;
    396424                }
    397425
    398426                return $object;