Make WordPress Core

Ticket #18375: 18375.2.diff

File 18375.2.diff, 22.7 KB (added by Mte90, 9 years ago)

now use get_file_data

  • src/wp-admin/edit-form-advanced.php

     
    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', 'page' == $post_type ? __('Page Attributes') : __('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

     
    15231523                        </label>
    15241524
    15251525        <?php   endif; // !$bulk
    1526 
    1527                         if ( 'page' === $screen->post_type ) :
    15281526        ?>
    15291527
    15301528                        <label>
     
    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

     
    787787 * @param object $post
    788788 */
    789789function page_attributes_meta_box($post) {
    790         $post_type_object = get_post_type_object($post->post_type);
    791         if ( $post_type_object->hierarchical ) {
     790        if ( is_post_type_hierarchical( $post->post_type ) ) {
    792791                $dropdown_args = array(
    793792                        'post_type'        => $post->post_type,
    794793                        'exclude_tree'     => $post->ID,
     
    819818<?php
    820819                } // end empty pages check
    821820        } // end hierarchical check.
    822         if ( 'page' == $post->post_type && 0 != count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID ) {
    823                 $template = !empty($post->page_template) ? $post->page_template : false;
     821
     822        if ( 0 < count( get_page_templates( $post ) ) && get_option( 'page_for_posts' ) != $post->ID ) {
     823                $template = ! empty( $post->page_template ) ? $post->page_template : false;
    824824                ?>
    825825<p><strong><?php _e('Template') ?></strong><?php
    826826        /**
     
    834834         */
    835835        do_action( 'page_attributes_meta_box_template', $template, $post );
    836836?></p>
    837 <label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
     837<label class="screen-reader-text" for="page_template"><?php _e('Template') ?></label><select name="page_template" id="page_template">
    838838<?php
    839839/**
    840840 * Filters the title of the default page template displayed in the drop-down.
     
    848848$default_title = apply_filters( 'default_page_template_title',  __( 'Default Template' ), 'meta-box' );
    849849?>
    850850<option value="default"><?php echo esc_html( $default_title ); ?></option>
    851 <?php page_template_dropdown($template); ?>
     851<?php page_template_dropdown( $template, $post->post_type ); ?>
    852852</select>
    853853<?php
    854854        } ?>
  • src/wp-admin/includes/template.php

     
    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) {
     
    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

     
    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

     
    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

     
    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                                $template_data = get_file_data( $full_path, array( 'Template Name' => 'Template Name', 'Template Type' => 'Template Type' ) );
     1031                                if ( empty( $template_data[ 'Template Name' ] ) ) {
    10301032                                        continue;
    1031                                 $page_templates[ $file ] = _cleanup_header_comment( $header[1] );
     1033                                }
     1034                               
     1035                                $types = array( 'page' );
     1036                                if ( !empty( $template_data[ 'Template Type' ] ) ) {
     1037                                        $types = explode( ',', _cleanup_header_comment( $template_data[ 'Template Type' ] ) );
     1038                                }
     1039
     1040                                foreach ( $types as $type ) {
     1041                                        $type = trim( $type );
     1042                                        if ( ! isset( $post_templates[ $type ] ) ) {
     1043                                                $post_templates[ $type ] = array();
     1044                                        }
     1045
     1046                                        $post_templates[ $type ][ $file ] = _cleanup_header_comment( $template_data[ 'Template Name' ] );
     1047                                }
    10321048                        }
    10331049
    1034                         $this->cache_add( 'page_templates', $page_templates );
     1050                        $this->cache_add( 'page_templates', $post_templates );
    10351051                }
    10361052
    10371053                if ( $this->load_textdomain() ) {
    1038                         foreach ( $page_templates as &$page_template ) {
    1039                                 $page_template = $this->translate_header( 'Template Name', $page_template );
     1054                        foreach ( $post_templates as &$post_type ) {
     1055                                foreach ( $post_type as &$post_template ) {
     1056                                        $post_template = $this->translate_header( 'Template Name', $post_template );
     1057                                }
    10401058                        }
    10411059                }
    10421060
    1043                 if ( $this->parent() )
    1044                         $page_templates += $this->parent()->get_page_templates( $post );
     1061                return $post_templates;
     1062        }
    10451063
     1064        /**
     1065         * Returns the theme's post templates for a given post type.
     1066         *
     1067         * @since 3.4.0
     1068         * @since 4.7.0 Added the `$post_type` parameter.
     1069         * @access public
     1070         *
     1071         * @param WP_Post|null $post      Optional. The post being edited, provided for context.
     1072         * @param string       $post_type Optional. Post type to get the templates for. Default 'page'.
     1073         *                                If a post is provided, its post type is used.
     1074         * @return array Array of page templates, keyed by filename, with the value of the translated header name.
     1075         */
     1076        public function get_page_templates( $post = null, $post_type = 'page' ) {
     1077                if ( $post ) {
     1078                        $post_type = get_post_type( $post );
     1079                }
     1080
     1081                $post_templates = $this->get_post_templates();
     1082                $post_templates = isset( $post_templates[ $post_type ] ) ? $post_templates[ $post_type ] : array();
     1083
     1084                if ( $this->parent() ) {
     1085                        $post_templates += $this->parent()->get_page_templates( $post );
     1086                }
     1087
    10461088                /**
    10471089                 * Filters list of page templates for a theme.
    10481090                 *
    10491091                 * @since 3.9.0
    10501092                 * @since 4.4.0 Converted to allow complete control over the `$page_templates` array.
     1093                 * @since 4.7.0 Added the `$post_type` parameter.
    10511094                 *
    1052                  * @param array        $page_templates Array of page templates. Keys are filenames,
     1095                 * @param array        $post_templates Array of page templates. Keys are filenames,
    10531096                 *                                     values are translated names.
    10541097                 * @param WP_Theme     $this           The theme object.
    10551098                 * @param WP_Post|null $post           The post being edited, provided for context, or null.
     1099                 * @param string       $post_type      Post type to get the templates for.
    10561100                 */
    1057                 return (array) apply_filters( 'theme_page_templates', $page_templates, $this, $post );
     1101                return (array) apply_filters( 'theme_page_templates', $post_templates, $this, $post, $post_type );
    10581102        }
    10591103
    10601104        /**
  • src/wp-includes/post-template.php

     
    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 ) )
     
    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

     
    12431243 *                       post types. Default is 'Parent Page:'.
    12441244 * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'.
    12451245 * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'.
     1246 * - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'.
    12461247 * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'.
    12471248 * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' /
    12481249 *                           'Uploaded to this page'.
     
    15281529 * @since 4.4.0
    15291530 * @since 4.5.0 Added the ability to pass a post type name in addition to object.
    15301531 * @since 4.6.0 Converted the `$post_type` parameter to accept a WP_Post_Type object.
     1532 * @since 4.7.0 Added the `attributes` label.
    15311533 *
    15321534 * @param string|WP_Post_Type $post_type Post type name or object.
    15331535 * @return bool Whether the post type should be considered viewable.
     
    33103312
    33113313        $post = get_post( $post_ID );
    33123314
    3313         if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
     3315        if ( ! empty( $postarr['page_template'] ) ) {
    33143316                $post->page_template = $postarr['page_template'];
    33153317                $page_templates = wp_get_theme()->get_page_templates( $post );
    33163318                if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
  • src/wp-includes/template.php

     
    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 ) {
  • tests/phpunit/tests/admin/includesTheme.php

     
    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() );
    6767        }
     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' ) );
     87        }
    6888}
  • tests/phpunit/tests/post/objects.php

     
    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 );
     
    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

     
    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();
    279278
    280                 $this->assertFalse( get_page_template_slug( $post_id ) );
     279                $this->assertEquals( '', get_page_template_slug( $post_id ) );
    281280
     281                update_post_meta( $post_id, '_wp_page_template', 'default' );
     282
     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' );
     296
    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

     
    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

     
    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() {
     
    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',
     
    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-😀',