Posts, Post Types: Ensure `is_page_template()` can only return true when viewing a singular post query.

Props natereist, dlh, johnbillion.
Merges [39599] to the 4.7 branch.
Fixes #39211.


git-svn-id: https://develop.svn.wordpress.org/branches/4.7@39608 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2016-12-16 05:40:01 +00:00
parent c41a10ecb1
commit 4d3fb455f5
2 changed files with 30 additions and 0 deletions

View File

@ -1631,6 +1631,10 @@ function get_the_password_form( $post = 0 ) {
* @return bool True on success, false on failure.
*/
function is_page_template( $template = '' ) {
if ( ! is_singular() ) {
return false;
}
$page_template = get_page_template_slug( get_queried_object_id() );
if ( empty( $template ) )

View File

@ -1073,6 +1073,32 @@ class Tests_Query_Conditionals extends WP_UnitTestCase {
$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
*/