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
This commit is contained in:
Scott Taylor 2013-09-06 17:26:04 +00:00
parent c4068bc95b
commit 0474a18863
2 changed files with 25 additions and 10 deletions

View File

@ -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;

View File

@ -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]',