| [ 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. Pass 0 when `$delete_all` is true. 386 * The value no longer selects the rows to delete, but is still passed 387 * on to the hooks fired below. 388 * @param string $meta_key Metadata key. 389 * @param mixed $meta_value Optional. Metadata value. Must be serializable if non-scalar. 390 * If specified, only delete metadata entries with this value. 391 * Otherwise, delete all entries with the specified meta_key. 392 * Pass `null`, `false`, or an empty string to skip this check. 393 * (For backward compatibility, it is not possible to pass an empty string 394 * to delete those entries with an empty string for a value.) 395 * Default empty string. 396 * @param bool $delete_all Optional. If true, delete matching metadata entries for all objects, 397 * ignoring the specified object_id. Otherwise, only delete 398 * matching metadata entries for the specified object_id. Default false. 399 * @return bool True on successful delete, false on failure. 400 * 401 * @phpstan-param non-negative-int $object_id 402 */ 403 function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) { 404 global $wpdb; 405 406 if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) { 407 return false; 408 } 409 410 $object_id = absint( $object_id ); 411 if ( ! $object_id && ! $delete_all ) { 412 return false; 413 } 414 415 $table = _get_meta_table( $meta_type ); 416 if ( ! $table ) { 417 return false; 418 } 419 420 $type_column = sanitize_key( $meta_type . '_id' ); 421 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 422 423 // expected_slashed ($meta_key) 424 $meta_key = wp_unslash( $meta_key ); 425 $meta_value = wp_unslash( $meta_value ); 426 427 /** 428 * Short-circuits deleting metadata of a specific type. 429 * 430 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 431 * (blog, post, comment, term, user, or any other type with an associated meta table). 432 * Returning a non-null value will effectively short-circuit the function. 433 * 434 * Possible hook names include: 435 * 436 * - `delete_blog_metadata` 437 * - `delete_post_metadata` 438 * - `delete_comment_metadata` 439 * - `delete_term_metadata` 440 * - `delete_user_metadata` 441 * 442 * @since 3.1.0 443 * 444 * @param null|bool $delete Whether to allow metadata deletion of the given type. 445 * @param int $object_id ID of the object metadata is for. 446 * @param string $meta_key Metadata key. 447 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 448 * @param bool $delete_all Whether to delete the matching metadata entries 449 * for all objects, ignoring the specified $object_id. 450 * Default false. 451 */ 452 $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all ); 453 if ( null !== $check ) { 454 return (bool) $check; 455 } 456 457 $_meta_value = $meta_value; 458 $meta_value = maybe_serialize( $meta_value ); 459 460 $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key ); 461 462 if ( ! $delete_all ) { 463 $query .= $wpdb->prepare( " AND $type_column = %d", $object_id ); 464 } 465 466 if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) { 467 $query .= $wpdb->prepare( ' AND meta_value = %s', $meta_value ); 468 } 469 470 $meta_ids = $wpdb->get_col( $query ); 471 if ( ! count( $meta_ids ) ) { 472 return false; 473 } 474 475 if ( $delete_all ) { 476 if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) { 477 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) ); 478 } else { 479 $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) ); 480 } 481 } 482 483 /** 484 * Fires immediately before deleting metadata of a specific type. 485 * 486 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 487 * (blog, post, comment, term, user, or any other type with an associated meta table). 488 * 489 * Possible hook names include: 490 * 491 * - `delete_blog_meta` 492 * - `delete_post_meta` 493 * - `delete_comment_meta` 494 * - `delete_term_meta` 495 * - `delete_user_meta` 496 * 497 * @since 3.1.0 498 * 499 * @param string[] $meta_ids An array of metadata entry IDs to delete. 500 * @param int $object_id ID of the object metadata is for. 501 * @param string $meta_key Metadata key. 502 * @param mixed $_meta_value Metadata value. 503 */ 504 do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 505 506 // Old-style action. 507 if ( 'post' === $meta_type ) { 508 /** 509 * Fires immediately before deleting metadata for a post. 510 * 511 * @since 2.9.0 512 * 513 * @param string[] $meta_ids An array of metadata entry IDs to delete. 514 */ 515 do_action( 'delete_postmeta', $meta_ids ); 516 } 517 518 $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )'; 519 520 $count = $wpdb->query( $query ); 521 522 if ( ! $count ) { 523 return false; 524 } 525 526 if ( $delete_all ) { 527 $data = (array) $object_ids; 528 } else { 529 $data = array( $object_id ); 530 } 531 wp_cache_delete_multiple( $data, $meta_type . '_meta' ); 532 533 /** 534 * Fires immediately after deleting metadata of a specific type. 535 * 536 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 537 * (blog, post, comment, term, user, or any other type with an associated meta table). 538 * 539 * Possible hook names include: 540 * 541 * - `deleted_blog_meta` 542 * - `deleted_post_meta` 543 * - `deleted_comment_meta` 544 * - `deleted_term_meta` 545 * - `deleted_user_meta` 546 * 547 * @since 2.9.0 548 * 549 * @param string[] $meta_ids An array of metadata entry IDs to delete. 550 * @param int $object_id ID of the object metadata is for. 551 * @param string $meta_key Metadata key. 552 * @param mixed $_meta_value Metadata value. 553 */ 554 do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value ); 555 556 // Old-style action. 557 if ( 'post' === $meta_type ) { 558 /** 559 * Fires immediately after deleting metadata for a post. 560 * 561 * @since 2.9.0 562 * 563 * @param string[] $meta_ids An array of metadata entry IDs to delete. 564 */ 565 do_action( 'deleted_postmeta', $meta_ids ); 566 } 567 568 return true; 569 } 570 571 /** 572 * Retrieves the value of a metadata field for the specified object type and ID. 573 * 574 * If the meta field exists, a single value is returned if `$single` is true, 575 * or an array of values if it's false. 576 * 577 * If the meta field does not exist, the result depends on get_metadata_default(). 578 * By default, an empty string is returned if `$single` is true, or an empty array 579 * if it's false. 580 * 581 * @since 2.9.0 582 * 583 * @see get_metadata_raw() 584 * @see get_metadata_default() 585 * 586 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 587 * 'user', or any other object type with an associated meta table. 588 * @param int $object_id ID of the object metadata is for. 589 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 590 * the specified object. Default empty string. 591 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. 592 * This parameter has no effect if `$meta_key` is not specified. Default false. 593 * @return mixed An array of values if `$single` is false. 594 * The value of the meta field if `$single` is true. 595 * False for an invalid `$object_id` (non-numeric, zero, or negative value), 596 * or if `$meta_type` is not specified. 597 * An empty array if a valid but non-existing object ID is passed and `$single` is false. 598 * An empty string if a valid but non-existing object ID is passed and `$single` is true. 599 * The same empty array or empty string if `$meta_type` has no metadata table, in which 600 * case there is no cache to return even when `$meta_key` is not specified. 601 * Note: Non-serialized values are returned as strings: 602 * - false values are returned as empty strings ('') 603 * - true values are returned as '1' 604 * - numbers (both integer and float) are returned as strings 605 * Arrays and objects retain their original type. 606 * These conversions apply to stored values. A default value registered 607 * with {@see register_meta()} is never stored, so it is returned with 608 * the type it was registered with, which may be an integer, float, or 609 * boolean. 610 * 611 * @phpstan-param int|numeric-string $object_id 612 * @phpstan-return ( 613 * $meta_key is ''|'0' 614 * ? ( $single is true 615 * ? array<array-key, list<string>>|string|false 616 * : array<array-key, list<string>>|false ) 617 * : ( $single is true 618 * ? mixed 619 * : list<mixed>|false ) 620 * ) 621 */ 622 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) { 623 $value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single ); 624 if ( ! is_null( $value ) ) { 625 return $value; 626 } 627 628 return get_metadata_default( $meta_type, $object_id, $meta_key, $single ); 629 } 630 631 /** 632 * Retrieves raw metadata value for the specified object. 633 * 634 * @since 5.5.0 635 * @since 7.2.0 A cached value that is not an array is now treated as a cache miss. 636 * 637 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 638 * 'user', or any other object type with an associated meta table. 639 * @param int $object_id ID of the object metadata is for. 640 * @param string $meta_key Optional. Metadata key. If not specified, retrieve all metadata for 641 * the specified object. Default empty string. 642 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. 643 * This parameter has no effect if `$meta_key` is not specified. Default false. 644 * @return mixed An array of values if `$single` is false. 645 * The value of the meta field if `$single` is true. 646 * False for an invalid `$object_id` (non-numeric, zero, or negative value), 647 * or if `$meta_type` is not specified. 648 * Null if the value does not exist. 649 * Only stored values are returned. Unlike {@see get_metadata()}, a default 650 * registered with {@see register_meta()} is never consulted, so a value is 651 * always a string unless it was stored serialized, in which case the array 652 * or object retains its original type. 653 * When `$meta_key` is not specified, the values are returned exactly as 654 * they are held in the object cache, which means they are still serialized. 655 * 656 * @phpstan-param int|numeric-string $object_id 657 * @phpstan-return ( 658 * $meta_key is ''|'0' 659 * ? array<array-key, list<string>>|false|null 660 * : ( $single is true 661 * ? string|array<mixed>|object|false|null 662 * : list<string|array<mixed>|object>|false|null ) 663 * ) 664 */ 665 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) { 666 if ( ! $meta_type || ! is_numeric( $object_id ) ) { 667 return false; 668 } 669 670 $object_id = absint( $object_id ); 671 if ( ! $object_id ) { 672 return false; 673 } 674 675 /** 676 * Short-circuits the return value of a meta field. 677 * 678 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 679 * (blog, post, comment, term, user, or any other type with an associated meta table). 680 * Returning a non-null value will effectively short-circuit the function. 681 * 682 * Possible filter names include: 683 * 684 * - `get_blog_metadata` 685 * - `get_post_metadata` 686 * - `get_comment_metadata` 687 * - `get_term_metadata` 688 * - `get_user_metadata` 689 * 690 * @since 3.1.0 691 * @since 5.5.0 Added the `$meta_type` parameter. 692 * 693 * @param mixed $value The value to return, either a single metadata value or an array 694 * of values depending on the value of `$single`. Default null. 695 * @param int $object_id ID of the object metadata is for. 696 * @param string $meta_key Metadata key. 697 * @param bool $single Whether to return only the first value of the specified `$meta_key`. 698 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 699 * 'user', or any other object type with an associated meta table. 700 */ 701 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type ); 702 if ( null !== $check ) { 703 if ( $single && is_array( $check ) ) { 704 return $check[0]; 705 } else { 706 return $check; 707 } 708 } 709 710 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); 711 712 // A cached value that is not an array is unusable, treat it as a cache miss. 713 if ( ! $meta_cache || ! is_array( $meta_cache ) ) { 714 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); 715 $meta_cache = $meta_cache[ $object_id ] ?? null; 716 } 717 718 if ( ! $meta_key ) { 719 return $meta_cache; 720 } 721 722 if ( isset( $meta_cache[ $meta_key ] ) ) { 723 if ( $single ) { 724 return maybe_unserialize( $meta_cache[ $meta_key ][0] ); 725 } else { 726 return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] ); 727 } 728 } 729 730 return null; 731 } 732 733 /** 734 * Retrieves default metadata value for the specified meta key and object. 735 * 736 * By default, an empty string is returned if `$single` is true, or an empty array 737 * if it's false. 738 * 739 * @since 5.5.0 740 * 741 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 742 * 'user', or any other object type with an associated meta table. 743 * @param int $object_id ID of the object metadata is for. 744 * @param string $meta_key Metadata key. 745 * @param bool $single Optional. If true, return only the first value of the specified `$meta_key`. 746 * This parameter has no effect if `$meta_key` is not specified. Default false. 747 * @return mixed An array of default values if `$single` is false. 748 * The default value of the meta field if `$single` is true. 749 * 750 * @phpstan-param int|numeric-string $object_id 751 * @phpstan-return ( $single is true ? mixed : list<mixed> ) 752 */ 753 function get_metadata_default( $meta_type, $object_id, $meta_key, $single = false ) { 754 if ( $single ) { 755 $value = ''; 756 } else { 757 $value = array(); 758 } 759 760 /** 761 * Filters the default metadata value for a specified meta key and object. 762 * 763 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 764 * (blog, post, comment, term, user, or any other type with an associated meta table). 765 * 766 * Possible filter names include: 767 * 768 * - `default_blog_metadata` 769 * - `default_post_metadata` 770 * - `default_comment_metadata` 771 * - `default_term_metadata` 772 * - `default_user_metadata` 773 * 774 * @since 5.5.0 775 * 776 * @param mixed $value The value to return, either a single metadata value or an array 777 * of values depending on the value of `$single`. 778 * @param int $object_id ID of the object metadata is for. 779 * @param string $meta_key Metadata key. 780 * @param bool $single Whether to return only the first value of the specified `$meta_key`. 781 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 782 * 'user', or any other object type with an associated meta table. 783 */ 784 $value = apply_filters( "default_{$meta_type}_metadata", $value, $object_id, $meta_key, $single, $meta_type ); 785 786 if ( ! $single && ! wp_is_numeric_array( $value ) ) { 787 $value = array( $value ); 788 } 789 790 return $value; 791 } 792 793 /** 794 * Determines if a meta field with the given key exists for the given object ID. 795 * 796 * @since 3.3.0 797 * @since 7.2.0 A cached value that is not an array is now treated as a cache miss. 798 * 799 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 800 * 'user', or any other object type with an associated meta table. 801 * @param int $object_id ID of the object metadata is for. 802 * @param string $meta_key Metadata key. 803 * @return bool Whether a meta field with the given key exists. 804 */ 805 function metadata_exists( $meta_type, $object_id, $meta_key ) { 806 if ( ! $meta_type || ! is_numeric( $object_id ) ) { 807 return false; 808 } 809 810 $object_id = absint( $object_id ); 811 if ( ! $object_id ) { 812 return false; 813 } 814 815 /** This filter is documented in wp-includes/meta.php */ 816 $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true, $meta_type ); 817 if ( null !== $check ) { 818 return (bool) $check; 819 } 820 821 $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); 822 823 // A cached value that is not an array is unusable, treat it as a cache miss. 824 if ( ! $meta_cache || ! is_array( $meta_cache ) ) { 825 $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); 826 $meta_cache = $meta_cache[ $object_id ] ?? null; 827 } 828 829 if ( isset( $meta_cache[ $meta_key ] ) ) { 830 return true; 831 } 832 833 return false; 834 } 835 836 /** 837 * Retrieves metadata by meta ID. 838 * 839 * @since 3.3.0 840 * 841 * @global wpdb $wpdb WordPress database abstraction object. 842 * 843 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 844 * 'user', or any other object type with an associated meta table. 845 * @param int $meta_id ID for a specific meta row. 846 * @return stdClass|false { 847 * Metadata object, or boolean `false` if the metadata doesn't exist. 848 * 849 * @type string $meta_key The meta key. 850 * @type mixed $meta_value The unserialized meta value. 851 * @type string $meta_id Optional. The meta ID when the meta type is any value except 'user'. 852 * @type string $umeta_id Optional. The meta ID when the meta type is 'user'. 853 * @type string $blog_id Optional. The object ID when the meta type is 'blog'. 854 * @type string $post_id Optional. The object ID when the meta type is 'post'. 855 * @type string $comment_id Optional. The object ID when the meta type is 'comment'. 856 * @type string $term_id Optional. The object ID when the meta type is 'term'. 857 * @type string $user_id Optional. The object ID when the meta type is 'user'. 858 * } 859 */ 860 function get_metadata_by_mid( $meta_type, $meta_id ) { 861 global $wpdb; 862 863 if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { 864 return false; 865 } 866 867 $meta_id = (int) $meta_id; 868 if ( $meta_id <= 0 ) { 869 return false; 870 } 871 872 $table = _get_meta_table( $meta_type ); 873 if ( ! $table ) { 874 return false; 875 } 876 877 /** 878 * Short-circuits the return value when fetching a meta field by meta ID. 879 * 880 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 881 * (blog, post, comment, term, user, or any other type with an associated meta table). 882 * Returning a non-null value will effectively short-circuit the function. 883 * 884 * Possible hook names include: 885 * 886 * - `get_blog_metadata_by_mid` 887 * - `get_post_metadata_by_mid` 888 * - `get_comment_metadata_by_mid` 889 * - `get_term_metadata_by_mid` 890 * - `get_user_metadata_by_mid` 891 * 892 * @since 5.0.0 893 * 894 * @param stdClass|null $value The value to return. 895 * @param int $meta_id Meta ID. 896 */ 897 $check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id ); 898 if ( null !== $check ) { 899 return $check; 900 } 901 902 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 903 904 $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); 905 906 if ( empty( $meta ) ) { 907 return false; 908 } 909 910 if ( isset( $meta->meta_value ) ) { 911 $meta->meta_value = maybe_unserialize( $meta->meta_value ); 912 } 913 914 return $meta; 915 } 916 917 /** 918 * Updates metadata by meta ID. 919 * 920 * @since 3.3.0 921 * 922 * @global wpdb $wpdb WordPress database abstraction object. 923 * 924 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 925 * 'user', or any other object type with an associated meta table. 926 * @param int $meta_id ID for a specific meta row. 927 * @param string $meta_value Metadata value. Must be serializable if non-scalar. 928 * @param string|false $meta_key Optional. You can provide a meta key to update it. Default false. 929 * @return bool True on successful update, false on failure. 930 */ 931 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) { 932 global $wpdb; 933 934 // Make sure everything is valid. 935 if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { 936 return false; 937 } 938 939 $meta_id = (int) $meta_id; 940 if ( $meta_id <= 0 ) { 941 return false; 942 } 943 944 $table = _get_meta_table( $meta_type ); 945 if ( ! $table ) { 946 return false; 947 } 948 949 $column = sanitize_key( $meta_type . '_id' ); 950 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 951 952 /** 953 * Short-circuits updating metadata of a specific type by meta ID. 954 * 955 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 956 * (blog, post, comment, term, user, or any other type with an associated meta table). 957 * Returning a non-null value will effectively short-circuit the function. 958 * 959 * Possible hook names include: 960 * 961 * - `update_blog_metadata_by_mid` 962 * - `update_post_metadata_by_mid` 963 * - `update_comment_metadata_by_mid` 964 * - `update_term_metadata_by_mid` 965 * - `update_user_metadata_by_mid` 966 * 967 * @since 5.0.0 968 * 969 * @param null|bool $check Whether to allow updating metadata for the given type. 970 * @param int $meta_id Meta ID. 971 * @param mixed $meta_value Meta value. Must be serializable if non-scalar. 972 * @param string|false $meta_key Meta key, if provided. 973 */ 974 $check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key ); 975 if ( null !== $check ) { 976 return (bool) $check; 977 } 978 979 // Fetch the meta and go on if it's found. 980 $meta = get_metadata_by_mid( $meta_type, $meta_id ); 981 if ( $meta ) { 982 $original_key = $meta->meta_key; 983 $object_id = $meta->{$column}; 984 985 /* 986 * If a new meta_key (last parameter) was specified, change the meta key, 987 * otherwise use the original key in the update statement. 988 */ 989 if ( false === $meta_key ) { 990 $meta_key = $original_key; 991 } elseif ( ! is_string( $meta_key ) ) { 992 return false; 993 } 994 995 $meta_subtype = get_object_subtype( $meta_type, $object_id ); 996 997 // Sanitize the meta. 998 $_meta_value = $meta_value; 999 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); 1000 $meta_value = maybe_serialize( $meta_value ); 1001 1002 // Format the data query arguments. 1003 $data = array( 1004 'meta_key' => $meta_key, 1005 'meta_value' => $meta_value, 1006 ); 1007 1008 // Format the where query arguments. 1009 $where = array(); 1010 $where[ $id_column ] = $meta_id; 1011 1012 /** This action is documented in wp-includes/meta.php */ 1013 do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 1014 1015 if ( 'post' === $meta_type ) { 1016 /** This action is documented in wp-includes/meta.php */ 1017 do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 1018 } 1019 1020 // Run the update query, all fields in $data are %s, $where is a %d. 1021 $result = $wpdb->update( $table, $data, $where, '%s', '%d' ); 1022 if ( ! $result ) { 1023 return false; 1024 } 1025 1026 // Clear the caches. 1027 wp_cache_delete( $object_id, $meta_type . '_meta' ); 1028 1029 /** This action is documented in wp-includes/meta.php */ 1030 do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value ); 1031 1032 if ( 'post' === $meta_type ) { 1033 /** This action is documented in wp-includes/meta.php */ 1034 do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value ); 1035 } 1036 1037 return true; 1038 } 1039 1040 // And if the meta was not found. 1041 return false; 1042 } 1043 1044 /** 1045 * Deletes metadata by meta ID. 1046 * 1047 * @since 3.3.0 1048 * 1049 * @global wpdb $wpdb WordPress database abstraction object. 1050 * 1051 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1052 * 'user', or any other object type with an associated meta table. 1053 * @param int $meta_id ID for a specific meta row. 1054 * @return bool True on successful delete, false on failure. 1055 */ 1056 function delete_metadata_by_mid( $meta_type, $meta_id ) { 1057 global $wpdb; 1058 1059 // Make sure everything is valid. 1060 if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { 1061 return false; 1062 } 1063 1064 $meta_id = (int) $meta_id; 1065 if ( $meta_id <= 0 ) { 1066 return false; 1067 } 1068 1069 $table = _get_meta_table( $meta_type ); 1070 if ( ! $table ) { 1071 return false; 1072 } 1073 1074 // Object and ID columns. 1075 $column = sanitize_key( $meta_type . '_id' ); 1076 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 1077 1078 /** 1079 * Short-circuits deleting metadata of a specific type by meta ID. 1080 * 1081 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 1082 * (blog, post, comment, term, user, or any other type with an associated meta table). 1083 * Returning a non-null value will effectively short-circuit the function. 1084 * 1085 * Possible hook names include: 1086 * 1087 * - `delete_blog_metadata_by_mid` 1088 * - `delete_post_metadata_by_mid` 1089 * - `delete_comment_metadata_by_mid` 1090 * - `delete_term_metadata_by_mid` 1091 * - `delete_user_metadata_by_mid` 1092 * 1093 * @since 5.0.0 1094 * 1095 * @param null|bool $delete Whether to allow metadata deletion of the given type. 1096 * @param int $meta_id Meta ID. 1097 */ 1098 $check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id ); 1099 if ( null !== $check ) { 1100 return (bool) $check; 1101 } 1102 1103 // Fetch the meta and go on if it's found. 1104 $meta = get_metadata_by_mid( $meta_type, $meta_id ); 1105 if ( $meta ) { 1106 $object_id = (int) $meta->{$column}; 1107 1108 /** This action is documented in wp-includes/meta.php */ 1109 do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); 1110 1111 // Old-style action. 1112 if ( 'post' === $meta_type || 'comment' === $meta_type ) { 1113 /** 1114 * Fires immediately before deleting post or comment metadata of a specific type. 1115 * 1116 * The dynamic portion of the hook name, `$meta_type`, refers to the meta 1117 * object type (post or comment). 1118 * 1119 * Possible hook names include: 1120 * 1121 * - `delete_postmeta` 1122 * - `delete_commentmeta` 1123 * 1124 * @since 3.4.0 1125 * 1126 * @param int $meta_id ID of the metadata entry to delete. 1127 */ 1128 do_action( "delete_{$meta_type}meta", $meta_id ); 1129 } 1130 1131 // Run the query, will return true if deleted, false otherwise. 1132 $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) ); 1133 1134 // Clear the caches. 1135 wp_cache_delete( $object_id, $meta_type . '_meta' ); 1136 1137 /** This action is documented in wp-includes/meta.php */ 1138 do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value ); 1139 1140 // Old-style action. 1141 if ( 'post' === $meta_type || 'comment' === $meta_type ) { 1142 /** 1143 * Fires immediately after deleting post or comment metadata of a specific type. 1144 * 1145 * The dynamic portion of the hook name, `$meta_type`, refers to the meta 1146 * object type (post or comment). 1147 * 1148 * Possible hook names include: 1149 * 1150 * - `deleted_postmeta` 1151 * - `deleted_commentmeta` 1152 * 1153 * @since 3.4.0 1154 * 1155 * @param int $meta_id Deleted metadata entry ID. 1156 */ 1157 do_action( "deleted_{$meta_type}meta", $meta_id ); 1158 } 1159 1160 return $result; 1161 1162 } 1163 1164 // Meta ID was not found. 1165 return false; 1166 } 1167 1168 /** 1169 * Updates the metadata cache for the specified objects. 1170 * 1171 * @since 2.9.0 1172 * @since 7.2.0 A cached value that is not an array is now treated as a cache miss. 1173 * 1174 * @global wpdb $wpdb WordPress database abstraction object. 1175 * 1176 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1177 * 'user', or any other object type with an associated meta table. 1178 * @param string|int[] $object_ids Array or comma delimited list of object IDs to update cache for. 1179 * @return array|false Metadata cache for the specified objects, or false on failure. 1180 */ 1181 function update_meta_cache( $meta_type, $object_ids ) { 1182 global $wpdb; 1183 1184 if ( ! $meta_type || ! $object_ids ) { 1185 return false; 1186 } 1187 1188 $table = _get_meta_table( $meta_type ); 1189 if ( ! $table ) { 1190 return false; 1191 } 1192 1193 $column = sanitize_key( $meta_type . '_id' ); 1194 1195 if ( ! is_array( $object_ids ) ) { 1196 $object_ids = preg_replace( '|[^0-9,]|', '', $object_ids ); 1197 $object_ids = explode( ',', $object_ids ); 1198 } 1199 1200 $object_ids = array_map( 'intval', $object_ids ); 1201 1202 /** 1203 * Short-circuits updating the metadata cache of a specific type. 1204 * 1205 * The dynamic portion of the hook name, `$meta_type`, refers to the meta object type 1206 * (blog, post, comment, term, user, or any other type with an associated meta table). 1207 * Returning a non-null value will effectively short-circuit the function. 1208 * 1209 * Possible hook names include: 1210 * 1211 * - `update_blog_metadata_cache` 1212 * - `update_post_metadata_cache` 1213 * - `update_comment_metadata_cache` 1214 * - `update_term_metadata_cache` 1215 * - `update_user_metadata_cache` 1216 * 1217 * @since 5.0.0 1218 * 1219 * @param mixed $check Whether to allow updating the meta cache of the given type. 1220 * @param int[] $object_ids Array of object IDs to update the meta cache for. 1221 */ 1222 $check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids ); 1223 if ( null !== $check ) { 1224 return (bool) $check; 1225 } 1226 1227 $cache_group = $meta_type . '_meta'; 1228 $non_cached_ids = array(); 1229 $invalid_cache_ids = array(); 1230 $cache = array(); 1231 $cache_values = wp_cache_get_multiple( $object_ids, $cache_group ); 1232 1233 foreach ( $cache_values as $id => $cached_object ) { 1234 if ( false === $cached_object ) { 1235 $non_cached_ids[] = $id; 1236 } elseif ( ! is_array( $cached_object ) ) { 1237 // A cached value that is not an array is unusable, treat it as a cache miss. 1238 $non_cached_ids[] = $id; 1239 $invalid_cache_ids[] = $id; 1240 } else { 1241 $cache[ $id ] = $cached_object; 1242 } 1243 } 1244 1245 /* 1246 * Remove unusable cached values so that the regenerated values can be added. 1247 * A delete followed by an add is used instead of wp_cache_set_multiple() 1248 * so that wp_suspend_cache_addition() is still respected. 1249 */ 1250 if ( ! empty( $invalid_cache_ids ) ) { 1251 wp_cache_delete_multiple( $invalid_cache_ids, $cache_group ); 1252 } 1253 1254 if ( empty( $non_cached_ids ) ) { 1255 return $cache; 1256 } 1257 1258 // Get meta info. 1259 $id_list = implode( ',', $non_cached_ids ); 1260 $id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id'; 1261 1262 $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 ); 1263 1264 if ( ! empty( $meta_list ) ) { 1265 foreach ( $meta_list as $metarow ) { 1266 $mpid = (int) $metarow[ $column ]; 1267 $mkey = $metarow['meta_key']; 1268 $mval = $metarow['meta_value']; 1269 1270 // Force subkeys to be array type. 1271 if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) { 1272 $cache[ $mpid ] = array(); 1273 } 1274 if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) { 1275 $cache[ $mpid ][ $mkey ] = array(); 1276 } 1277 1278 // Add a value to the current pid/key. 1279 $cache[ $mpid ][ $mkey ][] = $mval; 1280 } 1281 } 1282 1283 $data = array(); 1284 foreach ( $non_cached_ids as $id ) { 1285 if ( ! isset( $cache[ $id ] ) ) { 1286 $cache[ $id ] = array(); 1287 } 1288 $data[ $id ] = $cache[ $id ]; 1289 } 1290 1291 wp_cache_add_multiple( $data, $cache_group ); 1292 1293 return $cache; 1294 } 1295 1296 /** 1297 * Retrieves the queue for lazy-loading metadata. 1298 * 1299 * @since 4.5.0 1300 * 1301 * @return WP_Metadata_Lazyloader Metadata lazyloader queue. 1302 */ 1303 function wp_metadata_lazyloader() { 1304 static $wp_metadata_lazyloader; 1305 1306 if ( null === $wp_metadata_lazyloader ) { 1307 $wp_metadata_lazyloader = new WP_Metadata_Lazyloader(); 1308 } 1309 1310 return $wp_metadata_lazyloader; 1311 } 1312 1313 /** 1314 * Given a meta query, generates SQL clauses to be appended to a main query. 1315 * 1316 * @since 3.2.0 1317 * 1318 * @see WP_Meta_Query 1319 * 1320 * @param array $meta_query A meta query. 1321 * @param string $type Type of meta. 1322 * @param string $primary_table Primary database table name. 1323 * @param string $primary_id_column Primary ID column name. 1324 * @param object $context Optional. The main query object. Default null. 1325 * @return string[]|false { 1326 * Array containing JOIN and WHERE SQL clauses to append to the main query, 1327 * or false if no table exists for the requested meta type. 1328 * 1329 * @type string $join SQL fragment to append to the main JOIN clause. 1330 * @type string $where SQL fragment to append to the main WHERE clause. 1331 * } 1332 */ 1333 function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) { 1334 $meta_query_obj = new WP_Meta_Query( $meta_query ); 1335 return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context ); 1336 } 1337 1338 /** 1339 * Retrieves the name of the metadata table for the specified object type. 1340 * 1341 * @since 2.9.0 1342 * 1343 * @global wpdb $wpdb WordPress database abstraction object. 1344 * 1345 * @param string $type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1346 * 'user', or any other object type with an associated meta table. 1347 * @return string|false Metadata table name, or false if no metadata table exists 1348 */ 1349 function _get_meta_table( $type ) { 1350 global $wpdb; 1351 1352 $table_name = $type . 'meta'; 1353 1354 if ( empty( $wpdb->$table_name ) ) { 1355 return false; 1356 } 1357 1358 return $wpdb->$table_name; 1359 } 1360 1361 /** 1362 * Determines whether a meta key is considered protected. 1363 * 1364 * @since 3.1.3 1365 * 1366 * @param string $meta_key Metadata key. 1367 * @param string $meta_type Optional. Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1368 * 'user', or any other object type with an associated meta table. Default empty string. 1369 * @return bool Whether the meta key is considered protected. 1370 */ 1371 function is_protected_meta( $meta_key, $meta_type = '' ) { 1372 $sanitized_key = preg_replace( "/[^\x20-\x7E\p{L}]/", '', $meta_key ); 1373 $protected = strlen( $sanitized_key ) > 0 && ( '_' === $sanitized_key[0] ); 1374 1375 /** 1376 * Filters whether a meta key is considered protected. 1377 * 1378 * @since 3.2.0 1379 * 1380 * @param bool $protected Whether the key is considered protected. 1381 * @param string $meta_key Metadata key. 1382 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1383 * 'user', or any other object type with an associated meta table. 1384 */ 1385 return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type ); 1386 } 1387 1388 /** 1389 * Sanitizes meta value. 1390 * 1391 * @since 3.1.3 1392 * @since 4.9.8 The `$object_subtype` parameter was added. 1393 * 1394 * @param string $meta_key Metadata key. 1395 * @param mixed $meta_value Metadata value to sanitize. 1396 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1397 * 'user', or any other object type with an associated meta table. 1398 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. 1399 * @return mixed Sanitized $meta_value. 1400 */ 1401 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { 1402 if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) { 1403 1404 /** 1405 * Filters the sanitization of a specific meta key of a specific meta type and subtype. 1406 * 1407 * The dynamic portions of the hook name, `$object_type`, `$meta_key`, 1408 * and `$object_subtype`, refer to the metadata object type (blog, comment, post, term, or user), 1409 * the meta key value, and the object subtype respectively. 1410 * 1411 * @since 4.9.8 1412 * 1413 * @param mixed $meta_value Metadata value to sanitize. 1414 * @param string $meta_key Metadata key. 1415 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1416 * 'user', or any other object type with an associated meta table. 1417 * @param string $object_subtype Object subtype. 1418 */ 1419 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype ); 1420 } 1421 1422 /** 1423 * Filters the sanitization of a specific meta key of a specific meta type. 1424 * 1425 * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`, 1426 * refer to the metadata object type (blog, comment, post, term, or user) and the meta 1427 * key value, respectively. 1428 * 1429 * @since 3.3.0 1430 * 1431 * @param mixed $meta_value Metadata value to sanitize. 1432 * @param string $meta_key Metadata key. 1433 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1434 * 'user', or any other object type with an associated meta table. 1435 */ 1436 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type ); 1437 } 1438 1439 /** 1440 * Registers a meta key. 1441 * 1442 * It is recommended to register meta keys for a specific combination of object type and object subtype. If passing 1443 * an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly 1444 * overridden in case a more specific meta key of the same name exists for the same object type and a subtype. 1445 * 1446 * If an object type does not support any subtypes, such as blogs, users, or comments, you should commonly call this function 1447 * without passing a subtype. 1448 * 1449 * @since 3.3.0 1450 * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified 1451 * to support an array of data to attach to registered meta keys}. Previous arguments for 1452 * `$sanitize_callback` and `$auth_callback` have been folded into this array. 1453 * @since 4.9.8 The `$object_subtype` argument was added to the arguments array. 1454 * @since 5.3.0 Valid meta types expanded to include "array" and "object". 1455 * @since 5.5.0 The `$default` argument was added to the arguments array. 1456 * @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array. 1457 * @since 6.7.0 The `label` argument was added to the arguments array. 1458 * 1459 * @global array $wp_meta_keys Global registry for meta keys. 1460 * 1461 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1462 * 'user', or any other object type with an associated meta table. 1463 * @param string $meta_key Meta key to register. 1464 * @param array $args { 1465 * Data used to describe the meta key when registered. 1466 * 1467 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, 1468 * the meta key will be registered on the entire object type. Default empty. 1469 * @type string $type The type of data associated with this meta key. 1470 * Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'. 1471 * @type string $label A human-readable label of the data attached to this meta key. 1472 * @type string $description A description of the data attached to this meta key. 1473 * @type bool $single Whether the meta key has one value per object, or an array of values per object. 1474 * @type mixed $default The default value returned from get_metadata() if no value has been set yet. 1475 * When using a non-single meta key, the default value is for the first entry. 1476 * In other words, when calling get_metadata() with `$single` set to `false`, 1477 * the default value given here will be wrapped in an array. 1478 * @type callable $sanitize_callback A function or method to call when sanitizing `$meta_key` data. 1479 * @type callable $auth_callback Optional. A function or method to call when performing edit_post_meta, 1480 * add_post_meta, and delete_post_meta capability checks. 1481 * @type bool|array $show_in_rest Whether data associated with this meta key can be considered public and 1482 * should be accessible via the REST API. A custom post type must also declare 1483 * support for custom fields for registered meta to be accessible via REST. 1484 * When registering complex meta values this argument may optionally be an 1485 * array with 'schema' or 'prepare_callback' keys instead of a boolean. 1486 * @type bool $revisions_enabled Whether to enable revisions support for this meta_key. Can only be used when the 1487 * object type is 'post'. 1488 * } 1489 * @param string|array $deprecated Deprecated. Use `$args` instead. 1490 * @return bool True if the meta key was successfully registered in the global array, false if not. 1491 * Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks, 1492 * but will not add to the global registry. 1493 */ 1494 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { 1495 global $wp_meta_keys; 1496 1497 if ( ! is_array( $wp_meta_keys ) ) { 1498 $wp_meta_keys = array(); 1499 } 1500 1501 $defaults = array( 1502 'object_subtype' => '', 1503 'type' => 'string', 1504 'label' => '', 1505 'description' => '', 1506 'default' => '', 1507 'single' => false, 1508 'sanitize_callback' => null, 1509 'auth_callback' => null, 1510 'show_in_rest' => false, 1511 'revisions_enabled' => false, 1512 ); 1513 1514 // There used to be individual args for sanitize and auth callbacks. 1515 $has_old_sanitize_cb = false; 1516 $has_old_auth_cb = false; 1517 1518 if ( is_callable( $args ) ) { 1519 $args = array( 1520 'sanitize_callback' => $args, 1521 ); 1522 1523 $has_old_sanitize_cb = true; 1524 } else { 1525 $args = (array) $args; 1526 } 1527 1528 if ( is_callable( $deprecated ) ) { 1529 $args['auth_callback'] = $deprecated; 1530 $has_old_auth_cb = true; 1531 } 1532 1533 /** 1534 * Filters the registration arguments when registering meta. 1535 * 1536 * @since 4.6.0 1537 * 1538 * @param array $args Array of meta registration arguments. 1539 * @param array $defaults Array of default arguments. 1540 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1541 * 'user', or any other object type with an associated meta table. 1542 * @param string $meta_key Meta key. 1543 */ 1544 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1545 unset( $defaults['default'] ); 1546 $args = wp_parse_args( $args, $defaults ); 1547 1548 // Require an item schema when registering array meta. 1549 if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) { 1550 if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) { 1551 _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' ); 1552 1553 return false; 1554 } 1555 } 1556 1557 $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : ''; 1558 if ( $args['revisions_enabled'] ) { 1559 if ( 'post' !== $object_type ) { 1560 _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object type supports revisions.' ), '6.4.0' ); 1561 1562 return false; 1563 } elseif ( ! empty( $object_subtype ) && ! post_type_supports( $object_subtype, 'revisions' ) ) { 1564 _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object subtype supports revisions.' ), '6.4.0' ); 1565 1566 return false; 1567 } 1568 } 1569 1570 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. 1571 if ( empty( $args['auth_callback'] ) ) { 1572 if ( is_protected_meta( $meta_key, $object_type ) ) { 1573 $args['auth_callback'] = '__return_false'; 1574 } else { 1575 $args['auth_callback'] = '__return_true'; 1576 } 1577 } 1578 1579 // Back-compat: old sanitize and auth callbacks are applied to all of an object type. 1580 if ( is_callable( $args['sanitize_callback'] ) ) { 1581 if ( ! empty( $object_subtype ) ) { 1582 add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 ); 1583 } else { 1584 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); 1585 } 1586 } 1587 1588 if ( is_callable( $args['auth_callback'] ) ) { 1589 if ( ! empty( $object_subtype ) ) { 1590 add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 ); 1591 } else { 1592 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1593 } 1594 } 1595 1596 if ( array_key_exists( 'default', $args ) ) { 1597 $schema = $args; 1598 if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) { 1599 $schema = array_merge( $schema, $args['show_in_rest']['schema'] ); 1600 } 1601 1602 $check = rest_validate_value_from_schema( $args['default'], $schema ); 1603 if ( is_wp_error( $check ) ) { 1604 _doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' ); 1605 1606 return false; 1607 } 1608 1609 if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) { 1610 add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 ); 1611 } 1612 } 1613 1614 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1615 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1616 unset( $args['object_subtype'] ); 1617 1618 $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args; 1619 1620 return true; 1621 } 1622 1623 return false; 1624 } 1625 1626 /** 1627 * Filters into default_{$object_type}_metadata and adds in default value. 1628 * 1629 * @since 5.5.0 1630 * 1631 * @global array $wp_meta_keys Global registry for meta keys. 1632 * 1633 * @param mixed $value Current value passed to filter. 1634 * @param int $object_id ID of the object metadata is for. 1635 * @param string $meta_key Metadata key. 1636 * @param bool $single If true, return only the first value of the specified `$meta_key`. 1637 * This parameter has no effect if `$meta_key` is not specified. 1638 * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1639 * 'user', or any other object type with an associated meta table. 1640 * @return mixed An array of default values if `$single` is false. 1641 * The default value of the meta field if `$single` is true. 1642 */ 1643 function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) { 1644 global $wp_meta_keys; 1645 1646 if ( wp_installing() ) { 1647 return $value; 1648 } 1649 1650 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) { 1651 return $value; 1652 } 1653 1654 $defaults = array(); 1655 foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) { 1656 foreach ( $meta_data as $_meta_key => $args ) { 1657 if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) { 1658 $defaults[ $sub_type ] = $args; 1659 } 1660 } 1661 } 1662 1663 if ( ! $defaults ) { 1664 return $value; 1665 } 1666 1667 // If this meta type does not have subtypes, then the default is keyed as an empty string. 1668 if ( isset( $defaults[''] ) ) { 1669 $metadata = $defaults['']; 1670 } else { 1671 $sub_type = get_object_subtype( $meta_type, $object_id ); 1672 if ( ! isset( $defaults[ $sub_type ] ) ) { 1673 return $value; 1674 } 1675 $metadata = $defaults[ $sub_type ]; 1676 } 1677 1678 if ( $single ) { 1679 $value = $metadata['default']; 1680 } else { 1681 $value = array( $metadata['default'] ); 1682 } 1683 1684 return $value; 1685 } 1686 1687 /** 1688 * Checks if a meta key is registered. 1689 * 1690 * @since 4.6.0 1691 * @since 4.9.8 The `$object_subtype` parameter was added. 1692 * 1693 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1694 * 'user', or any other object type with an associated meta table. 1695 * @param string $meta_key Metadata key. 1696 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. 1697 * @return bool True if the meta key is registered to the object type and, if provided, 1698 * the object subtype. False if not. 1699 */ 1700 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { 1701 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); 1702 1703 return isset( $meta_keys[ $meta_key ] ); 1704 } 1705 1706 /** 1707 * Unregisters a meta key from the list of registered keys. 1708 * 1709 * @since 4.6.0 1710 * @since 4.9.8 The `$object_subtype` parameter was added. 1711 * 1712 * @global array $wp_meta_keys Global registry for meta keys. 1713 * 1714 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1715 * 'user', or any other object type with an associated meta table. 1716 * @param string $meta_key Metadata key. 1717 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. 1718 * @return bool True if successful. False if the meta key was not registered. 1719 */ 1720 function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { 1721 global $wp_meta_keys; 1722 1723 if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1724 return false; 1725 } 1726 1727 $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ]; 1728 1729 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 1730 if ( ! empty( $object_subtype ) ) { 1731 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] ); 1732 } else { 1733 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); 1734 } 1735 } 1736 1737 if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) { 1738 if ( ! empty( $object_subtype ) ) { 1739 remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] ); 1740 } else { 1741 remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] ); 1742 } 1743 } 1744 1745 unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ); 1746 1747 // Do some clean up. 1748 if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1749 unset( $wp_meta_keys[ $object_type ][ $object_subtype ] ); 1750 } 1751 if ( empty( $wp_meta_keys[ $object_type ] ) ) { 1752 unset( $wp_meta_keys[ $object_type ] ); 1753 } 1754 1755 return true; 1756 } 1757 1758 /** 1759 * Retrieves a list of registered metadata args for an object type, keyed by their meta keys. 1760 * 1761 * @since 4.6.0 1762 * @since 4.9.8 The `$object_subtype` parameter was added. 1763 * 1764 * @global array $wp_meta_keys Global registry for meta keys. 1765 * 1766 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1767 * 'user', or any other object type with an associated meta table. 1768 * @param string $object_subtype Optional. The subtype of the object type. Default empty string. 1769 * @return array[] List of registered metadata args, keyed by their meta keys. 1770 */ 1771 function get_registered_meta_keys( $object_type, $object_subtype = '' ) { 1772 global $wp_meta_keys; 1773 1774 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1775 return array(); 1776 } 1777 1778 return $wp_meta_keys[ $object_type ][ $object_subtype ]; 1779 } 1780 1781 /** 1782 * Retrieves registered metadata for a specified object. 1783 * 1784 * The results include both meta that is registered specifically for the 1785 * object's subtype and meta that is registered for the entire object type. 1786 * 1787 * @since 4.6.0 1788 * 1789 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1790 * 'user', or any other object type with an associated meta table. 1791 * @param int $object_id ID of the object the metadata is for. 1792 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered 1793 * metadata for the specified object. 1794 * @return mixed A single value or array of values for a key if specified. An array of all registered keys 1795 * and values for an object ID if not. False if a given $meta_key is not registered. 1796 */ 1797 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { 1798 $object_subtype = get_object_subtype( $object_type, $object_id ); 1799 1800 if ( ! empty( $meta_key ) ) { 1801 if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1802 $object_subtype = ''; 1803 } 1804 1805 if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1806 return false; 1807 } 1808 1809 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); 1810 $meta_key_data = $meta_keys[ $meta_key ]; 1811 1812 $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] ); 1813 1814 return $data; 1815 } 1816 1817 $data = get_metadata( $object_type, $object_id ); 1818 if ( ! $data ) { 1819 return array(); 1820 } 1821 1822 $meta_keys = get_registered_meta_keys( $object_type ); 1823 if ( ! empty( $object_subtype ) ) { 1824 $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) ); 1825 } 1826 1827 return array_intersect_key( $data, $meta_keys ); 1828 } 1829 1830 /** 1831 * Filters out `register_meta()` args based on an allowed list. 1832 * 1833 * `register_meta()` args may change over time, so requiring the allowed list 1834 * to be explicitly turned off is a warranty seal of sorts. 1835 * 1836 * @access private 1837 * @since 5.5.0 1838 * 1839 * @param array $args Arguments from `register_meta()`. 1840 * @param array $default_args Default arguments for `register_meta()`. 1841 * @return array Filtered arguments. 1842 */ 1843 function _wp_register_meta_args_allowed_list( $args, $default_args ) { 1844 return array_intersect_key( $args, $default_args ); 1845 } 1846 1847 /** 1848 * Returns the object subtype for a given object ID of a specific type. 1849 * 1850 * @since 4.9.8 1851 * @since 7.2.0 Added support for 'blog' object type in multisite. 1852 * 1853 * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term', 1854 * 'user', or any other object type with an associated meta table. 1855 * @param int $object_id ID of the object to retrieve its subtype. 1856 * @return string The object subtype or an empty string if unspecified subtype. 1857 */ 1858 function get_object_subtype( $object_type, $object_id ) { 1859 $object_id = (int) $object_id; 1860 $object_subtype = ''; 1861 1862 switch ( $object_type ) { 1863 case 'post': 1864 $post_type = get_post_type( $object_id ); 1865 1866 if ( ! empty( $post_type ) ) { 1867 $object_subtype = $post_type; 1868 } 1869 break; 1870 1871 case 'term': 1872 $term = get_term( $object_id ); 1873 if ( ! $term instanceof WP_Term ) { 1874 break; 1875 } 1876 1877 $object_subtype = $term->taxonomy; 1878 break; 1879 1880 case 'comment': 1881 $comment = get_comment( $object_id ); 1882 if ( ! $comment ) { 1883 break; 1884 } 1885 1886 $object_subtype = 'comment'; 1887 break; 1888 1889 case 'user': 1890 $user = get_user_by( 'id', $object_id ); 1891 if ( ! $user ) { 1892 break; 1893 } 1894 1895 $object_subtype = 'user'; 1896 break; 1897 1898 case 'blog': 1899 if ( ! is_multisite() || $object_id <= 0 ) { 1900 break; 1901 } 1902 1903 $site = get_site( $object_id ); 1904 if ( ! $site ) { 1905 break; 1906 } 1907 1908 $object_subtype = 'blog'; 1909 break; 1910 } 1911 1912 /** 1913 * Filters the object subtype identifier. 1914 * 1915 * The dynamic portion of the hook name, `$object_type`, refers to the meta object type 1916 * (blog, post, comment, term, user, or any other type with an associated meta table). 1917 * 1918 * Possible hook names include: 1919 * 1920 * - `get_object_subtype_blog` 1921 * - `get_object_subtype_post` 1922 * - `get_object_subtype_comment` 1923 * - `get_object_subtype_term` 1924 * - `get_object_subtype_user` 1925 * 1926 * @since 4.9.8 1927 * 1928 * @param string $object_subtype Object subtype or empty string to override. 1929 * @param int $object_id ID of the object to get the subtype for. 1930 */ 1931 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1932 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Sep 29 08:20:35 2026 | Cross-referenced by PHPXref |