Make WordPress Core

Changeset 29039


Ignore:
Timestamp:
07/09/2014 04:03:17 PM (12 years ago)
Author:
johnbillion
Message:

Add support for a full path parameter to is_page() and is_single(). Props Jesper800, engelen, johnbillion. Fixes #16802.

Location:
trunk
Files:
2 edited

Legend:

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

    r29027 r29039  
    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         */
    … …  
    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;
    … …  
    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         */
    … …  
    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        }
  • trunk/tests/phpunit/tests/query/conditionals.php

    r28966 r29039  
    693693        }
    694694
     695        /**
     696         * @ticket 16802
     697         */
     698        function test_is_single_with_parent() {
     699                // Use custom hierarchical post type
     700                $post_type = 'test_hierarchical';
     701
     702                register_post_type( $post_type, array(
     703                        'hierarchical' => true,
     704                        'rewrite'      => true,
     705                        'has_archive'  => true,
     706                        'public'       => true
     707                ) );
     708
     709                // Create parent and child posts
     710                $parent_id = $this->factory->post->create( array(
     711                        'post_type' => $post_type,
     712                        'post_name' => 'foo'
     713                ) );
     714
     715                $post_id = $this->factory->post->create( array(
     716                        'post_type'   => $post_type,
     717                        'post_name'   => 'bar',
     718                        'post_parent' => $parent_id
     719                ) );
     720
     721                // Tests
     722                $this->go_to( "/?p=$post_id&post_type=$post_type" );
     723
     724                $post = get_queried_object();
     725                $q = $GLOBALS['wp_query'];
     726
     727                $this->assertTrue( is_single() );
     728                $this->assertFalse( $q->is_page );
     729                $this->assertTrue( $q->is_single );
     730                $this->assertFalse( $q->is_attachment );
     731                $this->assertTrue( is_single( $post ) );
     732                $this->assertTrue( is_single( $post->ID ) );
     733                $this->assertTrue( is_single( $post->post_title ) );
     734                $this->assertTrue( is_single( $post->post_name ) );
     735                $this->assertTrue( is_single( 'foo/bar' ) );
     736                $this->assertFalse( is_single( $parent_id ) );
     737                $this->assertFalse( is_single( 'foo/bar/baz' ) );
     738                $this->assertFalse( is_single( 'bar/bar' ) );
     739                $this->assertFalse( is_single( 'foo' ) );
     740        }
     741
    695742        function test_is_page() {
    696743                $post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
    … …  
    708755                $this->assertTrue( is_page( $post->post_title ) );
    709756                $this->assertTrue( is_page( $post->post_name ) );
     757        }
     758
     759        /**
     760         * @ticket 16802
     761         */
     762        function test_is_page_with_parent() {
     763                $parent_id = $this->factory->post->create( array(
     764                        'post_type' => 'page',
     765                        'post_name' => 'foo',
     766                ) );
     767                $post_id = $this->factory->post->create( array(
     768                        'post_type'   => 'page',
     769                        'post_name'   => 'bar',
     770                        'post_parent' => $parent_id,
     771                ) );
     772                $this->go_to( "/?page_id=$post_id" );
     773
     774                $post = get_queried_object();
     775                $q = $GLOBALS['wp_query'];
     776
     777                $this->assertTrue( is_page() );
     778                $this->assertFalse( $q->is_single );
     779                $this->assertTrue( $q->is_page );
     780                $this->assertFalse( $q->is_attachment );
     781                $this->assertTrue( is_page( $post ) );
     782                $this->assertTrue( is_page( $post->ID ) );
     783                $this->assertTrue( is_page( $post->post_title ) );
     784                $this->assertTrue( is_page( $post->post_name ) );
     785                $this->assertTrue( is_page( 'foo/bar' ) );
     786                $this->assertFalse( is_page( $parent_id ) );
     787                $this->assertFalse( is_page( 'foo/bar/baz' ) );
     788                $this->assertFalse( is_page( 'bar/bar' ) );
     789                $this->assertFalse( is_page( 'foo' ) );
    710790        }
    711791
Note: See TracChangeset for help on using the changeset viewer.