WordPress.org

Make WordPress Core

Changeset 32683


Ignore:
Timestamp:
06/02/2015 01:29:44 PM (4 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.

Location:
trunk
Files:
2 edited

Legend:

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

    r32637 r32683  
    570570     *                                         include meta fields. Default 'all'.
    571571     *     @type string       $who             Type of users to query. Accepts 'authors'. Default empty (all users).
     572     *     @type bool|array   $has_published_posts Pass an array of post types to filter results to users who have
     573     *                                             published posts in those post types. `true` is an alias for all
     574     *                                             public post types.
    572575     * }
    573576     */
     
    593596                'count_total' => true,
    594597                'fields' => 'all',
    595                 'who' => ''
     598                'who' => '',
     599                'has_published_posts' => null,
    596600            ) );
    597601        }
     
    650654            $qv['meta_compare'] = '!=';
    651655            $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
     656        }
     657
     658        if ( $qv['has_published_posts'] && $blog_id ) {
     659            if ( true === $qv['has_published_posts'] ) {
     660                $post_types = get_post_types( array( 'public' => true ) );
     661            } else {
     662                $post_types = (array) $qv['has_published_posts'];
     663            }
     664
     665            foreach ( $post_types as &$post_type ) {
     666                $post_type = $wpdb->prepare( '%s', $post_type );
     667            }
     668
     669            $posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
     670            $this->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )";
    652671        }
    653672
  • 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.