From 6e8fd2509ec99b909543e6097349fb765432f6f5 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 2 Jun 2015 13:29:44 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@32683 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/user.php | 21 +++++- tests/phpunit/tests/user/query.php | 103 +++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/user.php b/src/wp-includes/user.php index c8d756f798..e3a1ba6108 100644 --- a/src/wp-includes/user.php +++ b/src/wp-includes/user.php @@ -569,6 +569,9 @@ class WP_User_Query { * 'url', 'registered'. Use 'all' for all fields and 'all_with_meta' to * include meta fields. Default 'all'. * @type string $who Type of users to query. Accepts 'authors'. Default empty (all users). + * @type bool|array $has_published_posts Pass an array of post types to filter results to users who have + * published posts in those post types. `true` is an alias for all + * public post types. * } */ public function prepare_query( $query = array() ) { @@ -592,7 +595,8 @@ class WP_User_Query { 'number' => '', 'count_total' => true, 'fields' => 'all', - 'who' => '' + 'who' => '', + 'has_published_posts' => null, ) ); } @@ -651,6 +655,21 @@ class WP_User_Query { $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query } + if ( $qv['has_published_posts'] && $blog_id ) { + if ( true === $qv['has_published_posts'] ) { + $post_types = get_post_types( array( 'public' => true ) ); + } else { + $post_types = (array) $qv['has_published_posts']; + } + + foreach ( $post_types as &$post_type ) { + $post_type = $wpdb->prepare( '%s', $post_type ); + } + + $posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts'; + $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 ) . " ) )"; + } + // Meta query. $this->meta_query = new WP_Meta_Query(); $this->meta_query->parse_query_vars( $qv ); diff --git a/tests/phpunit/tests/user/query.php b/tests/phpunit/tests/user/query.php index 9abf99da2f..2b22f1ec06 100644 --- a/tests/phpunit/tests/user/query.php +++ b/tests/phpunit/tests/user/query.php @@ -686,4 +686,107 @@ class Tests_User_Query extends WP_UnitTestCase { $this->assertContains( $users[1], $found ); $this->assertNotContains( $users[2], $found ); } + + /** + * @ticket 32250 + */ + public function test_has_published_posts_with_value_true_should_show_authors_of_posts_in_public_post_types() { + register_post_type( 'wptests_pt_public', array( 'public' => true ) ); + register_post_type( 'wptests_pt_private', array( 'public' => false ) ); + + $users = $this->factory->user->create_many( 3 ); + + $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); + $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); + + $q = new WP_User_Query( array( + 'has_published_posts' => true, + ) ); + + $found = wp_list_pluck( $q->get_results(), 'ID' ); + $expected = array( $users[0] ); + + $this->assertEqualSets( $expected, $found ); + } + + /** + * @ticket 32250 + */ + public function test_has_published_posts_should_obey_post_types() { + register_post_type( 'wptests_pt_public', array( 'public' => true ) ); + register_post_type( 'wptests_pt_private', array( 'public' => false ) ); + + $users = $this->factory->user->create_many( 3 ); + + $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'wptests_pt_public' ) ); + $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'wptests_pt_private' ) ); + $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); + + $q = new WP_User_Query( array( + 'has_published_posts' => array( 'wptests_pt_private', 'post' ), + ) ); + + $found = wp_list_pluck( $q->get_results(), 'ID' ); + $expected = array( $users[1], $users[2] ); + + $this->assertEqualSets( $expected, $found ); + } + + /** + * @ticket 32250 + */ + public function test_has_published_posts_should_ignore_non_published_posts() { + register_post_type( 'wptests_pt_public', array( 'public' => true ) ); + register_post_type( 'wptests_pt_private', array( 'public' => false ) ); + + $users = $this->factory->user->create_many( 3 ); + + $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'draft', 'post_type' => 'wptests_pt_public' ) ); + $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'inherit', 'post_type' => 'wptests_pt_private' ) ); + $this->factory->post->create( array( 'post_author' => $users[2], 'post_status' => 'publish', 'post_type' => 'post' ) ); + + $q = new WP_User_Query( array( + 'has_published_posts' => array( 'wptests_pt_public', 'wptests_pt_private', 'post' ), + ) ); + + $found = wp_list_pluck( $q->get_results(), 'ID' ); + $expected = array( $users[2] ); + + $this->assertEqualSets( $expected, $found ); + } + + /** + * @ticket 32250 + */ + public function test_has_published_posts_should_respect_blog_id() { + if ( ! is_multisite() ) { + $this->markTestSkipped( __METHOD__ . ' requires multisite.' ); + } + + $users = $this->factory->user->create_many( 3 ); + $blogs = $this->factory->blog->create_many( 2 ); + + add_user_to_blog( $blogs[0], $users[0], 'author' ); + add_user_to_blog( $blogs[0], $users[1], 'author' ); + add_user_to_blog( $blogs[1], $users[0], 'author' ); + add_user_to_blog( $blogs[1], $users[1], 'author' ); + + switch_to_blog( $blogs[0] ); + $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'post' ) ); + restore_current_blog(); + + switch_to_blog( $blogs[1] ); + $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'post' ) ); + restore_current_blog(); + + $q = new WP_User_Query( array( + 'has_published_posts' => array( 'post' ), + 'blog_id' => $blogs[1], + ) ); + + $found = wp_list_pluck( $q->get_results(), 'ID' ); + $expected = array( $users[1] ); + + $this->assertEqualSets( $expected, $found ); + } }