Theme Customizer: Block non-existent or non-allowed themes, unless the non-allowed theme is the active theme. Support a user having edit_theme_options xor switch_themes. fixes #20852.

git-svn-id: http://core.svn.wordpress.org/trunk@21010 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2012-06-06 20:34:24 +00:00
parent 23104564f0
commit 98b61d9a17
7 changed files with 67 additions and 57 deletions

View File

@ -102,7 +102,7 @@ $admin_body_class .= ' no-customize-support';
<?php <?php
// If the customize-loader script is enqueued, make sure the customize // If the customize-loader script is enqueued, make sure the customize
// body classes are correct as early as possible. // body classes are correct as early as possible.
if ( wp_script_is( 'customize-loader', 'queue' ) ) if ( wp_script_is( 'customize-loader', 'queue' ) && current_user_can( 'edit_theme_options' ) )
wp_customize_support_script(); wp_customize_support_script();
?> ?>

View File

@ -13,11 +13,6 @@ if ( ! current_user_can( 'edit_theme_options' ) )
global $wp_scripts, $wp_customize; global $wp_scripts, $wp_customize;
wp_reset_vars( array( 'theme' ) );
if ( ! $theme )
$theme = get_stylesheet();
$registered = $wp_scripts->registered; $registered = $wp_scripts->registered;
$wp_scripts = new WP_Scripts; $wp_scripts = new WP_Scripts;
$wp_scripts->registered = $registered; $wp_scripts->registered = $registered;
@ -48,7 +43,7 @@ do_action( 'customize_controls_print_scripts' );
</head> </head>
<body class="wp-full-overlay"> <body class="wp-full-overlay">
<form id="customize-controls" class="wrap wp-full-overlay-sidebar"> <form id="customize-controls" class="wrap wp-full-overlay-sidebar">
<?php wp_nonce_field( 'customize_controls' ); ?> <?php wp_nonce_field( 'customize_controls-' . $wp_customize->get_stylesheet() ); ?>
<div id="customize-header-actions" class="wp-full-overlay-header"> <div id="customize-header-actions" class="wp-full-overlay-header">
<?php <?php
$save_text = $wp_customize->is_theme_active() ? __( 'Save &amp; Publish' ) : __( 'Save &amp; Activate' ); $save_text = $wp_customize->is_theme_active() ? __( 'Save &amp; Publish' ) : __( 'Save &amp; Activate' );

View File

@ -125,21 +125,25 @@ class WP_Themes_List_Table extends WP_List_Table {
$version = $theme->display('Version'); $version = $theme->display('Version');
$author = $theme->display('Author'); $author = $theme->display('Author');
$activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=" . urlencode( $template ) . "&amp;stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $template ); $activate_link = wp_nonce_url( "themes.php?action=activate&amp;template=" . urlencode( $template ) . "&amp;stylesheet=" . urlencode( $stylesheet ), 'switch-theme_' . $stylesheet );
$preview_link = esc_url( add_query_arg( $preview_link = esc_url( add_query_arg(
array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => true, 'TB_iframe' => 'true' ), array( 'preview' => 1, 'template' => $template, 'stylesheet' => $stylesheet, 'preview_iframe' => true, 'TB_iframe' => 'true' ),
home_url( '/' ) ) ); home_url( '/' ) ) );
$actions = array(); $actions = array();
$actions[] = '<a href="' . $activate_link . '" class="activatelink" title="' $actions['activate'] = '<a href="' . $activate_link . '" class="activatelink" title="'
. esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>'; . esc_attr( sprintf( __( 'Activate &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Activate' ) . '</a>';
$actions[] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
. esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>' $actions['preview'] = '<a href="' . $preview_link . '" class="hide-if-customize" title="'
. '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">' . esc_attr( sprintf( __( 'Preview &#8220;%s&#8221;' ), $title ) ) . '">' . __( 'Preview' ) . '</a>';
. __( 'Live Preview' ) . '</a>';
if ( current_user_can( 'edit_theme_options' ) )
$actions['preview'] .= '<a href="' . wp_customize_url( $stylesheet ) . '" class="load-customize hide-if-no-customize">'
. __( 'Live Preview' ) . '</a>';
if ( ! is_multisite() && current_user_can( 'delete_themes' ) ) if ( ! is_multisite() && current_user_can( 'delete_themes' ) )
$actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( "themes.php?action=delete&amp;template=$stylesheet", 'delete-theme_' . $stylesheet ) $actions['delete'] = '<a class="submitdelete deletion" href="' . wp_nonce_url( "themes.php?action=delete&amp;stylesheet=$stylesheet", 'delete-theme_' . $stylesheet )
. '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) ) . '" onclick="' . "return confirm( '" . esc_js( sprintf( __( "You are about to delete this theme '%s'\n 'Cancel' to stop, 'OK' to delete." ), $title ) )
. "' );" . '">' . __( 'Delete' ) . '</a>'; . "' );" . '">' . __( 'Delete' ) . '</a>';

