Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor theme previews #50338

Merged
merged 3 commits into from
May 4, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 17 additions & 22 deletions lib/compat/wordpress-6.3/theme-previews.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,27 @@
*/

/**
* Filters the blog option to return the directory for the previewed theme.
* Filters the blog option to return the path for the previewed theme.
*
* @param string $current_stylesheet The current theme directory.
* @return string The previewed theme directory.
* @param string $current_stylesheet The current theme's stylesheet or template path.
* @return string The previewed theme's stylesheet or template path.
*/
function gutenberg_theme_preview_stylesheet( $current_stylesheet = null ) {
$preview_stylesheet = ! empty( $_GET['theme_preview'] ) ? $_GET['theme_preview'] : null;
$wp_theme = wp_get_theme( $preview_stylesheet );
if ( ! is_wp_error( $wp_theme->errors() ) && current_user_can( 'switch_themes' ) ) {
return sanitize_text_field( $preview_stylesheet );
function gutenberg_get_theme_preview_path( $current_stylesheet = null ) {
// Don't allow non-admins to preview themes.
if ( ! current_user_can( 'switch_themes' ) ) {
return;
}

return $current_stylesheet;
}

/**
* Filters the blog option to return the parent theme directory for the previewed theme.
*
* @param string $current_stylesheet The current theme directory.
* @return string The previewed theme directory.
*/
function gutenberg_theme_preview_template( $current_stylesheet = null ) {
$preview_stylesheet = ! empty( $_GET['theme_preview'] ) ? $_GET['theme_preview'] : null;
$wp_theme = wp_get_theme( $preview_stylesheet );
if ( ! is_wp_error( $wp_theme->errors() ) && current_user_can( 'switch_themes' ) ) {
return sanitize_text_field( $wp_theme->get_template() );
if ( ! is_wp_error( $wp_theme->errors() ) ) {
if ( current_filter() === 'stylesheet' ) {
$theme_path = $wp_theme->get_stylesheet();
} elseif ( current_filter() === 'template' ) {
$theme_path = $wp_theme->get_template();
}

return sanitize_text_field( $theme_path );
}

return $current_stylesheet;
Expand Down Expand Up @@ -124,8 +119,8 @@ function block_theme_activate_nonce() {
* Attaches filters to enable theme previews in the Site Editor.
*/
if ( ! empty( $_GET['theme_preview'] ) ) {
add_filter( 'stylesheet', 'gutenberg_theme_preview_stylesheet' );
add_filter( 'template', 'gutenberg_theme_preview_template' );
add_filter( 'stylesheet', 'gutenberg_get_theme_preview_path' );
add_filter( 'template', 'gutenberg_get_theme_preview_path' );
add_filter( 'init', 'gutenberg_attach_theme_preview_middleware' );
}

Expand Down