<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[21069] trunk: Theme Customizer: Validate themes with more than just an existence check.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg > ul, #logmsg > ol { margin-left: 0; margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/21069">21069</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2012-06-12 18:39:16 +0000 (Tue, 12 Jun 2012)</dd>
</dl>

<h3>Log Message</h3>
<pre>Theme Customizer: Validate themes with more than just an existence check.
 * The current theme goes through validate_current_theme().
 * If doing a preview of a different theme, we check theme-&gt;errors().

Also:
 * Don't attach previewing hooks when previewing the current theme.
Aside from being unnecessary, this prevents issues with a theme with
the error of theme_parent_invalid.
 * Call send_origin_headers() earlier, to allow wp_die( '0' ) to properly
be returned in a domain mapping situation.
 * Fix the 'Save &amp; Activate' message on themes.php.

fixes <a href="http://core.trac.wordpress.org/ticket/20921">#20921</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadmincustomizephp">trunk/wp-admin/customize.php</a></li>
<li><a href="#trunkwpadminthemesphp">trunk/wp-admin/themes.php</a></li>
<li><a href="#trunkwpincludesclasswpcustomizemanagerphp">trunk/wp-includes/class-wp-customize-manager.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadmincustomizephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/customize.php (21068 => 21069)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/customize.php        2012-06-12 17:58:23 UTC (rev 21068)
+++ trunk/wp-admin/customize.php        2012-06-12 18:39:16 UTC (rev 21069)
</span><span class="lines">@@ -161,7 +161,7 @@
</span><span class="cx">                 'url'      =&gt; array(
</span><span class="cx">                         'preview'       =&gt; esc_url( $url ? $url : home_url( '/' ) ),
</span><span class="cx">                         'parent'        =&gt; esc_url( admin_url() ),
</span><del>-                        'activated'     =&gt; esc_url( admin_url( 'themes.php?activated=true' ) ),
</del><ins>+                        'activated'     =&gt; admin_url( 'themes.php?activated=true&amp;previewed' ),
</ins><span class="cx">                         'ajax'          =&gt; esc_url( admin_url( 'admin-ajax.php', 'relative' ) ),
</span><span class="cx">                         'allowed'       =&gt; array_map( 'esc_url', $allowed_urls ),
</span><span class="cx">                         'isCrossDomain' =&gt; $cross_domain,
</span></span></pre></div>
<a id="trunkwpadminthemesphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/themes.php (21068 => 21069)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/themes.php        2012-06-12 17:58:23 UTC (rev 21068)
+++ trunk/wp-admin/themes.php        2012-06-12 18:39:16 UTC (rev 21069)
</span><span class="lines">@@ -92,18 +92,6 @@
</span><span class="cx"> require_once('./admin-header.php');
</span><span class="cx"> ?&gt;
</span><span class="cx"> 
</span><del>-&lt;?php if ( ! validate_current_theme() ) : ?&gt;
-&lt;div id=&quot;message1&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php _e('The active theme is broken. Reverting to the default theme.'); ?&gt;&lt;/p&gt;&lt;/div&gt;
-&lt;?php elseif ( isset($_GET['activated']) ) :
-                if ( isset($wp_registered_sidebars) &amp;&amp; count( (array) $wp_registered_sidebars ) &amp;&amp; current_user_can('edit_theme_options') ) { ?&gt;
-&lt;div id=&quot;message2&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php printf( __('New theme activated. This theme supports widgets, please visit the &lt;a href=&quot;%s&quot;&gt;widgets settings&lt;/a&gt; screen to configure them.'), admin_url( 'widgets.php' ) ); ?&gt;&lt;/p&gt;&lt;/div&gt;&lt;?php
-                } else { ?&gt;
-&lt;div id=&quot;message2&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php printf( __( 'New theme activated. &lt;a href=&quot;%s&quot;&gt;Visit site&lt;/a&gt;' ), home_url( '/' ) ); ?&gt;&lt;/p&gt;&lt;/div&gt;&lt;?php
-                }
-        elseif ( isset($_GET['deleted']) ) : ?&gt;
-&lt;div id=&quot;message3&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php _e('Theme deleted.') ?&gt;&lt;/p&gt;&lt;/div&gt;
-&lt;?php endif; ?&gt;
-
</del><span class="cx"> &lt;div class=&quot;wrap&quot;&gt;&lt;?php
</span><span class="cx"> screen_icon();
</span><span class="cx"> if ( ! is_multisite() &amp;&amp; current_user_can( 'install_themes' ) ) : ?&gt;
</span><span class="lines">@@ -114,6 +102,20 @@
</span><span class="cx"> &lt;?php endif; ?&gt;
</span><span class="cx"> &lt;/h2&gt;
</span><span class="cx"> &lt;?php
</span><ins>+if ( ! validate_current_theme() || isset( $_GET['broken'] ) ) : ?&gt;
+&lt;div id=&quot;message1&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php _e('The active theme is broken. Reverting to the default theme.'); ?&gt;&lt;/p&gt;&lt;/div&gt;
+&lt;?php elseif ( isset($_GET['activated']) ) :
+                if ( isset( $_GET['previewed'] ) ) { ?&gt;
+                &lt;div id=&quot;message2&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php printf( __( 'Settings saved and theme activated. &lt;a href=&quot;%s&quot;&gt;Visit site&lt;/a&gt;.' ), home_url( '/' ) ); ?&gt;&lt;/p&gt;&lt;/div&gt;
+                &lt;?php } elseif ( isset($wp_registered_sidebars) &amp;&amp; count( (array) $wp_registered_sidebars ) &amp;&amp; current_user_can('edit_theme_options') ) { ?&gt;
+&lt;div id=&quot;message2&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php printf( __('New theme activated. This theme supports widgets, please visit the &lt;a href=&quot;%s&quot;&gt;widgets settings&lt;/a&gt; screen to configure them.'), admin_url( 'widgets.php' ) ); ?&gt;&lt;/p&gt;&lt;/div&gt;&lt;?php
+                } else { ?&gt;
+&lt;div id=&quot;message2&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php printf( __( 'New theme activated. &lt;a href=&quot;%s&quot;&gt;Visit site&lt;/a&gt;' ), home_url( '/' ) ); ?&gt;&lt;/p&gt;&lt;/div&gt;&lt;?php
+                }
+        elseif ( isset($_GET['deleted']) ) : ?&gt;
+&lt;div id=&quot;message3&quot; class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php _e('Theme deleted.') ?&gt;&lt;/p&gt;&lt;/div&gt;
+&lt;?php
+endif;
</ins><span class="cx"> 
</span><span class="cx"> $ct = wp_get_theme();
</span><span class="cx"> $screenshot = $ct-&gt;get_screenshot();
</span></span></pre></div>
<a id="trunkwpincludesclasswpcustomizemanagerphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/class-wp-customize-manager.php (21068 => 21069)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/class-wp-customize-manager.php        2012-06-12 17:58:23 UTC (rev 21068)
+++ trunk/wp-includes/class-wp-customize-manager.php        2012-06-12 18:39:16 UTC (rev 21069)
</span><span class="lines">@@ -72,10 +72,13 @@
</span><span class="cx">          *
</span><span class="cx">          * @since 3.4.0
</span><span class="cx">          */
</span><del>-        private function wp_die( $ajax_message, $message ) {
</del><ins>+        protected function wp_die( $ajax_message, $message = null ) {
</ins><span class="cx">                 if ( $this-&gt;doing_ajax() )
</span><span class="cx">                         wp_die( $ajax_message );
</span><span class="cx"> 
</span><ins>+                if ( ! $message )
+                        $message = __( 'Cheatin&amp;#8217; uh?' );
+
</ins><span class="cx">                 wp_die( $message );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -98,31 +101,47 @@
</span><span class="cx">          * @since 3.4.0
</span><span class="cx">          */
</span><span class="cx">         public function setup_theme() {
</span><ins>+                send_origin_headers();
+
</ins><span class="cx">                 if ( is_admin() &amp;&amp; ! $this-&gt;doing_ajax() )
</span><span class="cx">                     auth_redirect();
</span><del>-                elseif ( $this-&gt;doing_ajax() &amp;&amp; ! is_user_logged_in())
-                    wp_die( 0 );
</del><ins>+                elseif ( $this-&gt;doing_ajax() &amp;&amp; ! is_user_logged_in() )
+                    $this-&gt;wp_die( 0 );
</ins><span class="cx"> 
</span><del>-                send_origin_headers();
</del><ins>+                show_admin_bar( false );
</ins><span class="cx"> 
</span><ins>+                if ( ! current_user_can( 'edit_theme_options' ) )
+                        $this-&gt;wp_die( -1 );
+
</ins><span class="cx">                 $this-&gt;original_stylesheet = get_stylesheet();
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
</span><span class="cx"> 
</span><del>-                // You can't preview a theme if it doesn't exist, or if it is not allowed (unless active).
-                if ( ! $this-&gt;theme-&gt;exists() )
-                        $this-&gt;wp_die( -1, __( 'Cheatin&amp;#8217; uh?' ) );
</del><ins>+                if ( $this-&gt;is_theme_active() ) {
+                        // Once the theme is loaded, we'll validate it.
+                        add_action( 'after_setup_theme', array( $this, 'after_setup_theme' ) );
+                } else {
+                        if ( ! current_user_can( 'switch_themes' ) )
+                                $this-&gt;wp_die( -1 );
</ins><span class="cx"> 
</span><del>-                if ( $this-&gt;theme-&gt;get_stylesheet() != get_stylesheet() &amp;&amp; ( ! $this-&gt;theme()-&gt;is_allowed() || ! current_user_can( 'switch_themes' ) ) )
-                        $this-&gt;wp_die( -1, __( 'Cheatin&amp;#8217; uh?' ) );
</del><ins>+                        // If the theme isn't active, you can't preview it if it is not allowed or has errors.
+                        if ( $this-&gt;theme()-&gt;errors() )
+                                $this-&gt;wp_die( -1 );
</ins><span class="cx"> 
</span><del>-                if ( ! current_user_can( 'edit_theme_options' ) )
-                        $this-&gt;wp_die( -1, __( 'Cheatin&amp;#8217; uh?' ) );
</del><ins>+                        if ( ! $this-&gt;theme()-&gt;is_allowed() )
+                                $this-&gt;wp_die( -1 );
+                }
</ins><span class="cx"> 
</span><span class="cx">                 $this-&gt;start_previewing_theme();
</span><del>-                show_admin_bar( false );
</del><span class="cx">         }
</span><span class="cx"> 
</span><ins>+        function after_setup_theme() {
+                if ( ! $this-&gt;doing_ajax() &amp;&amp; ! validate_current_theme() ) {
+                        wp_redirect( 'themes.php?broken=true' );
+                        exit;
+                }
+        }
+
</ins><span class="cx">         /**
</span><span class="cx">          * Start previewing the selected theme.
</span><span class="cx">          *
</span><span class="lines">@@ -137,18 +156,20 @@
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;previewing = true;
</span><span class="cx"> 
</span><del>-                add_filter( 'template', array( $this, 'get_template' ) );
-                add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
-                add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
</del><ins>+                if ( ! $this-&gt;is_theme_active() ) {
+                        add_filter( 'template', array( $this, 'get_template' ) );
+                        add_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
+                        add_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
+        
+                        // @link: http://core.trac.wordpress.org/ticket/20027
+                        add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
+                        add_filter( 'pre_option_template', array( $this, 'get_template' ) );
+        
+                        // Handle custom theme roots.
+                        add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
+                        add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
+                }
</ins><span class="cx"> 
</span><del>-                // @link: http://core.trac.wordpress.org/ticket/20027
-                add_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
-                add_filter( 'pre_option_template', array( $this, 'get_template' ) );
-
-                // Handle custom theme roots.
-                add_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
-                add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
-
</del><span class="cx">                 do_action( 'start_previewing_theme', $this );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -165,18 +186,20 @@
</span><span class="cx"> 
</span><span class="cx">                 $this-&gt;previewing = false;
</span><span class="cx"> 
</span><del>-                remove_filter( 'template', array( $this, 'get_template' ) );
-                remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
-                remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
</del><ins>+                if ( ! $this-&gt;is_theme_active() ) {
+                        remove_filter( 'template', array( $this, 'get_template' ) );
+                        remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
+                        remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
+        
+                        // @link: http://core.trac.wordpress.org/ticket/20027
+                        remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
+                        remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
+        
+                        // Handle custom theme roots.
+                        remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
+                        remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
+                }
</ins><span class="cx"> 
</span><del>-                // @link: http://core.trac.wordpress.org/ticket/20027
-                remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
-                remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
-
-                // Handle custom theme roots.
-                remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
-                remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
-
</del><span class="cx">                 do_action( 'stop_previewing_theme', $this );
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -389,7 +412,7 @@
</span><span class="cx">          * @return string Template name.
</span><span class="cx">          */
</span><span class="cx">         public function get_template() {
</span><del>-                return $this-&gt;theme-&gt;get_template();
</del><ins>+                return $this-&gt;theme()-&gt;get_template();
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -400,7 +423,7 @@
</span><span class="cx">          * @return string Stylesheet name.
</span><span class="cx">          */
</span><span class="cx">         public function get_stylesheet() {
</span><del>-                return $this-&gt;theme-&gt;get_stylesheet();
</del><ins>+                return $this-&gt;theme()-&gt;get_stylesheet();
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -433,7 +456,7 @@
</span><span class="cx">          * @return string Theme name.
</span><span class="cx">          */
</span><span class="cx">         public function current_theme( $current_theme ) {
</span><del>-                return $this-&gt;theme-&gt;display('Name');
</del><ins>+                return $this-&gt;theme()-&gt;display('Name');
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><span class="lines">@@ -448,7 +471,7 @@
</span><span class="cx">                 check_ajax_referer( 'customize_controls-' . $this-&gt;get_stylesheet(), 'nonce' );
</span><span class="cx"> 
</span><span class="cx">                 // Do we have to switch themes?
</span><del>-                if ( $this-&gt;get_stylesheet() != $this-&gt;original_stylesheet ) {
</del><ins>+                if ( ! $this-&gt;is_theme_active() ) {
</ins><span class="cx">                         // Temporarily stop previewing the theme to allow switch_themes()
</span><span class="cx">                         // to operate properly.
</span><span class="cx">                         $this-&gt;stop_previewing_theme();
</span><span class="lines">@@ -462,23 +485,10 @@
</span><span class="cx">                         $setting-&gt;save();
</span><span class="cx">                 }
</span><span class="cx"> 
</span><del>-                add_action( 'admin_notices', array( $this, '_save_feedback' ) );
-
</del><span class="cx">                 die;
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         /**
</span><del>-         * Show an admin notice after settings are saved.
-         *
-         * @since 3.4.0
-         */
-        public function _save_feedback() {
-                ?&gt;
-                &lt;div class=&quot;updated&quot;&gt;&lt;p&gt;&lt;?php printf( __( 'Settings saved and theme activated. &lt;a href=&quot;%s&quot;&gt;Visit site&lt;/a&gt;.' ), home_url( '/' ) ); ?&gt;&lt;/p&gt;&lt;/div&gt;
-                &lt;?php
-        }
-
-        /**
</del><span class="cx">          * Add a customize setting.
</span><span class="cx">          *
</span><span class="cx">          * @since 3.4.0
</span></span></pre>
</div>
</div>

</body>
</html>