diff --git src/wp-includes/post.php src/wp-includes/post.php
index 65c7b6c15c..2e472e40c1 100644
|
|
function wp_insert_post( $postarr, $wp_error = false ) { |
4036 | 4036 | // Add default term for all associated custom taxonomies. |
4037 | 4037 | if ( 'auto-draft' !== $post_status ) { |
4038 | 4038 | foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { |
4039 | | if ( ! empty( $tax_object->default_term ) && ( empty( $postarr['tax_input'] ) || ! isset( $postarr['tax_input'][ $taxonomy ] ) ) ) { |
4040 | | $postarr['tax_input'][ $taxonomy ] = array(); |
| 4039 | |
| 4040 | if ( ! empty( $tax_object->default_term ) ) { |
| 4041 | |
| 4042 | // Filter out empty terms. |
| 4043 | if ( isset( $postarr['tax_input'] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) { |
| 4044 | $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] ); |
| 4045 | } |
| 4046 | |
| 4047 | // Passed custom taxonomy list overwrites existing list if not empty. |
| 4048 | $terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) ); |
| 4049 | if ( ! empty( $terms) && empty( $postarr['tax_input'][ $taxonomy ] ) ){ |
| 4050 | $postarr['tax_input'][ $taxonomy ] = $terms; |
| 4051 | } |
| 4052 | |
| 4053 | if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
| 4054 | $default_term_id = get_option( 'default_taxonomy_' . $taxonomy ); |
| 4055 | if ( ! empty( $default_term_id ) ) { |
| 4056 | $postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id ); |
| 4057 | } |
| 4058 | } |
4041 | 4059 | } |
4042 | 4060 | } |
4043 | 4061 | } |
diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
index e0acadcf87..aa2b757bbe 100644
|
|
function unregister_taxonomy( $taxonomy ) { |
506 | 506 | $taxonomy_object->remove_rewrite_rules(); |
507 | 507 | $taxonomy_object->remove_hooks(); |
508 | 508 | |
| 509 | // Remove custom taxonomy default term option. |
| 510 | if ( ! empty( $taxonomy_object->default_term ) ) { |
| 511 | delete_option( 'default_taxonomy_' . $taxonomy_object->name ); |
| 512 | } |
| 513 | |
509 | 514 | // Remove the taxonomy. |
510 | 515 | unset( $wp_taxonomies[ $taxonomy ] ); |
511 | 516 | |
… |
… |
function wp_delete_term( $term, $taxonomy, $args = array() ) { |
1824 | 1829 | } |
1825 | 1830 | } |
1826 | 1831 | |
| 1832 | // Don't delete the default custom taxonomy term. |
| 1833 | $taxonomy_object = get_taxonomy( $taxonomy ); |
| 1834 | if ( ! empty( $taxonomy_object->default_term ) ) { |
| 1835 | $defaults['default'] = (int) get_option( 'default_taxonomy_' . $taxonomy ); |
| 1836 | if ( $defaults['default'] === $term ) { |
| 1837 | return 0; |
| 1838 | } |
| 1839 | } |
| 1840 | |
1827 | 1841 | $args = wp_parse_args( $args, $defaults ); |
1828 | 1842 | |
1829 | 1843 | if ( isset( $args['default'] ) ) { |
… |
… |
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { |
2512 | 2526 | $terms = array( $terms ); |
2513 | 2527 | } |
2514 | 2528 | |
2515 | | // Add default term. |
2516 | | $taxonomy_obj = get_taxonomy( $taxonomy ); |
2517 | | |
2518 | | // Default term for this taxonomy. |
2519 | | $default_term_id = get_option( 'default_taxonomy_' . $taxonomy ); |
2520 | | if ( empty( $terms ) && ! empty( $taxonomy_obj->default_term ) && ! empty( $default_term_id ) ) { |
2521 | | $terms[] = (int) $default_term_id; |
2522 | | } |
2523 | | |
2524 | 2529 | if ( ! $append ) { |
2525 | 2530 | $old_tt_ids = wp_get_object_terms( |
2526 | 2531 | $object_id, |
diff --git tests/phpunit/tests/taxonomy.php tests/phpunit/tests/taxonomy.php
index 7a5a065be1..cd87415b65 100644
|
|
class Tests_Taxonomy extends WP_UnitTestCase { |
999 | 999 | ) |
1000 | 1000 | ); |
1001 | 1001 | |
| 1002 | // Test default category. |
1002 | 1003 | $term = wp_get_post_terms( $post_id, $tax ); |
1003 | 1004 | $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id ); |
1004 | 1005 | |
| 1006 | // Test default term deletion. |
| 1007 | $this->assertSame( wp_delete_term( $term[0]->term_id, $tax ), 0 ); |
| 1008 | |
1005 | 1009 | // Add custom post type. |
1006 | 1010 | register_post_type( |
1007 | 1011 | 'post-custom-tax', |
… |
… |
class Tests_Taxonomy extends WP_UnitTestCase { |
1017 | 1021 | ); |
1018 | 1022 | $term = wp_get_post_terms( $post_id, $tax ); |
1019 | 1023 | $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id ); |
| 1024 | |
| 1025 | // wp_set_object_terms shouldn't assign default category. |
| 1026 | wp_set_object_terms( $post_id, array(), $tax ); |
| 1027 | $term = wp_get_post_terms( $post_id, $tax ); |
| 1028 | $this->assertSame( array(), $term ); |
| 1029 | |
| 1030 | unregister_taxonomy( $tax ); |
| 1031 | $this->assertSame( get_option( 'default_taxonomy_' . $tax ), false ); |
1020 | 1032 | } |
1021 | 1033 | } |