Skip to content

Commit

Permalink
Block Theme Previews: rename GET variable and prepare for core compat (
Browse files Browse the repository at this point in the history
…#51738)

* renamed get variable and loaded actions and filters conditionally

Co-authored-by: Andrei Draganescu <[email protected]>

* removed test code

* linter

---------

Co-authored-by: Andrei Draganescu <[email protected]>
  • Loading branch information
MaggieCabrera and draganescu authored Jun 21, 2023
1 parent 9fab0d7 commit 40d1c7d
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 19 deletions.
15 changes: 8 additions & 7 deletions lib/compat/wordpress-6.3/theme-previews.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) {
Expand Down Expand Up @@ -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'
);
Expand Down Expand Up @@ -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 = '<?php echo esc_html_e( 'Live Preview' ); ?>';
themeInfo.querySelector('.theme-actions').appendChild(livePreviewButton);
Expand All @@ -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 );
}
10 changes: 5 additions & 5 deletions packages/api-fetch/src/middlewares/theme-preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, any>} themePath
Expand All @@ -13,19 +13,19 @@ 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,
} );
}

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,
wp_theme_preview: themePath,
} );
}

Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/routes/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function useLink( params = {}, state, shouldReplace = false ) {
if ( isPreviewingTheme() ) {
params = {
...params,
gutenberg_theme_preview: currentlyPreviewingTheme(),
wp_theme_preview: currentlyPreviewingTheme(),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default function LeafMoreMenu( props ) {
postType: attributes.type,
postId: attributes.id,
...( isPreviewingTheme() && {
gutenberg_theme_preview: currentlyPreviewingTheme(),
wp_theme_preview: currentlyPreviewingTheme(),
} ),
} );
}
Expand All @@ -76,7 +76,7 @@ export default function LeafMoreMenu( props ) {
postType: 'page',
postId: attributes.id,
...( isPreviewingTheme() && {
gutenberg_theme_preview: currentlyPreviewingTheme(),
wp_theme_preview: currentlyPreviewingTheme(),
} ),
} );
}
Expand Down
5 changes: 2 additions & 3 deletions packages/edit-site/src/utils/is-previewing-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion packages/edit-site/src/utils/use-activate-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down

0 comments on commit 40d1c7d

Please sign in to comment.