Make WordPress Core

Changeset 32207


Ignore:
Timestamp:
04/20/2015 03:15:14 PM (10 years ago)
Author:
boonebgorges
Message:

Ensure that 'who' param is respected when generating meta_query in WP_User_Query.

Since [31669], the 'who' param had been parsed after meta_query was generated,
so that 'who' was effectively ignored.

Props imath.
Fixes #32019.

Location:
trunk
Files:
2 edited

Legend:

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

    r32116 r32207  
    627627        }
    628628
     629        $blog_id = 0;
     630        if ( isset( $qv['blog_id'] ) ) {
     631            $blog_id = absint( $qv['blog_id'] );
     632        }
     633
     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
    629641        // Meta query.
    630642        $this->meta_query = new WP_Meta_Query();
    631643        $this->meta_query->parse_query_vars( $qv );
    632 
    633         $blog_id = 0;
    634         if ( isset( $qv['blog_id'] ) ) {
    635             $blog_id = absint( $qv['blog_id'] );
    636         }
    637644
    638645        $role = '';
     
    774781
    775782            $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild );
    776         }
    777 
    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
    783783        }
    784784
  • trunk/tests/phpunit/tests/user/query.php

    r32001 r32207  
    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}
Note: See TracChangeset for help on using the changeset viewer.