Make WordPress Core


Ignore:
Timestamp:
12/17/2020 04:15:38 PM (4 years ago)
Author:
boonebgorges
Message:

Query: Respect post-type specific capabilities when querying for multiple post types.

After this change, the relevant read_private_posts capability is checked for
each queried post type. This ensures that private posts appear in search and
archive queries for users who have the ability to view those posts.

Props leogermani.

Fixes #13509, #48968, #48556.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/query/postStatus.php

    r49603 r49830  
    77    public static $editor_user_id;
    88    public static $author_user_id;
     9    public static $subscriber_user_id;
    910    public static $editor_private_post;
    1011    public static $author_private_post;
     
    1314
    1415    public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
    15         self::$editor_user_id = $factory->user->create( array( 'role' => 'editor' ) );
    16         self::$author_user_id = $factory->user->create( array( 'role' => 'author' ) );
     16        self::$editor_user_id     = $factory->user->create( array( 'role' => 'editor' ) );
     17        self::$author_user_id     = $factory->user->create( array( 'role' => 'author' ) );
     18        self::$subscriber_user_id = $factory->user->create( array( 'role' => 'subscriber' ) );
    1719
    1820        self::$editor_private_post = $factory->post->create(
     
    458460        $this->assertContains( $p1, wp_list_pluck( $q->posts, 'ID' ) );
    459461    }
     462
     463    /**
     464     * @ticket 48556
     465     * @ticket 13509
     466     */
     467    public function test_non_singular_queries_using_post_type_any_should_respect_post_type_read_private_posts_cap() {
     468        register_post_type(
     469            'wptests_pt1',
     470            array(
     471                'exclude_from_search' => false,
     472                'capabilities'        => [
     473                    'read_private_posts' => 'read_private_pt1s',
     474                ],
     475            )
     476        );
     477
     478        register_post_type(
     479            'wptests_pt2',
     480            array(
     481                'exclude_from_search' => false,
     482            )
     483        );
     484
     485        $post_ids = array();
     486
     487        $post_ids['wptests_pt1_p1'] = $this->factory->post->create(
     488            array(
     489                'post_type'   => 'wptests_pt1',
     490                'post_status' => 'private',
     491                'post_author' => self::$editor_user_id,
     492            )
     493        );
     494
     495        $post_ids['wptests_pt1_p2'] = $this->factory->post->create(
     496            array(
     497                'post_type'   => 'wptests_pt1',
     498                'post_status' => 'publish',
     499                'post_author' => self::$editor_user_id,
     500            )
     501        );
     502
     503        $post_ids['wptests_pt2_p1'] = $this->factory->post->create(
     504            array(
     505                'post_type'   => 'wptests_pt2',
     506                'post_status' => 'private',
     507                'post_author' => self::$editor_user_id,
     508            )
     509        );
     510
     511        $post_ids['wptests_pt2_p2'] = $this->factory->post->create(
     512            array(
     513                'post_type'   => 'wptests_pt2',
     514                'post_status' => 'publish',
     515                'post_author' => self::$editor_user_id,
     516            )
     517        );
     518
     519        wp_set_current_user( 0 );
     520
     521        $q = new WP_Query(
     522            array(
     523                'post_type' => 'any',
     524            )
     525        );
     526
     527        $this->assertSameSets( array( $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
     528
     529        wp_set_current_user( self::$subscriber_user_id );
     530        $GLOBALS['current_user']->add_cap( 'read_private_pt1s' );
     531
     532        $q = new WP_Query(
     533            array(
     534                'post_type' => 'any',
     535            )
     536        );
     537
     538        $this->assertSameSets( array( $post_ids['wptests_pt1_p1'], $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
     539    }
     540
     541    /**
     542     * @ticket 48556
     543     * @ticket 13509
     544     */
     545    public function test_non_singular_queries_using_multiple_post_type_should_respect_post_type_read_private_posts_cap() {
     546        wp_set_current_user( 0 );
     547
     548        register_post_type(
     549            'wptests_pt1',
     550            array(
     551                'exclude_from_search' => false,
     552                'capabilities'        => [
     553                    'read_private_posts' => 'read_private_pt1s',
     554                ],
     555            )
     556        );
     557
     558        register_post_type(
     559            'wptests_pt2',
     560            array(
     561                'exclude_from_search' => false,
     562            )
     563        );
     564
     565        $post_ids = array();
     566
     567        $post_ids['wptests_pt1_p1'] = $this->factory->post->create(
     568            array(
     569                'post_type'   => 'wptests_pt1',
     570                'post_status' => 'private',
     571                'post_author' => self::$editor_user_id,
     572            )
     573        );
     574
     575        $post_ids['wptests_pt1_p2'] = $this->factory->post->create(
     576            array(
     577                'post_type'   => 'wptests_pt1',
     578                'post_status' => 'publish',
     579                'post_author' => self::$editor_user_id,
     580            )
     581        );
     582
     583        $post_ids['wptests_pt2_p1'] = $this->factory->post->create(
     584            array(
     585                'post_type'   => 'wptests_pt2',
     586                'post_status' => 'private',
     587                'post_author' => self::$editor_user_id,
     588            )
     589        );
     590
     591        $post_ids['wptests_pt2_p2'] = $this->factory->post->create(
     592            array(
     593                'post_type'   => 'wptests_pt2',
     594                'post_status' => 'publish',
     595                'post_author' => self::$editor_user_id,
     596            )
     597        );
     598
     599        $q = new WP_Query(
     600            array(
     601                'post_type' => 'any',
     602            )
     603        );
     604
     605        $this->assertSameSets( array( $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
     606
     607        $u = $this->factory->user->create();
     608
     609        wp_set_current_user( self::$subscriber_user_id );
     610        $GLOBALS['current_user']->add_cap( 'read_private_pt1s' );
     611
     612        $q = new WP_Query(
     613            array(
     614                'post_type' => [ 'wptests_pt1', 'wptests_pt2' ],
     615            )
     616        );
     617
     618        $this->assertSameSets( array( $post_ids['wptests_pt1_p1'], $post_ids['wptests_pt1_p2'], $post_ids['wptests_pt2_p2'] ), wp_list_pluck( $q->posts, 'ID' ) );
     619    }
    460620}
Note: See TracChangeset for help on using the changeset viewer.