Make WordPress Core

Changeset 48480


Ignore:
Timestamp:
07/14/2020 04:39:44 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Taxonomy: Make some adjustments to handling default terms for custom taxonomies:

  • Move default term assignment from wp_set_object_terms() to wp_insert_post().
  • Make sure the passed taxonomy list overwrites the existing list if not empty.
  • Remove the default term option on unregister_taxonomy().
  • Prevent deletion of the default term in wp_delete_term().

Props enrico.sorcinelli, TimothyBlynJacobs.
See #43517.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r48462 r48480  
    40374037        if ( 'auto-draft' !== $post_status ) {
    40384038                foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) {
    4039                         if ( ! empty( $tax_object->default_term ) && ( empty( $postarr['tax_input'] ) || ! isset( $postarr['tax_input'][ $taxonomy ] ) ) ) {
    4040                                 $postarr['tax_input'][ $taxonomy ] = array();
     4039
     4040                        if ( ! empty( $tax_object->default_term ) ) {
     4041
     4042                                // Filter out empty terms.
     4043                                if ( isset( $postarr['tax_input'] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) {
     4044                                        $postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] );
     4045                                }
     4046
     4047                                // Passed custom taxonomy list overwrites existing list if not empty.
     4048                                $terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) );
     4049                                if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) {
     4050                                        $postarr['tax_input'][ $taxonomy ] = $terms;
     4051                                }
     4052
     4053                                if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) {
     4054                                        $default_term_id = get_option( 'default_taxonomy_' . $taxonomy );
     4055                                        if ( ! empty( $default_term_id ) ) {
     4056                                                $postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id );
     4057                                        }
     4058                                }
    40414059                        }
    40424060                }
  • trunk/src/wp-includes/taxonomy.php

    r48358 r48480  
    507507        $taxonomy_object->remove_hooks();
    508508
     509        // Remove custom taxonomy default term option.
     510        if ( ! empty( $taxonomy_object->default_term ) ) {
     511                delete_option( 'default_taxonomy_' . $taxonomy_object->name );
     512        }
     513
    509514        // Remove the taxonomy.
    510515        unset( $wp_taxonomies[ $taxonomy ] );
    … …  
    18251830        }
    18261831
     1832        // Don't delete the default custom taxonomy term.
     1833        $taxonomy_object = get_taxonomy( $taxonomy );
     1834        if ( ! empty( $taxonomy_object->default_term ) ) {
     1835                $defaults['default'] = (int) get_option( 'default_taxonomy_' . $taxonomy );
     1836                if ( $defaults['default'] === $term ) {
     1837                        return 0;
     1838                }
     1839        }
     1840
    18271841        $args = wp_parse_args( $args, $defaults );
    18281842
    … …  
    25132527        }
    25142528
    2515         // Add default term.
    2516         $taxonomy_obj = get_taxonomy( $taxonomy );
    2517 
    2518         // Default term for this taxonomy.
    2519         $default_term_id = get_option( 'default_taxonomy_' . $taxonomy );
    2520         if ( empty( $terms ) && ! empty( $taxonomy_obj->default_term ) && ! empty( $default_term_id ) ) {
    2521                 $terms[] = (int) $default_term_id;
    2522         }
    2523 
    25242529        if ( ! $append ) {
    25252530                $old_tt_ids = wp_get_object_terms(
  • trunk/tests/phpunit/tests/taxonomy.php

    r48356 r48480  
    10001000                );
    10011001
     1002                // Test default category.
    10021003                $term = wp_get_post_terms( $post_id, $tax );
    10031004                $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id );
     1005
     1006                // Test default term deletion.
     1007                $this->assertSame( wp_delete_term( $term[0]->term_id, $tax ), 0 );
    10041008
    10051009                // Add custom post type.
    … …  
    10181022                $term    = wp_get_post_terms( $post_id, $tax );
    10191023                $this->assertSame( get_option( 'default_taxonomy_' . $tax ), $term[0]->term_id );
     1024
     1025                // wp_set_object_terms shouldn't assign default category.
     1026                wp_set_object_terms( $post_id, array(), $tax );
     1027                $term = wp_get_post_terms( $post_id, $tax );
     1028                $this->assertSame( array(), $term );
     1029
     1030                unregister_taxonomy( $tax );
     1031                $this->assertSame( get_option( 'default_taxonomy_' . $tax ), false );
    10201032        }
    10211033}
Note: See TracChangeset for help on using the changeset viewer.