<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[51276] trunk: Query: Check each post-type's capabilities when querying multiple post-types.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/51276">51276</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/51276","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>peterwilsoncc</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-06-30 04:59:10 +0000 (Wed, 30 Jun 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Query: Check each post-type's capabilities when querying multiple post-types.

When querying multiple post types, check the `read_private_posts` capability for each post type when determining which post statuses to return. This ensures private posts appear in search results and archives for users permitted to read them.

Props leogermani, hellofromTonya, jeffpaul, peterwilsoncc.
Fixes <a href="https://core.trac.wordpress.org/ticket/48556">#48556</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesclasswpqueryphp">trunk/src/wp-includes/class-wp-query.php</a></li>
<li><a href="#trunktestsphpunittestsqueryinvalidQueriesphp">trunk/tests/phpunit/tests/query/invalidQueries.php</a></li>
<li><a href="#trunktestsphpunittestsquerypostStatusphp">trunk/tests/phpunit/tests/query/postStatus.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesclasswpqueryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/class-wp-query.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/class-wp-query.php  2021-06-30 04:12:20 UTC (rev 51275)
+++ trunk/src/wp-includes/class-wp-query.php    2021-06-30 04:59:10 UTC (rev 51276)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2420,26 +2420,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $skip_post_status = false;
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( 'any' === $post_type ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) );
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( empty( $in_search_post_types ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $where .= ' AND 1=0 ';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $post_type_where  = ' AND 1=0 ';
+                               $skip_post_status = true;
</ins><span class="cx" style="display: block; padding: 0 10px">                         } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $post_type_where = " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')";
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( ! empty( $post_type ) && is_array( $post_type ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where .= " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where = " AND {$wpdb->posts}.post_type IN ('" . implode( "', '", esc_sql( $post_type ) ) . "')";
</ins><span class="cx" style="display: block; padding: 0 10px">                 } elseif ( ! empty( $post_type ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where           .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where  = $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type );
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_type_object = get_post_type_object( $post_type );
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( $this->is_attachment ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where           .= " AND {$wpdb->posts}.post_type = 'attachment'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where  = " AND {$wpdb->posts}.post_type = 'attachment'";
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_type_object = get_post_type_object( 'attachment' );
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( $this->is_page ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where           .= " AND {$wpdb->posts}.post_type = 'page'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where  = " AND {$wpdb->posts}.post_type = 'page'";
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_type_object = get_post_type_object( 'page' );
</span><span class="cx" style="display: block; padding: 0 10px">                } else {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where           .= " AND {$wpdb->posts}.post_type = 'post'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $post_type_where  = " AND {$wpdb->posts}.post_type = 'post'";
</ins><span class="cx" style="display: block; padding: 0 10px">                         $post_type_object = get_post_type_object( 'post' );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2457,7 +2459,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $user_id = get_current_user_id();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $q_status = array();
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! empty( $q['post_status'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $skip_post_status ) {
+                       $where .= $post_type_where;
+               } elseif ( ! empty( $q['post_status'] ) ) {
+
+                       $where .= $post_type_where;
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         $statuswheres = array();
</span><span class="cx" style="display: block; padding: 0 10px">                        $q_status     = $q['post_status'];
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! is_array( $q_status ) ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2517,39 +2524,69 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                $where .= " AND ($where_status)";
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( ! $this->is_singular ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $where .= " AND ({$wpdb->posts}.post_status = 'publish'";
-
-                       // Add public states.
-                       $public_states = get_post_stati( array( 'public' => true ) );
-                       foreach ( (array) $public_states as $state ) {
-                               if ( 'publish' === $state ) { // Publish is hard-coded above.
-                                       continue;
-                               }
-                               $where .= " OR {$wpdb->posts}.post_status = '$state'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( 'any' === $post_type ) {
+                               $queried_post_types = get_post_types( array( 'exclude_from_search' => false ) );
+                       } elseif ( is_array( $post_type ) ) {
+                               $queried_post_types = $post_type;
+                       } elseif ( ! empty( $post_type ) ) {
+                               $queried_post_types = array( $post_type );
+                       } else {
+                               $queried_post_types = array( 'post' );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( $this->is_admin ) {
-                               // Add protected states that should show in the admin all list.
-                               $admin_all_states = get_post_stati(
-                                       array(
-                                               'protected'              => true,
-                                               'show_in_admin_all_list' => true,
-                                       )
-                               );
-                               foreach ( (array) $admin_all_states as $state ) {
-                                       $where .= " OR {$wpdb->posts}.post_status = '$state'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! empty( $queried_post_types ) ) {
+
+                               $status_type_clauses = array();
+
+                               foreach ( $queried_post_types as $queried_post_type ) {
+
+                                       $queried_post_type_object = get_post_type_object( $queried_post_type );
+
+                                       $type_where = '(' . $wpdb->prepare( "{$wpdb->posts}.post_type = %s AND (", $queried_post_type );
+
+                                       // Public statuses.
+                                       $public_statuses = get_post_stati( array( 'public' => true ) );
+                                       $status_clauses  = array();
+                                       foreach ( (array) $public_statuses as $public_status ) {
+                                               $status_clauses[] = "{$wpdb->posts}.post_status = '$public_status'";
+                                       }
+                                       $type_where .= implode( ' OR ', $status_clauses );
+
+                                       // Add protected states that should show in the admin all list.
+                                       if ( $this->is_admin ) {
+                                               $admin_all_statuses = get_post_stati(
+                                                       array(
+                                                               'protected'              => true,
+                                                               'show_in_admin_all_list' => true,
+                                                       )
+                                               );
+                                               foreach ( (array) $admin_all_statuses as $admin_all_status ) {
+                                                       $type_where .= " OR {$wpdb->posts}.post_status = '$admin_all_status'";
+                                               }
+                                       }
+
+                                       // Add private states that are visible to current user.
+                                       if ( is_user_logged_in() && $queried_post_type_object instanceof WP_Post_Type ) {
+                                               $read_private_cap = $queried_post_type_object->cap->read_private_posts;
+                                               $private_statuses = get_post_stati( array( 'private' => true ) );
+                                               foreach ( (array) $private_statuses as $private_status ) {
+                                                       $type_where .= current_user_can( $read_private_cap ) ? " \nOR {$wpdb->posts}.post_status = '$private_status'" : " \nOR ({$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$private_status')";
+                                               }
+                                       }
+
+                                       $type_where .= '))';
+
+                                       $status_type_clauses[] = $type_where;
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        }
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( is_user_logged_in() ) {
-                               // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
-                               $private_states = get_post_stati( array( 'private' => true ) );
-                               foreach ( (array) $private_states as $state ) {
-                                       $where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'";
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         if ( ! empty( $status_type_clauses ) ) {
+                                       $where .= ' AND (' . implode( ' OR ', $status_type_clauses ) . ')';
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        } else {
+                               $where .= ' AND 1=0 ';
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-                       $where .= ')';
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         } else {
+                       $where .= $post_type_where;
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                /*
</span></span></pre></div>
<a id="trunktestsphpunittestsqueryinvalidQueriesphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/query/invalidQueries.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query/invalidQueries.php        2021-06-30 04:12:20 UTC (rev 51275)
+++ trunk/tests/phpunit/tests/query/invalidQueries.php  2021-06-30 04:59:10 UTC (rev 51276)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -99,6 +99,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * Test WP Query with an invalid post type in a mutiple post type query.
+        *
+        * @ticket 48556
+        */
+       public function test_unregistered_post_type_wp_query_multiple_post_types() {
+               global $wpdb;
+
+               $query = new WP_Query(
+                       array(
+                               'post_type' => array( 'unregistered_cpt', 'page' ),
+                       )
+               );
+               $posts = $query->get_posts();
+
+               $this->assertContains( "{$wpdb->posts}.post_type = 'unregistered_cpt'", self::$last_posts_request );
+               $this->assertCount( 1, $posts, 'the valid `page` post type should still return one post' );
+       }
+
+       /**
</ins><span class="cx" style="display: block; padding: 0 10px">          * Test WP Query with an invalid post type specified in the URL.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @ticket 48556
</span></span></pre></div>
<a id="trunktestsphpunittestsquerypostStatusphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/query/postStatus.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/query/postStatus.php    2021-06-30 04:12:20 UTC (rev 51275)
+++ trunk/tests/phpunit/tests/query/postStatus.php      2021-06-30 04:59:10 UTC (rev 51276)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -6,22 +6,21 @@
</span><span class="cx" style="display: block; padding: 0 10px"> class Tests_Query_PostStatus extends WP_UnitTestCase {
</span><span class="cx" style="display: block; padding: 0 10px">        public static $editor_user_id;
</span><span class="cx" style="display: block; padding: 0 10px">        public static $author_user_id;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public static $editor_private_post;
-       public static $author_private_post;
-       public static $editor_privatefoo_post;
-       public static $author_privatefoo_post;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public static $subscriber_user_id;
+       public static $post_ids;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                self::$editor_user_id = $factory->user->create( array( 'role' => 'editor' ) );
-               self::$author_user_id = $factory->user->create( array( 'role' => 'author' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::$editor_user_id     = $factory->user->create( array( 'role' => 'editor' ) );
+               self::$author_user_id     = $factory->user->create( array( 'role' => 'author' ) );
+               self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                self::$editor_private_post = $factory->post->create(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::$post_ids['editor_private_post'] = $factory->post->create(
</ins><span class="cx" style="display: block; padding: 0 10px">                         array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_author' => self::$editor_user_id,
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_status' => 'private',
</span><span class="cx" style="display: block; padding: 0 10px">                        )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                self::$author_private_post = $factory->post->create(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::$post_ids['author_private_post'] = $factory->post->create(
</ins><span class="cx" style="display: block; padding: 0 10px">                         array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_author' => self::$author_user_id,
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_status' => 'private',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -30,13 +29,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Custom status with private=true.
</span><span class="cx" style="display: block; padding: 0 10px">                register_post_status( 'privatefoo', array( 'private' => true ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                self::$editor_privatefoo_post = $factory->post->create(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::$post_ids['editor_privatefoo_post'] = $factory->post->create(
</ins><span class="cx" style="display: block; padding: 0 10px">                         array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_author' => self::$editor_user_id,
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_status' => 'privatefoo',
</span><span class="cx" style="display: block; padding: 0 10px">                        )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                self::$author_privatefoo_post = $factory->post->create(
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         self::$post_ids['author_privatefoo_post'] = $factory->post->create(
</ins><span class="cx" style="display: block; padding: 0 10px">                         array(
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_author' => self::$author_user_id,
</span><span class="cx" style="display: block; padding: 0 10px">                                'post_status' => 'privatefoo',
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -43,8 +42,72 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px">                _unregister_post_status( 'privatefoo' );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               self::register_custom_post_objects();
+
+               self::$post_ids['wptests_pt1_p1'] = $factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt1',
+                               'post_status' => 'private',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               self::$post_ids['wptests_pt1_p2'] = $factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt1',
+                               'post_status' => 'publish',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               self::$post_ids['wptests_pt2_p1'] = $factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt2',
+                               'post_status' => 'private',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
+
+               self::$post_ids['wptests_pt2_p2'] = $factory->post->create(
+                       array(
+                               'post_type'   => 'wptests_pt2',
+                               'post_status' => 'publish',
+                               'post_author' => self::$editor_user_id,
+                       )
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        public function setUp() {
+               parent::setUp();
+               self::register_custom_post_objects();
+       }
+
+       /**
+        * Register custom post types and statuses used in multiple tests.
+        *
+        * CPTs and CPSs are reset between each test run so need to be registered
+        * in both the wpSetUpBeforeClass() and setUp() methods.
+        */
+       public static function register_custom_post_objects() {
+               register_post_type(
+                       'wptests_pt1',
+                       array(
+                               'exclude_from_search' => false,
+                               'capabilities'        => array(
+                                       'read_private_posts' => 'read_private_pt1s',
+                               ),
+                       )
+               );
+
+               register_post_type(
+                       'wptests_pt2',
+                       array(
+                               'exclude_from_search' => false,
+                       )
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_any_should_not_include_statuses_where_exclude_from_search_is_true() {
</span><span class="cx" style="display: block; padding: 0 10px">                register_post_status( 'foo', array( 'exclude_from_search' => true ) );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -78,8 +141,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $expected = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        self::$editor_private_post,
-                       self::$author_private_post,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 self::$post_ids['editor_private_post'],
+                       self::$post_ids['author_private_post'],
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSameSets( $expected, wp_list_pluck( $q->posts, 'ID' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -109,7 +172,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $expected = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        self::$author_private_post,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 self::$post_ids['author_private_post'],
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSameSets( $expected, wp_list_pluck( $q->posts, 'ID' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -126,8 +189,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $expected = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        self::$author_private_post,
-                       self::$editor_private_post,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 self::$post_ids['author_private_post'],
+                       self::$post_ids['editor_private_post'],
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSameSets( $expected, wp_list_pluck( $q->posts, 'ID' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -144,7 +207,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $expected = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        self::$author_private_post,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 self::$post_ids['author_private_post'],
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSameSets( $expected, wp_list_pluck( $q->posts, 'ID' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -161,8 +224,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $expected = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        self::$author_private_post,
-                       self::$editor_private_post,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 self::$post_ids['author_private_post'],
+                       self::$post_ids['editor_private_post'],
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSameSets( $expected, wp_list_pluck( $q->posts, 'ID' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -225,8 +288,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( self::$author_privatefoo_post, wp_list_pluck( $q->posts, 'ID' ) );
-               $this->assertContains( self::$editor_privatefoo_post, wp_list_pluck( $q->posts, 'ID' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertContains( self::$post_ids['author_privatefoo_post'], wp_list_pluck( $q->posts, 'ID' ) );
+               $this->assertContains( self::$post_ids['editor_privatefoo_post'], wp_list_pluck( $q->posts, 'ID' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_private_statuses_should_not_be_included_when_current_user_cannot_read_private_posts() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -236,16 +299,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $q = new WP_Query(
</span><span class="cx" style="display: block; padding: 0 10px">                        array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                'posts_per_page' => 2, // Or the query will short-circuit.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         'posts_per_page' => -1, // Or the query will short-circuit.
</ins><span class="cx" style="display: block; padding: 0 10px">                         )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $expected = array(
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        self::$author_privatefoo_post,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 self::$post_ids['author_privatefoo_post'],
</ins><span class="cx" style="display: block; padding: 0 10px">                 );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertContains( self::$author_privatefoo_post, wp_list_pluck( $q->posts, 'ID' ) );
-               $this->assertNotContains( self::$editor_privatefoo_post, wp_list_pluck( $q->posts, 'ID' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertContains( self::$post_ids['author_privatefoo_post'], wp_list_pluck( $q->posts, 'ID' ) );
+               $this->assertNotContains( self::$post_ids['editor_privatefoo_post'], wp_list_pluck( $q->posts, 'ID' ) );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        public function test_single_post_with_nonpublic_status_should_not_be_shown_to_logged_out_users() {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -457,4 +520,64 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertContains( $p1, wp_list_pluck( $q->posts, 'ID' ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 48556
+        * @ticket 13509
+        */
+       public function test_non_singular_queries_using_post_type_any_should_respect_post_type_read_private_posts_cap() {
+               $post_ids = self::$post_ids;
+
+               wp_set_current_user( 0 );
+
+               $q = new WP_Query(
+                       array(
+                               'post_type' => 'any',
+                       )
+               );
+
+               $this->assertSameSets( array( $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
+
+               wp_set_current_user( self::$subscriber_user_id );
+               $GLOBALS['current_user']->add_cap( 'read_private_pt1s' );
+
+               $q = new WP_Query(
+                       array(
+                               'post_type' => 'any',
+                       )
+               );
+
+               $this->assertSameSets( array( $post_ids['wptests_pt1_p1'], $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
+       }
+
+       /**
+        * @ticket 48556
+        * @ticket 13509
+        */
+       public function test_non_singular_queries_using_multiple_post_type_should_respect_post_type_read_private_posts_cap() {
+               wp_set_current_user( 0 );
+
+               $post_ids = self::$post_ids;
+
+               $q = new WP_Query(
+                       array(
+                               'post_type'      => 'any',
+                               'posts_per_page' => -1,
+                       )
+               );
+
+               $this->assertSameSets( array( $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
+
+               wp_set_current_user( self::$subscriber_user_id );
+               $GLOBALS['current_user']->add_cap( 'read_private_pt1s' );
+
+               $q = new WP_Query(
+                       array(
+                               'post_type'      => array( 'wptests_pt1', 'wptests_pt2' ),
+                               'posts_per_page' => -1,
+                       )
+               );
+
+               $this->assertSameSets( array( $post_ids['wptests_pt1_p1'], $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>