Make WordPress Core

Ticket #23849: 23849.diff

File 23849.diff, 3.8 KB (added by jdgrimes, 11 years ago)
  • src/wp-includes/user.php

     
    666666                        }
    667667
    668668                        if ( empty( $qv['meta_query'] ) || ! in_array( $cap_meta_query, $qv['meta_query'], true ) ) {
    669                                 $qv['meta_query'][] = $cap_meta_query;
     669                                // The cap meta query should always be an AND relation, so if there
     670                                // is a main meta query and it is using OR relations, we need to
     671                                // handle the cap meta query separate.
     672                                if ( ! empty( $qv['meta_query']['relation'] ) && 'OR' == strtoupper( $qv['meta_query']['relation'] ) ) {
     673                                        $meta_query = new WP_Meta_Query();
     674                                        $meta_query->parse_query_vars( array( 'meta_query' => array( $cap_meta_query ) ) );
     675
     676                                        if ( ! empty( $meta_query->queries ) ) {
     677                                                $clauses = $meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
     678
     679                                                $this->query_from .= str_replace(
     680                                                        array( "$wpdb->usermeta ON",           "$wpdb->usermeta." ),
     681                                                        array( "$wpdb->usermeta AS cap_mt ON", 'cap_mt.'          ),
     682                                                        $clauses['join']
     683                                                );
     684                                                $this->query_where .= str_replace( "$wpdb->usermeta", 'cap_mt', $clauses['where'] );
     685                                        }
     686                                } else {
     687                                        $qv['meta_query'][] = $cap_meta_query;
     688                                }
    670689                        }
    671690                }
    672691
  • tests/phpunit/tests/user/query.php

     
    137137                $query->prepare_query();
    138138                $this->assertEquals( $_query_vars, $query->query_vars );
    139139        }
     140
     141        /**
     142         * Test 'NOT EXISTS' user meta query compare with OR relations.
     143         *
     144         * @ticket 23849
     145         */
     146        public function test_meta_query_compare_not_exists() {
     147
     148                // Set the 'test_meta_not_exists' key for just the first user.
     149                update_user_meta( $this->user_id, 'test_meta_not_exists', 1 );
     150
     151                // A meta query for the users with that key should return one user.
     152                $query_1 = new WP_User_Query(
     153                        array(
     154                                'meta_query' => array(
     155                                        array(
     156                                                'key' => 'test_meta_not_exists',
     157                                                'compare' => 'EXISTS',
     158                                        ),
     159                                ),
     160                        )
     161                );
     162
     163                $this->assertEquals( 1, $query_1->get_total() );
     164
     165                // Run the not exists query with AND relation.
     166                $query_2 = new WP_User_Query(
     167                        array(
     168                                'meta_query' => array(
     169                                        'relation' => 'AND',
     170                                        array(
     171                                                'key' => 'test_meta_not_exists',
     172                                                'compare' => 'NOT EXISTS',
     173                                        ),
     174                                ),
     175                        )
     176                );
     177
     178                // We should find one user (the current user which won't have the meta key).
     179                $this->assertEquals( 1, $query_2->get_total() );
     180
     181                // Now run the not exists query with OR relation.
     182                $query_3 = new WP_User_Query(
     183                        array(
     184                                'meta_query' => array(
     185                                        'relation' => 'OR',
     186                                        array(
     187                                                'key' => 'test_meta_not_exists',
     188                                                'compare' => 'NOT EXISTS',
     189                                        ),
     190                                ),
     191                        )
     192                );
     193
     194                // We should find one user.
     195                $this->assertEquals( 1, $query_3->get_total() );
     196        }
     197
     198        /**
     199         * Test using user meta query when specifying a role.
     200         *
     201         * @ticket 23849
     202         */
     203        public function test_meta_query_with_role() {
     204
     205                $user_id = $this->factory->user->create( array( 'role' => 'author' ) );
     206
     207                update_user_meta( $user_id, 'test_meta_not_exists', 'off' );
     208
     209                $args = array(
     210                        'role'          => 'Author',
     211                        'number'        => 100,
     212                        'offset'        => 0,
     213                        'meta_query' => array(
     214                                'relation' => 'OR',
     215                                array(
     216                                        'key'           => 'test_meta_not_exists',
     217                                        'compare'       => 'NOT EXISTS',
     218                                ),
     219                                array(
     220                                        'key'           => 'test_meta_not_exists',
     221                                        'value'         => 'off',
     222                                        'compare'       => '!=',
     223                                ),
     224                        ),
     225                );
     226
     227                $this->assertEquals( 1, ( new WP_User_Query( $args ) )->get_total() );
     228
     229                update_user_meta( $user_id, 'test_meta_not_exists', 'on' );
     230
     231                $this->assertEquals( 2, ( new WP_User_Query( $args ) )->get_total() );
     232        }
    140233}