From 0474a188635951d30300ed5bd46fb364595d6925 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 6 Sep 2013 17:26:04 +0000 Subject: [PATCH] Allow `is_tag()` to accept `term_id`, `slug`, 'term_name` or array of any. Many other `is_*()` funcs already do this. Adds unit tests. Props ramiy. Fixes #18746. git-svn-id: https://develop.svn.wordpress.org/trunk@25287 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/query.php | 22 +++++++++++++--------- tests/phpunit/tests/query/conditionals.php | 13 ++++++++++++- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index 0286072738..07838ab8b6 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -246,10 +246,10 @@ function is_category( $category = '' ) { * @since 2.3.0 * @uses $wp_query * - * @param mixed $slug Optional. Tag slug or array of slugs. + * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs. * @return bool */ -function is_tag( $slug = '' ) { +function is_tag( $tag = '' ) { global $wp_query; if ( ! isset( $wp_query ) ) { @@ -257,7 +257,7 @@ function is_tag( $slug = '' ) { return false; } - return $wp_query->is_tag( $slug ); + return $wp_query->is_tag( $tag ); } /** @@ -3238,21 +3238,25 @@ class WP_Query { * * @since 3.1.0 * - * @param mixed $slug Optional. Tag slug or array of slugs. + * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs. * @return bool */ - function is_tag( $slug = '' ) { - if ( !$this->is_tag ) + function is_tag( $tag = '' ) { + if ( ! $this->is_tag ) return false; - if ( empty( $slug ) ) + if ( empty( $tag ) ) return true; $tag_obj = $this->get_queried_object(); - $slug = (array) $slug; + $tag = (array) $tag; - if ( in_array( $tag_obj->slug, $slug ) ) + if ( in_array( $tag_obj->term_id, $tag ) ) + return true; + elseif ( in_array( $tag_obj->name, $tag ) ) + return true; + elseif ( in_array( $tag_obj->slug, $tag ) ) return true; return false; diff --git a/tests/phpunit/tests/query/conditionals.php b/tests/phpunit/tests/query/conditionals.php index 57cdc4aa67..eaf3ba3e70 100644 --- a/tests/phpunit/tests/query/conditionals.php +++ b/tests/phpunit/tests/query/conditionals.php @@ -428,9 +428,20 @@ class Tests_Query_Conditionals extends WP_UnitTestCase { // 'tag/(.+?)/?$' => 'index.php?tag=$matches[1]', function test_tag() { - $this->factory->term->create( array( 'name' => 'tag-a', 'taxonomy' => 'post_tag' ) ); + $term_id = $this->factory->term->create( array( 'name' => 'Tag Named A', 'slug' => 'tag-a', 'taxonomy' => 'post_tag' ) ); $this->go_to('/tag/tag-a/'); $this->assertQueryTrue('is_archive', 'is_tag'); + + $tag = get_term( $term_id, 'post_tag' ); + + $this->assertTrue( is_tag() ); + $this->assertTrue( is_tag( $tag->name ) ); + $this->assertTrue( is_tag( $tag->slug ) ); + $this->assertTrue( is_tag( $tag->term_id ) ); + $this->assertTrue( is_tag( array() ) ); + $this->assertTrue( is_tag( array( $tag->name ) ) ); + $this->assertTrue( is_tag( array( $tag->slug ) ) ); + $this->assertTrue( is_tag( array( $tag->term_id ) ) ); } // 'author/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?author_name=$matches[1]&feed=$matches[2]',