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. Built from https://develop.svn.wordpress.org/trunk@30240 git-svn-id: http://core.svn.wordpress.org/trunk@30240 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
ef13be6f27
commit
32db3d1fe5
|
@ -2845,45 +2845,32 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $term_id = term_exists($slug) ) {
|
// Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy.
|
||||||
$existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name FROM $wpdb->terms WHERE term_id = %d", $term_id), ARRAY_A );
|
if ( $exists = term_exists( $slug, $taxonomy ) ) {
|
||||||
// We've got an existing term in the same taxonomy, which matches the name of the new term:
|
$existing_term = get_term( $exists['term_id'], $taxonomy );
|
||||||
if ( is_taxonomy_hierarchical($taxonomy) && $existing_term['name'] == $name && $exists = term_exists( (int) $term_id, $taxonomy ) ) {
|
|
||||||
// Hierarchical, and it matches an existing term, Do not allow same "name" in the same level.
|
if ( $name === $existing_term->name ) {
|
||||||
$siblings = get_terms($taxonomy, array('fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
|
|
||||||
if ( in_array($name, $siblings) ) {
|
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
|
||||||
if ( $slug_provided ) {
|
$siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
|
||||||
return new WP_Error( 'term_exists', __( 'A term with the name and slug provided already exists with this parent.' ), $exists['term_id'] );
|
if ( in_array( $name, $siblings ) ) {
|
||||||
} else {
|
return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists with this parent.' ), $exists['term_id'] );
|
||||||
return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $exists['term_id'] );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$slug = wp_unique_term_slug($slug, (object) $args);
|
return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists in this taxonomy.' ), $exists['term_id'] );
|
||||||
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, Create the 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;
|
|
||||||
} elseif ( $exists = term_exists( (int) $term_id, $taxonomy ) ) {
|
|
||||||
// Same name, same slug.
|
|
||||||
return new WP_Error( 'term_exists', __( 'A term with the name and slug provided already exists.' ), $exists['term_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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$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;
|
||||||
|
|
||||||
// Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.
|
// Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.
|
||||||
if ( empty($slug) ) {
|
if ( empty($slug) ) {
|
||||||
$slug = sanitize_title($slug, $term_id);
|
$slug = sanitize_title($slug, $term_id);
|
||||||
|
@ -3230,6 +3217,11 @@ function wp_unique_term_slug($slug, $term) {
|
||||||
if ( ! term_exists( $slug ) )
|
if ( ! term_exists( $slug ) )
|
||||||
return $slug;
|
return $slug;
|
||||||
|
|
||||||
|
// As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
|
||||||
|
if ( get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
|
||||||
|
return $slug;
|
||||||
|
}
|
||||||
|
|
||||||
// If the taxonomy supports hierarchy and the term has a parent, make the slug unique
|
// If the taxonomy supports hierarchy and the term has a parent, make the slug unique
|
||||||
// by incorporating parent slugs.
|
// by incorporating parent slugs.
|
||||||
if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
|
if ( is_taxonomy_hierarchical($term->taxonomy) && !empty($term->parent) ) {
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '4.1-alpha-30239';
|
$wp_version = '4.1-alpha-30240';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue