wp/wp-includes/meta.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
   495  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   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.
   496  *                          or any other object type with an associated meta table.
   497  * @param int    $object_id ID of the object metadata is for.
   497  * @param int    $object_id ID of the object metadata is for.
   498  * @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
   499  *                          the specified object. Default empty.
   499  *                          the specified object. Default empty.
   500  * @param bool   $single    Optional. If true, return only the first value of the specified meta_key.
   500  * @param bool   $single    Optional. 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.
   501  *                          This parameter has no effect if `$meta_key` is not specified. Default false.
   502  * @return mixed Single metadata value, or array of values.
   502  * @return mixed An array of values if `$single` is false.
   503  *               False if there's a problem with the parameters passed to the function.
   503  *               The value of the meta field if `$single` is true.
       
   504  *               False for an invalid `$object_id` (non-numeric, zero, or negative value),
       
   505  *               or if `$meta_type` is not specified.
       
   506  *               An empty string if a valid but non-existing object ID is passed.
   504  */
   507  */
   505 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
   508 function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false ) {
   506 	$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
   509 	$value = get_metadata_raw( $meta_type, $object_id, $meta_key, $single );
   507 	if ( ! is_null( $value ) ) {
   510 	if ( ! is_null( $value ) ) {
   508 		return $value;
   511 		return $value;
   519  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   522  * @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.
   523  *                          or any other object type with an associated meta table.
   521  * @param int    $object_id ID of the object metadata is for.
   524  * @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
   525  * @param string $meta_key  Optional. Metadata key. If not specified, retrieve all metadata for
   523  *                          the specified object. Default empty.
   526  *                          the specified object. Default empty.
   524  * @param bool   $single    Optional. If true, return only the first value of the specified meta_key.
   527  * @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.
   528  *                          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.
   529  * @return mixed An array of values if `$single` is false.
   527  *               False if there's a problem with the parameters passed to the function.
   530  *               The value of the meta field if `$single` is true.
       
   531  *               False for an invalid `$object_id` (non-numeric, zero, or negative value),
       
   532  *               or if `$meta_type` is not specified.
       
   533  *               Null if the value does not exist.
   528  */
   534  */
   529 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
   535 function get_metadata_raw( $meta_type, $object_id, $meta_key = '', $single = false ) {
   530 	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
   536 	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
   531 		return false;
   537 		return false;
   532 	}
   538 	}
   606  *
   612  *
   607  * @param string $meta_type Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   613  * @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.
   614  *                          or any other object type with an associated meta table.
   609  * @param int    $object_id ID of the object metadata is for.
   615  * @param int    $object_id ID of the object metadata is for.
   610  * @param string $meta_key  Metadata key.
   616  * @param string $meta_key  Metadata key.
   611  * @param bool   $single    Optional. If true, return only the first value of the specified meta_key.
   617  * @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.
   618  *                          This parameter has no effect if `$meta_key` is not specified. Default false.
   613  * @return mixed Single metadata value, or array of values.
   619  * @return mixed An array of default values if `$single` is false.
       
   620  *               The default value of the meta field if `$single` is true.
   614  */
   621  */
   615 function get_metadata_default( $meta_type, $object_id, $meta_key, $single = false ) {
   622 function get_metadata_default( $meta_type, $object_id, $meta_key, $single = false ) {
   616 	if ( $single ) {
   623 	if ( $single ) {
   617 		$value = '';
   624 		$value = '';
   618 	} else {
   625 	} else {
   671 	if ( ! $object_id ) {
   678 	if ( ! $object_id ) {
   672 		return false;
   679 		return false;
   673 	}
   680 	}
   674 
   681 
   675 	/** This filter is documented in wp-includes/meta.php */
   682 	/** This filter is documented in wp-includes/meta.php */
   676 	$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
   683 	$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true, $meta_type );
   677 	if ( null !== $check ) {
   684 	if ( null !== $check ) {
   678 		return (bool) $check;
   685 		return (bool) $check;
   679 	}
   686 	}
   680 
   687 
   681 	$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
   688 	$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );
   720 
   727 
   721 	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
   728 	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
   722 		return false;
   729 		return false;
   723 	}
   730 	}
   724 
   731 
   725 	$meta_id = intval( $meta_id );
   732 	$meta_id = (int) $meta_id;
   726 	if ( $meta_id <= 0 ) {
   733 	if ( $meta_id <= 0 ) {
   727 		return false;
   734 		return false;
   728 	}
   735 	}
   729 
   736 
   730 	$table = _get_meta_table( $meta_type );
   737 	$table = _get_meta_table( $meta_type );
   769  *
   776  *
   770  * @since 3.3.0
   777  * @since 3.3.0
   771  *
   778  *
   772  * @global wpdb $wpdb WordPress database abstraction object.
   779  * @global wpdb $wpdb WordPress database abstraction object.
   773  *
   780  *
   774  * @param string $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   781  * @param string       $meta_type  Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
   775  *                           or any other object type with an associated meta table.
   782  *                                 or any other object type with an associated meta table.
   776  * @param int    $meta_id    ID for a specific meta row.
   783  * @param int          $meta_id    ID for a specific meta row.
   777  * @param string $meta_value Metadata value. Must be serializable if non-scalar.
   784  * @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.
   785  * @param string|false $meta_key   Optional. You can provide a meta key to update it. Default false.
   779  * @return bool True on successful update, false on failure.
   786  * @return bool True on successful update, false on failure.
   780  */
   787  */
   781 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
   788 function update_metadata_by_mid( $meta_type, $meta_id, $meta_value, $meta_key = false ) {
   782 	global $wpdb;
   789 	global $wpdb;
   783 
   790 
   784 	// Make sure everything is valid.
   791 	// Make sure everything is valid.
   785 	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
   792 	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
   786 		return false;
   793 		return false;
   787 	}
   794 	}
   788 
   795 
   789 	$meta_id = intval( $meta_id );
   796 	$meta_id = (int) $meta_id;
   790 	if ( $meta_id <= 0 ) {
   797 	if ( $meta_id <= 0 ) {
   791 		return false;
   798 		return false;
   792 	}
   799 	}
   793 
   800 
   794 	$table = _get_meta_table( $meta_type );
   801 	$table = _get_meta_table( $meta_type );
   806 	 * (post, comment, term, user, or any other type with an associated meta table).
   813 	 * (post, comment, term, user, or any other type with an associated meta table).
   807 	 * Returning a non-null value will effectively short-circuit the function.
   814 	 * Returning a non-null value will effectively short-circuit the function.
   808 	 *
   815 	 *
   809 	 * @since 5.0.0
   816 	 * @since 5.0.0
   810 	 *
   817 	 *
   811 	 * @param null|bool   $check      Whether to allow updating metadata for the given type.
   818 	 * @param null|bool    $check      Whether to allow updating metadata for the given type.
   812 	 * @param int         $meta_id    Meta ID.
   819 	 * @param int          $meta_id    Meta ID.
   813 	 * @param mixed       $meta_value Meta value. Must be serializable if non-scalar.
   820 	 * @param mixed        $meta_value Meta value. Must be serializable if non-scalar.
   814 	 * @param string|bool $meta_key   Meta key, if provided.
   821 	 * @param string|false $meta_key   Meta key, if provided.
   815 	 */
   822 	 */
   816 	$check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key );
   823 	$check = apply_filters( "update_{$meta_type}_metadata_by_mid", null, $meta_id, $meta_value, $meta_key );
   817 	if ( null !== $check ) {
   824 	if ( null !== $check ) {
   818 		return (bool) $check;
   825 		return (bool) $check;
   819 	}
   826 	}
   899 	// Make sure everything is valid.
   906 	// Make sure everything is valid.
   900 	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
   907 	if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) {
   901 		return false;
   908 		return false;
   902 	}
   909 	}
   903 
   910 
   904 	$meta_id = intval( $meta_id );
   911 	$meta_id = (int) $meta_id;
   905 	if ( $meta_id <= 0 ) {
   912 	if ( $meta_id <= 0 ) {
   906 		return false;
   913 		return false;
   907 	}
   914 	}
   908 
   915 
   909 	$table = _get_meta_table( $meta_type );
   916 	$table = _get_meta_table( $meta_type );
  1053 	if ( empty( $non_cached_ids ) ) {
  1060 	if ( empty( $non_cached_ids ) ) {
  1054 		return $cache;
  1061 		return $cache;
  1055 	}
  1062 	}
  1056 
  1063 
  1057 	// Get meta info.
  1064 	// Get meta info.
  1058 	$id_list   = join( ',', $non_cached_ids );
  1065 	$id_list   = implode( ',', $non_cached_ids );
  1059 	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
  1066 	$id_column = ( 'user' === $meta_type ) ? 'umeta_id' : 'meta_id';
  1060 
  1067 
  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 );
  1068 	$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 );
  1062 
  1069 
  1063 	if ( ! empty( $meta_list ) ) {
  1070 	if ( ! empty( $meta_list ) ) {
  1064 		foreach ( $meta_list as $metarow ) {
  1071 		foreach ( $meta_list as $metarow ) {
  1065 			$mpid = intval( $metarow[ $column ] );
  1072 			$mpid = (int) $metarow[ $column ];
  1066 			$mkey = $metarow['meta_key'];
  1073 			$mkey = $metarow['meta_key'];
  1067 			$mval = $metarow['meta_value'];
  1074 			$mval = $metarow['meta_value'];
  1068 
  1075 
  1069 			// Force subkeys to be array type.
  1076 			// Force subkeys to be array type.
  1070 			if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {
  1077 			if ( ! isset( $cache[ $mpid ] ) || ! is_array( $cache[ $mpid ] ) ) {
  1157  * @param string $meta_type Optional. Type of object metadata is for. Accepts 'post', 'comment', 'term', 'user',
  1164  * @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.
  1165  *                          or any other object type with an associated meta table. Default empty.
  1159  * @return bool Whether the meta key is considered protected.
  1166  * @return bool Whether the meta key is considered protected.
  1160  */
  1167  */
  1161 function is_protected_meta( $meta_key, $meta_type = '' ) {
  1168 function is_protected_meta( $meta_key, $meta_type = '' ) {
  1162 	$protected = ( '_' === $meta_key[0] );
  1169 	$sanitized_key = preg_replace( "/[^\x20-\x7E\p{L}]/", '', $meta_key );
       
  1170 	$protected     = strlen( $sanitized_key ) > 0 && ( '_' === $sanitized_key[0] );
  1163 
  1171 
  1164 	/**
  1172 	/**
  1165 	 * Filters whether a meta key is considered protected.
  1173 	 * Filters whether a meta key is considered protected.
  1166 	 *
  1174 	 *
  1167 	 * @since 3.2.0
  1175 	 * @since 3.2.0
  1398  * @since 5.5.0
  1406  * @since 5.5.0
  1399  *
  1407  *
  1400  * @param mixed  $value     Current value passed to filter.
  1408  * @param mixed  $value     Current value passed to filter.
  1401  * @param int    $object_id ID of the object metadata is for.
  1409  * @param int    $object_id ID of the object metadata is for.
  1402  * @param string $meta_key  Metadata key.
  1410  * @param string $meta_key  Metadata key.
  1403  * @param bool   $single    If true, return only the first value of the specified meta_key.
  1411  * @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.
  1412  *                          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',
  1413  * @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.
  1414  *                          or any other object type with an associated meta table.
  1407  * @return mixed Single metadata default, or array of defaults.
  1415  * @return mixed An array of default values if `$single` is false.
       
  1416  *               The default value of the meta field if `$single` is true.
  1408  */
  1417  */
  1409 function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
  1418 function filter_default_metadata( $value, $object_id, $meta_key, $single, $meta_type ) {
  1410 	global $wp_meta_keys;
  1419 	global $wp_meta_keys;
  1411 
  1420 
  1412 	if ( wp_installing() ) {
  1421 	if ( wp_installing() ) {