Make WordPress Core


Ignore:
Timestamp:
11/05/2014 01:41:58 AM (10 years ago)
Author:
boonebgorges
Message:

Do not create shared taxonomy terms.

A "shared" term occurs when two entries in the wp_term_taxonomy table share a
single term_id, and thereby correspond to the same row in wp_terms. This
changeset stops the practice of creating shared terms: each new row in
wp_term_taxonomy will receive its own row in wp_terms. The new strategy
for term creation depends on whether the installation's database schema is up
to date for 4.1:

  • If so, terms are allowed to be created with the same slug as an existing term, as long as they are in different taxonomies and do not share a parent. Thus, a new tag with the slug 'wordpress' can exist alongside a category with the slug 'wordpress'.
  • If not, new terms will be forced to have unique slugs. Thus, on an installation containing a category with the slug 'wordpress', a new tag 'WordPress' will get the slug 'wordpress-2'.

Fixes #21950. See #5809.

File:
1 edited

Legend:

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

    r30239 r30240  
    28462846    }
    28472847
    2848     if ( $term_id = term_exists($slug) ) {
    2849         $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A );
    2850         // We've got an existing term in the same taxonomy, which matches the name of the new term:
    2851         if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {
    2852             // Hierarchical, and it matches an existing term, Do not allow same "name" in the same level.
    2853             $siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
    2854             if ( in_array($name, $siblings) ) {
    2855                 if ( $slug_provided ) {
    2856                     return new WP_Error( 'term_exists', __( 'A term with the name and slug provided already exists with this parent.' ), $exists['term_id'] );
    2857                 } else {
    2858                     return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $exists['term_id'] );
     2848    // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy.
     2849    if ( $exists = term_exists( $slug, $taxonomy ) ) {
     2850        $existing_term = get_term( $exists['term_id'], $taxonomy );
     2851
     2852        if ( $name === $existing_term->name ) {
     2853
     2854            if ( is_taxonomy_hierarchical( $taxonomy ) ) {
     2855                $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
     2856                if ( in_array( $name, $siblings ) ) {
     2857                    return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists with this parent.' ), $exists['term_id'] );
    28592858                }
     2859
    28602860            } else {
    2861                 $slug = wp_unique_term_slug($slug, (object) $args);
    2862                 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) {
    2863                     return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
    2864                 }
    2865                 $term_id = (int) $wpdb->insert_id;
     2861                return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists in this taxonomy.' ), $exists['term_id'] );
    28662862            }
    2867         } elseif ( $existing_term['name'] != $name ) {
    2868             // We've got an existing term, with a different name, Create the new term.
    2869             $slug = wp_unique_term_slug($slug, (object) $args);
    2870             if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) {
    2871                 return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
    2872             }
    2873             $term_id = (int) $wpdb->insert_id;
    2874         } elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) )  {
    2875             // Same name, same slug.
    2876             return new WP_Error( 'term_exists', __( 'A term with the name and slug provided already exists.' ), $exists['term_id'] );
    2877         }
    2878     } else {
    2879         // This term does not exist at all in the database, Create it.
    2880         $slug = wp_unique_term_slug($slug, (object) $args);
    2881         if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) {
    2882             return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error);
    2883         }
    2884         $term_id = (int) $wpdb->insert_id;
    2885     }
     2863        }
     2864    }
     2865
     2866    $slug = wp_unique_term_slug( $slug, (object) $args );
     2867
     2868    if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) {
     2869        return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error );
     2870    }
     2871
     2872    $term_id = (int) $wpdb->insert_id;
    28862873
    28872874    // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.
     
    32303217    if ( ! term_exists( $slug ) )
    32313218        return $slug;
     3219
     3220    // As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
     3221    if ( get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
     3222        return $slug;
     3223    }
    32323224
    32333225    // If the taxonomy supports hierarchy and the term has a parent, make the slug unique
Note: See TracChangeset for help on using the changeset viewer.