Make WordPress Core

Changeset 38095


Ignore:
Timestamp:
07/18/2016 09:15:37 PM (8 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.

Location:
trunk
Files:
3 edited

Legend:

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

    r38038 r38095  
    53595359
    53605360/**
    5361  * Check if an object type exists. By default, these are `post`, `comment`, `user`, and `term`.
    5362  *
    5363  * @since 4.6.0
    5364  *
    5365  * @param  string $object_type Object type to check.
    5366  * @return bool                True if the object type exists, false if not.
    5367  */
    5368 function wp_object_type_exists( $object_type ) {
    5369     /**
    5370      * Filters WordPress object types.
    5371      *
    5372      * @since 4.6.0
    5373      *
    5374      * @param array  $types Array of object types.
    5375      */
    5376     $types = apply_filters( 'wp_object_types', array( 'post', 'comment', 'user', 'term' ) );
    5377 
    5378     if ( in_array( $object_type, $types ) ) {
    5379         return true;
    5380     }
    5381 
    5382     return false;
    5383 }
    5384 
    5385 /**
    53865361 * Attempts to raise the PHP memory limit for memory intensive processes.
    53875362 *
  • 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
  • trunk/tests/phpunit/tests/meta/registerMeta.php

    r38041 r38095  
    44 */
    55class Tests_Meta_Register_Meta extends WP_UnitTestCase {
    6     protected static $editor_id;
    76    protected static $post_id;
    8     protected static $comment_id;
    97
    108    public static function wpSetUpBeforeClass( $factory ) {
    11         self::$editor_id = $factory->user->create( array( 'role' => 'editor' ) );
    129        self::$post_id = $factory->post->create();
    13         self::$comment_id = $factory->comment->create( array( 'comment_post_ID' => self::$post_id ) );
    1410    }
    1511
    1612    public static function wpTearDownAfterClass() {
    17         self::delete_user( self::$editor_id );
    18         wp_delete_comment( self::$comment_id, true );
    1913        wp_delete_post( self::$post_id, true );
    20     }
    21 
    22     function setUp() {
    23         parent::setUp();
    24         wp_set_current_user( self::$editor_id );
    2514    }
    2615
     
    2918    }
    3019
    31     public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type, $object_subtype ) {
     20    public function _new_sanitize_meta_cb( $meta_value, $meta_key, $object_type ) {
    3221        return $meta_key . ' new sanitized';
    3322    }
     
    6958    }
    7059
    71     public function test_register_meta_with_valid_object_type_and_object_subtype_returns_true() {
    72         $result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
    73         unregister_meta_key( 'post', 'post', 'flight_number' );
     60    public function test_register_meta_with_post_object_type_returns_true() {
     61        $result = register_meta( 'post', 'flight_number', array() );
     62        unregister_meta_key( 'post', 'flight_number' );
    7463
    7564        $this->assertTrue( $result );
    7665    }
    7766
    78     public function test_register_meta_with_post_object_type_and_subtype_populates_wp_meta_keys() {
    79         global $wp_meta_keys;
    80 
    81         register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
     67    public function test_register_meta_with_post_object_type_populates_wp_meta_keys() {
     68        global $wp_meta_keys;
     69
     70        register_meta( 'post', 'flight_number', array() );
    8271        $actual = $wp_meta_keys;
    83         unregister_meta_key( 'post', 'post', 'flight_number' );
     72        unregister_meta_key( 'post', 'flight_number' );
    8473
    8574        $expected = array(
    8675            'post' => array(
    87                 'post' => array(
    88                     'flight_number' => array(
    89                         'object_subtype' => 'post',
    90                         'type' => 'string',
    91                         'description' => '',
    92                         'single' => false,
    93                         'sanitize_callback' => null,
    94                         'auth_callback' => '__return_true',
    95                         'show_in_rest' => false,
    96                     ),
     76                'flight_number' => array(
     77                    'type' => 'string',
     78                    'description' => '',
     79                    'single' => false,
     80                    'sanitize_callback' => null,
     81                    'auth_callback' => '__return_true',
     82                    'show_in_rest' => false,
    9783                ),
    9884            ),
     
    10288    }
    10389
    104     public function test_register_meta_with_post_object_type_and_unregistered_subtype_populates_wp_meta_keys() {
    105         global $wp_meta_keys;
    106 
    107         register_meta( 'post', 'flight_number', array( 'object_subtype' => 'not_a_post_type' ) );
     90    public function test_register_meta_with_term_object_type_populates_wp_meta_keys() {
     91        global $wp_meta_keys;
     92        register_meta( 'term', 'category_icon', array() );
    10893        $actual = $wp_meta_keys;
    109         unregister_meta_key( 'post', 'not_a_post_type', 'flight_number' );
    110 
    111         $expected = array(
    112             'post' => array(
    113                 'not_a_post_type' => array(
    114                     'flight_number' => array(
    115                         'object_subtype' => 'not_a_post_type',
    116                         'type' => 'string',
    117                         'description' => '',
    118                         'single' => false,
    119                         'sanitize_callback' => null,
    120                         'auth_callback' => '__return_true',
    121                         'show_in_rest' => false,
    122                     ),
    123                 ),
    124             ),
    125         );
    126 
    127         $this->assertEquals( $actual, $expected );
    128     }
    129 
    130     public function test_register_meta_with_term_object_type_and_category_subtype_populates_wp_meta_keys() {
    131         global $wp_meta_keys;
    132         register_meta( 'term', 'category_icon', array( 'object_subtype' => 'category' ) );
    133         $actual = $wp_meta_keys;
    134         unregister_meta_key( 'term', 'category', 'category_icon' );
     94        unregister_meta_key( 'term', 'category_icon' );
    13595
    13696        $expected = array(
    13797            'term' => array(
    138                 'category' => array(
    139                     'category_icon' => array(
    140                         'object_subtype' => 'category',
    141                         'type' => 'string',
    142                         'description' => '',
    143                         'single' => false,
    144                         'sanitize_callback' => null,
    145                         'auth_callback' => '__return_true',
    146                         'show_in_rest' => false,
    147                     ),
    148                 ),
    149             ),
    150         );
    151 
    152         $this->assertEquals( $actual, $expected );
    153     }
    154 
    155     public function test_register_meta_with_comment_object_type_and_subtype_populates_wp_meta_keys() {
    156         global $wp_meta_keys;
    157         register_meta( 'comment', 'comment_rating', array( 'object_subtype' => 'comment' ) );
    158         $actual = $wp_meta_keys;
    159         unregister_meta_key( 'comment', 'comment', 'comment_rating' );
    160 
    161         $expected = array(
    162             'comment' => array(
    163                 'comment' => array(
    164                     'comment_rating' => array(
    165                         'object_subtype' => 'comment',
    166                         'type' => 'string',
    167                         'description' => '',
    168                         'single' => false,
    169                         'sanitize_callback' => null,
    170                         'auth_callback' => '__return_true',
    171                         'show_in_rest' => false,
    172                     ),
     98                'category_icon' => array(
     99                    'type' => 'string',
     100                    'description' => '',
     101                    'single' => false,
     102                    'sanitize_callback' => null,
     103                    'auth_callback' => '__return_true',
     104                    'show_in_rest' => false,
    173105                ),
    174106            ),
     
    189121    }
    190122
    191     public function test_register_meta_with_deprecated_sanitize_callback_param_returns_wp_error() {
     123    public function test_register_meta_with_deprecated_sanitize_callback_param_returns_false() {
    192124        $actual = register_meta( 'post', 'flight_number', array( $this, '_old_sanitize_meta_cb' ) );
    193125
     
    195127        remove_filter( 'auth_post_meta_flight_number', '__return_true');
    196128
    197         $this->assertWPError( $actual );
    198         $this->assertEquals( 'register_meta_failed', $actual->get_error_code() );
     129        $this->assertFalse( $actual );
    199130    }
    200131
     
    211142    public function test_register_meta_with_current_sanitize_callback_populates_wp_meta_keys() {
    212143        global $wp_meta_keys;
    213         register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
     144        register_meta( 'post', 'flight_number', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
    214145        $actual = $wp_meta_keys;
    215         unregister_meta_key( 'post', 'post', 'flight_number' );
     146        unregister_meta_key( 'post', 'flight_number' );
    216147
    217148        $expected = array(
    218149            'post' => array(
    219                 'post' => array(
    220                     'flight_number' => array(
    221                         'object_subtype' => 'post',
    222                         'type' => 'string',
    223                         'description' => '',
    224                         'single' => false,
    225                         'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ),
    226                         'auth_callback' => '__return_true',
    227                         'show_in_rest' => false,
    228                     ),
     150                'flight_number' => array(
     151                    'type' => 'string',
     152                    'description' => '',
     153                    'single' => false,
     154                    'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ),
     155                    'auth_callback' => '__return_true',
     156                    'show_in_rest' => false,
    229157                ),
    230158            ),
     
    234162
    235163    public function test_register_meta_with_current_sanitize_callback_returns_true() {
    236         $result = register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
    237         unregister_meta_key( 'post', 'post', 'flight_number' );
     164        $result = register_meta( 'post', 'flight_number', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
     165        unregister_meta_key( 'post', 'flight_number' );
    238166
    239167        $this->assertTrue( $result );
     
    241169
    242170    public function test_register_meta_with_new_sanitize_callback_parameter() {
    243         register_meta( 'post', 'new_sanitized_key', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
    244         $meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post', 'post' );
    245 
    246         unregister_meta_key( 'post', 'post', 'new_sanitized_key' );
     171        register_meta( 'post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
     172        $meta = sanitize_meta( 'new_sanitized_key', 'unsanitized', 'post' );
     173
     174        unregister_meta_key( 'post', 'new_sanitized_key' );
    247175
    248176        $this->assertEquals( 'new_sanitized_key new sanitized', $meta );
     
    250178
    251179    public function test_register_meta_unregistered_meta_key_removes_sanitize_filter() {
    252         register_meta( 'post', 'new_sanitized_key', array( 'object_subtype' => 'post', 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
    253         unregister_meta_key( 'post', 'post', 'new_sanitized_key' );
    254 
    255         $has_filter = has_filter( 'sanitize_post_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) );
     180        register_meta( 'post', 'new_sanitized_key', array( 'sanitize_callback' => array( $this, '_new_sanitize_meta_cb' ) ) );
     181        unregister_meta_key( 'post', 'new_sanitized_key' );
     182
     183        $has_filter = has_filter( 'sanitize_post_meta_new_sanitized_key', array( $this, '_new_sanitize_meta_cb' ) );
    256184
    257185        $this->assertFalse( $has_filter );
     
    259187
    260188    public function test_register_meta_unregistered_meta_key_removes_auth_filter() {
    261         register_meta( 'post', 'new_auth_key', array( 'object_subtype' => 'post', 'auth_callback' => array( $this, '_new_auth_meta_cb' ) ) );
    262         unregister_meta_key( 'post', 'post', 'new_auth_key' );
    263 
    264         $has_filter = has_filter( 'auth_post_post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) );
     189        register_meta( 'post', 'new_auth_key', array( 'auth_callback' => array( $this, '_new_auth_meta_cb' ) ) );
     190        unregister_meta_key( 'post', 'new_auth_key' );
     191
     192        $has_filter = has_filter( 'auth_post_meta_new_auth_key', array( $this, '_new_auth_meta_cb' ) );
    265193
    266194        $this->assertFalse( $has_filter );
     
    269197    public function test_unregister_meta_key_clears_key_from_wp_meta_keys() {
    270198        global $wp_meta_keys;
    271         register_meta( 'post', 'registered_key', array( 'object_subtype' => 'post' ) );
    272         unregister_meta_key( 'post', 'post', 'registered_key' );
     199        register_meta( 'post', 'registered_key', array() );
     200        unregister_meta_key( 'post', 'registered_key' );
    273201
    274202        $this->assertEquals( array(), $wp_meta_keys );
    275203    }
    276204
    277     public function test_unregister_meta_key_with_invalid_key_returns_wp_error() {
    278         $this->assertWPError( unregister_meta_key( 'post', 'post', 'not_a_registered_key' ) );
     205    public function test_unregister_meta_key_with_invalid_key_returns_false() {
     206        $this->assertFalse( unregister_meta_key( 'post', 'not_a_registered_key' ) );
    279207    }
    280208
    281209    public function test_get_registered_meta_keys() {
    282         register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
    283         register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
    284 
    285         $meta_keys = get_registered_meta_keys( 'post', 'post' );
    286 
    287         unregister_meta_key( 'post', 'post', 'registered_key1' );
    288         unregister_meta_key( 'post', 'post', 'registered_key2' );
     210        register_meta( 'post', 'registered_key1', array() );
     211        register_meta( 'post', 'registered_key2', array() );
     212
     213        $meta_keys = get_registered_meta_keys( 'post' );
     214
     215        unregister_meta_key( 'post', 'registered_key1' );
     216        unregister_meta_key( 'post', 'registered_key2' );
    289217
    290218        $this->assertArrayHasKey( 'registered_key1', $meta_keys );
     
    292220    }
    293221
    294     public function test_get_registered_meta_keys_with_subtype_without_registered_keys_is_empty() {
    295         register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
    296         register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
    297 
    298         $meta_keys = get_registered_meta_keys( 'post', 'page' );
    299 
    300         unregister_meta_key( 'post', 'post', 'registered_key1' );
    301         unregister_meta_key( 'post', 'post', 'registered_key2' );
     222    public function test_get_registered_meta_keys_with_invalid_type_is_empty() {
     223        register_meta( 'post', 'registered_key1', array() );
     224        register_meta( 'post', 'registered_key2', array() );
     225
     226        $meta_keys = get_registered_meta_keys( 'invalid-type' );
     227
     228        unregister_meta_key( 'post', 'registered_key1' );
     229        unregister_meta_key( 'post', 'registered_key2' );
    302230
    303231        $this->assertEmpty( $meta_keys );
    304232    }
    305233
    306     public function test_get_registered_meta_keys_with_invalid_type_is_empty() {
    307         register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
    308         register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'post' ) );
    309 
    310         $meta_keys = get_registered_meta_keys( 'invalid-type' );
    311 
    312         unregister_meta_key( 'post', 'post', 'registered_key1' );
    313         unregister_meta_key( 'post', 'post', 'registered_key2' );
    314 
    315         $this->assertEmpty( $meta_keys );
    316     }
    317 
    318     public function test_get_registered_meta_keys_has_count() {
    319         register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post' ) );
    320         register_meta( 'post', 'registered_key2', array( 'object_subtype' => 'page' ) );
     234    public function test_get_registered_meta_keys_description_arg() {
     235        register_meta( 'post', 'registered_key1', array( 'description' => 'I\'m just a field, take a good look at me' ) );
    321236
    322237        $meta_keys = get_registered_meta_keys( 'post' );
    323238
    324         unregister_meta_key( 'post', 'post', 'registered_key1' );
    325         unregister_meta_key( 'post', 'page', 'registered_key2' );
    326 
    327         $this->assertCount( 2, $meta_keys );
    328     }
    329 
    330     public function test_get_registered_meta_keys_description_arg() {
    331         register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'description' => 'I\'m just a field, take a good look at me' ) );
    332 
    333         $meta_keys = get_registered_meta_keys( 'post', 'post' );
    334 
    335         unregister_meta_key( 'post', 'post', 'registered_key1' );
     239        unregister_meta_key( 'post', 'registered_key1' );
    336240
    337241        $this->assertEquals( 'I\'m just a field, take a good look at me', $meta_keys['registered_key1']['description'] );
     
    339243
    340244    public function test_get_registered_meta_keys_invalid_arg() {
    341         register_meta( 'post', 'registered_key1', array( 'object_subtype' => 'post', 'invalid_arg' => 'invalid' ) );
    342 
    343         $meta_keys = get_registered_meta_keys( 'post', 'post' );
    344 
    345         unregister_meta_key( 'post', 'post', 'registered_key1' );
     245        register_meta( 'post', 'registered_key1', array( 'invalid_arg' => 'invalid' ) );
     246
     247        $meta_keys = get_registered_meta_keys( 'post' );
     248
     249        unregister_meta_key( 'post', 'registered_key1' );
    346250
    347251        $this->assertArrayNotHasKey( 'invalid_arg', $meta_keys['registered_key1'] );
     
    349253
    350254    public function test_get_registered_metadata() {
    351         register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
     255        register_meta( 'post', 'flight_number', array() );
    352256        add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
    353257
    354         $meta = get_registered_metadata( 'post', 'post', self::$post_id );
    355 
    356         unregister_meta_key( 'post', 'post', 'flight_number' );
     258        $meta = get_registered_metadata( 'post', self::$post_id );
     259
     260        unregister_meta_key( 'post', 'flight_number' );
    357261
    358262        $this->assertEquals( 'Oceanic 815', $meta['flight_number'][0] );
     
    360264
    361265    public function test_get_registered_metadata_by_key() {
    362         register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
     266        register_meta( 'post', 'flight_number', array() );
    363267        add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
    364268
    365         $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_number' );
    366 
    367         unregister_meta_key( 'post', 'post', 'flight_number' );
     269        $meta = get_registered_metadata( 'post', self::$post_id, 'flight_number' );
     270
     271        unregister_meta_key( 'post', 'flight_number' );
    368272
    369273        $this->assertEquals( 'Oceanic 815', $meta[0] );
     
    371275
    372276    public function test_get_registered_metadata_by_key_single() {
    373         register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post', 'single' => true ) );
     277        register_meta( 'post', 'flight_number', array( 'single' => true ) );
    374278        add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
    375279
    376         $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_number' );
    377 
    378         unregister_meta_key( 'post', 'post', 'flight_number' );
     280        $meta = get_registered_metadata( 'post', self::$post_id, 'flight_number' );
     281
     282        unregister_meta_key( 'post', 'flight_number' );
    379283
    380284        $this->assertEquals( 'Oceanic 815', $meta );
     
    382286
    383287    public function test_get_registered_metadata_by_invalid_key() {
    384         register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
     288        register_meta( 'post', 'flight_number', array() );
    385289        add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
    386290
    387         $meta = get_registered_metadata( 'post', 'post', self::$post_id, 'flight_pilot' );
    388 
    389         unregister_meta_key( 'post', 'post', 'flight_number' );
    390 
    391         $this->assertWPError( $meta );
    392     }
    393 
    394     public function test_get_registered_metadata_invalid_object_type() {
    395         register_meta( 'post', 'flight_number', array( 'object_subtype' => 'post' ) );
    396         add_post_meta( self::$post_id, 'flight_number', 'Oceanic 815' );
    397 
    398         $meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id );
    399 
    400         unregister_meta_key( 'post', 'post', 'flight_number' );
    401 
    402         $this->assertWPError( $meta );
    403     }
    404 
    405     public function test_get_registered_metadata_invalid() {
    406         $meta = get_registered_metadata( 'invalid-type', 'invalid-subtype', self::$post_id );
    407 
    408         $this->assertWPError( $meta );
     291        $meta = get_registered_metadata( 'post', self::$post_id, 'flight_pilot' );
     292
     293        unregister_meta_key( 'post', 'flight_number' );
     294
     295        $this->assertFalse( $meta );
     296    }
     297
     298    public function test_get_registered_metadata_invalid_object_type_returns_empty_array() {
     299        $meta = get_registered_metadata( 'invalid-type', self::$post_id );
     300
     301        $this->assertEmpty( $meta );
    409302    }
    410303}
Note: See TracChangeset for help on using the changeset viewer.