Skip to content

Commit

Permalink
Block Bindings: Refactor utils file. (WordPress#64740)
Browse files Browse the repository at this point in the history
* Refactor block binding utils

* Add security checks

Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>
Co-authored-by: gziolo <gziolo@git.wordpress.org>
Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org>
Co-authored-by: jsnajdr <jsnajdr@git.wordpress.org>
  • Loading branch information
5 people committed Aug 27, 2024
1 parent fb15df1 commit 57c9c69
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions packages/block-editor/src/utils/block-bindings.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
/**
* WordPress dependencies
*/
import { useDispatch, useSelect } from '@wordpress/data';
import { useDispatch, useRegistry } from '@wordpress/data';

/**
* Internal dependencies
*/
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.
Expand Down Expand Up @@ -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 ];
Expand All @@ -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,
} );
};

Expand All @@ -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,
} );
};

Expand Down

0 comments on commit 57c9c69

Please sign in to comment.