From 23abe58a59f8a685a1d76eeeea70e66bc88a4e10 Mon Sep 17 00:00:00 2001 From: nacin Date: Thu, 29 Mar 2012 03:00:38 +0000 Subject: [PATCH] Rewrite theme-editor.php to use the new WP_Theme API. see #20103. git-svn-id: http://svn.automattic.com/wordpress/trunk@20313 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/file.php | 2 +- wp-admin/theme-editor.php | 206 ++++++++++++++++--------------------- 2 files changed, 88 insertions(+), 120 deletions(-) diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index 447b2d678..9ff24384c 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -65,7 +65,7 @@ function get_file_description( $file ) { return sprintf( __( '%s Page Template' ), _cleanup_header_comment($name[1]) ); } - return basename( $file ); + return trim( basename( $file ) ); } /** diff --git a/wp-admin/theme-editor.php b/wp-admin/theme-editor.php index 71dbd1275..0258e3af3 100644 --- a/wp-admin/theme-editor.php +++ b/wp-admin/theme-editor.php @@ -42,80 +42,73 @@ get_current_screen()->set_help_sidebar( '

' . __('Support Forums') . '

' ); -wp_reset_vars(array('action', 'redirect', 'profile', 'error', 'warning', 'a', 'file', 'theme', 'dir')); +wp_reset_vars( array( 'action', 'error', 'file', 'theme' ) ); -$themes = get_themes(); +if ( $theme ) + $stylesheet = $theme; +else + $stylesheet = get_stylesheet(); -if (empty($theme)) { - $theme = get_current_theme(); +$theme = wp_get_theme( $stylesheet ); + +if ( ! $theme ) + wp_die( __( 'The requested theme does not exist.' ) ); + +$allowed_files = $theme->get_files( 'php', 1 ); +$style_files = $theme->get_files( 'css' ); +if ( isset( $style_files['style.css'] ) ) { + $allowed_files['style.css'] = $style_files['style.css']; + unset( $style_files['style.css'] ); } else { - $theme = stripslashes($theme); + $style_files['style.css'] = false; } - -if ( ! isset($themes[$theme]) ) - wp_die(__('The requested theme does not exist.')); - -$allowed_files = array_merge( $themes[$theme]['Stylesheet Files'], $themes[$theme]['Template Files'] ); +$allowed_files += $style_files; if ( empty( $file ) ) { - if ( false !== array_search( $themes[$theme]['Stylesheet Dir'] . '/style.css', $allowed_files ) ) - $file = $themes[$theme]['Stylesheet Dir'] . '/style.css'; - else - $file = $allowed_files[0]; -} else { - $file = stripslashes($file); - if ( 'theme' == $dir ) { - $file = dirname(dirname($themes[$theme]['Template Dir'])) . $file ; - } else if ( 'style' == $dir) { - $file = dirname(dirname($themes[$theme]['Stylesheet Dir'])) . $file ; + if ( ! empty( $allowed_files['style.css'] ) ) { + $relative_file = 'style.css'; + $file = $allowed_files['style.css']; + } else { + $relative_file = key( $allowed_files ); + $file = current( $allowed_files ); } +} else { + $relative_file = stripslashes( $file ); + $file = $theme->get_stylesheet_directory() . '/' . $relative_file; } -validate_file_to_edit($file, $allowed_files); -$scrollto = isset($_REQUEST['scrollto']) ? (int) $_REQUEST['scrollto'] : 0; -$file_show = basename( $file ); - -switch($action) { +validate_file_to_edit( $file, $allowed_files ); +$scrollto = isset( $_REQUEST['scrollto'] ) ? (int) $_REQUEST['scrollto'] : 0; +switch( $action ) { case 'update': - - check_admin_referer('edit-theme_' . $file . $theme); - - $newcontent = stripslashes($_POST['newcontent']); - $theme = urlencode($theme); - if (is_writeable($file)) { + check_admin_referer( 'edit-theme_' . $file . $stylesheet ); + $newcontent = stripslashes( $_POST['newcontent'] ); + $location = 'theme-editor.php?file=' . urlencode( $relative_file ) . '&theme=' . urlencode( $stylesheet ) . '&scrollto=' . $scrollto; + if ( is_writeable( $file ) ) { //is_writable() not always reliable, check return value. see comments @ http://uk.php.net/is_writable - $f = fopen($file, 'w+'); - if ($f !== false) { - fwrite($f, $newcontent); - fclose($f); - $location = "theme-editor.php?file=$file&theme=$theme&a=te&scrollto=$scrollto"; - } else { - $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto"; + $f = fopen( $file, 'w+' ); + if ( $f !== false ) { + fwrite( $f, $newcontent ); + fclose( $f ); + $location .= '&updated=true'; } - } else { - $location = "theme-editor.php?file=$file&theme=$theme&scrollto=$scrollto"; } - - $location = wp_kses_no_null($location); - $strip = array('%0d', '%0a', '%0D', '%0A'); - $location = _deep_replace($strip, $location); - header("Location: $location"); - exit(); - + wp_redirect( $location ); + exit; break; default: - require_once(ABSPATH . 'wp-admin/admin-header.php'); + require_once( ABSPATH . 'wp-admin/admin-header.php' ); - update_recently_edited($file); + update_recently_edited( $file ); - if ( !is_file($file) ) - $error = 1; + if ( ! is_file( $file ) ) + $error = true; $content = ''; - if ( !$error && filesize($file) > 0 ) { + if ( ! $error && filesize( $file ) > 0 ) { $f = fopen($file, 'r'); $content = fread($f, filesize($file)); @@ -134,14 +127,14 @@ default: } ?> - -

