diff --git src/wp-includes/capabilities.php src/wp-includes/capabilities.php
index e3c28a1cf1..9b713805f5 100644
|
|
function map_meta_cap( $cap, $user_id, ...$args ) { |
534 | 534 | break; |
535 | 535 | } |
536 | 536 | |
537 | | if ( 'delete_term' === $cap && ( get_option( 'default_' . $term->taxonomy ) == $term->term_id ) ) { |
| 537 | if ( 'delete_term' === $cap && ( get_option( 'default_' . $term->taxonomy ) == $term->term_id || get_option( 'default_taxonomy_' . $term->taxonomy ) == $term->term_id ) ) { |
538 | 538 | $caps[] = 'do_not_allow'; |
539 | 539 | break; |
540 | 540 | } |
diff --git src/wp-includes/class-wp-taxonomy.php src/wp-includes/class-wp-taxonomy.php
index 0ff90fb53e..49ead1b56c 100644
|
|
final class WP_Taxonomy { |
209 | 209 | */ |
210 | 210 | public $rest_controller_class; |
211 | 211 | |
| 212 | /** |
| 213 | * The default term name. If you pass an array you have to set |
| 214 | * 'name' and optionally slug' and 'description'. |
| 215 | * |
| 216 | * @since 5.5.0 |
| 217 | * @var array|string |
| 218 | */ |
| 219 | public $default_term; |
| 220 | |
212 | 221 | /** |
213 | 222 | * Whether it is a built-in taxonomy. |
214 | 223 | * |
… |
… |
final class WP_Taxonomy { |
278 | 287 | 'show_in_rest' => false, |
279 | 288 | 'rest_base' => false, |
280 | 289 | 'rest_controller_class' => false, |
| 290 | 'default_term' => null, |
281 | 291 | '_builtin' => false, |
282 | 292 | ); |
283 | 293 | |
… |
… |
final class WP_Taxonomy { |
376 | 386 | } |
377 | 387 | } |
378 | 388 | |
| 389 | // Default taxonomy term. |
| 390 | if ( ! empty( $args['default_term'] ) ) { |
| 391 | if ( ! is_array( $args['default_term'] ) ) { |
| 392 | $args['default_term'] = array( 'name' => $args['default_term'] ); |
| 393 | } |
| 394 | $args['default_term'] = wp_parse_args( $args['default_term'], array( 'name' => '', 'slug' => '', 'description' => '' ) ); |
| 395 | } |
| 396 | |
379 | 397 | foreach ( $args as $property_name => $property_value ) { |
380 | 398 | $this->$property_name = $property_value; |
381 | 399 | } |
diff --git src/wp-includes/post.php src/wp-includes/post.php
index 500bf54a5b..8760c69910 100644
|
|
function wp_insert_post( $postarr, $wp_error = false ) { |
4005 | 4005 | wp_set_post_tags( $post_ID, $postarr['tags_input'] ); |
4006 | 4006 | } |
4007 | 4007 | |
| 4008 | // Add default term for all associated custom taxonomies. |
| 4009 | if ( 'auto-draft' !== $post_status ) { |
| 4010 | foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { |
| 4011 | if ( ! empty( $tax_object->default_term ) && ( empty( $postarr['tax_input'] ) || ! isset( $postarr['tax_input'][ $taxonomy ] ) ) ) { |
| 4012 | $postarr['tax_input'][ $taxonomy ] = array(); |
| 4013 | } |
| 4014 | } |
| 4015 | } |
| 4016 | |
4008 | 4017 | // New-style support for all custom taxonomies. |
4009 | 4018 | if ( ! empty( $postarr['tax_input'] ) ) { |
4010 | 4019 | foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { |
diff --git src/wp-includes/taxonomy.php src/wp-includes/taxonomy.php
index 1693bad778..1ed288cbf1 100644
|
|
function is_taxonomy_hierarchical( $taxonomy ) { |
335 | 335 | * arguments to register the Taxonomy in REST API. |
336 | 336 | * @since 5.1.0 Introduced `meta_box_sanitize_cb` argument. |
337 | 337 | * @since 5.4.0 Added the registered taxonomy object as a return value. |
| 338 | * @since 5.5.0 Introduced `default_term` argument. |
338 | 339 | * |
339 | 340 | * @global array $wp_taxonomies Registered taxonomies. |
340 | 341 | * |
… |
… |
function is_taxonomy_hierarchical( $taxonomy ) { |
406 | 407 | * to post types, which confirms that the objects are published before |
407 | 408 | * counting them. Default _update_generic_term_count() for taxonomies |
408 | 409 | * attached to other object types, such as users. |
| 410 | * @type string|array $default_term { |
| 411 | * Default term to be used for the taxonomy. |
| 412 | * |
| 413 | * @type string $name Name of default term. |
| 414 | * @type string $slug Slug for default term. Default empty. |
| 415 | * @type string $description Description for default term. Default empty. |
| 416 | * } |
409 | 417 | * @type bool $_builtin This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY! |
410 | 418 | * Default false. |
411 | 419 | * } |
… |
… |
function register_taxonomy( $taxonomy, $object_type, $args = array() ) { |
432 | 440 | |
433 | 441 | $taxonomy_object->add_hooks(); |
434 | 442 | |
| 443 | // Add default term. |
| 444 | if ( ! empty( $taxonomy_object->default_term ) ) { |
| 445 | if ( $term = term_exists( $taxonomy_object->default_term['name'], $taxonomy ) ) { |
| 446 | update_option('default_taxonomy_' . $taxonomy_object->name, $term['term_id'] ); |
| 447 | } else { |
| 448 | $term = wp_insert_term( |
| 449 | $taxonomy_object->default_term['name'], |
| 450 | $taxonomy, |
| 451 | array( |
| 452 | 'slug' => sanitize_title( $taxonomy_object->default_term['slug'] ), |
| 453 | 'description' => $taxonomy_object->default_term['description'] |
| 454 | ) |
| 455 | ); |
| 456 | |
| 457 | // Update term id in options. |
| 458 | if ( ! is_wp_error( $term ) ) { |
| 459 | update_option('default_taxonomy_' . $taxonomy_object->name, $term['term_id'] ); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
435 | 464 | /** |
436 | 465 | * Fires after a taxonomy is registered. |
437 | 466 | * |
… |
… |
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { |
2452 | 2481 | $terms = array( $terms ); |
2453 | 2482 | } |
2454 | 2483 | |
| 2484 | // Add default term. |
| 2485 | $taxonomy_obj = get_taxonomy( $taxonomy ); |
| 2486 | |
| 2487 | // Default term for this taxonomy. |
| 2488 | if ( empty( $terms ) && ! empty ( $taxonomy_obj->default_term ) && ! empty( $default_term_id = get_option( 'default_taxonomy_' . $taxonomy ) ) ) { |
| 2489 | $terms[] = (int) $default_term_id; |
| 2490 | } |
| 2491 | |
2455 | 2492 | if ( ! $append ) { |
2456 | 2493 | $old_tt_ids = wp_get_object_terms( |
2457 | 2494 | $object_id, |
diff --git tests/phpunit/tests/taxonomy.php tests/phpunit/tests/taxonomy.php
index adf61e76fc..3375331360 100644
|
|
class Tests_Taxonomy extends WP_UnitTestCase { |
965 | 965 | |
966 | 966 | $this->assertEquals( $problematic_term, $term_name ); |
967 | 967 | } |
| 968 | |
| 969 | /** |
| 970 | * Test default term for custom taxonomy. |
| 971 | * |
| 972 | * @ticket 43517 |
| 973 | */ |
| 974 | function test_default_term_for_custom_taxonomy() { |
| 975 | |
| 976 | wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); |
| 977 | |
| 978 | $tax = 'custom-tax'; |
| 979 | |
| 980 | // Create custom taxonomy to test with. |
| 981 | register_taxonomy( |
| 982 | $tax, |
| 983 | 'post', |
| 984 | array( |
| 985 | 'hierarchical' => true, |
| 986 | 'public' => true, |
| 987 | 'default_term' => array( |
| 988 | 'name' => 'Default category', |
| 989 | 'slug' => 'default-category', |
| 990 | ) |
| 991 | ) |
| 992 | ); |
| 993 | |
| 994 | // Add post. |
| 995 | $post_id = wp_insert_post( |
| 996 | array( |
| 997 | 'post_title' => 'Foo', |
| 998 | 'post_type' => 'post', |
| 999 | ) |
| 1000 | ); |
| 1001 | |
| 1002 | $term = wp_get_post_terms( $post_id, $tax ); |
| 1003 | $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id ); |
| 1004 | |
| 1005 | // Add custom post type. |
| 1006 | register_post_type( |
| 1007 | 'post-custom-tax', |
| 1008 | array( |
| 1009 | 'taxonomies' => array( $tax ) |
| 1010 | ) |
| 1011 | ); |
| 1012 | $post_id = wp_insert_post( |
| 1013 | array( |
| 1014 | 'post_title' => 'Foo', |
| 1015 | 'post_type' => 'post-custom-tax', |
| 1016 | ) |
| 1017 | ); |
| 1018 | $term = wp_get_post_terms( $post_id, $tax ); |
| 1019 | $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id ); |
| 1020 | } |
968 | 1021 | } |