Changeset 55104
- Timestamp:
- 01/20/2023 12:19:09 AM (20 months ago)
- Location:
- trunk
- Files:
-
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/rest-api.php
r54997 r55104 2935 2935 * Support the "anyOf" and "oneOf" keywords. 2936 2936 * 2937 * @param array|object $ dataThe response data to modify.2938 * @param array $schema The schema for the endpoint used to filter the response.2939 * @param string $context The requested context.2937 * @param array|object $response_data The response data to modify. 2938 * @param array $schema The schema for the endpoint used to filter the response. 2939 * @param string $context The requested context. 2940 2940 * @return array|object The filtered response data. 2941 2941 */ 2942 function rest_filter_response_by_context( $ data, $schema, $context ) {2942 function rest_filter_response_by_context( $response_data, $schema, $context ) { 2943 2943 if ( isset( $schema['anyOf'] ) ) { 2944 $matching_schema = rest_find_any_matching_schema( $ data, $schema, '' );2944 $matching_schema = rest_find_any_matching_schema( $response_data, $schema, '' ); 2945 2945 if ( ! is_wp_error( $matching_schema ) ) { 2946 2946 if ( ! isset( $schema['type'] ) ) { … … 2948 2948 } 2949 2949 2950 $ data = rest_filter_response_by_context( $data, $matching_schema, $context );2950 $response_data = rest_filter_response_by_context( $response_data, $matching_schema, $context ); 2951 2951 } 2952 2952 } 2953 2953 2954 2954 if ( isset( $schema['oneOf'] ) ) { 2955 $matching_schema = rest_find_one_matching_schema( $ data, $schema, '', true );2955 $matching_schema = rest_find_one_matching_schema( $response_data, $schema, '', true ); 2956 2956 if ( ! is_wp_error( $matching_schema ) ) { 2957 2957 if ( ! isset( $schema['type'] ) ) { … … 2959 2959 } 2960 2960 2961 $ data = rest_filter_response_by_context( $data, $matching_schema, $context );2962 } 2963 } 2964 2965 if ( ! is_array( $ data ) && ! is_object( $data ) ) {2966 return $ data;2961 $response_data = rest_filter_response_by_context( $response_data, $matching_schema, $context ); 2962 } 2963 } 2964 2965 if ( ! is_array( $response_data ) && ! is_object( $response_data ) ) { 2966 return $response_data; 2967 2967 } 2968 2968 … … 2972 2972 $type = 'object'; // Back compat if a developer accidentally omitted the type. 2973 2973 } else { 2974 return $ data;2974 return $response_data; 2975 2975 } 2976 2976 … … 2979 2979 2980 2980 if ( $is_array_type && $is_object_type ) { 2981 if ( rest_is_array( $ data ) ) {2981 if ( rest_is_array( $response_data ) ) { 2982 2982 $is_object_type = false; 2983 2983 } else { … … 2988 2988 $has_additional_properties = $is_object_type && isset( $schema['additionalProperties'] ) && is_array( $schema['additionalProperties'] ); 2989 2989 2990 foreach ( $ data as $key => $value ) {2990 foreach ( $response_data as $key => $value ) { 2991 2991 $check = array(); 2992 2992 … … 3013 3013 if ( $is_array_type ) { 3014 3014 // All array items share schema, so there's no need to check each one. 3015 $ data = array();3015 $response_data = array(); 3016 3016 break; 3017 3017 } 3018 3018 3019 if ( is_object( $ data ) ) {3020 unset( $ data->$key );3019 if ( is_object( $response_data ) ) { 3020 unset( $response_data->$key ); 3021 3021 } else { 3022 unset( $ data[ $key ] );3022 unset( $response_data[ $key ] ); 3023 3023 } 3024 3024 } elseif ( is_array( $value ) || is_object( $value ) ) { 3025 3025 $new_value = rest_filter_response_by_context( $value, $check, $context ); 3026 3026 3027 if ( is_object( $ data ) ) {3028 $ data->$key = $new_value;3027 if ( is_object( $response_data ) ) { 3028 $response_data->$key = $new_value; 3029 3029 } else { 3030 $ data[ $key ] = $new_value;3031 } 3032 } 3033 } 3034 3035 return $ data;3030 $response_data[ $key ] = $new_value; 3031 } 3032 } 3033 } 3034 3035 return $response_data; 3036 3036 } 3037 3037 -
trunk/src/wp-includes/rest-api/endpoints/class-wp-rest-controller.php
r54969 r55104 290 290 * @since 4.7.0 291 291 * 292 * @param array $ dataResponse data to filter.293 * @param string $context Context defined in the schema.292 * @param array $response_data Response data to filter. 293 * @param string $context Context defined in the schema. 294 294 * @return array Filtered response. 295 295 */ 296 public function filter_response_by_context( $ data, $context ) {296 public function filter_response_by_context( $response_data, $context ) { 297 297 298 298 $schema = $this->get_item_schema(); 299 299 300 return rest_filter_response_by_context( $ data, $schema, $context );300 return rest_filter_response_by_context( $response_data, $schema, $context ); 301 301 } 302 302 … … 413 413 * @since 4.7.0 414 414 * 415 * @param array $ preparedPrepared response array.416 * @param WP_REST_Request $request Full details about the request.415 * @param array $response_data Prepared response array. 416 * @param WP_REST_Request $request Full details about the request. 417 417 * @return array Modified data object with additional fields. 418 418 */ 419 protected function add_additional_fields_to_object( $ prepared, $request ) {419 protected function add_additional_fields_to_object( $response_data, $request ) { 420 420 421 421 $additional_fields = $this->get_additional_fields(); … … 432 432 } 433 433 434 $ prepared[ $field_name ] = call_user_func(434 $response_data[ $field_name ] = call_user_func( 435 435 $field_options['get_callback'], 436 $ prepared,436 $response_data, 437 437 $field_name, 438 438 $request, … … 441 441 } 442 442 443 return $ prepared;443 return $response_data; 444 444 } 445 445 -
trunk/tests/phpunit/tests/rest-api/rest-attachments-controller.php
r55102 r55104 1679 1679 } 1680 1680 1681 public function additional_field_get_callback( $ object, $field_name ) {1681 public function additional_field_get_callback( $response_data, $field_name ) { 1682 1682 return 123; 1683 1683 } -
trunk/tests/phpunit/tests/rest-api/rest-autosaves-controller.php
r55102 r55104 515 515 } 516 516 517 public function additional_field_get_callback( $ object, $field_name ) {518 return get_post_meta( $ object['id'], $field_name, true );517 public function additional_field_get_callback( $response_data, $field_name ) { 518 return get_post_meta( $response_data['id'], $field_name, true ); 519 519 } 520 520 -
trunk/tests/phpunit/tests/rest-api/rest-categories-controller.php
r55102 r55104 1181 1181 } 1182 1182 1183 public function additional_field_get_callback( $ object, $field_name ) {1183 public function additional_field_get_callback( $response_data, $field_name ) { 1184 1184 return 123; 1185 1185 } -
trunk/tests/phpunit/tests/rest-api/rest-comments-controller.php
r55102 r55104 3299 3299 } 3300 3300 3301 public function additional_field_get_callback( $ object, $field_name ) {3302 return get_comment_meta( $ object['id'], $field_name, true );3301 public function additional_field_get_callback( $response_data, $field_name ) { 3302 return get_comment_meta( $response_data['id'], $field_name, true ); 3303 3303 } 3304 3304 -
trunk/tests/phpunit/tests/rest-api/rest-post-statuses-controller.php
r54891 r55104 201 201 } 202 202 203 public function additional_field_get_callback( $ object) {203 public function additional_field_get_callback( $response_data ) { 204 204 return 123; 205 205 } -
trunk/tests/phpunit/tests/rest-api/rest-post-types-controller.php
r54891 r55104 221 221 } 222 222 223 public function additional_field_get_callback( $ object) {223 public function additional_field_get_callback( $response_data ) { 224 224 return 123; 225 225 } -
trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php
r55102 r55104 4587 4587 } 4588 4588 4589 public function additional_field_get_callback( $ object, $field_name ) {4590 return get_post_meta( $ object['id'], $field_name, true );4589 public function additional_field_get_callback( $response_data, $field_name ) { 4590 return get_post_meta( $response_data['id'], $field_name, true ); 4591 4591 } 4592 4592 -
trunk/tests/phpunit/tests/rest-api/rest-revisions-controller.php
r55102 r55104 403 403 } 404 404 405 public function additional_field_get_callback( $ object, $field_name ) {406 return get_post_meta( $ object['id'], $field_name, true );405 public function additional_field_get_callback( $response_data, $field_name ) { 406 return get_post_meta( $response_data['id'], $field_name, true ); 407 407 } 408 408 -
trunk/tests/phpunit/tests/rest-api/rest-tags-controller.php
r55102 r55104 1325 1325 } 1326 1326 1327 public function additional_field_get_callback( $ object, $field_name ) {1327 public function additional_field_get_callback( $response_data, $field_name ) { 1328 1328 return 123; 1329 1329 } -
trunk/tests/phpunit/tests/rest-api/rest-users-controller.php
r55102 r55104 2826 2826 } 2827 2827 2828 public function additional_field_get_callback( $ object, $field_name ) {2829 return get_user_meta( $ object['id'], $field_name, true );2828 public function additional_field_get_callback( $response_data, $field_name ) { 2829 return get_user_meta( $response_data['id'], $field_name, true ); 2830 2830 } 2831 2831
Note: See TracChangeset
for help on using the changeset viewer.