diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php
index 68be073eef..7f158959e9 100644
a
|
b
|
class WP_REST_Server { |
405 | 405 | */ |
406 | 406 | $result = apply_filters( 'rest_pre_echo_response', $result, $this, $request ); |
407 | 407 | |
| 408 | // The 204 response shouldn't have a body. |
| 409 | if ( 204 === $code || null === $result ) { |
| 410 | return null; |
| 411 | } |
| 412 | |
408 | 413 | $result = wp_json_encode( $result ); |
409 | 414 | |
410 | 415 | $json_error_message = $this->get_json_last_error(); |
diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php
index 8c514e9e66..efb656ed4e 100644
a
|
b
|
class Tests_REST_Server extends WP_Test_REST_TestCase { |
1144 | 1144 | $this->assertEquals( 200, $response->get_status() ); |
1145 | 1145 | } |
1146 | 1146 | |
| 1147 | /** |
| 1148 | * @ticket 43691 |
| 1149 | */ |
| 1150 | public function test_does_not_echo_body_for_null_responses() { |
| 1151 | register_rest_route( |
| 1152 | 'test-ns', |
| 1153 | '/test', |
| 1154 | array( |
| 1155 | 'methods' => array( 'GET' ), |
| 1156 | 'callback' => function () { |
| 1157 | return new WP_REST_Response(); |
| 1158 | }, |
| 1159 | ) |
| 1160 | ); |
| 1161 | |
| 1162 | $result = rest_get_server()->serve_request( '/test-ns/test' ); |
| 1163 | |
| 1164 | $this->assertNull( $result ); |
| 1165 | $this->assertEquals( '', rest_get_server()->sent_body ); |
| 1166 | } |
| 1167 | |
| 1168 | /** |
| 1169 | * @ticket 43691 |
| 1170 | */ |
| 1171 | public function test_does_not_echo_body_for_responses_with_204_status() { |
| 1172 | register_rest_route( |
| 1173 | 'test-ns', |
| 1174 | '/test', |
| 1175 | array( |
| 1176 | 'methods' => array( 'GET' ), |
| 1177 | 'callback' => function () { |
| 1178 | return new WP_REST_Response( 'data', 204 ); |
| 1179 | }, |
| 1180 | ) |
| 1181 | ); |
| 1182 | |
| 1183 | $result = rest_get_server()->serve_request( '/test-ns/test' ); |
| 1184 | |
| 1185 | $this->assertNull( $result ); |
| 1186 | $this->assertEquals( '', rest_get_server()->sent_body ); |
| 1187 | } |
| 1188 | |
1147 | 1189 | public function _validate_as_integer_123( $value, $request, $key ) { |
1148 | 1190 | if ( ! is_int( $value ) ) { |
1149 | 1191 | return new WP_Error( 'some-error', 'This is not valid!' ); |