940
* @param string $meta_key Meta key
941
* @param mixed $meta_value Meta value to sanitize
942
* @param string $meta_type Type of meta
943
* @return mixed Sanitized $meta_value
940
* @param string $meta_key Meta key.
941
* @param mixed $meta_value Meta value to sanitize.
942
* @param string $object_type Type of object the meta is registered to.
944
* @return mixed Sanitized $meta_value.
945
function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
946
function sanitize_meta( $meta_key, $meta_value, $object_type ) {
948
* Filter the sanitization of a specific meta key of a specific meta type.
948
* Filters the sanitization of a specific meta key of a specific meta type.
950
950
* The dynamic portions of the hook name, `$meta_type`, and `$meta_key`,
951
951
* refer to the metadata object type (comment, post, or user) and the meta
952
* key value, respectively.
957
* @param mixed $meta_value Meta value to sanitize.
958
* @param string $meta_key Meta key.
959
* @param string $meta_type Meta type.
956
* @param mixed $meta_value Meta value to sanitize.
957
* @param string $meta_key Meta key.
958
* @param string $object_type Object type.
961
return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
960
return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
964
* Registers a meta key.
969
* @param string $meta_type Type of meta
970
* @param string $meta_key Meta key
971
* @param string|array $sanitize_callback A function or method to call when sanitizing the value of $meta_key.
972
* @param string|array $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks.
974
function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
975
if ( is_callable( $sanitize_callback ) )
976
add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
978
if ( empty( $auth_callback ) ) {
979
if ( is_protected_meta( $meta_key, $meta_type ) )
980
$auth_callback = '__return_false';
982
$auth_callback = '__return_true';
985
if ( is_callable( $auth_callback ) )
986
add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 );
967
* @since 4.6.0 {@link https://make.wordpress.org/core/2016/07/08/enhancing-register_meta-in-4-6/ Modified
968
* to support an array of data to attach to registered meta keys}. Previous arguments for
969
* `$sanitize_callback` and `$auth_callback` have been folded into this array.
971
* @param string $object_type Type of object this meta is registered to.
972
* @param string $meta_key Meta key to register.
973
* @param array $args {
974
* Data used to describe the meta key when registered.
976
* @type string $type The type of data associated with this meta key.
977
* @type string $description A description of the data attached to this meta key.
978
* @type bool $single Whether the meta key has one value per object, or an array of values per object.
979
* @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data.
980
* @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.
981
* @type bool $show_in_rest Whether data associated with this meta key can be considered public.
983
* @param string|array $deprecated Deprecated. Use `$args` instead.
985
* @return bool True if the meta key was successfully registered in the global array, false if not.
986
* Registering a meta key with distinct sanitize and auth callbacks will fire those
987
* callbacks, but will not add to the global registry.
989
function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
990
global $wp_meta_keys;
992
if ( ! is_array( $wp_meta_keys ) ) {
993
$wp_meta_keys = array();
1000
'sanitize_callback' => null,
1001
'auth_callback' => null,
1002
'show_in_rest' => false,
1005
// There used to be individual args for sanitize and auth callbacks
1006
$has_old_sanitize_cb = false;
1007
$has_old_auth_cb = false;
1009
if ( is_callable( $args ) ) {
1011
'sanitize_callback' => $args,
1014
$has_old_sanitize_cb = true;
1016
$args = (array) $args;
1019
if ( is_callable( $deprecated ) ) {
1020
$args['auth_callback'] = $deprecated;
1021
$has_old_auth_cb = true;
1025
* Filters the registration arguments when registering meta.
1029
* @param array $args Array of meta registration arguments.
1030
* @param array $defaults Array of default arguments.
1031
* @param string $object_type Object type.
1032
* @param string $meta_key Meta key.
1034
$args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key );
1035
$args = wp_parse_args( $args, $defaults );
1037
// If `auth_callback` is not provided, fall back to `is_protected_meta()`.
1038
if ( empty( $args['auth_callback'] ) ) {
1039
if ( is_protected_meta( $meta_key, $object_type ) ) {
1040
$args['auth_callback'] = '__return_false';
1042
$args['auth_callback'] = '__return_true';
1046
// Back-compat: old sanitize and auth callbacks are applied to all of an object type.
1047
if ( is_callable( $args['sanitize_callback'] ) ) {
1048
add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 );
1051
if ( is_callable( $args['auth_callback'] ) ) {
1052
add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
1055
// Global registry only contains meta keys registered with the array of arguments added in 4.6.0.
1056
if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) {
1057
$wp_meta_keys[ $object_type ][ $meta_key ] = $args;
1066
* Checks if a meta key is registered.
1070
* @param string $object_type The type of object.
1071
* @param string $meta_key The meta key.
1073
* @return bool True if the meta key is registered to the object type. False if not.
1075
function registered_meta_key_exists( $object_type, $meta_key ) {
1076
global $wp_meta_keys;
1078
if ( ! is_array( $wp_meta_keys ) ) {
1082
if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
1086
if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) {
1094
* Unregisters a meta key from the list of registered keys.
1098
* @param string $object_type The type of object.
1099
* @param string $meta_key The meta key.
1100
* @return bool True if successful. False if the meta key was not registered.
1102
function unregister_meta_key( $object_type, $meta_key ) {
1103
global $wp_meta_keys;
1105
if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
1109
$args = $wp_meta_keys[ $object_type ][ $meta_key ];
1111
if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) {
1112
remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] );
1115
if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) {
1116
remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );
1119
unset( $wp_meta_keys[ $object_type ][ $meta_key ] );
1122
if ( empty( $wp_meta_keys[ $object_type ] ) ) {
1123
unset( $wp_meta_keys[ $object_type ] );
1130
* Retrieves a list of registered meta keys for an object type.
1134
* @param string $object_type The type of object. Post, comment, user, term.
1135
* @return array List of registered meta keys.
1137
function get_registered_meta_keys( $object_type ) {
1138
global $wp_meta_keys;
1140
if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) ) {
1144
return $wp_meta_keys[ $object_type ];
1148
* Retrieves registered metadata for a specified object.
1152
* @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user)
1153
* @param int $object_id ID of the object the metadata is for.
1154
* @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered
1155
* metadata for the specified object.
1156
* @return mixed A single value or array of values for a key if specified. An array of all registered keys
1157
* and values for an object ID if not.
1159
function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
1160
if ( ! empty( $meta_key ) ) {
1161
if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {
1164
$meta_keys = get_registered_meta_keys( $object_type );
1165
$meta_key_data = $meta_keys[ $meta_key ];
1167
$data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] );
1172
$data = get_metadata( $object_type, $object_id );
1174
$meta_keys = get_registered_meta_keys( $object_type );
1175
$registered_data = array();
1177
// Someday, array_filter()
1178
foreach ( $meta_keys as $k => $v ) {
1179
if ( isset( $data[ $k ] ) ) {
1180
$registered_data[ $k ] = $data[ $k ];
1184
return $registered_data;
1188
* Filter out `register_meta()` args based on a whitelist.
1189
* `register_meta()` args may change over time, so requiring the whitelist
1190
* to be explicitly turned off is a warranty seal of sorts.
1195
* @param array $args Arguments from `register_meta()`.
1196
* @param array $default_args Default arguments for `register_meta()`.
1198
* @return array Filtered arguments.
1200
function _wp_register_meta_args_whitelist( $args, $default_args ) {
1201
$whitelist = array_keys( $default_args );
1203
// In an anonymous function world, this would be better as an array_filter()
1204
foreach ( $args as $key => $value ) {
1205
if ( ! in_array( $key, $whitelist ) ) {
1206
unset( $args[ $key ] );