Ticket #32250: 32250-2.diff
| File 32250-2.diff, 4.4 KB (added by , 11 years ago) |
|---|
-
src/wp-includes/user.php
diff --git src/wp-includes/user.php src/wp-includes/user.php index e602a3e..4e0f385 100644
class WP_User_Query { 555 555 * of fields. Accepts 'ID', 'display_name', 'login', 'nicename', 'email', 556 556 * 'url', 'registered'. Use 'all' for all fields and 'all_with_meta' to 557 557 * include meta fields. Default 'all'. 558 * @type string $who Type of users to query. Accepts 'authors'. Default empty (all users). 558 * @type string $who Type of users to query. Accepts 'authors', 'public_author'. Default empty (all users). 559 * @type bool|array $has_published_posts If the user has published posts in the specified post type(s). Accepts `true` 560 * to indicate all public post types; `false` to indicate the user has published in 561 * non-public post types; or `array( $post_type )` for users who have published posts 562 * in the specified post types. 559 563 * } 560 564 */ 561 565 public function prepare_query( $query = array() ) { … … class WP_User_Query { 638 642 $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query 639 643 } 640 644 645 if ( isset( $qv['has_published_posts'] ) && $blog_id ) { 646 647 if ( true === $qv['has_published_posts'] ) { 648 $post_types = get_post_types( array( 'public' => true ) ); 649 $operator = 'IN'; 650 } else if ( false === $qv['has_published_posts'] ) { 651 $post_types = get_post_types( array( 'public' => true ) ); 652 $operator = 'NOT IN'; 653 } else if ( is_array( $qv['has_published_posts'] ) ) { 654 $post_types = $qv['has_published_posts']; 655 $operator = 'IN'; 656 } 657 658 $this->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT $wpdb->posts.post_author FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type " . $operator . " ( '" . join( "', '", $post_types ) . "' ) )"; 659 } 660 641 661 // Meta query. 642 662 $this->meta_query = new WP_Meta_Query(); 643 663 $this->meta_query->parse_query_vars( $qv ); -
tests/phpunit/tests/user/query.php
diff --git tests/phpunit/tests/user/query.php tests/phpunit/tests/user/query.php index 9abf99d..11d5d8c 100644
class Tests_User_Query extends WP_UnitTestCase { 686 686 $this->assertContains( $users[1], $found ); 687 687 $this->assertNotContains( $users[2], $found ); 688 688 } 689 690 public function test_has_published_posts() { 691 692 $users = $this->factory->user->create_many( 3 ); 693 694 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'post' ) ); 695 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'post' ) ); 696 697 $q = new WP_User_Query( array( 698 'has_published_posts' => true, 699 ) ); 700 701 $found = wp_list_pluck( $q->get_results(), 'ID' ); 702 703 $this->assertEquals( 2, $q->get_total() ); 704 705 $this->assertContains( $users[0], $found ); 706 $this->assertContains( $users[1], $found ); 707 } 708 709 public function test_has_published_posts_in_non_public_post_types() { 710 711 $users = $this->factory->user->create_many( 3 ); 712 713 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'revision' ) ); 714 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'revision' ) ); 715 716 $q = new WP_User_Query( array( 717 'has_published_posts' => false, 718 ) ); 719 720 $found = wp_list_pluck( $q->get_results(), 'ID' ); 721 722 $this->assertEquals( 2, $q->get_total() ); 723 724 $this->assertContains( $users[0], $found ); 725 $this->assertContains( $users[1], $found ); 726 } 727 728 public function test_has_published_posts_in_custom_post_types() { 729 730 $users = $this->factory->user->create_many( 3 ); 731 732 $this->factory->post->create( array( 'post_author' => $users[0], 'post_status' => 'publish', 'post_type' => 'post' ) ); 733 $this->factory->post->create( array( 'post_author' => $users[1], 'post_status' => 'publish', 'post_type' => 'revision' ) ); 734 735 $q = new WP_User_Query( array( 736 'has_published_posts' => array( 'post' ), 737 ) ); 738 739 $found = wp_list_pluck( $q->get_results(), 'ID' ); 740 741 $this->assertEquals( 1, $q->get_total() ); 742 743 $this->assertContains( $users[0], $found ); 744 } 689 745 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)