From 28625bfc3824eedfcc2cbf0f8896e23ecc7b1d50 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Sat, 3 Oct 2015 20:25:26 +0000 Subject: [PATCH] When creating terms, avoid false dupe checks due to accented characters. `wp_insert_term()` doesn't allow the creation of a term when the term `name` is the same as another term in the same hierarchy level of the same taxonomy. Previously, this duplicate check used `get_term_by( 'name' )`, which uses the database collation to determine sameness. But common collations do not distinguish between accented and non-accented versions of a character. As a result, it was impossible to create a term 'Foo' if a sibling term with an accented character existed. We address this problem by using `get_terms()` to do the duplicate check. This query returns all potentially matching terms. We then do a stricter check for equivalence in PHP, before determining whether one of the matches is indeed a duplicate. Props boonebgorges, tyxla, geza.miklo, mehulkaklotar. Fixes #33864. Built from https://develop.svn.wordpress.org/trunk@34809 git-svn-id: http://core.svn.wordpress.org/trunk@34774 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/taxonomy-functions.php | 21 ++++++++++++++++++++- wp-includes/version.php | 2 +- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/wp-includes/taxonomy-functions.php b/wp-includes/taxonomy-functions.php index 8533641a1e..0692f6b833 100644 --- a/wp-includes/taxonomy-functions.php +++ b/wp-includes/taxonomy-functions.php @@ -2516,7 +2516,26 @@ function wp_insert_term( $term, $taxonomy, $args = array() ) { * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy, * unless a unique slug has been explicitly provided. */ - if ( $name_match = get_term_by( 'name', $name, $taxonomy ) ) { + $name_matches = get_terms( $taxonomy, array( + 'name' => $name, + 'hide_empty' => false, + ) ); + + /* + * The `name` match in `get_terms()` doesn't differentiate accented characters, + * so we do a stricter comparison here. + */ + $name_match = null; + if ( $name_matches ) { + foreach ( $name_matches as $_match ) { + if ( strtolower( $name ) === strtolower( $_match->name ) ) { + $name_match = $_match; + break; + } + } + } + + if ( $name_match ) { $slug_match = get_term_by( 'slug', $slug, $taxonomy ); if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) { if ( is_taxonomy_hierarchical( $taxonomy ) ) { diff --git a/wp-includes/version.php b/wp-includes/version.php index 703c632b93..ee45fb830a 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-34808'; +$wp_version = '4.4-alpha-34809'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.