diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php index 85397787ae..097f8186e9 100644 --- a/src/wp-includes/post.php +++ b/src/wp-includes/post.php @@ -4036,8 +4036,26 @@ function wp_insert_post( $postarr, $wp_error = false ) { // Add default term for all associated custom taxonomies. if ( 'auto-draft' !== $post_status ) { foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { - if ( ! empty( $tax_object->default_term ) && ( empty( $postarr['tax_input'] ) || ! isset( $postarr['tax_input'][ $taxonomy ] ) ) ) { - $postarr['tax_input'][ $taxonomy ] = array(); + + if ( ! empty( $tax_object->default_term ) ) { + + // Filter out empty terms. + if ( isset( $postarr['tax_input'] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) { + $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] ); + } + + // Passed custom taxonomy list overwrites existing list if not empty. + $terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) ); + if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) { + $postarr['tax_input'][ $taxonomy ] = $terms; + } + + if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) { + $default_term_id = get_option( 'default_taxonomy_' . $taxonomy ); + if ( ! empty( $default_term_id ) ) { + $postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id ); + } + } } } } diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index e0acadcf87..aa2b757bbe 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -506,6 +506,11 @@ function unregister_taxonomy( $taxonomy ) { $taxonomy_object->remove_rewrite_rules(); $taxonomy_object->remove_hooks(); + // Remove custom taxonomy default term option. + if ( ! empty( $taxonomy_object->default_term ) ) { + delete_option( 'default_taxonomy_' . $taxonomy_object->name ); + } + // Remove the taxonomy. unset( $wp_taxonomies[ $taxonomy ] ); @@ -1824,6 +1829,15 @@ function wp_delete_term( $term, $taxonomy, $args = array() ) { } } + // Don't delete the default custom taxonomy term. + $taxonomy_object = get_taxonomy( $taxonomy ); + if ( ! empty( $taxonomy_object->default_term ) ) { + $defaults['default'] = (int) get_option( 'default_taxonomy_' . $taxonomy ); + if ( $defaults['default'] === $term ) { + return 0; + } + } + $args = wp_parse_args( $args, $defaults ); if ( isset( $args['default'] ) ) { @@ -2512,15 +2526,6 @@ function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { $terms = array( $terms ); } - // Add default term. - $taxonomy_obj = get_taxonomy( $taxonomy ); - - // Default term for this taxonomy. - $default_term_id = get_option( 'default_taxonomy_' . $taxonomy ); - if ( empty( $terms ) && ! empty( $taxonomy_obj->default_term ) && ! empty( $default_term_id ) ) { - $terms[] = (int) $default_term_id; - } - if ( ! $append ) { $old_tt_ids = wp_get_object_terms( $object_id, diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php index 7a5a065be1..cd87415b65 100644 --- a/tests/phpunit/tests/taxonomy.php +++ b/tests/phpunit/tests/taxonomy.php @@ -999,9 +999,13 @@ class Tests_Taxonomy extends WP_UnitTestCase { ) ); + // Test default category. $term = wp_get_post_terms( $post_id, $tax ); $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id ); + // Test default term deletion. + $this->assertSame( wp_delete_term( $term[0]->term_id, $tax ), 0 ); + // Add custom post type. register_post_type( 'post-custom-tax', @@ -1017,5 +1021,13 @@ class Tests_Taxonomy extends WP_UnitTestCase { ); $term = wp_get_post_terms( $post_id, $tax ); $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id ); + + // wp_set_object_terms shouldn't assign default category. + wp_set_object_terms( $post_id, array(), $tax ); + $term = wp_get_post_terms( $post_id, $tax ); + $this->assertSame( array(), $term ); + + unregister_taxonomy( $tax ); + $this->assertSame( get_option( 'default_taxonomy_' . $tax ), false ); } }