Introduce WP_Object_Query. See #15032

git-svn-id: http://svn.automattic.com/wordpress/trunk@15715 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
scribu 2010-10-04 18:57:13 +00:00
parent 1190c4094b
commit f558d12bf0
4 changed files with 107 additions and 95 deletions

View File

@ -524,6 +524,105 @@ class WP {
} }
} }
/**
* WordPress Query class.
*
* Abstract class for handling advanced queries
*
* @package WordPress
* @since 3.1.0
*/
class WP_Object_Query {
/**
* Metadata query
*
* @since 3.1.0
* @access public
* @var array
*/
var $meta_query = array();
/*
* Populates the $meta_query property
*
* @access private
* @since 3.1.0
*
* @param array $qv The query variables
*/
function parse_meta_query( $qv ) {
if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
$this->meta_query = $qv['meta_query'];
}
$meta_query = array();
foreach ( array( 'key', 'value', 'compare' ) as $key ) {
if ( !empty( $qv[ "meta_$key" ] ) )
$meta_query[ $key ] = $qv[ "meta_$key" ];
}
if ( !empty( $meta_query ) ) {
$this->meta_query[] = $meta_query;
}
}
/*
* Used internally to generate an SQL string for searching across multiple meta key = value pairs
*
* @access private
* @since 3.1.0
*
* @param string $primary_table
* @param string $primary_id_column
* @param string $meta_table
* @param string $meta_id_column
* @return array( $join_sql, $where_sql )
*/
function get_meta_sql( $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {
global $wpdb;
$clauses = array();
$join = '';
$where = '';
$i = 0;
foreach ( $this->meta_query as $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_value = isset( $q['value'] ) ? trim( $q['value'] ) : '';
$meta_compare = isset( $q['compare'] ) ? $q['compare'] : '=';
if ( !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'like' ) ) )
$meta_compare = '=';
if ( empty( $meta_key ) && empty( $meta_value ) )
continue;
$alias = $i ? 'mt' . $i : $meta_table;
$join .= "\nINNER JOIN $meta_table";
$join .= $i ? " AS $alias" : '';
$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
$i++;
if ( !empty( $meta_key ) )
$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
if ( empty( $meta_value ) )
continue;
if ( 'like' == $meta_compare ) {
$where .= $wpdb->prepare( " AND $alias.meta_value LIKE %s", '%' . like_escape( $meta_value ) . '%' );
} else {
$where .= $wpdb->prepare( " AND $alias.meta_value $meta_compare %s", $meta_value );
}
}
return array( $join, $where );
}
}
/** /**
* WordPress Error class. * WordPress Error class.
* *

View File

@ -4256,62 +4256,6 @@ function _wp_search_sql($string, $cols) {
return ' AND (' . implode(' OR ', $searches) . ')'; return ' AND (' . implode(' OR ', $searches) . ')';
} }
/*
* Used internally to generate an SQL string for searching across multiple meta key = value pairs
*
* @access private
* @since 3.1.0
*
* @param array $queries An array of queries
* @param string $primary_table
* @param string $primary_id_column
* @param string $meta_table
* @param string $meta_id_column
* @return array( $join_sql, $where_sql )
*/
function _wp_meta_sql( $queries, $primary_table, $primary_id_column, $meta_table, $meta_id_column ) {
global $wpdb;
$clauses = array();
$join = '';
$where = '';
$i = 0;
foreach ( $queries as $q ) {
$meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
$meta_value = isset( $q['value'] ) ? trim( $q['value'] ) : '';
$meta_compare = isset( $q['compare'] ) ? $q['compare'] : '=';
if ( !in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'like' ) ) )
$meta_compare = '=';
if ( empty( $meta_key ) && empty( $meta_value ) )
continue;
$alias = $i ? 'mt' . $i : $meta_table;
$join .= "\nINNER JOIN $meta_table";
$join .= $i ? " AS $alias" : '';
$join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
$i++;
if ( !empty( $meta_key ) )
$where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
if ( empty( $meta_value ) )
continue;
if ( 'like' == $meta_compare ) {
$where .= $wpdb->prepare( " AND $alias.meta_value LIKE %s", '%' . like_escape( $meta_value ) . '%' );
} else {
$where .= $wpdb->prepare( " AND $alias.meta_value $meta_compare %s", $meta_value );
}
}
return array( $join, $where );
}
/* /*
* Used internally to tidy up the search terms * Used internally to tidy up the search terms
* *

View File

@ -641,7 +641,7 @@ function the_comment() {
* *
* @since 1.5.0 * @since 1.5.0
*/ */
class WP_Query { class WP_Query extends WP_Object_Query {
/** /**
* Query vars set by the user * Query vars set by the user
@ -670,15 +670,6 @@ class WP_Query {
*/ */
var $tax_query = array(); var $tax_query = array();
/**
* Metadata query
*
* @since 3.1.0
* @access public
* @var array
*/
var $meta_query = array();
/** /**
* Holds the data for a single object that is queried. * Holds the data for a single object that is queried.
* *
@ -1389,19 +1380,7 @@ class WP_Query {
$this->is_tax = true; $this->is_tax = true;
} }
if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) { $this->parse_meta_query( $qv );
$this->meta_query = $qv['meta_query'];
}
$meta_query = array();
foreach ( array( 'key', 'value', 'compare' ) as $key ) {
if ( !empty( $qv[ "meta_$key" ] ) )
$meta_query[ $key ] = $qv[ "meta_$key" ];
}
if ( !empty( $meta_query ) ) {
$this->meta_query[] = $meta_query;
}
if ( empty($qv['author']) || ($qv['author'] == '0') ) { if ( empty($qv['author']) || ($qv['author'] == '0') ) {
$this->is_author = false; $this->is_author = false;
@ -2211,7 +2190,7 @@ class WP_Query {
$where .= ')'; $where .= ')';
} }
list( $meta_join, $meta_where ) = _wp_meta_sql( $this->meta_query, $wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' ); list( $meta_join, $meta_where ) = $this->get_meta_sql( $wpdb->posts, 'ID', $wpdb->postmeta, 'post_id' );
$join .= $meta_join; $join .= $meta_join;
$where .= $meta_where; $where .= $meta_where;

View File

@ -330,7 +330,7 @@ function delete_user_option( $user_id, $option_name, $global = false ) {
* *
* @since 3.1.0 * @since 3.1.0
*/ */
class WP_User_Query { class WP_User_Query extends WP_Object_Query {
/** /**
* List of found user ids * List of found user ids
@ -444,11 +444,11 @@ class WP_User_Query {
$this->query_where .= _wp_search_sql( $search, array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') ); $this->query_where .= _wp_search_sql( $search, array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') );
} }
$this->parse_meta_query( $qv );
$role = trim( $qv['role'] ); $role = trim( $qv['role'] );
$blog_id = absint( $qv['blog_id'] ); $blog_id = absint( $qv['blog_id'] );
$meta_queries = array();
if ( $blog_id ) { if ( $blog_id ) {
$cap_meta_query = array(); $cap_meta_query = array();
$cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
@ -458,20 +458,10 @@ class WP_User_Query {
$cap_meta_query['compare'] = 'like'; $cap_meta_query['compare'] = 'like';
} }
$meta_queries[] = $cap_meta_query; $this->meta_query[] = $cap_meta_query;
} }
$meta_query = array(); list( $meta_join, $meta_where ) = $this->get_meta_sql( $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' );
foreach ( array( 'key', 'value', 'compare' ) as $key ) {
if ( !empty( $qv[ "meta_$key" ] ) )
$meta_query[ $key ] = $qv[ "meta_$key" ];
}
if ( !empty( $meta_query ) ) {
$meta_queries[] = $meta_query;
}
list( $meta_join, $meta_where ) = _wp_meta_sql( $meta_queries, $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' );
$this->query_from .= $meta_join; $this->query_from .= $meta_join;
$this->query_where .= $meta_where; $this->query_where .= $meta_where;