From cbffa223f62900e2f4266e28ee3bb899fd14daf8 Mon Sep 17 00:00:00 2001 From: Kai Hao Date: Tue, 14 May 2024 14:08:18 +0900 Subject: [PATCH] Fix unsynced patterns can enable overrides --- .../editor/src/hooks/pattern-overrides.js | 18 +++++--- .../editor/various/pattern-overrides.spec.js | 41 +++++++++++++++++++ 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/packages/editor/src/hooks/pattern-overrides.js b/packages/editor/src/hooks/pattern-overrides.js index 7b5b6ced083400..cd5f07aa8f48d1 100644 --- a/packages/editor/src/hooks/pattern-overrides.js +++ b/packages/editor/src/hooks/pattern-overrides.js @@ -19,6 +19,7 @@ const { ResetOverridesControl, PATTERN_TYPES, PARTIAL_SYNCING_SUPPORTED_BLOCKS, + PATTERN_SYNC_TYPES, } = unlock( patternsPrivateApis ); /** @@ -51,18 +52,23 @@ const withPatternOverrideControls = createHigherOrderComponent( // on every block. function ControlsWithStoreSubscription( props ) { const blockEditingMode = useBlockEditingMode(); - const { hasPatternOverridesSource, isEditingPattern } = useSelect( + const { hasPatternOverridesSource, isEditingSyncedPattern } = useSelect( ( select ) => { const { getBlockBindingsSource } = unlock( select( blocksStore ) ); + const { getCurrentPostType, getEditedPostAttribute } = + select( editorStore ); return { // For editing link to the site editor if the theme and user permissions support it. hasPatternOverridesSource: !! getBlockBindingsSource( 'core/pattern-overrides' ), - isEditingPattern: - select( editorStore ).getCurrentPostType() === - PATTERN_TYPES.user, + isEditingSyncedPattern: + getCurrentPostType() === PATTERN_TYPES.user && + getEditedPostAttribute( 'meta' )?.wp_pattern_sync_status !== + PATTERN_SYNC_TYPES.unsynced && + getEditedPostAttribute( 'wp_pattern_sync_status' ) !== + PATTERN_SYNC_TYPES.unsynced, }; }, [] @@ -76,9 +82,9 @@ function ControlsWithStoreSubscription( props ) { ); const shouldShowPatternOverridesControls = - isEditingPattern && blockEditingMode === 'default'; + isEditingSyncedPattern && blockEditingMode === 'default'; const shouldShowResetOverridesControl = - ! isEditingPattern && + ! isEditingSyncedPattern && !! props.attributes.metadata?.name && blockEditingMode !== 'disabled' && hasPatternBindings; diff --git a/test/e2e/specs/editor/various/pattern-overrides.spec.js b/test/e2e/specs/editor/various/pattern-overrides.spec.js index f252c670d7b075..8aa0c9c530609d 100644 --- a/test/e2e/specs/editor/various/pattern-overrides.spec.js +++ b/test/e2e/specs/editor/various/pattern-overrides.spec.js @@ -797,4 +797,45 @@ test.describe( 'Pattern Overrides', () => { await expect( secondParagraph ).toHaveText( 'overriden content' ); } ); } ); + + // https://github.com/WordPress/gutenberg/issues/61610. + test( 'unsynced patterns should not be able to enable overrides', async ( { + page, + admin, + requestUtils, + editor, + } ) => { + const pattern = await requestUtils.createBlock( { + title: 'Pattern', + content: ` +

paragraph

+`, + status: 'publish', + meta: { + wp_pattern_sync_status: 'unsynced', + }, + } ); + + await admin.visitSiteEditor( { + postId: pattern.id, + postType: 'wp_block', + canvas: 'edit', + } ); + + const paragraph = editor.canvas.getByRole( 'document', { + name: 'Block: Paragraph', + } ); + await editor.selectBlocks( paragraph ); + await editor.openDocumentSettingsSidebar(); + + const editorSettings = page.getByRole( 'region', { + name: 'Editor settings', + } ); + await editorSettings + .getByRole( 'button', { name: 'Advanced' } ) + .click(); + await expect( + editorSettings.getByRole( 'button', { name: 'Enable overrides' } ) + ).toBeHidden(); + } ); } );