Make WordPress Core

Changeset 49328


Ignore:
Timestamp:
10/27/2020 04:40:35 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.
Fixes #51320.

Location:
trunk
Files:
2 edited

Legend:

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

    r49326 r49328  
    40924092
    40934093                // Filter out empty terms.
    4094                 if ( isset( $postarr['tax_input'] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
     4094                if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
    40954095                    $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
    40964096                }
  • trunk/tests/phpunit/tests/taxonomy.php

    r49305 r49328  
    10081008
    10091009        // Add post.
    1010         $post_id = wp_insert_post(
     1010        $post_id = self::factory()->post->create(
    10111011            array(
    10121012                'post_title' => 'Foo',
     
    10151015        );
    10161016
    1017         // Test default category.
     1017        // Test default term.
    10181018        $term = wp_get_post_terms( $post_id, $tax );
    10191019        $this->assertSame( get_option( 'default_term_' . $tax ), $term[0]->term_id );
     
    10291029            )
    10301030        );
    1031         $post_id = wp_insert_post(
     1031        $post_id = self::factory()->post->create(
    10321032            array(
    10331033                'post_title' => 'Foo',
     
    10351035            )
    10361036        );
    1037         $term    = wp_get_post_terms( $post_id, $tax );
     1037
     1038        // Test default term.
     1039        $term = wp_get_post_terms( $post_id, $tax );
    10381040        $this->assertSame( get_option( 'default_term_' . $tax ), $term[0]->term_id );
    10391041
    1040         // wp_set_object_terms shouldn't assign default category.
     1042        // wp_set_object_terms() should not assign default term.
    10411043        wp_set_object_terms( $post_id, array(), $tax );
    10421044        $term = wp_get_post_terms( $post_id, $tax );
     
    10451047        unregister_taxonomy( $tax );
    10461048        $this->assertSame( get_option( 'default_term_' . $tax ), false );
     1049    }
     1050
     1051    /**
     1052     * @ticket 51320
     1053     */
     1054    function test_default_term_for_post_in_multiple_taxonomies() {
     1055        $post_type = 'test_post_type';
     1056        $tax1      = 'test_tax1';
     1057        $tax2      = 'test_tax2';
     1058
     1059        register_post_type( $post_type, array( 'taxonomies' => array( $tax1, $tax2 ) ) );
     1060        register_taxonomy( $tax1, $post_type, array( 'default_term' => 'term_1' ) );
     1061        register_taxonomy( $tax2, $post_type, array( 'default_term' => 'term_2' ) );
     1062
     1063        $post_id = self::factory()->post->create( array( 'post_type' => $post_type ) );
     1064
     1065        $taxonomies = get_post_taxonomies( $post_id );
     1066
     1067        $this->assertContains( $tax1, $taxonomies );
     1068        $this->assertContains( $tax2, $taxonomies );
    10471069    }
    10481070
Note: See TracChangeset for help on using the changeset viewer.