Changes from branches/3.0/wp-includes/meta.php at r15390 to trunk/wp-includes/meta.php at r17170
- File:
-
- 1 edited
-
trunk/wp-includes/meta.php (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-includes/meta.php
r15390 r17170 45 45 // expected_slashed ($meta_key) 46 46 $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; 47 52 48 53 if ( $unique && $wpdb->get_var( $wpdb->prepare( … … 52 57 53 58 $_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 ); 55 62 56 63 $wpdb->insert( $table, array( … … 106 113 // expected_slashed ($meta_key) 107 114 $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; 108 120 109 121 if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) ) … … 120 132 121 133 $_meta_value = $meta_value; 122 $meta_value = maybe_serialize( stripslashes_deep($meta_value));134 $meta_value = maybe_serialize( $meta_value ); 123 135 124 136 $data = compact( 'meta_value' ); … … 177 189 // expected_slashed ($meta_key) 178 190 $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 ); 180 199 181 200 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key ); … … 190 209 if ( !count( $meta_ids ) ) 191 210 return false; 211 212 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 192 213 193 214 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )"; … … 203 224 clean_user_cache($object_id); 204 225 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 ); 206 227 207 228 return true; … … 228 249 return false; 229 250 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 230 259 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); 231 260 232 261 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 ) 238 267 return $meta_cache; 239 268 … … 281 310 $cache_key = $meta_type . '_meta'; 282 311 $ids = array(); 312 $cache = array(); 283 313 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 ) 285 316 $ids[] = $id; 317 else 318 $cache[$id] = $cached_object; 286 319 } 287 320 288 321 if ( empty( $ids ) ) 289 return false;322 return $cache; 290 323 291 324 // Get meta info 292 325 $id_list = join(',', $ids); 293 $cache = array();294 326 $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)", 295 327 $meta_type), ARRAY_A ); … … 315 347 if ( ! isset($cache[$id]) ) 316 348 $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 } 321 351 322 352 return $cache; … … 324 354 325 355 /** 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 /** 326 473 * Retrieve the name of the metadata table for the specified object type. 327 474 * … … 329 476 * @uses $wpdb WordPress database object for queries. 330 477 * 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) 332 479 * @return mixed Metadata table name, or false if no metadata table exists 333 480 */
Note: See TracChangeset
for help on using the changeset viewer.