Posts, Post Types: Ensure is_page_template()
can only return true when viewing a singular post query.
Props natereist, dlh Fixes #39211 git-svn-id: https://develop.svn.wordpress.org/trunk@39599 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
2642833293
commit
c0bb5e4972
@ -1631,6 +1631,10 @@ function get_the_password_form( $post = 0 ) {
|
|||||||
* @return bool True on success, false on failure.
|
* @return bool True on success, false on failure.
|
||||||
*/
|
*/
|
||||||
function is_page_template( $template = '' ) {
|
function is_page_template( $template = '' ) {
|
||||||
|
if ( ! is_singular() ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$page_template = get_page_template_slug( get_queried_object_id() );
|
$page_template = get_page_template_slug( get_queried_object_id() );
|
||||||
|
|
||||||
if ( empty( $template ) )
|
if ( empty( $template ) )
|
||||||
|
@ -1073,6 +1073,32 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
|
|||||||
$this->assertTrue( is_page_template( array( 'test.php', 'example.php' ) ) );
|
$this->assertTrue( is_page_template( array( 'test.php', 'example.php' ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ticket 39211
|
||||||
|
*/
|
||||||
|
function test_is_page_template_not_singular() {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
// We need a non-post that shares an ID with a post assigned a template.
|
||||||
|
$user_id = self::factory()->user->create();
|
||||||
|
if ( ! get_post( $user_id ) ) {
|
||||||
|
$post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
|
||||||
|
$wpdb->update( $wpdb->posts, array( 'ID' => $user_id ), array( 'ID' => $post_id ), array( '%d' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
update_post_meta( $user_id, '_wp_page_template', 'example.php' );
|
||||||
|
|
||||||
|
// Verify that the post correctly reports having a template.
|
||||||
|
$this->go_to( get_post_permalink( $user_id ) );
|
||||||
|
$this->assertInstanceOf( 'WP_Post', get_queried_object() );
|
||||||
|
$this->assertTrue( is_page_template( 'example.php' ) );
|
||||||
|
|
||||||
|
// Verify that the non-post with a matching ID does not report having a template.
|
||||||
|
$this->go_to( get_author_posts_url( $user_id ) );
|
||||||
|
$this->assertInstanceOf( 'WP_User', get_queried_object() );
|
||||||
|
$this->assertFalse( is_page_template( 'example.php' ) );
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ticket 35902
|
* @ticket 35902
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user