| [ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Metadata API 4 * 5 * Functions for retrieving and manipulating metadata of various WordPress object types. Metadata 6 * for an object is a represented by a simple key-value pair. Objects may contain multiple 7 * metadata entries that share the same key and differ only in their value. 8 * 9 * @package WordPress 10 * @subpackage Meta 11 * @since 2.9.0 12 */ 13 14 /** 15 * Add metadata for the specified object. 16 * 17 * @since 2.9.0 18 * @uses $wpdb WordPress database object for queries. 19 * @uses do_action() Calls 'added_{$meta_type}_meta' with meta_id of added metadata entry, 20 * object ID, meta key, and meta value 21 * 22 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 23 * @param int $object_id ID of the object metadata is for 24 * @param string $meta_key Metadata key 25 * @param string $meta_value Metadata value 26 * @param bool $unique Optional, default is false. Whether the specified metadata key should be 27 * unique for the object. If true, and the object already has a value for the specified 28 * metadata key, no change will be made 29 * @return bool The meta ID on successful update, false on failure. 30 */ 31 function add_metadata($meta_type, $object_id, $meta_key, $meta_value, $unique = false) { 32 if ( !$meta_type || !$meta_key ) 33 return false; 34 35 if ( !$object_id = absint($object_id) ) 36 return false; 37 38 if ( ! $table = _get_meta_table($meta_type) ) 39 return false; 40 41 global $wpdb; 42 43 $column = esc_sql($meta_type . '_id'); 44 45 // expected_slashed ($meta_key) 46 $meta_key = stripslashes($meta_key); 47 $meta_value = stripslashes_deep($meta_value); 48 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); 49 50 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique ); 51 if ( null !== $check ) 52 return $check; 53 54 if ( $unique && $wpdb->get_var( $wpdb->prepare( 55 "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d", 56 $meta_key, $object_id ) ) ) 57 return false; 58 59 $_meta_value = $meta_value; 60 $meta_value = maybe_serialize( $meta_value ); 61 62 do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value ); 63 64 $result = $wpdb->insert( $table, array( 65 $column => $object_id, 66 'meta_key' => $meta_key, 67 'meta_value' => $meta_value 68 ) ); 69 70 if ( ! $result ) 71 return false; 72 73 $mid = (int) $wpdb->insert_id; 74 75 wp_cache_delete($object_id, $meta_type . '_meta'); 76 77 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value ); 78 79 return $mid; 80 } 81 82 /** 83 * Update metadata for the specified object. If no value already exists for the specified object 84 * ID and metadata key, the metadata will be added. 85 * 86 * @since 2.9.0 87 * @uses $wpdb WordPress database object for queries. 88 * @uses do_action() Calls 'update_{$meta_type}_meta' before updating metadata with meta_id of 89 * metadata entry to update, object ID, meta key, and meta value 90 * @uses do_action() Calls 'updated_{$meta_type}_meta' after updating metadata with meta_id of 91 * updated metadata entry, object ID, meta key, and meta value 92 * 93 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 94 * @param int $object_id ID of the object metadata is for 95 * @param string $meta_key Metadata key 96 * @param string $meta_value Metadata value 97 * @param string $prev_value Optional. If specified, only update existing metadata entries with 98 * the specified value. Otherwise, update all entries. 99 * @return bool True on successful update, false on failure. 100 */ 101 function update_metadata($meta_type, $object_id, $meta_key, $meta_value, $prev_value = '') { 102 if ( !$meta_type || !$meta_key ) 103 return false; 104 105 if ( !$object_id = absint($object_id) ) 106 return false; 107 108 if ( ! $table = _get_meta_table($meta_type) ) 109 return false; 110 111 global $wpdb; 112 113 $column = esc_sql($meta_type . '_id'); 114 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 115 116 // expected_slashed ($meta_key) 117 $meta_key = stripslashes($meta_key); 118 $passed_value = $meta_value; 119 $meta_value = stripslashes_deep($meta_value); 120 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); 121 122 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); 123 if ( null !== $check ) 124 return (bool) $check; 125 126 if ( ! $meta_id = $wpdb->get_var( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ) ) 127 return add_metadata($meta_type, $object_id, $meta_key, $passed_value); 128 129 // Compare existing value to new value if no prev value given and the key exists only once. 130 if ( empty($prev_value) ) { 131 $old_value = get_metadata($meta_type, $object_id, $meta_key); 132 if ( count($old_value) == 1 ) { 133 if ( $old_value[0] === $meta_value ) 134 return false; 135 } 136 } 137 138 $_meta_value = $meta_value; 139 $meta_value = maybe_serialize( $meta_value ); 140 141 $data = compact( 'meta_value' ); 142 $where = array( $column => $object_id, 'meta_key' => $meta_key ); 143 144 if ( !empty( $prev_value ) ) { 145 $prev_value = maybe_serialize($prev_value); 146 $where['meta_value'] = $prev_value; 147 } 148 149 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 150 151 if ( 'post' == $meta_type ) 152 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 153 154 $wpdb->update( $table, $data, $where ); 155 156 wp_cache_delete($object_id, $meta_type . '_meta'); 157 158 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 159 160 if ( 'post' == $meta_type ) 161 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 162 163 return true; 164 } 165 166 /** 167 * Delete metadata for the specified object. 168 * 169 * @since 2.9.0 170 * @uses $wpdb WordPress database object for queries. 171 * @uses do_action() Calls 'deleted_{$meta_type}_meta' after deleting with meta_id of 172 * deleted metadata entries, object ID, meta key, and meta value 173 * 174 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 175 * @param int $object_id ID of the object metadata is for 176 * @param string $meta_key Metadata key 177 * @param string $meta_value Optional. Metadata value. If specified, only delete metadata entries 178 * with this value. Otherwise, delete all entries with the specified meta_key. 179 * @param bool $delete_all Optional, default is false. If true, delete matching metadata entries 180 * for all objects, ignoring the specified object_id. Otherwise, only delete matching 181 * metadata entries for the specified object_id. 182 * @return bool True on successful delete, false on failure. 183 */ 184 function delete_metadata($meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false) { 185 if ( !$meta_type || !$meta_key ) 186 return false; 187 188 if ( (!$object_id = absint($object_id)) && !$delete_all ) 189 return false; 190 191 if ( ! $table = _get_meta_table($meta_type) ) 192 return false; 193 194 global $wpdb; 195 196 $type_column = esc_sql($meta_type . '_id'); 197 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 198 // expected_slashed ($meta_key) 199 $meta_key = stripslashes($meta_key); 200 $meta_value = stripslashes_deep($meta_value); 201 202 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); 203 if ( null !== $check ) 204 return (bool) $check; 205 206 $_meta_value = $meta_value; 207 $meta_value = maybe_serialize( $meta_value ); 208 209 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key ); 210 211 if ( !$delete_all ) 212 $query .= $wpdb->prepare(" AND $type_column = %d", $object_id ); 213 214 if ( $meta_value ) 215 $query .= $wpdb->prepare(" AND meta_value = %s", $meta_value ); 216 217 $meta_ids = $wpdb->get_col( $query ); 218 if ( !count( $meta_ids ) ) 219 return false; 220 221 if ( $delete_all ) 222 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) ); 223 224 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 225 226 if ( 'post' == $meta_type ) 227 do_action( 'delete_postmeta', $meta_ids ); 228 229 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . " )"; 230 231 $count = $wpdb->query($query); 232 233 if ( !$count ) 234 return false; 235 236 if ( $delete_all ) { 237 foreach ( (array) $object_ids as $o_id ) { 238 wp_cache_delete($o_id, $meta_type . '_meta'); 239 } 240 } else { 241 wp_cache_delete($object_id, $meta_type . '_meta'); 242 } 243 244 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 245 246 if ( 'post' == $meta_type ) 247 do_action( 'deleted_postmeta', $meta_ids ); 248 249 return true; 250 } 251 252 /** 253 * Retrieve metadata for the specified object. 254 * 255 * @since 2.9.0 256 * 257 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 258 * @param int $object_id ID of the object metadata is for 259 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 260 * the specified object. 261 * @param bool $single Optional, default is false. If true, return only the first value of the 262 * specified meta_key. This parameter has no effect if meta_key is not specified. 263 * @return string|array Single metadata value, or array of values 264 */ 265 function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) { 266 if ( !$meta_type ) 267 return false; 268 269 if ( !$object_id = absint($object_id) ) 270 return false; 271 272 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); 273 if ( null !== $check ) { 274 if ( $single && is_array( $check ) ) 275 return $check[0]; 276 else 277 return $check; 278 } 279 280 $meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); 281 282 if ( !$meta_cache ) { 283 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); 284 $meta_cache = $meta_cache[$object_id]; 285 } 286 287 if ( !$meta_key ) 288 return $meta_cache; 289 290 if ( isset($meta_cache[$meta_key]) ) { 291 if ( $single ) 292 return maybe_unserialize( $meta_cache[$meta_key][0] ); 293 else 294 return array_map('maybe_unserialize', $meta_cache[$meta_key]); 295 } 296 297 if ($single) 298 return ''; 299 else 300 return array(); 301 } 302 303 /** 304 * Determine if a meta key is set for a given object 305 * 306 * @since 3.3.0 307 * 308 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 309 * @param int $object_id ID of the object metadata is for 310 * @param string $meta_key Metadata key. 311 * @return boolean true of the key is set, false if not. 312 */ 313 function metadata_exists( $meta_type, $object_id, $meta_key ) { 314 if ( ! $meta_type ) 315 return false; 316 317 if ( ! $object_id = absint( $object_id ) ) 318 return false; 319 320 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true ); 321 if ( null !== $check ) 322 return true; 323 324 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); 325 326 if ( !$meta_cache ) { 327 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); 328 $meta_cache = $meta_cache[$object_id]; 329 } 330 331 if ( isset( $meta_cache[ $meta_key ] ) ) 332 return true; 333 334 return false; 335 } 336 337 /** 338 * Get meta data by meta ID 339 * 340 * @since 3.3.0 341 * 342 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 343 * @param int $meta_id ID for a specific meta row 344 * @return object Meta object or false. 345 */ 346 function get_metadata_by_mid( $meta_type, $meta_id ) { 347 global $wpdb; 348 349 if ( ! $meta_type ) 350 return false; 351 352 if ( !$meta_id = absint( $meta_id ) ) 353 return false; 354 355 if ( ! $table = _get_meta_table($meta_type) ) 356 return false; 357 358 $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id'; 359 360 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); 361 362 if ( empty( $meta ) ) 363 return false; 364 365 if ( isset( $meta->meta_value ) ) 366 $meta->meta_value = maybe_unserialize( $meta->meta_value ); 367 368 return $meta; 369 } 370 371 /** 372 * Update meta data by meta ID 373 * 374 * @since 3.3.0 375 * 376 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value 377 * and object_id of the given meta_id. 378 * 379 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 380 * @param int $meta_id ID for a specific meta row 381 * @param string $meta_value Metadata value 382 * @param string $meta_key Optional, you can provide a meta key to update it 383 * @return bool True on successful update, false on failure. 384 */ 385 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) { 386 global $wpdb; 387 388 // Make sure everything is valid. 389 if ( ! $meta_type ) 390 return false; 391 392 if ( ! $meta_id = absint( $meta_id ) ) 393 return false; 394 395 if ( ! $table = _get_meta_table( $meta_type ) ) 396 return false; 397 398 $column = esc_sql($meta_type . '_id'); 399 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 400 401 // Fetch the meta and go on if it's found. 402 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { 403 $original_key = $meta->meta_key; 404 $original_value = $meta->meta_value; 405 $object_id = $meta->{$column}; 406 407 // If a new meta_key (last parameter) was specified, change the meta key, 408 // otherwise use the original key in the update statement. 409 if ( false === $meta_key ) { 410 $meta_key = $original_key; 411 } elseif ( ! is_string( $meta_key ) ) { 412 return false; 413 } 414 415 // Sanitize the meta 416 $_meta_value = $meta_value; 417 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type ); 418 $meta_value = maybe_serialize( $meta_value ); 419 420 // Format the data query arguments. 421 $data = array( 422 'meta_key' => $meta_key, 423 'meta_value' => $meta_value 424 ); 425 426 // Format the where query arguments. 427 $where = array(); 428 $where[$id_column] = $meta_id; 429 430 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 431 432 if ( 'post' == $meta_type ) 433 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 434 435 // Run the update query, all fields in $data are %s, $where is a %d. 436 $result = (bool) $wpdb->update( $table, $data, $where, '%s', '%d' ); 437 438 // Clear the caches. 439 wp_cache_delete($object_id, $meta_type . '_meta'); 440 441 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 442 443 if ( 'post' == $meta_type ) 444 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 445 446 return $result; 447 } 448 449 // And if the meta was not found. 450 return false; 451 } 452 453 /** 454 * Delete meta data by meta ID 455 * 456 * @since 3.3.0 457 * 458 * @uses get_metadata_by_mid() Calls get_metadata_by_mid() to fetch the meta key, value 459 * and object_id of the given meta_id. 460 * 461 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 462 * @param int $meta_id ID for a specific meta row 463 * @return bool True on successful delete, false on failure. 464 */ 465 function delete_metadata_by_mid( $meta_type, $meta_id ) { 466 global $wpdb; 467 468 // Make sure everything is valid. 469 if ( ! $meta_type ) 470 return false; 471 472 if ( ! $meta_id = absint( $meta_id ) ) 473 return false; 474 475 if ( ! $table = _get_meta_table( $meta_type ) ) 476 return false; 477 478 // object and id columns 479 $column = esc_sql($meta_type . '_id'); 480 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 481 482 // Fetch the meta and go on if it's found. 483 if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) { 484 $object_id = $meta->{$column}; 485 486 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); 487 488 if ( 'post' == $meta_type ) 489 do_action( 'delete_postmeta', $meta_id ); 490 491 // Run the query, will return true if deleted, false otherwise 492 $result = (bool) $wpdb->query( $wpdb->prepare( "DELETE FROM $table WHERE $id_column = %d LIMIT 1;", $meta_id ) ); 493 494 // Clear the caches. 495 wp_cache_delete($object_id, $meta_type . '_meta'); 496 497 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); 498 499 if ( 'post' == $meta_type ) 500 do_action( 'deleted_postmeta', $meta_id ); 501 502 return $result; 503 504 } 505 506 // Meta id was not found. 507 return false; 508 } 509 510 /** 511 * Update the metadata cache for the specified objects. 512 * 513 * @since 2.9.0 514 * @uses $wpdb WordPress database object for queries. 515 * 516 * @param string $meta_type Type of object metadata is for (e.g., comment, post, or user) 517 * @param int|array $object_ids array or comma delimited list of object IDs to update cache for 518 * @return mixed Metadata cache for the specified objects, or false on failure. 519 */ 520 function update_meta_cache($meta_type, $object_ids) { 521 if ( empty( $meta_type ) || empty( $object_ids ) ) 522 return false; 523 524 if ( ! $table = _get_meta_table($meta_type) ) 525 return false; 526 527 $column = esc_sql($meta_type . '_id'); 528 529 global $wpdb; 530 531 if ( !is_array($object_ids) ) { 532 $object_ids = preg_replace('|[^0-9,]|', '', $object_ids); 533 $object_ids = explode(',', $object_ids); 534 } 535 536 $object_ids = array_map('intval', $object_ids); 537 538 $cache_key = $meta_type . '_meta'; 539 $ids = array(); 540 $cache = array(); 541 foreach ( $object_ids as $id ) { 542 $cached_object = wp_cache_get( $id, $cache_key ); 543 if ( false === $cached_object ) 544 $ids[] = $id; 545 else 546 $cache[$id] = $cached_object; 547 } 548 549 if ( empty( $ids ) ) 550 return $cache; 551 552 // Get meta info 553 $id_list = join(',', $ids); 554 $meta_list = $wpdb->get_results( $wpdb->prepare("SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list)", 555 $meta_type), ARRAY_A ); 556 557 if ( !empty($meta_list) ) { 558 foreach ( $meta_list as $metarow) { 559 $mpid = intval($metarow[$column]); 560 $mkey = $metarow['meta_key']; 561 $mval = $metarow['meta_value']; 562 563 // Force subkeys to be array type: 564 if ( !isset($cache[$mpid]) || !is_array($cache[$mpid]) ) 565 $cache[$mpid] = array(); 566 if ( !isset($cache[$mpid][$mkey]) || !is_array($cache[$mpid][$mkey]) ) 567 $cache[$mpid][$mkey] = array(); 568 569 // Add a value to the current pid/key: 570 $cache[$mpid][$mkey][] = $mval; 571 } 572 } 573 574 foreach ( $ids as $id ) { 575 if ( ! isset($cache[$id]) ) 576 $cache[$id] = array(); 577 wp_cache_add( $id, $cache[$id], $cache_key ); 578 } 579 580 return $cache; 581 } 582 583 /** 584 * Given a meta query, generates SQL clauses to be appended to a main query 585 * 586 * @since 3.2.0 587 * 588 * @see WP_Meta_Query 589 * 590 * @param array $meta_query A meta query 591 * @param string $type Type of meta 592 * @param string $primary_table 593 * @param string $primary_id_column 594 * @param object $context (optional) The main query object 595 * @return array( 'join' => $join_sql, 'where' => $where_sql ) 596 */ 597 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { 598 $meta_query_obj = new WP_Meta_Query( $meta_query ); 599 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context ); 600 } 601 602 /** 603 * Container class for a multiple metadata query 604 * 605 * @since 3.2.0 606 */ 607 class WP_Meta_Query { 608 /** 609 * List of metadata queries. A single query is an associative array: 610 * - 'key' string The meta key 611 * - 'value' string|array The meta value 612 * - 'compare' (optional) string How to compare the key to the value. 613 * Possible values: '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 614 * Default: '=' 615 * - 'type' string (optional) The type of the value. 616 * Possible values: 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED'. 617 * Default: 'CHAR' 618 * 619 * @since 3.2.0 620 * @access public 621 * @var array 622 */ 623 public $queries = array(); 624 625 /** 626 * The relation between the queries. Can be one of 'AND' or 'OR'. 627 * 628 * @since 3.2.0 629 * @access public 630 * @var string 631 */ 632 public $relation; 633 634 /** 635 * Constructor 636 * 637 * @param array $meta_query (optional) A meta query 638 */ 639 function __construct( $meta_query = false ) { 640 if ( !$meta_query ) 641 return; 642 643 if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) { 644 $this->relation = 'OR'; 645 } else { 646 $this->relation = 'AND'; 647 } 648 649 $this->queries = array(); 650 651 foreach ( $meta_query as $key => $query ) { 652 if ( ! is_array( $query ) ) 653 continue; 654 655 $this->queries[] = $query; 656 } 657 } 658 659 /** 660 * Constructs a meta query based on 'meta_*' query vars 661 * 662 * @since 3.2.0 663 * @access public 664 * 665 * @param array $qv The query variables 666 */ 667 function parse_query_vars( $qv ) { 668 $meta_query = array(); 669 670 // Simple query needs to be first for orderby=meta_value to work correctly 671 foreach ( array( 'key', 'compare', 'type' ) as $key ) { 672 if ( !empty( $qv[ "meta_$key" ] ) ) 673 $meta_query[0][ $key ] = $qv[ "meta_$key" ]; 674 } 675 676 // WP_Query sets 'meta_value' = '' by default 677 if ( isset( $qv[ 'meta_value' ] ) && '' !== $qv[ 'meta_value' ] ) 678 $meta_query[0]['value'] = $qv[ 'meta_value' ]; 679 680 if ( !empty( $qv['meta_query'] ) && is_array( $qv['meta_query'] ) ) { 681 $meta_query = array_merge( $meta_query, $qv['meta_query'] ); 682 } 683 684 $this->__construct( $meta_query ); 685 } 686 687 /** 688 * Generates SQL clauses to be appended to a main query. 689 * 690 * @since 3.2.0 691 * @access public 692 * 693 * @param string $type Type of meta 694 * @param string $primary_table 695 * @param string $primary_id_column 696 * @param object $context (optional) The main query object 697 * @return array( 'join' => $join_sql, 'where' => $where_sql ) 698 */ 699 function get_sql( $type, $primary_table, $primary_id_column, $context = null ) { 700 global $wpdb; 701 702 if ( ! $meta_table = _get_meta_table( $type ) ) 703 return false; 704 705 $meta_id_column = esc_sql( $type . '_id' ); 706 707 $join = array(); 708 $where = array(); 709 710 foreach ( $this->queries as $k => $q ) { 711 $meta_key = isset( $q['key'] ) ? trim( $q['key'] ) : ''; 712 $meta_type = isset( $q['type'] ) ? strtoupper( $q['type'] ) : 'CHAR'; 713 714 if ( 'NUMERIC' == $meta_type ) 715 $meta_type = 'SIGNED'; 716 elseif ( ! in_array( $meta_type, array( 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' ) ) ) 717 $meta_type = 'CHAR'; 718 719 $i = count( $join ); 720 $alias = $i ? 'mt' . $i : $meta_table; 721 722 // Set JOIN 723 $join[$i] = "INNER JOIN $meta_table"; 724 $join[$i] .= $i ? " AS $alias" : ''; 725 $join[$i] .= " ON ($primary_table.$primary_id_column = $alias.$meta_id_column)"; 726 727 $where[$k] = ''; 728 if ( !empty( $meta_key ) ) 729 $where[$k] = $wpdb->prepare( "$alias.meta_key = %s", $meta_key ); 730 731 if ( !isset( $q['value'] ) ) { 732 if ( empty( $where[$k] ) ) 733 unset( $join[$i] ); 734 continue; 735 } 736 737 $meta_value = $q['value']; 738 739 $meta_compare = is_array( $meta_value ) ? 'IN' : '='; 740 if ( isset( $q['compare'] ) ) 741 $meta_compare = strtoupper( $q['compare'] ); 742 743 if ( ! in_array( $meta_compare, array( '=', '!=', '>', '>=', '<', '<=', 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) 744 $meta_compare = '='; 745 746 if ( in_array( $meta_compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { 747 if ( ! is_array( $meta_value ) ) 748 $meta_value = preg_split( '/[,\s]+/', $meta_value ); 749 750 if ( empty( $meta_value ) ) { 751 unset( $join[$i] ); 752 continue; 753 } 754 } else { 755 $meta_value = trim( $meta_value ); 756 } 757 758 if ( 'IN' == substr( $meta_compare, -2) ) { 759 $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')'; 760 } elseif ( 'BETWEEN' == substr( $meta_compare, -7) ) { 761 $meta_value = array_slice( $meta_value, 0, 2 ); 762 $meta_compare_string = '%s AND %s'; 763 } elseif ( 'LIKE' == substr( $meta_compare, -4 ) ) { 764 $meta_value = '%' . like_escape( $meta_value ) . '%'; 765 $meta_compare_string = '%s'; 766 } else { 767 $meta_compare_string = '%s'; 768 } 769 770 if ( ! empty( $where[$k] ) ) 771 $where[$k] .= ' AND '; 772 773 $where[$k] = ' (' . $where[$k] . $wpdb->prepare( "CAST($alias.meta_value AS {$meta_type}) {$meta_compare} {$meta_compare_string})", $meta_value ); 774 } 775 776 $where = array_filter( $where ); 777 778 if ( empty( $where ) ) 779 $where = ''; 780 else 781 $where = ' AND (' . implode( "\n{$this->relation} ", $where ) . ' )'; 782 783 $join = implode( "\n", $join ); 784 if ( ! empty( $join ) ) 785 $join = ' ' . $join; 786 787 return apply_filters_ref_array( 'get_meta_sql', array( compact( 'join', 'where' ), $this->queries, $type, $primary_table, $primary_id_column, $context ) ); 788 } 789 } 790 791 /** 792 * Retrieve the name of the metadata table for the specified object type. 793 * 794 * @since 2.9.0 795 * @uses $wpdb WordPress database object for queries. 796 * 797 * @param string $type Type of object to get metadata table for (e.g., comment, post, or user) 798 * @return mixed Metadata table name, or false if no metadata table exists 799 */ 800 function _get_meta_table($type) { 801 global $wpdb; 802 803 $table_name = $type . 'meta'; 804 805 if ( empty($wpdb->$table_name) ) 806 return false; 807 808 return $wpdb->$table_name; 809 } 810 811 /** 812 * Determine whether a meta key is protected 813 * 814 * @since 3.1.3 815 * 816 * @param string $meta_key Meta key 817 * @return bool True if the key is protected, false otherwise. 818 */ 819 function is_protected_meta( $meta_key, $meta_type = null ) { 820 $protected = ( '_' == $meta_key[0] ); 821 822 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type ); 823 } 824 825 /** 826 * Sanitize meta value 827 * 828 * @since 3.1.3 829 * 830 * @param string $meta_key Meta key 831 * @param mixed $meta_value Meta value to sanitize 832 * @param string $meta_type Type of meta 833 * @return mixed Sanitized $meta_value 834 */ 835 function sanitize_meta( $meta_key, $meta_value, $meta_type ) { 836 return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type ); 837 } 838 839 /** 840 * Register meta key 841 * 842 * @since 3.3.0 843 * 844 * @param string $meta_type Type of meta 845 * @param string $meta_key Meta key 846 * @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key. 847 * @param string|array $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks. 848 * @param array $args Arguments 849 */ 850 function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) { 851 if ( is_callable( $sanitize_callback ) ) 852 add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 ); 853 854 if ( empty( $auth_callback ) ) { 855 if ( is_protected_meta( $meta_key, $meta_type ) ) 856 $auth_callback = '__return_false'; 857 else 858 $auth_callback = '__return_true'; 859 } 860 861 if ( is_callable( $auth_callback ) ) 862 add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 ); 863 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Tue Feb 7 03:55:55 2012 | Hosted by follow the white rabbit. |