Make WordPress Core


Ignore:
Timestamp:
03/11/2025 02:17:41 PM (3 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-autosaves-controller.php

    r59899 r59970  
    308308        if ( $request->is_method( 'HEAD' ) ) {
    309309            // Return early as this handler doesn't add any response headers.
    310             return new WP_REST_Response();
     310            return new WP_REST_Response( array() );
    311311        }
    312312        $response  = array();
     
    456456        if ( $request->is_method( 'HEAD' ) ) {
    457457            /** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php */
    458             return apply_filters( 'rest_prepare_autosave', new WP_REST_Response(), $post, $request );
     458            return apply_filters( 'rest_prepare_autosave', new WP_REST_Response( array() ), $post, $request );
    459459        }
    460460        $response = $this->revisions_controller->prepare_item_for_response( $post, $request );
Note: See TracChangeset for help on using the changeset viewer.