From 99b439c6f751d970e0a189da0afdadcf104dbb89 Mon Sep 17 00:00:00 2001 From: dd32 Date: Sun, 25 Apr 2010 07:35:16 +0000 Subject: [PATCH] Allow Terms to exist within multiple taxonomies (Regression in 3.0); If a term slug is provided on the form, Respect it (do not suffix it, instead, feedback with error); Do not allow hierarchical term names to exist multiple times within a set level (ie. AU/NSW + AU/NSW is bad, AU/NSW + AU/VIC + AU/VIC/NSW is ok). See #13060 git-svn-id: http://svn.automattic.com/wordpress/trunk@14229 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-ajax.php | 3 +-- wp-includes/taxonomy.php | 39 +++++++++++++++++++++++++++------------ 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/wp-admin/admin-ajax.php b/wp-admin/admin-ajax.php index 23aa1c16e..7c98208f3 100644 --- a/wp-admin/admin-ajax.php +++ b/wp-admin/admin-ajax.php @@ -572,9 +572,8 @@ case 'add-tag' : // From Manage->Tags $tag_full_name = $_tag->name . ' — ' . $tag_full_name; $level++; } - } - if ( is_taxonomy_hierarchical($taxonomy) ) $noparents = _tag_row( $tag, $level, $taxonomy ); + } $tag->name = $tag_full_name; $parents = _tag_row( $tag, 0, $taxonomy); diff --git a/wp-includes/taxonomy.php b/wp-includes/taxonomy.php index dc02a1777..938e58a56 100644 --- a/wp-includes/taxonomy.php +++ b/wp-includes/taxonomy.php @@ -1523,8 +1523,8 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { if ( empty($slug) ) $slug = sanitize_title($name); - elseif ( is_term($slug, $taxonomy) ) // Provided slug issue. - return new WP_Error('term_slug_exists', __('A Term with the slug provided already exists.')); + elseif ( is_term($slug) ) + return new WP_Error('term_slug_exists', __('A term with the slug provided already exists.')); $term_group = 0; if ( $alias_of ) { @@ -1541,22 +1541,37 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { } } - if ( ! $term_id = is_term($slug, $taxonomy) ) { - // Make sure the slug is unique accross all taxonomies. - $slug = wp_unique_term_slug($slug, (object) $args); - if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) - return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); - $term_id = (int) $wpdb->insert_id; - } else if ( is_taxonomy_hierarchical($taxonomy) && !empty($parent) ) { - // If the taxonomy supports hierarchy and the term has a parent, make the slug unique - // by incorporating parent slugs. + if ( $term_id = is_term($slug) ) { + $existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A ); + // We've got an existing term, which matches the name of the new term: + if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name ) { + // Heirarchical, and it matches an existing term, Do not allow same "name" in the same level. + $siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => (int)$parent) ); + if ( in_array($name, $siblings) ) { + return new WP_Error('term_exists', __('A term with the name provided already exists with this parent.')); + } else { + $slug = wp_unique_term_slug($slug, (object) $args); + if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) + return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); + $term_id = (int) $wpdb->insert_id; + } + } elseif ( $existing_term['name'] != $name ) { + // We've got an existing term, with a different name, Creat ethe new term. + $slug = wp_unique_term_slug($slug, (object) $args); + if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) + return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); + $term_id = (int) $wpdb->insert_id; + } + } else { + // This term does not exist at all in the database, Create it. $slug = wp_unique_term_slug($slug, (object) $args); if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) return new WP_Error('db_insert_error', __('Could not insert term into the database'), $wpdb->last_error); $term_id = (int) $wpdb->insert_id; } - if ( empty($slug) ) { + // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string. + if ( empty($slug) ) { $slug = sanitize_title($slug, $term_id); do_action( 'edit_terms', $term_id ); $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );