Make WordPress Core


Ignore:
Timestamp:
07/19/2018 06:48:52 PM (8 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/term/meta.php

    r41662 r43510  
    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();
    … …  
    442449                $q->set( 'cache_results', true );
    443450        }
     451
     452        /**
     453         * @ticket 38323
     454         * @dataProvider data_register_term_meta
     455         */
     456        public function test_register_term_meta( $taxonomy, $meta_key, $args ) {
     457                add_filter( 'register_meta_args', array( $this, 'filter_register_meta_args_set_last_register_meta_call' ), 10, 4 );
     458
     459                register_term_meta( $taxonomy, $meta_key, $args );
     460
     461                $args['object_subtype'] = $taxonomy;
     462
     463                // Reset global so subsequent data tests do not get polluted.
     464                $GLOBALS['wp_meta_keys'] = array();
     465
     466                $this->assertEquals( 'term', $this->last_register_meta_call['object_type'] );
     467                $this->assertEquals( $meta_key, $this->last_register_meta_call['meta_key'] );
     468                $this->assertEquals( $args, $this->last_register_meta_call['args'] );
     469        }
     470
     471        public function data_register_term_meta() {
     472                return array(
     473                        array( 'wptests_tax', 'registered_key1', array( 'single' => true ) ),
     474                        array( 'category', 'registered_key2', array() ),
     475                        array( '', 'registered_key3', array( 'sanitize_callback' => 'absint' ) ),
     476                );
     477        }
     478
     479        public function filter_register_meta_args_set_last_register_meta_call( $args, $defaults, $object_type, $meta_key ) {
     480                $this->last_register_meta_call['object_type'] = $object_type;
     481                $this->last_register_meta_call['meta_key']    = $meta_key;
     482                $this->last_register_meta_call['args']        = $args;
     483
     484                return $args;
     485        }
     486
     487        /**
     488         * @ticket 38323
     489         * @dataProvider data_unregister_term_meta
     490         */
     491        public function test_unregister_term_meta( $taxonomy, $meta_key ) {
     492                global $wp_meta_keys;
     493
     494                register_term_meta( $taxonomy, $meta_key, array() );
     495                unregister_term_meta( $taxonomy, $meta_key );
     496
     497                $actual = $wp_meta_keys;
     498
     499                // Reset global so subsequent data tests do not get polluted.
     500                $wp_meta_keys = array();
     501
     502                $this->assertEmpty( $actual );
     503        }
     504
     505        public function data_unregister_term_meta() {
     506                return array(
     507                        array( 'wptests_tax', 'registered_key1' ),
     508                        array( 'category', 'registered_key2' ),
     509                        array( '', 'registered_key3' ),
     510                );
     511        }
    444512}
Note: See TracChangeset for help on using the changeset viewer.