Make WordPress Core


Ignore:
Timestamp:
02/04/2020 10:20:25 PM (5 years ago)
Author:
SergeyBiryukov
Message:

Posts, Post Types: Fail gracefully when checking whether the post should be displayed in WP_Query::get_posts() against unregistered post status.

If the post status is not registered, assume it's not public.

Follow-up to [47178].

Props roytanck.
See #48653.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r47122 r47179  
    30673067        if ( ! empty( $this->posts ) && ( $this->is_single || $this->is_page ) ) {
    30683068            $status = get_post_status( $this->posts[0] );
     3069
    30693070            if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) {
    30703071                $this->is_page       = false;
     
    30723073                $this->is_attachment = true;
    30733074            }
    3074             $post_status_obj = get_post_status_object( $status );
    30753075
    30763076            // If the post_status was specifically requested, let it pass through.
    3077             if ( ! $post_status_obj->public && ! in_array( $status, $q_status ) ) {
    3078 
    3079                 if ( ! is_user_logged_in() ) {
    3080                     // User must be logged in to view unpublished posts.
    3081                     $this->posts = array();
    3082                 } else {
    3083                     if ( $post_status_obj->protected ) {
    3084                         // User must have edit permissions on the draft to preview.
    3085                         if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) {
    3086                             $this->posts = array();
     3077            if ( ! in_array( $status, $q_status ) ) {
     3078                $post_status_obj = get_post_status_object( $status );
     3079
     3080                if ( $post_status_obj && ! $post_status_obj->public ) {
     3081                    if ( ! is_user_logged_in() ) {
     3082                        // User must be logged in to view unpublished posts.
     3083                        $this->posts = array();
     3084                    } else {
     3085                        if ( $post_status_obj->protected ) {
     3086                            // User must have edit permissions on the draft to preview.
     3087                            if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) {
     3088                                $this->posts = array();
     3089                            } else {
     3090                                $this->is_preview = true;
     3091                                if ( 'future' != $status ) {
     3092                                    $this->posts[0]->post_date = current_time( 'mysql' );
     3093                                }
     3094                            }
     3095                        } elseif ( $post_status_obj->private ) {
     3096                            if ( ! current_user_can( $read_cap, $this->posts[0]->ID ) ) {
     3097                                $this->posts = array();
     3098                            }
    30873099                        } else {
    3088                             $this->is_preview = true;
    3089                             if ( 'future' != $status ) {
    3090                                 $this->posts[0]->post_date = current_time( 'mysql' );
    3091                             }
    3092                         }
    3093                     } elseif ( $post_status_obj->private ) {
    3094                         if ( ! current_user_can( $read_cap, $this->posts[0]->ID ) ) {
    30953100                            $this->posts = array();
    30963101                        }
    3097                     } else {
    3098                         $this->posts = array();
    30993102                    }
     3103                } else {
     3104                    // Post status is not registered, assume it's not public.
     3105                    $this->posts = array();
    31003106                }
    31013107            }
Note: See TracChangeset for help on using the changeset viewer.