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

Backport: Block theme previews #4627

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3eef498
BP: Add Theme Previews for block themes #50030
MaggieCabrera Jun 16, 2023
fa4b2e5
BP: Add Theme Previews for block themes #50030
MaggieCabrera Jun 16, 2023
fce6400
BP: Theme Preview: Restrict to admin users #50335
MaggieCabrera Jun 16, 2023
2c48f38
BP: Refactor theme previews #50338
MaggieCabrera Jun 16, 2023
7658703
BP: Theme Previews: Fix refactor #50354
MaggieCabrera Jun 16, 2023
ea870d3
BP: Avoid changing non-theme-preview requests #51221
MaggieCabrera Jun 16, 2023
841c25e
BP: [Block Theme Previews] Change the URL query string for more safet…
MaggieCabrera Jun 16, 2023
5dc273c
Update src/wp-admin/includes/theme-previews.php
MaggieCabrera Jun 16, 2023
aabf5dc
Update src/wp-admin/includes/theme-previews.php
MaggieCabrera Jun 16, 2023
ebf6909
BP: Remove the experiment option for Block Theme Previews #50983
MaggieCabrera Jun 16, 2023
9aa8b1f
remove gb mentions
MaggieCabrera Jun 16, 2023
0d4a6f4
Enable block theme live preview button #4376
MaggieCabrera Jun 16, 2023
7c78729
Removed script to include the live preview button
MaggieCabrera Jun 16, 2023
c0376f7
phpcs
MaggieCabrera Jun 16, 2023
1270e3e
moved file to the correct place
MaggieCabrera Jun 16, 2023
8339d07
Revert "moved file to the correct place"
MaggieCabrera Jun 16, 2023
bf1eafa
changed the include of the file
MaggieCabrera Jun 16, 2023
ffbcbb0
improved dockblocks
MaggieCabrera Jun 19, 2023
596545c
check that is not null
MaggieCabrera Jun 19, 2023
86be126
remove whitespace
scruffian Jun 21, 2023
4a6581d
Update src/wp-admin/includes/theme-previews.php
MaggieCabrera Jun 21, 2023
e6182be
Fix PHPCS
tellthemachines Jun 23, 2023
88c7f9d
Remove extra space
tellthemachines Jun 23, 2023
4c32603
Move filters, change filter to an action and rename url param
scruffian Jun 23, 2023
f260cd5
Update src/wp-admin/includes/theme-previews.php
scruffian Jun 23, 2023
391e296
Update src/wp-admin/includes/theme-previews.php
scruffian Jun 23, 2023
a217191
Add back customize check
scruffian Jun 23, 2023
9971a7a
Update src/wp-admin/themes.php
MaggieCabrera Jun 26, 2023
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
7 changes: 7 additions & 0 deletions src/wp-admin/includes/admin-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,10 @@

// Append '(Draft)' to draft page titles in the privacy page dropdown.
add_filter( 'list_pages', '_wp_privacy_settings_filter_draft_page_titles', 10, 2 );

// Attaches filters to enable theme previews in the Site Editor.
if ( ! empty( $_GET['wp_theme_preview'] ) ) {
add_filter( 'stylesheet', 'wp_get_theme_preview_path' );
add_filter( 'template', 'wp_get_theme_preview_path' );
add_action( 'init', 'wp_attach_theme_preview_middleware' );
}
1 change: 1 addition & 0 deletions src/wp-admin/includes/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@

/** WordPress Theme Administration API */
require_once ABSPATH . 'wp-admin/includes/theme.php';
require_once ABSPATH . 'wp-admin/includes/theme-previews.php';

/** WordPress Privacy Functions */
require_once ABSPATH . 'wp-admin/includes/privacy-tools.php';
Expand Down
56 changes: 56 additions & 0 deletions src/wp-admin/includes/theme-previews.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Theme previews using the Site Editor for block themes.
*
* @package WordPress
*/

/**
* Filters the blog option to return the path for the previewed theme.
*
MaggieCabrera marked this conversation as resolved.
Show resolved Hide resolved
* @since 6.3.0
*
* @param string $current_stylesheet The current theme's stylesheet or template path.
* @return string The previewed theme's stylesheet or template path.
*/
function wp_get_theme_preview_path( $current_stylesheet = null ) {
if ( ! current_user_can( 'switch_themes' ) ) {
return $current_stylesheet;
}

$preview_stylesheet = ! empty( $_GET['wp_theme_preview'] ) ? sanitize_text_field( wp_unslash( $_GET['wp_theme_preview'] ) ) : null;
$wp_theme = wp_get_theme( $preview_stylesheet );
if ( ! is_wp_error( $wp_theme->errors() ) ) {
if ( current_filter() === 'template' ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is happening here? Is this detecting if the theme has a parent? If so, I remember using $wp_theme->parent().

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for looking at this Johnny. This function (wp_get_theme_preview_path) is applied on two different filters - stylesheet and template. The return value of the function needs to be different depending on whether we are running on the stylesheet or template filter. The call to current_filter is determining which filter we are on so we know which value to return.

$theme_path = $wp_theme->get_template();
} else {
$theme_path = $wp_theme->get_stylesheet();
}

return sanitize_text_field( $theme_path );
}

return $current_stylesheet;
}

