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

Extract BlockThemePreviews-related code from the editor package #50863

25 changes: 11 additions & 14 deletions lib/compat/wordpress-6.3/theme-previews.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,15 @@ function block_theme_activate_nonce() {
<?php
}

// Hide this feature behind an experiment.
$gutenberg_experiments = get_option( 'gutenberg-experiments' );
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-theme-previews', $gutenberg_experiments ) ) {
/**
* Attaches filters to enable theme previews in the Site Editor.
*/
if ( ! empty( $_GET['theme_preview'] ) ) {
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 );
/**
* Attaches filters to enable theme previews in the Site Editor.
*/
if ( ! empty( $_GET['theme_preview'] ) ) {
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 );

3 changes: 0 additions & 3 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ function gutenberg_enable_experiments() {
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-details-blocks', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableDetailsBlocks = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-theme-previews', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableThemePreviews = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-pattern-enhancements', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnablePatternEnhancements = true', 'before' );
}
Expand Down
12 changes: 0 additions & 12 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,6 @@ function gutenberg_initialize_experiments_settings() {
)
);

add_settings_field(
'gutenberg-theme-previews',
__( 'Block Theme Previews', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Enable Block Theme Previews', 'gutenberg' ),
'id' => 'gutenberg-theme-previews',
)
);

Copy link
Contributor

Choose a reason for hiding this comment

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

I would be better to keep this PR surface area small and not remove the experiment in this PR.

Why? Because if we decide we need to roll back to making this an experiment we'll also have to revert all the other code changes. If there have been other changes to those files since then, the unfortunate developer will have to handle that which isn't something we should impose upon them.

Let's make the removal of the experiment a dedicated follow up PR.

Copy link
Contributor Author

@okmttdhr okmttdhr May 25, 2023

Choose a reason for hiding this comment

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

I'm going to create another PR for 1f8961a 👍 (reverted it for this PR)
c.f. #50863 (comment)

add_settings_field(
'gutenberg-interactivity-api-core-blocks',
__( 'Core blocks', 'gutenberg' ),
Expand Down
77 changes: 62 additions & 15 deletions packages/edit-site/src/components/save-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,75 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { Button, Modal } from '@wordpress/components';
import { EntitiesSavedStates } from '@wordpress/editor';
import {
EntitiesSavedStates,
useEntitiesSavedStatesIsDirty,
privateApis,
} from '@wordpress/editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { NavigableRegion } from '@wordpress/interface';
import { store as coreStore } from '@wordpress/core-data';

/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../store';
import { unlock } from '../../private-apis';
import { useActivateTheme } from '../../utils/use-activate-theme';
import {
currentlyPreviewingTheme,
isPreviewingTheme,
} from '../../utils/is-previewing-theme';

const { EntitiesSavedStatesExtensible } = unlock( privateApis );

const EntitiesSavedStatesForPreview = ( { onClose } ) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is nice. I would have just used an if statement for all this, but I really like the way you encapsulated this logic in a component.

const isDirtyProps = useEntitiesSavedStatesIsDirty();
let activateSaveLabel;
if ( isDirtyProps.isDirty ) {
activateSaveLabel = __( 'Activate & Save' );
} else {
activateSaveLabel = __( 'Activate' );
}

const { getTheme } = useSelect( coreStore );
const theme = getTheme( currentlyPreviewingTheme() );
const additionalPrompt = (
<p>
{ sprintf(
'Saving your changes will change your active theme to %1$s.',
theme?.name?.rendered
) }
</p>
);

const activateTheme = useActivateTheme();
const onSave = async ( values ) => {
await activateTheme();
return values;
};

return (
<EntitiesSavedStatesExtensible
{ ...{
...isDirtyProps,
additionalPrompt,
close: onClose,
onSave,
saveEnabled: true,
saveLabel: activateSaveLabel,
} }
/>
);
};

