wp/wp-includes/meta.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    21  *
    21  *
    22  * @param string $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
    22  * @param string $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
    23  *                           or any other object type with an associated meta table.
    23  *                           or any other object type with an associated meta table.
    24  * @param int    $object_id  ID of the object metadata is for.
    24  * @param int    $object_id  ID of the object metadata is for.
    25  * @param string $meta_key   Metadata key.
    25  * @param string $meta_key   Metadata key.
    26  * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
    26  * @param mixed  $meta_value Metadata value. Arrays and objects are stored as serialized data and
       
    27  *                           will be returned as the same type when retrieved. Other data types will
       
    28  *                           be stored as strings in the database:
       
    29  *                           - false is stored and retrieved as an empty string ('')
       
    30  *                           - true is stored and retrieved as '1'
       
    31  *                           - numbers (both integer and float) are stored and retrieved as strings
       
    32  *                           Must be serializable if non-scalar.
    27  * @param bool   $unique     Optional. Whether the specified metadata key should be unique for the object.
    33  * @param bool   $unique     Optional. Whether the specified metadata key should be unique for the object.
    28  *                           If true, and the object already has a value for the specified metadata key,
    34  *                           If true, and the object already has a value for the specified metadata key,
    29  *                           no change will be made. Default false.
    35  *                           no change will be made. Default false.
    30  * @return int|false The meta ID on success, false on failure.
    36  * @return int|false The meta ID on success, false on failure.
    31  */
    37  */
   566  *                          This parameter has no effect if `$meta_key` is not specified. Default false.
   572  *                          This parameter has no effect if `$meta_key` is not specified. Default false.
   567  * @return mixed An array of values if `$single` is false.
   573  * @return mixed An array of values if `$single` is false.
   568  *               The value of the meta field if `$single` is true.
   574  *               The value of the meta field if `$single` is true.
   569  *               False for an invalid `$object_id` (non-numeric, zero, or negative value),
   575  *               False for an invalid `$object_id` (non-numeric, zero, or negative value),
   570  *               or if `$meta_type` is not specified.
   576  *               or if `$meta_type` is not specified.
   571  *               An empty string if a valid but non-existing object ID is passed.
   577  *               An empty array if a valid but non-existing object ID is passed and `$single` is false.
       
   578  *               An empty string if a valid but non-existing object ID is passed and `$single` is true.
       
   579  *               Note: Non-serialized values are returned as strings:
       
   580  *               - false values are returned as empty strings ('')
       
   581  *               - true values are returned as '1'
       
   582  *               - numbers (both integer and float) are returned as strings
       
   583  *               Arrays and objects retain their original type.
   572  */
   584  */
   573 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
   585 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
   574 	$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
   586 	$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
   575 	if ( ! is_null( $value ) ) {
   587 	if ( ! is_null( $value ) ) {
   576 		return $value;
   588 		return $value;
  1151 	$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
  1163 	$check = apply_filters( "update_{$meta_type}_metadata_cache", null, $object_ids );
  1152 	if ( null !== $check ) {
  1164 	if ( null !== $check ) {
  1153 		return (bool) $check;
  1165 		return (bool) $check;
  1154 	}
  1166 	}
  1155 
  1167 
  1156 	$cache_key      = $meta_type . '_meta';
  1168 	$cache_group    = $meta_type . '_meta';
  1157 	$non_cached_ids = array();
  1169 	$non_cached_ids = array();
  1158 	$cache          = array();
  1170 	$cache          = array();
  1159 	$cache_values   = wp_cache_get_multiple( $object_ids, $cache_key );
  1171 	$cache_values   = wp_cache_get_multiple( $object_ids, $cache_group );
  1160 
  1172 
  1161 	foreach ( $cache_values as $id => $cached_object ) {
  1173 	foreach ( $cache_values as $id => $cached_object ) {
  1162 		if ( false === $cached_object ) {
  1174 		if ( false === $cached_object ) {
  1163 			$non_cached_ids[] = $id;
  1175 			$non_cached_ids[] = $id;
  1164 		} else {
  1176 		} else {
  1200 		if ( ! isset( $cache[ $id ] ) ) {
  1212 		if ( ! isset( $cache[ $id ] ) ) {
  1201 			$cache[ $id ] = array();
  1213 			$cache[ $id ] = array();
  1202 		}
  1214 		}
  1203 		$data[ $id ] = $cache[ $id ];
  1215 		$data[ $id ] = $cache[ $id ];
  1204 	}
  1216 	}
  1205 	wp_cache_add_multiple( $data, $cache_key );
  1217 	wp_cache_add_multiple( $data, $cache_group );
  1206 
  1218 
  1207 	return $cache;
  1219 	return $cache;
  1208 }
  1220 }
  1209 
  1221 
  1210 /**
  1222 /**
  1366  *              `$sanitize_callback` and `$auth_callback` have been folded into this array.
  1378  *              `$sanitize_callback` and `$auth_callback` have been folded into this array.
  1367  * @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
  1379  * @since 4.9.8 The `$object_subtype` argument was added to the arguments array.
  1368  * @since 5.3.0 Valid meta types expanded to include "array" and "object".
  1380  * @since 5.3.0 Valid meta types expanded to include "array" and "object".
  1369  * @since 5.5.0 The `$default` argument was added to the arguments array.
  1381  * @since 5.5.0 The `$default` argument was added to the arguments array.
  1370  * @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array.
  1382  * @since 6.4.0 The `$revisions_enabled` argument was added to the arguments array.
       
  1383  * @since 6.7.0 The `label` argument was added to the arguments array.
  1371  *
  1384  *
  1372  * @param string       $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1385  * @param string       $object_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1373  *                                  or any other object type with an associated meta table.
  1386  *                                  or any other object type with an associated meta table.
  1374  * @param string       $meta_key    Meta key to register.
  1387  * @param string       $meta_key    Meta key to register.
  1375  * @param array        $args {
  1388  * @param array        $args {
  1377  *
  1390  *
  1378  *     @type string     $object_subtype    A subtype; e.g. if the object type is "post", the post type. If left empty,
  1391  *     @type string     $object_subtype    A subtype; e.g. if the object type is "post", the post type. If left empty,
  1379  *                                         the meta key will be registered on the entire object type. Default empty.
  1392  *                                         the meta key will be registered on the entire object type. Default empty.
  1380  *     @type string     $type              The type of data associated with this meta key.
  1393  *     @type string     $type              The type of data associated with this meta key.
  1381  *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
  1394  *                                         Valid values are 'string', 'boolean', 'integer', 'number', 'array', and 'object'.
       
  1395  *     @type string     $label             A human-readable label of the data attached to this meta key.
  1382  *     @type string     $description       A description of the data attached to this meta key.
  1396  *     @type string     $description       A description of the data attached to this meta key.
  1383  *     @type bool       $single            Whether the meta key has one value per object, or an array of values per object.
  1397  *     @type bool       $single            Whether the meta key has one value per object, or an array of values per object.
  1384  *     @type mixed      $default           The default value returned from get_metadata() if no value has been set yet.
  1398  *     @type mixed      $default           The default value returned from get_metadata() if no value has been set yet.
  1385  *                                         When using a non-single meta key, the default value is for the first entry.
  1399  *                                         When using a non-single meta key, the default value is for the first entry.
  1386  *                                         In other words, when calling get_metadata() with `$single` set to `false`,
  1400  *                                         In other words, when calling get_metadata() with `$single` set to `false`,
  1409 	}
  1423 	}
  1410 
  1424 
  1411 	$defaults = array(
  1425 	$defaults = array(
  1412 		'object_subtype'    => '',
  1426 		'object_subtype'    => '',
  1413 		'type'              => 'string',
  1427 		'type'              => 'string',
       
  1428 		'label'             => '',
  1414 		'description'       => '',
  1429 		'description'       => '',
  1415 		'default'           => '',
  1430 		'default'           => '',
  1416 		'single'            => false,
  1431 		'single'            => false,
  1417 		'sanitize_callback' => null,
  1432 		'sanitize_callback' => null,
  1418 		'auth_callback'     => null,
  1433 		'auth_callback'     => null,