Make WordPress Core

Changeset 46160


Ignore:
Timestamp:
09/17/2019 07:57:18 PM (5 years ago)
Author:
desrosj
Message:

Posts, Post Types: Allow support arguments to be specified when registering post types.

The add_post_type_support() function accepts an optional third parameter that allows extra arguments to be supplied to configure post type support for a given feature. However, because of how register_post_type() and WP_Post_Type->add_supports() work, it is currently impossible to pass these additional arguments when initially registering a post type with register_post_type().

This change makes it possible to supply additional arguments for a feature using the supports argument of register_post_type().

Props MaximeCulea, seuser, desrosj, johnbillion.
Fixes #40413.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-post-type.php

    r45735 r46160  
    514514    public function add_supports() {
    515515        if ( ! empty( $this->supports ) ) {
    516             add_post_type_support( $this->name, $this->supports );
     516            foreach ( $this->supports as $feature => $args ) {
     517                if ( is_array( $args ) ) {
     518                    add_post_type_support( $this->name, $feature, $args );
     519                } else {
     520                    add_post_type_support( $this->name, $args );
     521                }
     522            }
    517523            unset( $this->supports );
    518524        } elseif ( false !== $this->supports ) {
  • trunk/tests/phpunit/tests/post/wpPostType.php

    r45607 r46160  
    6363    }
    6464
     65    /**
     66     * Test that supports can optionally receive nested args.
     67     *
     68     * @ticket 40413
     69     */
     70    public function test_add_supports_custom_with_args() {
     71        $post_type        = 'cpt';
     72        $post_type_object = new WP_Post_Type(
     73            $post_type,
     74            array(
     75                'supports' => array(
     76                    'support_with_args' => array(
     77                        'arg1',
     78                        'arg2',
     79                    ),
     80                    'support_without_args',
     81                ),
     82            )
     83        );
     84
     85        $post_type_object->add_supports();
     86        $post_type_supports = get_all_post_type_supports( $post_type );
     87
     88        $post_type_object->remove_supports();
     89        $post_type_supports_after = get_all_post_type_supports( $post_type );
     90
     91        $this->assertEqualSets(
     92            array(
     93                'support_with_args'    => array(
     94                    array(
     95                        'arg1',
     96                        'arg2',
     97                    ),
     98                ),
     99                'support_without_args' => true,
     100            ),
     101            $post_type_supports
     102        );
     103        $this->assertEqualSets( array(), $post_type_supports_after );
     104    }
     105
    65106    public function test_does_not_add_query_var_if_not_public() {
    66107        $this->set_permalink_structure( '/%postname%' );
Note: See TracChangeset for help on using the changeset viewer.