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

Fix blocks in unsynced patterns can enable overrides #61639

Merged
merged 1 commit into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
} );
} );
Loading