[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

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  /**
  14   * Add metadata for the specified object.
  15   *
  16   * @since 2.9.0
  17   *
  18   * @global wpdb $wpdb WordPress database abstraction object.
  19   *
  20   * @param string $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
  21   * @param int    $object_id  ID of the object metadata is for
  22   * @param string $meta_key   Metadata key
  23   * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
  24   * @param bool   $unique     Optional, default is false.
  25   *                           Whether the specified metadata key should be unique for the object.
  26   *                           If true, and the object already has a value for the specified metadata key,
  27   *                           no change will be made.
  28   * @return int|false The meta ID on success, false on failure.
  29   */
  30  function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) {
  31      global $wpdb;
  32  
  33      if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
  34          return false;
  35      }
  36  
  37      $object_id = absint( $object_id );
  38      if ( ! $object_id ) {
  39          return false;
  40      }
  41  
  42      $table = _get_meta_table( $meta_type );
  43      if ( ! $table ) {
  44          return false;
  45      }
  46  
  47      $meta_subtype = get_object_subtype( $meta_type, $object_id );
  48  
  49      $column = sanitize_key( $meta_type . '_id' );
  50  
  51      // expected_slashed ($meta_key)
  52      $meta_key   = wp_unslash( $meta_key );
  53      $meta_value = wp_unslash( $meta_value );
  54      $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
  55  
  56      /**
  57       * Filters whether to add metadata of a specific type.
  58       *
  59       * The dynamic portion of the hook, `$meta_type`, refers to the meta
  60       * object type (comment, post, term, or user). Returning a non-null value
  61       * will effectively short-circuit the function.
  62       *
  63       * @since 3.1.0
  64       *
  65       * @param null|bool $check      Whether to allow adding metadata for the given type.
  66       * @param int       $object_id  Object ID.
  67       * @param string    $meta_key   Meta key.
  68       * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
  69       * @param bool      $unique     Whether the specified meta key should be unique
  70       *                              for the object. Optional. Default false.
  71       */
  72      $check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
  73      if ( null !== $check ) {
  74          return $check;
  75      }
  76  
  77      if ( $unique && $wpdb->get_var(
  78          $wpdb->prepare(
  79              "SELECT COUNT(*) FROM $table WHERE meta_key = %s AND $column = %d",
  80              $meta_key,
  81              $object_id
  82          )
  83      ) ) {
  84          return false;
  85      }
  86  
  87      $_meta_value = $meta_value;
  88      $meta_value  = maybe_serialize( $meta_value );
  89  
  90      /**
  91       * Fires immediately before meta of a specific type is added.
  92       *
  93       * The dynamic portion of the hook, `$meta_type`, refers to the meta
  94       * object type (comment, post, term, or user).
  95       *
  96       * @since 3.1.0
  97       *
  98       * @param int    $object_id   Object ID.
  99       * @param string $meta_key    Meta key.
 100       * @param mixed  $_meta_value Meta value.
 101       */
 102      do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
 103  
 104      $result = $wpdb->insert(
 105          $table,
 106          array(
 107              $column      => $object_id,
 108              'meta_key'   => $meta_key,
 109              'meta_value' => $meta_value,
 110          )
 111      );
 112  
 113      if ( ! $result ) {
 114          return false;
 115      }
 116  
 117      $mid = (int) $wpdb->insert_id;
 118  
 119      wp_cache_delete( $object_id, $meta_type . '_meta' );
 120  
 121      /**
 122       * Fires immediately after meta of a specific type is added.
 123       *
 124       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 125       * object type (comment, post, term, or user).
 126       *
 127       * @since 2.9.0
 128       *
 129       * @param int    $mid         The meta ID after successful update.
 130       * @param int    $object_id   Object ID.
 131       * @param string $meta_key    Meta key.
 132       * @param mixed  $_meta_value Meta value.
 133       */
 134      do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
 135  
 136      return $mid;
 137  }
 138  
 139  /**
 140   * Update metadata for the specified object. If no value already exists for the specified object
 141   * ID and metadata key, the metadata will be added.
 142   *
 143   * @since 2.9.0
 144   *
 145   * @global wpdb $wpdb WordPress database abstraction object.
 146   *
 147   * @param string $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
 148   * @param int    $object_id  ID of the object metadata is for
 149   * @param string $meta_key   Metadata key
 150   * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
 151   * @param mixed  $prev_value Optional. If specified, only update existing metadata entries with
 152   *                           the specified value. Otherwise, update all entries.
 153   * @return int|bool The new meta field ID if a field with the given key didn't exist and was
 154   *                  therefore added, true on successful update, false on failure.
 155   */
 156  function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) {
 157      global $wpdb;
 158  
 159      if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
 160          return false;
 161      }
 162  
 163      $object_id = absint( $object_id );
 164      if ( ! $object_id ) {
 165          return false;
 166      }
 167  
 168      $table = _get_meta_table( $meta_type );
 169      if ( ! $table ) {
 170          return false;
 171      }
 172  
 173      $meta_subtype = get_object_subtype( $meta_type, $object_id );
 174  
 175      $column    = sanitize_key( $meta_type . '_id' );
 176      $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
 177  
 178      // expected_slashed ($meta_key)
 179      $raw_meta_key = $meta_key;
 180      $meta_key     = wp_unslash( $meta_key );
 181      $passed_value = $meta_value;
 182      $meta_value   = wp_unslash( $meta_value );
 183      $meta_value   = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
 184  
 185      /**
 186       * Filters whether to update metadata of a specific type.
 187       *
 188       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 189       * object type (comment, post, term, or user). Returning a non-null value
 190       * will effectively short-circuit the function.
 191       *
 192       * @since 3.1.0
 193       *
 194       * @param null|bool $check      Whether to allow updating metadata for the given type.
 195       * @param int       $object_id  Object ID.
 196       * @param string    $meta_key   Meta key.
 197       * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
 198       * @param mixed     $prev_value Optional. If specified, only update existing
 199       *                              metadata entries with the specified value.
 200       *                              Otherwise, update all entries.
 201       */
 202      $check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
 203      if ( null !== $check ) {
 204          return (bool) $check;
 205      }
 206  
 207      // Compare existing value to new value if no prev value given and the key exists only once.
 208      if ( empty( $prev_value ) ) {
 209          $old_value = get_metadata( $meta_type, $object_id, $meta_key );
 210          if ( count( $old_value ) == 1 ) {
 211              if ( $old_value[0] === $meta_value ) {
 212                  return false;
 213              }
 214          }
 215      }
 216  
 217      $meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s AND $column = %d", $meta_key, $object_id ) );
 218      if ( empty( $meta_ids ) ) {
 219          return add_metadata( $meta_type, $object_id, $raw_meta_key, $passed_value );
 220      }
 221  
 222      $_meta_value = $meta_value;
 223      $meta_value  = maybe_serialize( $meta_value );
 224  
 225      $data  = compact( 'meta_value' );
 226      $where = array(
 227          $column    => $object_id,
 228          'meta_key' => $meta_key,
 229      );
 230  
 231      if ( ! empty( $prev_value ) ) {
 232          $prev_value          = maybe_serialize( $prev_value );
 233          $where['meta_value'] = $prev_value;
 234      }
 235  
 236      foreach ( $meta_ids as $meta_id ) {
 237          /**
 238           * Fires immediately before updating metadata of a specific type.
 239           *
 240           * The dynamic portion of the hook, `$meta_type`, refers to the meta
 241           * object type (comment, post, term, or user).
 242           *
 243           * @since 2.9.0
 244           *
 245           * @param int    $meta_id     ID of the metadata entry to update.
 246           * @param int    $object_id   Object ID.
 247           * @param string $meta_key    Meta key.
 248           * @param mixed  $_meta_value Meta value.
 249           */
 250          do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
 251  
 252          if ( 'post' == $meta_type ) {
 253              /**
 254               * Fires immediately before updating a post's metadata.
 255               *
 256               * @since 2.9.0
 257               *
 258               * @param int    $meta_id    ID of metadata entry to update.
 259               * @param int    $object_id  Post ID.
 260               * @param string $meta_key   Meta key.
 261               * @param mixed  $meta_value Meta value. This will be a PHP-serialized string representation of the value if
 262               *                           the value is an array, an object, or itself a PHP-serialized string.
 263               */
 264              do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
 265          }
 266      }
 267  
 268      $result = $wpdb->update( $table, $data, $where );
 269      if ( ! $result ) {
 270          return false;
 271      }
 272  
 273      wp_cache_delete( $object_id, $meta_type . '_meta' );
 274  
 275      foreach ( $meta_ids as $meta_id ) {
 276          /**
 277           * Fires immediately after updating metadata of a specific type.
 278           *
 279           * The dynamic portion of the hook, `$meta_type`, refers to the meta
 280           * object type (comment, post, term, or user).
 281           *
 282           * @since 2.9.0
 283           *
 284           * @param int    $meta_id     ID of updated metadata entry.
 285           * @param int    $object_id   Object ID.
 286           * @param string $meta_key    Meta key.
 287           * @param mixed  $_meta_value Meta value.
 288           */
 289          do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
 290  
 291          if ( 'post' == $meta_type ) {
 292              /**
 293               * Fires immediately after updating a post's metadata.
 294               *
 295               * @since 2.9.0
 296               *
 297               * @param int    $meta_id    ID of updated metadata entry.
 298               * @param int    $object_id  Post ID.
 299               * @param string $meta_key   Meta key.
 300               * @param mixed  $meta_value Meta value. This will be a PHP-serialized string representation of the value if
 301               *                           the value is an array, an object, or itself a PHP-serialized string.
 302               */
 303              do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
 304          }
 305      }
 306  
 307      return true;
 308  }
 309  
 310  /**
 311   * Delete metadata for the specified object.
 312   *
 313   * @since 2.9.0
 314   *
 315   * @global wpdb $wpdb WordPress database abstraction object.
 316   *
 317   * @param string $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
 318   * @param int    $object_id  ID of the object metadata is for
 319   * @param string $meta_key   Metadata key
 320   * @param mixed  $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete
 321   *                           metadata entries with this value. Otherwise, delete all entries with the specified meta_key.
 322   *                           Pass `null`, `false`, or an empty string to skip this check. (For backward compatibility,
 323   *                           it is not possible to pass an empty string to delete those entries with an empty string
 324   *                           for a value.)
 325   * @param bool   $delete_all Optional, default is false. If true, delete matching metadata entries for all objects,
 326   *                           ignoring the specified object_id. Otherwise, only delete matching metadata entries for
 327   *                           the specified object_id.
 328   * @return bool True on successful delete, false on failure.
 329   */
 330  function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
 331      global $wpdb;
 332  
 333      if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) && ! $delete_all ) {
 334          return false;
 335      }
 336  
 337      $object_id = absint( $object_id );
 338      if ( ! $object_id && ! $delete_all ) {
 339          return false;
 340      }
 341  
 342      $table = _get_meta_table( $meta_type );
 343      if ( ! $table ) {
 344          return false;
 345      }
 346  
 347      $type_column = sanitize_key( $meta_type . '_id' );
 348      $id_column   = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
 349      // expected_slashed ($meta_key)
 350      $meta_key   = wp_unslash( $meta_key );
 351      $meta_value = wp_unslash( $meta_value );
 352  
 353      /**
 354       * Filters whether to delete metadata of a specific type.
 355       *
 356       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 357       * object type (comment, post, term, or user). Returning a non-null value
 358       * will effectively short-circuit the function.
 359       *
 360       * @since 3.1.0
 361       *
 362       * @param null|bool $delete     Whether to allow metadata deletion of the given type.
 363       * @param int       $object_id  Object ID.
 364       * @param string    $meta_key   Meta key.
 365       * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
 366       * @param bool      $delete_all Whether to delete the matching metadata entries
 367       *                              for all objects, ignoring the specified $object_id.
 368       *                              Default false.
 369       */
 370      $check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
 371      if ( null !== $check ) {
 372          return (bool) $check;
 373      }
 374  
 375      $_meta_value = $meta_value;
 376      $meta_value  = maybe_serialize( $meta_value );
 377  
 378      $query = $wpdb->prepare( "SELECT $id_column FROM $table WHERE meta_key = %s", $meta_key );
 379  
 380      if ( ! $delete_all ) {
 381          $query .= $wpdb->prepare( " AND $type_column = %d", $object_id );
 382      }
 383  
 384      if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
 385          $query .= $wpdb->prepare( ' AND meta_value = %s', $meta_value );
 386      }
 387  
 388      $meta_ids = $wpdb->get_col( $query );
 389      if ( ! count( $meta_ids ) ) {
 390          return false;
 391      }
 392  
 393      if ( $delete_all ) {
 394          if ( '' !== $meta_value && null !== $meta_value && false !== $meta_value ) {
 395              $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s AND meta_value = %s", $meta_key, $meta_value ) );
 396          } else {
 397              $object_ids = $wpdb->get_col( $wpdb->prepare( "SELECT $type_column FROM $table WHERE meta_key = %s", $meta_key ) );
 398          }
 399      }
 400  
 401      /**
 402       * Fires immediately before deleting metadata of a specific type.
 403       *
 404       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 405       * object type (comment, post, term, or user).
 406       *
 407       * @since 3.1.0
 408       *
 409       * @param array  $meta_ids    An array of metadata entry IDs to delete.
 410       * @param int    $object_id   Object ID.
 411       * @param string $meta_key    Meta key.
 412       * @param mixed  $_meta_value Meta value.
 413       */
 414      do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
 415  
 416      // Old-style action.
 417      if ( 'post' == $meta_type ) {
 418          /**
 419           * Fires immediately before deleting metadata for a post.
 420           *
 421           * @since 2.9.0
 422           *
 423           * @param array $meta_ids An array of post metadata entry IDs to delete.
 424           */
 425          do_action( 'delete_postmeta', $meta_ids );
 426      }
 427  
 428      $query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )';
 429  
 430      $count = $wpdb->query( $query );
 431  
 432      if ( ! $count ) {
 433          return false;
 434      }
 435  
 436      if ( $delete_all ) {
 437          foreach ( (array) $object_ids as $o_id ) {
 438              wp_cache_delete( $o_id, $meta_type . '_meta' );
 439          }
 440      } else {
 441          wp_cache_delete( $object_id, $meta_type . '_meta' );
 442      }
 443  
 444      /**
 445       * Fires immediately after deleting metadata of a specific type.
 446       *
 447       * The dynamic portion of the hook name, `$meta_type`, refers to the meta
 448       * object type (comment, post, term, or user).
 449       *
 450       * @since 2.9.0
 451       *
 452       * @param array  $meta_ids    An array of deleted metadata entry IDs.
 453       * @param int    $object_id   Object ID.
 454       * @param string $meta_key    Meta key.
 455       * @param mixed  $_meta_value Meta value.
 456       */
 457      do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
 458  
 459      // Old-style action.
 460      if ( 'post' == $meta_type ) {
 461          /**
 462           * Fires immediately after deleting metadata for a post.
 463           *
 464           * @since 2.9.0
 465           *
 466           * @param array $meta_ids An array of deleted post metadata entry IDs.
 467           */
 468          do_action( 'deleted_postmeta', $meta_ids );
 469      }
 470  
 471      return true;
 472  }
 473  
 474  /**
 475   * Retrieve metadata for the specified object.
 476   *
 477   * @since 2.9.0
 478   *
 479   * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
 480   * @param int    $object_id ID of the object metadata is for
 481   * @param string $meta_key  Optional. Metadata key. If not specified, retrieve all metadata for
 482   *                          the specified object.
 483   * @param bool   $single    Optional, default is false.
 484   *                          If true, return only the first value of the specified meta_key.
 485   *                          This parameter has no effect if meta_key is not specified.
 486   * @return mixed Single metadata value, or array of values
 487   */
 488  function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
 489      if ( ! $meta_type || ! is_numeric( $object_id ) ) {
 490          return false;
 491      }
 492  
 493      $object_id = absint( $object_id );
 494      if ( ! $object_id ) {
 495          return false;
 496      }
 497  
 498      /**
 499       * Filters whether to retrieve metadata of a specific type.
 500       *
 501       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 502       * object type (comment, post, term, or user). Returning a non-null value
 503       * will effectively short-circuit the function.
 504       *
 505       * @since 3.1.0
 506       *
 507       * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
 508       *                                     or an array of values.
 509       * @param int               $object_id Object ID.
 510       * @param string            $meta_key  Meta key.
 511       * @param bool              $single    Whether to return only the first value of the specified $meta_key.
 512       */
 513      $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
 514      if ( null !== $check ) {
 515          if ( $single && is_array( $check ) ) {
 516              return $check[0];
 517          } else {
 518              return $check;
 519          }
 520      }
 521  
 522      $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
 523  
 524      if ( ! $meta_cache ) {
 525          $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
 526          $meta_cache = $meta_cache[ $object_id ];
 527      }
 528  
 529      if ( ! $meta_key ) {
 530          return $meta_cache;
 531      }
 532  
 533      if ( isset( $meta_cache[ $meta_key ] ) ) {
 534          if ( $single ) {
 535              return maybe_unserialize( $meta_cache[ $meta_key ][0] );
 536          } else {
 537              return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
 538          }
 539      }
 540  
 541      if ( $single ) {
 542          return '';
 543      } else {
 544          return array();
 545      }
 546  }
 547  
 548  /**
 549   * Determine if a meta key is set for a given object
 550   *
 551   * @since 3.3.0
 552   *
 553   * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
 554   * @param int    $object_id ID of the object metadata is for
 555   * @param string $meta_key  Metadata key.
 556   * @return bool True of the key is set, false if not.
 557   */
 558  function metadata_exists( $meta_type, $object_id, $meta_key ) {
 559      if ( ! $meta_type || ! is_numeric( $object_id ) ) {
 560          return false;
 561      }
 562  
 563      $object_id = absint( $object_id );
 564      if ( ! $object_id ) {
 565          return false;
 566      }
 567  
 568      /** This filter is documented in wp-includes/meta.php */
 569      $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
 570      if ( null !== $check ) {
 571          return (bool) $check;
 572      }
 573  
 574      $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
 575  
 576      if ( ! $meta_cache ) {
 577          $meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
 578          $meta_cache = $meta_cache[ $object_id ];
 579      }
 580  
 581      if ( isset( $meta_cache[ $meta_key ] ) ) {
 582          return true;
 583      }
 584  
 585      return false;
 586  }
 587  
 588  /**
 589   * Get meta data by meta ID
 590   *
 591   * @since 3.3.0
 592   *
 593   * @global wpdb $wpdb WordPress database abstraction object.
 594   *
 595   * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
 596   * @param int    $meta_id   ID for a specific meta row
 597   * @return object|false Meta object or false.
 598   */
 599  function get_metadata_by_mid( $meta_type, $meta_id ) {
 600      global $wpdb;
 601  
 602      if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
 603          return false;
 604      }
 605  
 606      $meta_id = intval( $meta_id );
 607      if ( $meta_id <= 0 ) {
 608          return false;
 609      }
 610  
 611      $table = _get_meta_table( $meta_type );
 612      if ( ! $table ) {
 613          return false;
 614      }
 615  
 616      $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
 617  
 618      /**
 619       * Filters whether to retrieve metadata of a specific type by meta ID.
 620       *
 621       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 622       * object type (comment, post, term, or user). Returning a non-null value
 623       * will effectively short-circuit the function.
 624       *
 625       * @since 5.0.0
 626       *
 627       * @param mixed $value    The value get_metadata_by_mid() should return.
 628       * @param int   $meta_id  Meta ID.
 629       */
 630      $check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id );
 631      if ( null !== $check ) {
 632          return $check;
 633      }
 634  
 635      $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
 636  
 637      if ( empty( $meta ) ) {
 638          return false;
 639      }
 640  
 641      if ( isset( $meta->meta_value ) ) {
 642          $meta->meta_value = maybe_unserialize( $meta->meta_value );
 643      }
 644  
 645      return $meta;
 646  }
 647  
 648  /**
 649   * Update meta data by meta ID
 650   *
 651   * @since 3.3.0
 652   *
 653   * @global wpdb $wpdb WordPress database abstraction object.
 654   *
 655   * @param string $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
 656   * @param int    $meta_id    ID for a specific meta row
 657   * @param string $meta_value Metadata value
 658   * @param string $meta_key   Optional, you can provide a meta key to update it
 659   * @return bool True on successful update, false on failure.
 660   */
 661  function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
 662      global $wpdb;
 663  
 664      // Make sure everything is valid.
 665      if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
 666          return false;
 667      }
 668  
 669      $meta_id = intval( $meta_id );
 670      if ( $meta_id <= 0 ) {
 671          return false;
 672      }
 673  
 674      $table = _get_meta_table( $meta_type );
 675      if ( ! $table ) {
 676          return false;
 677      }
 678  
 679      $column    = sanitize_key( $meta_type . '_id' );
 680      $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
 681  
 682      /**
 683       * Filters whether to update metadata of a specific type by meta ID.
 684       *
 685       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 686       * object type (comment, post, term, or user). Returning a non-null value
 687       * will effectively short-circuit the function.
 688       *
 689       * @since 5.0.0
 690       *
 691       * @param null|bool   $check      Whether to allow updating metadata for the given type.
 692       * @param int         $meta_id    Meta ID.
 693       * @param mixed       $meta_value Meta value. Must be serializable if non-scalar.
 694       * @param string|bool $meta_key   Meta key, if provided.
 695       */
 696      $check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key );
 697      if ( null !== $check ) {
 698          return (bool) $check;
 699      }
 700  
 701      // Fetch the meta and go on if it's found.
 702      if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
 703          $original_key = $meta->meta_key;
 704          $object_id    = $meta->{$column};
 705  
 706          // If a new meta_key (last parameter) was specified, change the meta key,
 707          // otherwise use the original key in the update statement.
 708          if ( false === $meta_key ) {
 709              $meta_key = $original_key;
 710          } elseif ( ! is_string( $meta_key ) ) {
 711              return false;
 712          }
 713  
 714          $meta_subtype = get_object_subtype( $meta_type, $object_id );
 715  
 716          // Sanitize the meta
 717          $_meta_value = $meta_value;
 718          $meta_value  = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
 719          $meta_value  = maybe_serialize( $meta_value );
 720  
 721          // Format the data query arguments.
 722          $data = array(
 723              'meta_key'   => $meta_key,
 724              'meta_value' => $meta_value,
 725          );
 726  
 727          // Format the where query arguments.
 728          $where               = array();
 729          $where[ $id_column ] = $meta_id;
 730  
 731          /** This action is documented in wp-includes/meta.php */
 732          do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
 733  
 734          if ( 'post' == $meta_type ) {
 735              /** This action is documented in wp-includes/meta.php */
 736              do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
 737          }
 738  
 739          // Run the update query, all fields in $data are %s, $where is a %d.
 740          $result = $wpdb->update( $table, $data, $where, '%s', '%d' );
 741          if ( ! $result ) {
 742              return false;
 743          }
 744  
 745          // Clear the caches.
 746          wp_cache_delete( $object_id, $meta_type . '_meta' );
 747  
 748          /** This action is documented in wp-includes/meta.php */
 749          do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
 750  
 751          if ( 'post' == $meta_type ) {
 752              /** This action is documented in wp-includes/meta.php */
 753              do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
 754          }
 755  
 756          return true;
 757      }
 758  
 759      // And if the meta was not found.
 760      return false;
 761  }
 762  
 763  /**
 764   * Delete meta data by meta ID
 765   *
 766   * @since 3.3.0
 767   *
 768   * @global wpdb $wpdb WordPress database abstraction object.
 769   *
 770   * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
 771   * @param int    $meta_id   ID for a specific meta row
 772   * @return bool True on successful delete, false on failure.
 773   */
 774  function delete_metadata_by_mid( $meta_type, $meta_id ) {
 775      global $wpdb;
 776  
 777      // Make sure everything is valid.
 778      if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
 779          return false;
 780      }
 781  
 782      $meta_id = intval( $meta_id );
 783      if ( $meta_id <= 0 ) {
 784          return false;
 785      }
 786  
 787      $table = _get_meta_table( $meta_type );
 788      if ( ! $table ) {
 789          return false;
 790      }
 791  
 792      // object and id columns
 793      $column    = sanitize_key( $meta_type . '_id' );
 794      $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
 795  
 796      /**
 797       * Filters whether to delete metadata of a specific type by meta ID.
 798       *
 799       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 800       * object type (comment, post, term, or user). Returning a non-null value
 801       * will effectively short-circuit the function.
 802       *
 803       * @since 5.0.0
 804       *
 805       * @param null|bool $delete  Whether to allow metadata deletion of the given type.
 806       * @param int       $meta_id Meta ID.
 807       */
 808      $check = apply_filters( "delete_{$meta_type}_metadata_by_mid", null, $meta_id );
 809      if ( null !== $check ) {
 810          return (bool) $check;
 811      }
 812  
 813      // Fetch the meta and go on if it's found.
 814      if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
 815          $object_id = (int) $meta->{$column};
 816  
 817          /** This action is documented in wp-includes/meta.php */
 818          do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
 819  
 820          // Old-style action.
 821          if ( 'post' == $meta_type || 'comment' == $meta_type ) {
 822              /**
 823               * Fires immediately before deleting post or comment metadata of a specific type.
 824               *
 825               * The dynamic portion of the hook, `$meta_type`, refers to the meta
 826               * object type (post or comment).
 827               *
 828               * @since 3.4.0
 829               *
 830               * @param int $meta_id ID of the metadata entry to delete.
 831               */
 832              do_action( "delete_{$meta_type}meta", $meta_id );
 833          }
 834  
 835          // Run the query, will return true if deleted, false otherwise
 836          $result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
 837  
 838          // Clear the caches.
 839          wp_cache_delete( $object_id, $meta_type . '_meta' );
 840  
 841          /** This action is documented in wp-includes/meta.php */
 842          do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
 843  
 844          // Old-style action.
 845          if ( 'post' == $meta_type || 'comment' == $meta_type ) {
 846              /**
 847               * Fires immediately after deleting post or comment metadata of a specific type.
 848               *
 849               * The dynamic portion of the hook, `$meta_type`, refers to the meta
 850               * object type (post or comment).
 851               *
 852               * @since 3.4.0
 853               *
 854               * @param int $meta_ids Deleted metadata entry ID.
 855               */
 856              do_action( "deleted_{$meta_type}meta", $meta_id );
 857          }
 858  
 859          return $result;
 860  
 861      }
 862  
 863      // Meta id was not found.
 864      return false;
 865  }
 866  
 867  /**
 868   * Update the metadata cache for the specified objects.
 869   *
 870   * @since 2.9.0
 871   *
 872   * @global wpdb $wpdb WordPress database abstraction object.
 873   *
 874   * @param string    $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
 875   * @param int|array $object_ids Array or comma delimited list of object IDs to update cache for
 876   * @return array|false Metadata cache for the specified objects, or false on failure.
 877   */
 878  function update_meta_cache( $meta_type, $object_ids ) {
 879      global $wpdb;
 880  
 881      if ( ! $meta_type || ! $object_ids ) {
 882          return false;
 883      }
 884  
 885      $table = _get_meta_table( $meta_type );
 886      if ( ! $table ) {
 887          return false;
 888      }
 889  
 890      $column = sanitize_key( $meta_type . '_id' );
 891  
 892      if ( ! is_array( $object_ids ) ) {
 893          $object_ids = preg_replace( '|[^0-9,]|', '', $object_ids );
 894          $object_ids = explode( ',', $object_ids );
 895      }
 896  
 897      $object_ids = array_map( 'intval', $object_ids );
 898  
 899      /**
 900       * Filters whether to update the metadata cache of a specific type.
 901       *
 902       * The dynamic portion of the hook, `$meta_type`, refers to the meta
 903       * object type (comment, post, term, or user). Returning a non-null value
 904       * will effectively short-circuit the function.
 905       *
 906       * @since 5.0.0
 907       *
 908       * @param mixed $check      Whether to allow updating the meta cache of the given type.
 909       * @param array $object_ids Array of object IDs to update the meta cache for.
 910       */
 911      $check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
 912      if ( null !== $check ) {
 913          return (bool) $check;
 914      }
 915  
 916      $cache_key = $meta_type . '_meta';
 917      $ids       = array();
 918      $cache     = array();
 919      foreach ( $object_ids as $id ) {
 920          $cached_object = wp_cache_get( $id, $cache_key );
 921          if ( false === $cached_object ) {
 922              $ids[] = $id;
 923          } else {
 924              $cache[ $id ] = $cached_object;
 925          }
 926      }
 927  
 928      if ( empty( $ids ) ) {
 929          return $cache;
 930      }
 931  
 932      // Get meta info
 933      $id_list   = join( ',', $ids );
 934      $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
 935      $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 );
 936  
 937      if ( ! empty( $meta_list ) ) {
 938          foreach ( $meta_list as $metarow ) {
 939              $mpid = intval( $metarow[ $column ] );
 940              $mkey = $metarow['meta_key'];
 941              $mval = $metarow['meta_value'];
 942  
 943              // Force subkeys to be array type:
 944              if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {
 945                  $cache[ $mpid ] = array();
 946              }
 947              if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) {
 948                  $cache[ $mpid ][ $mkey ] = array();
 949              }
 950  
 951              // Add a value to the current pid/key:
 952              $cache[ $mpid ][ $mkey ][] = $mval;
 953          }
 954      }
 955  
 956      foreach ( $ids as $id ) {
 957          if ( ! isset( $cache[ $id ] ) ) {
 958              $cache[ $id ] = array();
 959          }
 960          wp_cache_add( $id, $cache[ $id ], $cache_key );
 961      }
 962  
 963      return $cache;
 964  }
 965  
 966  /**
 967   * Retrieves the queue for lazy-loading metadata.
 968   *
 969   * @since 4.5.0
 970   *
 971   * @return WP_Metadata_Lazyloader $lazyloader Metadata lazyloader queue.
 972   */
 973  function wp_metadata_lazyloader() {
 974      static $wp_metadata_lazyloader;
 975  
 976      if ( null === $wp_metadata_lazyloader ) {
 977          $wp_metadata_lazyloader = new WP_Metadata_Lazyloader();
 978      }
 979  
 980      return $wp_metadata_lazyloader;
 981  }
 982  
 983  /**
 984   * Given a meta query, generates SQL clauses to be appended to a main query.
 985   *
 986   * @since 3.2.0
 987   *
 988   * @see WP_Meta_Query
 989   *
 990   * @param array $meta_query         A meta query.
 991   * @param string $type              Type of meta.
 992   * @param string $primary_table     Primary database table name.
 993   * @param string $primary_id_column Primary ID column name.
 994   * @param object $context           Optional. The main query object
 995   * @return array Associative array of `JOIN` and `WHERE` SQL.
 996   */
 997  function get_meta_sql( $meta_query, $type, $primary_table, $primary_id_column, $context = null ) {
 998      $meta_query_obj = new WP_Meta_Query( $meta_query );
 999      return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
1000  }
1001  
1002  /**
1003   * Retrieve the name of the metadata table for the specified object type.
1004   *
1005   * @since 2.9.0
1006   *
1007   * @global wpdb $wpdb WordPress database abstraction object.
1008   *
1009   * @param string $type Type of object to get metadata table for (e.g., comment, post, term, or user).
1010   * @return string|false Metadata table name, or false if no metadata table exists
1011   */
1012  function _get_meta_table( $type ) {
1013      global $wpdb;
1014  
1015      $table_name = $type . 'meta';
1016  
1017      if ( empty( $wpdb->$table_name ) ) {
1018          return false;
1019      }
1020  
1021      return $wpdb->$table_name;
1022  }
1023  
1024  /**
1025   * Determines whether a meta key is considered protected.
1026   *
1027   * @since 3.1.3
1028   *
1029   * @param string      $meta_key  Meta key.
1030   * @param string|null $meta_type Optional. Type of object metadata is for (e.g., comment, post, term, or user).
1031   * @return bool Whether the meta key is considered protected.
1032   */
1033  function is_protected_meta( $meta_key, $meta_type = null ) {
1034      $protected = ( '_' == $meta_key[0] );
1035  
1036      /**
1037       * Filters whether a meta key is considered protected.
1038       *
1039       * @since 3.2.0
1040       *
1041       * @param bool        $protected Whether the key is considered protected.
1042       * @param string      $meta_key  Meta key.
1043       * @param string|null $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
1044       */
1045      return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
1046  }
1047  
1048  /**
1049   * Sanitize meta value.
1050   *
1051   * @since 3.1.3
1052   * @since 4.9.8 The `$object_subtype` parameter was added.
1053   *
1054   * @param string $meta_key       Meta key.
1055   * @param mixed  $meta_value     Meta value to sanitize.
1056   * @param string $object_type    Type of object the meta is registered to.
1057   * @param string $object_subtype Optional. The subtype of the object type.
1058   *
1059   * @return mixed Sanitized $meta_value.
1060   */
1061  function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
1062      if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
1063  
1064          /**
1065           * Filters the sanitization of a specific meta key of a specific meta type and subtype.
1066           *
1067           * The dynamic portions of the hook name, `$object_type`, `$meta_key`,
1068           * and `$object_subtype`, refer to the metadata object type (comment, post, term, or user),
1069           * the meta key value, and the object subtype respectively.
1070           *
1071           * @since 4.9.8
1072           *
1073           * @param mixed  $meta_value     Meta value to sanitize.
1074           * @param string $meta_key       Meta key.
1075           * @param string $object_type    Object type.
1076           * @param string $object_subtype Object subtype.
1077           */
1078          return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype );
1079      }
1080  
1081      /**
1082       * Filters the sanitization of a specific meta key of a specific meta type.
1083       *
1084       * The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
1085       * refer to the metadata object type (comment, post, term, or user) and the meta
1086       * key value, respectively.
1087       *
1088       * @since 3.3.0
1089       *
1090       * @param mixed  $meta_value      Meta value to sanitize.
1091       * @param string $meta_key        Meta key.
1092       * @param string $object_type     Object type.
1093       */
1094      return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
1095  }
1096  
1097  /**
1098   * Registers a meta key.
1099   *
1100   * It is recommended to register meta keys for a specific combination of object type and object subtype. If passing
1101   * an object subtype is omitted, the meta key will be registered for the entire object type, however it can be partly
1102   * overridden in case a more specific meta key of the same name exists for the same object type and a subtype.
1103   *
1104   * If an object type does not support any subtypes, such as users or comments, you should commonly call this function
1105   * without passing a subtype.
1106   *
1107   * @since 3.3.0
1108   * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified
1109   *              to support an array of data to attach to registered meta keys}. Previous arguments for
1110   *              `$sanitize_callback` and `$auth_callback` have been folded into this array.
1111   * @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
1112   *
1113   * @param string $object_type    Type of object this meta is registered to.
1114   * @param string $meta_key       Meta key to register.
1115   * @param array  $args {
1116   *     Data used to describe the meta key when registered.
1117   *
1118   *     @type string $object_subtype    A subtype; e.g. if the object type is "post", the post type. If left empty,
1119   *                                     the meta key will be registered on the entire object type. Default empty.
1120   *     @type string $type              The type of data associated with this meta key.
1121   *                                     Valid values are 'string', 'boolean', 'integer', and 'number'.
1122   *     @type string $description       A description of the data attached to this meta key.
1123   *     @type bool   $single            Whether the meta key has one value per object, or an array of values per object.
1124   *     @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
1125   *     @type string $auth_callback     Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
1126   *     @type bool   $show_in_rest      Whether data associated with this meta key can be considered public.
1127   * }
1128   * @param string|array $deprecated Deprecated. Use `$args` instead.
1129   *
1130   * @return bool True if the meta key was successfully registered in the global array, false if not.
1131   *                       Registering a meta key with distinct sanitize and auth callbacks will fire those
1132   *                       callbacks, but will not add to the global registry.
1133   */
1134  function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
1135      global $wp_meta_keys;
1136  
1137      if ( ! is_array( $wp_meta_keys ) ) {
1138          $wp_meta_keys = array();
1139      }
1140  
1141      $defaults = array(
1142          'object_subtype'    => '',
1143          'type'              => 'string',
1144          'description'       => '',
1145          'single'            => false,
1146          'sanitize_callback' => null,
1147          'auth_callback'     => null,
1148          'show_in_rest'      => false,
1149      );
1150  
1151      // There used to be individual args for sanitize and auth callbacks
1152      $has_old_sanitize_cb = false;
1153      $has_old_auth_cb     = false;
1154  
1155      if ( is_callable( $args ) ) {
1156          $args = array(
1157              'sanitize_callback' => $args,
1158          );
1159  
1160          $has_old_sanitize_cb = true;
1161      } else {
1162          $args = (array) $args;
1163      }
1164  
1165      if ( is_callable( $deprecated ) ) {
1166          $args['auth_callback'] = $deprecated;
1167          $has_old_auth_cb       = true;
1168      }
1169  
1170      /**
1171       * Filters the registration arguments when registering meta.
1172       *
1173       * @since 4.6.0
1174       *
1175       * @param array  $args        Array of meta registration arguments.
1176       * @param array  $defaults    Array of default arguments.
1177       * @param string $object_type Object type.
1178       * @param string $meta_key    Meta key.
1179       */
1180      $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
1181      $args = wp_parse_args( $args, $defaults );
1182  
1183      $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
1184  
1185      // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
1186      if ( empty( $args['auth_callback'] ) ) {
1187          if ( is_protected_meta( $meta_key, $object_type ) ) {
1188              $args['auth_callback'] = '__return_false';
1189          } else {
1190              $args['auth_callback'] = '__return_true';
1191          }
1192      }
1193  
1194      // Back-compat: old sanitize and auth callbacks are applied to all of an object type.
1195      if ( is_callable( $args['sanitize_callback'] ) ) {
1196          if ( ! empty( $object_subtype ) ) {
1197              add_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'], 10, 4 );
1198          } else {
1199              add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
1200          }
1201      }
1202  
1203      if ( is_callable( $args['auth_callback'] ) ) {
1204          if ( ! empty( $object_subtype ) ) {
1205              add_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'], 10, 6 );
1206          } else {
1207              add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
1208          }
1209      }
1210  
1211      // Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
1212      if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
1213          unset( $args['object_subtype'] );
1214  
1215          $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
1216  
1217          return true;
1218      }
1219  
1220      return false;
1221  }
1222  
1223  /**
1224   * Checks if a meta key is registered.
1225   *
1226   * @since 4.6.0
1227   * @since 4.9.8 The `$object_subtype` parameter was added.
1228   *
1229   * @param string $object_type    The type of object.
1230   * @param string $meta_key       The meta key.
1231   * @param string $object_subtype Optional. The subtype of the object type.
1232   *
1233   * @return bool True if the meta key is registered to the object type and, if provided,
1234   *              the object subtype. False if not.
1235   */
1236  function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
1237      $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
1238  
1239      return isset( $meta_keys[ $meta_key ] );
1240  }
1241  
1242  /**
1243   * Unregisters a meta key from the list of registered keys.
1244   *
1245   * @since 4.6.0
1246   * @since 4.9.8 The `$object_subtype` parameter was added.
1247   *
1248   * @param string $object_type    The type of object.
1249   * @param string $meta_key       The meta key.
1250   * @param string $object_subtype Optional. The subtype of the object type.
1251   * @return bool True if successful. False if the meta key was not registered.
1252   */
1253  function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
1254      global $wp_meta_keys;
1255  
1256      if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
1257          return false;
1258      }
1259  
1260      $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ];
1261  
1262      if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
1263          if ( ! empty( $object_subtype ) ) {
1264              remove_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['sanitize_callback'] );
1265          } else {
1266              remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
1267          }
1268      }
1269  
1270      if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
1271          if ( ! empty( $object_subtype ) ) {
1272              remove_filter( "auth_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $args['auth_callback'] );
1273          } else {
1274              remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
1275          }
1276      }
1277  
1278      unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
1279  
1280      // Do some clean up
1281      if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
1282          unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
1283      }
1284      if ( empty( $wp_meta_keys[ $object_type ] ) ) {
1285          unset( $wp_meta_keys[ $object_type ] );
1286      }
1287  
1288      return true;
1289  }
1290  
1291  /**
1292   * Retrieves a list of registered meta keys for an object type.
1293   *
1294   * @since 4.6.0
1295   * @since 4.9.8 The `$object_subtype` parameter was added.
1296   *
1297   * @param string $object_type    The type of object. Post, comment, user, term.
1298   * @param string $object_subtype Optional. The subtype of the object type.
1299   * @return array List of registered meta keys.
1300   */
1301  function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
1302      global $wp_meta_keys;
1303  
1304      if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
1305          return array();
1306      }
1307  
1308      return $wp_meta_keys[ $object_type ][ $object_subtype ];
1309  }
1310  
1311  /**
1312   * Retrieves registered metadata for a specified object.
1313   *
1314   * The results include both meta that is registered specifically for the
1315   * object's subtype and meta that is registered for the entire object type.
1316   *
1317   * @since 4.6.0
1318   *
1319   * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
1320   * @param int    $object_id   ID of the object the metadata is for.
1321   * @param string $meta_key    Optional. Registered metadata key. If not specified, retrieve all registered
1322   *                            metadata for the specified object.
1323   * @return mixed A single value or array of values for a key if specified. An array of all registered keys
1324   *               and values for an object ID if not. False if a given $meta_key is not registered.
1325   */
1326  function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
1327      $object_subtype = get_object_subtype( $object_type, $object_id );
1328  
1329      if ( ! empty( $meta_key ) ) {
1330          if ( ! empty( $object_subtype ) && ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
1331              $object_subtype = '';
1332          }
1333  
1334          if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) {
1335              return false;
1336          }
1337  
1338          $meta_keys     = get_registered_meta_keys( $object_type, $object_subtype );
1339          $meta_key_data = $meta_keys[ $meta_key ];
1340  
1341          $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
1342  
1343          return $data;
1344      }
1345  
1346      $data = get_metadata( $object_type, $object_id );
1347      if ( ! $data ) {
1348          return array();
1349      }
1350  
1351      $meta_keys = get_registered_meta_keys( $object_type );
1352      if ( ! empty( $object_subtype ) ) {
1353          $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $object_type, $object_subtype ) );
1354      }
1355  
1356      return array_intersect_key( $data, $meta_keys );
1357  }
1358  
1359  /**
1360   * Filter out `register_meta()` args based on a whitelist.
1361   * `register_meta()` args may change over time, so requiring the whitelist
1362   * to be explicitly turned off is a warranty seal of sorts.
1363   *
1364   * @access private
1365   * @since 4.6.0
1366   *
1367   * @param array $args         Arguments from `register_meta()`.
1368   * @param array $default_args Default arguments for `register_meta()`.
1369   *
1370   * @return array Filtered arguments.
1371   */
1372  function _wp_register_meta_args_whitelist( $args, $default_args ) {
1373      return array_intersect_key( $args, $default_args );
1374  }
1375  
1376  /**
1377   * Returns the object subtype for a given object ID of a specific type.
1378   *
1379   * @since 4.9.8
1380   *
1381   * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
1382   * @param int    $object_id   ID of the object to retrieve its subtype.
1383   * @return string The object subtype or an empty string if unspecified subtype.
1384   */
1385  function get_object_subtype( $object_type, $object_id ) {
1386      $object_id      = (int) $object_id;
1387      $object_subtype = '';
1388  
1389      switch ( $object_type ) {
1390          case 'post':
1391              $post_type = get_post_type( $object_id );
1392  
1393              if ( ! empty( $post_type ) ) {
1394                  $object_subtype = $post_type;
1395              }
1396              break;
1397  
1398          case 'term':
1399              $term = get_term( $object_id );
1400              if ( ! $term instanceof WP_Term ) {
1401                  break;
1402              }
1403  
1404              $object_subtype = $term->taxonomy;
1405              break;
1406  
1407          case 'comment':
1408              $comment = get_comment( $object_id );
1409              if ( ! $comment ) {
1410                  break;
1411              }
1412  
1413              $object_subtype = 'comment';
1414              break;
1415  
1416          case 'user':
1417              $user = get_user_by( 'id', $object_id );
1418              if ( ! $user ) {
1419                  break;
1420              }
1421  
1422              $object_subtype = 'user';
1423              break;
1424      }
1425  
1426      /**
1427       * Filters the object subtype identifier for a non standard object type.
1428       *
1429       * The dynamic portion of the hook, `$object_type`, refers to the object
1430       * type (post, comment, term, or user).
1431       *
1432       * @since 4.9.8
1433       *
1434       * @param string $object_subtype Empty string to override.
1435       * @param int    $object_id      ID of the object to get the subtype for.
1436       */
1437      return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id );
1438  }


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7