Ticket #38323: 38323.3.diff
File 38323.3.diff, 26.9 KB (added by , 6 years ago) |
---|
-
src/wp-includes/class-wp-meta-key.php
1 <?php 2 /** 3 * Meta API: WP_Meta_Key class. 4 * 5 * @package WordPress 6 * @subpackage Meta 7 * @since 4.8.0 8 */ 9 10 /** 11 * Class representing a registered meta key. 12 * 13 * @since 4.8.0 14 */ 15 class WP_Meta_Key { 16 17 /** 18 * Name of the meta key. 19 * 20 * @since 4.8.0 21 * @access private 22 * @var string 23 */ 24 private $meta_key = ''; 25 26 /** 27 * Type of object this meta key is registered to. 28 * 29 * @since 4.8.0 30 * @access private 31 * @var string 32 */ 33 private $object_type = ''; 34 35 /** 36 * Definition sets used to describe the meta key. 37 * 38 * @since 4.8.0 39 * @access private 40 * @var array 41 */ 42 private $definition_sets = array(); 43 44 /** 45 * Constructor. 46 * 47 * Registers the initial meta key definition set. 48 * 49 * @since 4.8.0 50 * @access public 51 * 52 * @param string $object_type Type of object this meta is registered to. 53 * @param string $meta_key Name of the meta key. 54 * @param array $args $args { 55 * Data used to describe the meta key. 56 * 57 * @type string $object_subtype A subtype; e.g. if the object type is "post", the post type. If left empty, 58 * the meta key will be registered on the entire object type. Default empty. 59 * @type string $type The type of data associated with this meta key. 60 * @type string $description A description of the data attached to this meta key. 61 * @type bool $single Whether the meta key has one value per object, or an array of values per object. 62 * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. 63 * @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. 64 * @type bool $show_in_rest Whether data associated with this meta key can be considered public. 65 * } 66 */ 67 public function __construct( $object_type, $meta_key, $args ) { 68 $this->object_type = $object_type; 69 $this->meta_key = $meta_key; 70 71 $this->register( $args ); 72 } 73 74 /** 75 * Registers a new definition set for this meta key. 76 * 77 * @since 4.8.0 78 * @access public 79 * 80 * @param array $args Data to describe the meta key. See `WP_Meta_Key::__construct()` for the list of supported arguments. 81 * @return bool|WP_Error True on success, WP_Error on failure. 82 */ 83 public function register( $args ) { 84 $defaults = $this->get_defaults(); 85 86 /** 87 * Filters the registration arguments when registering meta. 88 * 89 * @since 4.6.0 90 * 91 * @param array $args Array of meta registration arguments. 92 * @param array $defaults Array of default arguments. 93 * @param string $object_type Object type. 94 * @param string $meta_key Meta key. 95 */ 96 $args = apply_filters( 'register_meta_args', $args, $defaults, $this->object_type, $this->meta_key ); 97 $args = wp_parse_args( $args, $defaults ); 98 99 if ( empty( $args['auth_callback'] ) ) { 100 $args['auth_callback'] = self::get_default_auth_callback( $this->object_type, $this->meta_key ); 101 } 102 103 $object_subtype = isset( $args['object_subtype'] ) ? $args['object_subtype'] : ''; 104 105 /* 106 * A meta key cannot be registered on a subtype if it is already registered for the entire type. 107 * It can also not be registered for the entire type if it is already registered on a subtype. 108 */ 109 if ( ! empty( $object_subtype ) && isset( $this->definition_sets[ '' ] ) 110 || empty( $object_subtype ) && ! empty( $this->definition_sets ) ) { 111 return new WP_Error( 'meta_key_already_exists', sprintf( __( 'The meta key %1$s is already registered for the object type %2$s.' ), $this->meta_key, $this->object_type ) ); 112 } 113 114 $this->definition_sets[ $object_subtype ] = $args; 115 116 $this->add_filters( $object_subtype ); 117 118 return true; 119 } 120 121 /** 122 * Unregisters an existing definition set for this meta key. 123 * 124 * @since 4.8.0 125 * @access public 126 * 127 * @param string $object_subtype Optional. Name of the object subtype to remove its definition. When 128 * this is left empty, the definition set for the entire object type is 129 * unregistered. Default empty. 130 * @return bool|WP_Error True on success, WP_Error on failure. 131 */ 132 public function unregister( $object_subtype = '' ) { 133 if ( ! isset( $this->definition_sets[ $object_subtype ] ) ) { 134 if ( empty( $object_subtype ) ) { 135 return new WP_Error( 'meta_key_not_exists', sprintf( __( 'The meta key %1$s is not registered for the object type %2$s.' ), $this->meta_key, $this->object_type ) ); 136 } 137 138 return new WP_Error( 'meta_key_not_exists', sprintf( __( 'The meta key %1$s is not registered for the object type %2$s and subtype %3$s.' ), $this->meta_key, $this->object_type, $object_subtype ) ); 139 } 140 141 $this->remove_filters( $object_subtype ); 142 143 unset( $this->definition_sets[ $object_subtype ] ); 144 145 return true; 146 } 147 148 /** 149 * Checks whether a definition set is registered for a specific subtype. 150 * 151 * If the meta key is not registered for $object_subtype specifically, the method will also check 152 * whether it is registered for the entire object type. 153 * 154 * @since 4.8.0 155 * @access public 156 * 157 * @param string $object_subtype Optional. Name of the object subtype to check for. When this is left 158 * empty, the method checks for the definition set of the entire object 159 * type. Default empty. 160 * @return bool True if a definition set is registered, false otherwise. 161 */ 162 public function is_registered( $object_subtype = '' ) { 163 if ( ! isset( $this->definition_sets[ $object_subtype ] ) ) { 164 if ( empty( $object_subtype ) || ! isset( $this->definition_sets[''] ) ) { 165 return false; 166 } 167 } 168 169 return true; 170 } 171 172 /** 173 * Returns the definition set for a specific subtype. 174 * 175 * If the meta key is not registered for $object_subtype, the method will return the definition set 176 * registered for the entire type if available. 177 * 178 * @since 4.8.0 179 * @access public 180 * 181 * @param string $object_subtype Optional. Name of the subtype to get its definition set. When this is left 182 * empty, the method returns the definition set for the entire object type. 183 * Default empty. 184 * @return array|false Definition set data, or false if not registered. 185 */ 186 public function get_definition_set( $object_subtype = '' ) { 187 if ( ! isset( $this->definition_sets[ $object_subtype ] ) ) { 188 if ( empty( $object_subtype ) ) { 189 return false; 190 } 191 192 $object_subtype = ''; 193 194 if ( ! isset( $this->definition_sets[ $object_subtype ] ) ) { 195 return false; 196 } 197 } 198 199 return $this->definition_sets[ $object_subtype ]; 200 } 201 202 /** 203 * Returns the definition sets of all subtypes. 204 * 205 * @since 4.8.0 206 * @access public 207 * 208 * @return array Definition sets as `$object_subtype => $data` pairs. 209 */ 210 public function get_subtype_definition_sets() { 211 $definition_sets = $this->definition_sets; 212 if ( isset( $definition_sets[''] ) ) { 213 unset( $definition_sets[''] ); 214 } 215 216 return $definition_sets; 217 } 218 219 /** 220 * Returns the default arguments for a definition set. 221 * 222 * @since 4.8.0 223 * @access private 224 * 225 * @return array Array of default arguments. 226 */ 227 private function get_defaults() { 228 return array( 229 'object_subtype' => '', 230 'type' => 'string', 231 'description' => '', 232 'single' => false, 233 'sanitize_callback' => null, 234 'auth_callback' => null, 235 'show_in_rest' => false, 236 ); 237 } 238 239 /** 240 * Adds filters for sanitize and auth callbacks of this meta key. 241 * 242 * @since 4.8.0 243 * @access private 244 * 245 * @param string $object_subtype Optional. Name of the object subtype to add the filters to. When this is left empty, 246 * the method adds the filters to the entire object type. Default empty. 247 */ 248 private function add_filters( $object_subtype = '' ) { 249 $args = $this->definition_sets[ $object_subtype ]; 250 251 self::add_legacy_filters( $this->object_type, $this->meta_key, $args['sanitize_callback'], $args['auth_callback'], $object_subtype ); 252 } 253 254 /** 255 * Removes filters for sanitize and auth callbacks of this meta key. 256 * 257 * @since 4.8.0 258 * @access private 259 * 260 * @param string $object_subtype Optional. Name of the object subtype to remove the filters from. When this is left empty, 261 * the method removes the filters from the entire object type. Default empty. 262 */ 263 private function remove_filters( $object_subtype = '' ) { 264 $args = $this->definition_sets[ $object_subtype ]; 265 266 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 267 if ( ! empty( $object_subtype ) ) { 268 remove_filter( "sanitize_{$this->object_type}_{$object_subtype}_meta_{$this->meta_key}", $args['sanitize_callback'] ); 269 } else { 270 remove_filter( "sanitize_{$this->object_type}_meta_{$this->meta_key}", $args['sanitize_callback'] ); 271 } 272 } 273 274 if ( isset( $args['auth_callback'] ) && is_callable( $args['auth_callback'] ) ) { 275 if ( ! empty( $object_subtype ) ) { 276 remove_filter( "auth_{$this->object_type}_{$object_subtype}_meta_{$this->meta_key}", $args['auth_callback'] ); 277 } else { 278 remove_filter( "auth_{$this->object_type}_meta_{$this->meta_key}", $args['auth_callback'] ); 279 } 280 } 281 } 282 283 /** 284 * Adds filters for sanitize and auth callbacks of a specific meta key. 285 * 286 * @since 4.8.0 287 * @access public 288 * @static 289 * 290 * @param string $object_type Object type. 291 * @param string $meta_key Meta key. 292 * @param callable $sanitize_callback A function or method to call when sanitizing `$meta_key` data. 293 * @param callable $auth_callback Optional. A function or method to call when performing edit_post_meta, add_post_meta, and delete_post_meta capability checks. 294 * @param string $object_subtype Optional. Name of the object subtype to add the filters to. When this is left empty, 295 * the method adds the filters to the entire object type. Default empty. 296 */ 297 public static function add_legacy_filters( $object_type, $meta_key, $sanitize_callback, $auth_callback = null, $object_subtype = '' ) { 298 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. 299 if ( empty( $auth_callback ) ) { 300 $auth_callback = self::get_default_auth_callback( $object_type, $meta_key ); 301 } 302 303 if ( is_callable( $sanitize_callback ) ) { 304 if ( ! empty( $object_subtype ) ) { 305 add_filter( "sanitize_{$object_type}_{$object_subtype}_meta_{$meta_key}", $sanitize_callback, 10, 4 ); 306 } else { 307 add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $sanitize_callback, 10, 4 ); 308 } 309 } 310 311 if ( is_callable( $auth_callback ) ) { 312 if ( ! empty( $object_subtype ) ) { 313 add_filter( "auth_{$object_type}_{$object_subtype}_meta_{$meta_key}", $auth_callback, 10, 6 ); 314 } else { 315 add_filter( "auth_{$object_type}_meta_{$meta_key}", $auth_callback, 10, 6 ); 316 } 317 } 318 } 319 320 /** 321 * Returns the default authorization callback. 322 * 323 * @since 4.8.0 324 * @access private 325 * @static 326 * 327 * @param string $object_type Object type. 328 * @param string $meta_key Meta key. 329 * @return callable Default authorization callback. 330 */ 331 private static function get_default_auth_callback( $object_type, $meta_key ) { 332 if ( is_protected_meta( $meta_key, $object_type ) ) { 333 return '__return_false'; 334 } 335 336 return '__return_true'; 337 } 338 } -
src/wp-includes/meta.php
Property changes on: src/wp-includes/class-wp-meta-key.php ___________________________________________________________________ Added: svn:executable ## -0,0 +1 ## +* \ No newline at end of property
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://core.trac.wordpress.org/ticket/35658 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 Registered meta keys are now objects of the `WP_Meta_Key` class. Registering meta for 998 * specific object subtypes is now supported. 970 999 * 971 * @param string $object_type Type of object this meta is registered to. 972 * @param string $meta_key Meta key to register. 973 * @param array $args { 974 * Data used to describe the meta key when registered. 975 * 976 * @type string $type The type of data associated with this meta key. 977 * @type string $description A description of the data attached to this meta key. 978 * @type bool $single Whether the meta key has one value per object, or an array of values per object. 979 * @type string $sanitize_callback A function or method to call when sanitizing `$meta_key` data. 980 * @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. 981 * @type bool $show_in_rest Whether data associated with this meta key can be considered public. 982 * } 983 * @param string|array $deprecated Deprecated. Use `$args` instead. 1000 * @param string $object_type Type of object this meta is registered to. 1001 * @param string $meta_key Meta key to register. 1002 * @param array $args Data used to describe the meta key when registered. See 1003 * `WP_Meta_Key::__construct()` for the list of supported arguments. 1004 * @param string|array $deprecated Deprecated. Use `$args` instead. 984 1005 * 985 1006 * @return bool True if the meta key was successfully registered in the global array, false if not. 986 1007 * Registering a meta key with distinct sanitize and auth callbacks will fire those … … 993 1014 $wp_meta_keys = array(); 994 1015 } 995 1016 996 $defaults = array( 997 'type' => 'string', 998 'description' => '', 999 'single' => false, 1000 'sanitize_callback' => null, 1001 'auth_callback' => null, 1002 'show_in_rest' => false, 1003 ); 1004 1005 // There used to be individual args for sanitize and auth callbacks 1006 $has_old_sanitize_cb = false; 1007 $has_old_auth_cb = false; 1008 1009 if ( is_callable( $args ) ) { 1010 $args = array( 1011 'sanitize_callback' => $args, 1012 ); 1017 // Global registry only contains meta keys registered with the array of arguments added in 4.6.0. 1018 if ( is_callable( $args ) || is_callable( $deprecated ) ) { 1019 WP_Meta_Key::add_legacy_filters( $object_type, $meta_key, $args, $deprecated ); 1013 1020 1014 $has_old_sanitize_cb = true; 1015 } else { 1016 $args = (array) $args; 1021 return false; 1017 1022 } 1018 1023 1019 if ( is_callable( $deprecated ) ) { 1020 $args['auth_callback'] = $deprecated; 1021 $has_old_auth_cb = true; 1022 } 1024 $args = (array) $args; 1023 1025 1024 /** 1025 * Filters the registration arguments when registering meta. 1026 * 1027 * @since 4.6.0 1028 * 1029 * @param array $args Array of meta registration arguments. 1030 * @param array $defaults Array of default arguments. 1031 * @param string $object_type Object type. 1032 * @param string $meta_key Meta key. 1033 */ 1034 $args = apply_filters( 'register_meta_args', $args, $defaults, $object_type, $meta_key ); 1035 $args = wp_parse_args( $args, $defaults ); 1026 if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) { 1027 $meta_key_object = $wp_meta_keys[ $object_type ][ $meta_key ]; 1036 1028 1037 // If `auth_callback` is not provided, fall back to `is_protected_meta()`. 1038 if ( empty( $args['auth_callback'] ) ) { 1039 if ( is_protected_meta( $meta_key, $object_type ) ) { 1040 $args['auth_callback'] = '__return_false'; 1041 } else { 1042 $args['auth_callback'] = '__return_true'; 1029 if ( is_wp_error( $meta_key_object->register( $args ) ) ) { 1030 return false; 1043 1031 } 1044 } 1045 1046 // Back-compat: old sanitize and auth callbacks are applied to all of an object type. 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'] ) ) { 1052 add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 ); 1053 } 1054 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; 1032 } else { 1033 $meta_key_object = new WP_Meta_Key( $object_type, $meta_key, $args ); 1058 1034 1059 return true;1035 $wp_meta_keys[ $object_type ][ $meta_key ] = $meta_key_object; 1060 1036 } 1061 1037 1062 return false;1038 return true; 1063 1039 } 1064 1040 1065 1041 /** 1066 1042 * Checks if a meta key is registered. 1067 1043 * 1068 1044 * @since 4.6.0 1045 * @since 4.8.0 The `$object_subtype` parameter was added. 1069 1046 * 1070 1047 * @param string $object_type The type of object. 1071 1048 * @param string $meta_key The meta key. 1049 * @param string $object_subtype Optional. The subtype of the object type. 1072 1050 * 1073 1051 * @return bool True if the meta key is registered to the object type. False if not. 1074 1052 */ 1075 function registered_meta_key_exists( $object_type, $meta_key ) {1053 function registered_meta_key_exists( $object_type, $meta_key, $object_subtype = '' ) { 1076 1054 global $wp_meta_keys; 1077 1055 1078 1056 if ( ! is_array( $wp_meta_keys ) ) { … … 1083 1061 return false; 1084 1062 } 1085 1063 1086 if ( isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) {1087 return true;1064 if ( ! isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) { 1065 return false; 1088 1066 } 1089 1067 1090 return false; 1068 if ( ! $wp_meta_keys[ $object_type ][ $meta_key ]->is_registered( $object_subtype ) ) { 1069 return false; 1070 } 1071 1072 return true; 1091 1073 } 1092 1074 1093 1075 /** 1094 1076 * Unregisters a meta key from the list of registered keys. 1095 1077 * 1096 1078 * @since 4.6.0 1079 * @since 4.8.0 The `$object_subtype` parameter was added. 1097 1080 * 1098 * @param string $object_type The type of object. 1099 * @param string $meta_key The meta key. 1081 * @param string $object_type The type of object. 1082 * @param string $meta_key The meta key. 1083 * @param string $object_subtype Optional. The subtype of the object type. 1100 1084 * @return bool True if successful. False if the meta key was not registered. 1101 1085 */ 1102 function unregister_meta_key( $object_type, $meta_key ) {1086 function unregister_meta_key( $object_type, $meta_key, $object_subtype = '' ) { 1103 1087 global $wp_meta_keys; 1104 1088 1105 if ( ! registered_meta_key_exists( $object_type, $meta_key) ) {1089 if ( ! isset( $wp_meta_keys[ $object_type ][ $meta_key ] ) ) { 1106 1090 return false; 1107 1091 } 1108 1092 1109 $args = $wp_meta_keys[ $object_type ][ $meta_key ]; 1110 1111 if ( isset( $args['sanitize_callback'] ) && is_callable( $args['sanitize_callback'] ) ) { 1112 remove_filter( "sanitize_{$object_type}_meta_{$meta_key}", $args['sanitize_callback'] ); 1113 } 1093 $meta_key_object = $wp_meta_keys[ $object_type ][ $meta_key ]; 1114 1094 1115 if ( is set( $args['auth_callback'] ) && is_callable( $args['auth_callback']) ) {1116 re move_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'] );1095 if ( is_wp_error( $meta_key_object->unregister( $object_subtype ) ) ) { 1096 return false; 1117 1097 } 1118 1098 1119 unset( $wp_meta_keys[ $object_type ][ $meta_key ] );1120 1121 1099 // Do some clean up 1100 if ( ! $meta_key_object->get_definition_set() && empty( $meta_key_object->get_subtype_definition_sets() ) ) { 1101 unset( $wp_meta_keys[ $object_type ][ $meta_key ] ); 1102 } 1122 1103 if ( empty( $wp_meta_keys[ $object_type ] ) ) { 1123 1104 unset( $wp_meta_keys[ $object_type ] ); 1124 1105 } … … 1130 1111 * Retrieves a list of registered meta keys for an object type. 1131 1112 * 1132 1113 * @since 4.6.0 1114 * @since 4.8.0 The `$object_subtype` parameter was added. 1133 1115 * 1134 * @param string $object_type The type of object. Post, comment, user, term. 1116 * @param string $object_type The type of object. Post, comment, user, term. 1117 * @param string $object_subtype Optional. The subtype of the object type. 1135 1118 * @return array List of registered meta keys. 1136 1119 */ 1137 function get_registered_meta_keys( $object_type ) {1120 function get_registered_meta_keys( $object_type, $object_subtype = '' ) { 1138 1121 global $wp_meta_keys; 1139 1122 1140 1123 if ( ! is_array( $wp_meta_keys ) || ! isset( $wp_meta_keys[ $object_type ] ) ) { 1141 1124 return array(); 1142 1125 } 1143 1126 1144 return $wp_meta_keys[ $object_type ]; 1127 $meta_keys = array(); 1128 foreach ( $wp_meta_keys[ $object_type ] as $meta_key => $meta_key_object ) { 1129 if ( ! $meta_key_object->is_registered( $object_subtype ) ) { 1130 continue; 1131 } 1132 1133 $meta_keys[ $meta_key ] = $meta_key_object->get_definition_set( $object_subtype ); 1134 } 1135 1136 return $meta_keys; 1145 1137 } 1146 1138 1147 1139 /** 1148 1140 * Retrieves registered metadata for a specified object. 1149 1141 * 1142 * The results include both meta that is registered specifically for the 1143 * object's subtype and meta that is registered for the entire object type. 1144 * 1150 1145 * @since 4.6.0 1151 1146 * 1152 1147 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) … … 1157 1152 * and values for an object ID if not. 1158 1153 */ 1159 1154 function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) { 1155 $object_subtype = get_object_subtype( $object_type, $object_id ); 1156 1160 1157 if ( ! empty( $meta_key ) ) { 1161 if ( ! registered_meta_key_exists( $object_type, $meta_key ) ) {1158 if ( ! registered_meta_key_exists( $object_type, $meta_key, $object_subtype ) ) { 1162 1159 return false; 1163 1160 } 1164 $meta_keys = get_registered_meta_keys( $object_type ); 1161 1162 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); 1165 1163 $meta_key_data = $meta_keys[ $meta_key ]; 1166 1164 1167 1165 $data = get_metadata( $object_type, $object_id, $meta_key, $meta_key_data['single'] ); … … 1171 1169 1172 1170 $data = get_metadata( $object_type, $object_id ); 1173 1171 1174 $meta_keys = get_registered_meta_keys( $object_type );1172 $meta_keys = get_registered_meta_keys( $object_type, $object_subtype ); 1175 1173 $registered_data = array(); 1176 1174 1177 1175 // Someday, array_filter() … … 1209 1207 1210 1208 return $args; 1211 1209 } 1210 1211 /** 1212 * Returns the object subtype for a given object ID of a specific type. 1213 * 1214 * @since 4.8.0 1215 * 1216 * @param string $object_type Type of object to request metadata for. (e.g. comment, post, term, user) 1217 * @param int $object_id ID of the object to retrieve its subtype. 1218 * @return string The object subtype or an empty string if unspecified subtype. 1219 */ 1220 function get_object_subtype( $object_type, $object_id ) { 1221 $object_subtype = ''; 1222 1223 switch ( $object_type ) { 1224 case 'post': 1225 $post_type = get_post_type( $object_id ); 1226 if ( $post_type ) { 1227 $object_subtype = $post_type; 1228 } 1229 break; 1230 case 'term': 1231 $term = get_term( $object_id ); 1232 if ( $term ) { 1233 $object_subtype = $term->taxonomy; 1234 } 1235 break; 1236 case 'comment': 1237 $comment = get_comment( $object_id ); 1238 if ( $comment ) { 1239 $object_subtype = $comment->comment_type; 1240 } 1241 break; 1242 } 1243 1244 return apply_filters( "get_object_subtype_{$object_type}", $object_subtype, $object_id ); 1245 } -
src/wp-settings.php
159 159 require( ABSPATH . WPINC . '/class-wp-session-tokens.php' ); 160 160 require( ABSPATH . WPINC . '/class-wp-user-meta-session-tokens.php' ); 161 161 require( ABSPATH . WPINC . '/meta.php' ); 162 require( ABSPATH . WPINC . '/class-wp-meta-key.php' ); 162 163 require( ABSPATH . WPINC . '/class-wp-meta-query.php' ); 163 164 require( ABSPATH . WPINC . '/class-wp-metadata-lazyloader.php' ); 164 165 require( ABSPATH . WPINC . '/general-template.php' );