Make WordPress Core


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

Make get_meta_sql() a standalone function. See #15032

File:
1 edited

Legend:

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

    r16017 r16266  
    352352}
    353353
     354/*
     355 * Given a meta query, generates SQL clauses to be appended to a main query
     356 *
     357 * @since 3.1.0
     358 *
     359 * @param array $meta_query List of metadata queries. A single query is an associative array:
     360 * - 'key' string The meta key
     361 * - 'value' string|array The meta value
     362 * - 'compare' (optional) string How to compare the key to the value.
     363 *      Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
     364 *      Default: '='
     365 * - 'type' string (optional) The type of the value.
     366 *      Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
     367 *      Default: 'CHAR'
     368 *
     369 * @param string $meta_type
     370 * @param string $primary_table
     371 * @param string $primary_id_column
     372 * @return array( 'join' => $join_sql, 'where' => $where_sql )
     373 */
     374function get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column ) {
     375    global $wpdb;
     376
     377    if ( ! $meta_table = _get_meta_table( $meta_type ) )
     378        return false;
     379
     380    $meta_id_column = esc_sql( $meta_type . '_id' );
     381
     382    $clauses = array();
     383
     384    $join = '';
     385    $where = '';
     386    $i = 0;
     387    foreach ( $meta_query as $q ) {
     388        $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
     389        $meta_value = isset( $q['value'] ) ? $q['value'] : '';
     390        $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
     391        $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
     392
     393        if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
     394            $meta_compare = '=';
     395
     396        if ( 'NUMERIC' == $meta_type )
     397            $meta_type = 'SIGNED';
     398        elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
     399            $meta_type = 'CHAR';
     400
     401        if ( empty( $meta_key ) && empty( $meta_value ) )
     402            continue;
     403
     404        $alias = $i ? 'mt' . $i : $meta_table;
     405
     406        $join .= "\nINNER JOIN $meta_table";
     407        $join .= $i ? " AS $alias" : '';
     408        $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
     409
     410        $i++;
     411
     412        if ( !empty( $meta_key ) )
     413            $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
     414
     415        if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
     416            if ( ! is_array( $meta_value ) )
     417                $meta_value = preg_split( '/[,\s]+/', $meta_value );
     418        } else {
     419            $meta_value = trim( $meta_value );
     420        }
     421
     422        if ( empty( $meta_value ) )
     423            continue;
     424
     425        if ( 'IN' == substr( $meta_compare, -2) ) {
     426            $meta_field_types = substr( str_repeat( ',%s', count( $meta_value ) ), 1 );
     427            $meta_compare_string = "($meta_field_types)";
     428            unset( $meta_field_types );
     429        } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
     430            $meta_value = array_slice( $meta_value, 0, 2 );
     431            $meta_compare_string = '%s AND %s';
     432        } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
     433            $meta_value = '%' . like_escape( $meta_value ) . '%';
     434            $meta_compare_string = '%s';
     435        } else {
     436            $meta_compare_string = '%s';
     437        }
     438        $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
     439
     440        unset( $meta_compare_string );
     441    }
     442
     443    return apply_filters( 'get_meta_sql', compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column );
     444}
     445
    354446/**
    355447 * Retrieve the name of the metadata table for the specified object type.
Note: See TracChangeset for help on using the changeset viewer.