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