| 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 | if ( is_callable( $passed_args[0] ) ) { |
| | 1014 | $args->sanitize_callback = $passed_args[0]; |
| | 1015 | } else { |
| | 1016 | $args = $passed_args[0]; |
| | 1017 | } |
| | 1018 | |
| | 1019 | if ( isset( $passed_args[1] ) && is_callable( $passed_args[1] ) ) { |
| | 1020 | $args->auth_callback = $passed_args[1]; |
| | 1021 | } |
| | 1022 | |
| | 1023 | $args = wp_parse_args( $args, $defaults ); |
| | 1024 | |
| | 1025 | // PROOF OF CONCEPT ONLY |
| | 1026 | // This should be a function like like _wp_register_meta_whitelist() or some such |
| | 1027 | // And in default filters |
| | 1028 | // Also you could do the subtype enforcement in here too I guess |
| | 1029 | add_filter( 'register_meta_args', function( $args, $defaults ) { |
| | 1030 | $whitelist = array_keys( $defaults ); |
| | 1031 | |
| | 1032 | // In an anonymous function world, this would be way better as an array_filter |
| | 1033 | foreach ( $args as $key => $value ) { |
| | 1034 | if ( ! in_array( $key, $whitelist ) ) { |
| | 1035 | unset( $args[ $key ] ); |
| | 1036 | } |
| | 1037 | } |
| | 1038 | |
| | 1039 | return $args; |
| | 1040 | }, 10, 2 ) |
| | 1041 | |
| | 1042 | /** |
| | 1043 | * Need a filter docblock here, also how late should this go? |
| | 1044 | */ |
| | 1045 | apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); |
| | 1046 | |
| | 1047 | // Object subtypes are only supported for posts. |
| | 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 | $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args; |
| | 1055 | |
| | 1056 | if ( is_callable( $meta_args->sanitize_callback ) ) { |
| | 1057 | add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $meta_args->sanitize_callback, 10, 3 ); |
| | 1058 | } |
| | 1059 | |
| | 1060 | // If `auth_callback` is not provided, fall back to `is_protected_meta()`. |
| | 1061 | if ( empty( $meta_args->auth_callback ) ) { |
| | 1062 | if ( is_protected_meta( $meta_key, $object_type ) ) { |
| | 1063 | $args->auth_callback = '__return_false'; |
| | 1064 | } else { |
| | 1065 | $args->auth_callback = '__return_true'; |
| | 1066 | } |
| | 1067 | } |
| | 1068 | |
| | 1069 | if ( 'post' === $object_type && is_callable( $meta_args->auth_callback ) ) { |
| | 1070 | add_filter( "auth_{$object_type}_meta_{$meta_key}", $meta_args->auth_callback, 10, 6 ); |
| | 1071 | } |
| | 1073 | |
| | 1074 | /** |
| | 1075 | * Checks if a meta key is registered. |
| | 1076 | * |
| | 1077 | * @since 4.6.0 |
| | 1078 | * |
| | 1079 | * @param string $object_type |
| | 1080 | * @param string $meta_key |
| | 1081 | * @param string $object_subtype |
| | 1082 | * |
| | 1083 | * @return bool True if the meta key is registered to the object type and subtype. False if not. |
| | 1084 | */ |
| | 1085 | function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { |
| | 1086 | global $wp_meta_keys; |
| | 1087 | |
| | 1088 | if ( ! is_array( $wp_meta_keys ) ) { |
| | 1089 | return false; |
| | 1090 | } |
| | 1091 | |
| | 1092 | // Only specific core object types are supported. |
| | 1093 | if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) { |
| | 1094 | return false; |
| | 1095 | } |
| | 1096 | |
| | 1097 | // Posts are the only object type with support for object sub types. |
| | 1098 | if ( empty( $object_subtype ) || 'post' !== $object_type ) { |
| | 1099 | $object_subtype = $object_type; |
| | 1100 | } |
| | 1101 | |
| | 1102 | if ( ! isset( $wp_meta_keys[ $object_type] ) ) { |
| | 1103 | return false; |
| | 1104 | } |
| | 1105 | |
| | 1106 | if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
| | 1107 | return false; |
| | 1108 | } |
| | 1109 | |
| | 1110 | if ( isset( $wp_meta_keys[ $object_type][ $object_subtype][ $meta_key ] ) ) { |
| | 1111 | return true; |
| | 1112 | } |
| | 1113 | |
| | 1114 | return false; |
| | 1115 | } |
| | 1116 | |
| | 1117 | /** |
| | 1118 | * Unregisters a meta key from the list of registered keys. |
| | 1119 | * |
| | 1120 | * @since 4.6.0 |
| | 1121 | * |
| | 1122 | * @param string $object_type The type of object. |
| | 1123 | * @param string $meta_key The meta key. |
| | 1124 | * @param string $object_subtype Optional. The subtype of the object type. |
| | 1125 | * |
| | 1126 | * @return bool|WP_Error True if successful. WP_Error if the meta key is invalid. |
| | 1127 | */ |
| | 1128 | function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { |
| | 1129 | global $wp_meta_keys; |
| | 1130 | |
| | 1131 | if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { |
| | 1132 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) ); |
| | 1133 | } |
| | 1134 | |
| | 1135 | unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ); |
| | 1136 | |
| | 1137 | return true; |
| | 1138 | } |
| | 1139 | |
| | 1140 | /** |
| | 1141 | * Retrieves a list of registered meta keys for an object type and subtype. |
| | 1142 | * |
| | 1143 | * @since 4.6.0 |
| | 1144 | * |
| | 1145 | * @param string $object_type The type of object. Post, comment, user, term. |
| | 1146 | * @param string $object_subtype A subtype of the object. |
| | 1147 | * |
| | 1148 | * @return array List of registered meta keys. |
| | 1149 | */ |
| | 1150 | function get_registered_meta_keys( $object_type = 'post', $object_subtype = '' ) { |
| | 1151 | global $wp_meta_keys; |
| | 1152 | |
| | 1153 | if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { |
| | 1154 | return array(); |
| | 1155 | } |
| | 1156 | |
| | 1157 | if ( empty( $object_subtype ) || 'post' !== $object_type ) { |
| | 1158 | $object_subtype = $object_type; |
| | 1159 | } |
| | 1160 | |
| | 1161 | if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { |
| | 1162 | return array(); |
| | 1163 | } |
| | 1164 | |
| | 1165 | return $wp_meta_keys[ $object_type ][ $object_subtype ]; |
| | 1166 | } |
| | 1167 | |
| | 1168 | /** |
| | 1169 | * Retrieves registered metadata for a specified object. |
| | 1170 | * |
| | 1171 | * @since 4.6.0 |
| | 1172 | * |
| | 1173 | * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) |
| | 1174 | * @param int $object_id ID of the object the metadata is for. |
| | 1175 | * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered |
| | 1176 | * metadata for the specified object. |
| | 1177 | * @param string $object_subtype The subtype of the object's type to request metadata for. (e.g. custom post type) |
| | 1178 | * |
| | 1179 | * @return mixed|WP_Error |
| | 1180 | */ |
| | 1181 | function get_registered_metadata( $object_type = 'post', $object_id, $meta_key = '', $object_subtype = '' ) { |
| | 1182 | global $wp_meta_keys; |
| | 1183 | |
| | 1184 | if ( ! is_array( $wp_meta_keys ) ) { |
| | 1185 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); |
| | 1186 | } |
| | 1187 | |
| | 1188 | if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) { |
| | 1189 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) ); |
| | 1190 | } |
| | 1191 | |
| | 1192 | if ( empty( $object_subtype ) || 'post' !== $object_type ) { |
| | 1193 | $object_subtype = $object_type; |
| | 1194 | } elseif ( 'post' === $object_type ) { |
| | 1195 | $object_subtype = get_post_type( $object_id ); |
| | 1196 | } |
| | 1197 | |
| | 1198 | if( ! empty( $meta_key ) ) { |
| | 1199 | if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { |
| | 1200 | return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); |
| | 1201 | } |
| | 1202 | $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
| | 1203 | $meta_key_data = $meta_keys[ $object_type ][ $object_subtype ][ $meta_key ]; |
| | 1204 | |
| | 1205 | $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data->single ); |
| | 1206 | |
| | 1207 | return $data; |
| | 1208 | } |
| | 1209 | |
| | 1210 | $data = get_metadata( $object_type, $object_id, $meta_key ); |
| | 1211 | |
| | 1212 | $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); |
| | 1213 | $registered_data = array(); |
| | 1214 | |
| | 1215 | foreach( $meta_keys as $k => $v ) { |
| | 1216 | if ( isset( $data[ $k ] ) ) { |
| | 1217 | $registered_data[ $k ] = $data[ $k ]; |
| | 1218 | } |
| | 1219 | } |
| | 1220 | |
| | 1221 | return $registered_data; |
| | 1222 | } |