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