From adbed947d85f25cbdfaf3041f50229c08aee53ed Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 9 Oct 2014 03:15:50 +0000 Subject: [PATCH] Don't bail out of term_exists() when term sanitizes to an empty string. This change brings return values for failures of this sort in line with other failed lookups in term_exists(): a null value is now returned in all cases where the queried term is not found. Adds unit test for the sanitization issue. Modifies existing unit test to reflect the change in return value for empty term strings. Props boonebgorges, georgestephanis. Fixes #29589. git-svn-id: https://develop.svn.wordpress.org/trunk@29865 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 11 +++++------ tests/phpunit/tests/term.php | 15 ++++++++++++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 0e29545bab..f73b9666cd 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1656,9 +1656,10 @@ function get_terms( $taxonomies, $args = '' ) { * @param int|string $term The term to check * @param string $taxonomy The taxonomy name to use * @param int $parent Optional. ID of parent term under which to confine the exists search. - * @return mixed Returns 0 if the term does not exist. Returns the term ID if no taxonomy is specified - * and the term ID exists. Returns an array of the term ID and the term taxonomy ID - * if the taxonomy is specified and the pairing exists. + * @return mixed Returns null if the term does not exist. Returns the term ID + * if no taxonomy is specified and the term ID exists. Returns + * an array of the term ID and the term taxonomy ID the taxonomy + * is specified and the pairing exists. */ function term_exists( $term, $taxonomy = '', $parent = null ) { global $wpdb; @@ -1677,9 +1678,7 @@ function term_exists( $term, $taxonomy = '', $parent = null ) { } $term = trim( wp_unslash( $term ) ); - - if ( '' === $slug = sanitize_title($term) ) - return 0; + $slug = sanitize_title( $term ); $where = 't.slug = %s'; $else_where = 't.name = %s'; diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 9b0a92ad68..ef4051765e 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -112,7 +112,20 @@ class Tests_Term extends WP_UnitTestCase { } public function test_term_exists_term_trimmed_to_empty_string() { - $this->assertSame( 0, term_exists( ' ' ) ); + $this->assertNull( term_exists( ' ' ) ); + } + + /** + * @ticket 29589 + */ + public function test_term_exists_existing_term_that_sanitizes_to_empty() { + wp_insert_term( '//', 'category' ); + $this->assertNotEmpty( term_exists( '//' ) ); + $this->assertNotEmpty( term_exists( '//', 'category' ) ); + + wp_insert_term( '>>', 'category' ); + $this->assertNotEmpty( term_exists( '>>' ) ); + $this->assertNotEmpty( term_exists( '>>', 'category' ) ); } public function test_term_exists_taxonomy_nonempty_parent_nonempty_match_slug() {