Skip to content

Commit 8ae3a26

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@59970 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 34b98f1 commit 8ae3a26

File tree

38 files changed

+747
-79
lines changed

38 files changed

+747
-79
lines changed

src/wp-includes/rest-api/endpoints/class-wp-rest-autosaves-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ public function get_items( $request ) {
307307

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();
313313
$parent_id = $parent->ID;
@@ -455,7 +455,7 @@ public function prepare_item_for_response( $item, $request ) {
455455
// Don't prepare the response body for HEAD requests.
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 );
461461
$fields = $this->get_fields_for_response( $request );

src/wp-includes/rest-api/endpoints/class-wp-rest-block-pattern-categories-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function get_items_permissions_check( $request ) {
8383
public function get_items( $request ) {
8484
if ( $request->is_method( 'HEAD' ) ) {
8585
// Return early as this handler doesn't add any response headers.
86-
return new WP_REST_Response();
86+
return new WP_REST_Response( array() );
8787
}
8888

8989
$response = array();

src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function get_items_permissions_check( $request ) {
133133
public function get_items( $request ) {
134134
if ( $request->is_method( 'HEAD' ) ) {
135135
// Return early as this handler doesn't add any response headers.
136-
return new WP_REST_Response();
136+
return new WP_REST_Response( array() );
137137
}
138138

139139
$data = array();
@@ -258,7 +258,7 @@ public function prepare_item_for_response( $item, $request ) {
258258
// Don't prepare the response body for HEAD requests.
259259
if ( $request->is_method( 'HEAD' ) ) {
260260
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php */
261-
return apply_filters( 'rest_prepare_block_type', new WP_REST_Response(), $block_type, $request );
261+
return apply_filters( 'rest_prepare_block_type', new WP_REST_Response( array() ), $block_type, $request );
262262
}
263263

264264
$fields = $this->get_fields_for_response( $request );

src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ public function get_items( $request ) {
313313
$max_pages = (int) ceil( $total_comments / $request['per_page'] );
314314
}
315315

316-
$response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $comments );
316+
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $comments );
317317
$response->header( 'X-WP-Total', $total_comments );
318318
$response->header( 'X-WP-TotalPages', $max_pages );
319319

@@ -1054,7 +1054,7 @@ public function prepare_item_for_response( $item, $request ) {
10541054
// Don't prepare the response body for HEAD requests.
10551055
if ( $request->is_method( 'HEAD' ) ) {
10561056
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php */
1057-
return apply_filters( 'rest_prepare_comment', new WP_REST_Response(), $comment, $request );
1057+
return apply_filters( 'rest_prepare_comment', new WP_REST_Response( array() ), $comment, $request );
10581058
}
10591059

10601060
$fields = $this->get_fields_for_response( $request );

src/wp-includes/rest-api/endpoints/class-wp-rest-font-collections-controller.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function get_items( $request ) {
113113
$items[] = $item;
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 );
119119
$response->header( 'X-WP-TotalPages', $max_pages );
@@ -193,7 +193,7 @@ public function prepare_item_for_response( $item, $request ) {
193193
*/
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

199199
foreach ( $data_fields as $field ) {
@@ -209,7 +209,7 @@ public function prepare_item_for_response( $item, $request ) {
209209
*/
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

215215
$response = rest_ensure_response( $data );

src/wp-includes/rest-api/endpoints/class-wp-rest-global-styles-revisions-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public function get_items( $request ) {
248248

249249
$response = rest_ensure_response( $response );
250250
} else {
251-
$response = new WP_REST_Response();
251+
$response = new WP_REST_Response( array() );
252252
}
253253

254254
$response->header( 'X-WP-Total', (int) $total_revisions );
@@ -291,7 +291,7 @@ public function get_items( $request ) {
291291
public function prepare_item_for_response( $post, $request ) {
292292
// Don't prepare the response body for HEAD requests.
293293
if ( $request->is_method( 'HEAD' ) ) {
294-
return new WP_REST_Response();
294+
return new WP_REST_Response( array() );
295295
}
296296

297297
$parent = $this->get_parent( $request['parent'] );

src/wp-includes/rest-api/endpoints/class-wp-rest-pattern-directory-controller.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function get_items( $request ) {
163163

164164
if ( $request->is_method( 'HEAD' ) ) {
165165
// Return early as this handler doesn't add any response headers.
166-
return new WP_REST_Response();
166+
return new WP_REST_Response( array() );
167167
}
168168

169169
$response = array();

src/wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public function get_items_permissions_check( $request ) {
111111
public function get_items( $request ) {
112112
if ( $request->is_method( 'HEAD' ) ) {
113113
// Return early as this handler doesn't add any response headers.
114-
return new WP_REST_Response();
114+
return new WP_REST_Response( array() );
115115
}
116116

117117
$data = array();
@@ -186,7 +186,7 @@ public function prepare_item_for_response( $item, $request ) {
186186
// Don't prepare the response body for HEAD requests.
187187
if ( $request->is_method( 'HEAD' ) ) {
188188
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-post-types-controller.php */
189-
return apply_filters( 'rest_prepare_post_type', new WP_REST_Response(), $post_type, $request );
189+
return apply_filters( 'rest_prepare_post_type', new WP_REST_Response( array() ), $post_type, $request );
190190
}
191191

192192
$taxonomies = wp_list_filter( get_object_taxonomies( $post_type->name, 'objects' ), array( 'show_in_rest' => true ) );

src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ static function ( $format ) {
499499
);
500500
}
501501

502-
$response = $is_head_request ? new WP_REST_Response() : rest_ensure_response( $posts );
502+
$response = $is_head_request ? new WP_REST_Response( array() ) : rest_ensure_response( $posts );
503503

504504
$response->header( 'X-WP-Total', (int) $total_posts );
505505
$response->header( 'X-WP-TotalPages', (int) $max_pages );
@@ -1847,7 +1847,7 @@ public function prepare_item_for_response( $item, $request ) {
18471847
// Don't prepare the response body for HEAD requests.
18481848
if ( $request->is_method( 'HEAD' ) ) {
18491849
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php */
1850-
return apply_filters( "rest_prepare_{$this->post_type}", new WP_REST_Response(), $post, $request );
1850+
return apply_filters( "rest_prepare_{$this->post_type}", new WP_REST_Response( array() ), $post, $request );
18511851
}
18521852

18531853
$fields = $this->get_fields_for_response( $request );

src/wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ public function get_items( $request ) {
355355

356356
$response = rest_ensure_response( $response );
357357
} else {
358-
$response = new WP_REST_Response();
358+
$response = new WP_REST_Response( array() );
359359
}
360360

361361
$response->header( 'X-WP-Total', (int) $total_revisions );
@@ -591,7 +591,7 @@ public function prepare_item_for_response( $item, $request ) {
591591
// Don't prepare the response body for HEAD requests.
592592
if ( $request->is_method( 'HEAD' ) ) {
593593
/** This filter is documented in wp-includes/rest-api/endpoints/class-wp-rest-revisions-controller.php */
594-
return apply_filters( 'rest_prepare_revision', new WP_REST_Response(), $post, $request );
594+
return apply_filters( 'rest_prepare_revision', new WP_REST_Response( array() ), $post, $request );
595595
}
596596

597597
$fields = $this->get_fields_for_response( $request );

0 commit comments

Comments
 (0)