Make WordPress Core


Ignore:
Timestamp:
04/15/2021 01:06:00 AM (5 years ago)
Author:
desrosj
Message:

Grouped merges for 5.6.3.

  • REST API: Allow authors to read their own password protected posts.
  • About page update.

Merges [50717] to the 5.6 branch.

Location:
branches/5.6
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.6

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

    r49732 r50726  
    3131         */
    3232        protected $meta;
     33
     34        /**
     35         * Passwordless post access permitted.
     36         *
     37         * @since 5.7.1
     38         * @var int[]
     39         */
     40        protected $password_check_passed = array();
    3341
    3442        /**
     
    147155
    148156                return true;
     157        }
     158
     159        /**
     160         * Override the result of the post password check for REST requested posts.
     161         *
     162         * Allow users to read the content of password protected posts if they have
     163         * previously passed a permission check or if they have the `edit_post` capability
     164         * for the post being checked.
     165         *
     166         * @since 5.7.1
     167         *
     168         * @param bool    $required Whether the post requires a password check.
     169         * @param WP_Post $post     The post been password checked.
     170         * @return bool Result of password check taking in to account REST API considerations.
     171         */
     172        public function check_password_required( $required, $post ) {
     173                if ( ! $required ) {
     174                        return $required;
     175                }
     176
     177                $post = get_post( $post );
     178
     179                if ( ! $post ) {
     180                        return $required;
     181                }
     182
     183                if ( ! empty( $this->password_check_passed[ $post->ID ] ) ) {
     184                        // Password previously checked and approved.
     185                        return false;
     186                }
     187
     188                return ! current_user_can( 'edit_post', $post->ID );
    149189        }
    150190
     
    316356                // Allow access to all password protected posts if the context is edit.
    317357                if ( 'edit' === $request['context'] ) {
    318                         add_filter( 'post_password_required', '__return_false' );
     358                        add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
    319359                }
    320360
     
    332372                // Reset filter.
    333373                if ( 'edit' === $request['context'] ) {
    334                         remove_filter( 'post_password_required', '__return_false' );
     374                        remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
    335375                }
    336376
     
    447487                // Allow access to all password protected posts if the context is edit.
    448488                if ( 'edit' === $request['context'] ) {
    449                         add_filter( 'post_password_required', '__return_false' );
     489                        add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
    450490                }
    451491
     
    475515                }
    476516
    477                 // Edit context always gets access to password-protected posts.
    478                 if ( 'edit' === $request['context'] ) {
     517                /*
     518                 * Users always gets access to password protected content in the edit
     519                 * context if they have the `edit_post` meta capability.
     520                 */
     521                if (
     522                        'edit' === $request['context'] &&
     523                        current_user_can( 'edit_post', $post->ID )
     524                ) {
    479525                        return true;
    480526                }
     
    17061752
    17071753                if ( $this->can_access_password_content( $post, $request ) ) {
     1754                        $this->password_check_passed[ $post->ID ] = true;
    17081755                        // Allow access to the post, permissions already checked before.
    1709                         add_filter( 'post_password_required', '__return_false' );
     1756                        add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
    17101757
    17111758                        $has_password_filter = true;
     
    17451792                if ( $has_password_filter ) {
    17461793                        // Reset filter.
    1747                         remove_filter( 'post_password_required', '__return_false' );
     1794                        remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
    17481795                }
    17491796
Note: See TracChangeset for help on using the changeset viewer.