Make WordPress Core

Ticket #24257: 24257.diff

File 24257.diff, 3.0 KB (added by wonderboymusic, 11 years ago)
  • src/wp-includes/query.php

     
    175175 * @since 2.0.0
    176176 * @uses $wp_query
    177177 *
     178 * @param mixed $attachment Attachment ID, title, slug, or array of such.
    178179 * @return bool
    179180 */
    180 function is_attachment() {
     181function is_attachment( $attachment = '' ) {
    181182        global $wp_query;
    182183
    183184        if ( ! isset( $wp_query ) ) {
     
    185186                return false;
    186187        }
    187188
    188         return $wp_query->is_attachment();
     189        return $wp_query->is_attachment( $attachment );
    189190}
    190191
    191192/**
     
    33873388         *
    33883389         * @since 3.1.0
    33893390         *
     3391         * @param mixed $attachment Attachment ID, title, slug, or array of such.
    33903392         * @return bool
    33913393         */
    3392         function is_attachment() {
    3393                 return (bool) $this->is_attachment;
     3394        function is_attachment( $attachment = '' ) {
     3395                if ( ! $this->is_attachment ) {
     3396                        return false;
     3397                }
     3398
     3399                if ( empty( $attachment ) ) {
     3400                        return true;
     3401                }
     3402
     3403                $attachment = (array) $attachment;
     3404
     3405                $post_obj = $this->get_queried_object();
     3406
     3407                if ( in_array( $post_obj->ID, $attachment ) ) {
     3408                        return true;
     3409                } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
     3410                        return true;
     3411                } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
     3412                        return true;
     3413                }
     3414                return false;
    33943415        }
    33953416
    33963417        /**
  • tests/phpunit/tests/query/conditionals.php

     
    670670        function pre_get_posts_with_type_array( &$query ) {
    671671                $query->set( 'post_type', array( 'post', 'thearray' ) );
    672672        }
     673
     674        function test_is_single() {
     675                $post_id = $this->factory->post->create();
     676                $this->go_to( "/?p=$post_id" );
     677
     678                $post = get_queried_object();
     679
     680                $this->assertTrue( is_single() );
     681                $this->assertTrue( is_single( $post ) );
     682                $this->assertTrue( is_single( $post->ID ) );
     683                $this->assertTrue( is_single( $post->post_title ) );
     684                $this->assertTrue( is_single( $post->post_name ) );
     685        }
     686
     687        function test_is_page() {
     688                $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
     689                $this->go_to( "/?page_id=$post_id" );
     690
     691                $post = get_queried_object();
     692
     693                $this->assertTrue( is_page() );
     694                $this->assertTrue( is_page( $post ) );
     695                $this->assertTrue( is_page( $post->ID ) );
     696                $this->assertTrue( is_page( $post->post_title ) );
     697                $this->assertTrue( is_page( $post->post_name ) );
     698        }
     699
     700        function test_is_attachment() {
     701                $post_id = $this->factory->post->create( array( 'post_type' => 'attachment' ) );
     702                $this->go_to( "/?attachment_id=$post_id" );
     703
     704                $post = get_queried_object();
     705
     706                $this->assertTrue( is_attachment() );
     707                $this->assertTrue( is_attachment( $post ) );
     708                $this->assertTrue( is_attachment( $post->ID ) );
     709                $this->assertTrue( is_attachment( $post->post_title ) );
     710                $this->assertTrue( is_attachment( $post->post_name ) );
     711        }
    673712}