Make WordPress Core


Ignore:
Timestamp:
03/16/2015 11:15:34 AM (11 years ago)
Author:
boonebgorges
Message:

In wp_insert_term(), allow a term with an existing name if a unique $slug has been provided.

wp_insert_term() protects against the creation of terms with duplicate names
at the same level of a taxonomy hierarchy. However, it's historically been
possible to override this protection by explicitly providing a value of $slug
that is unique at the hierarchy tier. This ability was broken in [31734], and
the current changeset restores the original behavior.

A number of unit tests are added and refactored in support of these changes.

See #17689 for discussion of a fix that was superceded by [31734]. This commit
retains the fix for the underlying bug described in that ticket.

See #31328.

File:
1 edited

Legend:

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

    r31734 r31792  
    29042904        }
    29052905
    2906         // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy.
    2907         if ( $existing_term = get_term_by( 'name', $name, $taxonomy ) ) {
    2908                 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
    2909                         $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
    2910                         if ( in_array( $name, $siblings ) ) {
    2911                                 return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id );
     2906        /*
     2907         * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
     2908         * unless a unique slug has been explicitly provided.
     2909         */
     2910        if ( $name_match = get_term_by( 'name', $name, $taxonomy ) ) {
     2911                $slug_match = get_term_by( 'slug', $slug, $taxonomy );
     2912                if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
     2913                        if ( is_taxonomy_hierarchical( $taxonomy ) ) {
     2914                                $siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) );
     2915
     2916                                $existing_term = null;
     2917                                if ( $name_match->slug === $slug && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) {
     2918                                        $existing_term = $name_match;
     2919                                } elseif ( $slug_match && in_array( $slug, wp_list_pluck( $siblings, 'slug' ) ) ) {
     2920                                        $existing_term = $slug_match;
     2921                                }
     2922
     2923                                if ( $existing_term ) {
     2924                                        return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id );
     2925                                }
     2926                        } else {
     2927                                return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $name_match->term_id );
    29122928                        }
    2913                 } else {
    2914                         return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $existing_term->term_id );
    29152929                }
    29162930        }
Note: See TracChangeset for help on using the changeset viewer.