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 9d4a5fec05..81394f0629 100644
a
|
b
|
class WP_REST_Server { |
252 | 252 | $send_no_cache_headers = apply_filters( 'rest_send_nocache_headers', is_user_logged_in() ); |
253 | 253 | if ( $send_no_cache_headers ) { |
254 | 254 | foreach ( wp_get_nocache_headers() as $header => $header_value ) { |
| 255 | if ( empty( $header_value ) ) { |
| 256 | $this->clear_header( $header ); |
| 257 | continue; |
| 258 | } |
| 259 | |
255 | 260 | $this->send_header( $header, $header_value ); |
256 | 261 | } |
257 | 262 | } |
… |
… |
class WP_REST_Server { |
1261 | 1266 | $this->send_header( $key, $value ); |
1262 | 1267 | } |
1263 | 1268 | } |
| 1269 | /** |
| 1270 | * Sends an HTTP header. |
| 1271 | * |
| 1272 | * @since 4.4.0 |
| 1273 | * @access public |
| 1274 | * |
| 1275 | * @param string $key Header key. |
| 1276 | * @param string $value Header value. |
| 1277 | */ |
| 1278 | |
| 1279 | /** |
| 1280 | * Removes an HTTP header from the current response. |
| 1281 | * |
| 1282 | * @since 4.8.0 |
| 1283 | * @access public |
| 1284 | * |
| 1285 | * @param string $key Header key. |
| 1286 | */ |
| 1287 | public function clear_header( $key ) { |
| 1288 | |
| 1289 | // In PHP 5.3+ there is a way to remove an already set header. |
| 1290 | if ( function_exists( 'header_remove' ) ) { |
| 1291 | @header_remove( $key ); |
| 1292 | } else { |
| 1293 | // In PHP 5.2, send an empty header, but only as a |
| 1294 | // last resort to override a header already sent. |
| 1295 | $this->send_header( $key, '' ); |
| 1296 | } |
| 1297 | } |
1264 | 1298 | |
1265 | 1299 | /** |
1266 | 1300 | * Retrieves the raw request entity (body). |
diff --git a/tests/phpunit/includes/spy-rest-server.php b/tests/phpunit/includes/spy-rest-server.php
index 58e3c3b521..722f1b7b50 100644
a
|
b
|
class Spy_REST_Server extends WP_REST_Server { |
31 | 31 | $this->sent_headers[ $header ] = $value; |
32 | 32 | } |
33 | 33 | |
| 34 | public function clear_header( $header ) { |
| 35 | unset( $this->sent_headers[ $header ] ); |
| 36 | } |
| 37 | |
34 | 38 | /** |
35 | 39 | * Override the dispatch method so we can get a handle on the request object. |
36 | 40 | * |
diff --git a/tests/phpunit/tests/rest-api/rest-server.php b/tests/phpunit/tests/rest-api/rest-server.php
index 046d2b5f62..1cd9e68463 100644
a
|
b
|
class Tests_REST_Server extends WP_Test_REST_TestCase { |
763 | 763 | $headers = $this->server->sent_headers; |
764 | 764 | |
765 | 765 | foreach ( wp_get_nocache_headers() as $header => $value ) { |
| 766 | if ( empty( $value ) ) { |
| 767 | continue; |
| 768 | } |
| 769 | |
766 | 770 | $this->assertTrue( isset( $headers[ $header ] ), sprintf( 'Header %s is not present in the response.', $header ) ); |
767 | 771 | $this->assertEquals( $value, $headers[ $header ] ); |
768 | 772 | } |
| 773 | |
| 774 | // Last-Modified should be unset as per #WP23021 |
| 775 | $this->assertFalse( isset( $headers['Last-Modified'] ), 'Last-Modified should not be sent.' ); |
769 | 776 | } |
770 | 777 | |
771 | 778 | public function test_no_nocache_headers_on_unauthenticated_requests() { |