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. git-svn-id: https://develop.svn.wordpress.org/branches/5.5@49332 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
2f6e70f6d8
commit
7ba72fec4c
@ -4050,7 +4050,7 @@ function wp_insert_post( $postarr, $wp_error = false ) {
|
|||||||
if ( ! empty( $tax_object->default_term ) ) {
|
if ( ! empty( $tax_object->default_term ) ) {
|
||||||
|
|
||||||
// Filter out empty terms.
|
// Filter out empty terms.
|
||||||
if ( isset( $postarr['tax_input'] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
|
if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
|
||||||
$postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
|
$postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -992,14 +992,14 @@ class Tests_Taxonomy extends WP_UnitTestCase {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Add post.
|
// Add post.
|
||||||
$post_id = wp_insert_post(
|
$post_id = self::factory()->post->create(
|
||||||
array(
|
array(
|
||||||
'post_title' => 'Foo',
|
'post_title' => 'Foo',
|
||||||
'post_type' => 'post',
|
'post_type' => 'post',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test default category.
|
// Test default term.
|
||||||
$term = wp_get_post_terms( $post_id, $tax );
|
$term = wp_get_post_terms( $post_id, $tax );
|
||||||
$this->assertSame( get_option( 'default_term_' . $tax ), $term[0]->term_id );
|
$this->assertSame( get_option( 'default_term_' . $tax ), $term[0]->term_id );
|
||||||
|
|
||||||
@ -1013,16 +1013,18 @@ class Tests_Taxonomy extends WP_UnitTestCase {
|
|||||||
'taxonomies' => array( $tax ),
|
'taxonomies' => array( $tax ),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$post_id = wp_insert_post(
|
$post_id = self::factory()->post->create(
|
||||||
array(
|
array(
|
||||||
'post_title' => 'Foo',
|
'post_title' => 'Foo',
|
||||||
'post_type' => 'post-custom-tax',
|
'post_type' => 'post-custom-tax',
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
$term = wp_get_post_terms( $post_id, $tax );
|
|
||||||
|
// Test default term.
|
||||||
|
$term = wp_get_post_terms( $post_id, $tax );
|
||||||
$this->assertSame( get_option( 'default_term_' . $tax ), $term[0]->term_id );
|
$this->assertSame( get_option( 'default_term_' . $tax ), $term[0]->term_id );
|
||||||
|
|
||||||
// wp_set_object_terms shouldn't assign default category.
|
// wp_set_object_terms() should not assign default term.
|
||||||
wp_set_object_terms( $post_id, array(), $tax );
|
wp_set_object_terms( $post_id, array(), $tax );
|
||||||
$term = wp_get_post_terms( $post_id, $tax );
|
$term = wp_get_post_terms( $post_id, $tax );
|
||||||
$this->assertSame( array(), $term );
|
$this->assertSame( array(), $term );
|
||||||
@ -1030,4 +1032,24 @@ class Tests_Taxonomy extends WP_UnitTestCase {
|
|||||||
unregister_taxonomy( $tax );
|
unregister_taxonomy( $tax );
|
||||||
$this->assertSame( get_option( 'default_term_' . $tax ), false );
|
$this->assertSame( get_option( 'default_term_' . $tax ), false );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 51320
|
||||||
|
*/
|
||||||
|
function test_default_term_for_post_in_multiple_taxonomies() {
|
||||||
|
$post_type = 'test_post_type';
|
||||||
|
$tax1 = 'test_tax1';
|
||||||
|
$tax2 = 'test_tax2';
|
||||||
|
|
||||||
|
register_post_type( $post_type, array( 'taxonomies' => array( $tax1, $tax2 ) ) );
|
||||||
|
register_taxonomy( $tax1, $post_type, array( 'default_term' => 'term_1' ) );
|
||||||
|
register_taxonomy( $tax2, $post_type, array( 'default_term' => 'term_2' ) );
|
||||||
|
|
||||||
|
$post_id = self::factory()->post->create( array( 'post_type' => $post_type ) );
|
||||||
|
|
||||||
|
$taxonomies = get_post_taxonomies( $post_id );
|
||||||
|
|
||||||
|
$this->assertContains( $tax1, $taxonomies );
|
||||||
|
$this->assertContains( $tax2, $taxonomies );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user