Make WordPress Core

Ticket #41445: 41445.2.diff

File 41445.2.diff, 2.3 KB (added by adamsilverstein, 5 years ago)
  • src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php

    diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php
    index d30c6c0d90..933fc70171 100644
    a b class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { 
    659659                return $this->add_additional_fields_schema( $this->schema );
    660660        }
    661661
     662        /**
     663         * Checks if a given request has access to read an attachment.
     664         *
     665         * @param WP_REST_Request $request Full details about the request.
     666         * @return bool|WP_Error True if the request has read access for the item, WP_Error object otherwise.
     667         */
     668        public function get_item_permissions_check( $request ) {
     669                $query_args = array(
     670                        'post_status' => 'any',
     671                        'post_type' => 'any',
     672                        'meta_key'   => '_thumbnail_id',
     673                        'meta_value' => $request['id'],
     674                        'no_found_rows' => true,
     675                        'posts_per_page' => 100, // Set a reasonable maximum number of results.
     676                );
     677
     678                $posts_query  = new WP_Query();
     679                $query_result = $posts_query->query( $query_args );
     680
     681                $wp_error = false;
     682
     683                foreach ( $query_result as $post ) {
     684
     685                        if ( is_wp_error( $post ) ) {
     686                                continue;
     687                        }
     688
     689
     690                        if ( 'edit' === $request['context'] && $post && ! $this->check_update_permission( $post ) ) {
     691                                $wp_error = WP_Error( 'rest_forbidden_context', __( 'Sorry, you are not allowed to edit this post.' ), array( 'status' => rest_authorization_required_code() ) );
     692                        }
     693
     694                        if ( $post && ! empty( $request['password'] ) ) {
     695                                // Check post password, and return error if invalid.
     696                                if ( ! hash_equals( $post->post_password, $request['password'] ) ) {
     697                                        $wp_error = WP_Error( 'rest_post_incorrect_password', __( 'Incorrect post password.' ), array( 'status' => 403 ) );
     698                                }
     699                        }
     700
     701                        // Allow access to all password protected posts if the context is edit.
     702                        if ( 'edit' === $request['context'] ) {
     703                                add_filter( 'post_password_required', '__return_false' );
     704                        }
     705
     706                        if ( $post && $this->check_read_permission( $post ) ) {
     707                                return true;
     708                        }
     709                }
     710
     711                if( $wp_error && ! $has_permission ) {
     712                        return $wp_error;
     713                }
     714
     715                return parent::get_item_permissions_check( $request );
     716        }
     717
    662718        /**
    663719         * Handles an upload via raw POST data.
    664720         *