Make WordPress Core

Ticket #18375: 18375.patch

File 18375.patch, 8.4 KB (added by johnbillion, 14 years ago)
  • wp-admin/includes/meta-boxes.php

     
    569569<?php
    570570                } // end empty pages check
    571571        } // end hierarchical check.
    572         if ( 'page' == $post->post_type && 0 != count( get_page_templates() ) ) {
     572        if ( 0 != count( get_post_type_templates( $post->post_type ) ) ) {
    573573                $template = !empty($post->page_template) ? $post->page_template : false;
    574574                ?>
    575575<p><strong><?php _e('Template') ?></strong></p>
    576 <label class="screen-reader-text" for="page_template"><?php _e('Page Template') ?></label><select name="page_template" id="page_template">
    577 <option value='default'><?php _e('Default Template'); ?></option>
    578 <?php page_template_dropdown($template); ?>
     576<label class="screen-reader-text" for="page_template"><?php _e('Template') ?></label>
     577<select name="page_template" id="page_template">
     578        <option value='default'><?php _e('Default Template'); ?></option>
     579        <?php post_type_template_dropdown( $template, $post->post_type ); ?>
    579580</select>
    580581<?php
    581582        } ?>
  • wp-admin/includes/post.php

     
    476476
    477477        $post = get_post( $id, OBJECT, 'edit' );
    478478
    479         if ( $post->post_type == 'page' )
    480                 $post->page_template = get_post_meta( $id, '_wp_page_template', true );
     479        $post->page_template = get_post_meta( $id, '_wp_page_template', true );
    481480
    482481        return $post;
    483482}
  • wp-admin/includes/template.php

     
    291291        if ( $post_type_object->hierarchical )
    292292                echo '<div class="post_parent">' . $post->post_parent . '</div>';
    293293
    294         if ( $post->post_type == 'page' )
    295                 echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>';
     294        echo '<div class="page_template">' . esc_html( get_post_meta( $post->ID, '_wp_page_template', true ) ) . '</div>';
    296295
    297296        if ( $post_type_object->hierarchical )
    298297                echo '<div class="menu_order">' . $post->menu_order . '</div>';
     
    647646}
    648647
    649648/**
    650  * {@internal Missing Short Description}}
     649 * Display dropdown options for template files from the active theme
    651650 *
    652  * @since 1.5.0
     651 * @since 3.3
    653652 *
    654  * @param unknown_type $default
     653 * @param string $default Template file to select by default
     654 * @param string $post_type Post type
    655655 */
    656 function page_template_dropdown( $default = '' ) {
    657         $templates = get_page_templates();
     656function post_type_template_dropdown( $default = '', $post_type = 'page' ) {
     657        $templates = get_post_type_templates( $post_type );
    658658        ksort( $templates );
    659         foreach (array_keys( $templates ) as $template )
    660                 : if ( $default == $templates[$template] )
     659        foreach ( array_keys( $templates ) as $template ) {
     660                if ( $default == $templates[$template] )
    661661                        $selected = " selected='selected'";
    662662                else
    663663                        $selected = '';
    664         echo "\n\t<option value='".$templates[$template]."' $selected>$template</option>";
    665         endforeach;
     664                echo "\n\t<option value='" . $templates[$template] . "' $selected>$template</option>";
     665        }
    666666}
    667667
    668668/**
     669 * Display dropdown options for page template files from the active theme
     670 *
     671 * @since 1.5.0
     672 *
     673 * @param string $default Template to select by default
     674 */
     675function page_template_dropdown( $default = '' ) {
     676        post_type_template_dropdown( $default, 'page' );
     677}
     678
     679/**
    669680 * {@internal Missing Short Description}}
    670681 *
    671682 * @since 1.5.0
  • wp-admin/includes/theme.php

     
    159159}
    160160
    161161/**
    162  * Get the Page Templates available in this theme
     162 * Get the templates available in this theme for the given post type
    163163 *
    164  * @since 1.5.0
     164 * @since 3.3
    165165 *
    166166 * @return array Key is the template name, value is the filename of the template
    167167 */
    168 function get_page_templates() {
     168function get_post_type_templates( $post_type ) {
     169        static $post_type_templates;
     170
     171        if ( isset( $post_type_templates ) ) {
     172                if ( isset( $post_type_templates[$post_type] ) )
     173                        return $post_type_templates[$post_type];
     174                else
     175                        return array();
     176        }
     177
    169178        $themes = get_themes();
    170179        $theme = get_current_theme();
    171180        $templates = $themes[$theme]['Template Files'];
    172         $page_templates = array();
     181        $post_type_templates = array();
    173182
    174183        if ( is_array( $templates ) ) {
    175                 $base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );
     184                $base = array( trailingslashit( get_template_directory() ), trailingslashit( get_stylesheet_directory() ) );
    176185
    177186                foreach ( $templates as $template ) {
    178                         $basename = str_replace($base, '', $template);
     187                        $basename = str_replace( $base, '', $template );
    179188
    180189                        // don't allow template files in subdirectories
    181                         if ( false !== strpos($basename, '/') )
     190                        if ( false !== strpos( $basename, '/' ) )
    182191                                continue;
    183192
    184193                        if ( 'functions.php' == $basename )
    185194                                continue;
    186195
    187                         $template_data = implode( '', file( $template ));
     196                        $template_data = implode( '', file( $template ) );
     197                        $name = '';
    188198
    189                         $name = '';
    190199                        if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )
    191                                 $name = _cleanup_header_comment($name[1]);
     200                                $name = trim( _cleanup_header_comment( $name[1] ) );
    192201
     202                        if ( preg_match( '|Template Type:(.*)$|mi', $template_data, $type ) )
     203                                $types = explode( ',', _cleanup_header_comment( $type[1] ) );
     204                        else
     205                                $types = array( 'page' );
     206
    193207                        if ( !empty( $name ) ) {
    194                                 $page_templates[trim( $name )] = $basename;
     208                                foreach ( $types as $type ) {
     209                                        $type = trim( $type );
     210                                        if ( !isset( $post_type_templates[$type] ) )
     211                                                $post_type_templates[$type] = array();
     212                                        $post_type_templates[$type][$name] = $basename;
     213                                }
    195214                        }
    196215                }
    197216        }
    198217
    199         return $page_templates;
     218        if ( isset( $post_type_templates[$post_type] ) )
     219                return $post_type_templates[$post_type];
     220        else
     221                return array();
    200222}
    201223
    202224/**
     225 * Get the Page Templates available in this theme
     226 *
     227 * @since 1.5.0
     228 *
     229 * @return array Key is the template name, value is the filename of the template
     230 */
     231function get_page_templates() {
     232        return get_post_type_templates( 'page' );
     233}
     234
     235/**
    203236 * Tidies a filename for url display by the theme editor.
    204237 *
    205238 * @since 2.9.0
  • wp-includes/post-template.php

     
    12351235 * @return bool False on failure, true if success.
    12361236 */
    12371237function is_page_template($template = '') {
    1238         if (!is_page()) {
    1239                 return false;
    1240         }
    12411238
    12421239        global $wp_query;
    12431240
    1244         $page = $wp_query->get_queried_object();
    1245         $custom_fields = get_post_custom_values('_wp_page_template',$page->ID);
     1241        $post = $wp_query->get_queried_object();
     1242        $custom_fields = get_post_custom_values('_wp_page_template',$post->ID);
    12461243        $page_template = $custom_fields[0];
    12471244
    12481245        // We have no argument passed so just see if a page_template has been specified
  • wp-includes/post.php

     
    26212621
    26222622        $post = get_post($post_ID);
    26232623
    2624         if ( !empty($page_template) && 'page' == $data['post_type'] ) {
     2624        if ( !empty($page_template) ) {
    26252625                $post->page_template = $page_template;
    2626                 $page_templates = get_page_templates();
     2626                $page_templates = get_post_type_templates( $post->post_type );
    26272627                if ( 'default' != $page_template && !in_array($page_template, $page_templates) ) {
    26282628                        if ( $wp_error )
    26292629                                return new WP_Error('invalid_page_template', __('The page template is invalid.'));
  • wp-includes/theme.php

     
    10011001 */
    10021002function get_single_template() {
    10031003        $object = get_queried_object();
     1004        $template = get_post_meta( $object->ID, '_wp_page_template', true );
    10041005
     1006        if ( 'default' == $template )
     1007                $template = '';
     1008
    10051009        $templates = array();
     1010        if ( !empty($template) && !validate_file($template) )
     1011                $templates[] = $template;
    10061012
    10071013        $templates[] = "single-{$object->post_type}.php";
    10081014        $templates[] = "single.php";