Make WordPress Core


Ignore:
Timestamp:
10/27/2020 05:54:43 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Posts, Post Types: Check if taxonomy is set for the tax_input parameter of wp_insert_post().

This avoids a PHP notice when creating a post with multiple taxonomies both having a default term.

Props yakimun, szaqal21, hareesh-pillai, audrasjb.
Merges [49328] to the 5.5 branch.
Fixes #51320.

Location:
branches/5.5
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/5.5

  • branches/5.5/tests/phpunit/tests/taxonomy.php

    r48665 r49332  
    993993
    994994        // Add post.
    995         $post_id = wp_insert_post(
     995        $post_id = self::factory()->post->create(
    996996            array(
    997997                'post_title' => 'Foo',
     
    10001000        );
    10011001
    1002         // Test default category.
     1002        // Test default term.
    10031003        $term = wp_get_post_terms( $post_id, $tax );
    10041004        $this->assertSame( get_option( 'default_term_' . $tax ), $term[0]->term_id );
     
    10141014            )
    10151015        );
    1016         $post_id = wp_insert_post(
     1016        $post_id = self::factory()->post->create(
    10171017            array(
    10181018                'post_title' => 'Foo',
     
    10201020            )
    10211021        );
    1022         $term    = wp_get_post_terms( $post_id, $tax );
     1022
     1023        // Test default term.
     1024        $term = wp_get_post_terms( $post_id, $tax );
    10231025        $this->assertSame( get_option( 'default_term_' . $tax ), $term[0]->term_id );
    10241026
    1025         // wp_set_object_terms shouldn't assign default category.
     1027        // wp_set_object_terms() should not assign default term.
    10261028        wp_set_object_terms( $post_id, array(), $tax );
    10271029        $term = wp_get_post_terms( $post_id, $tax );
     
    10311033        $this->assertSame( get_option( 'default_term_' . $tax ), false );
    10321034    }
     1035
     1036    /**
     1037     * @ticket 51320
     1038     */
     1039    function test_default_term_for_post_in_multiple_taxonomies() {
     1040        $post_type = 'test_post_type';
     1041        $tax1      = 'test_tax1';
     1042        $tax2      = 'test_tax2';
     1043
     1044        register_post_type( $post_type, array( 'taxonomies' => array( $tax1, $tax2 ) ) );
     1045        register_taxonomy( $tax1, $post_type, array( 'default_term' => 'term_1' ) );
     1046        register_taxonomy( $tax2, $post_type, array( 'default_term' => 'term_2' ) );
     1047
     1048        $post_id = self::factory()->post->create( array( 'post_type' => $post_type ) );
     1049
     1050        $taxonomies = get_post_taxonomies( $post_id );
     1051
     1052        $this->assertContains( $tax1, $taxonomies );
     1053        $this->assertContains( $tax2, $taxonomies );
     1054    }
    10331055}
Note: See TracChangeset for help on using the changeset viewer.