Make WordPress Core

Ticket #16802: 16802.2.diff

File 16802.2.diff, 5.2 KB (added by engelen, 12 years ago)
  • src/wp-includes/query.php

    diff --git src/wp-includes/query.php src/wp-includes/query.php
    index cf7b80f..971e75f 100644
    class WP_Query {  
    43114311         *
    43124312         * @since 3.1.0
    43134313         *
    4314          * @param mixed $page Page ID, title, slug, or array of such.
     4314         * @param mixed $page Page ID, title, slug, path, or array of such.
    43154315         * @return bool
    43164316         */
    43174317        public function is_page( $page = '' ) {
    … … class WP_Query {  
    43254325
    43264326                $page = (array) $page;
    43274327
    4328                 if ( in_array( $page_obj->ID, $page ) )
     4328                if ( in_array( $page_obj->ID, $page ) ) {
    43294329                        return true;
    4330                 elseif ( in_array( $page_obj->post_title, $page ) )
     4330                } elseif ( in_array( $page_obj->post_title, $page ) ) {
    43314331                        return true;
    4332                 else if ( in_array( $page_obj->post_name, $page ) )
     4332                } else if ( in_array( $page_obj->post_name, $page ) ) {
    43334333                        return true;
     4334                } else {
     4335                        foreach ( $page as $pagepath ) {
     4336                                if ( ! strpos( $pagepath, '/' ) ) {
     4337                                        continue;
     4338                                }
     4339                                $pagepath_obj = get_page_by_path( $pagepath );
     4340
     4341                                if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
     4342                                        return true;
     4343                                }
     4344                        }
     4345                }
    43344346
    43354347                return false;
    43364348        }
    … … class WP_Query {  
    43924404         *
    43934405         * @since 3.1.0
    43944406         *
    4395          * @param mixed $post Post ID, title, slug, or array of such.
     4407         * @param mixed $post Post ID, title, slug, path, or array of such.
    43964408         * @return bool
    43974409         */
    43984410        public function is_single( $post = '' ) {
    … … class WP_Query {  
    44064418
    44074419                $post = (array) $post;
    44084420
    4409                 if ( in_array( $post_obj->ID, $post ) )
     4421                if ( in_array( $post_obj->ID, $post ) ) {
    44104422                        return true;
    4411                 elseif ( in_array( $post_obj->post_title, $post ) )
     4423                } elseif ( in_array( $post_obj->post_title, $post ) ) {
    44124424                        return true;
    4413                 elseif ( in_array( $post_obj->post_name, $post ) )
     4425                } elseif ( in_array( $post_obj->post_name, $post ) ) {
    44144426                        return true;
     4427                } else {
     4428                        foreach ( $post as $postpath ) {
     4429                                if ( ! strpos( $postpath, '/' ) ) {
     4430                                        continue;
     4431                                }
     4432                                $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );
    44154433
     4434                                if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
     4435                                        return true;
     4436                                }
     4437                        }
     4438                }
    44164439                return false;
    44174440        }
    44184441
  • tests/phpunit/tests/query/conditionals.php

    diff --git tests/phpunit/tests/query/conditionals.php tests/phpunit/tests/query/conditionals.php
    index 1af50ac..ded9ed7 100644
    class Tests_Query_Conditionals extends WP_UnitTestCase {  
    692692                $this->assertTrue( is_single( $post->post_name ) );
    693693        }
    694694
     695        function test_is_single_with_parent() {
     696                // Use custom hierarchical post type
     697                $post_type = rand_str( 20 );
     698
     699                register_post_type( $post_type, array(
     700                        'hierarchical' => true,
     701                        'rewrite' => true,
     702                        'has_archive' => true,
     703                        'public' => true
     704                ) );
     705
     706                // Create parent and child posts
     707                $parent_id = $this->factory->post->create( array(
     708                        'post_type' => $post_type,
     709                        'post_name' => 'foo'
     710                ) );
     711
     712                $post_id = $this->factory->post->create( array(
     713                        'post_type'   => $post_type,
     714                        'post_name'   => 'bar',
     715                        'post_parent' => $parent_id
     716                ) );
     717
     718                // Tests
     719                $this->go_to( "/?p=$post_id&post_type=$post_type" );
     720
     721                $post = get_queried_object();
     722                $q = $GLOBALS['wp_query'];
     723
     724                $this->assertTrue( is_single() );
     725                $this->assertFalse( $q->is_page );
     726                $this->assertTrue( $q->is_single );
     727                $this->assertFalse( $q->is_attachment );
     728                $this->assertTrue( is_single( $post ) );
     729                $this->assertTrue( is_single( $post->ID ) );
     730                $this->assertTrue( is_single( $post->post_title ) );
     731                $this->assertTrue( is_single( $post->post_name ) );
     732                $this->assertTrue( is_single( 'foo/bar' ) );
     733                $this->assertFalse( is_single( $parent_id ) );
     734                $this->assertFalse( is_single( 'foo/bar/baz' ) );
     735                $this->assertFalse( is_single( 'bar/bar' ) );
     736                $this->assertFalse( is_single( 'foo' ) );
     737        }
     738
    695739        function test_is_page() {
    696740                $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
    697741                $this->go_to( "/?page_id=$post_id" );
    … … class Tests_Query_Conditionals extends WP_UnitTestCase {  
    709753                $this->assertTrue( is_page( $post->post_name ) );
    710754        }
    711755
     756        function test_is_page_with_parent() {
     757                $parent_id = $this->factory->post->create( array(
     758                        'post_type' => 'page',
     759                        'post_name' => 'foo',
     760                ) );
     761                $post_id = $this->factory->post->create( array(
     762                        'post_type'   => 'page',
     763                        'post_name'   => 'bar',
     764                        'post_parent' => $parent_id,
     765                ) );
     766                $this->go_to( "/?page_id=$post_id" );
     767
     768                $post = get_queried_object();
     769                $q = $GLOBALS['wp_query'];
     770
     771                $this->assertTrue( is_page() );
     772                $this->assertFalse( $q->is_single );
     773                $this->assertTrue( $q->is_page );
     774                $this->assertFalse( $q->is_attachment );
     775                $this->assertTrue( is_page( $post ) );
     776                $this->assertTrue( is_page( $post->ID ) );
     777                $this->assertTrue( is_page( $post->post_title ) );
     778                $this->assertTrue( is_page( $post->post_name ) );
     779                $this->assertTrue( is_page( 'foo/bar' ) );
     780                $this->assertFalse( is_page( $parent_id ) );
     781                $this->assertFalse( is_page( 'foo/bar/baz' ) );
     782                $this->assertFalse( is_page( 'bar/bar' ) );
     783                $this->assertFalse( is_page( 'foo' ) );
     784        }
     785
    712786        function test_is_attachment() {
    713787                $post_id = $this->factory->post->create( array( 'post_type' => 'attachment' ) );
    714788                $this->go_to( "/?attachment_id=$post_id" );