[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Core 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 */ 12 13 require ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php'; 14 15 /** 16 * Adds metadata for the specified object. 17 * 18 * @since 2.9.0 19 * 20 * @global wpdb $wpdb WordPress database abstraction object. 21 * 22 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 23 * or any other object type with an associated meta table. 24 * @param int $object_id ID of the object metadata is for. 25 * @param string $meta_key Metadata key. 26 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 27 * @param bool $unique Optional. Whether the specified metadata key should be unique for the object. 28 * If true, and the object already has a value for the specified metadata key, 29 * no change will be made. Default false. 30 * @return int|false The meta ID on success, false on failure. 31 */ 32 function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) { 33 global $wpdb; 34 35 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) { 36 return false; 37 } 38 39 $object_id = absint( $object_id ); 40 if ( ! $object_id ) { 41 return false; 42 } 43 44 $table = _get_meta_table( $meta_type ); 45 if ( ! $table ) { 46 return false; 47 } 48 49 $meta_subtype = get_object_subtype( $meta_type, $object_id ); 50 51 $column = sanitize_key( $meta_type . '_id' ); 52 53 // expected_slashed ($meta_key) 54 $meta_key = wp_unslash( $meta_key ); 55 $meta_value = wp_unslash( $meta_value ); 56 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); 57 58 /** 59 * Short-circuits adding metadata of a specific type. 60 * 61 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 62 * (post, comment, term, user, or any other type with an associated meta table). 63 * Returning a non-null value will effectively short-circuit the function. 64 * 65 * Possible hook names include: 66 * 67 * - `add_post_metadata` 68 * - `add_comment_metadata` 69 * - `add_term_metadata` 70 * - `add_user_metadata` 71 * 72 * @since 3.1.0 73 * 74 * @param null|bool $check Whether to allow adding metadata for the given type. 75 * @param int $object_id ID of the object metadata is for. 76 * @param string $meta_key Metadata key. 77 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 78 * @param bool $unique Whether the specified meta key should be unique for the object. 79 */ 80 $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique ); 81 if ( null !== $check ) { 82 return $check; 83 } 84 85 if ( $unique && $wpdb->get_var( 86 $wpdb->prepare( 87 "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d", 88 $meta_key, 89 $object_id 90 ) 91 ) ) { 92 return false; 93 } 94 95 $_meta_value = $meta_value; 96 $meta_value = maybe_serialize( $meta_value ); 97 98 /** 99 * Fires immediately before meta of a specific type is added. 100 * 101 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 102 * (post, comment, term, user, or any other type with an associated meta table). 103 * 104 * Possible hook names include: 105 * 106 * - `add_post_meta` 107 * - `add_comment_meta` 108 * - `add_term_meta` 109 * - `add_user_meta` 110 * 111 * @since 3.1.0 112 * 113 * @param int $object_id ID of the object metadata is for. 114 * @param string $meta_key Metadata key. 115 * @param mixed $_meta_value Metadata value. 116 */ 117 do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value ); 118 119 $result = $wpdb->insert( 120 $table, 121 array( 122 $column => $object_id, 123 'meta_key' => $meta_key, 124 'meta_value' => $meta_value, 125 ) 126 ); 127 128 if ( ! $result ) { 129 return false; 130 } 131 132 $mid = (int) $wpdb->insert_id; 133 134 wp_cache_delete( $object_id, $meta_type . '_meta' ); 135 136 /** 137 * Fires immediately after meta of a specific type is added. 138 * 139 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 140 * (post, comment, term, user, or any other type with an associated meta table). 141 * 142 * Possible hook names include: 143 * 144 * - `added_post_meta` 145 * - `added_comment_meta` 146 * - `added_term_meta` 147 * - `added_user_meta` 148 * 149 * @since 2.9.0 150 * 151 * @param int $mid The meta ID after successful update. 152 * @param int $object_id ID of the object metadata is for. 153 * @param string $meta_key Metadata key. 154 * @param mixed $_meta_value Metadata value. 155 */ 156 do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value ); 157 158 return $mid; 159 } 160 161 /** 162 * Updates metadata for the specified object. If no value already exists for the specified object 163 * ID and metadata key, the metadata will be added. 164 * 165 * @since 2.9.0 166 * 167 * @global wpdb $wpdb WordPress database abstraction object. 168 * 169 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 170 * or any other object type with an associated meta table. 171 * @param int $object_id ID of the object metadata is for. 172 * @param string $meta_key Metadata key. 173 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 174 * @param mixed $prev_value Optional. Previous value to check before updating. 175 * If specified, only update existing metadata entries with 176 * this value. Otherwise, update all entries. Default empty string. 177 * @return int|bool The new meta field ID if a field with the given key didn't exist 178 * and was therefore added, true on successful update, 179 * false on failure or if the value passed to the function 180 * is the same as the one that is already in the database. 181 */ 182 function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) { 183 global $wpdb; 184 185 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) { 186 return false; 187 } 188 189 $object_id = absint( $object_id ); 190 if ( ! $object_id ) { 191 return false; 192 } 193 194 $table = _get_meta_table( $meta_type ); 195 if ( ! $table ) { 196 return false; 197 } 198 199 $meta_subtype = get_object_subtype( $meta_type, $object_id ); 200 201 $column = sanitize_key( $meta_type . '_id' ); 202 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 203 204 // expected_slashed ($meta_key) 205 $raw_meta_key = $meta_key; 206 $meta_key = wp_unslash( $meta_key ); 207 $passed_value = $meta_value; 208 $meta_value = wp_unslash( $meta_value ); 209 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); 210 211 /** 212 * Short-circuits updating metadata of a specific type. 213 * 214 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 215 * (post, comment, term, user, or any other type with an associated meta table). 216 * Returning a non-null value will effectively short-circuit the function. 217 * 218 * Possible hook names include: 219 * 220 * - `update_post_metadata` 221 * - `update_comment_metadata` 222 * - `update_term_metadata` 223 * - `update_user_metadata` 224 * 225 * @since 3.1.0 226 * 227 * @param null|bool $check Whether to allow updating metadata for the given type. 228 * @param int $object_id ID of the object metadata is for. 229 * @param string $meta_key Metadata key. 230 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 231 * @param mixed $prev_value Optional. Previous value to check before updating. 232 * If specified, only update existing metadata entries with 233 * this value. Otherwise, update all entries. 234 */ 235 $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value ); 236 if ( null !== $check ) { 237 return (bool) $check; 238 } 239 240 // Compare existing value to new value if no prev value given and the key exists only once. 241 if ( empty( $prev_value ) ) { 242 $old_value = get_metadata_raw( $meta_type, $object_id, $meta_key ); 243 if ( is_countable( $old_value ) && count( $old_value ) === 1 ) { 244 if ( $old_value[0] === $meta_value ) { 245 return false; 246 } 247 } 248 } 249 250 $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) ); 251 if ( empty( $meta_ids ) ) { 252 return add_metadata( $meta_type, $object_id, $raw_meta_key, $passed_value ); 253 } 254 255 $_meta_value = $meta_value; 256 $meta_value = maybe_serialize( $meta_value ); 257 258 $data = compact( 'meta_value' ); 259 $where = array( 260 $column => $object_id, 261 'meta_key' => $meta_key, 262 ); 263 264 if ( ! empty( $prev_value ) ) { 265 $prev_value = maybe_serialize( $prev_value ); 266 $where['meta_value'] = $prev_value; 267 } 268 269 foreach ( $meta_ids as $meta_id ) { 270 /** 271 * Fires immediately before updating metadata of a specific type. 272 * 273 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 274 * (post, comment, term, user, or any other type with an associated meta table). 275 * 276 * Possible hook names include: 277 * 278 * - `update_post_meta` 279 * - `update_comment_meta` 280 * - `update_term_meta` 281 * - `update_user_meta` 282 * 283 * @since 2.9.0 284 * 285 * @param int $meta_id ID of the metadata entry to update. 286 * @param int $object_id ID of the object metadata is for. 287 * @param string $meta_key Metadata key. 288 * @param mixed $_meta_value Metadata value. 289 */ 290 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 291 292 if ( 'post' === $meta_type ) { 293 /** 294 * Fires immediately before updating a post's metadata. 295 * 296 * @since 2.9.0 297 * 298 * @param int $meta_id ID of metadata entry to update. 299 * @param int $object_id Post ID. 300 * @param string $meta_key Metadata key. 301 * @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value 302 * if the value is an array, an object, or itself a PHP-serialized string. 303 */ 304 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 305 } 306 } 307 308 $result = $wpdb->update( $table, $data, $where ); 309 if ( ! $result ) { 310 return false; 311 } 312 313 wp_cache_delete( $object_id, $meta_type . '_meta' ); 314 315 foreach ( $meta_ids as $meta_id ) { 316 /** 317 * Fires immediately after updating metadata of a specific type. 318 * 319 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 320 * (post, comment, term, user, or any other type with an associated meta table). 321 * 322 * Possible hook names include: 323 * 324 * - `updated_post_meta` 325 * - `updated_comment_meta` 326 * - `updated_term_meta` 327 * - `updated_user_meta` 328 * 329 * @since 2.9.0 330 * 331 * @param int $meta_id ID of updated metadata entry. 332 * @param int $object_id ID of the object metadata is for. 333 * @param string $meta_key Metadata key. 334 * @param mixed $_meta_value Metadata value. 335 */ 336 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 337 338 if ( 'post' === $meta_type ) { 339 /** 340 * Fires immediately after updating a post's metadata. 341 * 342 * @since 2.9.0 343 * 344 * @param int $meta_id ID of updated metadata entry. 345 * @param int $object_id Post ID. 346 * @param string $meta_key Metadata key. 347 * @param mixed $meta_value Metadata value. This will be a PHP-serialized string representation of the value 348 * if the value is an array, an object, or itself a PHP-serialized string. 349 */ 350 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 351 } 352 } 353 354 return true; 355 } 356 357 /** 358 * Deletes metadata for the specified object. 359 * 360 * @since 2.9.0 361 * 362 * @global wpdb $wpdb WordPress database abstraction object. 363 * 364 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 365 * or any other object type with an associated meta table. 366 * @param int $object_id ID of the object metadata is for. 367 * @param string $meta_key Metadata key. 368 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. 369 * If specified, only delete metadata entries with this value. 370 * Otherwise, delete all entries with the specified meta_key. 371 * Pass `null`, `false`, or an empty string to skip this check. 372 * (For backward compatibility, it is not possible to pass an empty string 373 * to delete those entries with an empty string for a value.) 374 * Default empty string. 375 * @param bool $delete_all Optional. If true, delete matching metadata entries for all objects, 376 * ignoring the specified object_id. Otherwise, only delete 377 * matching metadata entries for the specified object_id. Default false. 378 * @return bool True on successful delete, false on failure. 379 */ 380 function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) { 381 global $wpdb; 382 383 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) { 384 return false; 385 } 386 387 $object_id = absint( $object_id ); 388 if ( ! $object_id && ! $delete_all ) { 389 return false; 390 } 391 392 $table = _get_meta_table( $meta_type ); 393 if ( ! $table ) { 394 return false; 395 } 396 397 $type_column = sanitize_key( $meta_type . '_id' ); 398 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 399 400 // expected_slashed ($meta_key) 401 $meta_key = wp_unslash( $meta_key ); 402 $meta_value = wp_unslash( $meta_value ); 403 404 /** 405 * Short-circuits deleting metadata of a specific type. 406 * 407 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 408 * (post, comment, term, user, or any other type with an associated meta table). 409 * Returning a non-null value will effectively short-circuit the function. 410 * 411 * Possible hook names include: 412 * 413 * - `delete_post_metadata` 414 * - `delete_comment_metadata` 415 * - `delete_term_metadata` 416 * - `delete_user_metadata` 417 * 418 * @since 3.1.0 419 * 420 * @param null|bool $delete Whether to allow metadata deletion of the given type. 421 * @param int $object_id ID of the object metadata is for. 422 * @param string $meta_key Metadata key. 423 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 424 * @param bool $delete_all Whether to delete the matching metadata entries 425 * for all objects, ignoring the specified $object_id. 426 * Default false. 427 */ 428 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); 429 if ( null !== $check ) { 430 return (bool) $check; 431 } 432 433 $_meta_value = $meta_value; 434 $meta_value = maybe_serialize( $meta_value ); 435 436 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key ); 437 438 if ( ! $delete_all ) { 439 $query .= $wpdb->prepare( " AND $type_column = %d", $object_id ); 440 } 441 442 if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) { 443 $query .= $wpdb->prepare( ' AND meta_value = %s', $meta_value ); 444 } 445 446 $meta_ids = $wpdb->get_col( $query ); 447 if ( ! count( $meta_ids ) ) { 448 return false; 449 } 450 451 if ( $delete_all ) { 452 if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) { 453 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) ); 454 } else { 455 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) ); 456 } 457 } 458 459 /** 460 * Fires immediately before deleting metadata of a specific type. 461 * 462 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 463 * (post, comment, term, user, or any other type with an associated meta table). 464 * 465 * Possible hook names include: 466 * 467 * - `delete_post_meta` 468 * - `delete_comment_meta` 469 * - `delete_term_meta` 470 * - `delete_user_meta` 471 * 472 * @since 3.1.0 473 * 474 * @param string[] $meta_ids An array of metadata entry IDs to delete. 475 * @param int $object_id ID of the object metadata is for. 476 * @param string $meta_key Metadata key. 477 * @param mixed $_meta_value Metadata value. 478 */ 479 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 480 481 // Old-style action. 482 if ( 'post' === $meta_type ) { 483 /** 484 * Fires immediately before deleting metadata for a post. 485 * 486 * @since 2.9.0 487 * 488 * @param string[] $meta_ids An array of metadata entry IDs to delete. 489 */ 490 do_action( 'delete_postmeta', $meta_ids ); 491 } 492 493 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )'; 494 495 $count = $wpdb->query( $query ); 496 497 if ( ! $count ) { 498 return false; 499 } 500 501 if ( $delete_all ) { 502 $data = (array) $object_ids; 503 } else { 504 $data = array( $object_id ); 505 } 506 wp_cache_delete_multiple( $data, $meta_type . '_meta' ); 507 508 /** 509 * Fires immediately after deleting metadata of a specific type. 510 * 511 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 512 * (post, comment, term, user, or any other type with an associated meta table). 513 * 514 * Possible hook names include: 515 * 516 * - `deleted_post_meta` 517 * - `deleted_comment_meta` 518 * - `deleted_term_meta` 519 * - `deleted_user_meta` 520 * 521 * @since 2.9.0 522 * 523 * @param string[] $meta_ids An array of metadata entry IDs to delete. 524 * @param int $object_id ID of the object metadata is for. 525 * @param string $meta_key Metadata key. 526 * @param mixed $_meta_value Metadata value. 527 */ 528 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 529 530 // Old-style action. 531 if ( 'post' === $meta_type ) { 532 /** 533 * Fires immediately after deleting metadata for a post. 534 * 535 * @since 2.9.0 536 * 537 * @param string[] $meta_ids An array of metadata entry IDs to delete. 538 */ 539 do_action( 'deleted_postmeta', $meta_ids ); 540 } 541 542 return true; 543 } 544 545 /** 546 * Retrieves the value of a metadata field for the specified object type and ID. 547 * 548 * If the meta field exists, a single value is returned if `$single` is true, 549 * or an array of values if it's false. 550 * 551 * If the meta field does not exist, the result depends on get_metadata_default(). 552 * By default, an empty string is returned if `$single` is true, or an empty array 553 * if it's false. 554 * 555 * @since 2.9.0 556 * 557 * @see get_metadata_raw() 558 * @see get_metadata_default() 559 * 560 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 561 * or any other object type with an associated meta table. 562 * @param int $object_id ID of the object metadata is for. 563 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 564 * the specified object. Default empty string. 565 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. 566 * This parameter has no effect if `$meta_key` is not specified. Default false. 567 * @return mixed An array of values if `$single` is false. 568 * The value of the meta field if `$single` is true. 569 * False for an invalid `$object_id` (non-numeric, zero, or negative value), 570 * or if `$meta_type` is not specified. 571 * An empty array if a valid but non-existing object ID is passed and `$single` is false. 572 * An empty string if a valid but non-existing object ID is passed and `$single` is true. 573 */ 574 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { 575 $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single ); 576 if ( ! is_null( $value ) ) { 577 return $value; 578 } 579 580 return get_metadata_default( $meta_type, $object_id, $meta_key, $single ); 581 } 582 583 /** 584 * Retrieves raw metadata value for the specified object. 585 * 586 * @since 5.5.0 587 * 588 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 589 * or any other object type with an associated meta table. 590 * @param int $object_id ID of the object metadata is for. 591 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 592 * the specified object. Default empty string. 593 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. 594 * This parameter has no effect if `$meta_key` is not specified. Default false. 595 * @return mixed An array of values if `$single` is false. 596 * The value of the meta field if `$single` is true. 597 * False for an invalid `$object_id` (non-numeric, zero, or negative value), 598 * or if `$meta_type` is not specified. 599 * Null if the value does not exist. 600 */ 601 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) { 602 if ( ! $meta_type || ! is_numeric( $object_id ) ) { 603 return false; 604 } 605 606 $object_id = absint( $object_id ); 607 if ( ! $object_id ) { 608 return false; 609 } 610 611 /** 612 * Short-circuits the return value of a meta field. 613 * 614 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 615 * (post, comment, term, user, or any other type with an associated meta table). 616 * Returning a non-null value will effectively short-circuit the function. 617 * 618 * Possible filter names include: 619 * 620 * - `get_post_metadata` 621 * - `get_comment_metadata` 622 * - `get_term_metadata` 623 * - `get_user_metadata` 624 * 625 * @since 3.1.0 626 * @since 5.5.0 Added the `$meta_type` parameter. 627 * 628 * @param mixed $value The value to return, either a single metadata value or an array 629 * of values depending on the value of `$single`. Default null. 630 * @param int $object_id ID of the object metadata is for. 631 * @param string $meta_key Metadata key. 632 * @param bool $single Whether to return only the first value of the specified `$meta_key`. 633 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 634 * or any other object type with an associated meta table. 635 */ 636 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type ); 637 if ( null !== $check ) { 638 if ( $single && is_array( $check ) ) { 639 return $check[0]; 640 } else { 641 return $check; 642 } 643 } 644 645 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); 646 647 if ( ! $meta_cache ) { 648 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); 649 if ( isset( $meta_cache[ $object_id ] ) ) { 650 $meta_cache = $meta_cache[ $object_id ]; 651 } else { 652 $meta_cache = null; 653 } 654 } 655 656 if ( ! $meta_key ) { 657 return $meta_cache; 658 } 659 660 if ( isset( $meta_cache[ $meta_key ] ) ) { 661 if ( $single ) { 662 return maybe_unserialize( $meta_cache[ $meta_key ][0] ); 663 } else { 664 return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] ); 665 } 666 } 667 668 return null; 669 } 670 671 /** 672 * Retrieves default metadata value for the specified meta key and object. 673 * 674 * By default, an empty string is returned if `$single` is true, or an empty array 675 * if it's false. 676 * 677 * @since 5.5.0 678 * 679 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 680 * or any other object type with an associated meta table. 681 * @param int $object_id ID of the object metadata is for. 682 * @param string $meta_key Metadata key. 683 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. 684 * This parameter has no effect if `$meta_key` is not specified. Default false. 685 * @return mixed An array of default values if `$single` is false. 686 * The default value of the meta field if `$single` is true. 687 */ 688 function get_metadata_default( $meta_type, $object_id, $meta_key, $single = false ) { 689 if ( $single ) { 690 $value = ''; 691 } else { 692 $value = array(); 693 } 694 695 /** 696 * Filters the default metadata value for a specified meta key and object. 697 * 698 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 699 * (post, comment, term, user, or any other type with an associated meta table). 700 * 701 * Possible filter names include: 702 * 703 * - `default_post_metadata` 704 * - `default_comment_metadata` 705 * - `default_term_metadata` 706 * - `default_user_metadata` 707 * 708 * @since 5.5.0 709 * 710 * @param mixed $value The value to return, either a single metadata value or an array 711 * of values depending on the value of `$single`. 712 * @param int $object_id ID of the object metadata is for. 713 * @param string $meta_key Metadata key. 714 * @param bool $single Whether to return only the first value of the specified `$meta_key`. 715 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 716 * or any other object type with an associated meta table. 717 */ 718 $value = apply_filters( "default_{$meta_type}_metadata", $value, $object_id, $meta_key, $single, $meta_type ); 719 720 if ( ! $single && ! wp_is_numeric_array( $value ) ) { 721 $value = array( $value ); 722 } 723 724 return $value; 725 } 726 727 /** 728 * Determines if a meta field with the given key exists for the given object ID. 729 * 730 * @since 3.3.0 731 * 732 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 733 * or any other object type with an associated meta table. 734 * @param int $object_id ID of the object metadata is for. 735 * @param string $meta_key Metadata key. 736 * @return bool Whether a meta field with the given key exists. 737 */ 738 function metadata_exists( $meta_type, $object_id, $meta_key ) { 739 if ( ! $meta_type || ! is_numeric( $object_id ) ) { 740 return false; 741 } 742 743 $object_id = absint( $object_id ); 744 if ( ! $object_id ) { 745 return false; 746 } 747 748 /** This filter is documented in wp-includes/meta.php */ 749 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true, $meta_type ); 750 if ( null !== $check ) { 751 return (bool) $check; 752 } 753 754 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); 755 756 if ( ! $meta_cache ) { 757 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); 758 $meta_cache = $meta_cache[ $object_id ]; 759 } 760 761 if ( isset( $meta_cache[ $meta_key ] ) ) { 762 return true; 763 } 764 765 return false; 766 } 767 768 /** 769 * Retrieves metadata by meta ID. 770 * 771 * @since 3.3.0 772 * 773 * @global wpdb $wpdb WordPress database abstraction object. 774 * 775 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 776 * or any other object type with an associated meta table. 777 * @param int $meta_id ID for a specific meta row. 778 * @return stdClass|false { 779 * Metadata object, or boolean `false` if the metadata doesn't exist. 780 * 781 * @type string $meta_key The meta key. 782 * @type mixed $meta_value The unserialized meta value. 783 * @type string $meta_id Optional. The meta ID when the meta type is any value except 'user'. 784 * @type string $umeta_id Optional. The meta ID when the meta type is 'user'. 785 * @type string $post_id Optional. The object ID when the meta type is 'post'. 786 * @type string $comment_id Optional. The object ID when the meta type is 'comment'. 787 * @type string $term_id Optional. The object ID when the meta type is 'term'. 788 * @type string $user_id Optional. The object ID when the meta type is 'user'. 789 * } 790 */ 791 function get_metadata_by_mid( $meta_type, $meta_id ) { 792 global $wpdb; 793 794 if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { 795 return false; 796 } 797 798 $meta_id = (int) $meta_id; 799 if ( $meta_id <= 0 ) { 800 return false; 801 } 802 803 $table = _get_meta_table( $meta_type ); 804 if ( ! $table ) { 805 return false; 806 } 807 808 /** 809 * Short-circuits the return value when fetching a meta field by meta ID. 810 * 811 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 812 * (post, comment, term, user, or any other type with an associated meta table). 813 * Returning a non-null value will effectively short-circuit the function. 814 * 815 * Possible hook names include: 816 * 817 * - `get_post_metadata_by_mid` 818 * - `get_comment_metadata_by_mid` 819 * - `get_term_metadata_by_mid` 820 * - `get_user_metadata_by_mid` 821 * 822 * @since 5.0.0 823 * 824 * @param stdClass|null $value The value to return. 825 * @param int $meta_id Meta ID. 826 */ 827 $check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id ); 828 if ( null !== $check ) { 829 return $check; 830 } 831 832 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 833 834 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); 835 836 if ( empty( $meta ) ) { 837 return false; 838 } 839 840 if ( isset( $meta->meta_value ) ) { 841 $meta->meta_value = maybe_unserialize( $meta->meta_value ); 842 } 843 844 return $meta; 845 } 846 847 /** 848 * Updates metadata by meta ID. 849 * 850 * @since 3.3.0 851 * 852 * @global wpdb $wpdb WordPress database abstraction object. 853 * 854 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 855 * or any other object type with an associated meta table. 856 * @param int $meta_id ID for a specific meta row. 857 * @param string $meta_value Metadata value. Must be serializable if non-scalar. 858 * @param string|false $meta_key Optional. You can provide a meta key to update it. Default false. 859 * @return bool True on successful update, false on failure. 860 */ 861 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) { 862 global $wpdb; 863 864 // Make sure everything is valid. 865 if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { 866 return false; 867 } 868 869 $meta_id = (int) $meta_id; 870 if ( $meta_id <= 0 ) { 871 return false; 872 } 873 874 $table = _get_meta_table( $meta_type ); 875 if ( ! $table ) { 876 return false; 877 } 878 879 $column = sanitize_key( $meta_type . '_id' ); 880 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 881 882 /** 883 * Short-circuits updating metadata of a specific type by meta ID. 884 * 885 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 886 * (post, comment, term, user, or any other type with an associated meta table). 887 * Returning a non-null value will effectively short-circuit the function. 888 * 889 * Possible hook names include: 890 * 891 * - `update_post_metadata_by_mid` 892 * - `update_comment_metadata_by_mid` 893 * - `update_term_metadata_by_mid` 894 * - `update_user_metadata_by_mid` 895 * 896 * @since 5.0.0 897 * 898 * @param null|bool $check Whether to allow updating metadata for the given type. 899 * @param int $meta_id Meta ID. 900 * @param mixed $meta_value Meta value. Must be serializable if non-scalar. 901 * @param string|false $meta_key Meta key, if provided. 902 */ 903 $check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key ); 904 if ( null !== $check ) { 905 return (bool) $check; 906 } 907 908 // Fetch the meta and go on if it's found. 909 $meta = get_metadata_by_mid( $meta_type, $meta_id ); 910 if ( $meta ) { 911 $original_key = $meta->meta_key; 912 $object_id = $meta->{$column}; 913 914 /* 915 * If a new meta_key (last parameter) was specified, change the meta key, 916 * otherwise use the original key in the update statement. 917 */ 918 if ( false === $meta_key ) { 919 $meta_key = $original_key; 920 } elseif ( ! is_string( $meta_key ) ) { 921 return false; 922 } 923 924 $meta_subtype = get_object_subtype( $meta_type, $object_id ); 925 926 // Sanitize the meta. 927 $_meta_value = $meta_value; 928 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); 929 $meta_value = maybe_serialize( $meta_value ); 930 931 // Format the data query arguments. 932 $data = array( 933 'meta_key' => $meta_key, 934 'meta_value' => $meta_value, 935 ); 936 937 // Format the where query arguments. 938 $where = array(); 939 $where[ $id_column ] = $meta_id; 940 941 /** This action is documented in wp-includes/meta.php */ 942 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 943 944 if ( 'post' === $meta_type ) { 945 /** This action is documented in wp-includes/meta.php */ 946 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 947 } 948 949 // Run the update query, all fields in $data are %s, $where is a %d. 950 $result = $wpdb->update( $table, $data, $where, '%s', '%d' ); 951 if ( ! $result ) { 952 return false; 953 } 954 955 // Clear the caches. 956 wp_cache_delete( $object_id, $meta_type . '_meta' ); 957 958 /** This action is documented in wp-includes/meta.php */ 959 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 960 961 if ( 'post' === $meta_type ) { 962 /** This action is documented in wp-includes/meta.php */ 963 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 964 } 965 966 return true; 967 } 968 969 // And if the meta was not found. 970 return false; 971 } 972 973 /** 974 * Deletes metadata by meta ID. 975 * 976 * @since 3.3.0 977 * 978 * @global wpdb $wpdb WordPress database abstraction object. 979 * 980 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 981 * or any other object type with an associated meta table. 982 * @param int $meta_id ID for a specific meta row. 983 * @return bool True on successful delete, false on failure. 984 */ 985 function delete_metadata_by_mid( $meta_type, $meta_id ) { 986 global $wpdb; 987 988 // Make sure everything is valid. 989 if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { 990 return false; 991 } 992 993 $meta_id = (int) $meta_id; 994 if ( $meta_id <= 0 ) { 995 return false; 996 } 997 998 $table = _get_meta_table( $meta_type ); 999 if ( ! $table ) { 1000 return false; 1001 } 1002 1003 // Object and ID columns. 1004 $column = sanitize_key( $meta_type . '_id' ); 1005 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 1006 1007 /** 1008 * Short-circuits deleting metadata of a specific type by meta ID. 1009 * 1010 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 1011 * (post, comment, term, user, or any other type with an associated meta table). 1012 * Returning a non-null value will effectively short-circuit the function. 1013 * 1014 * Possible hook names include: 1015 * 1016 * - `delete_post_metadata_by_mid` 1017 * - `delete_comment_metadata_by_mid` 1018 * - `delete_term_metadata_by_mid` 1019 * - `delete_user_metadata_by_mid` 1020 * 1021 * @since 5.0.0 1022 * 1023 * @param null|bool $delete Whether to allow metadata deletion of the given type. 1024 * @param int $meta_id Meta ID. 1025 */ 1026 $check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id ); 1027 if ( null !== $check ) { 1028 return (bool) $check; 1029 } 1030 1031 // Fetch the meta and go on if it's found. 1032 $meta = get_metadata_by_mid( $meta_type, $meta_id ); 1033 if ( $meta ) { 1034 $object_id = (int) $meta->{$column}; 1035 1036 /** This action is documented in wp-includes/meta.php */ 1037 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); 1038 1039 // Old-style action. 1040 if ( 'post' === $meta_type || 'comment' === $meta_type ) { 1041 /** 1042 * Fires immediately before deleting post or comment metadata of a specific type. 1043 * 1044 * The dynamic portion of the hook name, `$meta_type`, refers to the meta 1045 * object type (post or comment). 1046 * 1047 * Possible hook names include: 1048 * 1049 * - `delete_postmeta` 1050 * - `delete_commentmeta` 1051 * - `delete_termmeta` 1052 * - `delete_usermeta` 1053 * 1054 * @since 3.4.0 1055 * 1056 * @param int $meta_id ID of the metadata entry to delete. 1057 */ 1058 do_action( "delete_{$meta_type}meta", $meta_id ); 1059 } 1060 1061 // Run the query, will return true if deleted, false otherwise. 1062 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); 1063 1064 // Clear the caches. 1065 wp_cache_delete( $object_id, $meta_type . '_meta' ); 1066 1067 /** This action is documented in wp-includes/meta.php */ 1068 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); 1069 1070 // Old-style action. 1071 if ( 'post' === $meta_type || 'comment' === $meta_type ) { 1072 /** 1073 * Fires immediately after deleting post or comment metadata of a specific type. 1074 * 1075 * The dynamic portion of the hook name, `$meta_type`, refers to the meta 1076 * object type (post or comment). 1077 * 1078 * Possible hook names include: 1079 * 1080 * - `deleted_postmeta` 1081 * - `deleted_commentmeta` 1082 * - `deleted_termmeta` 1083 * - `deleted_usermeta` 1084 * 1085 * @since 3.4.0 1086 * 1087 * @param int $meta_id Deleted metadata entry ID. 1088 */ 1089 do_action( "deleted_{$meta_type}meta", $meta_id ); 1090 } 1091 1092 return $result; 1093 1094 } 1095 1096 // Meta ID was not found. 1097 return false; 1098 } 1099 1100 /** 1101 * Updates the metadata cache for the specified objects. 1102 * 1103 * @since 2.9.0 1104 * 1105 * @global wpdb $wpdb WordPress database abstraction object. 1106 * 1107 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1108 * or any other object type with an associated meta table. 1109 * @param string|int[] $object_ids Array or comma delimited list of object IDs to update cache for. 1110 * @return array|false Metadata cache for the specified objects, or false on failure. 1111 */ 1112 function update_meta_cache( $meta_type, $object_ids ) { 1113 global $wpdb; 1114 1115 if ( ! $meta_type || ! $object_ids ) { 1116 return false; 1117 } 1118 1119 $table = _get_meta_table( $meta_type ); 1120 if ( ! $table ) { 1121 return false; 1122 } 1123 1124 $column = sanitize_key( $meta_type . '_id' ); 1125 1126 if ( ! is_array( $object_ids ) ) { 1127 $object_ids = preg_replace( '|[^0-9,]|', '', $object_ids ); 1128 $object_ids = explode( ',', $object_ids ); 1129 } 1130 1131 $object_ids = array_map( 'intval', $object_ids ); 1132 1133 /** 1134 * Short-circuits updating the metadata cache of a specific type. 1135 * 1136 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 1137 * (post, comment, term, user, or any other type with an associated meta table). 1138 * Returning a non-null value will effectively short-circuit the function. 1139 * 1140 * Possible hook names include: 1141 * 1142 * - `update_post_metadata_cache` 1143 * - `update_comment_metadata_cache` 1144 * - `update_term_metadata_cache` 1145 * - `update_user_metadata_cache` 1146 * 1147 * @since 5.0.0 1148 * 1149 * @param mixed $check Whether to allow updating the meta cache of the given type. 1150 * @param int[] $object_ids Array of object IDs to update the meta cache for. 1151 */ 1152 $check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids ); 1153 if ( null !== $check ) { 1154 return (bool) $check; 1155 } 1156 1157 $cache_key = $meta_type . '_meta'; 1158 $non_cached_ids = array(); 1159 $cache = array(); 1160 $cache_values = wp_cache_get_multiple( $object_ids, $cache_key ); 1161 1162 foreach ( $cache_values as $id => $cached_object ) { 1163 if ( false === $cached_object ) { 1164 $non_cached_ids[] = $id; 1165 } else { 1166 $cache[ $id ] = $cached_object; 1167 } 1168 } 1169 1170 if ( empty( $non_cached_ids ) ) { 1171 return $cache; 1172 } 1173 1174 // Get meta info. 1175 $id_list = implode( ',', $non_cached_ids ); 1176 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 1177 1178 $meta_list = $wpdb->get_results( "SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC", ARRAY_A ); 1179 1180 if ( ! empty( $meta_list ) ) { 1181 foreach ( $meta_list as $metarow ) { 1182 $mpid = (int) $metarow[ $column ]; 1183 $mkey = $metarow['meta_key']; 1184 $mval = $metarow['meta_value']; 1185 1186 // Force subkeys to be array type. 1187 if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) { 1188 $cache[ $mpid ] = array(); 1189 } 1190 if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) { 1191 $cache[ $mpid ][ $mkey ] = array(); 1192 } 1193 1194 // Add a value to the current pid/key. 1195 $cache[ $mpid ][ $mkey ][] = $mval; 1196 } 1197 } 1198 1199 $data = array(); 1200 foreach ( $non_cached_ids as $id ) { 1201 if ( ! isset( $cache[ $id ] ) ) { 1202 $cache[ $id ] = array(); 1203 } 1204 $data[ $id ] = $cache[ $id ]; 1205 } 1206 wp_cache_add_multiple( $data, $cache_key ); 1207 1208 return $cache; 1209 } 1210 1211 /** 1212 * Retrieves the queue for lazy-loading metadata. 1213 * 1214 * @since 4.5.0 1215 * 1216 * @return WP_Metadata_Lazyloader Metadata lazyloader queue. 1217 */ 1218 function wp_metadata_lazyloader() { 1219 static $wp_metadata_lazyloader; 1220 1221 if ( null === $wp_metadata_lazyloader ) { 1222 $wp_metadata_lazyloader = new WP_Metadata_Lazyloader(); 1223 } 1224 1225 return $wp_metadata_lazyloader; 1226 } 1227 1228 /** 1229 * Given a meta query, generates SQL clauses to be appended to a main query. 1230 * 1231 * @since 3.2.0 1232 * 1233 * @see WP_Meta_Query 1234 * 1235 * @param array $meta_query A meta query. 1236 * @param string $type Type of meta. 1237 * @param string $primary_table Primary database table name. 1238 * @param string $primary_id_column Primary ID column name. 1239 * @param object $context Optional. The main query object. Default null. 1240 * @return string[]|false { 1241 * Array containing JOIN and WHERE SQL clauses to append to the main query, 1242 * or false if no table exists for the requested meta type. 1243 * 1244 * @type string $join SQL fragment to append to the main JOIN clause. 1245 * @type string $where SQL fragment to append to the main WHERE clause. 1246 * } 1247 */ 1248 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { 1249 $meta_query_obj = new WP_Meta_Query( $meta_query ); 1250 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context ); 1251 } 1252 1253 /** 1254 * Retrieves the name of the metadata table for the specified object type. 1255 * 1256 * @since 2.9.0 1257 * 1258 * @global wpdb $wpdb WordPress database abstraction object. 1259 * 1260 * @param string $type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1261 * or any other object type with an associated meta table. 1262 * @return string|false Metadata table name, or false if no metadata table exists 1263 */ 1264 function _get_meta_table( $type ) { 1265 global $wpdb; 1266 1267 $table_name = $type . 'meta'; 1268 1269 if ( empty( $wpdb->$table_name ) ) { 1270 return false; 1271 } 1272 1273 return $wpdb->$table_name; 1274 } 1275 1276 /** 1277 * Determines whether a meta key is considered protected. 1278 * 1279 * @since 3.1.3 1280 * 1281 * @param string $meta_key Metadata key. 1282 * @param string $meta_type Optional. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1283 * or any other object type with an associated meta table. Default empty string. 1284 * @return bool Whether the meta key is considered protected. 1285 */ 1286 function is_protected_meta( $meta_key, $meta_type = '' ) { 1287 $sanitized_key = preg_replace( "/[^\x20-\x7E\p{L}]/", '', $meta_key ); 1288 $protected = strlen( $sanitized_key ) > 0 && ( '_' === $sanitized_key[0] ); 1289 1290 /** 1291 * Filters whether a meta key is considered protected. 1292 * 1293 * @since 3.2.0 1294 * 1295 * @param bool $protected Whether the key is considered protected. 1296 * @param string $meta_key Metadata key. 1297 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1298 * or any other object type with an associated meta table. 1299 */ 1300 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type ); 1301 } 1302 1303 /** 1304 * Sanitizes meta value. 1305 * 1306 * @since 3.1.3 1307 * @since 4.9.8 The `$object_subtype` parameter was added. 1308 * 1309 * @param string $meta_key Metadata key. 1310 * @param mixed $meta_value Metadata value to sanitize. 1311 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1312 * or any other object type with an associated meta table. 1313 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. 1314 * @return mixed Sanitized $meta_value. 1315 */ 1316 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { 1317 if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { 1318 1319 /** 1320 * Filters the sanitization of a specific meta key of a specific meta type and subtype. 1321 * 1322 * The dynamic portions of the hook name, `$object_type`, `$meta_key`, 1323 * and `$object_subtype`, refer to the metadata object type (comment, post, term, or user), 1324 * the meta key value, and the object subtype respectively. 1325 * 1326 * @since 4.9.8 1327 * 1328 * @param mixed $meta_value Metadata value to sanitize. 1329 * @param string $meta_key Metadata key. 1330 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1331 * or any other object type with an associated meta table. 1332 * @param string $object_subtype Object subtype. 1333 */ 1334 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype ); 1335 } 1336 1337 /** 1338 * Filters the sanitization of a specific meta key of a specific meta type. 1339 * 1340 * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`, 1341 * refer to the metadata object type (comment, post, term, or user) and the meta 1342 * key value, respectively. 1343 * 1344 * @since 3.3.0 1345 * 1346 * @param mixed $meta_value Metadata value to sanitize. 1347 * @param string $meta_key Metadata key. 1348 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1349 * or any other object type with an associated meta table. 1350 */ 1351 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type ); 1352 } 1353 1354 /** 1355 * Registers a meta key. 1356 * 1357 * It is recommended to register meta keys for a specific combination of object type and object subtype. If passing 1358 * an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly 1359 * overridden in case a more specific meta key of the same name exists for the same object type and a subtype. 1360 * 1361 * If an object type does not support any subtypes, such as users or comments, you should commonly call this function 1362 * without passing a subtype. 1363 * 1364 * @since 3.3.0 1365 * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified 1366 * to support an array of data to attach to registered meta keys}. Previous arguments for 1367 * `$sanitize_callback` and `$auth_callback` have been folded into this array. 1368 * @since 4.9.8 The `$object_subtype` argument was added to the arguments array. 1369 * @since 5.3.0 Valid meta types expanded to include "array" and "object". 1370 * @since 5.5.0 The `$default` argument was added to the arguments array. 1371 * @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array. 1372 * @since 6.7.0 The `label` argument was added to the arguments array. 1373 * 1374 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1375 * or any other object type with an associated meta table. 1376 * @param string $meta_key Meta key to register. 1377 * @param array $args { 1378 * Data used to describe the meta key when registered. 1379 * 1380 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, 1381 * the meta key will be registered on the entire object type. Default empty. 1382 * @type string $type The type of data associated with this meta key. 1383 * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. 1384 * @type string $label A human-readable label of the data attached to this meta key. 1385 * @type string $description A description of the data attached to this meta key. 1386 * @type bool $single Whether the meta key has one value per object, or an array of values per object. 1387 * @type mixed $default The default value returned from get_metadata() if no value has been set yet. 1388 * When using a non-single meta key, the default value is for the first entry. 1389 * In other words, when calling get_metadata() with `$single` set to `false`, 1390 * the default value given here will be wrapped in an array. 1391 * @type callable $sanitize_callback A function or method to call when sanitizing `$meta_key` data. 1392 * @type callable $auth_callback Optional. A function or method to call when performing edit_post_meta, 1393 * add_post_meta, and delete_post_meta capability checks. 1394 * @type bool|array $show_in_rest Whether data associated with this meta key can be considered public and 1395 * should be accessible via the REST API. A custom post type must also declare 1396 * support for custom fields for registered meta to be accessible via REST. 1397 * When registering complex meta values this argument may optionally be an 1398 * array with 'schema' or 'prepare_callback' keys instead of a boolean. 1399 * @type bool $revisions_enabled Whether to enable revisions support for this meta_key. Can only be used when the 1400 * object type is 'post'. 1401 * } 1402 * @param string|array $deprecated Deprecated. Use `$args` instead. 1403 * @return bool True if the meta key was successfully registered in the global array, false if not. 1404 * Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks, 1405 * but will not add to the global registry. 1406 */ 1407 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { 1408 global $wp_meta_keys; 1409 1410 if ( ! is_array( $wp_meta_keys ) ) { 1411 $wp_meta_keys = array(); 1412 } 1413 1414 $defaults = array( 1415 'object_subtype' => '', 1416 'type' => 'string', 1417 'label' => '', 1418 'description' => '', 1419 'default' => '', 1420 'single' => false, 1421 'sanitize_callback' => null, 1422 'auth_callback' => null, 1423 'show_in_rest' => false, 1424 'revisions_enabled' => false, 1425 ); 1426 1427 // There used to be individual args for sanitize and auth callbacks. 1428 $has_old_sanitize_cb = false; 1429 $has_old_auth_cb = false; 1430 1431 if ( is_callable( $args ) ) { 1432 $args = array( 1433 'sanitize_callback' => $args, 1434 ); 1435 1436 $has_old_sanitize_cb = true; 1437 } else { 1438 $args = (array) $args; 1439 } 1440 1441 if ( is_callable( $deprecated ) ) { 1442 $args['auth_callback'] = $deprecated; 1443 $has_old_auth_cb = true; 1444 } 1445 1446 /** 1447 * Filters the registration arguments when registering meta. 1448 * 1449 * @since 4.6.0 1450 * 1451 * @param array $args Array of meta registration arguments. 1452 * @param array $defaults Array of default arguments. 1453 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1454 * or any other object type with an associated meta table. 1455 * @param string $meta_key Meta key. 1456 */ 1457 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1458 unset( $defaults['default'] ); 1459 $args = wp_parse_args( $args, $defaults ); 1460 1461 // Require an item schema when registering array meta. 1462 if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) { 1463 if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) { 1464 _doing_it_wrong( __FUNCTION__, __( 'When registering an "array" meta type to show in the REST API, you must specify the schema for each array item in "show_in_rest.schema.items".' ), '5.3.0' ); 1465 1466 return false; 1467 } 1468 } 1469 1470 $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : ''; 1471 if ( $args['revisions_enabled'] ) { 1472 if ( 'post' !== $object_type ) { 1473 _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object type supports revisions.' ), '6.4.0' ); 1474 1475 return false; 1476 } elseif ( ! empty( $object_subtype ) && ! post_type_supports( $object_subtype, 'revisions' ) ) { 1477 _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object subtype supports revisions.' ), '6.4.0' ); 1478 1479 return false; 1480 } 1481 } 1482 1483 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. 1484 if ( empty( $args['auth_callback'] ) ) { 1485 if ( is_protected_meta( $meta_key, $object_type ) ) { 1486 $args['auth_callback'] = '__return_false'; 1487 } else { 1488 $args['auth_callback'] = '__return_true'; 1489 } 1490 } 1491 1492 // Back-compat: old sanitize and auth callbacks are applied to all of an object type. 1493 if ( is_callable( $args['sanitize_callback'] ) ) { 1494 if ( ! empty( $object_subtype ) ) { 1495 add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 ); 1496 } else { 1497 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); 1498 } 1499 } 1500 1501 if ( is_callable( $args['auth_callback'] ) ) { 1502 if ( ! empty( $object_subtype ) ) { 1503 add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 ); 1504 } else { 1505 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1506 } 1507 } 1508 1509 if ( array_key_exists( 'default', $args ) ) { 1510 $schema = $args; 1511 if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) { 1512 $schema = array_merge( $schema, $args['show_in_rest']['schema'] ); 1513 } 1514 1515 $check = rest_validate_value_from_schema( $args['default'], $schema ); 1516 if ( is_wp_error( $check ) ) { 1517 _doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' ); 1518 1519 return false; 1520 } 1521 1522 if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) { 1523 add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 ); 1524 } 1525 } 1526 1527 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1528 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1529 unset( $args['object_subtype'] ); 1530 1531 $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args; 1532 1533 return true; 1534 } 1535 1536 return false; 1537 } 1538 1539 /** 1540 * Filters into default_{$object_type}_metadata and adds in default value. 1541 * 1542 * @since 5.5.0 1543 * 1544 * @param mixed $value Current value passed to filter. 1545 * @param int $object_id ID of the object metadata is for. 1546 * @param string $meta_key Metadata key. 1547 * @param bool $single If true, return only the first value of the specified `$meta_key`. 1548 * This parameter has no effect if `$meta_key` is not specified. 1549 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1550 * or any other object type with an associated meta table. 1551 * @return mixed An array of default values if `$single` is false. 1552 * The default value of the meta field if `$single` is true. 1553 */ 1554 function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) { 1555 global $wp_meta_keys; 1556 1557 if ( wp_installing() ) { 1558 return $value; 1559 } 1560 1561 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) { 1562 return $value; 1563 } 1564 1565 $defaults = array(); 1566 foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) { 1567 foreach ( $meta_data as $_meta_key => $args ) { 1568 if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) { 1569 $defaults[ $sub_type ] = $args; 1570 } 1571 } 1572 } 1573 1574 if ( ! $defaults ) { 1575 return $value; 1576 } 1577 1578 // If this meta type does not have subtypes, then the default is keyed as an empty string. 1579 if ( isset( $defaults[''] ) ) { 1580 $metadata = $defaults['']; 1581 } else { 1582 $sub_type = get_object_subtype( $meta_type, $object_id ); 1583 if ( ! isset( $defaults[ $sub_type ] ) ) { 1584 return $value; 1585 } 1586 $metadata = $defaults[ $sub_type ]; 1587 } 1588 1589 if ( $single ) { 1590 $value = $metadata['default']; 1591 } else { 1592 $value = array( $metadata['default'] ); 1593 } 1594 1595 return $value; 1596 } 1597 1598 /** 1599 * Checks if a meta key is registered. 1600 * 1601 * @since 4.6.0 1602 * @since 4.9.8 The `$object_subtype` parameter was added. 1603 * 1604 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1605 * or any other object type with an associated meta table. 1606 * @param string $meta_key Metadata key. 1607 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. 1608 * @return bool True if the meta key is registered to the object type and, if provided, 1609 * the object subtype. False if not. 1610 */ 1611 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { 1612 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); 1613 1614 return isset( $meta_keys[ $meta_key ] ); 1615 } 1616 1617 /** 1618 * Unregisters a meta key from the list of registered keys. 1619 * 1620 * @since 4.6.0 1621 * @since 4.9.8 The `$object_subtype` parameter was added. 1622 * 1623 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1624 * or any other object type with an associated meta table. 1625 * @param string $meta_key Metadata key. 1626 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. 1627 * @return bool True if successful. False if the meta key was not registered. 1628 */ 1629 function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { 1630 global $wp_meta_keys; 1631 1632 if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1633 return false; 1634 } 1635 1636 $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ]; 1637 1638 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 1639 if ( ! empty( $object_subtype ) ) { 1640 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] ); 1641 } else { 1642 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); 1643 } 1644 } 1645 1646 if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) { 1647 if ( ! empty( $object_subtype ) ) { 1648 remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] ); 1649 } else { 1650 remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] ); 1651 } 1652 } 1653 1654 unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ); 1655 1656 // Do some clean up. 1657 if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1658 unset( $wp_meta_keys[ $object_type ][ $object_subtype ] ); 1659 } 1660 if ( empty( $wp_meta_keys[ $object_type ] ) ) { 1661 unset( $wp_meta_keys[ $object_type ] ); 1662 } 1663 1664 return true; 1665 } 1666 1667 /** 1668 * Retrieves a list of registered metadata args for an object type, keyed by their meta keys. 1669 * 1670 * @since 4.6.0 1671 * @since 4.9.8 The `$object_subtype` parameter was added. 1672 * 1673 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1674 * or any other object type with an associated meta table. 1675 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. 1676 * @return array[] List of registered metadata args, keyed by their meta keys. 1677 */ 1678 function get_registered_meta_keys( $object_type, $object_subtype = '' ) { 1679 global $wp_meta_keys; 1680 1681 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1682 return array(); 1683 } 1684 1685 return $wp_meta_keys[ $object_type ][ $object_subtype ]; 1686 } 1687 1688 /** 1689 * Retrieves registered metadata for a specified object. 1690 * 1691 * The results include both meta that is registered specifically for the 1692 * object's subtype and meta that is registered for the entire object type. 1693 * 1694 * @since 4.6.0 1695 * 1696 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1697 * or any other object type with an associated meta table. 1698 * @param int $object_id ID of the object the metadata is for. 1699 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered 1700 * metadata for the specified object. 1701 * @return mixed A single value or array of values for a key if specified. An array of all registered keys 1702 * and values for an object ID if not. False if a given $meta_key is not registered. 1703 */ 1704 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { 1705 $object_subtype = get_object_subtype( $object_type, $object_id ); 1706 1707 if ( ! empty( $meta_key ) ) { 1708 if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1709 $object_subtype = ''; 1710 } 1711 1712 if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1713 return false; 1714 } 1715 1716 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); 1717 $meta_key_data = $meta_keys[ $meta_key ]; 1718 1719 $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] ); 1720 1721 return $data; 1722 } 1723 1724 $data = get_metadata( $object_type, $object_id ); 1725 if ( ! $data ) { 1726 return array(); 1727 } 1728 1729 $meta_keys = get_registered_meta_keys( $object_type ); 1730 if ( ! empty( $object_subtype ) ) { 1731 $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) ); 1732 } 1733 1734 return array_intersect_key( $data, $meta_keys ); 1735 } 1736 1737 /** 1738 * Filters out `register_meta()` args based on an allowed list. 1739 * 1740 * `register_meta()` args may change over time, so requiring the allowed list 1741 * to be explicitly turned off is a warranty seal of sorts. 1742 * 1743 * @access private 1744 * @since 5.5.0 1745 * 1746 * @param array $args Arguments from `register_meta()`. 1747 * @param array $default_args Default arguments for `register_meta()`. 1748 * @return array Filtered arguments. 1749 */ 1750 function _wp_register_meta_args_allowed_list( $args, $default_args ) { 1751 return array_intersect_key( $args, $default_args ); 1752 } 1753 1754 /** 1755 * Returns the object subtype for a given object ID of a specific type. 1756 * 1757 * @since 4.9.8 1758 * 1759 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user', 1760 * or any other object type with an associated meta table. 1761 * @param int $object_id ID of the object to retrieve its subtype. 1762 * @return string The object subtype or an empty string if unspecified subtype. 1763 */ 1764 function get_object_subtype( $object_type, $object_id ) { 1765 $object_id = (int) $object_id; 1766 $object_subtype = ''; 1767 1768 switch ( $object_type ) { 1769 case 'post': 1770 $post_type = get_post_type( $object_id ); 1771 1772 if ( ! empty( $post_type ) ) { 1773 $object_subtype = $post_type; 1774 } 1775 break; 1776 1777 case 'term': 1778 $term = get_term( $object_id ); 1779 if ( ! $term instanceof WP_Term ) { 1780 break; 1781 } 1782 1783 $object_subtype = $term->taxonomy; 1784 break; 1785 1786 case 'comment': 1787 $comment = get_comment( $object_id ); 1788 if ( ! $comment ) { 1789 break; 1790 } 1791 1792 $object_subtype = 'comment'; 1793 break; 1794 1795 case 'user': 1796 $user = get_user_by( 'id', $object_id ); 1797 if ( ! $user ) { 1798 break; 1799 } 1800 1801 $object_subtype = 'user'; 1802 break; 1803 } 1804 1805 /** 1806 * Filters the object subtype identifier for a non-standard object type. 1807 * 1808 * The dynamic portion of the hook name, `$object_type`, refers to the meta object type 1809 * (post, comment, term, user, or any other type with an associated meta table). 1810 * 1811 * Possible hook names include: 1812 * 1813 * - `get_object_subtype_post` 1814 * - `get_object_subtype_comment` 1815 * - `get_object_subtype_term` 1816 * - `get_object_subtype_user` 1817 * 1818 * @since 4.9.8 1819 * 1820 * @param string $object_subtype Empty string to override. 1821 * @param int $object_id ID of the object to get the subtype for. 1822 */ 1823 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1824 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Nov 21 08:20:01 2024 | Cross-referenced by PHPXref |