const _EntitiesSavedStates = ( { onClose } ) => {
if ( isPreviewingTheme() ) {
return <EntitiesSavedStatesForPreview onClose={ onClose } />;
}
return <EntitiesSavedStates close={ onClose } />;
};

export default function SavePanel() {
const { isSaveViewOpen, canvasMode } = useSelect( ( select ) => {
Expand All @@ -33,18 +91,7 @@ export default function SavePanel() {
};
}, [] );
const { setIsSaveViewOpened } = useDispatch( editSiteStore );
const activateTheme = useActivateTheme();
const onClose = () => setIsSaveViewOpened( false );
const onSave = async ( values ) => {
await activateTheme();
return values;
};

const entitySavedStates = window?.__experimentalEnableThemePreviews ? (
<EntitiesSavedStates close={ onClose } onSave={ onSave } />
) : (
<EntitiesSavedStates close={ onClose } />
);

if ( canvasMode === 'view' ) {
return isSaveViewOpen ? (
Expand All @@ -56,7 +103,7 @@ export default function SavePanel() {
'Save site, content, and template changes'
) }
>
{ entitySavedStates }
<_EntitiesSavedStates onClose={ onClose } />
</Modal>
) : null;
}
Expand All @@ -69,7 +116,7 @@ export default function SavePanel() {
ariaLabel={ __( 'Save panel' ) }
>
{ isSaveViewOpen ? (
entitySavedStates
<_EntitiesSavedStates onClose={ onClose } />
) : (
<div className="edit-site-editor__toggle-save-panel">
<Button
Expand Down
5 changes: 1 addition & 4 deletions packages/edit-site/src/utils/is-previewing-theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import { getQueryArg } from '@wordpress/url';

export function isPreviewingTheme() {
return (
window?.__experimentalEnableThemePreviews &&
getQueryArg( window.location.href, 'theme_preview' ) !== undefined
);
return getQueryArg( window.location.href, 'theme_preview' ) !== undefined;
}

export function currentlyPreviewingTheme() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { useState } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

const TRANSLATED_SITE_PROPERTIES = {
title: __( 'Title' ),
description: __( 'Tagline' ),
site_logo: __( 'Logo' ),
site_icon: __( 'Icon' ),
show_on_front: __( 'Show on front' ),
page_on_front: __( 'Page on front' ),
};

export const useIsDirty = () => {
const { dirtyEntityRecords } = useSelect( ( select ) => {
const dirtyRecords =
select( coreStore ).__experimentalGetDirtyEntityRecords();

// Remove site object and decouple into its edited pieces.
const dirtyRecordsWithoutSite = dirtyRecords.filter(
( record ) => ! ( record.kind === 'root' && record.name === 'site' )
);

const siteEdits = select( coreStore ).getEntityRecordEdits(
'root',
'site'
);

const siteEditsAsEntities = [];
for ( const property in siteEdits ) {
siteEditsAsEntities.push( {
kind: 'root',
name: 'site',
title: TRANSLATED_SITE_PROPERTIES[ property ] || property,
property,
} );
}
const dirtyRecordsWithSiteItems = [
...dirtyRecordsWithoutSite,
...siteEditsAsEntities,
];

return {
dirtyEntityRecords: dirtyRecordsWithSiteItems,
};
}, [] );

// Unchecked entities to be ignored by save function.
const [ unselectedEntities, _setUnselectedEntities ] = useState( [] );

const setUnselectedEntities = (
{ kind, name, key, property },
checked
) => {
if ( checked ) {
_setUnselectedEntities(
unselectedEntities.filter(
( elt ) =>
elt.kind !== kind ||
elt.name !== name ||
elt.key !== key ||
elt.property !== property
)
);
} else {
_setUnselectedEntities( [
...unselectedEntities,
{ kind, name, key, property },
] );
}
};

const isDirty = dirtyEntityRecords.length - unselectedEntities.length > 0;

return {
dirtyEntityRecords,
isDirty,
setUnselectedEntities,
unselectedEntities,
};
};
Loading