View File

@ -11,19 +11,19 @@
* *
* @since 2.8.0 * @since 2.8.0
* *
* @param string $template Template directory of the theme to delete * @param string $stylesheet Stylesheet of the theme to delete
* @param string $redirect Redirect to page when complete. * @param string $redirect Redirect to page when complete.
* @return mixed * @return mixed
*/ */
function delete_theme($template, $redirect = '') { function delete_theme($stylesheet, $redirect = '') {
global $wp_filesystem; global $wp_filesystem;
if ( empty($template) ) if ( empty($stylesheet) )
return false; return false;
ob_start(); ob_start();
if ( empty( $redirect ) ) if ( empty( $redirect ) )
$redirect = wp_nonce_url('themes.php?action=delete&template=' . $template, 'delete-theme_' . $template); $redirect = wp_nonce_url('themes.php?action=delete&stylesheet=' . $stylesheet, 'delete-theme_' . $stylesheet);
if ( false === ($credentials = request_filesystem_credentials($redirect)) ) { if ( false === ($credentials = request_filesystem_credentials($redirect)) ) {
$data = ob_get_contents(); $data = ob_get_contents();
ob_end_clean(); ob_end_clean();
@ -61,11 +61,11 @@ function delete_theme($template, $redirect = '') {
return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.')); return new WP_Error('fs_no_themes_dir', __('Unable to locate WordPress theme directory.'));
$themes_dir = trailingslashit( $themes_dir ); $themes_dir = trailingslashit( $themes_dir );
$theme_dir = trailingslashit($themes_dir . $template); $theme_dir = trailingslashit($themes_dir . $stylesheet);
$deleted = $wp_filesystem->delete($theme_dir, true); $deleted = $wp_filesystem->delete($theme_dir, true);
if ( ! $deleted ) if ( ! $deleted )
return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $template) ); return new WP_Error('could_not_remove_theme', sprintf(__('Could not fully remove the theme %s.'), $stylesheet) );
// Force refresh of theme update information // Force refresh of theme update information
delete_site_transient('update_themes'); delete_site_transient('update_themes');

View File

@ -16,15 +16,19 @@ $wp_list_table = _get_list_table('WP_Themes_List_Table');
if ( current_user_can( 'switch_themes' ) && isset($_GET['action'] ) ) { if ( current_user_can( 'switch_themes' ) && isset($_GET['action'] ) ) {
if ( 'activate' == $_GET['action'] ) { if ( 'activate' == $_GET['action'] ) {
check_admin_referer('switch-theme_' . $_GET['template']); check_admin_referer('switch-theme_' . $_GET['stylesheet']);
$theme = wp_get_theme( $_GET['stylesheet'] );
if ( ! $theme->exists() || ! $theme->is_allowed() )
wp_die( __( 'Cheatin&#8217; uh?' ) );
switch_theme($_GET['template'], $_GET['stylesheet']); switch_theme($_GET['template'], $_GET['stylesheet']);
wp_redirect( admin_url('themes.php?activated=true') ); wp_redirect( admin_url('themes.php?activated=true') );
exit; exit;
} elseif ( 'delete' == $_GET['action'] ) { } elseif ( 'delete' == $_GET['action'] ) {
check_admin_referer('delete-theme_' . $_GET['template']); check_admin_referer('delete-theme_' . $_GET['stylesheet']);
if ( !current_user_can('delete_themes') ) $theme = wp_get_theme( $_GET['stylesheet'] );
if ( !current_user_can('delete_themes') || ! $theme->exists() )
wp_die( __( 'Cheatin&#8217; uh?' ) ); wp_die( __( 'Cheatin&#8217; uh?' ) );
delete_theme($_GET['template']); delete_theme($_GET['stylesheet']);
wp_redirect( admin_url('themes.php?deleted=true') ); wp_redirect( admin_url('themes.php?deleted=true') );
exit; exit;
} }
@ -60,6 +64,8 @@ if ( current_user_can( 'install_themes' ) ) {
) ); ) );
} }
endif; // switch_themes
if ( current_user_can( 'edit_theme_options' ) ) { if ( current_user_can( 'edit_theme_options' ) ) {
$help_customize = $help_customize =
'<p>' . __('Click on the "Live Preview" link under any theme to preview that theme and change theme options in a separate, full-screen view. Any installed theme can be previewed and customized in this way.') . '</p>'. '<p>' . __('Click on the "Live Preview" link under any theme to preview that theme and change theme options in a separate, full-screen view. Any installed theme can be previewed and customized in this way.') . '</p>'.
@ -83,8 +89,6 @@ get_current_screen()->set_help_sidebar(
wp_enqueue_script( 'theme' ); wp_enqueue_script( 'theme' );
wp_enqueue_script( 'customize-loader' ); wp_enqueue_script( 'customize-loader' );
endif;
require_once('./admin-header.php'); require_once('./admin-header.php');
?> ?>
@ -120,9 +124,11 @@ $customize_title = sprintf( __( 'Customize &#8220;%s&#8221;' ), $ct->display('Na
?> ?>
<div id="current-theme" class="<?php echo esc_attr( $class ); ?>"> <div id="current-theme" class="<?php echo esc_attr( $class ); ?>">
<?php if ( $screenshot ) : ?> <?php if ( $screenshot ) : ?>
<?php if ( current_user_can( 'edit_theme_options' ) ) : ?>
<a href="<?php echo wp_customize_url(); ?>" class="load-customize hide-if-no-customize" title="<?php echo esc_attr( $customize_title ); ?>"> <a href="<?php echo wp_customize_url(); ?>" class="load-customize hide-if-no-customize" title="<?php echo esc_attr( $customize_title ); ?>">
<img src="<?php echo esc_url( $screenshot ); ?>" alt="<?php esc_attr_e( 'Current theme preview' ); ?>" /> <img src="<?php echo esc_url( $screenshot ); ?>" alt="<?php esc_attr_e( 'Current theme preview' ); ?>" />
</a> </a>
<?php endif; ?>
<img class="hide-if-customize" src="<?php echo esc_url( $screenshot ); ?>" alt="<?php esc_attr_e( 'Current theme preview' ); ?>" /> <img class="hide-if-customize" src="<?php echo esc_url( $screenshot ); ?>" alt="<?php esc_attr_e( 'Current theme preview' ); ?>" />
<?php endif; ?> <?php endif; ?>
@ -140,9 +146,6 @@ $customize_title = sprintf( __( 'Customize &#8220;%s&#8221;' ), $ct->display('Na
<?php theme_update_available( $ct ); ?> <?php theme_update_available( $ct ); ?>
</div> </div>
<div class="theme-options">
<a id="customize-current-theme-link" href="<?php echo wp_customize_url(); ?>" class="load-customize hide-if-no-customize" title="<?php echo esc_attr( $customize_title ); ?>"><?php _e( 'Customize' )?></a>
<span><?php _e( 'Options:' )?></span>
<?php <?php
// Pretend you didn't see this. // Pretend you didn't see this.
$options = array(); $options = array();
@ -171,13 +174,26 @@ $customize_title = sprintf( __( 'Customize &#8220;%s&#8221;' ), $ct->display('Na
} }
} }
if ( $options || current_user_can( 'edit_theme_options' ) ) :
?>
<div class="theme-options">
<?php if ( current_user_can( 'edit_theme_options' ) ) : ?>
<a id="customize-current-theme-link" href="<?php echo wp_customize_url(); ?>" class="load-customize hide-if-no-customize" title="<?php echo esc_attr( $customize_title ); ?>"><?php _e( 'Customize' ); ?></a>
<?php
endif; // edit_theme_options
if ( $options ) :
?>
<span><?php _e( 'Options:' )?></span>
<ul>
<?php foreach ( $options as $option ) : ?>
<li><?php echo $option; ?></li>
<?php endforeach; ?>
</ul>
</div>
<?php
endif; // options
endif; // options || edit_theme_options
?> ?>
<ul>
<?php foreach ( $options as $option ) : ?>
<li><?php echo $option; ?></li>
<?php endforeach; ?>
</ul>
</div>
</div> </div>

View File

@ -78,11 +78,22 @@ final class WP_Customize_Manager {
* @since 3.4.0 * @since 3.4.0
*/ */
public function setup_theme() { public function setup_theme() {
if ( ! ( isset( $_REQUEST['customize'] ) && 'on' == $_REQUEST['customize'] ) && ! basename( $_SERVER['PHP_SELF'] ) == 'customize.php' )
return;
send_origin_headers(); send_origin_headers();
$this->original_stylesheet = get_stylesheet();
$this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
// You can't preview a theme if it doesn't exist, or if it is not allowed (unless active).
if ( ! $this->theme->exists() )
wp_die( __( 'Cheatin&#8217; uh?' ) );
if ( $this->theme->get_stylesheet() != get_stylesheet() && ( ! $this->theme()->is_allowed() || ! current_user_can( 'switch_themes' ) ) )
wp_die( __( 'Cheatin&#8217; uh?' ) );
if ( ! current_user_can( 'edit_theme_options' ) )
wp_die( __( 'Cheatin&#8217; uh?' ) );
$this->start_previewing_theme(); $this->start_previewing_theme();
show_admin_bar( false ); show_admin_bar( false );
} }
@ -95,20 +106,10 @@ final class WP_Customize_Manager {
* @since 3.4.0 * @since 3.4.0
*/ */
public function start_previewing_theme() { public function start_previewing_theme() {
if ( $this->is_preview() || false === $this->theme || ( $this->theme && ! $this->theme->exists() ) ) // Bail if we're already previewing.
if ( $this->is_preview() )
return; return;
// Initialize $theme and $original_stylesheet if they do not yet exist.
if ( ! isset( $this->theme ) ) {
$this->theme = wp_get_theme( isset( $_REQUEST['theme'] ) ? $_REQUEST['theme'] : null );
if ( ! $this->theme->exists() ) {
$this->theme = false;
return;
}
}
$this->original_stylesheet = get_stylesheet();
$this->previewing = true; $this->previewing = true;
add_filter( 'template', array( $this, 'get_template' ) ); add_filter( 'template', array( $this, 'get_template' ) );
@ -419,13 +420,10 @@ final class WP_Customize_Manager {
if ( ! $this->is_preview() ) if ( ! $this->is_preview() )
die; die;
check_ajax_referer( 'customize_controls', 'nonce' ); check_ajax_referer( 'customize_controls-' . $this->get_stylesheet(), 'nonce' );
// Do we have to switch themes? // Do we have to switch themes?
if ( $this->get_stylesheet() != $this->original_stylesheet ) { if ( $this->get_stylesheet() != $this->original_stylesheet ) {
if ( ! current_user_can( 'switch_themes' ) )
die;
// Temporarily stop previewing the theme to allow switch_themes() // Temporarily stop previewing the theme to allow switch_themes()
// to operate properly. // to operate properly.
$this->stop_previewing_theme(); $this->stop_previewing_theme();

View File

@ -17,12 +17,9 @@ if ( typeof wp === 'undefined' )
// Ensure the loader is supported. // Ensure the loader is supported.
// Check for settings, postMessage support, and whether we require CORS support. // Check for settings, postMessage support, and whether we require CORS support.
if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) { if ( ! Loader.settings || ! $.support.postMessage || ( ! $.support.cors && Loader.settings.isCrossDomain ) ) {
this.body.removeClass( 'customize-support' ).addClass( 'no-customize-support' );
return; return;
} }
this.body.removeClass( 'no-customize-support' ).addClass( 'customize-support' );
this.window = $( window ); this.window = $( window );
this.element = $( '<div id="customize-container" />' ).appendTo( this.body ); this.element = $( '<div id="customize-container" />' ).appendTo( this.body );