Make WordPress Core

Changeset 48043


Ignore:
Timestamp:
06/14/2020 09:40:10 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Posts, Post Types: Introduce default_category_post_types filter.

The filter allows custom post types associated with the category taxonomy to opt in to requiring a default category, same as regular posts.

Props enrico.sorcinelli.
Fixes #43516.

Location:
trunk
Files:
2 edited

Legend:

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

    r47922 r48043  
    46904690 * Set categories for a post.
    46914691 *
    4692  * If the post categories parameter is not set, then the default category is
    4693  * going used.
     4692 * If no categories are provided, the default category is used.
    46944693 *
    46954694 * @since 2.1.0
     
    47074706    $post_type   = get_post_type( $post_ID );
    47084707    $post_status = get_post_status( $post_ID );
    4709     // If $post_categories isn't already an array, make it one:
     4708
     4709    // If $post_categories isn't already an array, make it one.
    47104710    $post_categories = (array) $post_categories;
     4711
    47114712    if ( empty( $post_categories ) ) {
    4712         if ( 'post' === $post_type && 'auto-draft' !== $post_status ) {
     4713        /**
     4714         * Filters post types (in addition to 'post') that require a default category.
     4715         *
     4716         * @since 5.5.0
     4717         *
     4718         * @param array $post_types An array of post types. Default empty array.
     4719         */
     4720        $default_category_post_types = apply_filters( 'default_category_post_types', array() );
     4721
     4722        // Regular posts always require a default category.
     4723        $default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) );
     4724
     4725        if ( in_array( $post_type, $default_category_post_types, true )
     4726            && is_object_in_taxonomy( $post_type, 'category' )
     4727            && 'auto-draft' !== $post_status
     4728        ) {
    47134729            $post_categories = array( get_option( 'default_category' ) );
    47144730            $append          = false;
  • trunk/tests/phpunit/tests/term.php

    r47341 r48043  
    157157        $this->assertEquals( 1, count( $post->post_category ) );
    158158        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     159
    159160        $term1 = wp_insert_term( 'Foo', 'category' );
    160161        $term2 = wp_insert_term( 'Bar', 'category' );
    161162        $term3 = wp_insert_term( 'Baz', 'category' );
     163
    162164        wp_set_post_categories( $post_id, array( $term1['term_id'], $term2['term_id'] ) );
    163165        $this->assertEquals( 2, count( $post->post_category ) );
     
    168170
    169171        $term4 = wp_insert_term( 'Burrito', 'category' );
     172
    170173        wp_set_post_categories( $post_id, $term4['term_id'] );
    171174        $this->assertEquals( array( $term4['term_id'] ), $post->post_category );
     
    181184        $this->assertEquals( 1, count( $post->post_category ) );
    182185        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     186    }
     187
     188    /**
     189     * @ticket 43516
     190     */
     191    function test_wp_set_post_categories_sets_default_category_for_custom_post_types() {
     192        add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     193
     194        register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     195
     196        $post_id = self::factory()->post->create( array( 'post_type' => 'cpt' ) );
     197        $post    = get_post( $post_id );
     198
     199        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     200
     201        $term = wp_insert_term( 'Foo', 'category' );
     202
     203        wp_set_post_categories( $post_id, $term['term_id'] );
     204        $this->assertEquals( $term['term_id'], $post->post_category[0] );
     205
     206        wp_set_post_categories( $post_id, array() );
     207        $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     208
     209        remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     210    }
     211
     212    function filter_default_category_post_types( $post_types ) {
     213        $post_types[] = 'cpt';
     214        return $post_types;
    183215    }
    184216
Note: See TracChangeset for help on using the changeset viewer.