Skip to content

Commit

Permalink
Fix unsynced patterns can enable overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin940726 authored and talldan committed May 15, 2024
1 parent e19858b commit cbffa22
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
18 changes: 12 additions & 6 deletions packages/editor/src/hooks/pattern-overrides.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const {
ResetOverridesControl,
PATTERN_TYPES,
PARTIAL_SYNCING_SUPPORTED_BLOCKS,
PATTERN_SYNC_TYPES,
} = unlock( patternsPrivateApis );

/**
Expand Down Expand Up @@ -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,
};
},
[]
Expand All @@ -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;
Expand Down
41 changes: 41 additions & 0 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<!-- wp:paragraph -->
<p>paragraph</p>
<!-- /wp: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();
} );
} );

0 comments on commit cbffa22

Please sign in to comment.