WordPress.org

Make WordPress Core

Changeset 15565


Ignore:
Timestamp:
09/05/2010 01:31:33 PM (9 years ago)
Author:
scribu
Message:

introduce _wp_meta_sql(). Preparation for adding blog_id to WP_User_Query. See #14572

Location:
trunk/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r15496 r15565  
    42204220
    42214221/*
     4222 * Used internally to generate an SQL string for searching across multiple meta key = value pairs
     4223 *
     4224 * @access private
     4225 * @since 3.1.0
     4226 *
     4227 * @param array $queries An array of queries
     4228 * @return string
     4229 */
     4230function _wp_meta_sql( $queries, $meta_id_column, $table ) {
     4231    global $wpdb;
     4232
     4233    $clauses = array();
     4234
     4235    foreach ( $queries as $query ) {
     4236        $meta_key = trim( @$query['meta_key'] );
     4237        $meta_value = trim( @$query['meta_value'] );
     4238        $meta_compare = @$query['meta_compare'];
     4239
     4240        if ( empty( $meta_compare ) || !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'like' ) ) )
     4241            $meta_compare = '=';
     4242
     4243        if ( empty( $meta_key ) )
     4244            continue;
     4245
     4246        $clause = $wpdb->prepare( "WHEN %s THEN meta_value ", $meta_key );
     4247
     4248        if ( empty( $meta_value ) ) {
     4249            $clauses[] = $clause . "IS NOT NULL";
     4250        } elseif ( 'like' == $meta_compare ) {
     4251            $clauses[] = $clause . $wpdb->prepare( "LIKE %s", '%' . like_escape( $meta_value ) . '%' );
     4252        } else {
     4253            $clauses[] = $clause . $wpdb->prepare( "$meta_compare %s", $meta_value );
     4254        }
     4255    }
     4256
     4257    if ( empty( $clauses ) )
     4258        return '';
     4259
     4260    return "
     4261        SELECT $meta_id_column
     4262        FROM $table
     4263        WHERE CASE meta_key
     4264        " . implode( "\n", $clauses ) . "
     4265        END
     4266        GROUP BY $meta_id_column
     4267        HAVING COUNT(*) = " . count( $clauses );
     4268}
     4269
     4270
     4271/*
    42224272 * Used internally to tidy up the search terms
    42234273 *
  • trunk/wp-includes/user.php

    r15543 r15565  
    380380                'meta_key' => '', 'meta_value' => '',
    381381                'include' => array(), 'exclude' => array(),
    382                 'fields' => 'all'
     382                'fields' => 'all',
    383383            ) );
    384384
     
    446446        $role = trim( $qv['role'] );
    447447
    448         if ( $role || is_multisite() ) {
    449             $this->query_from .= " INNER JOIN $wpdb->usermeta ON $wpdb->users.ID = $wpdb->usermeta.user_id";
    450             $this->query_where .= $wpdb->prepare( " AND $wpdb->usermeta.meta_key = %s", $wpdb->prefix . 'capabilities' );
    451         }
     448        $meta_queries = array();
     449
     450        $cap_meta_query = array();
     451        $cap_meta_key = $wpdb->prefix . 'capabilities';
     452
     453        if ( $role || is_multisite() )
     454            $cap_meta_query['meta_key'] = $cap_meta_key;
    452455
    453456        if ( $role ) {
    454             $this->query_where .= $wpdb->prepare( " AND $wpdb->usermeta.meta_value LIKE %s", '%' . like_escape( $role ) . '%' );
    455         }
    456 
    457         $meta_key = trim( $qv['meta_key'] );
    458         $meta_value = trim( $qv['meta_value'] );
    459         if ( $meta_key ) {
    460             if ( empty( $meta_value ) ) {
    461                 $subquery = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s", $meta_key );
    462             }
    463             else {
    464                 $subquery = $wpdb->prepare( "SELECT user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value );
    465             }
    466 
    467             $this->query_where .= " AND $wpdb->users.ID IN ($subquery)";
     457            $cap_meta_query['meta_value'] = $role;
     458            $cap_meta_query['meta_compare'] = 'like';
     459        }
     460
     461        $meta_queries[] = $cap_meta_query;
     462
     463        $meta_queries[] = array(
     464            'meta_key' => @$qv['meta_key'],
     465            'meta_value' => @$qv['meta_key'],
     466            'meta_compare' => @$qv['meta_key'],
     467        );
     468
     469        $meta_query_sql = _wp_meta_sql( $meta_queries, 'user_id', $wpdb->usermeta );
     470
     471        if ( !empty( $meta_query_sql ) ) {
     472            $this->query_where .= " AND $wpdb->users.ID IN ($meta_query_sql)";
    468473        }
    469474
Note: See TracChangeset for help on using the changeset viewer.