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

git-svn-id: https://develop.svn.wordpress.org/trunk@29039 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn 2014-07-09 16:03:17 +00:00
parent 21cd1dffd9
commit b3e3684d21
2 changed files with 111 additions and 8 deletions

View File

@ -4311,7 +4311,7 @@ class WP_Query {
*
* @since 3.1.0
*
* @param mixed $page Page ID, title, slug, or array of such.
* @param mixed $page Page ID, title, slug, path, or array of such.
* @return bool
*/
public function is_page( $page = '' ) {
@ -4325,12 +4325,24 @@ class WP_Query {
$page = (array) $page;
if ( in_array( $page_obj->ID, $page ) )
if ( in_array( $page_obj->ID, $page ) ) {
return true;
elseif ( in_array( $page_obj->post_title, $page ) )
} elseif ( in_array( $page_obj->post_title, $page ) ) {
return true;
else if ( in_array( $page_obj->post_name, $page ) )
} else if ( in_array( $page_obj->post_name, $page ) ) {
return true;
} else {
foreach ( $page as $pagepath ) {
if ( ! strpos( $pagepath, '/' ) ) {
continue;
}
$pagepath_obj = get_page_by_path( $pagepath );
if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
return true;
}
}
}
return false;
}
@ -4392,7 +4404,7 @@ class WP_Query {
*
* @since 3.1.0
*
* @param mixed $post Post ID, title, slug, or array of such.
* @param mixed $post Post ID, title, slug, path, or array of such.
* @return bool
*/
public function is_single( $post = '' ) {
@ -4406,13 +4418,24 @@ class WP_Query {
$post = (array) $post;
if ( in_array( $post_obj->ID, $post ) )
if ( in_array( $post_obj->ID, $post ) ) {
return true;
elseif ( in_array( $post_obj->post_title, $post ) )
} elseif ( in_array( $post_obj->post_title, $post ) ) {
return true;
elseif ( in_array( $post_obj->post_name, $post ) )
} elseif ( in_array( $post_obj->post_name, $post ) ) {
return true;
} else {
foreach ( $post as $postpath ) {
if ( ! strpos( $postpath, '/' ) ) {
continue;
}
$postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );
if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
return true;
}
}
}
return false;
}

View File

@ -692,6 +692,53 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
$this->assertTrue( is_single( $post->post_name ) );
}
/**
* @ticket 16802
*/
function test_is_single_with_parent() {
// Use custom hierarchical post type
$post_type = 'test_hierarchical';
register_post_type( $post_type, array(
'hierarchical' => true,
'rewrite' => true,
'has_archive' => true,
'public' => true
) );
// Create parent and child posts
$parent_id = $this->factory->post->create( array(
'post_type' => $post_type,
'post_name' => 'foo'
) );
$post_id = $this->factory->post->create( array(
'post_type' => $post_type,
'post_name' => 'bar',
'post_parent' => $parent_id
) );
// Tests
$this->go_to( "/?p=$post_id&post_type=$post_type" );
$post = get_queried_object();
$q = $GLOBALS['wp_query'];
$this->assertTrue( is_single() );
$this->assertFalse( $q->is_page );
$this->assertTrue( $q->is_single );
$this->assertFalse( $q->is_attachment );
$this->assertTrue( is_single( $post ) );
$this->assertTrue( is_single( $post->ID ) );
$this->assertTrue( is_single( $post->post_title ) );
$this->assertTrue( is_single( $post->post_name ) );
$this->assertTrue( is_single( 'foo/bar' ) );
$this->assertFalse( is_single( $parent_id ) );
$this->assertFalse( is_single( 'foo/bar/baz' ) );
$this->assertFalse( is_single( 'bar/bar' ) );
$this->assertFalse( is_single( 'foo' ) );
}
function test_is_page() {
$post_id = $this->factory->post->create( array( 'post_type' => 'page' ) );
$this->go_to( "/?page_id=$post_id" );
@ -709,6 +756,39 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
$this->assertTrue( is_page( $post->post_name ) );
}
/**
* @ticket 16802
*/
function test_is_page_with_parent() {
$parent_id = $this->factory->post->create( array(
'post_type' => 'page',
'post_name' => 'foo',
) );
$post_id = $this->factory->post->create( array(
'post_type' => 'page',
'post_name' => 'bar',
'post_parent' => $parent_id,
) );
$this->go_to( "/?page_id=$post_id" );
$post = get_queried_object();
$q = $GLOBALS['wp_query'];
$this->assertTrue( is_page() );
$this->assertFalse( $q->is_single );
$this->assertTrue( $q->is_page );
$this->assertFalse( $q->is_attachment );
$this->assertTrue( is_page( $post ) );
$this->assertTrue( is_page( $post->ID ) );
$this->assertTrue( is_page( $post->post_title ) );
$this->assertTrue( is_page( $post->post_name ) );
$this->assertTrue( is_page( 'foo/bar' ) );
$this->assertFalse( is_page( $parent_id ) );
$this->assertFalse( is_page( 'foo/bar/baz' ) );
$this->assertFalse( is_page( 'bar/bar' ) );
$this->assertFalse( is_page( 'foo' ) );
}
function test_is_attachment() {
$post_id = $this->factory->post->create( array( 'post_type' => 'attachment' ) );
$this->go_to( "/?attachment_id=$post_id" );