Make WordPress Core


Ignore:
Timestamp:
06/21/2018 09:06:50 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.
Fixes #38323.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/term/meta.php

    r42343 r43378  
    77 */
    88class Tests_Term_Meta extends WP_UnitTestCase {
     9
     10    private $last_register_meta_call = array(
     11        'object_type' => '',
     12        'meta_key'    => '',
     13        'args'        => array(),
     14    );
     15
    916    public function setUp() {
    1017        parent::setUp();
     
    464471        $q->set( 'cache_results', true );
    465472    }
     473
     474    /**
     475     * @ticket 38323
     476     * @dataProvider data_register_term_meta
     477     */
     478    public function test_register_term_meta( $taxonomy, $meta_key, $args ) {
     479        add_filter( 'register_meta_args', array( $this, 'filter_register_meta_args_set_last_register_meta_call' ), 10, 4 );
     480
     481        register_term_meta( $taxonomy, $meta_key, $args );
     482
     483        $args['object_subtype'] = $taxonomy;
     484
     485        // Reset global so subsequent data tests do not get polluted.
     486        $GLOBALS['wp_meta_keys'] = array();
     487
     488        $this->assertEquals( 'term', $this->last_register_meta_call['object_type'] );
     489        $this->assertEquals( $meta_key, $this->last_register_meta_call['meta_key'] );
     490        $this->assertEquals( $args, $this->last_register_meta_call['args'] );
     491    }
     492
     493    public function data_register_term_meta() {
     494        return array(
     495            array( 'wptests_tax', 'registered_key1', array( 'single' => true ) ),
     496            array( 'category', 'registered_key2', array() ),
     497            array( '', 'registered_key3', array( 'sanitize_callback' => 'absint' ) ),
     498        );
     499    }
     500
     501    public function filter_register_meta_args_set_last_register_meta_call( $args, $defaults, $object_type, $meta_key ) {
     502        $this->last_register_meta_call['object_type'] = $object_type;
     503        $this->last_register_meta_call['meta_key']    = $meta_key;
     504        $this->last_register_meta_call['args']        = $args;
     505
     506        return $args;
     507    }
     508
     509    /**
     510     * @ticket 38323
     511     * @dataProvider data_unregister_term_meta
     512     */
     513    public function test_unregister_term_meta( $taxonomy, $meta_key ) {
     514        global $wp_meta_keys;
     515
     516        register_term_meta( $taxonomy, $meta_key, array() );
     517        unregister_term_meta( $taxonomy, $meta_key );
     518
     519        $actual = $wp_meta_keys;
     520
     521        // Reset global so subsequent data tests do not get polluted.
     522        $wp_meta_keys = array();
     523
     524        $this->assertEmpty( $actual );
     525    }
     526
     527    public function data_unregister_term_meta() {
     528        return array(
     529            array( 'wptests_tax', 'registered_key1' ),
     530            array( 'category', 'registered_key2' ),
     531            array( '', 'registered_key3' ),
     532        );
     533    }
    466534}
Note: See TracChangeset for help on using the changeset viewer.