Make WordPress Core


Ignore:
Timestamp:
06/02/2015 01:29:44 PM (11 years ago)
Author:
boonebgorges
Message:

Introduce 'has_published_posts' parameter for WP_User_Query.

This allows user query results to be limited to those users who have published
posts in at least one of the specified post types.

Props joehoyle, boonebgorges.
Fixes #32250.

File:
1 edited

Legend:

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

    r32207 r32683  
    687687                $this->assertNotContains( $users[2], $found );
    688688        }
     689
     690        /**
     691         * @ticket 32250
     692         */
     693        public function test_has_published_posts_with_value_true_should_show_authors_of_posts_in_public_post_types() {
     694                register_post_type( 'wptests_pt_public', array( 'public' => true ) );
     695                register_post_type( 'wptests_pt_private', array( 'public' => false ) );
     696
     697                $users = $this->factory->user->create_many( 3 );
     698
     699                $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) );
     700                $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) );
     701
     702                $q = new WP_User_Query( array(
     703                        'has_published_posts' => true,
     704                ) );
     705
     706                $found = wp_list_pluck( $q->get_results(), 'ID' );
     707                $expected = array( $users[0] );
     708
     709                $this->assertEqualSets( $expected, $found );
     710        }
     711
     712        /**
     713         * @ticket 32250
     714         */
     715        public function test_has_published_posts_should_obey_post_types() {
     716                register_post_type( 'wptests_pt_public', array( 'public' => true ) );
     717                register_post_type( 'wptests_pt_private', array( 'public' => false ) );
     718
     719                $users = $this->factory->user->create_many( 3 );
     720
     721                $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) );
     722                $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) );
     723                $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) );
     724
     725                $q = new WP_User_Query( array(
     726                        'has_published_posts' => array( 'wptests_pt_private', 'post' ),
     727                ) );
     728
     729                $found = wp_list_pluck( $q->get_results(), 'ID' );
     730                $expected = array( $users[1], $users[2] );
     731
     732                $this->assertEqualSets( $expected, $found );
     733        }
     734
     735        /**
     736         * @ticket 32250
     737         */
     738        public function test_has_published_posts_should_ignore_non_published_posts() {
     739                register_post_type( 'wptests_pt_public', array( 'public' => true ) );
     740                register_post_type( 'wptests_pt_private', array( 'public' => false ) );
     741
     742                $users = $this->factory->user->create_many( 3 );
     743
     744                $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) );
     745                $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) );
     746                $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) );
     747
     748                $q = new WP_User_Query( array(
     749                        'has_published_posts' => array( 'wptests_pt_public', 'wptests_pt_private', 'post' ),
     750                ) );
     751
     752                $found = wp_list_pluck( $q->get_results(), 'ID' );
     753                $expected = array( $users[2] );
     754
     755                $this->assertEqualSets( $expected, $found );
     756        }
     757
     758        /**
     759         * @ticket 32250
     760         */
     761        public function test_has_published_posts_should_respect_blog_id() {
     762                if ( ! is_multisite() ) {
     763                        $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
     764                }
     765
     766                $users = $this->factory->user->create_many( 3 );
     767                $blogs = $this->factory->blog->create_many( 2 );
     768
     769                add_user_to_blog( $blogs[0], $users[0], 'author' );
     770                add_user_to_blog( $blogs[0], $users[1], 'author' );
     771                add_user_to_blog( $blogs[1], $users[0], 'author' );
     772                add_user_to_blog( $blogs[1], $users[1], 'author' );
     773
     774                switch_to_blog( $blogs[0] );
     775                $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'post' ) );
     776                restore_current_blog();
     777
     778                switch_to_blog( $blogs[1] );
     779                $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'post' ) );
     780                restore_current_blog();
     781
     782                $q = new WP_User_Query( array(
     783                        'has_published_posts' => array( 'post' ),
     784                        'blog_id' => $blogs[1],
     785                ) );
     786
     787                $found = wp_list_pluck( $q->get_results(), 'ID' );
     788                $expected = array( $users[1] );
     789
     790                $this->assertEqualSets( $expected, $found );
     791        }
    689792}
Note: See TracChangeset for help on using the changeset viewer.