Ticket #38323: 38323.diff
File 38323.diff, 13.4 KB (added by , 8 years ago) |
---|
-
src/wp-includes/meta.php
44 44 return false; 45 45 } 46 46 47 $meta_subtype = get_object_subtype( $meta_type, $object_id ); 48 47 49 $column = sanitize_key($meta_type . '_id'); 48 50 49 51 // expected_slashed ($meta_key) 50 52 $meta_key = wp_unslash($meta_key); 51 53 $meta_value = wp_unslash($meta_value); 52 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );54 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); 53 55 54 56 /** 55 57 * Filters whether to add metadata of a specific type. … … 157 159 return false; 158 160 } 159 161 162 $meta_subtype = get_object_subtype( $meta_type, $object_id ); 163 160 164 $column = sanitize_key($meta_type . '_id'); 161 165 $id_column = 'user' == $meta_type ? 'umeta_id' : 'meta_id'; 162 166 … … 165 169 $meta_key = wp_unslash($meta_key); 166 170 $passed_value = $meta_value; 167 171 $meta_value = wp_unslash($meta_value); 168 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );172 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); 169 173 170 174 /** 171 175 * Filters whether to update metadata of a specific type. … … 641 645 return false; 642 646 } 643 647 648 $meta_subtype = get_object_subtype( $meta_type, $object_id ); 649 644 650 // Sanitize the meta 645 651 $_meta_value = $meta_value; 646 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type );652 $meta_value = sanitize_meta( $meta_key, $meta_value, $meta_type, $meta_subtype ); 647 653 $meta_value = maybe_serialize( $meta_value ); 648 654 649 655 // Format the data query arguments. … … 936 942 * Sanitize meta value. 937 943 * 938 944 * @since 3.1.3 945 * @since 4.8.0 The `$object_subtype` parameter was added. 939 946 * 940 947 * @param string $meta_key Meta key. 941 948 * @param mixed $meta_value Meta value to sanitize. … … 943 950 * 944 951 * @return mixed Sanitized $meta_value. 945 952 */ 946 function sanitize_meta( $meta_key, $meta_value, $object_type ) { 953 function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) { 954 if ( ! empty( $object_subtype ) ) { 955 /** 956 * Filters the sanitization of a specific meta key of a specific meta type and subtype. 957 * 958 * The dynamic portions of the hook name, `$object_type`, `$object_subtype` 959 * and `$meta_key`, refer to the metadata object type (comment, post, term or user), 960 * the object subtype and the meta key value, respectively. 961 * 962 * @since 4.8.0 963 * 964 * @param mixed $meta_value Meta value to sanitize. 965 * @param string $meta_key Meta key. 966 * @param string $object_type Object type. 967 * @param string $object_subtype Object subtype. 968 */ 969 $meta_value = apply_filters( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype ); 970 } 971 947 972 /** 948 973 * Filters the sanitization of a specific meta key of a specific meta type. 949 974 * … … 952 977 * key value, respectively. 953 978 * 954 979 * @since 3.3.0 980 * @since 4.8.0 Added the `$object_subtype` parameter. 955 981 * 956 * @param mixed $meta_value Meta value to sanitize. 957 * @param string $meta_key Meta key. 958 * @param string $object_type Object type. 982 * @param mixed $meta_value Meta value to sanitize. 983 * @param string $meta_key Meta key. 984 * @param string $object_type Object type. 985 * @param string $object_subtype Object subtype. 959 986 */ 960 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );987 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type, $object_subtype ); 961 988 } 962 989 963 990 /** … … 967 994 * @since 4.6.0 {@link https://make.wordpress.org/core/2016/07/08/enhancing-register_meta-in-4-6/ Modified 968 995 * to support an array of data to attach to registered meta keys}. Previous arguments for 969 996 * `$sanitize_callback` and `$auth_callback` have been folded into this array. 997 * @since 4.8.0 The `$object_subtype` argument was added to the arguments array. 970 998 * 971 999 * @param string $object_type Type of object this meta is registered to. 972 1000 * @param string $meta_key Meta key to register. 973 1001 * @param array $args { 974 1002 * Data used to describe the meta key when registered. 975 1003 * 1004 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, 1005 * the meta key will be registered on the entire object type. Default empty. 976 1006 * @type string $type The type of data associated with this meta key. 977 1007 * @type string $description A description of the data attached to this meta key. 978 1008 * @type bool $single Whether the meta key has one value per object, or an array of values per object. … … 994 1024 } 995 1025 996 1026 $defaults = array( 1027 'object_subtype' => '', 997 1028 'type' => 'string', 998 1029 'description' => '', 999 1030 'single' => false, … … 1034 1065 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1035 1066 $args = wp_parse_args( $args, $defaults ); 1036 1067 1068 $object_subtype = ! empty( $args['object_subtype'] ) ? $args['object_subtype'] : ''; 1069 1037 1070 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. 1038 1071 if ( empty( $args['auth_callback'] ) ) { 1039 1072 if ( is_protected_meta( $meta_key, $object_type ) ) { … … 1045 1078 1046 1079 // Back-compat: old sanitize and auth callbacks are applied to all of an object type. 1047 1080 if ( is_callable( $args['sanitize_callback'] ) ) { 1048 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); 1081 if ( ! empty( $object_subtype ) ) { 1082 add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 ); 1083 } else { 1084 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 ); 1085 } 1049 1086 } 1050 1087 1051 1088 if ( is_callable( $args['auth_callback'] ) ) { 1052 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1089 if ( ! empty( $object_subtype ) ) { 1090 add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1091 } else { 1092 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1093 } 1053 1094 } 1054 1095 1055 1096 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1056 1097 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1057 $wp_meta_keys[ $object_type ][ $ meta_key ] = $args;1098 $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args; 1058 1099 1059 1100 return true; 1060 1101 } … … 1066 1107 * Checks if a meta key is registered. 1067 1108 * 1068 1109 * @since 4.6.0 1110 * @since 4.8.0 The `$object_subtype` parameter was added. 1069 1111 * 1070 1112 * @param string $object_type The type of object. 1071 1113 * @param string $meta_key The meta key. 1114 * @param string $object_subtype Optional. The subtype of the object type. 1072 1115 * 1073 1116 * @return bool True if the meta key is registered to the object type. False if not. 1074 1117 */ 1075 function registered_meta_key_exists( $object_type, $meta_key ) {1118 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { 1076 1119 global $wp_meta_keys; 1077 1120 1078 1121 if ( ! is_array( $wp_meta_keys ) ) { … … 1083 1126 return false; 1084 1127 } 1085 1128 1086 if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) { 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 ] ) ) { 1087 1134 return true; 1088 1135 } 1089 1136 … … 1094 1141 * Unregisters a meta key from the list of registered keys. 1095 1142 * 1096 1143 * @since 4.6.0 1144 * @since 4.8.0 The `$object_subtype` parameter was added. 1097 1145 * 1098 * @param string $object_type The type of object. 1099 * @param string $meta_key The meta key. 1146 * @param string $object_type The type of object. 1147 * @param string $meta_key The meta key. 1148 * @param string $object_subtype Optional. The subtype of the object type. 1100 1149 * @return bool True if successful. False if the meta key was not registered. 1101 1150 */ 1102 function unregister_meta_key( $object_type, $meta_key ) {1151 function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { 1103 1152 global $wp_meta_keys; 1104 1153 1105 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {1154 if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1106 1155 return false; 1107 1156 } 1108 1157 1109 $args = $wp_meta_keys[ $object_type ][ $ meta_key ];1158 $args = $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ]; 1110 1159 1111 1160 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 1112 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); 1161 if ( ! empty( $object_subtype ) ) { 1162 remove_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'] ); 1163 } else { 1164 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); 1165 } 1113 1166 } 1114 1167 1115 1168 if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) { 1116 remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] ); 1169 if ( ! empty( $object_subtype ) ) { 1170 remove_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'] ); 1171 } else { 1172 remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] ); 1173 } 1117 1174 } 1118 1175 1119 unset( $wp_meta_keys[ $object_type ][ $ meta_key ] );1176 unset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ); 1120 1177 1121 1178 // Do some clean up 1179 if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1180 unset( $wp_meta_keys[ $object_type ][ $object_subtype ] ); 1181 } 1122 1182 if ( empty( $wp_meta_keys[ $object_type ] ) ) { 1123 1183 unset( $wp_meta_keys[ $object_type ] ); 1124 1184 } … … 1130 1190 * Retrieves a list of registered meta keys for an object type. 1131 1191 * 1132 1192 * @since 4.6.0 1193 * @since 4.8.0 The `$object_subtype` parameter was added. 1133 1194 * 1134 * @param string $object_type The type of object. Post, comment, user, term. 1195 * @param string $object_type The type of object. Post, comment, user, term. 1196 * @param string $object_subtype Optional. The subtype of the object type. 1135 1197 * @return array List of registered meta keys. 1136 1198 */ 1137 function get_registered_meta_keys( $object_type ) {1199 function get_registered_meta_keys( $object_type, $object_subtype = '' ) { 1138 1200 global $wp_meta_keys; 1139 1201 1140 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) ) {1202 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) || ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1141 1203 return array(); 1142 1204 } 1143 1205 1144 return $wp_meta_keys[ $object_type ] ;1206 return $wp_meta_keys[ $object_type ][ $object_subtype ]; 1145 1207 } 1146 1208 1147 1209 /** 1148 1210 * Retrieves registered metadata for a specified object. 1149 1211 * 1212 * The results include both meta that is registered specifically for the 1213 * object's subtype and meta that is registered for the entire object type. 1214 * 1150 1215 * @since 4.6.0 1151 1216 * 1152 1217 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) … … 1157 1222 * and values for an object ID if not. 1158 1223 */ 1159 1224 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { 1225 $object_subtype = get_object_subtype( $object_type, $object_id ); 1226 1160 1227 if ( ! empty( $meta_key ) ) { 1161 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1228 if ( ! empty( $object_subtype ) ) { 1229 if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1230 $object_subtype = ''; 1231 } 1232 } 1233 1234 if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1162 1235 return false; 1163 1236 } 1164 $meta_keys = get_registered_meta_keys( $object_type ); 1237 1238 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); 1165 1239 $meta_key_data = $meta_keys[ $meta_key ]; 1166 1240 1167 1241 $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] ); … … 1171 1245 1172 1246 $data = get_metadata( $object_type, $object_id ); 1173 1247 1174 $meta_keys = get_registered_meta_keys( $object_type );1248 $meta_keys = get_registered_meta_keys( $object_type, '' ); 1175 1249 $registered_data = array(); 1176 1250 1177 1251 // Someday, array_filter() … … 1181 1255 } 1182 1256 } 1183 1257 1258 if ( ! empty( $object_subtype ) ) { 1259 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); 1260 1261 foreach ( $meta_keys as $k => $v ) { 1262 if ( isset( $data[ $k ] ) ) { 1263 $registered_data[ $k ] = $data[ $k ]; 1264 } 1265 } 1266 } 1267 1184 1268 return $registered_data; 1185 1269 } 1186 1270 … … 1209 1293 1210 1294 return $args; 1211 1295 } 1296 1297 /** 1298 * Returns the object subtype for a given object ID of a specific type. 1299 * 1300 * @since 4.8.0 1301 * 1302 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) 1303 * @param int $object_id ID of the object to retrieve its subtype. 1304 * @return string The object subtype or an empty string if unspecified subtype. 1305 */ 1306 function get_object_subtype( $object_type, $object_id ) { 1307 $object_subtype = ''; 1308 1309 switch ( $object_type ) { 1310 case 'post': 1311 $post_type = get_post_type( $object_id ); 1312 if ( $post_type ) { 1313 $object_subtype = $post_type; 1314 } 1315 break; 1316 case 'term': 1317 $term = get_term( $object_id ); 1318 if ( $term ) { 1319 $object_subtype = $term->taxonomy; 1320 } 1321 break; 1322 case 'comment': 1323 $comment = get_comment( $object_id ); 1324 if ( $comment ) { 1325 $object_subtype = $comment->comment_type; 1326 } 1327 break; 1328 } 1329 1330 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1331 }