Make WordPress Core

Ticket #16802: 16802.diff

File 16802.diff, 3.6 KB (added by johnbillion, 11 years ago)
  • src/wp-includes/query.php

     
    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 = '' ) {
     
    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        }
     
    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 = '' ) {
     
    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;
    4415 
     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 );
     4433
     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

     
    709709                $this->assertTrue( is_page( $post->post_name ) );
    710710        }
    711711
     712        /**
     713         * @ticket 16802
     714         */
     715        function test_is_page_with_parent() {
     716                $parent_id = $this->factory->post->create( array(
     717                        'post_type' => 'page',
     718                        'post_name' => 'foo',
     719                ) );
     720                $post_id = $this->factory->post->create( array(
     721                        'post_type'   => 'page',
     722                        'post_name'   => 'bar',
     723                        'post_parent' => $parent_id,
     724                ) );
     725                $this->go_to( "/?page_id=$post_id" );
     726
     727                $post = get_queried_object();
     728                $q = $GLOBALS['wp_query'];
     729
     730                $this->assertTrue( is_page() );
     731                $this->assertFalse( $q->is_single );
     732                $this->assertTrue( $q->is_page );
     733                $this->assertFalse( $q->is_attachment );
     734                $this->assertTrue( is_page( $post ) );
     735                $this->assertTrue( is_page( $post->ID ) );
     736                $this->assertTrue( is_page( $post->post_title ) );
     737                $this->assertTrue( is_page( $post->post_name ) );
     738                $this->assertTrue( is_page( 'foo/bar' ) );
     739                $this->assertFalse( is_page( $parent_id ) );
     740                $this->assertFalse( is_page( 'foo/bar/baz' ) );
     741                $this->assertFalse( is_page( 'bar/bar' ) );
     742                $this->assertFalse( is_page( 'foo' ) );
     743        }
     744
    712745        function test_is_attachment() {
    713746                $post_id = $this->factory->post->create( array( 'post_type' => 'attachment' ) );
    714747                $this->go_to( "/?attachment_id=$post_id" );