Make WordPress Core


Ignore:
Timestamp:
01/29/2025 06:10:47 PM (20 months ago)
Author:
johnbillion
Message:

Posts, Post Types: Add no-cache headers to password protected posts.

This instructs an intermediate cache, for example a proxy server, to not cache a password protected post both before and after a visitor has entered a password.

Props brevilo, haozi, ironprogrammer, narenin

Fixes #61711

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/wp/sendHeaders.php

    r54710 r59728  
    77 */
    88class Tests_WP_SendHeaders extends WP_UnitTestCase {
     9        protected $headers_sent = array();
    910
    1011        /**
     
    3637                $this->go_to( get_permalink( $post_id ) );
    3738        }
     39
     40        /**
     41         * @ticket 61711
     42         */
     43        public function test_send_headers_sets_cache_control_header_for_password_protected_posts() {
     44                $password = 'password';
     45
     46                add_filter(
     47                        'wp_headers',
     48                        function ( $headers ) {
     49                                $this->headers_sent = $headers;
     50                                return $headers;
     51                        }
     52                );
     53
     54                $post_id = self::factory()->post->create(
     55                        array(
     56                                'post_password' => $password,
     57                        )
     58                );
     59                $this->go_to( get_permalink( $post_id ) );
     60
     61                $headers_without_password         = $this->headers_sent;
     62                $password_status_without_password = post_password_required( $post_id );
     63
     64                require_once ABSPATH . WPINC . '/class-phpass.php';
     65
     66                $hash = ( new PasswordHash( 8, true ) )->HashPassword( $password );
     67
     68                $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] = $hash;
     69
     70                $this->go_to( get_permalink( $post_id ) );
     71
     72                $headers_with_password         = $this->headers_sent;
     73                $password_status_with_password = post_password_required( $post_id );
     74
     75                $this->assertTrue( $password_status_without_password );
     76                $this->assertArrayHasKey( 'Cache-Control', $headers_without_password );
     77
     78                $this->assertFalse( $password_status_with_password );
     79                $this->assertArrayHasKey( 'Cache-Control', $headers_with_password );
     80        }
    3881}
Note: See TracChangeset for help on using the changeset viewer.