Make WordPress Core


Ignore:
Timestamp:
04/15/2021 01:07:14 AM (5 years ago)
Author:
peterwilsoncc
Message:

Grouped merges for 5.3.7.

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

Merges [50717] to the 5.3 branch.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.3/src/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php

    r46897 r50728  
    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        /**
     
    143151
    144152                return true;
     153        }
     154
     155        /**
     156         * Override the result of the post password check for REST requested posts.
     157         *
     158         * Allow users to read the content of password protected posts if they have
     159         * previously passed a permission check or if they have the `edit_post` capability
     160         * for the post being checked.
     161         *
     162         * @since 5.7.1
     163         *
     164         * @param bool    $required Whether the post requires a password check.
     165         * @param WP_Post $post     The post been password checked.
     166         * @return bool Result of password check taking in to account REST API considerations.
     167         */
     168        public function check_password_required( $required, $post ) {
     169                if ( ! $required ) {
     170                        return $required;
     171                }
     172
     173                $post = get_post( $post );
     174
     175                if ( ! $post ) {
     176                        return $required;
     177                }
     178
     179                if ( ! empty( $this->password_check_passed[ $post->ID ] ) ) {
     180                        // Password previously checked and approved.
     181                        return false;
     182                }
     183
     184                return ! current_user_can( 'edit_post', $post->ID );
    145185        }
    146186
     
    300340                // Allow access to all password protected posts if the context is edit.
    301341                if ( 'edit' === $request['context'] ) {
    302                         add_filter( 'post_password_required', '__return_false' );
     342                        add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
    303343                }
    304344
     
    316356                // Reset filter.
    317357                if ( 'edit' === $request['context'] ) {
    318                         remove_filter( 'post_password_required', '__return_false' );
     358                        remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
    319359                }
    320360
     
    414454                // Allow access to all password protected posts if the context is edit.
    415455                if ( 'edit' === $request['context'] ) {
    416                         add_filter( 'post_password_required', '__return_false' );
     456                        add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
    417457                }
    418458
     
    442482                }
    443483
    444                 // Edit context always gets access to password-protected posts.
    445                 if ( 'edit' === $request['context'] ) {
     484                /*
     485                 * Users always gets access to password protected content in the edit
     486                 * context if they have the `edit_post` meta capability.
     487                 */
     488                if (
     489                        'edit' === $request['context'] &&
     490                        current_user_can( 'edit_post', $post->ID )
     491                ) {
    446492                        return true;
    447493                }
     
    15341580
    15351581                if ( $this->can_access_password_content( $post, $request ) ) {
     1582                        $this->password_check_passed[ $post->ID ] = true;
    15361583                        // Allow access to the post, permissions already checked before.
    1537                         add_filter( 'post_password_required', '__return_false' );
     1584                        add_filter( 'post_password_required', array( $this, 'check_password_required' ), 10, 2 );
    15381585
    15391586                        $has_password_filter = true;
     
    15691616                if ( $has_password_filter ) {
    15701617                        // Reset filter.
    1571                         remove_filter( 'post_password_required', '__return_false' );
     1618                        remove_filter( 'post_password_required', array( $this, 'check_password_required' ) );
    15721619                }
    15731620
Note: See TracChangeset for help on using the changeset viewer.