Make WordPress Core

Changeset 37991


Ignore:
Timestamp:
07/06/2016 06:08:55 PM (10 years ago)
Author:
helen
Message:

Meta: Make registration error conditions return consistently.

In doing this, non-core object types are no longer forcibly blocked and are instead checked against wp_object_type_exists() which has a filterable return value. Still, filter that at your own risk.

props Faison for the initial patch.
see 35658.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

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

    r37985 r37991  
    53575357        return preg_replace( '/(?:Z|[+-]\d{2}(?::\d{2})?)$/', '', $formatted );
    53585358}
     5359
     5360/**
     5361 * Check if an object type exists. By default, these are `post`, `comment`, `user`, and `term`.
     5362 *
     5363 * @param  string $object_type Object type to check.
     5364 * @return bool                True if the object type exists, false if not.
     5365 */
     5366function wp_object_type_exists( $object_type ) {
     5367        /**
     5368         * Filters WordPress object types.
     5369         *
     5370         * @since 4.6.0
     5371         *
     5372         * @param array  $types Array of object types.
     5373         */
     5374        $types = apply_filters( 'wp_object_types', array( 'post', 'comment', 'user', 'term' ) );
     5375
     5376        if ( in_array( $object_type, $types ) ) {
     5377                return true;
     5378        }
     5379
     5380        return false;
     5381}
  • trunk/src/wp-includes/meta.php

    r37990 r37991  
    10051005 * @param string|array $deprecated Deprecated. Use `$args` instead.
    10061006 *
    1007  * @return bool True if the meta key was successfully registered in the global array, false if not.
    1008  *              Registering a meta key with distinct sanitize and auth callbacks will fire those
    1009  *              callbacks, but will not add to the global registry as it requires a subtype.
     1007 * @return bool|WP_error True if the meta key was successfully registered in the global array, WP_Error if not.
     1008 *                       Registering a meta key with distinct sanitize and auth callbacks will fire those
     1009 *                       callbacks, but will not add to the global registry as it requires a subtype.
    10101010 */
    10111011function register_meta( $object_type, $meta_key, $args, $deprecated = null ) {
     
    10161016        }
    10171017
    1018         /* translators: object type name */
    1019         if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) {
    1020                 _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid object type: %s.' ), $object_type ), '4.6.0' );
     1018        if ( ! wp_object_type_exists( $object_type ) ) {
     1019                return new WP_Error( 'register_meta_failed', __( 'Meta can only be registered against a core object type.' ) );
    10211020        }
    10221021
     
    10641063        // Object subtype is required if using the args style of registration
    10651064        if ( ! $has_old_sanitize_cb && empty( $args['object_subtype'] ) ) {
    1066                 return false;
     1065                return new WP_Error( 'register_meta_failed', __( 'Meta must be registered against an object subtype.' ) );
    10671066        }
    10681067
     
    11031102        }
    11041103
    1105         return false;
     1104        return new WP_Error( 'register_meta_failed', __( 'Sanitize and auth callbacks registered; meta key not registered.' ) );
    11061105}
    11071106
     
    11151114 * @param string $meta_key       The meta key.
    11161115 *
    1117  * @return bool True if the meta key is registered to the object type and subtype. False if not.
     1116 * @return bool|WP_error True if the meta key is registered to the object type and subtype. False if not.
     1117 *                       WP_Error if an invalid object type is passed.
    11181118 */
    11191119function registered_meta_key_exists( $object_type, $object_subtype, $meta_key ) {
     
    11251125
    11261126        // Only specific core object types are supported.
    1127         if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) {
    1128                 return false;
     1127        if ( ! wp_object_type_exists( $object_type ) ) {
     1128                return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) );
    11291129        }
    11301130
     
    12241224        }
    12251225
    1226         if ( ! in_array( $object_type, array( 'post', 'comment', 'user', 'term' ) ) ) {
     1226        if ( ! wp_object_type_exists( $object_type ) ) {
    12271227                return new WP_Error( 'invalid_meta_key', __( 'Invalid meta key. Not a core object type.' ) );
    12281228        }
Note: See TracChangeset for help on using the changeset viewer.