| 961 | | return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type ); |
| | 968 | $meta_value = apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype ); |
| | 969 | |
| | 970 | /** |
| | 971 | * Filters the sanitization of a specific meta key of a specific meta type and subtype. |
| | 972 | * |
| | 973 | * The dynamic portions of the hook name, `$meta_type`, `$meta_subtype`, |
| | 974 | * and `$meta_key`, refer to the metadata object type (comment, post, or user) |
| | 975 | * the object subtype, and the meta key value, respectively. |
| | 976 | * |
| | 977 | * @since 4.6.0 |
| | 978 | * |
| | 979 | * @param mixed $meta_value Meta value to sanitize. |
| | 980 | * @param string $meta_key Meta key. |
| | 981 | * @param string $object_type Object type. |
| | 982 | * @param string $object_subtype Object subtype. |
| | 983 | */ |
| | 984 | return apply_filters( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype ); |
| 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. |
| | 994 | * @param string $object_type Type of object this meta is registered to. |
| | 995 | * @param string $meta_key Meta key to register. |
| | 996 | * @param array $args { |
| | 997 | * Data used to describe the meta key when registered. |
| | 998 | * |
| | 999 | * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. |
| | 1000 | * @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. |
| | 1001 | * @type string $object_subtype If the object type is "post", the post type. |
| | 1002 | * @type string $type The type of data associated with this meta key. |
| | 1003 | * @type string $description A description of the data attached to this meta key. |
| | 1004 | * @type bool $show_in_rest Whether data associated with this meta key can be considered public. |
| | 1005 | * } |
| 985 | | if ( is_callable( $auth_callback ) ) |
| 986 | | add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 ); |
| | 1014 | if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) { |
| | 1015 | _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid object type: %s.' ), $object_type ), '4.6.0' ); |
| | 1016 | } |
| | 1017 | |
| | 1018 | $defaults = array( |
| | 1019 | 'sanitize_callback' => null, |
| | 1020 | 'auth_callback' => null, |
| | 1021 | 'object_subtype' => '', |
| | 1022 | 'type' => 'string', |
| | 1023 | 'description' => '', |
| | 1024 | 'show_in_rest' => false, |
| | 1025 | ); |
| | 1026 | |
| | 1027 | $passed_args = array_slice( func_get_args(), 2 ); |
| | 1028 | |
| | 1029 | // There used to be individual args for sanitize and auth callbacks |
| | 1030 | $has_old_sanitize_cb = $has_old_auth_cb = false; |
| | 1031 | |
| | 1032 | if ( is_callable( $passed_args[0] ) ) { |
| | 1033 | $args->sanitize_callback = $passed_args[0]; |
| | 1034 | $has_old_sanitize_cb = true; |
| | 1035 | } else { |
| | 1036 | $args = $passed_args[0]; |
| | 1037 | } |
| | 1038 | |
| | 1039 | if ( isset( $passed_args[1] ) && is_callable( $passed_args[1] ) ) { |
| | 1040 | $args->auth_callback = $passed_args[1]; |
| | 1041 | $has_old_auth_cb = true; |
| | 1042 | } |
| | 1043 | |
| | 1044 | $args = wp_parse_args( $args, $defaults ); |
| | 1045 | |
| | 1046 | // PROOF OF CONCEPT ONLY |
| | 1047 | // This should be a function like like _wp_register_meta_whitelist() or some such |
| | 1048 | // And in default filters |
| | 1049 | // Also you could do the subtype enforcement in here too I guess |
| | 1050 | add_filter( 'register_meta_args', function( $args, $defaults ) { |
| | 1051 | $whitelist = array_keys( $defaults ); |
| | 1052 | |
| | 1053 | // In an anonymous function world, this would be way better as an array_filter |
| | 1054 | foreach ( $args as $key => $value ) { |
| | 1055 | if ( ! in_array( $key, $whitelist ) ) { |
| | 1056 | unset( $args[ $key ] ); |
| | 1057 | } |
| | 1058 | } |
| | 1059 | |
| | 1060 | return $args; |
| | 1061 | }, 10, 2 ) |
| | 1062 | |
| | 1063 | // Object subtypes are only supported for posts. |
| | 1064 | // @TODO HHS: Seems like this should be hooked on, not directly enforced |
| | 1065 | if ( 'post' === $object_type && isset( $args['object_subtype'] ) ) { |
| | 1066 | $object_subtype = $args['object_subtype']; |
| | 1067 | } else { |
| | 1068 | $object_subtype = $object_type; |
| | 1069 | } |
| | 1070 | |
| | 1071 | /** |
| | 1072 | * Need a filter docblock here, also how late should this go? |
| | 1073 | * And what orders should the context args be in? Most useful first, prob. |
| | 1074 | */ |
| | 1075 | apply_filters( 'register_meta_args', $args, $defaults, $meta_key, $object_type, $object_subtype ); |
| | 1076 | |
| | 1077 | $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args; |
| | 1078 | |
| | 1079 | // Back-compat: old sanitize and auth callbacks applied to all of an object type |
| | 1080 | if ( $has_old_sanitize_cb && is_callable( $args->sanitize_callback ) ) { |
| | 1081 | add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args->sanitize_callback, 10, 3 ); |
| | 1082 | } elseif ( is_callable( $args->sanitize_callback ) ) { |
| | 1083 | add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args->sanitize_callback, 10, 3 ); |
| | 1084 | } |
| | 1085 | |
| | 1086 | // If `auth_callback` is not provided, fall back to `is_protected_meta()`. |
| | 1087 | if ( empty( $args->auth_callback ) ) { |
| | 1088 | if ( is_protected_meta( $meta_key, $object_type ) ) { |
| | 1089 | $args->auth_callback = '__return_false'; |
| | 1090 | } else { |
| | 1091 | $args->auth_callback = '__return_true'; |
| | 1092 | } |
| | 1093 | } |
| | 1094 | |
| | 1095 | if ( $has_old_auth_cb && is_callable( $args->auth_callback ) ) { |
| | 1096 | add_filter( "auth_{$object_type}_meta_{$meta_key}", $args->auth_callback, 10, 3 ); |
| | 1097 | } elseif ( is_callable( $args->auth_callback ) ) { |
| | 1098 | add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args->auth_callback, 10, 3 ); |
| | 1099 | } |
| | 1101 | |
| | 1102 | /** |
| | 1103 | * Checks if a meta key is registered. |
| | 1104 | * |
| | 1105 | * @since 4.6.0 |
| | 1106 | * |
| | 1107 | * @param string $object_type |
| | 1108 | * @param string $meta_key |
| | 1109 | * @param string $object_subtype |
| | 1110 | * |
| | 1111 | * @return bool True if the meta key is registered to the object type and subtype. False if not. |
| | 1112 | */ |
| | 1113 | function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { |
| | 1114 | global $wp_meta_keys; |
| | 1115 | |
| | 1116 | if ( ! is_array( $wp_meta_keys ) ) { |
| | 1117 | return false; |
| | 1118 | } |
| | 1119 | |
| | 1120 | // Only specific core object types are supported. |
| | 1121 | if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) { |
| | 1122 | return false; |
| | 1123 | } |
| | 1124 | |
| | 1125 | // Posts are the only object type with support for object sub types. |
| | 1126 | if ( empty( $object_subtype ) || 'post' !== $object_type ) { |
| | 1127 | $object_subtype = $object_type; |
| | 1128 | } |
| | 1129 | |
| | 1130 | if ( ! isset( $wp_meta_keys[ $object_type] ) ) { |
| | 1131 | return false; |
| | 1132 | } |
| | 1133 | |
| | 1134 | if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
| | 1135 | return false; |
| | 1136 | } |
| | 1137 | |
| | 1138 | if ( isset( $wp_meta_keys[ $object_type][ $object_subtype][ $meta_key ] ) ) { |
| | 1139 | return true; |
| | 1140 | } |
| | 1141 | |
| | 1142 | return false; |
| | 1143 | } |
| | 1144 | |
| | 1145 | /** |
| | 1146 | * Unregisters a meta key from the list of registered keys. |
| | 1147 | * |
| | 1148 | * @since 4.6.0 |
| | 1149 | * |
| | 1150 | * @param string $object_type The type of object. |
| | 1151 | * @param string $meta_key The meta key. |
| | 1152 | * @param string $object_subtype Optional. The subtype of the object type. |
| | 1153 | * |
| | 1154 | * @return bool|WP_Error True if successful. WP_Error if the meta key is invalid. |
| | 1155 | */ |
| | 1156 | function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { |
| | 1157 | global $wp_meta_keys; |
| | 1158 | |
| | 1159 | if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { |
| | 1160 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) ); |
| | 1161 | } |
| | 1162 | |
| | 1163 | unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ); |
| | 1164 | |
| | 1165 | return true; |
| | 1166 | } |
| | 1167 | |
| | 1168 | /** |
| | 1169 | * Retrieves a list of registered meta keys for an object type and subtype. |
| | 1170 | * |
| | 1171 | * @since 4.6.0 |
| | 1172 | * |
| | 1173 | * @param string $object_type The type of object. Post, comment, user, term. |
| | 1174 | * @param string $object_subtype A subtype of the object. |
| | 1175 | * |
| | 1176 | * @return array List of registered meta keys. |
| | 1177 | */ |
| | 1178 | function get_registered_meta_keys( $object_type = 'post', $object_subtype = '' ) { |
| | 1179 | global $wp_meta_keys; |
| | 1180 | |
| | 1181 | if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { |
| | 1182 | return array(); |
| | 1183 | } |
| | 1184 | |
| | 1185 | if ( empty( $object_subtype ) || 'post' !== $object_type ) { |
| | 1186 | $object_subtype = $object_type; |
| | 1187 | } |
| | 1188 | |
| | 1189 | if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
| | 1190 | return array(); |
| | 1191 | } |
| | 1192 | |
| | 1193 | return $wp_meta_keys[ $object_type ][ $object_subtype ]; |
| | 1194 | } |
| | 1195 | |
| | 1196 | /** |
| | 1197 | * Retrieves registered metadata for a specified object. |
| | 1198 | * |
| | 1199 | * @since 4.6.0 |
| | 1200 | * |
| | 1201 | * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) |
| | 1202 | * @param int $object_id ID of the object the metadata is for. |
| | 1203 | * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered |
| | 1204 | * metadata for the specified object. |
| | 1205 | * @param string $object_subtype The subtype of the object's type to request metadata for. (e.g. custom post type) |
| | 1206 | * |
| | 1207 | * @return mixed|WP_Error |
| | 1208 | */ |
| | 1209 | function get_registered_metadata( $object_type = 'post', $object_id, $meta_key = '', $object_subtype = '' ) { |
| | 1210 | global $wp_meta_keys; |
| | 1211 | |
| | 1212 | if ( ! is_array( $wp_meta_keys ) ) { |
| | 1213 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); |
| | 1214 | } |
| | 1215 | |
| | 1216 | if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) { |
| | 1217 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) ); |
| | 1218 | } |
| | 1219 | |
| | 1220 | if ( empty( $object_subtype ) || 'post' !== $object_type ) { |
| | 1221 | $object_subtype = $object_type; |
| | 1222 | } elseif ( 'post' === $object_type ) { |
| | 1223 | $object_subtype = get_post_type( $object_id ); |
| | 1224 | } |
| | 1225 | |
| | 1226 | if( ! empty( $meta_key ) ) { |
| | 1227 | if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { |
| | 1228 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); |
| | 1229 | } |
| | 1230 | $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
| | 1231 | $meta_key_data = $meta_keys[ $object_type ][ $object_subtype ][ $meta_key ]; |
| | 1232 | |
| | 1233 | $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data->single ); |
| | 1234 | |
| | 1235 | return $data; |
| | 1236 | } |
| | 1237 | |
| | 1238 | $data = get_metadata( $object_type, $object_id, $meta_key ); |
| | 1239 | |
| | 1240 | $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
| | 1241 | $registered_data = array(); |
| | 1242 | |
| | 1243 | foreach( $meta_keys as $k => $v ) { |
| | 1244 | if ( isset( $data[ $k ] ) ) { |
| | 1245 | $registered_data[ $k ] = $data[ $k ]; |
| | 1246 | } |
| | 1247 | } |
| | 1248 | |
| | 1249 | return $registered_data; |
| | 1250 | } |