Make WordPress Core


Ignore:
Timestamp:
07/18/2016 09:15:37 PM (10 years ago)
Author:
jeremyfelt
Message:

Meta: Remove object subtype handling from register_meta().

Registration is now based solely on object type, which allows the code around this to be simplified significantly.

In the process of making this adjustment:

  • register_meta(), unregister_meta_key(), get_registered_metadata(), and registered_meta_key_exists() no longer return WP_Error objects.
  • The recently introduced wp_object_type_exists() function and the restriction on object type has been removed.

Note: No guarantee of uniqueness is made across object subtypes. Registered meta keys should be uniquely prefixed to avoid conflict.

Fixes #35658.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/meta.php

    r38041 r38095  
    937937 *
    938938 * @since 3.1.3
    939  * @since 4.6.0 Added the `$object_subtype` parameter.
    940939 *
    941940 * @param string $meta_key       Meta key.
    942941 * @param mixed  $meta_value     Meta value to sanitize.
    943942 * @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.
    945943 *
    946944 * @return mixed Sanitized $meta_value.
    947945 */
    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 
     946function sanitize_meta( $meta_key, $meta_value, $object_type ) {
    967947        /**
    968948         * Filters the sanitization of a specific meta key of a specific meta type.
     
    973953         *
    974954         * @since 3.3.0
    975          * @since 4.6.0 Added the `$object_subtype` parameter.
    976955         *
    977956         * @param mixed  $meta_value      Meta value to sanitize.
    978957         * @param string $meta_key        Meta key.
    979958         * @param string $object_type     Object type.
    980          * @param string $object_subtype  Object subtype.
    981959         */
    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 );
    983961}
    984962
     
    996974 *     Data used to describe the meta key when registered.
    997975 *
    998  *     @type string $object_subtype    A subtype; e.g. if the object type is "post", the post type.
    999976 *     @type string $type              The type of data associated with this meta key.
    1000977 *     @type string $description       A description of the data attached to this meta key.
     
    1006983 * @param string|array $deprecated Deprecated. Use `$args` instead.
    1007984 *
    1008  * @return bool|WP_error True if the meta key was successfully registered in the global array, WP_Error if not.
     985 * @return bool True if the meta key was successfully registered in the global array, false if not.
    1009986 *                       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.
    1011988 */
    1012989function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
     
    1017994        }
    1018995
    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 
    1023996        $defaults = array(
    1024                 'object_subtype'    => '',
    1025997                'type'              => 'string',
    1026998                'description'       => '',
     
    10501022        }
    10511023
    1052         $args = wp_parse_args( $args, $defaults );
    1053 
    10541024        /**
    10551025         * Filters the registration arguments when registering meta.
     
    10631033         */
    10641034        $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 );
    10701036
    10711037        // If `auth_callback` is not provided, fall back to `is_protected_meta()`.
     
    10781044        }
    10791045
    1080         $object_subtype = '';
    1081 
    1082         if ( ! empty( $args['object_subtype'] ) ) {
    1083                 $object_subtype = $args['object_subtype'];
    1084         }
    1085 
    10861046        // 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'] ) ) {
    10921052                add_filter( "auth_{$object_type}_meta_{$meta_key}", $args['auth_callback'], 10, 6 );
    10931053        }
    10941054
    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;
    11081058
    11091059                return true;
    11101060        }
    11111061
    1112         return new WP_Error( 'register_meta_failed', __( 'Sanitize and auth callbacks registered; meta key not registered.' ) );
     1062        return false;
    11131063}
    11141064
     
    11191069 *
    11201070 * @param string $object_type    The type of object.
    1121  * @param string $object_subtype The subtype of the object type.
    11221071 * @param string $meta_key       The meta key.
    11231072 *
    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 */
     1075function registered_meta_key_exists( $object_type, $meta_key ) {
    11281076        global $wp_meta_keys;
    11291077
     
    11321080        }
    11331081
    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 
    11391082        if ( ! isset( $wp_meta_keys[ $object_type ] ) ) {
    11401083                return false;
    11411084        }
    11421085
    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 ] ) ) {
    11481087                return true;
    11491088        }
     
    11581097 *
    11591098 * @param string $object_type    The type of object.
    1160  * @param string $object_subtype The subtype of the object type.
    11611099 * @param string $meta_key       The meta key.
    11621100 *
    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 */
     1103function unregister_meta_key( $object_type, $meta_key ) {
    11661104        global $wp_meta_keys;
    11671105
    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 ];
    11731111
    11741112        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'] );
    11761114        }
    11771115
    11781116        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 ] );
    11831121
    11841122        // 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 
    11891123        if ( empty( $wp_meta_keys[ $object_type ] ) ) {
    11901124                unset( $wp_meta_keys[ $object_type ] );
     
    11951129
    11961130/**
    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.
    11981132 *
    11991133 * @since 4.6.0
    12001134 *
    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.
    12031136 *
    12041137 * @return array List of registered meta keys.
    12051138 */
    1206 function get_registered_meta_keys( $object_type, $object_subtype = '' ) {
     1139function get_registered_meta_keys( $object_type ) {
    12071140        global $wp_meta_keys;
    12081141
     
    12111144        }
    12121145
    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 ];
    12221147}
    12231148
     
    12271152 * @since 4.6.0
    12281153 *
    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 */
     1162function get_registered_metadata( $object_type, $object_id, $meta_key = '' ) {
    12481163        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 );
    12531168                $meta_key_data = $meta_keys[ $meta_key ];
    12541169
     
    12601175        $data = get_metadata( $object_type, $object_id );
    12611176
    1262         $meta_keys = get_registered_meta_keys( $object_type, $object_subtype );
     1177        $meta_keys = get_registered_meta_keys( $object_type );
    12631178        $registered_data = array();
    12641179
Note: See TracChangeset for help on using the changeset viewer.