From c0be4fafcffac6df760bb3f3804e2ca98ebe47ed Mon Sep 17 00:00:00 2001 From: scribu Date: Tue, 9 Nov 2010 23:22:13 +0000 Subject: [PATCH] Make get_meta_sql() a standalone function. See #15032 git-svn-id: https://develop.svn.wordpress.org/trunk@16266 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-includes/class-wp-object-query.php | 93 --------------------------- wp-includes/meta.php | 92 ++++++++++++++++++++++++++ wp-includes/query.php | 2 +- wp-includes/user.php | 2 +- 4 files changed, 94 insertions(+), 95 deletions(-) diff --git a/wp-includes/class-wp-object-query.php b/wp-includes/class-wp-object-query.php index 7798cd00b8..25691445f2 100644 --- a/wp-includes/class-wp-object-query.php +++ b/wp-includes/class-wp-object-query.php @@ -71,99 +71,6 @@ class WP_Object_Query { $qv['meta_query'] = $meta_query; } - /* - * Used internally to generate an SQL string for searching across multiple meta key = value pairs - * - * @access protected - * @since 3.1.0 - * - * @param array $meta_query List of metadata queries. A single query is an associative array: - * - 'key' string The meta key - * - 'value' string|array The meta value - * - 'compare' (optional) string How to compare the key to the value. - * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. - * Default: '=' - * - 'type' string (optional) The type of the value. - * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. - * Default: 'CHAR' - * - * @param string $meta_type - * @param string $primary_table - * @param string $primary_id_column - * @return array( $join_sql, $where_sql ) - */ - function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column ) { - global $wpdb; - - if ( ! $meta_table = _get_meta_table( $meta_type ) ) - return false; - - $meta_id_column = esc_sql( $meta_type . '_id' ); - - $clauses = array(); - - $join = ''; - $where = ''; - $i = 0; - foreach ( $meta_query as $q ) { - $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; - $meta_value = isset( $q['value'] ) ? $q['value'] : ''; - $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '='; - $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR'; - - if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) - $meta_compare = '='; - - if ( 'NUMERIC' == $meta_type ) - $meta_type = 'SIGNED'; - elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) - $meta_type = 'CHAR'; - - 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 ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { - if ( ! is_array( $meta_value ) ) - $meta_value = preg_split( '/[,\s]+/', $meta_value ); - } else { - $meta_value = trim( $meta_value ); - } - - if ( empty( $meta_value ) ) - continue; - - if ( 'IN' == substr( $meta_compare, -2) ) { - $meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 ); - $meta_compare_string = "($meta_field_types)"; - unset( $meta_field_types ); - } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) { - $meta_value = array_slice( $meta_value, 0, 2 ); - $meta_compare_string = '%s AND %s'; - } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) { - $meta_value = '%' . like_escape( $meta_value ) . '%'; - $meta_compare_string = '%s'; - } else { - $meta_compare_string = '%s'; - } - $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value ); - - unset( $meta_compare_string ); - } - - return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column ); - } - /* * Used internally to generate an SQL string for searching across multiple taxonomies * diff --git a/wp-includes/meta.php b/wp-includes/meta.php index f6c7b31ed4..4e9123f583 100644 --- a/wp-includes/meta.php +++ b/wp-includes/meta.php @@ -351,6 +351,98 @@ function update_meta_cache($meta_type, $object_ids) { return $cache; } +/* + * Given a meta query, generates SQL clauses to be appended to a main query + * + * @since 3.1.0 + * + * @param array $meta_query List of metadata queries. A single query is an associative array: + * - 'key' string The meta key + * - 'value' string|array The meta value + * - 'compare' (optional) string How to compare the key to the value. + * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. + * Default: '=' + * - 'type' string (optional) The type of the value. + * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. + * Default: 'CHAR' + * + * @param string $meta_type + * @param string $primary_table + * @param string $primary_id_column + * @return array( 'join' => $join_sql, 'where' => $where_sql ) + */ +function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column ) { + global $wpdb; + + if ( ! $meta_table = _get_meta_table( $meta_type ) ) + return false; + + $meta_id_column = esc_sql( $meta_type . '_id' ); + + $clauses = array(); + + $join = ''; + $where = ''; + $i = 0; + foreach ( $meta_query as $q ) { + $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; + $meta_value = isset( $q['value'] ) ? $q['value'] : ''; + $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '='; + $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR'; + + if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) + $meta_compare = '='; + + if ( 'NUMERIC' == $meta_type ) + $meta_type = 'SIGNED'; + elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) + $meta_type = 'CHAR'; + + 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 ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { + if ( ! is_array( $meta_value ) ) + $meta_value = preg_split( '/[,\s]+/', $meta_value ); + } else { + $meta_value = trim( $meta_value ); + } + + if ( empty( $meta_value ) ) + continue; + + if ( 'IN' == substr( $meta_compare, -2) ) { + $meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 ); + $meta_compare_string = "($meta_field_types)"; + unset( $meta_field_types ); + } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) { + $meta_value = array_slice( $meta_value, 0, 2 ); + $meta_compare_string = '%s AND %s'; + } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) { + $meta_value = '%' . like_escape( $meta_value ) . '%'; + $meta_compare_string = '%s'; + } else { + $meta_compare_string = '%s'; + } + $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value ); + + unset( $meta_compare_string ); + } + + return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column ); +} + /** * Retrieve the name of the metadata table for the specified object type. * diff --git a/wp-includes/query.php b/wp-includes/query.php index 57b68e4386..cc7c2379ad 100644 --- a/wp-includes/query.php +++ b/wp-includes/query.php @@ -2147,7 +2147,7 @@ class WP_Query extends WP_Object_Query { } if ( !empty( $q['meta_query'] ) ) { - $clauses = $this->get_meta_sql( $q['meta_query'], 'post', $wpdb->posts, 'ID' ); + $clauses = get_meta_sql( $q['meta_query'], 'post', $wpdb->posts, 'ID' ); $join .= $clauses['join']; $where .= $clauses['where']; } diff --git a/wp-includes/user.php b/wp-includes/user.php index 0874f430ef..b4c50cd1b6 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -481,7 +481,7 @@ class WP_User_Query extends WP_Object_Query { } if ( !empty( $qv['meta_query'] ) ) { - $clauses = $this->get_meta_sql( $qv['meta_query'], 'user', $wpdb->users, 'ID' ); + $clauses = get_meta_sql( $qv['meta_query'], 'user', $wpdb->users, 'ID' ); $this->query_from .= $clauses['join']; $this->query_where .= $clauses['where']; }