Make WordPress Core

Ticket #18375: 18375.diff

File 18375.diff, 24.7 KB (added by swissspidy, 9 years ago)
  • src/wp-admin/edit-form-advanced.php

    diff --git src/wp-admin/edit-form-advanced.php src/wp-admin/edit-form-advanced.php
    index 75c3cef..0a1f886 100644
    foreach ( get_object_taxonomies( $post ) as $tax_name ) { 
    259259        add_meta_box( $tax_meta_box_id, $label, $taxonomy->meta_box_cb, null, 'side', 'core', array( 'taxonomy' => $tax_name ) );
    260260}
    261261
    262 if ( post_type_supports($post_type, 'page-attributes') )
    263         add_meta_box('pageparentdiv', 'page' == $post_type ? __('Page Attributes') : __('Attributes'), 'page_attributes_meta_box', null, 'side', 'core');
     262if ( post_type_supports( $post_type, 'page-attributes' ) ) {
     263        add_meta_box( 'pageparentdiv', $post_type_object->labels->attributes, 'page_attributes_meta_box', null, 'side', 'core' );
     264}
    264265
    265266if ( $thumbnail_support && current_user_can( 'upload_files' ) )
    266267        add_meta_box('postimagediv', esc_html( $post_type_object->labels->featured_image ), 'post_thumbnail_meta_box', null, 'side', 'low');
  • src/wp-admin/includes/class-wp-posts-list-table.php

    diff --git src/wp-admin/includes/class-wp-posts-list-table.php src/wp-admin/includes/class-wp-posts-list-table.php
    index 7ef790e..08a6730 100644
    class WP_Posts_List_Table extends WP_List_Table { 
    15231523                        </label>
    15241524
    15251525        <?php   endif; // !$bulk
    1526 
    1527                         if ( 'page' === $screen->post_type ) :
    15281526        ?>
    15291527
    15301528                        <label>
    class WP_Posts_List_Table extends WP_List_Table { 
    15381536                                        $default_title = apply_filters( 'default_page_template_title',  __( 'Default Template' ), 'quick-edit' );
    15391537                                ?>
    15401538                                        <option value="default"><?php echo esc_html( $default_title ); ?></option>
    1541                                         <?php page_template_dropdown() ?>
     1539                                        <?php page_template_dropdown( $post->page_template, $screen->post_type ) ?>
    15421540                                </select>
    15431541                        </label>
    15441542
    15451543        <?php
    1546                         endif; // page post_type
    15471544                endif; // page-attributes
    15481545        ?>
    15491546
  • src/wp-admin/includes/meta-boxes.php

    diff --git src/wp-admin/includes/meta-boxes.php src/wp-admin/includes/meta-boxes.php
    index 2d12faf..5915777 100644
    function post_revisions_meta_box( $post ) { 
    785785 * @param object $post
    786786 */
    787787function page_attributes_meta_box($post) {
    788         $post_type_object = get_post_type_object($post->post_type);
    789         if ( $post_type_object->hierarchical ) {
     788        if ( is_post_type_hierarchical( $post->post_type ) ) {
    790789                $dropdown_args = array(
    791790                        'post_type'        => $post->post_type,
    792791                        'exclude_tree'     => $post->ID,
    function page_attributes_meta_box($post) { 
    817816<?php
    818817                } // end empty pages check
    819818        } // end hierarchical check.
    820         if ( 'page' == $post->post_type && 0 != count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID ) {
    821                 $template = !empty($post->page_template) ? $post->page_template : false;
     819
     820        if ( 0 < count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID ) {
     821                $template = ! empty( $post->page_template ) ? $post->page_template : false;
    822822                ?>
    823823<p><strong><?php _e('Template') ?></strong><?php
    824824        /**
    function page_attributes_meta_box($post) { 
    832832         */
    833833        do_action( 'page_attributes_meta_box_template', $template, $post );
    834834?></p>
    835 <label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
     835<label class="screen-reader-text" for="page_template"><?php _e('Template') ?></label><select name="page_template" id="page_template">
    836836<?php
    837837/**
    838838 * Filters the title of the default page template displayed in the drop-down.
    function page_attributes_meta_box($post) { 
    846846$default_title = apply_filters( 'default_page_template_title',  __( 'Default Template' ), 'meta-box' );
    847847?>
    848848<option value="default"><?php echo esc_html( $default_title ); ?></option>
    849 <?php page_template_dropdown($template); ?>
     849<?php page_template_dropdown( $template, $post->post_type ); ?>
    850850</select>
    851851<?php
    852852        } ?>
  • src/wp-admin/includes/template.php

    diff --git src/wp-admin/includes/template.php src/wp-admin/includes/template.php
    index 4eac951..2a75523 100644
    function get_inline_data($post) { 
    293293        <div class="ss">' . mysql2date( 's', $post->post_date, false ) . '</div>
    294294        <div class="post_password">' . esc_html( $post->post_password ) . '</div>';
    295295
    296         if ( $post_type_object->hierarchical )
     296        if ( $post_type_object->hierarchical ) {
    297297                echo '<div class="post_parent">' . $post->post_parent . '</div>';
     298        }
    298299
    299         if ( $post->post_type == 'page' )
    300                 echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>';
     300        echo '<div class="page_template">' . esc_html( $post->page_template ) . '</div>';
    301301
    302         if ( post_type_supports( $post->post_type, 'page-attributes' ) )
     302        if ( post_type_supports( $post->post_type, 'page-attributes' ) ) {
    303303                echo '<div class="menu_order">' . $post->menu_order . '</div>';
     304        }
    304305
    305306        $taxonomy_names = get_object_taxonomies( $post->post_type );
    306307        foreach ( $taxonomy_names as $taxonomy_name) {
    function touch_time( $edit = 1, $for_post = 1, $tab_index = 0, $multi = 0 ) { 
    761762 * Print out option HTML elements for the page templates drop-down.
    762763 *
    763764 * @since 1.5.0
     765 * @since 4.7.0 Added the `$post_type` parameter.
    764766 *
    765  * @param string $default Optional. The template file name. Default empty.
     767 * @param string $default   Optional. The template file name. Default empty.
     768 * @param string $post_type Optional. Post type to get templates for. Default 'post'.
    766769 */
    767 function page_template_dropdown( $default = '' ) {
    768         $templates = get_page_templates( get_post() );
     770function page_template_dropdown( $default = '', $post_type = 'page' ) {
     771        $templates = get_page_templates( null, $post_type );
    769772        ksort( $templates );
    770773        foreach ( array_keys( $templates ) as $template ) {
    771774                $selected = selected( $default, $templates[ $template ], false );
  • src/wp-admin/includes/theme.php

    diff --git src/wp-admin/includes/theme.php src/wp-admin/includes/theme.php
    index 95aaec1..905e1be 100644
    function delete_theme($stylesheet, $redirect = '') { 
    102102 * Get the Page Templates available in this theme
    103103 *
    104104 * @since 1.5.0
     105 * @since 4.7.0 Added the `$post_type` parameter.
    105106 *
    106  * @param WP_Post|null $post Optional. The post being edited, provided for context.
     107 * @param WP_Post|null $post      Optional. The post being edited, provided for context.
     108 * @param string       $post_type Optional. Post type to get the templates for. Default 'page'.
    107109 * @return array Key is the template name, value is the filename of the template
    108110 */
    109 function get_page_templates( $post = null ) {
    110         return array_flip( wp_get_theme()->get_page_templates( $post ) );
     111function get_page_templates( $post = null, $post_type = 'page' ) {
     112        return array_flip( wp_get_theme()->get_page_templates( $post, $post_type ) );
    111113}
    112114
    113115/**
  • src/wp-includes/class-wp-post.php

    diff --git src/wp-includes/class-wp-post.php src/wp-includes/class-wp-post.php
    index a21776f..c966934 100644
    final class WP_Post { 
    254254                        return true;
    255255
    256256                if ( 'page_template' == $key )
    257                         return ( 'page' == $this->post_type );
     257                        return true;
    258258
    259259                if ( 'post_category' == $key )
    260260                   return true;
  • src/wp-includes/class-wp-theme.php

    diff --git src/wp-includes/class-wp-theme.php src/wp-includes/class-wp-theme.php
    index 40af39d..80f8362 100644
    final class WP_Theme implements ArrayAccess { 
    10051005        }
    10061006
    10071007        /**
    1008          * Returns the theme's page templates.
     1008         * Returns the theme's post templates.
    10091009         *
    1010          * @since 3.4.0
     1010         * @since 4.7.0
    10111011         * @access public
    10121012         *
    1013          * @param WP_Post|null $post Optional. The post being edited, provided for context.
    1014          * @return array Array of page templates, keyed by filename, with the value of the translated header name.
     1013         * @return array Array of page templates, keyed by filename and post type,
     1014         *               with the value of the translated header name.
    10151015         */
    1016         public function get_page_templates( $post = null ) {
     1016        public function get_post_templates() {
    10171017                // If you screw up your current theme and we invalidate your parent, most things still work. Let it slide.
    1018                 if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) )
     1018                if ( $this->errors() && $this->errors()->get_error_codes() !== array( 'theme_parent_invalid' ) ) {
    10191019                        return array();
     1020                }
    10201021
    1021                 $page_templates = $this->cache_get( 'page_templates' );
     1022                $post_templates = $this->cache_get( 'post_templates' );
    10221023
    1023                 if ( ! is_array( $page_templates ) ) {
    1024                         $page_templates = array();
     1024                if ( ! is_array( $post_templates ) ) {
     1025                        $post_templates = array();
    10251026
    10261027                        $files = (array) $this->get_files( 'php', 1 );
    10271028
    10281029                        foreach ( $files as $file => $full_path ) {
    1029                                 if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) )
     1030                                if ( ! preg_match( '|Template Name:(.*)$|mi', file_get_contents( $full_path ), $header ) ) {
    10301031                                        continue;
    1031                                 $page_templates[ $file ] = _cleanup_header_comment( $header[1] );
     1032                                }
     1033
     1034                                $types = array( 'page' );
     1035                                if ( preg_match( '|Template Type:(.*)$|mi', file_get_contents( $full_path ), $type ) ) {
     1036                                        $types = explode( ',', _cleanup_header_comment( $type[1] ) );
     1037                                }
     1038
     1039                                foreach ( $types as $type ) {
     1040                                        $type = trim( $type );
     1041                                        if ( ! isset( $post_templates[ $type ] ) ) {
     1042                                                $post_templates[ $type ] = array();
     1043                                        }
     1044
     1045                                        $post_templates[ $type ][ $file ] = _cleanup_header_comment( $header[1] );
     1046                                }
    10321047                        }
    10331048
    1034                         $this->cache_add( 'page_templates', $page_templates );
     1049                        $this->cache_add( 'page_templates', $post_templates );
    10351050                }
    10361051
    10371052                if ( $this->load_textdomain() ) {
    1038                         foreach ( $page_templates as &$page_template ) {
    1039                                 $page_template = $this->translate_header( 'Template Name', $page_template );
     1053                        foreach ( $post_templates as &$post_type ) {
     1054                                foreach ( $post_type as &$post_template ) {
     1055                                        $post_template = $this->translate_header( 'Template Name', $post_template );
     1056                                }
    10401057                        }
    10411058                }
    10421059
    1043                 if ( $this->parent() )
    1044                         $page_templates += $this->parent()->get_page_templates( $post );
     1060                return $post_templates;
     1061        }
     1062
     1063        /**
     1064         * Returns the theme's post templates for a given post type.
     1065         *
     1066         * @since 3.4.0
     1067         * @since 4.7.0 Added the `$post_type` parameter.
     1068         * @access public
     1069         *
     1070         * @param WP_Post|null $post      Optional. The post being edited, provided for context.
     1071         * @param string       $post_type Optional. Post type to get the templates for. Default 'page'.
     1072         *                                If a post is provided, its post type is used.
     1073         * @return array Array of page templates, keyed by filename, with the value of the translated header name.
     1074         */
     1075        public function get_page_templates( $post = null, $post_type = 'page' ) {
     1076                if ( $post ) {
     1077                        $post_type = get_post_type( $post );
     1078                }
     1079
     1080                $post_templates = $this->get_post_templates();
     1081                $post_templates = isset( $post_templates[ $post_type ] ) ? $post_templates[ $post_type ] : array();
     1082
     1083                if ( $this->parent() ) {
     1084                        $post_templates += $this->parent()->get_page_templates( $post );
     1085                }
    10451086
    10461087                /**
    10471088                 * Filters list of page templates for a theme.
    10481089                 *
    10491090                 * @since 3.9.0
    10501091                 * @since 4.4.0 Converted to allow complete control over the `$page_templates` array.
     1092                 * @since 4.7.0 Added the `$post_type` parameter.
    10511093                 *
    1052                  * @param array        $page_templates Array of page templates. Keys are filenames,
     1094                 * @param array        $post_templates Array of page templates. Keys are filenames,
    10531095                 *                                     values are translated names.
    10541096                 * @param WP_Theme     $this           The theme object.
    10551097                 * @param WP_Post|null $post           The post being edited, provided for context, or null.
     1098                 * @param string       $post_type      Post type to get the templates for.
    10561099                 */
    1057                 return (array) apply_filters( 'theme_page_templates', $page_templates, $this, $post );
     1100                return (array) apply_filters( 'theme_page_templates', $post_templates, $this, $post, $post_type );
    10581101        }
    10591102
    10601103        /**
  • src/wp-includes/post-template.php

    diff --git src/wp-includes/post-template.php src/wp-includes/post-template.php
    index 0c1d1e4..94f6e5d 100644
    function get_the_password_form( $post = 0 ) { 
    16131613 *
    16141614 * @since 2.5.0
    16151615 * @since 4.2.0 The `$template` parameter was changed to also accept an array of page templates.
     1616 * @since 4.7.0 Now supports any post type, not just pages.
    16161617 *
    16171618 * @param string|array $template The specific template name or array of templates to match.
    16181619 * @return bool True on success, false on failure.
    16191620 */
    16201621function is_page_template( $template = '' ) {
    1621         if ( ! is_page() )
    1622                 return false;
    1623 
    16241622        $page_template = get_page_template_slug( get_queried_object_id() );
    16251623
    16261624        if ( empty( $template ) )
    function is_page_template( $template = '' ) { 
    16411639}
    16421640
    16431641/**
    1644  * Get the specific template name for a page.
     1642 * Get the specific template name for a given post.
    16451643 *
    16461644 * @since 3.4.0
     1645 * @since 4.7.0 Now supports all post types.
    16471646 *
    1648  * @param int $post_id Optional. The page ID to check. Defaults to the current post, when used in the loop.
     1647 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
    16491648 * @return string|false Page template filename. Returns an empty string when the default page template
    1650  *      is in use. Returns false if the post is not a page.
     1649 *      is in use. Returns false if the post does not exist.
    16511650 */
    1652 function get_page_template_slug( $post_id = null ) {
    1653         $post = get_post( $post_id );
    1654         if ( ! $post || 'page' != $post->post_type )
     1651function get_page_template_slug( $post = null ) {
     1652        $post = get_post( $post );
     1653
     1654        if ( ! $post ) {
    16551655                return false;
     1656        }
     1657
    16561658        $template = get_post_meta( $post->ID, '_wp_page_template', true );
    1657         if ( ! $template || 'default' == $template )
     1659
     1660        if ( ! $template || 'default' == $template ) {
    16581661                return '';
     1662        }
     1663
    16591664        return $template;
    16601665}
    16611666
  • src/wp-includes/post.php

    diff --git src/wp-includes/post.php src/wp-includes/post.php
    index ced823c..f581017 100644
    function _post_type_meta_capabilities( $capabilities = null ) { 
    12421242 *                       post types. Default is 'Parent Page:'.
    12431243 * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'.
    12441244 * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'.
     1245 * - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'.
    12451246 * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'.
    12461247 * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' /
    12471248 *                           'Uploaded to this page'.
    function _post_type_meta_capabilities( $capabilities = null ) { 
    12671268 * @since 4.4.0 Added the `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`,
    12681269 *              `items_list_navigation`, and `items_list` labels.
    12691270 * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
     1271 * @since 4.7.0 Added the `attributes` label.
    12701272 *
    12711273 * @access private
    12721274 *
    function get_post_type_labels( $post_type_object ) { 
    12881290                'parent_item_colon' => array( null, __('Parent Page:') ),
    12891291                'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ),
    12901292                'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ),
     1293                'attributes' => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ),
    12911294                'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ),
    12921295                'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ),
    12931296                'featured_image' => array( __( 'Featured Image' ), __( 'Featured Image' ) ),
    function wp_insert_post( $postarr, $wp_error = false ) { 
    33073310
    33083311        $post = get_post( $post_ID );
    33093312
    3310         if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
     3313        if ( ! empty( $postarr['page_template'] ) ) {
    33113314                $post->page_template = $postarr['page_template'];
    33123315                $page_templates = wp_get_theme()->get_page_templates( $post );
    33133316                if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
  • src/wp-includes/template.php

    diff --git src/wp-includes/template.php src/wp-includes/template.php
    index 946732f..120e698 100644
    function get_single_template() { 
    432432        $templates = array();
    433433
    434434        if ( ! empty( $object->post_type ) ) {
     435                $template = get_page_template_slug( $object );
     436                if ( $template && 0 === validate_file( $template ) ) {
     437                        $templates[] = $template;
     438                }
    435439
    436440                $name_decoded = urldecode( $object->post_name );
    437441                if ( $name_decoded !== $object->post_name ) {
  • new file tests/phpunit/data/themedir1/page-templates/subdir/template-sub-dir-post-types.php

    diff --git tests/phpunit/data/themedir1/page-templates/subdir/template-sub-dir-post-types.php tests/phpunit/data/themedir1/page-templates/subdir/template-sub-dir-post-types.php
    new file mode 100644
    index 0000000..7f96d18
    - +  
     1<?php
     2/*
     3    Template Name: Sub Dir
     4    Template Type: post, foo
     5 */
  • new file tests/phpunit/data/themedir1/page-templates/template-top-level-post-types.php

    diff --git tests/phpunit/data/themedir1/page-templates/template-top-level-post-types.php tests/phpunit/data/themedir1/page-templates/template-top-level-post-types.php
    new file mode 100644
    index 0000000..19ece9e
    - +  
     1<?php
     2/*
     3    Template Name: Top Level
     4    Template Type: post, foo
     5 */
  • tests/phpunit/tests/admin/includesTheme.php

    diff --git tests/phpunit/tests/admin/includesTheme.php tests/phpunit/tests/admin/includesTheme.php
    index 382c6bb..ba76bf1 100644
    class Tests_Admin_includesTheme extends WP_UnitTestCase { 
    4848
    4949                switch_theme( $theme['Template'], $theme['Stylesheet'] );
    5050
    51                 $templates = get_page_templates();
    52                 $this->assertCount( 3, $templates );
    53                 $this->assertEquals( "template-top-level.php", $templates['Top Level'] );
    54                 $this->assertEquals( "subdir/template-sub-dir.php", $templates['Sub Dir'] );
    55                 $this->assertEquals( "template-header.php", $templates['This Template Header Is On One Line'] );
     51                $this->assertEqualSetsWithIndex( array(
     52                        'Top Level'                           => 'template-top-level.php',
     53                        'Sub Dir'                             => 'subdir/template-sub-dir.php',
     54                        'This Template Header Is On One Line' => 'template-header.php',
     55                ), get_page_templates() );
    5656
    5757                $theme = wp_get_theme( 'page-templates' );
    5858                $this->assertNotEmpty( $theme );
    5959
    6060                switch_theme( $theme['Template'], $theme['Stylesheet'] );
    6161
    62                 $templates = get_page_templates();
    63                 $this->assertCount( 3, $templates );
    64                 $this->assertEquals( "template-top-level.php", $templates['Top Level'] );
    65                 $this->assertEquals( "subdir/template-sub-dir.php", $templates['Sub Dir'] );
    66                 $this->assertEquals( "template-header.php", $templates['This Template Header Is On One Line'] );
     62                $this->assertEqualSetsWithIndex( array(
     63                        'Top Level'                           => 'template-top-level.php',
     64                        'Sub Dir'                             => 'subdir/template-sub-dir.php',
     65                        'This Template Header Is On One Line' => 'template-header.php',
     66                ), get_page_templates() );
     67        }
     68
     69        /**
     70         * @ticket 18375
     71         */
     72        function test_page_templates_different_post_types() {
     73                $theme = wp_get_theme( 'page-templates' );
     74                $this->assertNotEmpty( $theme );
     75
     76                switch_theme( $theme['Template'], $theme['Stylesheet'] );
     77
     78                $this->assertEqualSetsWithIndex( array(
     79                        'Top Level' => 'template-top-level-post-types.php',
     80                        'Sub Dir'   => 'subdir/template-sub-dir-post-types.php',
     81                ), get_page_templates( null, 'foo' ) );
     82                $this->assertEqualSetsWithIndex( array(
     83                        'Top Level' => 'template-top-level-post-types.php',
     84                        'Sub Dir'   => 'subdir/template-sub-dir-post-types.php',
     85                ), get_page_templates( null, 'post' ) );
     86                $this->assertEquals( array(), get_page_templates( null, 'bar' ) );
    6787        }
    6888}
  • tests/phpunit/tests/post/objects.php

    diff --git tests/phpunit/tests/post/objects.php tests/phpunit/tests/post/objects.php
    index 5ad92c6..fb376a2 100644
    class Tests_Post_Objects extends WP_UnitTestCase { 
    134134                $this->assertEquals( array( 'Bar', 'Baz', 'Foo' ), $post['tags_input'] );
    135135        }
    136136
     137        /**
     138         * @ticket 18375
     139         */
    137140        function test_get_page_template_property() {
    138141                $post_id = self::factory()->post->create();
    139142                $post = get_post( $post_id );
    class Tests_Post_Objects extends WP_UnitTestCase { 
    145148                update_post_meta( $post_id, '_wp_page_template', 'foo.php' );
    146149                $template = get_post_meta( $post->ID, '_wp_page_template', true );
    147150                $this->assertEquals( 'foo.php', $template );
    148                 // The post is not a page so the template is still empty
    149                 $this->assertEquals( '', $post->page_template );
    150 
    151                 // Now the post is a page and should retrieve the template
    152                 wp_update_post( array( 'ID' => $post->ID, 'post_type' => 'page' ) );
    153                 $post = get_post( $post_id );
    154151                $this->assertEquals( $template, $post->page_template );
    155152        }
    156153
  • tests/phpunit/tests/post/template.php

    diff --git tests/phpunit/tests/post/template.php tests/phpunit/tests/post/template.php
    index ac5958d..062b04a 100644
    NO; 
    271271
    272272        /**
    273273         * @ticket 31389
     274         * @ticket 18375
    274275         */
    275276        public function test_get_page_template_slug_non_page() {
    276                 $post_id = self::factory()->post->create( array(
    277                         'post_type' => 'post',
    278                 ) );
     277                $post_id = self::factory()->post->create();
     278
     279                $this->assertEquals( '', get_page_template_slug( $post_id ) );
     280
     281                update_post_meta( $post_id, '_wp_page_template', 'default' );
    279282
    280                 $this->assertFalse( get_page_template_slug( $post_id ) );
     283                $this->assertEquals( '', get_page_template_slug( $post_id ) );
     284
     285                update_post_meta( $post_id, '_wp_page_template', 'example.php' );
     286                $this->assertEquals( 'example.php', get_page_template_slug( $post_id ) );
     287        }
     288
     289        /**
     290         * @ticket 18375
     291         */
     292        public function test_get_page_template_slug_non_page_from_loop() {
     293                $post_id = self::factory()->post->create();
     294
     295                update_post_meta( $post_id, '_wp_page_template', 'example.php' );
    281296
    282297                $this->go_to( get_permalink( $post_id ) );
    283                 $this->assertFalse( get_page_template_slug() );
     298
     299                $this->assertEquals( 'example.php', get_page_template_slug() );
    284300        }
    285301
    286302        /**
  • tests/phpunit/tests/query/conditionals.php

    diff --git tests/phpunit/tests/query/conditionals.php tests/phpunit/tests/query/conditionals.php
    index 21963c7..debd18d 100644
    class Tests_Query_Conditionals extends WP_UnitTestCase { 
    10441044        }
    10451045
    10461046        /**
     1047         * @ticket 18375
     1048         */
     1049        function test_is_page_template_other_post_type() {
     1050                $post_id = self::factory()->post->create( array( 'post_type' => 'post' ) );
     1051                update_post_meta( $post_id, '_wp_page_template', 'example.php' );
     1052                $this->go_to( get_post_permalink( $post_id ) );
     1053                $this->assertFalse( is_page_template( array( 'test.php' ) ) );
     1054                $this->assertTrue( is_page_template( array( 'test.php', 'example.php' ) ) );
     1055        }
     1056
     1057        /**
    10471058         * @ticket 35902
    10481059         */
    10491060        public function test_is_attachment_should_not_match_numeric_id_to_post_title_beginning_with_id() {
  • tests/phpunit/tests/template.php

    diff --git tests/phpunit/tests/template.php tests/phpunit/tests/template.php
    index 00030f2..d303c57 100644
    class Tests_Template extends WP_UnitTestCase { 
    3636                        'post_date' => '1984-02-25 12:34:56',
    3737                ) );
    3838                set_post_format( self::$post, 'quote' );
     39                add_post_meta( self::$post->ID, '_wp_page_template', 'templates/post.php' );
    3940        }
    4041
    4142        public function setUp() {
    class Tests_Template extends WP_UnitTestCase { 
    203204                ) );
    204205        }
    205206
     207        /**
     208         * @ticket 18375
     209         */
    206210        public function test_single_template_hierarchy_for_post() {
    207211                $this->assertTemplateHierarchy( get_permalink( self::$post ), array(
     212                        'templates/post.php',
    208213                        'single-post-post-name-😀.php',
    209214                        'single-post-post-name-%f0%9f%98%80.php',
    210215                        'single-post.php',
    class Tests_Template extends WP_UnitTestCase { 
    228233                ) );
    229234        }
    230235
     236        /**
     237         * @ticket 18375
     238         */
     239        public function test_single_template_hierarchy_for_custom_post_type_with_page_template() {
     240                $cpt = self::factory()->post->create_and_get( array(
     241                        'post_type' => 'cpt',
     242                        'post_name' => 'cpt-name-😀',
     243                ) );
     244                add_post_meta( $cpt->ID, '_wp_page_template', 'templates/cpt.php' );
     245
     246                $this->assertTemplateHierarchy( get_permalink( $cpt ), array(
     247                        'templates/cpt.php',
     248                        'single-cpt-cpt-name-😀.php',
     249                        'single-cpt-cpt-name-%f0%9f%98%80.php',
     250                        'single-cpt.php',
     251                        'single.php',
     252                        'singular.php',
     253                ) );
     254        }
     255
    231256        public function test_attachment_template_hierarchy() {
    232257                $attachment = self::factory()->attachment->create_and_get( array(
    233258                        'post_name'      => 'attachment-name-😀',