Make WordPress Core


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.0/wp-includes/meta.php

    r17170 r15390  
    4545        // expected_slashed ($meta_key)
    4646        $meta_key = stripslashes($meta_key);
    47         $meta_value = stripslashes_deep($meta_value);
    48 
    49         $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
    50         if ( null !== $check )
    51                 return (bool) $check;
    5247
    5348        if ( $unique && $wpdb->get_var( $wpdb->prepare(
    … …  
    5752
    5853        $_meta_value = $meta_value;
    59         $meta_value = maybe_serialize( $meta_value );
    60 
    61         do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
     54        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
    6255
    6356        $wpdb->insert( $table, array(
    … …  
    113106        // expected_slashed ($meta_key)
    114107        $meta_key = stripslashes($meta_key);
    115         $meta_value = stripslashes_deep($meta_value);
    116 
    117         $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
    118         if ( null !== $check )
    119                 return (bool) $check;
    120108
    121109        if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
    … …  
    132120
    133121        $_meta_value = $meta_value;
    134         $meta_value = maybe_serialize( $meta_value );
     122        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
    135123
    136124        $data  = compact( 'meta_value' );
    … …  
    189177        // expected_slashed ($meta_key)
    190178        $meta_key = stripslashes($meta_key);
    191         $meta_value = stripslashes_deep($meta_value);
    192 
    193         $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
    194         if ( null !== $check )
    195                 return (bool) $check;
    196 
    197         $_meta_value = $meta_value;
    198         $meta_value = maybe_serialize( $meta_value );
     179        $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
    199180
    200181        $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
    … …  
    209190        if ( !count( $meta_ids ) )
    210191                return false;
    211 
    212         do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
    213192
    214193        $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
    … …  
    224203                clean_user_cache($object_id);
    225204
    226         do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
     205        do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $meta_value );
    227206
    228207        return true;
    … …  
    249228                return false;
    250229
    251         $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
    252         if ( null !== $check ) {
    253                 if ( $single && is_array( $check ) )
    254                         return $check[0];
    255                 else
    256                         return $check;
    257         }
    258 
    259230        $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
    260231
    261232        if ( !$meta_cache ) {
    262                 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
    263                 $meta_cache = $meta_cache[$object_id];
    264         }
    265 
    266         if ( !$meta_key )
     233                update_meta_cache($meta_type, $object_id);
     234                $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
     235        }
     236
     237        if ( ! $meta_key )
    267238                return $meta_cache;
    268239
    … …  
    310281        $cache_key = $meta_type . '_meta';
    311282        $ids = array();
    312         $cache = array();
    313283        foreach ( $object_ids as $id ) {
    314                 $cached_object = wp_cache_get( $id, $cache_key );
    315                 if ( false === $cached_object )
     284                if ( false === wp_cache_get($id, $cache_key) )
    316285                        $ids[] = $id;
    317                 else
    318                         $cache[$id] = $cached_object;
    319286        }
    320287
    321288        if ( empty( $ids ) )
    322                 return $cache;
     289                return false;
    323290
    324291        // Get meta info
    325292        $id_list = join(',', $ids);
     293        $cache = array();
    326294        $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)",
    327295                $meta_type), ARRAY_A );
    … …  
    347315                if ( ! isset($cache[$id]) )
    348316                        $cache[$id] = array();
    349                 wp_cache_add( $id, $cache[$id], $cache_key );
    350         }
     317        }
     318
     319        foreach ( array_keys($cache) as $object)
     320                wp_cache_set($object, $cache[$object], $cache_key);
    351321
    352322        return $cache;
    … …  
    354324
    355325/**
    356  * Given a meta query, generates SQL clauses to be appended to a main query
    357  *
    358  * @since 3.1.0
    359  * @access private
    360  *
    361  * @param array $meta_query List of metadata queries. A single query is an associative array:
    362  * - 'key' string The meta key
    363  * - 'value' string|array The meta value
    364  * - 'compare' (optional) string How to compare the key to the value.
    365  *              Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.
    366  *              Default: '='
    367  * - 'type' string (optional) The type of the value.
    368  *              Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'.
    369  *              Default: 'CHAR'
    370  *
    371  * @param string $meta_type
    372  * @param string $primary_table
    373  * @param string $primary_id_column
    374  * @param object $context (optional) The main query object
    375  * @return array( 'join' => $join_sql, 'where' => $where_sql )
    376  */
    377 function _get_meta_sql( $meta_query, $meta_type, $primary_table, $primary_id_column, $context = null ) {
    378         global $wpdb;
    379 
    380         if ( ! $meta_table = _get_meta_table( $meta_type ) )
    381                 return false;
    382 
    383         $meta_id_column = esc_sql( $meta_type . '_id' );
    384 
    385         $join = '';
    386         $where = '';
    387         $i = 0;
    388         foreach ( $meta_query as $q ) {
    389                 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : '';
    390                 $meta_value = isset( $q['value'] ) ? $q['value'] : '';
    391                 $meta_compare = isset( $q['compare'] ) ? strtoupper( $q['compare'] ) : '=';
    392                 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR';
    393 
    394                 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) )
    395                         $meta_compare = '=';
    396 
    397                 if ( 'NUMERIC' == $meta_type )
    398                         $meta_type = 'SIGNED';
    399                 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) )
    400                         $meta_type = 'CHAR';
    401 
    402                 if ( empty( $meta_key ) && empty( $meta_value ) )
    403                         continue;
    404 
    405                 $alias = $i ? 'mt' . $i : $meta_table;
    406 
    407                 $join .= "\nINNER JOIN $meta_table";
    408                 $join .= $i ? " AS $alias" : '';
    409                 $join .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)";
    410 
    411                 $i++;
    412 
    413                 if ( !empty( $meta_key ) )
    414                         $where .= $wpdb->prepare( " AND $alias.meta_key = %s", $meta_key );
    415 
    416                 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) {
    417                         if ( ! is_array( $meta_value ) )
    418                                 $meta_value = preg_split( '/[,\s]+/', $meta_value );
    419                 } else {
    420                         $meta_value = trim( $meta_value );
    421                 }
    422 
    423                 if ( empty( $meta_value ) )
    424                         continue;
    425 
    426                 if ( 'IN' == substr( $meta_compare, -2) ) {
    427                         $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
    428                 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) {
    429                         $meta_value = array_slice( $meta_value, 0, 2 );
    430                         $meta_compare_string = '%s AND %s';
    431                 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) {
    432                         $meta_value = '%' . like_escape( $meta_value ) . '%';
    433                         $meta_compare_string = '%s';
    434                 } else {
    435                         $meta_compare_string = '%s';
    436                 }
    437 
    438                 // @todo Temporary hack to support empty values. Do not use outside of core.
    439                 if ( '_wp_zero_value' == $meta_value )
    440                         $meta_value = 0;
    441 
    442                 $where .= $wpdb->prepare( " AND CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string}", $meta_value );
    443         }
    444 
    445         return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $meta_query, $meta_type, $primary_table, $primary_id_column, &$context ) );
    446 }
    447 
    448 /**
    449  * Populates the $meta_query property
    450  *
    451  * @access private
    452  * @since 3.1.0
    453  *
    454  * @param array $qv The query variables
    455  */
    456 function _parse_meta_query( &$qv ) {
    457         $meta_query = array();
    458 
    459         // Simple query needs to be first for orderby=meta_value to work correctly
    460         foreach ( array( 'key', 'value', 'compare', 'type' ) as $key ) {
    461                 if ( !empty( $qv[ "meta_$key" ] ) )
    462                         $meta_query[0][ $key ] = $qv[ "meta_$key" ];
    463         }
    464 
    465         if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) {
    466                 $meta_query = array_merge( $meta_query, $qv['meta_query'] );
    467         }
    468 
    469         $qv['meta_query'] = $meta_query;
    470 }
    471 
    472 /**
    473326 * Retrieve the name of the metadata table for the specified object type.
    474327 *
    … …  
    476329 * @uses $wpdb WordPress database object for queries.
    477330 *
    478  * @param string $type Type of object to get metadata table for (e.g., comment, post, or user)
     331 * @param string $meta_type Type of object to get metadata table for (e.g., comment, post, or user)
    479332 * @return mixed Metadata table name, or false if no metadata table exists
    480333 */
Note: See TracChangeset for help on using the changeset viewer.