Make WordPress Core


Ignore:
Timestamp:
06/21/2018 09:06:50 PM (7 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/src/wp-includes/rest-api/fields/class-wp-rest-meta-fields.php

    r42343 r43378  
    2424     */
    2525    abstract protected function get_meta_type();
     26
     27    /**
     28     * Retrieves the object meta subtype.
     29     *
     30     * @since 5.0.0
     31     *
     32     * @return string Subtype for the meta type, or empty string if no specific subtype.
     33     */
     34    protected function get_meta_subtype() {
     35        return '';
     36    }
    2637
    2738    /**
     
    341352        $registered = array();
    342353
    343         foreach ( get_registered_meta_keys( $this->get_meta_type() ) as $name => $args ) {
     354        $meta_type    = $this->get_meta_type();
     355        $meta_subtype = $this->get_meta_subtype();
     356
     357        $meta_keys = get_registered_meta_keys( $meta_type );
     358        if ( ! empty( $meta_subtype ) ) {
     359            $meta_keys = array_merge( $meta_keys, get_registered_meta_keys( $meta_type, $meta_subtype ) );
     360        }
     361
     362        foreach ( $meta_keys as $name => $args ) {
    344363            if ( empty( $args['show_in_rest'] ) ) {
    345364                continue;
Note: See TracChangeset for help on using the changeset viewer.