Make WordPress Core


Ignore:
Timestamp:
07/19/2018 06:48:52 PM (6 years ago)
Author:
kadamwhite
Message:

REST API: Support meta registration for specific object subtypes.

Introduce an object_subtype argument to the args array for register_meta() which can be used to limit meta registration to a single subtype (e.g. a custom post type or taxonomy, vs all posts or taxonomies).

Introduce register_post_meta() and register_term_meta() wrapper methods for register_meta to provide a convenient interface for the common case of registering meta for a specific taxonomy or post type. These methods work the way plugin developers have often expected register_meta to function, and should be used in place of direct register_meta where possible.

Props flixos90, tharsheblows, spacedmonkey.

Merges [43378] to the 4.9 branch.
Fixes #38323.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9/tests/phpunit/tests/post/meta.php

    r35244 r43510  
    66 */
    77class Tests_Post_Meta extends WP_UnitTestCase {
     8
     9    private $last_register_meta_call = array(
     10        'object_type' => '',
     11        'meta_key'    => '',
     12        'args'        => array(),
     13    );
     14
    815    function setUp() {
    916        parent::setUp();
     
    238245
    239246    }
     247
     248    /**
     249     * @ticket 38323
     250     * @dataProvider data_register_post_meta
     251     */
     252    public function test_register_post_meta( $post_type, $meta_key, $args ) {
     253        add_filter( 'register_meta_args', array( $this, 'filter_register_meta_args_set_last_register_meta_call' ), 10, 4 );
     254
     255        register_post_meta( $post_type, $meta_key, $args );
     256
     257        $args['object_subtype'] = $post_type;
     258
     259        // Reset global so subsequent data tests do not get polluted.
     260        $GLOBALS['wp_meta_keys'] = array();
     261
     262        $this->assertEquals( 'post', $this->last_register_meta_call['object_type'] );
     263        $this->assertEquals( $meta_key, $this->last_register_meta_call['meta_key'] );
     264        $this->assertEquals( $args, $this->last_register_meta_call['args'] );
     265    }
     266
     267    public function data_register_post_meta() {
     268        return array(
     269            array( 'post', 'registered_key1', array( 'single' => true ) ),
     270            array( 'page', 'registered_key2', array() ),
     271            array( '', 'registered_key3', array( 'sanitize_callback' => 'absint' ) ),
     272        );
     273    }
     274
     275    public function filter_register_meta_args_set_last_register_meta_call( $args, $defaults, $object_type, $meta_key ) {
     276        $this->last_register_meta_call['object_type'] = $object_type;
     277        $this->last_register_meta_call['meta_key']    = $meta_key;
     278        $this->last_register_meta_call['args']        = $args;
     279
     280        return $args;
     281    }
     282
     283    /**
     284     * @ticket 38323
     285     * @dataProvider data_unregister_post_meta
     286     */
     287    public function test_unregister_post_meta( $post_type, $meta_key ) {
     288        global $wp_meta_keys;
     289
     290        register_post_meta( $post_type, $meta_key, array() );
     291        unregister_post_meta( $post_type, $meta_key );
     292
     293        $actual = $wp_meta_keys;
     294
     295        // Reset global so subsequent data tests do not get polluted.
     296        $wp_meta_keys = array();
     297
     298        $this->assertEmpty( $actual );
     299    }
     300
     301    public function data_unregister_post_meta() {
     302        return array(
     303            array( 'post', 'registered_key1' ),
     304            array( 'page', 'registered_key2' ),
     305            array( '', 'registered_key3' ),
     306        );
     307    }
    240308}
Note: See TracChangeset for help on using the changeset viewer.