Make WordPress Core

Changeset 53126


Ignore:
Timestamp:
04/11/2022 05:09:53 AM (3 years ago)
Author:
peterwilsoncc
Message:

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.

Location:
trunk
Files:
8 edited

Legend:

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

    r53058 r53126  
    440440         */
    441441        $args = apply_filters( 'register_post_type_args', $args, $this->name );
     442
     443        $post_type = $this->name;
     444
     445        /**
     446         * Filters the arguments for registering a specific post type.
     447         *
     448         * The dynamic portion of the filter name, `$this->name`, refers to the post type key.
     449         *
     450         * @since 6.0.0
     451         *
     452         * @param array  $args      Array of arguments for registering a post type.
     453         *                          See the register_post_type() function for accepted arguments.
     454         * @param string $post_type Post type key.
     455         */
     456        $args = apply_filters( "register_{$post_type}_post_type_args", $args, $this->name );
    442457
    443458        $has_edit_link = ! empty( $args['_edit_link'] );
  • trunk/src/wp-includes/class-wp-taxonomy.php

    r53058 r53126  
    315315         */
    316316        $args = apply_filters( 'register_taxonomy_args', $args, $this->name, (array) $object_type );
     317
     318        $taxonomy = $this->name;
     319
     320        /**
     321         * Filters the arguments for registering a specific taxonomy.
     322         *
     323         * The dynamic portion of the filter name, `$this->name`, refers to the taxonomy key.
     324         *
     325         * @since 6.0.0
     326         *
     327         * @param array    $args        Array of arguments for registering a taxonomy.
     328         *                              See the register_taxonomy() function for accepted arguments.
     329         * @param string   $taxonomy    Taxonomy key.
     330         * @param string[] $object_type Array of names of object types for the taxonomy.
     331         */
     332        $args = apply_filters( "register_{$taxonomy}_taxonomy_args", $args, $this->name, (array) $object_type );
    317333
    318334        $defaults = array(
  • trunk/src/wp-includes/post.php

    r53060 r53126  
    17101710    do_action( 'registered_post_type', $post_type, $post_type_object );
    17111711
     1712    /**
     1713     * Fires after a specific post type is registered.
     1714     *
     1715     * The dynamic portion of the filter name, `$post_type`, refers to the post type key.
     1716     *
     1717     * @since 6.0.0
     1718     *
     1719     * @param string       $post_type        Post type.
     1720     * @param WP_Post_Type $post_type_object Arguments used to register the post type.
     1721     */
     1722    do_action( "registered_post_type_{$post_type}", $post_type, $post_type_object );
     1723
    17121724    return $post_type_object;
    17131725}
  • trunk/src/wp-includes/taxonomy.php

    r53058 r53126  
    529529     */
    530530    do_action( 'registered_taxonomy', $taxonomy, $object_type, (array) $taxonomy_object );
     531
     532    /**
     533     * Fires after a specific taxonomy is registered.
     534     *
     535     * The dynamic portion of the filter name, `$taxonomy`, refers to the taxonomy key.
     536     *
     537     * @since 6.0.0
     538     *
     539     * @param string       $taxonomy    Taxonomy slug.
     540     * @param array|string $object_type Object type or array of object types.
     541     * @param array        $args        Array of taxonomy registration arguments.
     542     */
     543    do_action( "registered_taxonomy_{$taxonomy}", $taxonomy, $object_type, (array) $taxonomy_object );
    531544
    532545    return $taxonomy_object;
  • trunk/tests/phpunit/tests/post/types.php

    r52389 r53126  
    162162        // Should fall back to 'public'.
    163163        $this->assertSame( $public, $args->show_in_admin_bar );
     164    }
     165
     166    /**
     167     * @ticket 53212
     168     * @covers ::register_post_type
     169     */
     170    public function test_fires_registered_post_type_actions() {
     171        $post_type = 'cpt';
     172        $action    = new MockAction();
     173
     174        add_action( 'registered_post_type', array( $action, 'action' ) );
     175        add_action( "registered_post_type_{$post_type}", array( $action, 'action' ) );
     176
     177        register_post_type( $post_type );
     178        register_post_type( $this->post_type );
     179
     180        $this->assertSame( 3, $action->get_call_count() );
    164181    }
    165182
  • trunk/tests/phpunit/tests/post/wpPostType.php

    r51404 r53126  
    217217        $this->assertSameSets( array(), $taxonomies_after );
    218218    }
     219
     220    public function test_applies_registration_args_filters() {
     221        $post_type = 'cpt';
     222        $action    = new MockAction();
     223
     224        add_filter( 'register_post_type_args', array( $action, 'filter' ) );
     225        add_filter( "register_{$post_type}_post_type_args", array( $action, 'filter' ) );
     226
     227        new WP_Post_Type( $post_type );
     228        new WP_Post_Type( 'random' );
     229
     230        $this->assertSame( 3, $action->get_call_count() );
     231    }
    219232}
  • trunk/tests/phpunit/tests/taxonomy.php

    r52389 r53126  
    222222    }
    223223
     224
     225    /**
     226     * @ticket 53212
     227     */
     228    public function test_register_taxonomy_fires_registered_actions() {
     229        $taxonomy = 'taxonomy53212';
     230        $action   = new MockAction();
     231
     232        add_action( 'registered_taxonomy', array( $action, 'action' ) );
     233        add_action( "registered_taxonomy_{$taxonomy}", array( $action, 'action' ) );
     234
     235        register_taxonomy( $taxonomy, 'post' );
     236        register_taxonomy( 'random', 'post' );
     237
     238        $this->assertSame( 3, $action->get_call_count() );
     239    }
     240
    224241    /**
    225242     * @ticket 11058
  • trunk/tests/phpunit/tests/term/wpTaxonomy.php

    r52389 r53126  
    100100
    101101    }
     102
     103    public function test_applies_registration_args_filters() {
     104        $taxonomy = 'taxonomy5';
     105        $action   = new MockAction();
     106
     107        add_filter( 'register_taxonomy_args', array( $action, 'filter' ) );
     108        add_filter( "register_{$taxonomy}_taxonomy_args", array( $action, 'filter' ) );
     109
     110        new WP_Taxonomy( $taxonomy, 'post' );
     111        new WP_Taxonomy( 'random', 'post' );
     112
     113        $this->assertSame( 3, $action->get_call_count() );
     114    }
    102115}
Note: See TracChangeset for help on using the changeset viewer.