Changeset 38095 for trunk/src/wp-includes/meta.php
- Timestamp:
- 07/18/2016 09:15:37 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/meta.php
r38041 r38095 937 937 * 938 938 * @since 3.1.3 939 * @since 4.6.0 Added the `$object_subtype` parameter.940 939 * 941 940 * @param string $meta_key Meta key. 942 941 * @param mixed $meta_value Meta value to sanitize. 943 942 * @param string $object_type Type of object the meta is registered to. 944 * @param string $object_subtype Optional. Subtype of object. Will inherit the object type by default.945 943 * 946 944 * @return mixed Sanitized $meta_value. 947 945 */ 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 } 966 946 function sanitize_meta( $meta_key, $meta_value, $object_type ) { 967 947 /** 968 948 * Filters the sanitization of a specific meta key of a specific meta type. … … 973 953 * 974 954 * @since 3.3.0 975 * @since 4.6.0 Added the `$object_subtype` parameter.976 955 * 977 956 * @param mixed $meta_value Meta value to sanitize. 978 957 * @param string $meta_key Meta key. 979 958 * @param string $object_type Object type. 980 * @param string $object_subtype Object subtype.981 959 */ 982 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type , $object_subtype);960 return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type ); 983 961 } 984 962 … … 996 974 * Data used to describe the meta key when registered. 997 975 * 998 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type.999 976 * @type string $type The type of data associated with this meta key. 1000 977 * @type string $description A description of the data attached to this meta key. … … 1006 983 * @param string|array $deprecated Deprecated. Use `$args` instead. 1007 984 * 1008 * @return bool |WP_error True if the meta key was successfully registered in the global array, WP_Errorif not.985 * @return bool True if the meta key was successfully registered in the global array, false if not. 1009 986 * Registering a meta key with distinct sanitize and auth callbacks will fire those 1010 * callbacks, but will not add to the global registry as it requires a subtype.987 * callbacks, but will not add to the global registry. 1011 988 */ 1012 989 function register_meta( $object_type, $meta_key, $args, $deprecated = null ) { … … 1017 994 } 1018 995 1019 if ( ! wp_object_type_exists( $object_type ) ) {1020 return new WP_Error( 'register_meta_failed', __( 'Meta can only be registered against a core object type.' ) );1021 }1022 1023 996 $defaults = array( 1024 'object_subtype' => '',1025 997 'type' => 'string', 1026 998 'description' => '', … … 1050 1022 } 1051 1023 1052 $args = wp_parse_args( $args, $defaults );1053 1054 1024 /** 1055 1025 * Filters the registration arguments when registering meta. … … 1063 1033 */ 1064 1034 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1065 1066 // Object subtype is required if using the args style of registration 1067 if ( ! $has_old_sanitize_cb && ! $has_old_auth_cb && empty( $args['object_subtype'] ) ) { 1068 return new WP_Error( 'register_meta_failed', __( 'Meta must be registered against an object subtype.' ) ); 1069 } 1035 $args = wp_parse_args( $args, $defaults ); 1070 1036 1071 1037 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. … … 1078 1044 } 1079 1045 1080 $object_subtype = '';1081 1082 if ( ! empty( $args['object_subtype'] ) ) {1083 $object_subtype = $args['object_subtype'];1084 }1085 1086 1046 // Back-compat: old sanitize and auth callbacks are applied to all of an object type. 1087 if ( $has_old_sanitize_cb &&is_callable( $args['sanitize_callback'] ) ) {1088 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4);1089 } 1090 1091 if ( $has_old_auth_cb &&is_callable( $args['auth_callback'] ) ) {1047 if ( is_callable( $args['sanitize_callback'] ) ) { 1048 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'], 10, 3 ); 1049 } 1050 1051 if ( is_callable( $args['auth_callback'] ) ) { 1092 1052 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1093 1053 } 1094 1054 1095 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb) { 1096 if ( is_callable( $args['sanitize_callback'] ) ) { 1097 add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'], 10, 4 ); 1098 } 1099 1100 if ( is_callable( $args['auth_callback'] ) ) { 1101 add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1102 } 1103 } 1104 1105 // Global registry only contains meta keys registered in the new way with a subtype. 1106 if ( ! empty( $object_subtype ) ) { 1107 $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args; 1055 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1056 if ( ! $has_old_auth_cb && ! $has_old_sanitize_cb ) { 1057 $wp_meta_keys[ $object_type ][ $meta_key ] = $args; 1108 1058 1109 1059 return true; 1110 1060 } 1111 1061 1112 return new WP_Error( 'register_meta_failed', __( 'Sanitize and auth callbacks registered; meta key not registered.' ) );1062 return false; 1113 1063 } 1114 1064 … … 1119 1069 * 1120 1070 * @param string $object_type The type of object. 1121 * @param string $object_subtype The subtype of the object type.1122 1071 * @param string $meta_key The meta key. 1123 1072 * 1124 * @return bool|WP_error True if the meta key is registered to the object type and subtype. False if not. 1125 * WP_Error if an invalid object type is passed. 1126 */ 1127 function registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) { 1073 * @return bool True if the meta key is registered to the object type. False if not. 1074 */ 1075 function registered_meta_key_exists( $object_type, $meta_key ) { 1128 1076 global $wp_meta_keys; 1129 1077 … … 1132 1080 } 1133 1081 1134 // Only specific core object types are supported.1135 if ( ! wp_object_type_exists( $object_type ) ) {1136 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) );1137 }1138 1139 1082 if ( ! isset( $wp_meta_keys[ $object_type ] ) ) { 1140 1083 return false; 1141 1084 } 1142 1085 1143 if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1144 return false; 1145 } 1146 1147 if ( isset( $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] ) ) { 1086 if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) { 1148 1087 return true; 1149 1088 } … … 1158 1097 * 1159 1098 * @param string $object_type The type of object. 1160 * @param string $object_subtype The subtype of the object type.1161 1099 * @param string $meta_key The meta key. 1162 1100 * 1163 * @return bool |WP_Error True if successful. WP_Error if the meta key is invalid.1164 */ 1165 function unregister_meta_key( $object_type, $ object_subtype, $meta_key ) {1101 * @return bool True if successful. False if the meta key was not registered. 1102 */ 1103 function unregister_meta_key( $object_type, $meta_key ) { 1166 1104 global $wp_meta_keys; 1167 1105 1168 if ( ! registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) ) {1169 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key' ) );1170 } 1171 1172 $args = $wp_meta_keys[ $object_type ][ $ object_subtype ][ $meta_key ];1106 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1107 return false; 1108 } 1109 1110 $args = $wp_meta_keys[ $object_type ][ $meta_key ]; 1173 1111 1174 1112 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 1175 remove_filter( "sanitize_{$object_type}_ {$object_subtype}_meta_{$meta_key}", $args['sanitize_callback'] );1113 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); 1176 1114 } 1177 1115 1178 1116 if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) { 1179 remove_filter( "auth_{$object_type}_ {$object_subtype}_meta_{$meta_key}", $args['auth_callback'] );1180 } 1181 1182 unset( $wp_meta_keys[ $object_type ][ $ object_subtype ][ $meta_key ] );1117 remove_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] ); 1118 } 1119 1120 unset( $wp_meta_keys[ $object_type ][ $meta_key ] ); 1183 1121 1184 1122 // Do some clean up 1185 if ( empty( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) {1186 unset( $wp_meta_keys[ $object_type ][ $object_subtype ] );1187 }1188 1189 1123 if ( empty( $wp_meta_keys[ $object_type ] ) ) { 1190 1124 unset( $wp_meta_keys[ $object_type ] ); … … 1195 1129 1196 1130 /** 1197 * Retrieves a list of registered meta keys for an object type and optionally subtype.1131 * Retrieves a list of registered meta keys for an object type. 1198 1132 * 1199 1133 * @since 4.6.0 1200 1134 * 1201 * @param string $object_type The type of object. Post, comment, user, term. 1202 * @param string $object_subtype Optional. A subtype of the object (e.g. custom post type). 1135 * @param string $object_type The type of object. Post, comment, user, term. 1203 1136 * 1204 1137 * @return array List of registered meta keys. 1205 1138 */ 1206 function get_registered_meta_keys( $object_type , $object_subtype = '') {1139 function get_registered_meta_keys( $object_type ) { 1207 1140 global $wp_meta_keys; 1208 1141 … … 1211 1144 } 1212 1145 1213 if ( empty( $object_subtype ) && isset( $wp_meta_keys[ $object_type ] ) ) { 1214 return $wp_meta_keys[ $object_type ]; 1215 } 1216 1217 if ( ! isset( $wp_meta_keys[ $object_type ][ $object_subtype ] ) ) { 1218 return array(); 1219 } 1220 1221 return $wp_meta_keys[ $object_type ][ $object_subtype ]; 1146 return $wp_meta_keys[ $object_type ]; 1222 1147 } 1223 1148 … … 1227 1152 * @since 4.6.0 1228 1153 * 1229 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) 1230 * @param string $object_subtype The subtype of the object's type to request metadata for. (e.g. custom post type) 1231 * @param int $object_id ID of the object the metadata is for. 1232 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered 1233 * metadata for the specified object. 1234 * 1235 * @return mixed|WP_Error 1236 */ 1237 function get_registered_metadata( $object_type, $object_subtype, $object_id, $meta_key = '' ) { 1238 global $wp_meta_keys; 1239 1240 if ( ! is_array( $wp_meta_keys ) ) { 1241 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) ); 1242 } 1243 1244 if ( ! wp_object_type_exists( $object_type ) ) { 1245 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) ); 1246 } 1247 1154 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) 1155 * @param int $object_id ID of the object the metadata is for. 1156 * @param string $meta_key Optional. Registered metadata key. If not specified, retrieve all registered 1157 * metadata for the specified object. 1158 * 1159 * @return mixed A single value or array of values for a key if specified. An array of all registered keys 1160 * and values for an object ID if not. 1161 */ 1162 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { 1248 1163 if ( ! empty( $meta_key ) ) { 1249 if ( ! registered_meta_key_exists( $object_type, $ object_subtype, $meta_key ) ) {1250 return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not registered.' ) );1251 } 1252 $meta_keys = get_registered_meta_keys( $object_type , $object_subtype);1164 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) { 1165 return false; 1166 } 1167 $meta_keys = get_registered_meta_keys( $object_type ); 1253 1168 $meta_key_data = $meta_keys[ $meta_key ]; 1254 1169 … … 1260 1175 $data = get_metadata( $object_type, $object_id ); 1261 1176 1262 $meta_keys = get_registered_meta_keys( $object_type , $object_subtype);1177 $meta_keys = get_registered_meta_keys( $object_type ); 1263 1178 $registered_data = array(); 1264 1179
Note: See TracChangeset
for help on using the changeset viewer.