From 49d23a2bbee2b654555365933f88b665f0aa5ac6 Mon Sep 17 00:00:00 2001 From: whyisjake Date: Tue, 7 Jul 2020 00:55:03 +0000 Subject: [PATCH] Taxonomy: Add support for default terms for custom taxonomies. The new default_term argument is added to `register_taxonomy()` allowing a user to define the default term `name` and optionally `slug` and `description`. Fixes #43517. Props enrico.sorcinelli, SergeyBiryukov, desrosj, davidbaumwald, whyisjake. Built from https://develop.svn.wordpress.org/trunk@48356 git-svn-id: http://core.svn.wordpress.org/trunk@48125 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/capabilities.php | 2 +- wp-includes/class-wp-taxonomy.php | 25 +++++++++++++++++++++ wp-includes/post.php | 9 ++++++++ wp-includes/taxonomy.php | 37 +++++++++++++++++++++++++++++++ wp-includes/version.php | 2 +- 5 files changed, 73 insertions(+), 2 deletions(-) diff --git a/wp-includes/capabilities.php b/wp-includes/capabilities.php index a19487757c..d2eae061bc 100644 --- a/wp-includes/capabilities.php +++ b/wp-includes/capabilities.php @@ -539,7 +539,7 @@ function map_meta_cap( $cap, $user_id, ...$args ) { break; } - if ( 'delete_term' === $cap && ( get_option( 'default_' . $term->taxonomy ) == $term->term_id ) ) { + if ( 'delete_term' === $cap && ( get_option( 'default_' . $term->taxonomy ) == $term->term_id || get_option( 'default_taxonomy_' . $term->taxonomy ) == $term->term_id ) ) { $caps[] = 'do_not_allow'; break; } diff --git a/wp-includes/class-wp-taxonomy.php b/wp-includes/class-wp-taxonomy.php index ebc7b0406f..c34a03c38e 100644 --- a/wp-includes/class-wp-taxonomy.php +++ b/wp-includes/class-wp-taxonomy.php @@ -209,6 +209,15 @@ final class WP_Taxonomy { */ public $rest_controller_class; + /** + * The default term name for this taxonomy. If you pass an array you have + * to set 'name' and optionally 'slug' and 'description'. + * + * @since 5.5.0 + * @var array|string + */ + public $default_term; + /** * The controller instance for this taxonomy's REST API endpoints. * @@ -288,6 +297,7 @@ final class WP_Taxonomy { 'show_in_rest' => false, 'rest_base' => false, 'rest_controller_class' => false, + 'default_term' => null, '_builtin' => false, ); @@ -386,6 +396,21 @@ final class WP_Taxonomy { } } + // Default taxonomy term. + if ( ! empty( $args['default_term'] ) ) { + if ( ! is_array( $args['default_term'] ) ) { + $args['default_term'] = array( 'name' => $args['default_term'] ); + } + $args['default_term'] = wp_parse_args( + $args['default_term'], + array( + 'name' => '', + 'slug' => '', + 'description' => '', + ) + ); + } + foreach ( $args as $property_name => $property_value ) { $this->$property_name = $property_value; } diff --git a/wp-includes/post.php b/wp-includes/post.php index d677b4afe0..65c7b6c15c 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -4033,6 +4033,15 @@ function wp_insert_post( $postarr, $wp_error = false ) { wp_set_post_tags( $post_ID, $postarr['tags_input'] ); } + // 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(); + } + } + } + // New-style support for all custom taxonomies. if ( ! empty( $postarr['tax_input'] ) ) { foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index 707e26359c..3a89d641db 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -335,6 +335,7 @@ function is_taxonomy_hierarchical( $taxonomy ) { * arguments to register the Taxonomy in REST API. * @since 5.1.0 Introduced `meta_box_sanitize_cb` argument. * @since 5.4.0 Added the registered taxonomy object as a return value. + * @since 5.5.0 Introduced `default_term` argument. * * @global array $wp_taxonomies Registered taxonomies. * @@ -406,6 +407,13 @@ function is_taxonomy_hierarchical( $taxonomy ) { * to post types, which confirms that the objects are published before * counting them. Default _update_generic_term_count() for taxonomies * attached to other object types, such as users. + * @type string|array $default_term { + * Default term to be used for the taxonomy. + * + * @type string $name Name of default term. + * @type string $slug Slug for default term. Default empty. + * @type string $description Description for default term. Default empty. + * } * @type bool $_builtin This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY! * Default false. * } @@ -432,6 +440,27 @@ function register_taxonomy( $taxonomy, $object_type, $args = array() ) { $taxonomy_object->add_hooks(); + // Add default term. + if ( ! empty( $taxonomy_object->default_term ) ) { + if ( $term = term_exists( $taxonomy_object->default_term['name'], $taxonomy ) ) { + update_option( 'default_taxonomy_' . $taxonomy_object->name, $term['term_id'] ); + } else { + $term = wp_insert_term( + $taxonomy_object->default_term['name'], + $taxonomy, + array( + 'slug' => sanitize_title( $taxonomy_object->default_term['slug'] ), + 'description' => $taxonomy_object->default_term['description'], + ) + ); + + // Update term id in options. + if ( ! is_wp_error( $term ) ) { + update_option( 'default_taxonomy_' . $taxonomy_object->name, $term['term_id'] ); + } + } + } + /** * Fires after a taxonomy is registered. * @@ -2482,6 +2511,14 @@ 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. + if ( empty( $terms ) && ! empty( $taxonomy_obj->default_term ) && ! empty( $default_term_id = get_option( 'default_taxonomy_' . $taxonomy ) ) ) { + $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 c69cd4b27c..f5c0bf685b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-alpha-48355'; +$wp_version = '5.5-alpha-48356'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.