Make WordPress Core

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

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

    diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
    index 39e1891..1f256a4 100644
    a b function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $appe 
    41684168        // If $post_categories isn't already an array, make it one:
    41694169        $post_categories = (array) $post_categories;
    41704170        if ( empty( $post_categories ) ) {
    4171                 if ( 'post' == $post_type && 'auto-draft' != $post_status ) {
     4171                /**
     4172                 * Filters post types (in addition to 'post') where to apply default category taxonomy term.
     4173                 *
     4174                 * @since 5.0.0
     4175                 *
     4176                 * @param array $post_types An array post types. Default to empty array.
     4177                 */
     4178                $default_category_post_types = apply_filters( 'default_category_post_types', array() );
     4179                if ( in_array( $post_type, array_merge( $default_category_post_types, array( 'post' ) ) ) && 'auto-draft' != $post_status ) {
    41724180                        $post_categories = array( get_option( 'default_category' ) );
    41734181                        $append          = false;
    41744182                } else {
  • tests/phpunit/tests/post.php

    diff --git a/tests/phpunit/tests/post.php b/tests/phpunit/tests/post.php
    index c0b20bb..d1ac71c 100644
    a b class Tests_Post extends WP_UnitTestCase { 
    13541354                $this->assertEquals( $changeset_data, json_decode( get_post( $post_id )->post_content, true ) );
    13551355        }
    13561356
     1357        /**
     1358         * Test that setting void category for CPT associated with 'category' taxonomy has default category.
     1359         *
     1360         * @ticket 43516
     1361         */
     1362        function test_set_post_category_cpt_default_category () {
     1363                add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     1364
     1365                $term = wp_insert_term( 'Foo', 'category' );
     1366
     1367                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_category' => array( $term['term_id'] ) ) );
     1368                $post = get_post( $post_id );
     1369                $this->assertInternalType( 'array', $post->post_category );
     1370                $this->assertEquals( $term['term_id'], $post->post_category[0] );
     1371
     1372                // Reset categories
     1373                wp_set_post_categories( $post_id, array() );
     1374                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1375
     1376                register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     1377                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt', 'post_category' => array( $term['term_id'] ) ) );
     1378                $post = get_post( $post_id );
     1379
     1380                // Reset categories
     1381                wp_set_post_categories( $post_id, array() );
     1382                $this->assertInternalType( 'array', $post->post_category );
     1383                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1384
     1385                remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     1386        }
     1387
     1388        /**
     1389         * Test that new 'post' and CPT associated with 'category' taxonomy has default category.
     1390         *
     1391         * @ticket 43516
     1392         */
     1393        function test_insert_post_cpt_default_category () {
     1394                add_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     1395
     1396                $post_id = wp_insert_post( array( 'post_title' => 'Foo' ) );
     1397                $post = get_post( $post_id );
     1398                $this->assertInternalType( 'array', $post->post_category );
     1399                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1400
     1401                register_post_type( 'cpt', array( 'taxonomies' => array( 'category' ) ) );
     1402                $post_id = wp_insert_post( array( 'post_title' => 'Foo', 'post_type' => 'cpt' ) );
     1403                $post = get_post( $post_id );
     1404                $this->assertInternalType( 'array', $post->post_category );
     1405                $this->assertEquals( get_option( 'default_category' ), $post->post_category[0] );
     1406
     1407                remove_filter( 'default_category_post_types', array( $this, 'filter_default_category_post_types' ) );
     1408        }
     1409
     1410        function filter_default_category_post_types ( $post_types ) {
     1411                $post_types[] = 'cpt';
     1412                return $post_types;
     1413        }
    13571414}