Make WordPress Core


Ignore:
Timestamp:
11/09/2010 11:22:13 PM (16 years ago)
Author:
scribu
Message:

Make get_meta_sql() a standalone function. See #15032

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-object-query.php

    r16170 r16266  
    7070
    7171                $qv['meta_query'] = $meta_query;
    72         }
    73 
    74         /*
    75          * Used internally to generate an SQL string for searching across multiple meta key = value pairs
    76          *
    77          * @access protected
    78          * @since 3.1.0
    79          *
    80          * @param array $meta_query List of metadata queries. A single query is an associative array:
    81          * - 'key' string The meta key
    82          * - 'value' string|array The meta value
    83          * - 'compare' (optional) string How to compare the key to the value.
    84          *              Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
    85          *              Default: '='
    86          * - 'type' string (optional) The type of the value.
    87          *              Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
    88          *              Default: 'CHAR'
    89          *
    90          * @param string $meta_type
    91          * @param string $primary_table
    92          * @param string $primary_id_column
    93          * @return array( $join_sql, $where_sql )
    94          */
    95         function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column ) {
    96                 global $wpdb;
    97 
    98                 if ( ! $meta_table = _get_meta_table( $meta_type ) )
    99                         return false;
    100 
    101                 $meta_id_column = esc_sql( $meta_type . '_id' );
    102 
    103                 $clauses = array();
    104 
    105                 $join = '';
    106                 $where = '';
    107                 $i = 0;
    108                 foreach ( $meta_query as $q ) {
    109                         $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
    110                         $meta_value = isset( $q['value'] ) ? $q['value'] : '';
    111                         $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
    112                         $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
    113 
    114                         if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
    115                                 $meta_compare = '=';
    116 
    117                         if ( 'NUMERIC' == $meta_type )
    118                                 $meta_type = 'SIGNED';
    119                         elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
    120                                 $meta_type = 'CHAR';
    121 
    122                         if ( empty( $meta_key ) && empty( $meta_value ) )
    123                                 continue;
    124 
    125                         $alias = $i ? 'mt' . $i : $meta_table;
    126 
    127                         $join .= "\nINNER JOIN $meta_table";
    128                         $join .= $i ? " AS $alias" : '';
    129                         $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
    130 
    131                         $i++;
    132 
    133                         if ( !empty( $meta_key ) )
    134                                 $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
    135 
    136                         if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
    137                                 if ( ! is_array( $meta_value ) )
    138                                         $meta_value = preg_split( '/[,\s]+/', $meta_value );
    139                         } else {
    140                                 $meta_value = trim( $meta_value );
    141                         }
    142 
    143                         if ( empty( $meta_value ) )
    144                                 continue;
    145 
    146                         if ( 'IN' == substr( $meta_compare, -2) ) {
    147                                 $meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 );
    148                                 $meta_compare_string = "($meta_field_types)";
    149                                 unset( $meta_field_types );
    150                         } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
    151                                 $meta_value = array_slice( $meta_value, 0, 2 );
    152                                 $meta_compare_string = '%s AND %s';
    153                         } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
    154                                 $meta_value = '%' . like_escape( $meta_value ) . '%';
    155                                 $meta_compare_string = '%s';
    156                         } else {
    157                                 $meta_compare_string = '%s';
    158                         }
    159                         $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
    160 
    161                         unset( $meta_compare_string );
    162                 }
    163 
    164                 return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column );
    16572        }
    16673
Note: See TracChangeset for help on using the changeset viewer.