+ +

($file_show)" : $file_show; - -$is_child_theme = $themes[$theme]['Template'] != $themes[$theme]['Stylesheet']; +$description = get_file_description( $file ); +$file_show = array_search( $file, $allowed_files ); +if ( $description != $file_show ) + $description .= ' (' . $file_show . ')'; ?>
@@ -149,19 +142,16 @@ $is_child_theme = $themes[$theme]['Template'] != $themes[$theme]['Stylesheet'];
-

+

display('Name') . ': ' . $description; ?>

@@ -172,76 +162,56 @@ $is_child_theme = $themes[$theme]['Template'] != $themes[$theme]['Stylesheet'];

- -

+ is_child_theme() ) : ?> +

get_template()) . '">' . $theme->parent()->display('Name') . '' ); ?>

    $absolute_filename ) : + if ( 'style.css' == $filename ) { + echo "\t
\n\t

" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "

\n\t
    \n"; + if ( ! $absolute_filename ) + continue; + } - $description = trim( get_file_description($template_file) ); - $template_show = basename($template_file); - $filedesc = ( $description != $template_file ) ? "$description
    ($template_show)" : "$description"; - $filedesc = ( $template_file == $file ) ? "$description
    ($template_show)
    " : $filedesc; - $template_mapping[ $description ] = array( _get_template_edit_filename($template_file, $template_dir), $filedesc ); - } - ksort( $template_mapping ); - while ( list( $template_sorted_key, list( $template_file, $filedesc ) ) = each( $template_mapping ) ) : - ?> -
  • - -
-

-
    + $file_description = get_file_description( $filename ); + if ( $file_description != basename( $filename ) ) + $file_description .= '
    (' . $filename . ')'; + + if ( $absolute_filename == $file ) + $file_description = '' . $file_description . ''; +?> +
  • ($style_show)" : "$description"; - $filedesc = ( $style_file == $file ) ? "$description
    ($style_show)
    " : $filedesc; - $template_mapping[ $description ] = array( _get_template_edit_filename($style_file, $stylesheet_dir), $filedesc ); - } - ksort( $template_mapping ); - while ( list( $template_sorted_key, list( $style_file, $filedesc ) ) = each( $template_mapping ) ) : - ?> -
  • - -
+ endforeach; +?> +
- +

' . __('Oops, no such file exists! Double check the name and try again, merci.') . '

'; +else : ?> - -
+ +
- - + +
- +
- +
- + is_child_theme() && $theme->get_template() == get_template() ) : ?>

@@ -254,9 +224,7 @@ if ($allowed_files) :

' . __('Oops, no such file exists! Double check the name and try again, merci.') . '

'; - } +endif; // $error ?>
@@ -272,4 +240,4 @@ jQuery(document).ready(function($){ break; } -include(ABSPATH . "wp-admin/admin-footer.php"); +include(ABSPATH . 'wp-admin/admin-footer.php' );