From a811171a0c5c006ffb5aa4997c6362942e14e0a1 Mon Sep 17 00:00:00 2001 From: scribu Date: Mon, 4 Oct 2010 18:57:13 +0000 Subject: [PATCH] Introduce WP_Object_Query. See #15032 git-svn-id: https://develop.svn.wordpress.org/trunk@15715 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/classes.php | 99 +++++++++++++++++++++++++++++++++++++++ wp-includes/functions.php | 56 ---------------------- wp-includes/query.php | 27 ++--------- wp-includes/user.php | 20 ++------ 4 files changed, 107 insertions(+), 95 deletions(-) diff --git a/wp-includes/classes.php b/wp-includes/classes.php index 6fa4e03ba2..3c499f5036 100644 --- a/wp-includes/classes.php +++ b/wp-includes/classes.php @@ -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. * diff --git a/wp-includes/functions.php b/wp-includes/functions.php index eb66547470..9a10e572d7 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -4256,62 +4256,6 @@ function _wp_search_sql($string, $cols) { 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 * diff --git a/wp-includes/query.php b/wp-includes/query.php index ed2e000ca1..075c7a0a32 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -641,7 +641,7 @@ function the_comment() { * * @since 1.5.0 */ -class WP_Query { +class WP_Query extends WP_Object_Query { /** * Query vars set by the user @@ -670,15 +670,6 @@ class WP_Query { */ 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. * @@ -1389,19 +1380,7 @@ class WP_Query { $this->is_tax = true; } - 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; - } + $this->parse_meta_query( $qv ); if ( empty($qv['author']) || ($qv['author'] == '0') ) { $this->is_author = false; @@ -2211,7 +2190,7 @@ class WP_Query { $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; $where .= $meta_where; diff --git a/wp-includes/user.php b/wp-includes/user.php index af0e42fccc..57d5ebb4f5 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -330,7 +330,7 @@ function delete_user_option( $user_id, $option_name, $global = false ) { * * @since 3.1.0 */ -class WP_User_Query { +class WP_User_Query extends WP_Object_Query { /** * 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->parse_meta_query( $qv ); + $role = trim( $qv['role'] ); $blog_id = absint( $qv['blog_id'] ); - $meta_queries = array(); - if ( $blog_id ) { $cap_meta_query = array(); $cap_meta_query['key'] = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; @@ -458,20 +458,10 @@ class WP_User_Query { $cap_meta_query['compare'] = 'like'; } - $meta_queries[] = $cap_meta_query; + $this->meta_query[] = $cap_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 ) ) { - $meta_queries[] = $meta_query; - } - - list( $meta_join, $meta_where ) = _wp_meta_sql( $meta_queries, $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' ); + list( $meta_join, $meta_where ) = $this->get_meta_sql( $wpdb->users, 'ID', $wpdb->usermeta, 'user_id' ); $this->query_from .= $meta_join; $this->query_where .= $meta_where;