Skip to content

Commit

Permalink
Implement pattern overrides behind IS_GUTENBERG_PLUGIN flag (#59702)
Browse files Browse the repository at this point in the history
Co-authored-by: talldan <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: getdave <[email protected]>
  • Loading branch information
4 people committed Mar 11, 2024
1 parent b78d306 commit dee8699
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 17 deletions.
6 changes: 5 additions & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,13 @@ function gutenberg_is_experiment_enabled( $name ) {
}
require __DIR__ . '/compat/wordpress-6.5/block-bindings/block-bindings.php';
require __DIR__ . '/compat/wordpress-6.5/block-bindings/post-meta.php';
require __DIR__ . '/compat/wordpress-6.5/block-bindings/pattern-overrides.php';
require __DIR__ . '/compat/wordpress-6.5/script-loader.php';

// Not to be included in WordPress 6.5.
if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
require __DIR__ . '/compat/wordpress-6.6/block-bindings/pattern-overrides.php';
}

// Experimental features.
require __DIR__ . '/experimental/block-editor-settings-mobile.php';
require __DIR__ . '/experimental/blocks.php';
Expand Down
53 changes: 38 additions & 15 deletions packages/block-library/src/block/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
BlockControls,
} from '@wordpress/block-editor';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import { parse, cloneBlock } from '@wordpress/blocks';
import { parse, cloneBlock, store as blocksStore } from '@wordpress/blocks';
import { RichTextData } from '@wordpress/rich-text';

/**
Expand Down Expand Up @@ -265,6 +265,7 @@ export default function ReusableBlockEdit( {
getBlockEditingMode,
onNavigateToEntityRecord,
editingMode,
hasPatternOverridesSource,
} = useSelect(
( select ) => {
const { canUser } = select( coreStore );
Expand All @@ -273,6 +274,7 @@ export default function ReusableBlockEdit( {
getSettings,
getBlockEditingMode: _getBlockEditingMode,
} = select( blockEditorStore );
const { getBlockBindingsSource } = unlock( select( blocksStore ) );
const blocks = getBlocks( patternClientId );
const canEdit = canUser( 'update', 'blocks', ref );

Expand All @@ -284,6 +286,9 @@ export default function ReusableBlockEdit( {
onNavigateToEntityRecord:
getSettings().onNavigateToEntityRecord,
editingMode: _getBlockEditingMode( patternClientId ),
hasPatternOverridesSource: !! getBlockBindingsSource(
'core/pattern-overrides'
),
};
},
[ patternClientId, ref ]
Expand All @@ -295,13 +300,20 @@ export default function ReusableBlockEdit( {
setBlockEditingMode,
innerBlocks,
// Disable editing if the pattern itself is disabled.
editingMode === 'disabled' ? 'disabled' : undefined
editingMode === 'disabled' || ! hasPatternOverridesSource
? 'disabled'
: undefined
);
}, [ editingMode, innerBlocks, setBlockEditingMode ] );
}, [
editingMode,
innerBlocks,
setBlockEditingMode,
hasPatternOverridesSource,
] );

const canOverrideBlocks = useMemo(
() => hasOverridableBlocks( innerBlocks ),
[ innerBlocks ]
() => hasPatternOverridesSource && hasOverridableBlocks( innerBlocks ),
[ hasPatternOverridesSource, innerBlocks ]
);

const initialBlocks = useMemo(
Expand Down Expand Up @@ -329,19 +341,21 @@ export default function ReusableBlockEdit( {
registry.batch( () => {
setBlockEditingMode( patternClientId, 'default' );
syncDerivedUpdates( () => {
replaceInnerBlocks(
patternClientId,
applyInitialContentValuesToInnerBlocks(
initialBlocks,
initialContent.current,
defaultContent.current,
legacyIdMap.current
)
);
const blocks = hasPatternOverridesSource
? applyInitialContentValuesToInnerBlocks(
initialBlocks,
initialContent.current,
defaultContent.current,
legacyIdMap.current
)
: initialBlocks;

replaceInnerBlocks( patternClientId, blocks );
} );
setBlockEditingMode( patternClientId, originalEditingMode );
} );
}, [
hasPatternOverridesSource,
__unstableMarkNextChangeAsNotPersistent,
patternClientId,
initialBlocks,
Expand Down Expand Up @@ -377,6 +391,9 @@ export default function ReusableBlockEdit( {
// Sync the `content` attribute from the updated blocks to the pattern block.
// `syncDerivedUpdates` is used here to avoid creating an additional undo level.
useEffect( () => {
if ( ! hasPatternOverridesSource ) {
return;
}
const { getBlocks } = registry.select( blockEditorStore );
let prevBlocks = getBlocks( patternClientId );
return registry.subscribe( () => {
Expand All @@ -394,7 +411,13 @@ export default function ReusableBlockEdit( {
} );
}
}, blockEditorStore );
}, [ syncDerivedUpdates, patternClientId, registry, setAttributes ] );
}, [
hasPatternOverridesSource,
syncDerivedUpdates,
patternClientId,
registry,
setAttributes,
] );

const handleEditOriginal = () => {
onNavigateToEntityRecord( {
Expand Down
5 changes: 4 additions & 1 deletion packages/editor/src/bindings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@ import patternOverrides from './pattern-overrides';
import postMeta from './post-meta';

const { registerBlockBindingsSource } = unlock( dispatch( blocksStore ) );
registerBlockBindingsSource( patternOverrides );
registerBlockBindingsSource( postMeta );

if ( process.env.IS_GUTENBERG_PLUGIN ) {
registerBlockBindingsSource( patternOverrides );
}
14 changes: 14 additions & 0 deletions packages/patterns/src/components/use-set-pattern-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@
* WordPress dependencies
*/
import { usePrevious } from '@wordpress/compose';
import { store as blocksStore } from '@wordpress/blocks';
import { useEffect } from '@wordpress/element';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import { PARTIAL_SYNCING_SUPPORTED_BLOCKS } from '../constants';

import { unlock } from '../lock-unlock';

function removeBindings( bindings, syncedAttributes ) {
let updatedBindings = {};
for ( const attributeName of syncedAttributes ) {
Expand Down Expand Up @@ -42,14 +46,23 @@ export default function useSetPatternBindings(
{ name, attributes, setAttributes },
currentPostType
) {
const hasPatternOverridesSource = useSelect( ( select ) => {
const { getBlockBindingsSource } = unlock( select( blocksStore ) );

// For editing link to the site editor if the theme and user permissions support it.
return !! getBlockBindingsSource( 'core/pattern-overrides' );
}, [] );

const metadataName = attributes?.metadata?.name ?? '';
const prevMetadataName = usePrevious( metadataName ) ?? '';
const bindings = attributes?.metadata?.bindings;

useEffect( () => {
// Bindings should only be created when editing a wp_block post type,
// and also when there's a change to the user-given name for the block.
// Also check that the pattern overrides source is registered.
if (
! hasPatternOverridesSource ||
currentPostType !== 'wp_block' ||
metadataName === prevMetadataName
) {
Expand Down Expand Up @@ -95,6 +108,7 @@ export default function useSetPatternBindings(
} );
}
}, [
hasPatternOverridesSource,
bindings,
prevMetadataName,
metadataName,
Expand Down

0 comments on commit dee8699

Please sign in to comment.