Make WordPress Core

Ticket #43516: 43516.default-category-filter-cpt.2.patch

File 43516.default-category-filter-cpt.2.patch, 3.8 KB (added by enrico.sorcinelli, 5 years ago)
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index 500bf54a5b..c6a76082cc 100644
    function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $appe 
    46794679        // If $post_categories isn't already an array, make it one:
    46804680        $post_categories = (array) $post_categories;
    46814681        if ( empty( $post_categories ) ) {
    4682                 if ( 'post' === $post_type && 'auto-draft' !== $post_status ) {
     4682                /**
     4683                 * Filters post types (in addition to 'post') where to apply default category taxonomy term.
     4684                 *
     4685                 * @since 5.5.0
     4686                 *
     4687                 * @param array $post_types An array post types. Default to empty array.
     4688                 */
     4689                $default_category_post_types = apply_filters( 'default_category_post_types', array() );
     4690                if ( in_array( $post_type, array_merge( $default_category_post_types, array( 'post' ) ) ) && 'auto-draft' !== $post_status ) {
    46834691                        $post_categories = array( get_option( 'default_category' ) );
    46844692                        $append          = false;
    46854693                } else {
  • tests/phpunit/tests/post.php

    diff --git tests/phpunit/tests/post.php tests/phpunit/tests/post.php
    index ca18c41b0e..665660c6c9 100644
    class Tests_Post extends WP_UnitTestCase { 
    14661466                $tags = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
    14671467                $this->assertEqualSets( array( $tag_2['term_id'], $tag_3['term_id'] ), $tags );
    14681468        }
     1469
     1470        /**
     1471         * Test that setting void category for CPT associated with 'category' taxonomy has default category.
     1472         *
     1473         * @ticket 43516
     1474         */
     1475        function test_set_post_category_cpt_default_category () {
     1476                add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     1477
     1478                $term = wp_insert_term( 'Foo', 'category' );
     1479
     1480                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_category' => array( $term['term_id'] ) ) );
     1481                $post = get_post( $post_id );
     1482                $this->assertInternalType( 'array', $post->post_category );
     1483                $this->assertEquals( $term['term_id'], $post->post_category[0] );
     1484
     1485                // Reset categories
     1486                wp_set_post_categories( $post_id, array() );
     1487                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1488
     1489                register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     1490                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt', 'post_category' => array( $term['term_id'] ) ) );
     1491                $post = get_post( $post_id );
     1492
     1493                // Reset categories
     1494                wp_set_post_categories( $post_id, array() );
     1495                $this->assertInternalType( 'array', $post->post_category );
     1496                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1497
     1498                remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     1499        }
     1500
     1501        /**
     1502         * Test that new 'post' and CPT associated with 'category' taxonomy has default category.
     1503         *
     1504         * @ticket 43516
     1505         */
     1506        function test_insert_post_cpt_default_category () {
     1507                add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     1508
     1509                $post_id = wp_insert_post( array( 'post_title' => 'Foo' ) );
     1510                $post = get_post( $post_id );
     1511                $this->assertInternalType( 'array', $post->post_category );
     1512                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1513
     1514                register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     1515                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt' ) );
     1516                $post = get_post( $post_id );
     1517                $this->assertInternalType( 'array', $post->post_category );
     1518                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1519
     1520                remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     1521        }
     1522
     1523        function filter_default_category_post_types ( $post_types ) {
     1524                $post_types[] = 'cpt';
     1525                return $post_types;
     1526        }
    14691527}