diff --git a/src/wp-includes/capabilities.php b/src/wp-includes/capabilities.php
index 6b1a672..25da5e2 100644
|
a
|
b
|
function map_meta_cap( $cap, $user_id ) { |
| 511 | 511 | break; |
| 512 | 512 | } |
| 513 | 513 | |
| 514 | | if ( 'delete_term' === $cap && ( $term->term_id == get_option( 'default_' . $term->taxonomy ) ) ) { |
| | 514 | if ( 'delete_term' === $cap && ( $term->term_id == get_option( 'default_' . $term->taxonomy ) || $term->term_id == get_option( 'default_taxonomy_' . $term->taxonomy ) ) ) { |
| 515 | 515 | $caps[] = 'do_not_allow'; |
| 516 | 516 | break; |
| 517 | 517 | } |
diff --git a/src/wp-includes/class-wp-taxonomy.php b/src/wp-includes/class-wp-taxonomy.php
index ef6f1a0..7c5a402 100644
|
a
|
b
|
final class WP_Taxonomy { |
| 205 | 205 | public $rest_controller_class; |
| 206 | 206 | |
| 207 | 207 | /** |
| | 208 | * The default term name. If you pass an array you have to set |
| | 209 | * 'name' and optionally slug' and 'description'. |
| | 210 | * |
| | 211 | * @since 5.0.0 |
| | 212 | * @var array|string |
| | 213 | */ |
| | 214 | public $default_term; |
| | 215 | |
| | 216 | /** |
| 208 | 217 | * Whether it is a built-in taxonomy. |
| 209 | 218 | * |
| 210 | 219 | * @since 4.7.0 |
| … |
… |
final class WP_Taxonomy { |
| 273 | 282 | 'show_in_rest' => false, |
| 274 | 283 | 'rest_base' => false, |
| 275 | 284 | 'rest_controller_class' => false, |
| | 285 | 'default_term' => null, |
| 276 | 286 | '_builtin' => false, |
| 277 | 287 | ); |
| 278 | 288 | |
| … |
… |
final class WP_Taxonomy { |
| 370 | 380 | } |
| 371 | 381 | } |
| 372 | 382 | |
| | 383 | // Default taxonomy term. |
| | 384 | if ( ! empty( $args['default_term'] ) ) { |
| | 385 | if ( ! is_array( $args['default_term'] ) ) { |
| | 386 | $args['default_term'] = array( 'name' => $args['default_term'] ); |
| | 387 | } |
| | 388 | $args['default_term'] = wp_parse_args( $args['default_term'], array( 'name' => '', 'slug' => '', 'description' => '' ) ); |
| | 389 | } |
| | 390 | |
| 373 | 391 | foreach ( $args as $property_name => $property_value ) { |
| 374 | 392 | $this->$property_name = $property_value; |
| 375 | 393 | } |
diff --git a/src/wp-includes/post.php b/src/wp-includes/post.php
index 39e1891..7cd0588 100644
|
a
|
b
|
function wp_insert_post( $postarr, $wp_error = false ) { |
| 3573 | 3573 | wp_set_post_tags( $post_ID, $postarr['tags_input'] ); |
| 3574 | 3574 | } |
| 3575 | 3575 | |
| | 3576 | // Add default term for all associated custom taxonomies. |
| | 3577 | if ( 'auto-draft' != $post_status ) { |
| | 3578 | foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { |
| | 3579 | if ( ! empty( $tax_object->default_term ) && ( empty( $postarr['tax_input'] ) || ! isset( $postarr['tax_input'][ $taxonomy ] ) ) ) { |
| | 3580 | $postarr['tax_input'][ $taxonomy ] = array(); |
| | 3581 | } |
| | 3582 | } |
| | 3583 | } |
| | 3584 | |
| 3576 | 3585 | // New-style support for all custom taxonomies. |
| 3577 | 3586 | if ( ! empty( $postarr['tax_input'] ) ) { |
| 3578 | 3587 | foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { |
diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php
index 4744dd4..d791e66 100644
|
a
|
b
|
function is_taxonomy_hierarchical( $taxonomy ) { |
| 324 | 324 | * @since 4.7.0 Introduced `show_in_rest`, 'rest_base' and 'rest_controller_class' |
| 325 | 325 | * arguments to register the Taxonomy in REST API. |
| 326 | 326 | * @since 5.0.0 Introduced `meta_box_sanitize_cb` argument. |
| | 327 | * @since 5.0.0 Introduced `default_term` argument. |
| 327 | 328 | * |
| 328 | 329 | * @global array $wp_taxonomies Registered taxonomies. |
| 329 | 330 | * |
| … |
… |
function is_taxonomy_hierarchical( $taxonomy ) { |
| 394 | 395 | * to post types, which confirms that the objects are published before |
| 395 | 396 | * counting them. Default _update_generic_term_count() for taxonomies |
| 396 | 397 | * attached to other object types, such as users. |
| | 398 | * @type string|array $default_term { |
| | 399 | * Default term to be used for the taxonomy. |
| | 400 | * |
| | 401 | * @type string $name Name of default term. |
| | 402 | * @type string $slug Slug for default term (default empty). |
| | 403 | * @type string $description Description for default term (default empty). |
| | 404 | * } |
| 397 | 405 | * @type bool $_builtin This taxonomy is a "built-in" taxonomy. INTERNAL USE ONLY! |
| 398 | 406 | * Default false. |
| 399 | 407 | * } |
| … |
… |
function register_taxonomy( $taxonomy, $object_type, $args = array() ) { |
| 420 | 428 | |
| 421 | 429 | $taxonomy_object->add_hooks(); |
| 422 | 430 | |
| | 431 | // Add default term. |
| | 432 | if ( ! empty( $taxonomy_object->default_term ) ) { |
| | 433 | if ( $term = term_exists( $taxonomy_object->default_term['name'], $taxonomy ) ) { |
| | 434 | update_option('default_taxonomy_' . $taxonomy_object->name, $term['term_id'] ); |
| | 435 | } |
| | 436 | else { |
| | 437 | $term = wp_insert_term( $taxonomy_object->default_term['name'], $taxonomy, array( |
| | 438 | 'slug' => sanitize_title( $taxonomy_object->default_term['slug'] ), |
| | 439 | 'description' => $taxonomy_object->default_term['description'] |
| | 440 | )); |
| | 441 | |
| | 442 | // Update term id in options. |
| | 443 | if ( ! is_wp_error( $term ) ) { |
| | 444 | update_option('default_taxonomy_' . $taxonomy_object->name, $term['term_id']); |
| | 445 | } |
| | 446 | } |
| | 447 | } |
| | 448 | |
| 423 | 449 | /** |
| 424 | 450 | * Fires after a taxonomy is registered. |
| 425 | 451 | * |
| … |
… |
function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) { |
| 2375 | 2401 | $terms = array( $terms ); |
| 2376 | 2402 | } |
| 2377 | 2403 | |
| | 2404 | // Add default term |
| | 2405 | $taxonomy_obj = get_taxonomy( $taxonomy ); |
| | 2406 | |
| | 2407 | // Default term for this taxonomy. |
| | 2408 | if ( empty( $terms ) && ! empty ( $taxonomy_obj->default_term ) && ! empty( $default_term_id = get_option( 'default_taxonomy_' . $taxonomy ) ) ) { |
| | 2409 | $terms[] = (int) $default_term_id; |
| | 2410 | } |
| | 2411 | |
| 2378 | 2412 | if ( ! $append ) { |
| 2379 | 2413 | $old_tt_ids = wp_get_object_terms( |
| 2380 | 2414 | $object_id, $taxonomy, array( |
diff --git a/tests/phpunit/tests/taxonomy.php b/tests/phpunit/tests/taxonomy.php
index d651b12..bcaac7e 100644
|
a
|
b
|
class Tests_Taxonomy extends WP_UnitTestCase { |
| 926 | 926 | |
| 927 | 927 | $this->assertEquals( $problematic_term, $term_name ); |
| 928 | 928 | } |
| | 929 | |
| | 930 | /** |
| | 931 | * Test default term for custom taxonomy. |
| | 932 | * |
| | 933 | * @ticket 43517 |
| | 934 | */ |
| | 935 | function test_default_term_for_custom_taxonomy() { |
| | 936 | |
| | 937 | wp_set_current_user( self::factory()->user->create( array( 'role' => 'editor' ) ) ); |
| | 938 | |
| | 939 | $tax = 'custom-tax'; |
| | 940 | |
| | 941 | // Create custom taxonomy to test with. |
| | 942 | register_taxonomy( $tax, 'post', array( |
| | 943 | 'hierarchical' => true, |
| | 944 | 'public' => true, |
| | 945 | 'default_term' => array( 'name' => 'Default category', 'slug' => 'default-category' ) |
| | 946 | ) ); |
| | 947 | |
| | 948 | // Add post. |
| | 949 | $post_id = wp_insert_post( |
| | 950 | array( |
| | 951 | 'post_title' => 'Foo', |
| | 952 | 'post_type' => 'post', |
| | 953 | ) |
| | 954 | ); |
| | 955 | $term = wp_get_post_terms( $post_id, $tax ); |
| | 956 | $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id ); |
| | 957 | |
| | 958 | // Add custom post. |
| | 959 | register_post_type( 'post-custom-tax', array( 'taxonomies' => array( $tax ) ) ); |
| | 960 | $post_id = wp_insert_post( |
| | 961 | array( |
| | 962 | 'post_title' => 'Foo', |
| | 963 | 'post_type' => 'post-custom-tax', |
| | 964 | ) |
| | 965 | ); |
| | 966 | $term = wp_get_post_terms( $post_id, $tax ); |
| | 967 | $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id ); |
| | 968 | } |
| 929 | 969 | } |