Skip to content
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
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(
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder why you chose to use the selector over process.env.IS_GUTENBERG_PLUGIN

Copy link
Contributor Author

@talldan talldan Mar 9, 2024

Choose a reason for hiding this comment

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

Yeah, I was in two minds about it.

My thinking is that this could be maintained to work like this even after the process.env.IS_GUTENBERG_PLUGIN code is removed, as a potential unregisterBindingSource( 'core/pattern-overrides' ) call could result in the same thing.

Actually, I don't think this approach is a good idea, as a WP 6.5 user could re-register pattern overrides, so I think we'll need to add process.env.IS_GUTENBERG_PLUGIN everywhere.

edit: The register action is private at the moment, so maybe this solution is ok. I don't mind implementing it either way though.

Copy link
Contributor

Choose a reason for hiding this comment

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

The register action is private at the moment, so maybe this solution is ok.

Given that it's still possible, shall we just put behind the flag?

Copy link
Contributor

Choose a reason for hiding this comment

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

@getdave It's not possible for third-parties, so it doesn't impact.

Personally, I don't mind either way.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh I thought a third party could opt into Private APIs it's just they have to jump through a lot of hoops to do so.

Personally, I don't mind either way.

Good enough for me. @talldan can make a call.

'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