wp/wp-includes/meta.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     9  * @package WordPress
     9  * @package WordPress
    10  * @subpackage Meta
    10  * @subpackage Meta
    11  */
    11  */
    12 
    12 
    13 /**
    13 /**
    14  * Add metadata for the specified object.
    14  * Adds metadata for the specified object.
    15  *
    15  *
    16  * @since 2.9.0
    16  * @since 2.9.0
    17  *
    17  *
    18  * @global wpdb $wpdb WordPress database abstraction object.
    18  * @global wpdb $wpdb WordPress database abstraction object.
    19  *
    19  *
    20  * @param string $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
    20  * @param string $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
    21  * @param int    $object_id  ID of the object metadata is for
    21  *                           or any other object type with an associated meta table.
    22  * @param string $meta_key   Metadata key
    22  * @param int    $object_id  ID of the object metadata is for.
       
    23  * @param string $meta_key   Metadata key.
    23  * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
    24  * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
    24  * @param bool   $unique     Optional, default is false.
    25  * @param bool   $unique     Optional. Whether the specified metadata key should be unique for the object.
    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,
    26  *                           If true, and the object already has a value for the specified metadata key,
    27  *                           no change will be made.
    27  *                           no change will be made. Default false.
    28  * @return int|false The meta ID on success, false on failure.
    28  * @return int|false The meta ID on success, false on failure.
    29  */
    29  */
    30 function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) {
    30 function add_metadata( $meta_type, $object_id, $meta_key, $meta_value, $unique = false ) {
    31 	global $wpdb;
    31 	global $wpdb;
    32 
    32 
    52 	$meta_key   = wp_unslash( $meta_key );
    52 	$meta_key   = wp_unslash( $meta_key );
    53 	$meta_value = wp_unslash( $meta_value );
    53 	$meta_value = wp_unslash( $meta_value );
    54 	$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
    54 	$meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
    55 
    55 
    56 	/**
    56 	/**
    57 	 * Filters whether to add metadata of a specific type.
    57 	 * Short-circuits adding metadata of a specific type.
    58 	 *
    58 	 *
    59 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
    59 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
    60 	 * object type (comment, post, term, or user). Returning a non-null value
    60 	 * (post, comment, term, user, or any other type with an associated meta table).
    61 	 * will effectively short-circuit the function.
    61 	 * Returning a non-null value will effectively short-circuit the function.
    62 	 *
    62 	 *
    63 	 * @since 3.1.0
    63 	 * @since 3.1.0
    64 	 *
    64 	 *
    65 	 * @param null|bool $check      Whether to allow adding metadata for the given type.
    65 	 * @param null|bool $check      Whether to allow adding metadata for the given type.
    66 	 * @param int       $object_id  Object ID.
    66 	 * @param int       $object_id  ID of the object metadata is for.
    67 	 * @param string    $meta_key   Meta key.
    67 	 * @param string    $meta_key   Metadata key.
    68 	 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
    68 	 * @param mixed     $meta_value Metadata value. Must be serializable if non-scalar.
    69 	 * @param bool      $unique     Whether the specified meta key should be unique
    69 	 * @param bool      $unique     Whether the specified meta key should be unique for the object.
    70 	 *                              for the object. Optional. Default false.
       
    71 	 */
    70 	 */
    72 	$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
    71 	$check = apply_filters( "add_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $unique );
    73 	if ( null !== $check ) {
    72 	if ( null !== $check ) {
    74 		return $check;
    73 		return $check;
    75 	}
    74 	}
    88 	$meta_value  = maybe_serialize( $meta_value );
    87 	$meta_value  = maybe_serialize( $meta_value );
    89 
    88 
    90 	/**
    89 	/**
    91 	 * Fires immediately before meta of a specific type is added.
    90 	 * Fires immediately before meta of a specific type is added.
    92 	 *
    91 	 *
    93 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
    92 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
    94 	 * object type (comment, post, term, or user).
    93 	 * (post, comment, term, user, or any other type with an associated meta table).
    95 	 *
    94 	 *
    96 	 * @since 3.1.0
    95 	 * @since 3.1.0
    97 	 *
    96 	 *
    98 	 * @param int    $object_id   Object ID.
    97 	 * @param int    $object_id   ID of the object metadata is for.
    99 	 * @param string $meta_key    Meta key.
    98 	 * @param string $meta_key    Metadata key.
   100 	 * @param mixed  $_meta_value Meta value.
    99 	 * @param mixed  $_meta_value Metadata value. Serialized if non-scalar.
   101 	 */
   100 	 */
   102 	do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
   101 	do_action( "add_{$meta_type}_meta", $object_id, $meta_key, $_meta_value );
   103 
   102 
   104 	$result = $wpdb->insert(
   103 	$result = $wpdb->insert(
   105 		$table,
   104 		$table,
   119 	wp_cache_delete( $object_id, $meta_type . '_meta' );
   118 	wp_cache_delete( $object_id, $meta_type . '_meta' );
   120 
   119 
   121 	/**
   120 	/**
   122 	 * Fires immediately after meta of a specific type is added.
   121 	 * Fires immediately after meta of a specific type is added.
   123 	 *
   122 	 *
   124 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   123 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   125 	 * object type (comment, post, term, or user).
   124 	 * (post, comment, term, user, or any other type with an associated meta table).
   126 	 *
   125 	 *
   127 	 * @since 2.9.0
   126 	 * @since 2.9.0
   128 	 *
   127 	 *
   129 	 * @param int    $mid         The meta ID after successful update.
   128 	 * @param int    $mid         The meta ID after successful update.
   130 	 * @param int    $object_id   Object ID.
   129 	 * @param int    $object_id   ID of the object metadata is for.
   131 	 * @param string $meta_key    Meta key.
   130 	 * @param string $meta_key    Metadata key.
   132 	 * @param mixed  $_meta_value Meta value.
   131 	 * @param mixed  $_meta_value Metadata value. Serialized if non-scalar.
   133 	 */
   132 	 */
   134 	do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
   133 	do_action( "added_{$meta_type}_meta", $mid, $object_id, $meta_key, $_meta_value );
   135 
   134 
   136 	return $mid;
   135 	return $mid;
   137 }
   136 }
   138 
   137 
   139 /**
   138 /**
   140  * Update metadata for the specified object. If no value already exists for the specified object
   139  * Updates metadata for the specified object. If no value already exists for the specified object
   141  * ID and metadata key, the metadata will be added.
   140  * ID and metadata key, the metadata will be added.
   142  *
   141  *
   143  * @since 2.9.0
   142  * @since 2.9.0
   144  *
   143  *
   145  * @global wpdb $wpdb WordPress database abstraction object.
   144  * @global wpdb $wpdb WordPress database abstraction object.
   146  *
   145  *
   147  * @param string $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
   146  * @param string $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   148  * @param int    $object_id  ID of the object metadata is for
   147  *                           or any other object type with an associated meta table.
   149  * @param string $meta_key   Metadata key
   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.
   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
   151  * @param mixed  $prev_value Optional. Previous value to check before updating.
   152  *                           the specified value. Otherwise, update all entries.
   152  *                           If specified, only update existing metadata entries with
   153  * @return int|bool The new meta field ID if a field with the given key didn't exist and was
   153  *                           this value. Otherwise, update all entries. Default empty.
   154  *                  therefore added, true on successful update, false on failure.
   154  * @return int|bool The new meta field ID if a field with the given key didn't exist
       
   155  *                  and was therefore added, true on successful update,
       
   156  *                  false on failure or if the value passed to the function
       
   157  *                  is the same as the one that is already in the database.
   155  */
   158  */
   156 function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) {
   159 function update_metadata( $meta_type, $object_id, $meta_key, $meta_value, $prev_value = '' ) {
   157 	global $wpdb;
   160 	global $wpdb;
   158 
   161 
   159 	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
   162 	if ( ! $meta_type || ! $meta_key || ! is_numeric( $object_id ) ) {
   171 	}
   174 	}
   172 
   175 
   173 	$meta_subtype = get_object_subtype( $meta_type, $object_id );
   176 	$meta_subtype = get_object_subtype( $meta_type, $object_id );
   174 
   177 
   175 	$column    = sanitize_key( $meta_type . '_id' );
   178 	$column    = sanitize_key( $meta_type . '_id' );
   176 	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
   179 	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
   177 
   180 
   178 	// expected_slashed ($meta_key)
   181 	// expected_slashed ($meta_key)
   179 	$raw_meta_key = $meta_key;
   182 	$raw_meta_key = $meta_key;
   180 	$meta_key     = wp_unslash( $meta_key );
   183 	$meta_key     = wp_unslash( $meta_key );
   181 	$passed_value = $meta_value;
   184 	$passed_value = $meta_value;
   182 	$meta_value   = wp_unslash( $meta_value );
   185 	$meta_value   = wp_unslash( $meta_value );
   183 	$meta_value   = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
   186 	$meta_value   = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
   184 
   187 
   185 	/**
   188 	/**
   186 	 * Filters whether to update metadata of a specific type.
   189 	 * Short-circuits updating metadata of a specific type.
   187 	 *
   190 	 *
   188 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   191 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   189 	 * object type (comment, post, term, or user). Returning a non-null value
   192 	 * (post, comment, term, user, or any other type with an associated meta table).
   190 	 * will effectively short-circuit the function.
   193 	 * Returning a non-null value will effectively short-circuit the function.
   191 	 *
   194 	 *
   192 	 * @since 3.1.0
   195 	 * @since 3.1.0
   193 	 *
   196 	 *
   194 	 * @param null|bool $check      Whether to allow updating metadata for the given type.
   197 	 * @param null|bool $check      Whether to allow updating metadata for the given type.
   195 	 * @param int       $object_id  Object ID.
   198 	 * @param int       $object_id  ID of the object metadata is for.
   196 	 * @param string    $meta_key   Meta key.
   199 	 * @param string    $meta_key   Metadata key.
   197 	 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
   200 	 * @param mixed     $meta_value Metadata value. Must be serializable if non-scalar.
   198 	 * @param mixed     $prev_value Optional. If specified, only update existing
   201 	 * @param mixed     $prev_value Optional. Previous value to check before updating.
   199 	 *                              metadata entries with the specified value.
   202 	 *                              If specified, only update existing metadata entries with
   200 	 *                              Otherwise, update all entries.
   203 	 *                              this value. Otherwise, update all entries.
   201 	 */
   204 	 */
   202 	$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
   205 	$check = apply_filters( "update_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $prev_value );
   203 	if ( null !== $check ) {
   206 	if ( null !== $check ) {
   204 		return (bool) $check;
   207 		return (bool) $check;
   205 	}
   208 	}
   206 
   209 
   207 	// Compare existing value to new value if no prev value given and the key exists only once.
   210 	// Compare existing value to new value if no prev value given and the key exists only once.
   208 	if ( empty( $prev_value ) ) {
   211 	if ( empty( $prev_value ) ) {
   209 		$old_value = get_metadata( $meta_type, $object_id, $meta_key );
   212 		$old_value = get_metadata_raw( $meta_type, $object_id, $meta_key );
   210 		if ( count( $old_value ) == 1 ) {
   213 		if ( is_countable( $old_value ) && count( $old_value ) === 1 ) {
   211 			if ( $old_value[0] === $meta_value ) {
   214 			if ( $old_value[0] === $meta_value ) {
   212 				return false;
   215 				return false;
   213 			}
   216 			}
   214 		}
   217 		}
   215 	}
   218 	}
   235 
   238 
   236 	foreach ( $meta_ids as $meta_id ) {
   239 	foreach ( $meta_ids as $meta_id ) {
   237 		/**
   240 		/**
   238 		 * Fires immediately before updating metadata of a specific type.
   241 		 * Fires immediately before updating metadata of a specific type.
   239 		 *
   242 		 *
   240 		 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   243 		 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   241 		 * object type (comment, post, term, or user).
   244 		 * (post, comment, term, user, or any other type with an associated meta table).
   242 		 *
   245 		 *
   243 		 * @since 2.9.0
   246 		 * @since 2.9.0
   244 		 *
   247 		 *
   245 		 * @param int    $meta_id     ID of the metadata entry to update.
   248 		 * @param int    $meta_id     ID of the metadata entry to update.
   246 		 * @param int    $object_id   Object ID.
   249 		 * @param int    $object_id   ID of the object metadata is for.
   247 		 * @param string $meta_key    Meta key.
   250 		 * @param string $meta_key    Metadata key.
   248 		 * @param mixed  $_meta_value Meta value.
   251 		 * @param mixed  $_meta_value Metadata value. Serialized if non-scalar.
   249 		 */
   252 		 */
   250 		do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
   253 		do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
   251 
   254 
   252 		if ( 'post' == $meta_type ) {
   255 		if ( 'post' === $meta_type ) {
   253 			/**
   256 			/**
   254 			 * Fires immediately before updating a post's metadata.
   257 			 * Fires immediately before updating a post's metadata.
   255 			 *
   258 			 *
   256 			 * @since 2.9.0
   259 			 * @since 2.9.0
   257 			 *
   260 			 *
   258 			 * @param int    $meta_id    ID of metadata entry to update.
   261 			 * @param int    $meta_id    ID of metadata entry to update.
   259 			 * @param int    $object_id  Post ID.
   262 			 * @param int    $object_id  Post ID.
   260 			 * @param string $meta_key   Meta key.
   263 			 * @param string $meta_key   Metadata key.
   261 			 * @param mixed  $meta_value Meta value. This will be a PHP-serialized string representation of the value if
   264 			 * @param mixed  $meta_value Metadata value. This will be a PHP-serialized string representation of the value
   262 			 *                           the value is an array, an object, or itself a PHP-serialized string.
   265 			 *                           if the value is an array, an object, or itself a PHP-serialized string.
   263 			 */
   266 			 */
   264 			do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
   267 			do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
   265 		}
   268 		}
   266 	}
   269 	}
   267 
   270 
   274 
   277 
   275 	foreach ( $meta_ids as $meta_id ) {
   278 	foreach ( $meta_ids as $meta_id ) {
   276 		/**
   279 		/**
   277 		 * Fires immediately after updating metadata of a specific type.
   280 		 * Fires immediately after updating metadata of a specific type.
   278 		 *
   281 		 *
   279 		 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   282 		 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   280 		 * object type (comment, post, term, or user).
   283 		 * (post, comment, term, user, or any other type with an associated meta table).
   281 		 *
   284 		 *
   282 		 * @since 2.9.0
   285 		 * @since 2.9.0
   283 		 *
   286 		 *
   284 		 * @param int    $meta_id     ID of updated metadata entry.
   287 		 * @param int    $meta_id     ID of updated metadata entry.
   285 		 * @param int    $object_id   Object ID.
   288 		 * @param int    $object_id   ID of the object metadata is for.
   286 		 * @param string $meta_key    Meta key.
   289 		 * @param string $meta_key    Metadata key.
   287 		 * @param mixed  $_meta_value Meta value.
   290 		 * @param mixed  $_meta_value Metadata value. Serialized if non-scalar.
   288 		 */
   291 		 */
   289 		do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
   292 		do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
   290 
   293 
   291 		if ( 'post' == $meta_type ) {
   294 		if ( 'post' === $meta_type ) {
   292 			/**
   295 			/**
   293 			 * Fires immediately after updating a post's metadata.
   296 			 * Fires immediately after updating a post's metadata.
   294 			 *
   297 			 *
   295 			 * @since 2.9.0
   298 			 * @since 2.9.0
   296 			 *
   299 			 *
   297 			 * @param int    $meta_id    ID of updated metadata entry.
   300 			 * @param int    $meta_id    ID of updated metadata entry.
   298 			 * @param int    $object_id  Post ID.
   301 			 * @param int    $object_id  Post ID.
   299 			 * @param string $meta_key   Meta key.
   302 			 * @param string $meta_key   Metadata key.
   300 			 * @param mixed  $meta_value Meta value. This will be a PHP-serialized string representation of the value if
   303 			 * @param mixed  $meta_value Metadata value. This will be a PHP-serialized string representation of the value
   301 			 *                           the value is an array, an object, or itself a PHP-serialized string.
   304 			 *                           if the value is an array, an object, or itself a PHP-serialized string.
   302 			 */
   305 			 */
   303 			do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
   306 			do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
   304 		}
   307 		}
   305 	}
   308 	}
   306 
   309 
   307 	return true;
   310 	return true;
   308 }
   311 }
   309 
   312 
   310 /**
   313 /**
   311  * Delete metadata for the specified object.
   314  * Deletes metadata for the specified object.
   312  *
   315  *
   313  * @since 2.9.0
   316  * @since 2.9.0
   314  *
   317  *
   315  * @global wpdb $wpdb WordPress database abstraction object.
   318  * @global wpdb $wpdb WordPress database abstraction object.
   316  *
   319  *
   317  * @param string $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
   320  * @param string $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   318  * @param int    $object_id  ID of the object metadata is for
   321  *                           or any other object type with an associated meta table.
   319  * @param string $meta_key   Metadata key
   322  * @param int    $object_id  ID of the object metadata is for.
   320  * @param mixed  $meta_value Optional. Metadata value. Must be serializable if non-scalar. If specified, only delete
   323  * @param string $meta_key   Metadata key.
   321  *                           metadata entries with this value. Otherwise, delete all entries with the specified meta_key.
   324  * @param mixed  $meta_value Optional. Metadata value. Must be serializable if non-scalar.
   322  *                           Pass `null`, `false`, or an empty string to skip this check. (For backward compatibility,
   325  *                           If specified, only delete metadata entries with this value.
   323  *                           it is not possible to pass an empty string to delete those entries with an empty string
   326  *                           Otherwise, delete all entries with the specified meta_key.
   324  *                           for a value.)
   327  *                           Pass `null`, `false`, or an empty string to skip this check.
   325  * @param bool   $delete_all Optional, default is false. If true, delete matching metadata entries for all objects,
   328  *                           (For backward compatibility, it is not possible to pass an empty string
   326  *                           ignoring the specified object_id. Otherwise, only delete matching metadata entries for
   329  *                           to delete those entries with an empty string for a value.)
   327  *                           the specified object_id.
   330  * @param bool   $delete_all Optional. If true, delete matching metadata entries for all objects,
       
   331  *                           ignoring the specified object_id. Otherwise, only delete
       
   332  *                           matching metadata entries for the specified object_id. Default false.
   328  * @return bool True on successful delete, false on failure.
   333  * @return bool True on successful delete, false on failure.
   329  */
   334  */
   330 function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
   335 function delete_metadata( $meta_type, $object_id, $meta_key, $meta_value = '', $delete_all = false ) {
   331 	global $wpdb;
   336 	global $wpdb;
   332 
   337 
   343 	if ( ! $table ) {
   348 	if ( ! $table ) {
   344 		return false;
   349 		return false;
   345 	}
   350 	}
   346 
   351 
   347 	$type_column = sanitize_key( $meta_type . '_id' );
   352 	$type_column = sanitize_key( $meta_type . '_id' );
   348 	$id_column   = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
   353 	$id_column   = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
       
   354 
   349 	// expected_slashed ($meta_key)
   355 	// expected_slashed ($meta_key)
   350 	$meta_key   = wp_unslash( $meta_key );
   356 	$meta_key   = wp_unslash( $meta_key );
   351 	$meta_value = wp_unslash( $meta_value );
   357 	$meta_value = wp_unslash( $meta_value );
   352 
   358 
   353 	/**
   359 	/**
   354 	 * Filters whether to delete metadata of a specific type.
   360 	 * Short-circuits deleting metadata of a specific type.
   355 	 *
   361 	 *
   356 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   362 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   357 	 * object type (comment, post, term, or user). Returning a non-null value
   363 	 * (post, comment, term, user, or any other type with an associated meta table).
   358 	 * will effectively short-circuit the function.
   364 	 * Returning a non-null value will effectively short-circuit the function.
   359 	 *
   365 	 *
   360 	 * @since 3.1.0
   366 	 * @since 3.1.0
   361 	 *
   367 	 *
   362 	 * @param null|bool $delete     Whether to allow metadata deletion of the given type.
   368 	 * @param null|bool $delete     Whether to allow metadata deletion of the given type.
   363 	 * @param int       $object_id  Object ID.
   369 	 * @param int       $object_id  ID of the object metadata is for.
   364 	 * @param string    $meta_key   Meta key.
   370 	 * @param string    $meta_key   Metadata key.
   365 	 * @param mixed     $meta_value Meta value. Must be serializable if non-scalar.
   371 	 * @param mixed     $meta_value Metadata value. Must be serializable if non-scalar.
   366 	 * @param bool      $delete_all Whether to delete the matching metadata entries
   372 	 * @param bool      $delete_all Whether to delete the matching metadata entries
   367 	 *                              for all objects, ignoring the specified $object_id.
   373 	 *                              for all objects, ignoring the specified $object_id.
   368 	 *                              Default false.
   374 	 *                              Default false.
   369 	 */
   375 	 */
   370 	$check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
   376 	$check = apply_filters( "delete_{$meta_type}_metadata", null, $object_id, $meta_key, $meta_value, $delete_all );
   399 	}
   405 	}
   400 
   406 
   401 	/**
   407 	/**
   402 	 * Fires immediately before deleting metadata of a specific type.
   408 	 * Fires immediately before deleting metadata of a specific type.
   403 	 *
   409 	 *
   404 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   410 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   405 	 * object type (comment, post, term, or user).
   411 	 * (post, comment, term, user, or any other type with an associated meta table).
   406 	 *
   412 	 *
   407 	 * @since 3.1.0
   413 	 * @since 3.1.0
   408 	 *
   414 	 *
   409 	 * @param array  $meta_ids    An array of metadata entry IDs to delete.
   415 	 * @param string[] $meta_ids    An array of metadata entry IDs to delete.
   410 	 * @param int    $object_id   Object ID.
   416 	 * @param int      $object_id   ID of the object metadata is for.
   411 	 * @param string $meta_key    Meta key.
   417 	 * @param string   $meta_key    Metadata key.
   412 	 * @param mixed  $_meta_value Meta value.
   418 	 * @param mixed    $_meta_value Metadata value. Serialized if non-scalar.
   413 	 */
   419 	 */
   414 	do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
   420 	do_action( "delete_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
   415 
   421 
   416 	// Old-style action.
   422 	// Old-style action.
   417 	if ( 'post' == $meta_type ) {
   423 	if ( 'post' === $meta_type ) {
   418 		/**
   424 		/**
   419 		 * Fires immediately before deleting metadata for a post.
   425 		 * Fires immediately before deleting metadata for a post.
   420 		 *
   426 		 *
   421 		 * @since 2.9.0
   427 		 * @since 2.9.0
   422 		 *
   428 		 *
   423 		 * @param array $meta_ids An array of post metadata entry IDs to delete.
   429 		 * @param string[] $meta_ids An array of metadata entry IDs to delete.
   424 		 */
   430 		 */
   425 		do_action( 'delete_postmeta', $meta_ids );
   431 		do_action( 'delete_postmeta', $meta_ids );
   426 	}
   432 	}
   427 
   433 
   428 	$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )';
   434 	$query = "DELETE FROM $table WHERE $id_column IN( " . implode( ',', $meta_ids ) . ' )';
   442 	}
   448 	}
   443 
   449 
   444 	/**
   450 	/**
   445 	 * Fires immediately after deleting metadata of a specific type.
   451 	 * Fires immediately after deleting metadata of a specific type.
   446 	 *
   452 	 *
   447 	 * The dynamic portion of the hook name, `$meta_type`, refers to the meta
   453 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   448 	 * object type (comment, post, term, or user).
   454 	 * (post, comment, term, user, or any other type with an associated meta table).
   449 	 *
   455 	 *
   450 	 * @since 2.9.0
   456 	 * @since 2.9.0
   451 	 *
   457 	 *
   452 	 * @param array  $meta_ids    An array of deleted metadata entry IDs.
   458 	 * @param string[] $meta_ids    An array of metadata entry IDs to delete.
   453 	 * @param int    $object_id   Object ID.
   459 	 * @param int      $object_id   ID of the object metadata is for.
   454 	 * @param string $meta_key    Meta key.
   460 	 * @param string   $meta_key    Metadata key.
   455 	 * @param mixed  $_meta_value Meta value.
   461 	 * @param mixed    $_meta_value Metadata value. Serialized if non-scalar.
   456 	 */
   462 	 */
   457 	do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
   463 	do_action( "deleted_{$meta_type}_meta", $meta_ids, $object_id, $meta_key, $_meta_value );
   458 
   464 
   459 	// Old-style action.
   465 	// Old-style action.
   460 	if ( 'post' == $meta_type ) {
   466 	if ( 'post' === $meta_type ) {
   461 		/**
   467 		/**
   462 		 * Fires immediately after deleting metadata for a post.
   468 		 * Fires immediately after deleting metadata for a post.
   463 		 *
   469 		 *
   464 		 * @since 2.9.0
   470 		 * @since 2.9.0
   465 		 *
   471 		 *
   466 		 * @param array $meta_ids An array of deleted post metadata entry IDs.
   472 		 * @param string[] $meta_ids An array of metadata entry IDs to delete.
   467 		 */
   473 		 */
   468 		do_action( 'deleted_postmeta', $meta_ids );
   474 		do_action( 'deleted_postmeta', $meta_ids );
   469 	}
   475 	}
   470 
   476 
   471 	return true;
   477 	return true;
   472 }
   478 }
   473 
   479 
   474 /**
   480 /**
   475  * Retrieve metadata for the specified object.
   481  * Retrieves the value of a metadata field for the specified object type and ID.
       
   482  *
       
   483  * If the meta field exists, a single value is returned if `$single` is true,
       
   484  * or an array of values if it's false.
       
   485  *
       
   486  * If the meta field does not exist, the result depends on get_metadata_default().
       
   487  * By default, an empty string is returned if `$single` is true, or an empty array
       
   488  * if it's false.
   476  *
   489  *
   477  * @since 2.9.0
   490  * @since 2.9.0
   478  *
   491  *
   479  * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
   492  * @see get_metadata_raw()
   480  * @param int    $object_id ID of the object metadata is for
   493  * @see get_metadata_default()
       
   494  *
       
   495  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
   496  *                          or any other object type with an associated meta table.
       
   497  * @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
   498  * @param string $meta_key  Optional. Metadata key. If not specified, retrieve all metadata for
   482  *                          the specified object.
   499  *                          the specified object. Default empty.
   483  * @param bool   $single    Optional, default is false.
   500  * @param bool   $single    Optional. If true, return only the first value of the specified meta_key.
   484  *                          If true, return only the first value of the specified meta_key.
   501  *                          This parameter has no effect if meta_key is not specified. Default false.
   485  *                          This parameter has no effect if meta_key is not specified.
   502  * @return mixed Single metadata value, or array of values.
   486  * @return mixed Single metadata value, or array of values
   503  *               False if there's a problem with the parameters passed to the function.
   487  */
   504  */
   488 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
   505 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
       
   506 	$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
       
   507 	if ( ! is_null( $value ) ) {
       
   508 		return $value;
       
   509 	}
       
   510 
       
   511 	return get_metadata_default( $meta_type, $object_id, $meta_key, $single );
       
   512 }
       
   513 
       
   514 /**
       
   515  * Retrieves raw metadata value for the specified object.
       
   516  *
       
   517  * @since 5.5.0
       
   518  *
       
   519  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
   520  *                          or any other object type with an associated meta table.
       
   521  * @param int    $object_id ID of the object metadata is for.
       
   522  * @param string $meta_key  Optional. Metadata key. If not specified, retrieve all metadata for
       
   523  *                          the specified object. Default empty.
       
   524  * @param bool   $single    Optional. If true, return only the first value of the specified meta_key.
       
   525  *                          This parameter has no effect if meta_key is not specified. Default false.
       
   526  * @return mixed Single metadata value, or array of values. Null if the value does not exist.
       
   527  *               False if there's a problem with the parameters passed to the function.
       
   528  */
       
   529 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
   489 	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
   530 	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
   490 		return false;
   531 		return false;
   491 	}
   532 	}
   492 
   533 
   493 	$object_id = absint( $object_id );
   534 	$object_id = absint( $object_id );
   494 	if ( ! $object_id ) {
   535 	if ( ! $object_id ) {
   495 		return false;
   536 		return false;
   496 	}
   537 	}
   497 
   538 
   498 	/**
   539 	/**
   499 	 * Filters whether to retrieve metadata of a specific type.
   540 	 * Short-circuits the return value of a meta field.
   500 	 *
   541 	 *
   501 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   542 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   502 	 * object type (comment, post, term, or user). Returning a non-null value
   543 	 * (post, comment, term, user, or any other type with an associated meta table).
   503 	 * will effectively short-circuit the function.
   544 	 * Returning a non-null value will effectively short-circuit the function.
       
   545 	 *
       
   546 	 * Possible filter names include:
       
   547 	 *
       
   548 	 *  - `get_post_metadata`
       
   549 	 *  - `get_comment_metadata`
       
   550 	 *  - `get_term_metadata`
       
   551 	 *  - `get_user_metadata`
   504 	 *
   552 	 *
   505 	 * @since 3.1.0
   553 	 * @since 3.1.0
   506 	 *
   554 	 * @since 5.5.0 Added the `$meta_type` parameter.
   507 	 * @param null|array|string $value     The value get_metadata() should return - a single metadata value,
   555 	 *
   508 	 *                                     or an array of values.
   556 	 * @param mixed  $value     The value to return, either a single metadata value or an array
   509 	 * @param int               $object_id Object ID.
   557 	 *                          of values depending on the value of `$single`. Default null.
   510 	 * @param string            $meta_key  Meta key.
   558 	 * @param int    $object_id ID of the object metadata is for.
   511 	 * @param bool              $single    Whether to return only the first value of the specified $meta_key.
   559 	 * @param string $meta_key  Metadata key.
       
   560 	 * @param bool   $single    Whether to return only the first value of the specified `$meta_key`.
       
   561 	 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
   562 	 *                          or any other object type with an associated meta table.
   512 	 */
   563 	 */
   513 	$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
   564 	$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single, $meta_type );
   514 	if ( null !== $check ) {
   565 	if ( null !== $check ) {
   515 		if ( $single && is_array( $check ) ) {
   566 		if ( $single && is_array( $check ) ) {
   516 			return $check[0];
   567 			return $check[0];
   517 		} else {
   568 		} else {
   518 			return $check;
   569 			return $check;
   521 
   572 
   522 	$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
   573 	$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
   523 
   574 
   524 	if ( ! $meta_cache ) {
   575 	if ( ! $meta_cache ) {
   525 		$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
   576 		$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
   526 		$meta_cache = $meta_cache[ $object_id ];
   577 		if ( isset( $meta_cache[ $object_id ] ) ) {
       
   578 			$meta_cache = $meta_cache[ $object_id ];
       
   579 		} else {
       
   580 			$meta_cache = null;
       
   581 		}
   527 	}
   582 	}
   528 
   583 
   529 	if ( ! $meta_key ) {
   584 	if ( ! $meta_key ) {
   530 		return $meta_cache;
   585 		return $meta_cache;
   531 	}
   586 	}
   536 		} else {
   591 		} else {
   537 			return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
   592 			return array_map( 'maybe_unserialize', $meta_cache[ $meta_key ] );
   538 		}
   593 		}
   539 	}
   594 	}
   540 
   595 
       
   596 	return null;
       
   597 }
       
   598 
       
   599 /**
       
   600  * Retrieves default metadata value for the specified meta key and object.
       
   601  *
       
   602  * By default, an empty string is returned if `$single` is true, or an empty array
       
   603  * if it's false.
       
   604  *
       
   605  * @since 5.5.0
       
   606  *
       
   607  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
   608  *                          or any other object type with an associated meta table.
       
   609  * @param int    $object_id ID of the object metadata is for.
       
   610  * @param string $meta_key  Metadata key.
       
   611  * @param bool   $single    Optional. If true, return only the first value of the specified meta_key.
       
   612  *                          This parameter has no effect if meta_key is not specified. Default false.
       
   613  * @return mixed Single metadata value, or array of values.
       
   614  */
       
   615 function get_metadata_default( $meta_type, $object_id, $meta_key, $single = false ) {
   541 	if ( $single ) {
   616 	if ( $single ) {
   542 		return '';
   617 		$value = '';
   543 	} else {
   618 	} else {
   544 		return array();
   619 		$value = array();
   545 	}
   620 	}
   546 }
   621 
   547 
   622 	/**
   548 /**
   623 	 * Filters the default metadata value for a specified meta key and object.
   549  * Determine if a meta key is set for a given object
   624 	 *
       
   625 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
       
   626 	 * (post, comment, term, user, or any other type with an associated meta table).
       
   627 	 *
       
   628 	 * Possible filter names include:
       
   629 	 *
       
   630 	 *  - `default_post_metadata`
       
   631 	 *  - `default_comment_metadata`
       
   632 	 *  - `default_term_metadata`
       
   633 	 *  - `default_user_metadata`
       
   634 	 *
       
   635 	 * @since 5.5.0
       
   636 	 *
       
   637 	 * @param mixed  $value     The value to return, either a single metadata value or an array
       
   638 	 *                          of values depending on the value of `$single`.
       
   639 	 * @param int    $object_id ID of the object metadata is for.
       
   640 	 * @param string $meta_key  Metadata key.
       
   641 	 * @param bool   $single    Whether to return only the first value of the specified `$meta_key`.
       
   642 	 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
   643 	 *                          or any other object type with an associated meta table.
       
   644 	 */
       
   645 	$value = apply_filters( "default_{$meta_type}_metadata", $value, $object_id, $meta_key, $single, $meta_type );
       
   646 
       
   647 	if ( ! $single && ! wp_is_numeric_array( $value ) ) {
       
   648 		$value = array( $value );
       
   649 	}
       
   650 
       
   651 	return $value;
       
   652 }
       
   653 
       
   654 /**
       
   655  * Determines if a meta field with the given key exists for the given object ID.
   550  *
   656  *
   551  * @since 3.3.0
   657  * @since 3.3.0
   552  *
   658  *
   553  * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
   659  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   554  * @param int    $object_id ID of the object metadata is for
   660  *                          or any other object type with an associated meta table.
       
   661  * @param int    $object_id ID of the object metadata is for.
   555  * @param string $meta_key  Metadata key.
   662  * @param string $meta_key  Metadata key.
   556  * @return bool True of the key is set, false if not.
   663  * @return bool Whether a meta field with the given key exists.
   557  */
   664  */
   558 function metadata_exists( $meta_type, $object_id, $meta_key ) {
   665 function metadata_exists( $meta_type, $object_id, $meta_key ) {
   559 	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
   666 	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
   560 		return false;
   667 		return false;
   561 	}
   668 	}
   584 
   691 
   585 	return false;
   692 	return false;
   586 }
   693 }
   587 
   694 
   588 /**
   695 /**
   589  * Get meta data by meta ID
   696  * Retrieves metadata by meta ID.
   590  *
   697  *
   591  * @since 3.3.0
   698  * @since 3.3.0
   592  *
   699  *
   593  * @global wpdb $wpdb WordPress database abstraction object.
   700  * @global wpdb $wpdb WordPress database abstraction object.
   594  *
   701  *
   595  * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
   702  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   596  * @param int    $meta_id   ID for a specific meta row
   703  *                          or any other object type with an associated meta table.
   597  * @return object|false Meta object or false.
   704  * @param int    $meta_id   ID for a specific meta row.
       
   705  * @return stdClass|false {
       
   706  *     Metadata object, or boolean `false` if the metadata doesn't exist.
       
   707  *
       
   708  *     @type string $meta_key   The meta key.
       
   709  *     @type mixed  $meta_value The unserialized meta value.
       
   710  *     @type string $meta_id    Optional. The meta ID when the meta type is any value except 'user'.
       
   711  *     @type string $umeta_id   Optional. The meta ID when the meta type is 'user'.
       
   712  *     @type string $post_id    Optional. The object ID when the meta type is 'post'.
       
   713  *     @type string $comment_id Optional. The object ID when the meta type is 'comment'.
       
   714  *     @type string $term_id    Optional. The object ID when the meta type is 'term'.
       
   715  *     @type string $user_id    Optional. The object ID when the meta type is 'user'.
       
   716  * }
   598  */
   717  */
   599 function get_metadata_by_mid( $meta_type, $meta_id ) {
   718 function get_metadata_by_mid( $meta_type, $meta_id ) {
   600 	global $wpdb;
   719 	global $wpdb;
   601 
   720 
   602 	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
   721 	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
   611 	$table = _get_meta_table( $meta_type );
   730 	$table = _get_meta_table( $meta_type );
   612 	if ( ! $table ) {
   731 	if ( ! $table ) {
   613 		return false;
   732 		return false;
   614 	}
   733 	}
   615 
   734 
   616 	$id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id';
       
   617 
       
   618 	/**
   735 	/**
   619 	 * Filters whether to retrieve metadata of a specific type by meta ID.
   736 	 * Short-circuits the return value when fetching a meta field by meta ID.
   620 	 *
   737 	 *
   621 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   738 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   622 	 * object type (comment, post, term, or user). Returning a non-null value
   739 	 * (post, comment, term, user, or any other type with an associated meta table).
   623 	 * will effectively short-circuit the function.
   740 	 * Returning a non-null value will effectively short-circuit the function.
   624 	 *
   741 	 *
   625 	 * @since 5.0.0
   742 	 * @since 5.0.0
   626 	 *
   743 	 *
   627 	 * @param mixed $value    The value get_metadata_by_mid() should return.
   744 	 * @param stdClass|null $value   The value to return.
   628 	 * @param int   $meta_id  Meta ID.
   745 	 * @param int           $meta_id Meta ID.
   629 	 */
   746 	 */
   630 	$check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id );
   747 	$check = apply_filters( "get_{$meta_type}_metadata_by_mid", null, $meta_id );
   631 	if ( null !== $check ) {
   748 	if ( null !== $check ) {
   632 		return $check;
   749 		return $check;
   633 	}
   750 	}
   634 
   751 
       
   752 	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
       
   753 
   635 	$meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
   754 	$meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) );
   636 
   755 
   637 	if ( empty( $meta ) ) {
   756 	if ( empty( $meta ) ) {
   638 		return false;
   757 		return false;
   639 	}
   758 	}
   644 
   763 
   645 	return $meta;
   764 	return $meta;
   646 }
   765 }
   647 
   766 
   648 /**
   767 /**
   649  * Update meta data by meta ID
   768  * Updates metadata by meta ID.
   650  *
   769  *
   651  * @since 3.3.0
   770  * @since 3.3.0
   652  *
   771  *
   653  * @global wpdb $wpdb WordPress database abstraction object.
   772  * @global wpdb $wpdb WordPress database abstraction object.
   654  *
   773  *
   655  * @param string $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
   774  * @param string $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   656  * @param int    $meta_id    ID for a specific meta row
   775  *                           or any other object type with an associated meta table.
   657  * @param string $meta_value Metadata value
   776  * @param int    $meta_id    ID for a specific meta row.
   658  * @param string $meta_key   Optional, you can provide a meta key to update it
   777  * @param string $meta_value Metadata value. Must be serializable if non-scalar.
       
   778  * @param string $meta_key   Optional. You can provide a meta key to update it. Default false.
   659  * @return bool True on successful update, false on failure.
   779  * @return bool True on successful update, false on failure.
   660  */
   780  */
   661 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
   781 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
   662 	global $wpdb;
   782 	global $wpdb;
   663 
   783 
   675 	if ( ! $table ) {
   795 	if ( ! $table ) {
   676 		return false;
   796 		return false;
   677 	}
   797 	}
   678 
   798 
   679 	$column    = sanitize_key( $meta_type . '_id' );
   799 	$column    = sanitize_key( $meta_type . '_id' );
   680 	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
   800 	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
   681 
   801 
   682 	/**
   802 	/**
   683 	 * Filters whether to update metadata of a specific type by meta ID.
   803 	 * Short-circuits updating metadata of a specific type by meta ID.
   684 	 *
   804 	 *
   685 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   805 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   686 	 * object type (comment, post, term, or user). Returning a non-null value
   806 	 * (post, comment, term, user, or any other type with an associated meta table).
   687 	 * will effectively short-circuit the function.
   807 	 * Returning a non-null value will effectively short-circuit the function.
   688 	 *
   808 	 *
   689 	 * @since 5.0.0
   809 	 * @since 5.0.0
   690 	 *
   810 	 *
   691 	 * @param null|bool   $check      Whether to allow updating metadata for the given type.
   811 	 * @param null|bool   $check      Whether to allow updating metadata for the given type.
   692 	 * @param int         $meta_id    Meta ID.
   812 	 * @param int         $meta_id    Meta ID.
   697 	if ( null !== $check ) {
   817 	if ( null !== $check ) {
   698 		return (bool) $check;
   818 		return (bool) $check;
   699 	}
   819 	}
   700 
   820 
   701 	// Fetch the meta and go on if it's found.
   821 	// Fetch the meta and go on if it's found.
   702 	if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
   822 	$meta = get_metadata_by_mid( $meta_type, $meta_id );
       
   823 	if ( $meta ) {
   703 		$original_key = $meta->meta_key;
   824 		$original_key = $meta->meta_key;
   704 		$object_id    = $meta->{$column};
   825 		$object_id    = $meta->{$column};
   705 
   826 
   706 		// If a new meta_key (last parameter) was specified, change the meta key,
   827 		// If a new meta_key (last parameter) was specified, change the meta key,
   707 		// otherwise use the original key in the update statement.
   828 		// otherwise use the original key in the update statement.
   711 			return false;
   832 			return false;
   712 		}
   833 		}
   713 
   834 
   714 		$meta_subtype = get_object_subtype( $meta_type, $object_id );
   835 		$meta_subtype = get_object_subtype( $meta_type, $object_id );
   715 
   836 
   716 		// Sanitize the meta
   837 		// Sanitize the meta.
   717 		$_meta_value = $meta_value;
   838 		$_meta_value = $meta_value;
   718 		$meta_value  = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
   839 		$meta_value  = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype );
   719 		$meta_value  = maybe_serialize( $meta_value );
   840 		$meta_value  = maybe_serialize( $meta_value );
   720 
   841 
   721 		// Format the data query arguments.
   842 		// Format the data query arguments.
   729 		$where[ $id_column ] = $meta_id;
   850 		$where[ $id_column ] = $meta_id;
   730 
   851 
   731 		/** This action is documented in wp-includes/meta.php */
   852 		/** This action is documented in wp-includes/meta.php */
   732 		do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
   853 		do_action( "update_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
   733 
   854 
   734 		if ( 'post' == $meta_type ) {
   855 		if ( 'post' === $meta_type ) {
   735 			/** This action is documented in wp-includes/meta.php */
   856 			/** This action is documented in wp-includes/meta.php */
   736 			do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
   857 			do_action( 'update_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
   737 		}
   858 		}
   738 
   859 
   739 		// Run the update query, all fields in $data are %s, $where is a %d.
   860 		// Run the update query, all fields in $data are %s, $where is a %d.
   746 		wp_cache_delete( $object_id, $meta_type . '_meta' );
   867 		wp_cache_delete( $object_id, $meta_type . '_meta' );
   747 
   868 
   748 		/** This action is documented in wp-includes/meta.php */
   869 		/** This action is documented in wp-includes/meta.php */
   749 		do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
   870 		do_action( "updated_{$meta_type}_meta", $meta_id, $object_id, $meta_key, $_meta_value );
   750 
   871 
   751 		if ( 'post' == $meta_type ) {
   872 		if ( 'post' === $meta_type ) {
   752 			/** This action is documented in wp-includes/meta.php */
   873 			/** This action is documented in wp-includes/meta.php */
   753 			do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
   874 			do_action( 'updated_postmeta', $meta_id, $object_id, $meta_key, $meta_value );
   754 		}
   875 		}
   755 
   876 
   756 		return true;
   877 		return true;
   759 	// And if the meta was not found.
   880 	// And if the meta was not found.
   760 	return false;
   881 	return false;
   761 }
   882 }
   762 
   883 
   763 /**
   884 /**
   764  * Delete meta data by meta ID
   885  * Deletes metadata by meta ID.
   765  *
   886  *
   766  * @since 3.3.0
   887  * @since 3.3.0
   767  *
   888  *
   768  * @global wpdb $wpdb WordPress database abstraction object.
   889  * @global wpdb $wpdb WordPress database abstraction object.
   769  *
   890  *
   770  * @param string $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
   891  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   771  * @param int    $meta_id   ID for a specific meta row
   892  *                          or any other object type with an associated meta table.
       
   893  * @param int    $meta_id   ID for a specific meta row.
   772  * @return bool True on successful delete, false on failure.
   894  * @return bool True on successful delete, false on failure.
   773  */
   895  */
   774 function delete_metadata_by_mid( $meta_type, $meta_id ) {
   896 function delete_metadata_by_mid( $meta_type, $meta_id ) {
   775 	global $wpdb;
   897 	global $wpdb;
   776 
   898 
   787 	$table = _get_meta_table( $meta_type );
   909 	$table = _get_meta_table( $meta_type );
   788 	if ( ! $table ) {
   910 	if ( ! $table ) {
   789 		return false;
   911 		return false;
   790 	}
   912 	}
   791 
   913 
   792 	// object and id columns
   914 	// Object and ID columns.
   793 	$column    = sanitize_key( $meta_type . '_id' );
   915 	$column    = sanitize_key( $meta_type . '_id' );
   794 	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
   916 	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
   795 
   917 
   796 	/**
   918 	/**
   797 	 * Filters whether to delete metadata of a specific type by meta ID.
   919 	 * Short-circuits deleting metadata of a specific type by meta ID.
   798 	 *
   920 	 *
   799 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   921 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   800 	 * object type (comment, post, term, or user). Returning a non-null value
   922 	 * (post, comment, term, user, or any other type with an associated meta table).
   801 	 * will effectively short-circuit the function.
   923 	 * Returning a non-null value will effectively short-circuit the function.
   802 	 *
   924 	 *
   803 	 * @since 5.0.0
   925 	 * @since 5.0.0
   804 	 *
   926 	 *
   805 	 * @param null|bool $delete  Whether to allow metadata deletion of the given type.
   927 	 * @param null|bool $delete  Whether to allow metadata deletion of the given type.
   806 	 * @param int       $meta_id Meta ID.
   928 	 * @param int       $meta_id Meta ID.
   809 	if ( null !== $check ) {
   931 	if ( null !== $check ) {
   810 		return (bool) $check;
   932 		return (bool) $check;
   811 	}
   933 	}
   812 
   934 
   813 	// Fetch the meta and go on if it's found.
   935 	// Fetch the meta and go on if it's found.
   814 	if ( $meta = get_metadata_by_mid( $meta_type, $meta_id ) ) {
   936 	$meta = get_metadata_by_mid( $meta_type, $meta_id );
       
   937 	if ( $meta ) {
   815 		$object_id = (int) $meta->{$column};
   938 		$object_id = (int) $meta->{$column};
   816 
   939 
   817 		/** This action is documented in wp-includes/meta.php */
   940 		/** 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 );
   941 		do_action( "delete_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
   819 
   942 
   820 		// Old-style action.
   943 		// Old-style action.
   821 		if ( 'post' == $meta_type || 'comment' == $meta_type ) {
   944 		if ( 'post' === $meta_type || 'comment' === $meta_type ) {
   822 			/**
   945 			/**
   823 			 * Fires immediately before deleting post or comment metadata of a specific type.
   946 			 * Fires immediately before deleting post or comment metadata of a specific type.
   824 			 *
   947 			 *
   825 			 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   948 			 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   826 			 * object type (post or comment).
   949 			 * object type (post or comment).
   830 			 * @param int $meta_id ID of the metadata entry to delete.
   953 			 * @param int $meta_id ID of the metadata entry to delete.
   831 			 */
   954 			 */
   832 			do_action( "delete_{$meta_type}meta", $meta_id );
   955 			do_action( "delete_{$meta_type}meta", $meta_id );
   833 		}
   956 		}
   834 
   957 
   835 		// Run the query, will return true if deleted, false otherwise
   958 		// Run the query, will return true if deleted, false otherwise.
   836 		$result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
   959 		$result = (bool) $wpdb->delete( $table, array( $id_column => $meta_id ) );
   837 
   960 
   838 		// Clear the caches.
   961 		// Clear the caches.
   839 		wp_cache_delete( $object_id, $meta_type . '_meta' );
   962 		wp_cache_delete( $object_id, $meta_type . '_meta' );
   840 
   963 
   841 		/** This action is documented in wp-includes/meta.php */
   964 		/** 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 );
   965 		do_action( "deleted_{$meta_type}_meta", (array) $meta_id, $object_id, $meta->meta_key, $meta->meta_value );
   843 
   966 
   844 		// Old-style action.
   967 		// Old-style action.
   845 		if ( 'post' == $meta_type || 'comment' == $meta_type ) {
   968 		if ( 'post' === $meta_type || 'comment' === $meta_type ) {
   846 			/**
   969 			/**
   847 			 * Fires immediately after deleting post or comment metadata of a specific type.
   970 			 * Fires immediately after deleting post or comment metadata of a specific type.
   848 			 *
   971 			 *
   849 			 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   972 			 * The dynamic portion of the hook, `$meta_type`, refers to the meta
   850 			 * object type (post or comment).
   973 			 * object type (post or comment).
   858 
   981 
   859 		return $result;
   982 		return $result;
   860 
   983 
   861 	}
   984 	}
   862 
   985 
   863 	// Meta id was not found.
   986 	// Meta ID was not found.
   864 	return false;
   987 	return false;
   865 }
   988 }
   866 
   989 
   867 /**
   990 /**
   868  * Update the metadata cache for the specified objects.
   991  * Updates the metadata cache for the specified objects.
   869  *
   992  *
   870  * @since 2.9.0
   993  * @since 2.9.0
   871  *
   994  *
   872  * @global wpdb $wpdb WordPress database abstraction object.
   995  * @global wpdb $wpdb WordPress database abstraction object.
   873  *
   996  *
   874  * @param string    $meta_type  Type of object metadata is for (e.g., comment, post, term, or user).
   997  * @param string       $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   875  * @param int|array $object_ids Array or comma delimited list of object IDs to update cache for
   998  *                                 or any other object type with an associated meta table.
       
   999  * @param string|int[] $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.
  1000  * @return array|false Metadata cache for the specified objects, or false on failure.
   877  */
  1001  */
   878 function update_meta_cache( $meta_type, $object_ids ) {
  1002 function update_meta_cache( $meta_type, $object_ids ) {
   879 	global $wpdb;
  1003 	global $wpdb;
   880 
  1004 
   895 	}
  1019 	}
   896 
  1020 
   897 	$object_ids = array_map( 'intval', $object_ids );
  1021 	$object_ids = array_map( 'intval', $object_ids );
   898 
  1022 
   899 	/**
  1023 	/**
   900 	 * Filters whether to update the metadata cache of a specific type.
  1024 	 * Short-circuits updating the metadata cache of a specific type.
   901 	 *
  1025 	 *
   902 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta
  1026 	 * The dynamic portion of the hook, `$meta_type`, refers to the meta object type
   903 	 * object type (comment, post, term, or user). Returning a non-null value
  1027 	 * (post, comment, term, user, or any other type with an associated meta table).
   904 	 * will effectively short-circuit the function.
  1028 	 * Returning a non-null value will effectively short-circuit the function.
   905 	 *
  1029 	 *
   906 	 * @since 5.0.0
  1030 	 * @since 5.0.0
   907 	 *
  1031 	 *
   908 	 * @param mixed $check      Whether to allow updating the meta cache of the given type.
  1032 	 * @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.
  1033 	 * @param int[] $object_ids Array of object IDs to update the meta cache for.
   910 	 */
  1034 	 */
   911 	$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
  1035 	$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
   912 	if ( null !== $check ) {
  1036 	if ( null !== $check ) {
   913 		return (bool) $check;
  1037 		return (bool) $check;
   914 	}
  1038 	}
   915 
  1039 
   916 	$cache_key = $meta_type . '_meta';
  1040 	$cache_key      = $meta_type . '_meta';
   917 	$ids       = array();
  1041 	$non_cached_ids = array();
   918 	$cache     = array();
  1042 	$cache          = array();
   919 	foreach ( $object_ids as $id ) {
  1043 	$cache_values   = wp_cache_get_multiple( $object_ids, $cache_key );
   920 		$cached_object = wp_cache_get( $id, $cache_key );
  1044 
       
  1045 	foreach ( $cache_values as $id => $cached_object ) {
   921 		if ( false === $cached_object ) {
  1046 		if ( false === $cached_object ) {
   922 			$ids[] = $id;
  1047 			$non_cached_ids[] = $id;
   923 		} else {
  1048 		} else {
   924 			$cache[ $id ] = $cached_object;
  1049 			$cache[ $id ] = $cached_object;
   925 		}
  1050 		}
   926 	}
  1051 	}
   927 
  1052 
   928 	if ( empty( $ids ) ) {
  1053 	if ( empty( $non_cached_ids ) ) {
   929 		return $cache;
  1054 		return $cache;
   930 	}
  1055 	}
   931 
  1056 
   932 	// Get meta info
  1057 	// Get meta info.
   933 	$id_list   = join( ',', $ids );
  1058 	$id_list   = join( ',', $non_cached_ids );
   934 	$id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id';
  1059 	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
       
  1060 
   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 );
  1061 	$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 
  1062 
   937 	if ( ! empty( $meta_list ) ) {
  1063 	if ( ! empty( $meta_list ) ) {
   938 		foreach ( $meta_list as $metarow ) {
  1064 		foreach ( $meta_list as $metarow ) {
   939 			$mpid = intval( $metarow[ $column ] );
  1065 			$mpid = intval( $metarow[ $column ] );
   940 			$mkey = $metarow['meta_key'];
  1066 			$mkey = $metarow['meta_key'];
   941 			$mval = $metarow['meta_value'];
  1067 			$mval = $metarow['meta_value'];
   942 
  1068 
   943 			// Force subkeys to be array type:
  1069 			// Force subkeys to be array type.
   944 			if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {
  1070 			if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {
   945 				$cache[ $mpid ] = array();
  1071 				$cache[ $mpid ] = array();
   946 			}
  1072 			}
   947 			if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) {
  1073 			if ( ! isset( $cache[ $mpid ][ $mkey ] ) || ! is_array( $cache[ $mpid ][ $mkey ] ) ) {
   948 				$cache[ $mpid ][ $mkey ] = array();
  1074 				$cache[ $mpid ][ $mkey ] = array();
   949 			}
  1075 			}
   950 
  1076 
   951 			// Add a value to the current pid/key:
  1077 			// Add a value to the current pid/key.
   952 			$cache[ $mpid ][ $mkey ][] = $mval;
  1078 			$cache[ $mpid ][ $mkey ][] = $mval;
   953 		}
  1079 		}
   954 	}
  1080 	}
   955 
  1081 
   956 	foreach ( $ids as $id ) {
  1082 	foreach ( $non_cached_ids as $id ) {
   957 		if ( ! isset( $cache[ $id ] ) ) {
  1083 		if ( ! isset( $cache[ $id ] ) ) {
   958 			$cache[ $id ] = array();
  1084 			$cache[ $id ] = array();
   959 		}
  1085 		}
   960 		wp_cache_add( $id, $cache[ $id ], $cache_key );
  1086 		wp_cache_add( $id, $cache[ $id ], $cache_key );
   961 	}
  1087 	}
   966 /**
  1092 /**
   967  * Retrieves the queue for lazy-loading metadata.
  1093  * Retrieves the queue for lazy-loading metadata.
   968  *
  1094  *
   969  * @since 4.5.0
  1095  * @since 4.5.0
   970  *
  1096  *
   971  * @return WP_Metadata_Lazyloader $lazyloader Metadata lazyloader queue.
  1097  * @return WP_Metadata_Lazyloader Metadata lazyloader queue.
   972  */
  1098  */
   973 function wp_metadata_lazyloader() {
  1099 function wp_metadata_lazyloader() {
   974 	static $wp_metadata_lazyloader;
  1100 	static $wp_metadata_lazyloader;
   975 
  1101 
   976 	if ( null === $wp_metadata_lazyloader ) {
  1102 	if ( null === $wp_metadata_lazyloader ) {
   985  *
  1111  *
   986  * @since 3.2.0
  1112  * @since 3.2.0
   987  *
  1113  *
   988  * @see WP_Meta_Query
  1114  * @see WP_Meta_Query
   989  *
  1115  *
   990  * @param array $meta_query         A meta query.
  1116  * @param array  $meta_query        A meta query.
   991  * @param string $type              Type of meta.
  1117  * @param string $type              Type of meta.
   992  * @param string $primary_table     Primary database table name.
  1118  * @param string $primary_table     Primary database table name.
   993  * @param string $primary_id_column Primary ID column name.
  1119  * @param string $primary_id_column Primary ID column name.
   994  * @param object $context           Optional. The main query object
  1120  * @param object $context           Optional. The main query object
   995  * @return array Associative array of `JOIN` and `WHERE` SQL.
  1121  * @return array Associative array of `JOIN` and `WHERE` SQL.
   998 	$meta_query_obj = new WP_Meta_Query( $meta_query );
  1124 	$meta_query_obj = new WP_Meta_Query( $meta_query );
   999 	return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
  1125 	return $meta_query_obj->get_sql( $type, $primary_table, $primary_id_column, $context );
  1000 }
  1126 }
  1001 
  1127 
  1002 /**
  1128 /**
  1003  * Retrieve the name of the metadata table for the specified object type.
  1129  * Retrieves the name of the metadata table for the specified object type.
  1004  *
  1130  *
  1005  * @since 2.9.0
  1131  * @since 2.9.0
  1006  *
  1132  *
  1007  * @global wpdb $wpdb WordPress database abstraction object.
  1133  * @global wpdb $wpdb WordPress database abstraction object.
  1008  *
  1134  *
  1009  * @param string $type Type of object to get metadata table for (e.g., comment, post, term, or user).
  1135  * @param string $type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1136  *                     or any other object type with an associated meta table.
  1010  * @return string|false Metadata table name, or false if no metadata table exists
  1137  * @return string|false Metadata table name, or false if no metadata table exists
  1011  */
  1138  */
  1012 function _get_meta_table( $type ) {
  1139 function _get_meta_table( $type ) {
  1013 	global $wpdb;
  1140 	global $wpdb;
  1014 
  1141 
  1024 /**
  1151 /**
  1025  * Determines whether a meta key is considered protected.
  1152  * Determines whether a meta key is considered protected.
  1026  *
  1153  *
  1027  * @since 3.1.3
  1154  * @since 3.1.3
  1028  *
  1155  *
  1029  * @param string      $meta_key  Meta key.
  1156  * @param string $meta_key  Metadata key.
  1030  * @param string|null $meta_type Optional. Type of object metadata is for (e.g., comment, post, term, or user).
  1157  * @param string $meta_type Optional. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1158  *                          or any other object type with an associated meta table. Default empty.
  1031  * @return bool Whether the meta key is considered protected.
  1159  * @return bool Whether the meta key is considered protected.
  1032  */
  1160  */
  1033 function is_protected_meta( $meta_key, $meta_type = null ) {
  1161 function is_protected_meta( $meta_key, $meta_type = '' ) {
  1034 	$protected = ( '_' == $meta_key[0] );
  1162 	$protected = ( '_' === $meta_key[0] );
  1035 
  1163 
  1036 	/**
  1164 	/**
  1037 	 * Filters whether a meta key is considered protected.
  1165 	 * Filters whether a meta key is considered protected.
  1038 	 *
  1166 	 *
  1039 	 * @since 3.2.0
  1167 	 * @since 3.2.0
  1040 	 *
  1168 	 *
  1041 	 * @param bool        $protected Whether the key is considered protected.
  1169 	 * @param bool   $protected Whether the key is considered protected.
  1042 	 * @param string      $meta_key  Meta key.
  1170 	 * @param string $meta_key  Metadata key.
  1043 	 * @param string|null $meta_type Type of object metadata is for (e.g., comment, post, term, or user).
  1171 	 * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1172 	 *                          or any other object type with an associated meta table.
  1044 	 */
  1173 	 */
  1045 	return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
  1174 	return apply_filters( 'is_protected_meta', $protected, $meta_key, $meta_type );
  1046 }
  1175 }
  1047 
  1176 
  1048 /**
  1177 /**
  1049  * Sanitize meta value.
  1178  * Sanitizes meta value.
  1050  *
  1179  *
  1051  * @since 3.1.3
  1180  * @since 3.1.3
  1052  * @since 4.9.8 The `$object_subtype` parameter was added.
  1181  * @since 4.9.8 The `$object_subtype` parameter was added.
  1053  *
  1182  *
  1054  * @param string $meta_key       Meta key.
  1183  * @param string $meta_key       Metadata key.
  1055  * @param mixed  $meta_value     Meta value to sanitize.
  1184  * @param mixed  $meta_value     Metadata value to sanitize.
  1056  * @param string $object_type    Type of object the meta is registered to.
  1185  * @param string $object_type    Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1186  *                               or any other object type with an associated meta table.
  1057  * @param string $object_subtype Optional. The subtype of the object type.
  1187  * @param string $object_subtype Optional. The subtype of the object type.
  1058  *
       
  1059  * @return mixed Sanitized $meta_value.
  1188  * @return mixed Sanitized $meta_value.
  1060  */
  1189  */
  1061 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
  1190 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}" ) ) {
  1191 	if ( ! empty( $object_subtype ) && has_filter( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}" ) ) {
  1063 
  1192 
  1068 		 * and `$object_subtype`, refer to the metadata object type (comment, post, term, or user),
  1197 		 * and `$object_subtype`, refer to the metadata object type (comment, post, term, or user),
  1069 		 * the meta key value, and the object subtype respectively.
  1198 		 * the meta key value, and the object subtype respectively.
  1070 		 *
  1199 		 *
  1071 		 * @since 4.9.8
  1200 		 * @since 4.9.8
  1072 		 *
  1201 		 *
  1073 		 * @param mixed  $meta_value     Meta value to sanitize.
  1202 		 * @param mixed  $meta_value     Metadata value to sanitize.
  1074 		 * @param string $meta_key       Meta key.
  1203 		 * @param string $meta_key       Metadata key.
  1075 		 * @param string $object_type    Object type.
  1204 		 * @param string $object_type    Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1205 		 *                               or any other object type with an associated meta table.
  1076 		 * @param string $object_subtype Object subtype.
  1206 		 * @param string $object_subtype Object subtype.
  1077 		 */
  1207 		 */
  1078 		return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype );
  1208 		return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}_for_{$object_subtype}", $meta_value, $meta_key, $object_type, $object_subtype );
  1079 	}
  1209 	}
  1080 
  1210 
  1085 	 * refer to the metadata object type (comment, post, term, or user) and the meta
  1215 	 * refer to the metadata object type (comment, post, term, or user) and the meta
  1086 	 * key value, respectively.
  1216 	 * key value, respectively.
  1087 	 *
  1217 	 *
  1088 	 * @since 3.3.0
  1218 	 * @since 3.3.0
  1089 	 *
  1219 	 *
  1090 	 * @param mixed  $meta_value      Meta value to sanitize.
  1220 	 * @param mixed  $meta_value  Metadata value to sanitize.
  1091 	 * @param string $meta_key        Meta key.
  1221 	 * @param string $meta_key    Metadata key.
  1092 	 * @param string $object_type     Object type.
  1222 	 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1223 	 *                            or any other object type with an associated meta table.
  1093 	 */
  1224 	 */
  1094 	return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
  1225 	return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
  1095 }
  1226 }
  1096 
  1227 
  1097 /**
  1228 /**
  1107  * @since 3.3.0
  1238  * @since 3.3.0
  1108  * @since 4.6.0 {@link https://core.trac.wordpress.org/ticket/35658 Modified
  1239  * @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
  1240  *              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.
  1241  *              `$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.
  1242  * @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
  1112  *
  1243  * @since 5.3.0 Valid meta types expanded to include "array" and "object".
  1113  * @param string $object_type    Type of object this meta is registered to.
  1244  * @since 5.5.0 The `$default` argument was added to the arguments array.
  1114  * @param string $meta_key       Meta key to register.
  1245  *
  1115  * @param array  $args {
  1246  * @param string       $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1247  *                                  or any other object type with an associated meta table.
       
  1248  * @param string       $meta_key    Meta key to register.
       
  1249  * @param array        $args {
  1116  *     Data used to describe the meta key when registered.
  1250  *     Data used to describe the meta key when registered.
  1117  *
  1251  *
  1118  *     @type string $object_subtype    A subtype; e.g. if the object type is "post", the post type. If left empty,
  1252  *     @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.
  1253  *                                         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.
  1254  *     @type string     $type              The type of data associated with this meta key.
  1121  *                                     Valid values are 'string', 'boolean', 'integer', and 'number'.
  1255  *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
  1122  *     @type string $description       A description of the data attached to this meta key.
  1256  *     @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.
  1257  *     @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.
  1258  *     @type mixed      $default           The default value returned from get_metadata() if no value has been set yet.
  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.
  1259  *                                         When using a non-single meta key, the default value is for the first entry.
  1126  *     @type bool   $show_in_rest      Whether data associated with this meta key can be considered public.
  1260  *                                         In other words, when calling get_metadata() with `$single` set to `false`,
       
  1261  *                                         the default value given here will be wrapped in an array.
       
  1262  *     @type callable   $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
       
  1263  *     @type callable   $auth_callback     Optional. A function or method to call when performing edit_post_meta,
       
  1264  *                                         add_post_meta, and delete_post_meta capability checks.
       
  1265  *     @type bool|array $show_in_rest      Whether data associated with this meta key can be considered public and
       
  1266  *                                         should be accessible via the REST API. A custom post type must also declare
       
  1267  *                                         support for custom fields for registered meta to be accessible via REST.
       
  1268  *                                         When registering complex meta values this argument may optionally be an
       
  1269  *                                         array with 'schema' or 'prepare_callback' keys instead of a boolean.
  1127  * }
  1270  * }
  1128  * @param string|array $deprecated Deprecated. Use `$args` instead.
  1271  * @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.
  1272  * @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
  1273  *              Registering a meta key with distinct sanitize and auth callbacks will fire those callbacks,
  1132  *                       callbacks, but will not add to the global registry.
  1274  *              but will not add to the global registry.
  1133  */
  1275  */
  1134 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
  1276 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
  1135 	global $wp_meta_keys;
  1277 	global $wp_meta_keys;
  1136 
  1278 
  1137 	if ( ! is_array( $wp_meta_keys ) ) {
  1279 	if ( ! is_array( $wp_meta_keys ) ) {
  1140 
  1282 
  1141 	$defaults = array(
  1283 	$defaults = array(
  1142 		'object_subtype'    => '',
  1284 		'object_subtype'    => '',
  1143 		'type'              => 'string',
  1285 		'type'              => 'string',
  1144 		'description'       => '',
  1286 		'description'       => '',
       
  1287 		'default'           => '',
  1145 		'single'            => false,
  1288 		'single'            => false,
  1146 		'sanitize_callback' => null,
  1289 		'sanitize_callback' => null,
  1147 		'auth_callback'     => null,
  1290 		'auth_callback'     => null,
  1148 		'show_in_rest'      => false,
  1291 		'show_in_rest'      => false,
  1149 	);
  1292 	);
  1150 
  1293 
  1151 	// There used to be individual args for sanitize and auth callbacks
  1294 	// There used to be individual args for sanitize and auth callbacks.
  1152 	$has_old_sanitize_cb = false;
  1295 	$has_old_sanitize_cb = false;
  1153 	$has_old_auth_cb     = false;
  1296 	$has_old_auth_cb     = false;
  1154 
  1297 
  1155 	if ( is_callable( $args ) ) {
  1298 	if ( is_callable( $args ) ) {
  1156 		$args = array(
  1299 		$args = array(
  1172 	 *
  1315 	 *
  1173 	 * @since 4.6.0
  1316 	 * @since 4.6.0
  1174 	 *
  1317 	 *
  1175 	 * @param array  $args        Array of meta registration arguments.
  1318 	 * @param array  $args        Array of meta registration arguments.
  1176 	 * @param array  $defaults    Array of default arguments.
  1319 	 * @param array  $defaults    Array of default arguments.
  1177 	 * @param string $object_type Object type.
  1320 	 * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1321 	 *                            or any other object type with an associated meta table.
  1178 	 * @param string $meta_key    Meta key.
  1322 	 * @param string $meta_key    Meta key.
  1179 	 */
  1323 	 */
  1180 	$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
  1324 	$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
       
  1325 	unset( $defaults['default'] );
  1181 	$args = wp_parse_args( $args, $defaults );
  1326 	$args = wp_parse_args( $args, $defaults );
       
  1327 
       
  1328 	// Require an item schema when registering array meta.
       
  1329 	if ( false !== $args['show_in_rest'] && 'array' === $args['type'] ) {
       
  1330 		if ( ! is_array( $args['show_in_rest'] ) || ! isset( $args['show_in_rest']['schema']['items'] ) ) {
       
  1331 			_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' );
       
  1332 
       
  1333 			return false;
       
  1334 		}
       
  1335 	}
  1182 
  1336 
  1183 	$object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
  1337 	$object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : '';
  1184 
  1338 
  1185 	// If `auth_callback` is not provided, fall back to `is_protected_meta()`.
  1339 	// If `auth_callback` is not provided, fall back to `is_protected_meta()`.
  1186 	if ( empty( $args['auth_callback'] ) ) {
  1340 	if ( empty( $args['auth_callback'] ) ) {
  1206 		} else {
  1360 		} else {
  1207 			add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
  1361 			add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
  1208 		}
  1362 		}
  1209 	}
  1363 	}
  1210 
  1364 
       
  1365 	if ( array_key_exists( 'default', $args ) ) {
       
  1366 		$schema = $args;
       
  1367 		if ( is_array( $args['show_in_rest'] ) && isset( $args['show_in_rest']['schema'] ) ) {
       
  1368 			$schema = array_merge( $schema, $args['show_in_rest']['schema'] );
       
  1369 		}
       
  1370 
       
  1371 		$check = rest_validate_value_from_schema( $args['default'], $schema );
       
  1372 		if ( is_wp_error( $check ) ) {
       
  1373 			_doing_it_wrong( __FUNCTION__, __( 'When registering a default meta value the data must match the type provided.' ), '5.5.0' );
       
  1374 
       
  1375 			return false;
       
  1376 		}
       
  1377 
       
  1378 		if ( ! has_filter( "default_{$object_type}_metadata", 'filter_default_metadata' ) ) {
       
  1379 			add_filter( "default_{$object_type}_metadata", 'filter_default_metadata', 10, 5 );
       
  1380 		}
       
  1381 	}
       
  1382 
  1211 	// Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
  1383 	// 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 ) {
  1384 	if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
  1213 		unset( $args['object_subtype'] );
  1385 		unset( $args['object_subtype'] );
  1214 
  1386 
  1215 		$wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
  1387 		$wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
  1219 
  1391 
  1220 	return false;
  1392 	return false;
  1221 }
  1393 }
  1222 
  1394 
  1223 /**
  1395 /**
       
  1396  * Filters into default_{$object_type}_metadata and adds in default value.
       
  1397  *
       
  1398  * @since 5.5.0
       
  1399  *
       
  1400  * @param mixed  $value     Current value passed to filter.
       
  1401  * @param int    $object_id ID of the object metadata is for.
       
  1402  * @param string $meta_key  Metadata key.
       
  1403  * @param bool   $single    If true, return only the first value of the specified meta_key.
       
  1404  *                          This parameter has no effect if meta_key is not specified.
       
  1405  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1406  *                          or any other object type with an associated meta table.
       
  1407  * @return mixed Single metadata default, or array of defaults.
       
  1408  */
       
  1409 function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
       
  1410 	global $wp_meta_keys;
       
  1411 
       
  1412 	if ( wp_installing() ) {
       
  1413 		return $value;
       
  1414 	}
       
  1415 
       
  1416 	if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $meta_type ] ) ) {
       
  1417 		return $value;
       
  1418 	}
       
  1419 
       
  1420 	$defaults = array();
       
  1421 	foreach ( $wp_meta_keys[ $meta_type ] as $sub_type => $meta_data ) {
       
  1422 		foreach ( $meta_data as $_meta_key => $args ) {
       
  1423 			if ( $_meta_key === $meta_key && array_key_exists( 'default', $args ) ) {
       
  1424 				$defaults[ $sub_type ] = $args;
       
  1425 			}
       
  1426 		}
       
  1427 	}
       
  1428 
       
  1429 	if ( ! $defaults ) {
       
  1430 		return $value;
       
  1431 	}
       
  1432 
       
  1433 	// If this meta type does not have subtypes, then the default is keyed as an empty string.
       
  1434 	if ( isset( $defaults[''] ) ) {
       
  1435 		$metadata = $defaults[''];
       
  1436 	} else {
       
  1437 		$sub_type = get_object_subtype( $meta_type, $object_id );
       
  1438 		if ( ! isset( $defaults[ $sub_type ] ) ) {
       
  1439 			return $value;
       
  1440 		}
       
  1441 		$metadata = $defaults[ $sub_type ];
       
  1442 	}
       
  1443 
       
  1444 	if ( $single ) {
       
  1445 		$value = $metadata['default'];
       
  1446 	} else {
       
  1447 		$value = array( $metadata['default'] );
       
  1448 	}
       
  1449 
       
  1450 	return $value;
       
  1451 }
       
  1452 
       
  1453 /**
  1224  * Checks if a meta key is registered.
  1454  * Checks if a meta key is registered.
  1225  *
  1455  *
  1226  * @since 4.6.0
  1456  * @since 4.6.0
  1227  * @since 4.9.8 The `$object_subtype` parameter was added.
  1457  * @since 4.9.8 The `$object_subtype` parameter was added.
  1228  *
  1458  *
  1229  * @param string $object_type    The type of object.
  1459  * @param string $object_type    Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1230  * @param string $meta_key       The meta key.
  1460  *                               or any other object type with an associated meta table.
       
  1461  * @param string $meta_key       Metadata key.
  1231  * @param string $object_subtype Optional. The subtype of the object type.
  1462  * @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,
  1463  * @return bool True if the meta key is registered to the object type and, if provided,
  1234  *              the object subtype. False if not.
  1464  *              the object subtype. False if not.
  1235  */
  1465  */
  1236 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
  1466 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) {
  1237 	$meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
  1467 	$meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
  1243  * Unregisters a meta key from the list of registered keys.
  1473  * Unregisters a meta key from the list of registered keys.
  1244  *
  1474  *
  1245  * @since 4.6.0
  1475  * @since 4.6.0
  1246  * @since 4.9.8 The `$object_subtype` parameter was added.
  1476  * @since 4.9.8 The `$object_subtype` parameter was added.
  1247  *
  1477  *
  1248  * @param string $object_type    The type of object.
  1478  * @param string $object_type    Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1249  * @param string $meta_key       The meta key.
  1479  *                               or any other object type with an associated meta table.
       
  1480  * @param string $meta_key       Metadata key.
  1250  * @param string $object_subtype Optional. The subtype of the object type.
  1481  * @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.
  1482  * @return bool True if successful. False if the meta key was not registered.
  1252  */
  1483  */
  1253 function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
  1484 function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) {
  1254 	global $wp_meta_keys;
  1485 	global $wp_meta_keys;
  1275 		}
  1506 		}
  1276 	}
  1507 	}
  1277 
  1508 
  1278 	unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
  1509 	unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] );
  1279 
  1510 
  1280 	// Do some clean up
  1511 	// Do some clean up.
  1281 	if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
  1512 	if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
  1282 		unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
  1513 		unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );
  1283 	}
  1514 	}
  1284 	if ( empty( $wp_meta_keys[ $object_type ] ) ) {
  1515 	if ( empty( $wp_meta_keys[ $object_type ] ) ) {
  1285 		unset( $wp_meta_keys[ $object_type ] );
  1516 		unset( $wp_meta_keys[ $object_type ] );
  1292  * Retrieves a list of registered meta keys for an object type.
  1523  * Retrieves a list of registered meta keys for an object type.
  1293  *
  1524  *
  1294  * @since 4.6.0
  1525  * @since 4.6.0
  1295  * @since 4.9.8 The `$object_subtype` parameter was added.
  1526  * @since 4.9.8 The `$object_subtype` parameter was added.
  1296  *
  1527  *
  1297  * @param string $object_type    The type of object. Post, comment, user, term.
  1528  * @param string $object_type    Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1529  *                               or any other object type with an associated meta table.
  1298  * @param string $object_subtype Optional. The subtype of the object type.
  1530  * @param string $object_subtype Optional. The subtype of the object type.
  1299  * @return array List of registered meta keys.
  1531  * @return string[] List of registered meta keys.
  1300  */
  1532  */
  1301 function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
  1533 function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
  1302 	global $wp_meta_keys;
  1534 	global $wp_meta_keys;
  1303 
  1535 
  1304 	if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
  1536 	if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {
  1314  * The results include both meta that is registered specifically for the
  1546  * 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.
  1547  * object's subtype and meta that is registered for the entire object type.
  1316  *
  1548  *
  1317  * @since 4.6.0
  1549  * @since 4.6.0
  1318  *
  1550  *
  1319  * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
  1551  * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1552  *                            or any other object type with an associated meta table.
  1320  * @param int    $object_id   ID of the object the metadata is for.
  1553  * @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
  1554  * @param string $meta_key    Optional. Registered metadata key. If not specified, retrieve all registered
  1322  *                            metadata for the specified object.
  1555  *                            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
  1556  * @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.
  1557  *               and values for an object ID if not. False if a given $meta_key is not registered.
  1355 
  1588 
  1356 	return array_intersect_key( $data, $meta_keys );
  1589 	return array_intersect_key( $data, $meta_keys );
  1357 }
  1590 }
  1358 
  1591 
  1359 /**
  1592 /**
  1360  * Filter out `register_meta()` args based on a whitelist.
  1593  * Filters out `register_meta()` args based on an allowed list.
  1361  * `register_meta()` args may change over time, so requiring the whitelist
  1594  *
       
  1595  * `register_meta()` args may change over time, so requiring the allowed list
  1362  * to be explicitly turned off is a warranty seal of sorts.
  1596  * to be explicitly turned off is a warranty seal of sorts.
  1363  *
  1597  *
  1364  * @access private
  1598  * @access private
  1365  * @since 4.6.0
  1599  * @since 5.5.0
  1366  *
  1600  *
  1367  * @param array $args         Arguments from `register_meta()`.
  1601  * @param array $args         Arguments from `register_meta()`.
  1368  * @param array $default_args Default arguments for `register_meta()`.
  1602  * @param array $default_args Default arguments for `register_meta()`.
  1369  *
       
  1370  * @return array Filtered arguments.
  1603  * @return array Filtered arguments.
  1371  */
  1604  */
  1372 function _wp_register_meta_args_whitelist( $args, $default_args ) {
  1605 function _wp_register_meta_args_allowed_list( $args, $default_args ) {
  1373 	return array_intersect_key( $args, $default_args );
  1606 	return array_intersect_key( $args, $default_args );
  1374 }
  1607 }
  1375 
  1608 
  1376 /**
  1609 /**
  1377  * Returns the object subtype for a given object ID of a specific type.
  1610  * Returns the object subtype for a given object ID of a specific type.
  1378  *
  1611  *
  1379  * @since 4.9.8
  1612  * @since 4.9.8
  1380  *
  1613  *
  1381  * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
  1614  * @param string $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
       
  1615  *                            or any other object type with an associated meta table.
  1382  * @param int    $object_id   ID of the object to retrieve its subtype.
  1616  * @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.
  1617  * @return string The object subtype or an empty string if unspecified subtype.
  1384  */
  1618  */
  1385 function get_object_subtype( $object_type, $object_id ) {
  1619 function get_object_subtype( $object_type, $object_id ) {
  1386 	$object_id      = (int) $object_id;
  1620 	$object_id      = (int) $object_id;
  1422 			$object_subtype = 'user';
  1656 			$object_subtype = 'user';
  1423 			break;
  1657 			break;
  1424 	}
  1658 	}
  1425 
  1659 
  1426 	/**
  1660 	/**
  1427 	 * Filters the object subtype identifier for a non standard object type.
  1661 	 * Filters the object subtype identifier for a non-standard object type.
  1428 	 *
  1662 	 *
  1429 	 * The dynamic portion of the hook, `$object_type`, refers to the object
  1663 	 * The dynamic portion of the hook, `$object_type`, refers to the meta object type
  1430 	 * type (post, comment, term, or user).
  1664 	 * (post, comment, term, user, or any other type with an associated meta table).
  1431 	 *
  1665 	 *
  1432 	 * @since 4.9.8
  1666 	 * @since 4.9.8
  1433 	 *
  1667 	 *
  1434 	 * @param string $object_subtype Empty string to override.
  1668 	 * @param string $object_subtype Empty string to override.
  1435 	 * @param int    $object_id      ID of the object to get the subtype for.
  1669 	 * @param int    $object_id      ID of the object to get the subtype for.