WordPress.org

Make WordPress Core

Changeset 20313


Ignore:
Timestamp:
03/29/2012 03:00:38 AM (10 years ago)
Author:
nacin
Message:

Rewrite theme-editor.php to use the new WP_Theme API. see #20103.

Location:
trunk/wp-admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/file.php

    r20070 r20313  
    6666    }
    6767
    68     return basename( $file );
     68    return trim( basename( $file ) );
    6969}
    7070
  • trunk/wp-admin/theme-editor.php

    r19687 r20313  
    4343);
    4444
    45 wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme', 'dir'));
    46 
    47 $themes = get_themes();
    48 
    49 if (empty($theme)) {
    50     $theme = get_current_theme();
     45wp_reset_vars( array( 'action', 'error', 'file', 'theme' ) );
     46
     47if ( $theme )
     48    $stylesheet = $theme;
     49else
     50    $stylesheet = get_stylesheet();
     51
     52$theme = wp_get_theme( $stylesheet );
     53
     54if ( ! $theme )
     55    wp_die( __( 'The requested theme does not exist.' ) );
     56
     57$allowed_files = $theme->get_files( 'php', 1 );
     58$style_files = $theme->get_files( 'css' );
     59if ( isset( $style_files['style.css'] ) ) {
     60    $allowed_files['style.css'] = $style_files['style.css'];
     61    unset( $style_files['style.css'] );
    5162} else {
    52     $theme = stripslashes($theme);
    53 }
    54 
    55 if ( ! isset($themes[$theme]) )
    56     wp_die(__('The requested theme does not exist.'));
    57 
    58 $allowed_files = array_merge( $themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files'] );
     63    $style_files['style.css'] = false;
     64}
     65$allowed_files += $style_files;
    5966
    6067if ( empty( $file ) ) {
    61     if ( false !== array_search( $themes[$theme]['Stylesheet Dir'] . '/style.css', $allowed_files ) )
    62         $file = $themes[$theme]['Stylesheet Dir'] . '/style.css';
    63     else
    64         $file = $allowed_files[0];
     68    if ( ! empty( $allowed_files['style.css'] ) ) {
     69        $relative_file = 'style.css';
     70        $file = $allowed_files['style.css'];
     71    } else {
     72        $relative_file = key( $allowed_files );
     73        $file = current( $allowed_files );
     74    }
    6575} else {
    66     $file = stripslashes($file);
    67     if ( 'theme' == $dir ) {
    68         $file = dirname(dirname($themes[$theme]['Template Dir'])) . $file ;
    69     } else if ( 'style' == $dir) {
    70         $file = dirname(dirname($themes[$theme]['Stylesheet Dir'])) . $file ;
     76    $relative_file = stripslashes( $file );
     77    $file = $theme->get_stylesheet_directory() . '/' . $relative_file;
     78}
     79
     80validate_file_to_edit( $file, $allowed_files );
     81$scrollto = isset( $_REQUEST['scrollto'] ) ? (int) $_REQUEST['scrollto'] : 0;
     82
     83switch( $action ) {
     84case 'update':
     85    check_admin_referer( 'edit-theme_' . $file . $stylesheet );
     86    $newcontent = stripslashes( $_POST['newcontent'] );
     87    $location = 'theme-editor.php?file=' . urlencode( $relative_file ) . '&theme=' . urlencode( $stylesheet ) . '&scrollto=' . $scrollto;
     88    if ( is_writeable( $file ) ) {
     89        //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
     90        $f = fopen( $file, 'w+' );
     91        if ( $f !== false ) {
     92            fwrite( $f, $newcontent );
     93            fclose( $f );
     94            $location .= '&updated=true';
     95        }
    7196    }
    72 }
    73 
    74 validate_file_to_edit($file, $allowed_files);
    75 $scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0;
    76 $file_show = basename( $file );
    77 
    78 switch($action) {
    79 
    80 case 'update':
    81 
    82     check_admin_referer('edit-theme_' . $file . $theme);
    83 
    84     $newcontent = stripslashes($_POST['newcontent']);
    85     $theme = urlencode($theme);
    86     if (is_writeable($file)) {
    87         //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable
    88         $f = fopen($file, 'w+');
    89         if ($f !== false) {
    90             fwrite($f, $newcontent);
    91             fclose($f);
    92             $location = "theme-editor.php?file=$file&theme=$theme&a=te&scrollto=$scrollto";
    93         } else {
    94             $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
    95         }
    96     } else {
    97         $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto";
    98     }
    99 
    100     $location = wp_kses_no_null($location);
    101     $strip = array('%0d', '%0a', '%0D', '%0A');
    102     $location = _deep_replace($strip, $location);
    103     header("Location: $location");
    104     exit();
    105 
     97    wp_redirect( $location );
     98    exit;
    10699break;
    107100
    108101default:
    109102
    110     require_once(ABSPATH . 'wp-admin/admin-header.php');
    111 
    112     update_recently_edited($file);
    113 
    114     if ( !is_file($file) )
    115         $error = 1;
     103    require_once( ABSPATH . 'wp-admin/admin-header.php' );
     104
     105    update_recently_edited( $file );
     106
     107    if ( ! is_file( $file ) )
     108        $error = true;
    116109
    117110    $content = '';
    118     if ( !$error && filesize($file) > 0 ) {
     111    if ( ! $error && filesize( $file ) > 0 ) {
    119112        $f = fopen($file, 'r');
    120113        $content = fread($f, filesize($file));
     
    135128
    136129    ?>
    137 <?php if (isset($_GET['a'])) : ?>
    138  <div id="message" class="updated"><p><?php _e('File edited successfully.') ?></p></div>
     130<?php if ( isset( $_GET['updated'] ) ) : ?>
     131 <div id="message" class="updated"><p><?php _e( 'File edited successfully.' ) ?></p></div>
    139132<?php endif;
    140133
    141 $description = get_file_description($file);
    142 $desc_header = ( $description != $file_show ) ? "$description <span>($file_show)</span>" : $file_show;
    143 
    144 $is_child_theme = $themes[$theme]['Template'] != $themes[$theme]['Stylesheet'];
     134$description = get_file_description( $file );
     135$file_show = array_search( $file, $allowed_files );
     136if ( $description != $file_show )
     137    $description .= ' <span>(' . $file_show . ')</span>';
    145138?>
    146139<div class="wrap">
     
    150143<div class="fileedit-sub">
    151144<div class="alignleft">
    152 <h3><?php echo $themes[$theme]['Name'] . ': ' . $desc_header; ?></h3>
     145<h3><?php echo $theme->display('Name') . ': ' . $description; ?></h3>
    153146</div>
    154147<div class="alignright">
     
    157150        <select name="theme" id="theme">
    158151<?php
    159     foreach ($themes as $a_theme) {
    160     $theme_name = $a_theme['Name'];
    161     if ($theme_name == $theme) $selected = " selected='selected'";
    162     else $selected = '';
    163     $theme_name = esc_attr($theme_name);
    164     echo "\n\t<option value=\"$theme_name\" $selected>$theme_name</option>";
     152foreach ( wp_get_themes() as $a_stylesheet => $a_theme ) {
     153    $selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : '';
     154    echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display('Name') . '</option>';
    165155}
    166156?>
     
    173163    <div id="templateside">
    174164<?php
    175 if ($allowed_files) :
     165if ( $allowed_files ) :
    176166?>
    177167    <h3><?php _e('Templates'); ?></h3>
    178     <?php if ( $is_child_theme ) : ?>
    179     <p class="howto"><?php printf( __( 'This child theme inherits templates from a parent theme, %s.' ), $themes[$theme]['Parent Theme'] ); ?></p>
     168    <?php if ( $theme->is_child_theme() ) : ?>
     169    <p class="howto"><?php printf( __( 'This child theme inherits templates from a parent theme, %s.' ), '<a href="' . self_admin_url('theme-editor.php?theme=' . $theme->get_template()) . '">' . $theme->parent()->display('Name') . '</a>' ); ?></p>
    180170    <?php endif; ?>
    181171    <ul>
    182172<?php
    183     $template_mapping = array();
    184     $template_dir = $themes[$theme]['Template Dir'];
    185     foreach ( $themes[$theme]['Template Files'] as $template_file ) {
    186         // Don't show parent templates.
    187         if ( $is_child_theme && strpos( $template_file, trailingslashit( $template_dir ) ) === 0 )
    188             continue;
    189 
    190         $description = trim( get_file_description($template_file) );
    191         $template_show = basename($template_file);
    192         $filedesc = ( $description != $template_file ) ? "$description<br /><span class='nonessential'>($template_show)</span>" : "$description";
    193         $filedesc = ( $template_file == $file ) ? "<span class='highlight'>$description<br /><span class='nonessential'>($template_show)</span></span>" : $filedesc;
    194         $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc );
    195     }
    196     ksort( $template_mapping );
    197     while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) :
    198     ?>
    199         <li><a href="theme-editor.php?file=<?php echo urlencode( $template_file ) ?>&amp;theme=<?php echo urlencode( $theme ) ?>&amp;dir=theme"><?php echo $filedesc ?></a></li>
    200 <?php endwhile; ?>
    201     </ul>
    202     <h3><?php /* translators: Theme stylesheets in theme editor */ _ex('Styles', 'Theme stylesheets in theme editor'); ?></h3>
    203     <ul>
    204 <?php
    205     $template_mapping = array();
    206     $stylesheet_dir = $themes[$theme]['Stylesheet Dir'];
    207     foreach ( $themes[$theme]['Stylesheet Files'] as $style_file ) {
    208         // Don't show parent styles.
    209         if ( $is_child_theme && strpos( $style_file, trailingslashit( $template_dir ) ) === 0 )
    210             continue;
    211 
    212         $description = trim( get_file_description($style_file) );
    213         $style_show = basename($style_file);
    214         $filedesc = ( $description != $style_file ) ? "$description<br /><span class='nonessential'>($style_show)</span>" : "$description";
    215         $filedesc = ( $style_file == $file ) ? "<span class='highlight'>$description<br /><span class='nonessential'>($style_show)</span></span>" : $filedesc;
    216         $template_mapping[ $description ] = array( _get_template_edit_filename($style_file, $stylesheet_dir), $filedesc );
    217     }
    218     ksort( $template_mapping );
    219     while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) :
    220         ?>
    221         <li><a href="theme-editor.php?file=<?php echo urlencode( $style_file ) ?>&amp;theme=<?php echo urlencode($theme) ?>&amp;dir=style"><?php echo $filedesc ?></a></li>
    222 <?php endwhile; ?>
    223     </ul>
     173    foreach ( $allowed_files as $filename => $absolute_filename ) :
     174        if ( 'style.css' == $filename ) {
     175            echo "\t</ul>\n\t<h3>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h3>\n\t<ul>\n";
     176            if ( ! $absolute_filename )
     177                continue;
     178        }
     179
     180        $file_description = get_file_description( $filename );
     181        if ( $file_description != basename( $filename ) )
     182            $file_description .= '<br /><span class="nonessential">(' . $filename . ')</span>';
     183
     184        if ( $absolute_filename == $file )
     185            $file_description = '<span class="highlight">' . $file_description . '</span>';
     186?>
     187        <li><a href="theme-editor.php?file=<?php echo urlencode( $filename ) ?>&amp;theme=<?php echo urlencode( $stylesheet ) ?>"><?php echo $file_description; ?></a></li>
     188<?php
     189    endforeach;
     190?>
     191</ul>
    224192<?php endif; ?>
    225193</div>
    226 <?php if (!$error) { ?>
     194<?php if ( $error ) :
     195    echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
     196else : ?>
    227197    <form name="template" id="template" action="theme-editor.php" method="post">
    228     <?php wp_nonce_field('edit-theme_' . $file . $theme) ?>
    229          <div><textarea cols="70" rows="25" name="newcontent" id="newcontent" tabindex="1"><?php echo $content ?></textarea>
     198    <?php wp_nonce_field( 'edit-theme_' . $file . $stylesheet ); ?>
     199         <div><textarea cols="70" rows="30" name="newcontent" id="newcontent" tabindex="1"><?php echo $content ?></textarea>
    230200         <input type="hidden" name="action" value="update" />
    231          <input type="hidden" name="file" value="<?php echo esc_attr($file) ?>" />
    232          <input type="hidden" name="theme" value="<?php echo esc_attr($theme) ?>" />
     201         <input type="hidden" name="file" value="<?php echo esc_attr( $relative_file ); ?>" />
     202         <input type="hidden" name="theme" value="<?php echo esc_attr( $theme->get_stylesheet() ); ?>" />
    233203         <input type="hidden" name="scrollto" id="scrollto" value="<?php echo $scrollto; ?>" />
    234204         </div>
    235     <?php if ( isset($functions ) && count($functions) ) { ?>
     205    <?php if ( ! empty( $functions ) ) : ?>
    236206        <div id="documentation" class="hide-if-no-js">
    237207        <label for="docs-list"><?php _e('Documentation:') ?></label>
     
    239209        <input type="button" class="button" value=" <?php esc_attr_e( 'Lookup' ); ?> " onclick="if ( '' != jQuery('#docs-list').val() ) { window.open( 'http://api.wordpress.org/core/handbook/1.0/?function=' + escape( jQuery( '#docs-list' ).val() ) + '&amp;locale=<?php echo urlencode( get_locale() ) ?>&amp;version=<?php echo urlencode( $wp_version ) ?>&amp;redirect=true'); }" />
    240210        </div>
    241     <?php } ?>
     211    <?php endif; ?>
    242212
    243213        <div>
    244         <?php if ( is_child_theme() && ! $is_child_theme && $themes[$theme]['Template'] == get_option('template') ) : ?>
     214        <?php if ( is_child_theme() && ! $theme->is_child_theme() && $theme->get_template() == get_template() ) : ?>
    245215            <p><?php if ( is_writeable( $file ) ) { ?><strong><?php _e( 'Caution:' ); ?></strong><?php } ?>
    246216            <?php _e( 'This is a file in your current parent theme.' ); ?></p>
     
    255225    </form>
    256226<?php
    257     } else {
    258         echo '<div class="error"><p>' . __('Oops, no such file exists! Double check the name and try again, merci.') . '</p></div>';
    259     }
     227endif; // $error
    260228?>
    261229<br class="clear" />
     
    273241}
    274242
    275 include(ABSPATH . "wp-admin/admin-footer.php");
     243include(ABSPATH . 'wp-admin/admin-footer.php' );
Note: See TracChangeset for help on using the changeset viewer.