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.



git-svn-id: https://develop.svn.wordpress.org/trunk@48356 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jake Spurlock 2020-07-07 00:53:41 +00:00
parent 4736a74c48
commit abfd9441b7
5 changed files with 125 additions and 1 deletions

View File

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

View File

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

View File

@ -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 ) {

View File

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

View File

@ -965,4 +965,57 @@ class Tests_Taxonomy extends WP_UnitTestCase {
$this->assertEquals( $problematic_term, $term_name );
}
/**
* Test default term for custom taxonomy.
*
* @ticket 43517
*/
function test_default_term_for_custom_taxonomy() {
wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) );
$tax = 'custom-tax';
// Create custom taxonomy to test with.
register_taxonomy(
$tax,
'post',
array(
'hierarchical' => true,
'public' => true,
'default_term' => array(
'name' => 'Default category',
'slug' => 'default-category',
),
)
);
// Add post.
$post_id = wp_insert_post(
array(
'post_title' => 'Foo',
'post_type' => 'post',
)
);
$term = wp_get_post_terms( $post_id, $tax );
$this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id );
// Add custom post type.
register_post_type(
'post-custom-tax',
array(
'taxonomies' => array( $tax ),
)
);
$post_id = wp_insert_post(
array(
'post_title' => 'Foo',
'post_type' => 'post-custom-tax',
)
);
$term = wp_get_post_terms( $post_id, $tax );
$this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id );
}
}