Posts, Post Types/Taxonomies: Add object type specific registration filters.

Add post type and taxonomy specific registration argument hooks. 

Introduces the filters `register_{$post_type}_post_type_args` and `register_{$taxonomy}_taxonomy_args`. Introduces the actions `registered_post_type_{$post_type}` and `registered_taxonomy_{$taxonomy}`.

Props pbiron, dlh, davidbaumwald, hellofromTonya, milana_cap.
Fixes #53212.



git-svn-id: https://develop.svn.wordpress.org/trunk@53126 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2022-04-11 05:09:53 +00:00
parent 3c804bbead
commit affb0bcb0c
8 changed files with 116 additions and 0 deletions
+15
View File
@@ -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.
+16
View File
@@ -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' => '',
+12
View File
@@ -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;
}
+13
View File
@@ -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;
}
+17
View File
@@ -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;
+13
View File
@@ -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() );
}
}
+17
View File
@@ -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
*/
+13
View File
@@ -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() );
}
}