From 49a9cede70ad0f06344869325fb939ca47f49a9f Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 14 Jul 2020 16:41:03 +0000 Subject: [PATCH] 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 --- wp-includes/post.php | 22 ++++++++++++++++++++-- wp-includes/taxonomy.php | 23 ++++++++++++++--------- wp-includes/version.php | 2 +- 3 files changed, 35 insertions(+), 12 deletions(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index 85397787ae..097f8186e9 100644 --- a/wp-includes/post.php +++ b/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/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index e0acadcf87..aa2b757bbe 100644 --- a/wp-includes/taxonomy.php +++ b/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/wp-includes/version.php b/wp-includes/version.php index aa9d2121ba..25f2870327 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.