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.
Built from https://develop.svn.wordpress.org/trunk@48480


git-svn-id: http://core.svn.wordpress.org/trunk@48249 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-07-14 16:41:03 +00:00
parent 9e8d1b1a68
commit 49a9cede70
3 changed files with 35 additions and 12 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

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.5-beta1-48479';
$wp_version = '5.5-beta1-48480';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.