Make WordPress Core

Ticket #35658: 35658.4.diff

File 35658.4.diff, 10.9 KB (added by jeremyfelt, 9 years ago)
  • src/wp-includes/meta.php

     
    936936 * Sanitize meta value.
    937937 *
    938938 * @since 3.1.3
     939 * @since 4.6.0 Added the `$object_subtype` parameter.
    939940 *
    940  * @param string $meta_key   Meta key
    941  * @param mixed  $meta_value Meta value to sanitize
    942  * @param string $meta_type  Type of meta
    943  * @return mixed Sanitized $meta_value
     941 * @param string $meta_key       Meta key.
     942 * @param mixed  $meta_value     Meta value to sanitize.
     943 * @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 *
     946 * @return mixed Sanitized $meta_value.
    944947 */
    945 function sanitize_meta( $meta_key, $meta_value, $meta_type ) {
     948function sanitize_meta( $meta_key, $meta_value, $object_type, $object_subtype = '' ) {
     949        if ( empty( $object_subtype ) ) {
     950                $object_subtype = $object_type;
     951        }
    946952
    947953        /**
    948954         * Filters the sanitization of a specific meta key of a specific meta type.
     
    958964         * @param string $meta_key   Meta key.
    959965         * @param string $meta_type  Meta type.
    960966         */
    961         return apply_filters( "sanitize_{$meta_type}_meta_{$meta_key}", $meta_value, $meta_key, $meta_type );
     967        return apply_filters( "sanitize_{$object_type}_meta_{$meta_key}", $meta_value, $meta_key, $object_type );
    962968}
    963969
    964970/**
    965  * Register meta key
     971 * Registers a meta key.
    966972 *
    967973 * @since 3.3.0
     974 * @since 4.6.0 Modified to support an array of data to attach to registered meta keys. Previous arguments for
     975 *              `$sanitize_callback` and `$auth_callback` have been folded into this array.
    968976 *
    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 * }
    973989 */
    974 function register_meta( $meta_type, $meta_key, $sanitize_callback, $auth_callback = null ) {
    975         if ( is_callable( $sanitize_callback ) )
    976                 add_filter( "sanitize_{$meta_type}_meta_{$meta_key}", $sanitize_callback, 10, 3 );
     990function register_meta( $object_type, $meta_key, $args ) {
     991        global $wp_meta_keys;
    977992
    978         if ( empty( $auth_callback ) ) {
    979                 if ( is_protected_meta( $meta_key, $meta_type ) )
    980                         $auth_callback = '__return_false';
    981                 else
    982                         $auth_callback = '__return_true';
     993        if ( ! is_array( $wp_meta_keys ) ) {
     994                $wp_meta_keys = array();
    983995        }
    984996
    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        $meta_args = (object) array(
     1002                'sanitize_callback' => null,
     1003                'old_sanitize_callback' => null,
     1004                'auth_callback' => null,
     1005                'old_auth_callback' => null,
     1006                'object_subtype' => '',
     1007                'type' => 'string',
     1008                'description' => '',
     1009                'show_in_rest' => false,
     1010        );
     1011
     1012        $passed_args = array_slice( func_get_args(), 2 );
     1013
     1014        if ( is_callable( $passed_args[0] ) ) {
     1015                $meta_args->old_sanitize_callback = $passed_args[0];
     1016        } elseif ( isset( $passed_args[0]['sanitize_callback'] ) ) {
     1017                $meta_args->sanitize_callback = $passed_args[0]['sanitize_callback'];
     1018        }
     1019
     1020        if ( isset( $passed_args[1] ) && is_callable( $passed_args[1] ) ) {
     1021                $meta_args->old_auth_callback = $passed_args[1];
     1022        } elseif ( isset( $passed_args[0]['auth_callback'] ) ) {
     1023                $meta_args->auth_callback = $passed_args[0]['auth_callback'];
     1024        }
     1025
     1026        if ( isset( $passed_args[0]['show_in_rest'] ) && $passed_args[0]['show_in_rest'] ) {
     1027                $meta_args->show_in_rest = true;
     1028        }
     1029
     1030        if ( isset( $passed_args[0]['type'] ) ) {
     1031                $meta_args->type = $passed_args[0]['type'];
     1032        }
     1033
     1034        if ( isset( $passed_args[0]['description'] ) ) {
     1035                $meta_args->description = $passed_args[0]['description'];
     1036        }
     1037
     1038        // Object subtypes are only supported for posts.
     1039        if ( 'post' === $object_type && isset( $passed_args[0]['object_subtype'] ) ) {
     1040                $object_subtype = $passed_args[0]['object_subtype'];
     1041        } else {
     1042                $object_subtype = $object_type;
     1043        }
     1044
     1045        $wp_meta_keys[ $object_type ][ $object_subtype ][ $meta_key ] = $args;
     1046
     1047        if ( is_callable( $meta_args->old_sanitize_callback ) ) {
     1048                add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $meta_args->old_sanitize_callback, 10, 3 );
     1049        }
     1050
     1051        if ( is_callable( $meta_args->sanitize_callback ) ) {
     1052                add_filter( "sanitize_{$object_type}_meta_{$meta_key}", $meta_args->sanitize_callback, 10, 3 );
     1053        }
     1054
     1055        // If neither new or legacy `auth_callback` is provided, fallback to `is_protected_meta()`.
     1056        if ( empty( $meta_args->auth_callback ) && empty( $meta_args->old_auth_callback ) ) {
     1057                if ( is_protected_meta( $meta_key, $object_type ) ) {
     1058                        $meta_args->auth_callback = '__return_false';
     1059                } else {
     1060                        $meta_args->auth_callback = '__return_true';
     1061                }
     1062        }
     1063
     1064        // The auth here is currently used to edit or add meta, not to view, and only for posts.
     1065        if ( 'post' === $object_type && is_callable( $meta_args->old_auth_callback ) ) {
     1066                add_filter( "auth_{$object_type}_meta_{$meta_key}", $meta_args->old_auth_callback, 10, 6 );
     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        }
    9871072}
     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 */
     1085function 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 */
     1128function 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 */
     1150function 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 */
     1181function 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}