Make WordPress Core


Ignore:
Timestamp:
04/25/2012 03:44:06 PM (13 years ago)
Author:
koopersmith
Message:

Theme Customizer: Preview the target theme during save to ensure all settings are properly registered. Temporarily revert to the active theme when running switch_theme. fixes #20508, see #19910.

Introduces WP_Customize->start_previewing_theme() and WP_Customize->stop_previewing_theme() to easily enable/disable the theme filters as needed.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-customize.php

    r20572 r20584  
    2828        require( ABSPATH . WPINC . '/class-wp-customize-control.php' );
    2929
    30         add_action( 'setup_theme',  array( $this, 'customize_previewing' ) );
     30        add_action( 'setup_theme',  array( $this, 'setup_theme' ) );
    3131        add_action( 'admin_init',   array( $this, 'admin_init' ) );
    3232        add_action( 'wp_loaded',    array( $this, 'wp_loaded' ) );
     
    6262     * @since 3.4.0
    6363     */
    64     public function customize_previewing() {
     64    public function setup_theme() {
    6565        if ( ! isset( $_REQUEST['customize'] ) || 'on' != $_REQUEST['customize'] )
    6666            return;
    6767
    68         if ( ! $this->set_theme() || isset( $_REQUEST['save_customize_controls'] ) )
    69             return;
     68        $this->start_previewing_theme();
     69        show_admin_bar( false );
     70    }
     71
     72    /**
     73     * Start previewing the selected theme.
     74     *
     75     * Adds filters to change the current theme.
     76     *
     77     * @since 3.4.0
     78     */
     79    public function start_previewing_theme() {
     80        if ( $this->is_preview() || false === $this->theme || ( $this->theme && ! $this->theme->exists() ) )
     81            return;
     82
     83        // Initialize $theme and $original_stylesheet if they do not yet exist.
     84        if ( ! isset( $this->theme ) ) {
     85            $this->theme = wp_get_theme( $_REQUEST['theme'] );
     86            if ( ! $this->theme->exists() ) {
     87                $this->theme = false;
     88                return;
     89            }
     90        }
     91
     92        $this->original_stylesheet = get_stylesheet();
    7093
    7194        $this->previewing = true;
    72 
    73         show_admin_bar( false );
    74 
    75         $this->original_stylesheet = get_stylesheet();
    7695
    7796        add_filter( 'template', array( $this, 'get_template' ) );
     
    87106        add_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
    88107
    89         do_action( 'customize_previewing' );
     108        do_action( 'start_previewing_theme' );
     109    }
     110
     111    /**
     112     * Stop previewing the selected theme.
     113     *
     114     * Removes filters to change the current theme.
     115     *
     116     * @since 3.4.0
     117     */
     118    public function stop_previewing_theme() {
     119        if ( ! $this->is_preview() )
     120            return;
     121
     122        $this->previewing = false;
     123
     124        remove_filter( 'template', array( $this, 'get_template' ) );
     125        remove_filter( 'stylesheet', array( $this, 'get_stylesheet' ) );
     126        remove_filter( 'pre_option_current_theme', array( $this, 'current_theme' ) );
     127
     128        // @link: http://core.trac.wordpress.org/ticket/20027
     129        remove_filter( 'pre_option_stylesheet', array( $this, 'get_stylesheet' ) );
     130        remove_filter( 'pre_option_template', array( $this, 'get_template' ) );
     131
     132        // Handle custom theme roots.
     133        remove_filter( 'pre_option_stylesheet_root', array( $this, 'get_stylesheet_root' ) );
     134        remove_filter( 'pre_option_template_root', array( $this, 'get_template_root' ) );
     135
     136        do_action( 'stop_previewing_theme' );
    90137    }
    91138
     
    161208
    162209    /**
    163      * Set the stylesheet name of the previewed theme.
    164      *
    165      * @since 3.4.0
    166      *
    167      * @return bool|string Stylesheet name.
    168      */
    169     public function set_theme() {
    170         if ( isset( $this->theme ) )
    171             return $this->theme;
    172 
    173         $this->theme = wp_get_theme( $_REQUEST['theme'] );
    174         if ( ! $this->theme->exists() )
    175             $this->theme = false;
    176 
    177         return $this->theme;
    178     }
    179 
    180     /**
    181210     * Retrieve the template name of the previewed theme.
    182211     *
     
    268297     */
    269298    public function save() {
    270         if ( $this->is_preview() )
     299        if ( ! $this->is_preview() )
    271300            return;
    272301
    273302        check_admin_referer( 'customize_controls' );
    274303
    275         if ( ! $this->set_theme() )
    276             return;
    277 
    278         $active_template   = get_template();
    279         $active_stylesheet = get_stylesheet();
    280 
    281304        // Do we have to switch themes?
    282         if ( $this->get_template() != $active_template || $this->get_stylesheet() != $active_stylesheet ) {
     305        if ( $this->get_stylesheet() != $this->original_stylesheet ) {
    283306            if ( ! current_user_can( 'switch_themes' ) )
    284307                return;
    285308
     309            // Temporarily stop previewing the theme to allow switch_themes()
     310            // to operate properly.
     311            $this->stop_previewing_theme();
    286312            switch_theme( $this->get_template(), $this->get_stylesheet() );
     313            $this->start_previewing_theme();
    287314        }
    288315
Note: See TracChangeset for help on using the changeset viewer.