From fc5479111ea86a4d75c7fdd5628db468ea0e821c Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Wed, 21 Jun 2023 13:40:36 +0200 Subject: [PATCH 1/3] renamed get variable and loaded actions and filters conditionally Co-authored-by: Andrei Draganescu <107534+draganescu@users.noreply.github.com> --- lib/compat/wordpress-6.3/theme-previews.php | 15 ++++++------- .../src/middlewares/theme-preview.js | 21 ++++++++++++------- .../edit-site/src/components/routes/link.js | 2 +- .../leaf-more-menu.js | 4 ++-- .../src/utils/is-previewing-theme.js | 5 ++--- .../edit-site/src/utils/use-activate-theme.js | 2 +- 6 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lib/compat/wordpress-6.3/theme-previews.php b/lib/compat/wordpress-6.3/theme-previews.php index d5334b319ff8a2..5ca79c2de84b40 100644 --- a/lib/compat/wordpress-6.3/theme-previews.php +++ b/lib/compat/wordpress-6.3/theme-previews.php @@ -17,7 +17,7 @@ function gutenberg_get_theme_preview_path( $current_stylesheet = null ) { return $current_stylesheet; } - $preview_stylesheet = ! empty( $_GET['gutenberg_theme_preview'] ) ? $_GET['gutenberg_theme_preview'] : null; + $preview_stylesheet = ! empty( $_GET['wp_theme_preview'] ) ? $_GET['wp_theme_preview'] : null; $wp_theme = wp_get_theme( $preview_stylesheet ); if ( ! is_wp_error( $wp_theme->errors() ) ) { if ( current_filter() === 'template' ) { @@ -45,7 +45,7 @@ function gutenberg_attach_theme_preview_middleware() { 'wp-api-fetch', sprintf( 'wp.apiFetch.use( wp.apiFetch.createThemePreviewMiddleware( %s ) );', - wp_json_encode( sanitize_text_field( $_GET['gutenberg_theme_preview'] ) ) + wp_json_encode( sanitize_text_field( $_GET['wp_theme_preview'] ) ) ), 'after' ); @@ -88,7 +88,7 @@ function addLivePreviewButton() { livePreviewButton.setAttribute('class', 'button button-primary'); livePreviewButton.setAttribute( 'href', - `/wp-admin/site-editor.php?gutenberg_theme_preview=${themePath}&return=themes.php` + `/wp-admin/site-editor.php?wp_theme_preview=${themePath}&return=themes.php` ); livePreviewButton.innerHTML = ''; themeInfo.querySelector('.theme-actions').appendChild(livePreviewButton); @@ -115,12 +115,13 @@ function block_theme_activate_nonce() { /** * Attaches filters to enable theme previews in the Site Editor. */ -if ( ! empty( $_GET['gutenberg_theme_preview'] ) ) { +if ( ! empty( $_GET['wp_theme_preview'] ) && ! function_exists( 'wp_get_theme_preview_path' ) ) { add_filter( 'stylesheet', 'gutenberg_get_theme_preview_path' ); add_filter( 'template', 'gutenberg_get_theme_preview_path' ); add_filter( 'init', 'gutenberg_attach_theme_preview_middleware' ); } -add_action( 'admin_head', 'block_theme_activate_nonce' ); -add_action( 'admin_print_footer_scripts', 'add_live_preview_button', 11 ); - +if ( ! function_exists( 'wp_get_theme_preview_path' ) ) { + add_action( 'admin_head', 'block_theme_activate_nonce' ); + add_action( 'admin_print_footer_scripts', 'add_live_preview_button', 11 ); +} \ No newline at end of file diff --git a/packages/api-fetch/src/middlewares/theme-preview.js b/packages/api-fetch/src/middlewares/theme-preview.js index c8f4c0b7aeaf13..b0d3540ce91193 100644 --- a/packages/api-fetch/src/middlewares/theme-preview.js +++ b/packages/api-fetch/src/middlewares/theme-preview.js @@ -4,7 +4,7 @@ import { addQueryArgs, hasQueryArg } from '@wordpress/url'; /** - * This appends a `gutenberg_theme_preview` parameter to the REST API request URL if + * This appends a `wp_theme_preview` parameter to the REST API request URL if * the admin URL contains a `theme` GET parameter. * * @param {Record} themePath @@ -13,20 +13,27 @@ import { addQueryArgs, hasQueryArg } from '@wordpress/url'; const createThemePreviewMiddleware = ( themePath ) => ( options, next ) => { if ( typeof options.url === 'string' && - ! hasQueryArg( options.url, 'gutenberg_theme_preview' ) + ! hasQueryArg( options.url, 'wp_theme_preview' ) ) { options.url = addQueryArgs( options.url, { - gutenberg_theme_preview: themePath, + wp_theme_preview: themePath, } ); } + //window.__experimentalThemePreview if ( typeof options.path === 'string' && - ! hasQueryArg( options.path, 'gutenberg_theme_preview' ) + ! hasQueryArg( options.path, 'wp_theme_preview' ) ) { - options.path = addQueryArgs( options.path, { - gutenberg_theme_preview: themePath, - } ); + if ( window.__experimentalThemePreview ) { + options.path = addQueryArgs( options.path, { + wp_theme_preview: themePath, + } ); + } else { + options.path = addQueryArgs( options.path, { + wp_theme_preview: themePath, + } ); + } } return next( options ); diff --git a/packages/edit-site/src/components/routes/link.js b/packages/edit-site/src/components/routes/link.js index 1f386ef0bb3dc4..3191e6b9c6f3ac 100644 --- a/packages/edit-site/src/components/routes/link.js +++ b/packages/edit-site/src/components/routes/link.js @@ -37,7 +37,7 @@ export function useLink( params = {}, state, shouldReplace = false ) { if ( isPreviewingTheme() ) { params = { ...params, - gutenberg_theme_preview: currentlyPreviewingTheme(), + wp_theme_preview: currentlyPreviewingTheme(), }; } diff --git a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/leaf-more-menu.js b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/leaf-more-menu.js index c2fbc4ea480ef6..d714ecb43b15d9 100644 --- a/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/leaf-more-menu.js +++ b/packages/edit-site/src/components/sidebar-navigation-screen-navigation-menus/leaf-more-menu.js @@ -67,7 +67,7 @@ export default function LeafMoreMenu( props ) { postType: attributes.type, postId: attributes.id, ...( isPreviewingTheme() && { - gutenberg_theme_preview: currentlyPreviewingTheme(), + wp_theme_preview: currentlyPreviewingTheme(), } ), } ); } @@ -76,7 +76,7 @@ export default function LeafMoreMenu( props ) { postType: 'page', postId: attributes.id, ...( isPreviewingTheme() && { - gutenberg_theme_preview: currentlyPreviewingTheme(), + wp_theme_preview: currentlyPreviewingTheme(), } ), } ); } diff --git a/packages/edit-site/src/utils/is-previewing-theme.js b/packages/edit-site/src/utils/is-previewing-theme.js index 4ce57bfdbc508a..1a71c441f9925e 100644 --- a/packages/edit-site/src/utils/is-previewing-theme.js +++ b/packages/edit-site/src/utils/is-previewing-theme.js @@ -5,14 +5,13 @@ import { getQueryArg } from '@wordpress/url'; export function isPreviewingTheme() { return ( - getQueryArg( window.location.href, 'gutenberg_theme_preview' ) !== - undefined + getQueryArg( window.location.href, 'wp_theme_preview' ) !== undefined ); } export function currentlyPreviewingTheme() { if ( isPreviewingTheme() ) { - return getQueryArg( window.location.href, 'gutenberg_theme_preview' ); + return getQueryArg( window.location.href, 'wp_theme_preview' ); } return null; } diff --git a/packages/edit-site/src/utils/use-activate-theme.js b/packages/edit-site/src/utils/use-activate-theme.js index 966705a7722e48..64654318274cd6 100644 --- a/packages/edit-site/src/utils/use-activate-theme.js +++ b/packages/edit-site/src/utils/use-activate-theme.js @@ -31,7 +31,7 @@ export function useActivateTheme() { '&_wpnonce=' + window.BLOCK_THEME_ACTIVATE_NONCE; await window.fetch( activationURL ); - const { gutenberg_theme_preview: themePreview, ...params } = + const { wp_theme_preview: themePreview, ...params } = location.params; history.replace( params ); } From 460017f04ec9e238d29110fd618543fb857dc017 Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Wed, 21 Jun 2023 13:58:15 +0200 Subject: [PATCH 2/3] removed test code --- packages/api-fetch/src/middlewares/theme-preview.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/packages/api-fetch/src/middlewares/theme-preview.js b/packages/api-fetch/src/middlewares/theme-preview.js index b0d3540ce91193..8ab21fc3284763 100644 --- a/packages/api-fetch/src/middlewares/theme-preview.js +++ b/packages/api-fetch/src/middlewares/theme-preview.js @@ -19,21 +19,14 @@ const createThemePreviewMiddleware = ( themePath ) => ( options, next ) => { wp_theme_preview: themePath, } ); } - //window.__experimentalThemePreview if ( typeof options.path === 'string' && ! hasQueryArg( options.path, 'wp_theme_preview' ) ) { - if ( window.__experimentalThemePreview ) { - options.path = addQueryArgs( options.path, { - wp_theme_preview: themePath, - } ); - } else { - options.path = addQueryArgs( options.path, { - wp_theme_preview: themePath, - } ); - } + options.path = addQueryArgs( options.path, { + wp_theme_preview: themePath, + } ); } return next( options ); From 6388f6223d2e7c34cc30dd769e443fef392c25d8 Mon Sep 17 00:00:00 2001 From: MaggieCabrera Date: Wed, 21 Jun 2023 16:43:34 +0200 Subject: [PATCH 3/3] linter --- lib/compat/wordpress-6.3/theme-previews.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.3/theme-previews.php b/lib/compat/wordpress-6.3/theme-previews.php index 5ca79c2de84b40..eab05c5824b1ff 100644 --- a/lib/compat/wordpress-6.3/theme-previews.php +++ b/lib/compat/wordpress-6.3/theme-previews.php @@ -124,4 +124,4 @@ function block_theme_activate_nonce() { if ( ! function_exists( 'wp_get_theme_preview_path' ) ) { add_action( 'admin_head', 'block_theme_activate_nonce' ); add_action( 'admin_print_footer_scripts', 'add_live_preview_button', 11 ); -} \ No newline at end of file +}