From 65ec280a54699c6d7ab4f41c806a01d4169fbded Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Tue, 17 Sep 2019 19:57:18 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@46160 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-post-type.php | 8 ++++- tests/phpunit/tests/post/wpPostType.php | 41 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-post-type.php b/src/wp-includes/class-wp-post-type.php index f2e14a0a93..faf7a0d496 100644 --- a/src/wp-includes/class-wp-post-type.php +++ b/src/wp-includes/class-wp-post-type.php @@ -513,7 +513,13 @@ final class WP_Post_Type { */ public function add_supports() { if ( ! empty( $this->supports ) ) { - add_post_type_support( $this->name, $this->supports ); + foreach ( $this->supports as $feature => $args ) { + if ( is_array( $args ) ) { + add_post_type_support( $this->name, $feature, $args ); + } else { + add_post_type_support( $this->name, $args ); + } + } unset( $this->supports ); } elseif ( false !== $this->supports ) { // Add default features. diff --git a/tests/phpunit/tests/post/wpPostType.php b/tests/phpunit/tests/post/wpPostType.php index 793e61cae9..041d1320c2 100644 --- a/tests/phpunit/tests/post/wpPostType.php +++ b/tests/phpunit/tests/post/wpPostType.php @@ -62,6 +62,47 @@ class Tests_WP_Post_Type extends WP_UnitTestCase { $this->assertEqualSets( array(), $post_type_supports_after ); } + /** + * Test that supports can optionally receive nested args. + * + * @ticket 40413 + */ + public function test_add_supports_custom_with_args() { + $post_type = 'cpt'; + $post_type_object = new WP_Post_Type( + $post_type, + array( + 'supports' => array( + 'support_with_args' => array( + 'arg1', + 'arg2', + ), + 'support_without_args', + ), + ) + ); + + $post_type_object->add_supports(); + $post_type_supports = get_all_post_type_supports( $post_type ); + + $post_type_object->remove_supports(); + $post_type_supports_after = get_all_post_type_supports( $post_type ); + + $this->assertEqualSets( + array( + 'support_with_args' => array( + array( + 'arg1', + 'arg2', + ), + ), + 'support_without_args' => true, + ), + $post_type_supports + ); + $this->assertEqualSets( array(), $post_type_supports_after ); + } + public function test_does_not_add_query_var_if_not_public() { $this->set_permalink_structure( '/%postname%' );