| 945 | | function sanitize_meta( $meta_key, $meta_value, $meta_type ) { |
| | 948 | function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { |
| | 949 | if ( ! empty( $object_subtype ) ) { |
| | 950 | /** |
| | 951 | * Filters the sanitization of a specific meta key of a specific meta type and subtype. |
| | 952 | * |
| | 953 | * The dynamic portions of the hook name, `$meta_type`, `$meta_subtype`, |
| | 954 | * and `$meta_key`, refer to the metadata object type (comment, post, or user) |
| | 955 | * the object subtype, and the meta key value, respectively. |
| | 956 | * |
| | 957 | * @since 4.6.0 |
| | 958 | * |
| | 959 | * @param mixed $meta_value Meta value to sanitize. |
| | 960 | * @param string $meta_key Meta key. |
| | 961 | * @param string $object_type Object type. |
| | 962 | * @param string $object_subtype Object subtype. |
| | 963 | */ |
| | 964 | $meta_value = apply_filters( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype ); |
| | 965 | } |
| 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 A subtype; e.g. 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 | * |
| | 1006 | * @return bool True if the meta key was successfully registered in the global array, false if not. |
| | 1007 | * Registering a meta key with distinct sanitize and auth callbacks will fire those |
| | 1008 | * callbacks, but will not add to the global registry as it requires a subtype. |
| | 1009 | * } |
| 985 | | if ( is_callable( $auth_callback ) ) |
| 986 | | add_filter( "auth_{$meta_type}_meta_{$meta_key}", $auth_callback, 10, 6 ); |
| | 1018 | /* translators: object type name */ |
| | 1019 | if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) { |
| | 1020 | _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid object type: %s.' ), $object_type ), '4.6.0' ); |
| | 1021 | } |
| | 1022 | |
| | 1023 | $defaults = array( |
| | 1024 | 'sanitize_callback' => null, |
| | 1025 | 'auth_callback' => null, |
| | 1026 | 'object_subtype' => '', |
| | 1027 | 'type' => 'string', |
| | 1028 | 'description' => '', |
| | 1029 | 'show_in_rest' => false, |
| | 1030 | ); |
| | 1031 | |
| | 1032 | $passed_args = array_slice( func_get_args(), 2 ); |
| | 1033 | |
| | 1034 | // There used to be individual args for sanitize and auth callbacks |
| | 1035 | $has_old_sanitize_cb = $has_old_auth_cb = false; |
| | 1036 | |
| | 1037 | if ( is_callable( $passed_args[0] ) ) { |
| | 1038 | $args['sanitize_callback'] = $passed_args[0]; |
| | 1039 | $has_old_sanitize_cb = true; |
| | 1040 | } else { |
| | 1041 | $args = $passed_args[0]; |
| | 1042 | } |
| | 1043 | |
| | 1044 | if ( isset( $passed_args[1] ) && is_callable( $passed_args[1] ) ) { |
| | 1045 | $args['auth_callback'] = $passed_args[1]; |
| | 1046 | $has_old_auth_cb = true; |
| | 1047 | } |
| | 1048 | |
| | 1049 | $args = wp_parse_args( $args, $defaults ); |
| | 1050 | |
| | 1051 | /** |
| | 1052 | * Filters the registration arguments when registering meta. |
| | 1053 | * |
| | 1054 | * @since 4.6.0 |
| | 1055 | * |
| | 1056 | * @param array $args Array of meta registration arguments. |
| | 1057 | * @param array $defaults Array of default arguments. |
| | 1058 | * @param string $object_type Object type. |
| | 1059 | * @param string $meta_key Meta key. |
| | 1060 | */ |
| | 1061 | $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); |
| | 1062 | |
| | 1063 | // Object subtype is required if using the args style of registration |
| | 1064 | if ( ! $has_old_sanitize_cb && empty( $args['object_subtype'] ) ) { |
| | 1065 | return false; |
| | 1066 | } |
| | 1067 | |
| | 1068 | // Back-compat: old sanitize and auth callbacks applied to all of an object type |
| | 1069 | if ( $has_old_sanitize_cb ) { |
| | 1070 | add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); |
| | 1071 | } elseif ( is_callable( $args['sanitize_callback'] ) && ! empty( $object_subtype ) ) { |
| | 1072 | add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); |
| | 1073 | } |
| | 1074 | |
| | 1075 | // If `auth_callback` is not provided, fall back to `is_protected_meta()`. |
| | 1076 | if ( empty( $args['auth_callback'] ) ) { |
| | 1077 | if ( is_protected_meta( $meta_key, $object_type ) ) { |
| | 1078 | $args['auth_callback'] = '__return_false'; |
| | 1079 | } else { |
| | 1080 | $args['auth_callback'] = '__return_true'; |
| | 1081 | } |
| | 1082 | } |
| | 1083 | |
| | 1084 | if ( $has_old_auth_cb ) { |
| | 1085 | add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 3 ); |
| | 1086 | } elseif ( is_callable( $args['auth_callback'] ) && ! empty( $object_subtype ) ) { |
| | 1087 | add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'], 10, 3 ); |
| | 1088 | } |
| | 1089 | |
| | 1090 | $object_subtype = $args['object_subtype']; |
| | 1091 | |
| | 1092 | // Global registry only contains meta keys registered in the new way with a subtype. |
| | 1093 | if ( ! empty( $object_subtype ) ) { |
| | 1094 | $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args; |
| | 1095 | |
| | 1096 | return true; |
| | 1097 | } |
| | 1098 | |
| | 1099 | return false; |
| | 1101 | |
| | 1102 | /** |
| | 1103 | * Checks if a meta key is registered. |
| | 1104 | * |
| | 1105 | * @since 4.6.0 |
| | 1106 | * |
| | 1107 | * @param string $object_type The type of object. |
| | 1108 | * @param string $object_subtype The subtype of the object type. |
| | 1109 | * @param string $meta_key The meta key. |
| | 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, $object_subtype, $meta_key ) { |
| | 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 | if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { |
| | 1126 | return false; |
| | 1127 | } |
| | 1128 | |
| | 1129 | if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
| | 1130 | return false; |
| | 1131 | } |
| | 1132 | |
| | 1133 | if ( isset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ) ) { |
| | 1134 | return true; |
| | 1135 | } |
| | 1136 | |
| | 1137 | return false; |
| | 1138 | } |
| | 1139 | |
| | 1140 | /** |
| | 1141 | * Unregisters a meta key from the list of registered keys. |
| | 1142 | * |
| | 1143 | * @since 4.6.0 |
| | 1144 | * |
| | 1145 | * @param string $object_type The type of object. |
| | 1146 | * @param string $object_subtype The subtype of the object type. |
| | 1147 | * @param string $meta_key The meta key. |
| | 1148 | * |
| | 1149 | * @return bool|WP_Error True if successful. WP_Error if the meta key is invalid. |
| | 1150 | */ |
| | 1151 | function unregister_meta_key( $object_type, $object_subtype, $meta_key ) { |
| | 1152 | global $wp_meta_keys; |
| | 1153 | |
| | 1154 | if ( ! registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) ) { |
| | 1155 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) ); |
| | 1156 | } |
| | 1157 | |
| | 1158 | unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ); |
| | 1159 | |
| | 1160 | return true; |
| | 1161 | } |
| | 1162 | |
| | 1163 | /** |
| | 1164 | * Retrieves a list of registered meta keys for an object type and optionally subtype. |
| | 1165 | * |
| | 1166 | * @since 4.6.0 |
| | 1167 | * |
| | 1168 | * @param string $object_type The type of object. Post, comment, user, term. |
| | 1169 | * @param string $object_subtype Optional. A subtype of the object (e.g. custom post type). |
| | 1170 | * |
| | 1171 | * @return array List of registered meta keys. |
| | 1172 | */ |
| | 1173 | function get_registered_meta_keys( $object_type, $object_subtype = '' ) { |
| | 1174 | global $wp_meta_keys; |
| | 1175 | |
| | 1176 | if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { |
| | 1177 | return array(); |
| | 1178 | } |
| | 1179 | |
| | 1180 | if ( empty( $object_subtype ) && isset( $wp_meta_keys[ $object_type ] ) ) { |
| | 1181 | return $wp_meta_keys[ $object_type ]; |
| | 1182 | } |
| | 1183 | |
| | 1184 | if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
| | 1185 | return array(); |
| | 1186 | } |
| | 1187 | |
| | 1188 | return $wp_meta_keys[ $object_type ][ $object_subtype ]; |
| | 1189 | } |
| | 1190 | |
| | 1191 | /** |
| | 1192 | * Retrieves registered metadata for a specified object. |
| | 1193 | * |
| | 1194 | * @since 4.6.0 |
| | 1195 | * |
| | 1196 | * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) |
| | 1197 | * @param string $object_subtype The subtype of the object's type to request metadata for. (e.g. custom post type) |
| | 1198 | * @param int $object_id ID of the object the metadata is for. |
| | 1199 | * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered |
| | 1200 | * metadata for the specified object. |
| | 1201 | * |
| | 1202 | * @return mixed|WP_Error |
| | 1203 | */ |
| | 1204 | function get_registered_metadata( $object_type, $object_subtype, $object_id, $meta_key = '' ) { |
| | 1205 | global $wp_meta_keys; |
| | 1206 | |
| | 1207 | if ( ! is_array( $wp_meta_keys ) ) { |
| | 1208 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); |
| | 1209 | } |
| | 1210 | |
| | 1211 | if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) { |
| | 1212 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) ); |
| | 1213 | } |
| | 1214 | |
| | 1215 | if ( ! empty( $meta_key ) ) { |
| | 1216 | if ( ! registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) ) { |
| | 1217 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); |
| | 1218 | } |
| | 1219 | $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
| | 1220 | $meta_key_data = $meta_keys[ $object_type ][ $object_subtype ][ $meta_key ]; |
| | 1221 | |
| | 1222 | $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data->single ); |
| | 1223 | |
| | 1224 | return $data; |
| | 1225 | } |
| | 1226 | |
| | 1227 | $data = get_metadata( $object_type, $object_id, $meta_key ); |
| | 1228 | |
| | 1229 | $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
| | 1230 | $registered_data = array(); |
| | 1231 | |
| | 1232 | // Someday, array_filter() |
| | 1233 | foreach ( $meta_keys as $k => $v ) { |
| | 1234 | if ( isset( $data[ $k ] ) ) { |
| | 1235 | $registered_data[ $k ] = $data[ $k ]; |
| | 1236 | } |
| | 1237 | } |
| | 1238 | |
| | 1239 | return $registered_data; |
| | 1240 | } |
| | 1241 | |
| | 1242 | /** |
| | 1243 | * Filter out `register_meta()` args based on a whitelist. |
| | 1244 | * `register_meta()` args may change over time, so requiring the whitelist |
| | 1245 | * to be explicitly turned off is a warranty seal of sorts. |
| | 1246 | * |
| | 1247 | * @access private |
| | 1248 | * @since 4.6.0 |
| | 1249 | * |
| | 1250 | * @param array $args Arguments from `register_meta()`. |
| | 1251 | * @param array $default_args Default arguments for `register_meta()`. |
| | 1252 | * |
| | 1253 | * @return array Filtered arguments. |
| | 1254 | */ |
| | 1255 | function _wp_register_meta_args_whitelist( $args, $default_args ) { |
| | 1256 | $whitelist = array_keys( $default_args ); |
| | 1257 | |
| | 1258 | // In an anonymous function world, this would be better as an array_filter() |
| | 1259 | foreach ( $args as $key => $value ) { |
| | 1260 | if ( ! in_array( $key, $whitelist ) ) { |
| | 1261 | unset( $args[ $key ] ); |
| | 1262 | } |
| | 1263 | } |
| | 1264 | |
| | 1265 | return $args; |
| | 1266 | } |