WordPress.org

Make WordPress Core

Changeset 39599


Ignore:
Timestamp:
12/14/16 03:42:58 (3 months ago)
Author:
johnbillion
Message:

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

Props natereist, dlh
Fixes #39211

Location:
trunk
Files:
2 edited

Legend:

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

    r38951 r39599  
    16321632 */ 
    16331633function is_page_template( $template = '' ) { 
     1634    if ( ! is_singular() ) { 
     1635        return false; 
     1636    } 
     1637 
    16341638    $page_template = get_page_template_slug( get_queried_object_id() ); 
    16351639 
  • trunk/tests/phpunit/tests/query/conditionals.php

    r39095 r39599  
    10751075 
    10761076    /** 
     1077     * @ticket 39211 
     1078     */ 
     1079    function test_is_page_template_not_singular() { 
     1080        global $wpdb; 
     1081 
     1082        // We need a non-post that shares an ID with a post assigned a template. 
     1083        $user_id = self::factory()->user->create(); 
     1084        if ( ! get_post( $user_id ) ) { 
     1085            $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) ); 
     1086            $wpdb->update( $wpdb->posts, array( 'ID' => $user_id ), array( 'ID' => $post_id ), array( '%d' ) ); 
     1087        } 
     1088 
     1089        update_post_meta( $user_id, '_wp_page_template', 'example.php' ); 
     1090 
     1091        // Verify that the post correctly reports having a template. 
     1092        $this->go_to( get_post_permalink( $user_id ) ); 
     1093        $this->assertInstanceOf( 'WP_Post', get_queried_object() ); 
     1094        $this->assertTrue( is_page_template( 'example.php' ) ); 
     1095 
     1096        // Verify that the non-post with a matching ID does not report having a template. 
     1097        $this->go_to( get_author_posts_url( $user_id ) ); 
     1098        $this->assertInstanceOf( 'WP_User', get_queried_object() ); 
     1099        $this->assertFalse( is_page_template( 'example.php' ) ); 
     1100    } 
     1101 
     1102    /** 
    10771103     * @ticket 35902 
    10781104     */ 
Note: See TracChangeset for help on using the changeset viewer.