From 57c9c69afbd2e295719b7f201b821da7fe5729fb Mon Sep 17 00:00:00 2001 From: Carlos Bravo <37012961+cbravobernal@users.noreply.github.com> Date: Tue, 27 Aug 2024 11:29:14 +0200 Subject: [PATCH] Block Bindings: Refactor utils file. (#64740) * Refactor block binding utils * Add security checks Co-authored-by: cbravobernal Co-authored-by: gziolo Co-authored-by: SantosGuillamot Co-authored-by: jsnajdr --- .../block-editor/src/utils/block-bindings.js | 31 +++++++++---------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/packages/block-editor/src/utils/block-bindings.js b/packages/block-editor/src/utils/block-bindings.js index 4ba6e1a362958..b3daf4f4b36b4 100644 --- a/packages/block-editor/src/utils/block-bindings.js +++ b/packages/block-editor/src/utils/block-bindings.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { useDispatch, useSelect } from '@wordpress/data'; +import { useDispatch, useRegistry } from '@wordpress/data'; /** * Internal dependencies @@ -9,10 +9,14 @@ import { useDispatch, useSelect } from '@wordpress/data'; import { store as blockEditorStore } from '../store'; import { useBlockEditContext } from '../components/block-edit'; +function isObjectEmpty( object ) { + return ! object || Object.keys( object ).length === 0; +} + export function useBlockBindingsUtils() { const { clientId } = useBlockEditContext(); const { updateBlockAttributes } = useDispatch( blockEditorStore ); - const { getBlockAttributes } = useSelect( blockEditorStore ); + const { getBlockAttributes } = useRegistry().select( blockEditorStore ); /** * Updates the value of the bindings connected to block attributes. @@ -44,8 +48,10 @@ export function useBlockBindingsUtils() { * ``` */ const updateBlockBindings = ( bindings ) => { - const { metadata } = getBlockAttributes( clientId ); - const newBindings = { ...metadata?.bindings }; + const { metadata: { bindings: currentBindings, ...metadata } = {} } = + getBlockAttributes( clientId ); + const newBindings = { ...currentBindings }; + Object.entries( bindings ).forEach( ( [ attribute, binding ] ) => { if ( ! binding && newBindings[ attribute ] ) { delete newBindings[ attribute ]; @@ -59,15 +65,12 @@ export function useBlockBindingsUtils() { bindings: newBindings, }; - if ( Object.keys( newMetadata.bindings ).length === 0 ) { + if ( isObjectEmpty( newMetadata.bindings ) ) { delete newMetadata.bindings; } updateBlockAttributes( clientId, { - metadata: - Object.keys( newMetadata ).length === 0 - ? undefined - : newMetadata, + metadata: isObjectEmpty( newMetadata ) ? undefined : newMetadata, } ); }; @@ -83,14 +86,10 @@ export function useBlockBindingsUtils() { * ``` */ const removeAllBlockBindings = () => { - const { metadata } = getBlockAttributes( clientId ); - const newMetadata = { ...metadata }; - delete newMetadata.bindings; + const { metadata: { bindings, ...metadata } = {} } = + getBlockAttributes( clientId ); updateBlockAttributes( clientId, { - metadata: - Object.keys( newMetadata ).length === 0 - ? undefined - : newMetadata, + metadata: isObjectEmpty( metadata ) ? undefined : metadata, } ); };