Make WordPress Core


Ignore:
Timestamp:
03/11/2025 02:17:41 PM (12 months ago)
Author:
TimothyBlynJacobs
Message:

REST API: Fix fatal error when making HEAD requests with _fields filter.

In [59889] the REST API controllers were adjusted to perform less work when responding to HEAD requests. The WP_REST_Response body would now be null, which caused issues with filters that expected the response body to be an array.

This commit sets the response body to be an empty array when preparing the response instead. The body will still be discarded, but this provides better backward comppatibility with code that assumes an array will be used.

See #56481.
Props antonvlasenko, timothyblynjacobs, mamaduka, wildworks.

File:
1 edited

Legend:

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

    r59899 r59970  
    114114        }
    115115
    116         $response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $items );
     116        $response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $items );
    117117
    118118        $response->header( 'X-WP-Total', (int) $total_items );
     
    194194            if ( $request->is_method( 'HEAD' ) ) {
    195195                /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
    196                 return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response(), $item, $request );
     196                return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );
    197197            }
    198198
     
    210210        if ( $request->is_method( 'HEAD' ) ) {
    211211            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php */
    212             return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response(), $item, $request );
     212            return apply_filters( 'rest_prepare_font_collection', new WP_REST_Response( array() ), $item, $request );
    213213        }
    214214
Note: See TracChangeset for help on using the changeset viewer.