/**
* Adds a middleware to `apiFetch` to set the theme for the preview.
MaggieCabrera marked this conversation as resolved.
Show resolved Hide resolved
* This adds a `wp_theme_preview` URL parameter to API requests from the Site Editor, so they also respond as if the theme is set to the value of the parameter.
*
* @since 6.3.0
*/
function wp_attach_theme_preview_middleware() {
// Don't allow non-admins to preview themes.
if ( ! current_user_can( 'switch_themes' ) ) {
return;
}

wp_add_inline_script(
'wp-api-fetch',
sprintf(
'wp.apiFetch.use( wp.apiFetch.createThemePreviewMiddleware( %s ) );',
wp_json_encode( sanitize_text_field( wp_unslash( $_GET['wp_theme_preview'] ) ) )
),
'after'
);
}
21 changes: 13 additions & 8 deletions src/wp-admin/includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -711,16 +711,21 @@ function wp_prepare_themes_for_js( $themes = null ) {
$is_block_theme = $theme->is_block_theme();

if ( $is_block_theme && $can_edit_theme_options ) {
$customize_action = esc_url( admin_url( 'site-editor.php' ) );
$customize_action = admin_url( 'site-editor.php' );
if ( $current_theme !== $slug ) {
$customize_action = add_query_arg( 'wp_theme_preview', $slug, $customize_action );
}
} elseif ( ! $is_block_theme && $can_customize && $can_edit_theme_options ) {
$customize_action = esc_url(
add_query_arg(
array(
'return' => urlencode( sanitize_url( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),
),
wp_customize_url( $slug )
)
$customize_action = wp_customize_url( $slug );
}
if ( null !== $customize_action ) {
$customize_action = add_query_arg(
array(
'return' => urlencode( sanitize_url( remove_query_arg( wp_removable_query_args(), wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) ),
),
$customize_action
);
$customize_action = esc_url( $customize_action );
}

$update_requires_wp = isset( $updates[ $slug ]['requires'] ) ? $updates[ $slug ]['requires'] : null;
Expand Down
15 changes: 7 additions & 8 deletions src/wp-admin/themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@
?>
<a class="button activate" href="<?php echo $theme['actions']['activate']; ?>" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Activate' ); ?></a>
<?php
if ( ! $theme['blockTheme'] && current_user_can( 'edit_theme_options' ) && current_user_can( 'customize' ) ) {
// Only classic themes require the "customize" capability.
if ( current_user_can( 'edit_theme_options' ) && ( $theme['blockTheme'] || current_user_can( 'customize' ) ) ) {
/* translators: %s: Theme name. */
$live_preview_aria_label = sprintf( _x( 'Live Preview %s', 'theme' ), '{{ data.name }}' );
?>
Expand Down Expand Up @@ -914,13 +915,11 @@ function wp_theme_auto_update_setting_template() {
$aria_label = sprintf( _x( 'Activate %s', 'theme' ), '{{ data.name }}' );
?>
<a class="button activate" href="{{{ data.actions.activate }}}" aria-label="<?php echo esc_attr( $aria_label ); ?>"><?php _e( 'Activate' ); ?></a>
<# if ( ! data.blockTheme ) { #>
<?php
/* translators: %s: Theme name. */
$live_preview_aria_label = sprintf( _x( 'Live Preview %s', 'theme' ), '{{ data.name }}' );
?>
<a aria-label="<?php echo esc_attr( $live_preview_aria_label ); ?>" class="button button-primary load-customize hide-if-no-customize" href="{{{ data.actions.customize }}}"><?php _e( 'Live Preview' ); ?></a>
<# } #>
<?php
/* translators: %s: Theme name. */
$live_preview_aria_label = sprintf( _x( 'Live Preview %s', 'theme' ), '{{ data.name }}' );
?>
<a aria-label="<?php echo esc_attr( $live_preview_aria_label ); ?>" class="button button-primary load-customize hide-if-no-customize" href="{{{ data.actions.customize }}}"><?php _e( 'Live Preview' ); ?></a>
<# } else { #>
<?php
/* translators: %s: Theme name. */
Expand Down
Loading