WordPress.org

Make WordPress Core


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