Make WordPress Core


Ignore:
File:
1 edited

Legend:

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

    r15390 r17170  
    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;
    4752
    4853        if ( $unique && $wpdb->get_var( $wpdb->prepare(
     
    5257
    5358        $_meta_value = $meta_value;
    54         $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     59        $meta_value = maybe_serialize( $meta_value );
     60
     61        do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
    5562
    5663        $wpdb->insert( $table, array(
     
    106113        // expected_slashed ($meta_key)
    107114        $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;
    108120
    109121        if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) )
     
    120132
    121133        $_meta_value = $meta_value;
    122         $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     134        $meta_value = maybe_serialize( $meta_value );
    123135
    124136        $data  = compact( 'meta_value' );
     
    177189        // expected_slashed ($meta_key)
    178190        $meta_key = stripslashes($meta_key);
    179         $meta_value = maybe_serialize( stripslashes_deep($meta_value) );
     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 );
    180199
    181200        $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
     
    190209        if ( !count( $meta_ids ) )
    191210                return false;
     211
     212        do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
    192213
    193214        $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )";
     
    203224                clean_user_cache($object_id);
    204225
    205         do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $meta_value );
     226        do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
    206227
    207228        return true;
     
    228249                return false;
    229250
     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
    230259        $meta_cache = wp_cache_get($object_id, $meta_type . '_meta');
    231260
    232261        if ( !$meta_cache ) {
    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 )
     262                $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
     263                $meta_cache = $meta_cache[$object_id];
     264        }
     265
     266        if ( !$meta_key )
    238267                return $meta_cache;
    239268
     
    281310        $cache_key = $meta_type . '_meta';
    282311        $ids = array();
     312        $cache = array();
    283313        foreach ( $object_ids as $id ) {
    284                 if ( false === wp_cache_get($id, $cache_key) )
     314                $cached_object = wp_cache_get( $id, $cache_key );
     315                if ( false === $cached_object )
    285316                        $ids[] = $id;
     317                else
     318                        $cache[$id] = $cached_object;
    286319        }
    287320
    288321        if ( empty( $ids ) )
    289                 return false;
     322                return $cache;
    290323
    291324        // Get meta info
    292325        $id_list = join(',', $ids);
    293         $cache = array();
    294326        $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)",
    295327                $meta_type), ARRAY_A );
     
    315347                if ( ! isset($cache[$id]) )
    316348                        $cache[$id] = array();
    317         }
    318 
    319         foreach ( array_keys($cache) as $object)
    320                 wp_cache_set($object, $cache[$object], $cache_key);
     349                wp_cache_add( $id, $cache[$id], $cache_key );
     350        }
    321351
    322352        return $cache;
     
    324354
    325355/**
     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 */
     377function _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 */
     456function _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/**
    326473 * Retrieve the name of the metadata table for the specified object type.
    327474 *
     
    329476 * @uses $wpdb WordPress database object for queries.
    330477 *
    331  * @param string $meta_type Type of object to get metadata table for (e.g., comment, post, or user)
     478 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user)
    332479 * @return mixed Metadata table name, or false if no metadata table exists
    333480 */
Note: See TracChangeset for help on using the changeset viewer.