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
This commit is contained in:
parent
a7ad4e288e
commit
49d23a2bbe
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 ) {
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue