Always set WP_Theme->template even when there is an error and we have no idea what the template is. (Assume it is the stylesheet.) Prevents a number of issues including WP_Theme->is_child_theme() lying. Tidy the theme editor for broken themes and themes with no templates (PHP files), or no template (parent), or are broken. Allow broken themes to be edited. see #20103.

git-svn-id: http://svn.automattic.com/wordpress/trunk@20315 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2012-03-29 04:16:17 +00:00
parent 107733e263
commit 5b8037afe6
2 changed files with 17 additions and 11 deletions

View File

@ -55,6 +55,8 @@ if ( ! $theme )
wp_die( __( 'The requested theme does not exist.' ) ); wp_die( __( 'The requested theme does not exist.' ) );
$allowed_files = $theme->get_files( 'php', 1 ); $allowed_files = $theme->get_files( 'php', 1 );
$has_templates = ! empty( $allowed_files );
$style_files = $theme->get_files( 'css' ); $style_files = $theme->get_files( 'css' );
if ( isset( $style_files['style.css'] ) ) { if ( isset( $style_files['style.css'] ) ) {
$allowed_files['style.css'] = $style_files['style.css']; $allowed_files['style.css'] = $style_files['style.css'];
@ -132,7 +134,7 @@ default:
<?php endif; <?php endif;
$description = get_file_description( $file ); $description = get_file_description( $file );
$file_show = array_search( $file, $allowed_files ); $file_show = array_search( $file, array_filter( $allowed_files ) );
if ( $description != $file_show ) if ( $description != $file_show )
$description .= ' <span>(' . $file_show . ')</span>'; $description .= ' <span>(' . $file_show . ')</span>';
?> ?>
@ -142,14 +144,14 @@ if ( $description != $file_show )
<div class="fileedit-sub"> <div class="fileedit-sub">
<div class="alignleft"> <div class="alignleft">
<h3><?php echo $theme->display('Name') . ': ' . $description; ?></h3> <h3><?php echo $theme->display('Name'); if ( $description ) echo ': ' . $description; ?></h3>
</div> </div>
<div class="alignright"> <div class="alignright">
<form action="theme-editor.php" method="post"> <form action="theme-editor.php" method="post">
<strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong> <strong><label for="theme"><?php _e('Select theme to edit:'); ?> </label></strong>
<select name="theme" id="theme"> <select name="theme" id="theme">
<?php <?php
foreach ( wp_get_themes() as $a_stylesheet => $a_theme ) { foreach ( wp_get_themes( array( 'errors' => null ) ) as $a_stylesheet => $a_theme ) {
$selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : ''; $selected = $a_stylesheet == $stylesheet ? ' selected="selected"' : '';
echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display('Name') . '</option>'; echo "\n\t" . '<option value="' . esc_attr( $a_stylesheet ) . '"' . $selected . '>' . $a_theme->display('Name') . '</option>';
} }
@ -162,7 +164,8 @@ foreach ( wp_get_themes() as $a_stylesheet => $a_theme ) {
</div> </div>
<div id="templateside"> <div id="templateside">
<?php <?php
if ( $allowed_files ) : if ( array_filter( $allowed_files ) ) :
if ( $has_templates || $theme->is_child_theme() ) :
?> ?>
<h3><?php _e('Templates'); ?></h3> <h3><?php _e('Templates'); ?></h3>
<?php if ( $theme->is_child_theme() ) : ?> <?php if ( $theme->is_child_theme() ) : ?>
@ -170,6 +173,8 @@ if ( $allowed_files ) :
<?php endif; ?> <?php endif; ?>
<ul> <ul>
<?php <?php
endif;
foreach ( $allowed_files as $filename => $absolute_filename ) : foreach ( $allowed_files as $filename => $absolute_filename ) :
if ( 'style.css' == $filename ) { if ( 'style.css' == $filename ) {
echo "\t</ul>\n\t<h3>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h3>\n\t<ul>\n"; echo "\t</ul>\n\t<h3>" . _x( 'Styles', 'Theme stylesheets in theme editor' ) . "</h3>\n\t<ul>\n";

View File

@ -199,14 +199,16 @@ final class WP_Theme implements ArrayAccess {
$this->errors = new WP_Error( 'theme_not_found', __( 'The theme directory does not exist.' ) ); $this->errors = new WP_Error( 'theme_not_found', __( 'The theme directory does not exist.' ) );
else else
$this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) ); $this->errors = new WP_Error( 'theme_no_stylesheet', __( 'Stylesheet is missing.' ) );
$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet ) ); $this->template = $this->stylesheet;
$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
if ( ! file_exists( $this->theme_root ) ) // Don't cache this one. if ( ! file_exists( $this->theme_root ) ) // Don't cache this one.
$this->errors->add( 'theme_root_missing', __( 'ERROR: The themes directory is either empty or doesn&#8217;t exist. Please check your installation.' ) ); $this->errors->add( 'theme_root_missing', __( 'ERROR: The themes directory is either empty or doesn&#8217;t exist. Please check your installation.' ) );
return; return;
} elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) { } elseif ( ! is_readable( $this->theme_root . '/' . $theme_file ) ) {
$this->headers['Name'] = $this->stylesheet; $this->headers['Name'] = $this->stylesheet;
$this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) ); $this->errors = new WP_Error( 'theme_stylesheet_not_readable', __( 'Stylesheet is not readable.' ) );
$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet ) ); $this->template = $this->stylesheet;
$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
return; return;
} else { } else {
$this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' ); $this->headers = get_file_data( $this->theme_root . '/' . $theme_file, self::$file_headers, 'theme' );
@ -218,13 +220,12 @@ final class WP_Theme implements ArrayAccess {
} }
} }
// (If template is set from cache, we know it's good.) // (If template is set from cache [and there are no errors], we know it's good.)
if ( ! $this->template && ! ( $this->template = $this->headers['Template'] ) ) { if ( ! $this->template && ! ( $this->template = $this->headers['Template'] ) ) {
if ( file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) { $this->template = $this->stylesheet;
$this->template = $this->stylesheet; if ( ! file_exists( $this->theme_root . '/' . $this->stylesheet . '/index.php' ) ) {
} else {
$this->errors = new WP_Error( 'theme_no_index', __( 'Template is missing.' ) ); $this->errors = new WP_Error( 'theme_no_index', __( 'Template is missing.' ) );
$this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet ) ); $this->cache_add( 'theme', array( 'headers' => $this->headers, 'errors' => $this->errors, 'stylesheet' => $this->stylesheet, 'template' => $this->template ) );
return; return;
} }
} }