Taxonomy: Make some adjustments to handling default terms for custom taxonomies:

* Move default term assignment from `wp_set_object_terms()` to `wp_insert_post()`.
* Make sure the passed taxonomy list overwrites the existing list if not empty.
* Remove the default term option on `unregister_taxonomy()`.
* Prevent deletion of the default term in `wp_delete_term()`.

Props enrico.sorcinelli, TimothyBlynJacobs.
See #43517.

git-svn-id: https://develop.svn.wordpress.org/trunk@48480 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-07-14 16:39:44 +00:00
parent 9181ecff85
commit c843cf19c3
3 changed files with 46 additions and 11 deletions

View File

@ -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 );
}
}
}
}
}

View File

@ -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,

View File

@ -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 );
}
}