diff --git a/src/wp-includes/class-wp-post-type.php b/src/wp-includes/class-wp-post-type.php index b5e4caa14c..777ef58dce 100644 --- a/src/wp-includes/class-wp-post-type.php +++ b/src/wp-includes/class-wp-post-type.php @@ -440,6 +440,21 @@ final class WP_Post_Type { */ $args = apply_filters( 'register_post_type_args', $args, $this->name ); + $post_type = $this->name; + + /** + * Filters the arguments for registering a specific post type. + * + * The dynamic portion of the filter name, `$this->name`, refers to the post type key. + * + * @since 6.0.0 + * + * @param array $args Array of arguments for registering a post type. + * See the register_post_type() function for accepted arguments. + * @param string $post_type Post type key. + */ + $args = apply_filters( "register_{$post_type}_post_type_args", $args, $this->name ); + $has_edit_link = ! empty( $args['_edit_link'] ); // Args prefixed with an underscore are reserved for internal use. diff --git a/src/wp-includes/class-wp-taxonomy.php b/src/wp-includes/class-wp-taxonomy.php index 27b04d952b..72f039adcd 100644 --- a/src/wp-includes/class-wp-taxonomy.php +++ b/src/wp-includes/class-wp-taxonomy.php @@ -315,6 +315,22 @@ final class WP_Taxonomy { */ $args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type ); + $taxonomy = $this->name; + + /** + * Filters the arguments for registering a specific taxonomy. + * + * The dynamic portion of the filter name, `$this->name`, refers to the taxonomy key. + * + * @since 6.0.0 + * + * @param array $args Array of arguments for registering a taxonomy. + * See the register_taxonomy() function for accepted arguments. + * @param string $taxonomy Taxonomy key. + * @param string[] $object_type Array of names of object types for the taxonomy. + */ + $args = apply_filters( "register_{$taxonomy}_taxonomy_args", $args, $this->name, (array) $object_type ); + $defaults = array( 'labels' => array(), 'description' => '', diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index f0d5750494..0793de3760 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -1709,6 +1709,18 @@ function register_post_type( $post_type, $args = array() ) { */ do_action( 'registered_post_type', $post_type, $post_type_object ); + /** + * Fires after a specific post type is registered. + * + * The dynamic portion of the filter name, `$post_type`, refers to the post type key. + * + * @since 6.0.0 + * + * @param string $post_type Post type. + * @param WP_Post_Type $post_type_object Arguments used to register the post type. + */ + do_action( "registered_post_type_{$post_type}", $post_type, $post_type_object ); + return $post_type_object; } diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 2f3298e142..fa6c2c641a 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -529,6 +529,19 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { */ do_action( 'registered_taxonomy', $taxonomy, $object_type, (array) $taxonomy_object ); + /** + * Fires after a specific taxonomy is registered. + * + * The dynamic portion of the filter name, `$taxonomy`, refers to the taxonomy key. + * + * @since 6.0.0 + * + * @param string $taxonomy Taxonomy slug. + * @param array|string $object_type Object type or array of object types. + * @param array $args Array of taxonomy registration arguments. + */ + do_action( "registered_taxonomy_{$taxonomy}", $taxonomy, $object_type, (array) $taxonomy_object ); + return $taxonomy_object; } diff --git a/tests/phpunit/tests/post/types.php b/tests/phpunit/tests/post/types.php index 7bba3b639b..5ea10cbb2c 100644 --- a/tests/phpunit/tests/post/types.php +++ b/tests/phpunit/tests/post/types.php @@ -163,6 +163,23 @@ class Tests_Post_Types extends WP_UnitTestCase { $this->assertSame( $public, $args->show_in_admin_bar ); } + /** + * @ticket 53212 + * @covers ::register_post_type + */ + public function test_fires_registered_post_type_actions() { + $post_type = 'cpt'; + $action = new MockAction(); + + add_action( 'registered_post_type', array( $action, 'action' ) ); + add_action( "registered_post_type_{$post_type}", array( $action, 'action' ) ); + + register_post_type( $post_type ); + register_post_type( $this->post_type ); + + $this->assertSame( 3, $action->get_call_count() ); + } + public function test_register_taxonomy_for_object_type() { global $wp_taxonomies; diff --git a/tests/phpunit/tests/post/wpPostType.php b/tests/phpunit/tests/post/wpPostType.php index c08af7a142..8443c804ad 100644 --- a/tests/phpunit/tests/post/wpPostType.php +++ b/tests/phpunit/tests/post/wpPostType.php @@ -216,4 +216,17 @@ class Tests_Post_WP_Post_Type extends WP_UnitTestCase { $this->assertSameSets( array( 'post_tag' ), $taxonomies ); $this->assertSameSets( array(), $taxonomies_after ); } + + public function test_applies_registration_args_filters() { + $post_type = 'cpt'; + $action = new MockAction(); + + add_filter( 'register_post_type_args', array( $action, 'filter' ) ); + add_filter( "register_{$post_type}_post_type_args", array( $action, 'filter' ) ); + + new WP_Post_Type( $post_type ); + new WP_Post_Type( 'random' ); + + $this->assertSame( 3, $action->get_call_count() ); + } } diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 122f55b6bc..a1d06c4691 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -221,6 +221,23 @@ class Tests_Taxonomy extends WP_UnitTestCase { $this->assertFalse( $tax_2->show_in_quick_edit ); } + + /** + * @ticket 53212 + */ + public function test_register_taxonomy_fires_registered_actions() { + $taxonomy = 'taxonomy53212'; + $action = new MockAction(); + + add_action( 'registered_taxonomy', array( $action, 'action' ) ); + add_action( "registered_taxonomy_{$taxonomy}", array( $action, 'action' ) ); + + register_taxonomy( $taxonomy, 'post' ); + register_taxonomy( 'random', 'post' ); + + $this->assertSame( 3, $action->get_call_count() ); + } + /** * @ticket 11058 */ diff --git a/tests/phpunit/tests/term/wpTaxonomy.php b/tests/phpunit/tests/term/wpTaxonomy.php index a47edcc9ee..8914228dc4 100644 --- a/tests/phpunit/tests/term/wpTaxonomy.php +++ b/tests/phpunit/tests/term/wpTaxonomy.php @@ -99,4 +99,17 @@ class Tests_WP_Taxonomy extends WP_UnitTestCase { $this->assertFalse( $has_action_after ); } + + public function test_applies_registration_args_filters() { + $taxonomy = 'taxonomy5'; + $action = new MockAction(); + + add_filter( 'register_taxonomy_args', array( $action, 'filter' ) ); + add_filter( "register_{$taxonomy}_taxonomy_args", array( $action, 'filter' ) ); + + new WP_Taxonomy( $taxonomy, 'post' ); + new WP_Taxonomy( 'random', 'post' ); + + $this->assertSame( 3, $action->get_call_count() ); + } }