Make WordPress Core

Ticket #32019: 32019.2.patch

File 32019.2.patch, 3.3 KB (added by boonebgorges, 10 years ago)
  • src/wp-includes/user.php

    diff --git src/wp-includes/user.php src/wp-includes/user.php
    index 9db8521..e602a3e 100644
    class WP_User_Query { 
    626626                        $include = false;
    627627                }
    628628
    629                 // Meta query.
    630                 $this->meta_query = new WP_Meta_Query();
    631                 $this->meta_query->parse_query_vars( $qv );
    632 
    633629                $blog_id = 0;
    634630                if ( isset( $qv['blog_id'] ) ) {
    635631                        $blog_id = absint( $qv['blog_id'] );
    636632                }
    637633
     634                if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
     635                        $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
     636                        $qv['meta_value'] = 0;
     637                        $qv['meta_compare'] = '!=';
     638                        $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
     639                }
     640
     641                // Meta query.
     642                $this->meta_query = new WP_Meta_Query();
     643                $this->meta_query->parse_query_vars( $qv );
     644
    638645                $role = '';
    639646                if ( isset( $qv['role'] ) ) {
    640647                        $role = trim( $qv['role'] );
    class WP_User_Query { 
    775782                        $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild );
    776783                }
    777784
    778                 if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
    779                         $qv['meta_key'] = $wpdb->get_blog_prefix( $blog_id ) . 'user_level';
    780                         $qv['meta_value'] = 0;
    781                         $qv['meta_compare'] = '!=';
    782                         $qv['blog_id'] = $blog_id = 0; // Prevent extra meta query
    783                 }
    784 
    785785                if ( ! empty( $include ) ) {
    786786                        // Sanitized earlier.
    787787                        $ids = implode( ',', $include );
  • tests/phpunit/tests/user/query.php

    diff --git tests/phpunit/tests/user/query.php tests/phpunit/tests/user/query.php
    index f208f56..9abf99d 100644
    class Tests_User_Query extends WP_UnitTestCase { 
    625625                $this->assertSame( array( 'author' ), $user->roles );
    626626                $this->assertSame( array( 'author' => true ), $user->caps );
    627627        }
     628
     629        /**
     630         * @ticket 32019
     631         */
     632        public function test_who_authors() {
     633                if ( ! is_multisite() ) {
     634                        $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
     635                }
     636
     637                $b = $this->factory->blog->create();
     638                $users = $this->factory->user->create_many( 3 );
     639
     640                add_user_to_blog( $b, $users[0], 'subscriber' );
     641                add_user_to_blog( $b, $users[1], 'author' );
     642                add_user_to_blog( $b, $users[2], 'editor' );
     643
     644                $q = new WP_User_Query( array(
     645                        'who' => 'authors',
     646                        'blog_id' => $b,
     647                ) );
     648
     649                $found = wp_list_pluck( $q->get_results(), 'ID' );
     650
     651                $this->assertNotContains( $users[0], $found );
     652                $this->assertContains( $users[1], $found );
     653                $this->assertContains( $users[2], $found );
     654        }
     655
     656        /**
     657         * @ticket 32019
     658         */
     659        public function test_who_authors_should_work_alongside_meta_query() {
     660                if ( ! is_multisite() ) {
     661                        $this->markTestSkipped( __METHOD__ . ' requires multisite.' );
     662                }
     663
     664                $b = $this->factory->blog->create();
     665                $users = $this->factory->user->create_many( 3 );
     666
     667                add_user_to_blog( $b, $users[0], 'subscriber' );
     668                add_user_to_blog( $b, $users[1], 'author' );
     669                add_user_to_blog( $b, $users[2], 'editor' );
     670
     671                add_user_meta( $users[1], 'foo', 'bar' );
     672                add_user_meta( $users[2], 'foo', 'baz' );
     673
     674                $q = new WP_User_Query( array(
     675                        'who' => 'authors',
     676                        'blog_id' => $b,
     677                        'meta_query' => array(
     678                                'key' => 'foo',
     679                                'value' => 'bar',
     680                        ),
     681                ) );
     682
     683                $found = wp_list_pluck( $q->get_results(), 'ID' );
     684
     685                $this->assertNotContains( $users[0], $found );
     686                $this->assertContains( $users[1], $found );
     687                $this->assertNotContains( $users[2], $found );
     688        }
    628689}