[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> meta.php (source)

   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   * @phpstan-param null $deprecated
1495   */
1496  function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
1497      global $wp_meta_keys;
1498  
1499      if ( ! is_array( $wp_meta_keys ) ) {
1500          $wp_meta_keys = array();
1501      }
1502  
1503      $defaults = array(
1504          'object_subtype'    => '',
1505          'type'              => 'string',
1506          'label'             => '',
1507          'description'       => '',
1508          'default'           => '',
1509          'single'            => false,
1510          'sanitize_callback' => null,
1511          'auth_callback'     => null,
1512          'show_in_rest'      => false,
1513          'revisions_enabled' => false,
1514      );
1515  
1516      // There used to be individual args for sanitize and auth callbacks.
1517      $has_old_sanitize_cb = false;
1518      $has_old_auth_cb     = false;
1519  
1520      if ( is_callable( $args ) ) {
1521          $args = array(
1522              'sanitize_callback' => $args,
1523          );
1524  
1525          $has_old_sanitize_cb = true;
1526      } else {
1527          $args = (array) $args;
1528      }
1529  
1530      if ( is_callable( $deprecated ) ) {
1531          $args['auth_callback'] = $deprecated;
1532          $has_old_auth_cb       = true;
1533      }
1534  
1535      /**
1536       * Filters the registration arguments when registering meta.
1537       *
1538       * @since 4.6.0
1539       *
1540       * @param array  $args        Array of meta registration arguments.
1541       * @param array  $defaults    Array of default arguments.
1542       * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1543       *                            'user', or any other object type with an associated meta table.
1544       * @param string $meta_key    Meta key.
1545       */
1546      $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
1547      unset( $defaults['default'] );
1548      $args = wp_parse_args( $args, $defaults );
1549  
1550      // Require an item schema when registering array meta.
1551      if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) {
1552          if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) {
1553              _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' );
1554  
1555              return false;
1556          }
1557      }
1558  
1559      $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
1560      if ( $args['revisions_enabled'] ) {
1561          if ( 'post' !== $object_type ) {
1562              _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object type supports revisions.' ), '6.4.0' );
1563  
1564              return false;
1565          } elseif ( ! empty( $object_subtype ) && ! post_type_supports( $object_subtype, 'revisions' ) ) {
1566              _doing_it_wrong( __FUNCTION__, __( 'Meta keys cannot enable revisions support unless the object subtype supports revisions.' ), '6.4.0' );
1567  
1568              return false;
1569          }
1570      }
1571  
1572      // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
1573      if ( empty( $args['auth_callback'] ) ) {
1574          if ( is_protected_meta( $meta_key, $object_type ) ) {
1575              $args['auth_callback'] = '__return_false';
1576          } else {
1577              $args['auth_callback'] = '__return_true';
1578          }
1579      }
1580  
1581      // Back-compat: old sanitize and auth callbacks are applied to all of an object type.
1582      if ( is_callable( $args['sanitize_callback'] ) ) {
1583          if ( ! empty( $object_subtype ) ) {
1584              add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 );
1585          } else {
1586              add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
1587          }
1588      }
1589  
1590      if ( is_callable( $args['auth_callback'] ) ) {
1591          if ( ! empty( $object_subtype ) ) {
1592              add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 );
1593          } else {
1594              add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
1595          }
1596      }
1597  
1598      if ( array_key_exists( 'default', $args ) ) {
1599          $schema = $args;
1600          if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) {
1601              $schema = array_merge( $schema, $args['show_in_rest']['schema'] );
1602          }
1603  
1604          $check = rest_validate_value_from_schema( $args['default'], $schema );
1605          if ( is_wp_error( $check ) ) {
1606              _doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' );
1607  
1608              return false;
1609          }
1610  
1611          if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) {
1612              add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 );
1613          }
1614      }
1615  
1616      // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
1617      if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
1618          unset( $args['object_subtype'] );
1619  
1620          $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
1621  
1622          return true;
1623      }
1624  
1625      return false;
1626  }
1627  
1628  /**
1629   * Filters into default_{$object_type}_metadata and adds in default value.
1630   *
1631   * @since 5.5.0
1632   *
1633   * @global array $wp_meta_keys Global registry for meta keys.
1634   *
1635   * @param mixed  $value     Current value passed to filter.
1636   * @param int    $object_id ID of the object metadata is for.
1637   * @param string $meta_key  Metadata key.
1638   * @param bool   $single    If true, return only the first value of the specified `$meta_key`.
1639   *                          This parameter has no effect if `$meta_key` is not specified.
1640   * @param string $meta_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1641   *                          'user', or any other object type with an associated meta table.
1642   * @return mixed An array of default values if `$single` is false.
1643   *               The default value of the meta field if `$single` is true.
1644   */
1645  function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
1646      global $wp_meta_keys;
1647  
1648      if ( wp_installing() ) {
1649          return $value;
1650      }
1651  
1652      if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) {
1653          return $value;
1654      }
1655  
1656      $defaults = array();
1657      foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) {
1658          foreach ( $meta_data as $_meta_key => $args ) {
1659              if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) {
1660                  $defaults[ $sub_type ] = $args;
1661              }
1662          }
1663      }
1664  
1665      if ( ! $defaults ) {
1666          return $value;
1667      }
1668  
1669      // If this meta type does not have subtypes, then the default is keyed as an empty string.
1670      if ( isset( $defaults[''] ) ) {
1671          $metadata = $defaults[''];
1672      } else {
1673          $sub_type = get_object_subtype( $meta_type, $object_id );
1674          if ( ! isset( $defaults[ $sub_type ] ) ) {
1675              return $value;
1676          }
1677          $metadata = $defaults[ $sub_type ];
1678      }
1679  
1680      if ( $single ) {
1681          $value = $metadata['default'];
1682      } else {
1683          $value = array( $metadata['default'] );
1684      }
1685  
1686      return $value;
1687  }
1688  
1689  /**
1690   * Checks if a meta key is registered.
1691   *
1692   * @since 4.6.0
1693   * @since 4.9.8 The `$object_subtype` parameter was added.
1694   *
1695   * @param string $object_type    Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1696   *                               'user', or any other object type with an associated meta table.
1697   * @param string $meta_key       Metadata key.
1698   * @param string $object_subtype Optional. The subtype of the object type. Default empty string.
1699   * @return bool True if the meta key is registered to the object type and, if provided,
1700   *              the object subtype. False if not.
1701   */
1702  function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
1703      $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
1704  
1705      return isset( $meta_keys[ $meta_key ] );
1706  }
1707  
1708  /**
1709   * Unregisters a meta key from the list of registered keys.
1710   *
1711   * @since 4.6.0
1712   * @since 4.9.8 The `$object_subtype` parameter was added.
1713   *
1714   * @global array $wp_meta_keys Global registry for meta keys.
1715   *
1716   * @param string $object_type    Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1717   *                               'user', or any other object type with an associated meta table.
1718   * @param string $meta_key       Metadata key.
1719   * @param string $object_subtype Optional. The subtype of the object type. Default empty string.
1720   * @return bool True if successful. False if the meta key was not registered.
1721   */
1722  function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
1723      global $wp_meta_keys;
1724  
1725      if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
1726          return false;
1727      }
1728  
1729      $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
1730  
1731      if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
1732          if ( ! empty( $object_subtype ) ) {
1733              remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] );
1734          } else {
1735              remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
1736          }
1737      }
1738  
1739      if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
1740          if ( ! empty( $object_subtype ) ) {
1741              remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] );
1742          } else {
1743              remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
1744          }
1745      }
1746  
1747      unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
1748  
1749      // Do some clean up.
1750      if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
1751          unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
1752      }
1753      if ( empty( $wp_meta_keys[ $object_type ] ) ) {
1754          unset( $wp_meta_keys[ $object_type ] );
1755      }
1756  
1757      return true;
1758  }
1759  
1760  /**
1761   * Retrieves a list of registered metadata args for an object type, keyed by their meta keys.
1762   *
1763   * @since 4.6.0
1764   * @since 4.9.8 The `$object_subtype` parameter was added.
1765   *
1766   * @global array $wp_meta_keys Global registry for meta keys.
1767   *
1768   * @param string $object_type    Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1769   *                               'user', or any other object type with an associated meta table.
1770   * @param string $object_subtype Optional. The subtype of the object type. Default empty string.
1771   * @return array[] List of registered metadata args, keyed by their meta keys.
1772   */
1773  function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
1774      global $wp_meta_keys;
1775  
1776      if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
1777          return array();
1778      }
1779  
1780      return $wp_meta_keys[ $object_type ][ $object_subtype ];
1781  }
1782  
1783  /**
1784   * Retrieves registered metadata for a specified object.
1785   *
1786   * The results include both meta that is registered specifically for the
1787   * object's subtype and meta that is registered for the entire object type.
1788   *
1789   * @since 4.6.0
1790   *
1791   * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1792   *                            'user', or any other object type with an associated meta table.
1793   * @param int    $object_id   ID of the object the metadata is for.
1794   * @param string $meta_key    Optional. Registered metadata key. If not specified, retrieve all registered
1795   *                            metadata for the specified object.
1796   * @return mixed A single value or array of values for a key if specified. An array of all registered keys
1797   *               and values for an object ID if not. False if a given $meta_key is not registered.
1798   */
1799  function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
1800      $object_subtype = get_object_subtype( $object_type, $object_id );
1801  
1802      if ( ! empty( $meta_key ) ) {
1803          if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
1804              $object_subtype = '';
1805          }
1806  
1807          if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
1808              return false;
1809          }
1810  
1811          $meta_keys     = get_registered_meta_keys( $object_type, $object_subtype );
1812          $meta_key_data = $meta_keys[ $meta_key ];
1813  
1814          $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
1815  
1816          return $data;
1817      }
1818  
1819      $data = get_metadata( $object_type, $object_id );
1820      if ( ! $data ) {
1821          return array();
1822      }
1823  
1824      $meta_keys = get_registered_meta_keys( $object_type );
1825      if ( ! empty( $object_subtype ) ) {
1826          $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
1827      }
1828  
1829      return array_intersect_key( $data, $meta_keys );
1830  }
1831  
1832  /**
1833   * Filters out `register_meta()` args based on an allowed list.
1834   *
1835   * `register_meta()` args may change over time, so requiring the allowed list
1836   * to be explicitly turned off is a warranty seal of sorts.
1837   *
1838   * @access private
1839   * @since 5.5.0
1840   *
1841   * @param array $args         Arguments from `register_meta()`.
1842   * @param array $default_args Default arguments for `register_meta()`.
1843   * @return array Filtered arguments.
1844   */
1845  function _wp_register_meta_args_allowed_list( $args, $default_args ) {
1846      return array_intersect_key( $args, $default_args );
1847  }
1848  
1849  /**
1850   * Returns the object subtype for a given object ID of a specific type.
1851   *
1852   * @since 4.9.8
1853   * @since 7.2.0 Added support for 'blog' object type in multisite.
1854   *
1855   * @param string $object_type Type of object metadata is for. Accepts 'blog', 'post', 'comment', 'term',
1856   *                            'user', or any other object type with an associated meta table.
1857   * @param int    $object_id   ID of the object to retrieve its subtype.
1858   * @return string The object subtype or an empty string if unspecified subtype.
1859   */
1860  function get_object_subtype( $object_type, $object_id ) {
1861      $object_id      = (int) $object_id;
1862      $object_subtype = '';
1863  
1864      switch ( $object_type ) {
1865          case 'post':
1866              $post_type = get_post_type( $object_id );
1867  
1868              if ( ! empty( $post_type ) ) {
1869                  $object_subtype = $post_type;
1870              }
1871              break;
1872  
1873          case 'term':
1874              $term = get_term( $object_id );
1875              if ( ! $term instanceof WP_Term ) {
1876                  break;
1877              }
1878  
1879              $object_subtype = $term->taxonomy;
1880              break;
1881  
1882          case 'comment':
1883              $comment = get_comment( $object_id );
1884              if ( ! $comment ) {
1885                  break;
1886              }
1887  
1888              $object_subtype = 'comment';
1889              break;
1890  
1891          case 'user':
1892              $user = get_user_by( 'id', $object_id );
1893              if ( ! $user ) {
1894                  break;
1895              }
1896  
1897              $object_subtype = 'user';
1898              break;
1899  
1900          case 'blog':
1901              if ( ! is_multisite() || $object_id <= 0 ) {
1902                  break;
1903              }
1904  
1905              $site = get_site( $object_id );
1906              if ( ! $site ) {
1907                  break;
1908              }
1909  
1910              $object_subtype = 'blog';
1911              break;
1912      }
1913  
1914      /**
1915       * Filters the object subtype identifier.
1916       *
1917       * The dynamic portion of the hook name, `$object_type`, refers to the meta object type
1918       * (blog, post, comment, term, user, or any other type with an associated meta table).
1919       *
1920       * Possible hook names include:
1921       *
1922       *  - `get_object_subtype_blog`
1923       *  - `get_object_subtype_post`
1924       *  - `get_object_subtype_comment`
1925       *  - `get_object_subtype_term`
1926       *  - `get_object_subtype_user`
1927       *
1928       * @since 4.9.8
1929       *
1930       * @param string $object_subtype Object subtype or empty string to override.
1931       * @param int    $object_id      ID of the object to get the subtype for.
1932       */
1933      return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
1934  }


Generated : Tue Oct 20 08:20:34 2026 Cross-referenced by PHPXref