Make WordPress Core

Changeset 25287


Ignore:
Timestamp:
09/06/2013 05:26:04 PM (11 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/query.php

    r25280 r25287  
    247247 * @uses $wp_query
    248248 *
    249  * @param mixed $slug Optional. Tag slug or array of slugs.
     249 * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
    250250 * @return bool
    251251 */
    252 function is_tag( $slug = '' ) {
     252function is_tag( $tag = '' ) {
    253253    global $wp_query;
    254254
     
    258258    }
    259259
    260     return $wp_query->is_tag( $slug );
     260    return $wp_query->is_tag( $tag );
    261261}
    262262
     
    32393239     * @since 3.1.0
    32403240     *
    3241      * @param mixed $slug Optional. Tag slug or array of slugs.
     3241     * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
    32423242     * @return bool
    32433243     */
    3244     function is_tag( $slug = '' ) {
    3245         if ( !$this->is_tag )
     3244    function is_tag( $tag = '' ) {
     3245        if ( ! $this->is_tag )
    32463246            return false;
    32473247
    3248         if ( empty( $slug ) )
     3248        if ( empty( $tag ) )
    32493249            return true;
    32503250
    32513251        $tag_obj = $this->get_queried_object();
    32523252
    3253         $slug = (array) $slug;
    3254 
    3255         if ( in_array( $tag_obj->slug, $slug ) )
     3253        $tag = (array) $tag;
     3254
     3255        if ( in_array( $tag_obj->term_id, $tag ) )
     3256            return true;
     3257        elseif ( in_array( $tag_obj->name, $tag ) )
     3258            return true;
     3259        elseif ( in_array( $tag_obj->slug, $tag ) )
    32563260            return true;
    32573261
  • trunk/tests/phpunit/tests/query/conditionals.php

    r25280 r25287  
    429429    // 'tag/(.+?)/?$' => 'index.php?tag=$matches[1]',
    430430    function test_tag() {
    431         $this->factory->term->create( array( 'name' => 'tag-a', 'taxonomy' => 'post_tag' ) );
     431        $term_id = $this->factory->term->create( array( 'name' => 'Tag Named A', 'slug' => 'tag-a', 'taxonomy' => 'post_tag' ) );
    432432        $this->go_to('/tag/tag-a/');
    433433        $this->assertQueryTrue('is_archive', 'is_tag');
     434
     435        $tag = get_term( $term_id, 'post_tag' );
     436
     437        $this->assertTrue( is_tag() );
     438        $this->assertTrue( is_tag( $tag->name ) );
     439        $this->assertTrue( is_tag( $tag->slug ) );
     440        $this->assertTrue( is_tag( $tag->term_id ) );
     441        $this->assertTrue( is_tag( array() ) );
     442        $this->assertTrue( is_tag( array( $tag->name ) ) );
     443        $this->assertTrue( is_tag( array( $tag->slug ) ) );
     444        $this->assertTrue( is_tag( array( $tag->term_id ) ) );
    434445    }
    435446
Note: See TracChangeset for help on using the changeset viewer.