From b5785c0056484d461b065f461c9b8ab7651ce412 Mon Sep 17 00:00:00 2001 From: mricoul Date: Mon, 18 May 2026 14:28:02 +0200 Subject: [PATCH 1/2] fix(editor): Ensure reliable unregistration of block variations Introduces a mechanism to wait for target blocks to be fully registered in the editor store before attempting to unregister their variations. This prevents potential issues where variations might not be unregistered if blocks are not yet available when `domReady` fires. Additionally, this change refactors block style and variation unregistration into dedicated helper functions for improved code organization and readability. --- src/js/common/editor.js | 75 +++++++++++++++++++++++++++++++---------- 1 file changed, 58 insertions(+), 17 deletions(-) diff --git a/src/js/common/editor.js b/src/js/common/editor.js index 7b36b46..634a569 100644 --- a/src/js/common/editor.js +++ b/src/js/common/editor.js @@ -2,36 +2,77 @@ /* Customize BFFEditorSettings in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */ import domReady from '@wordpress/dom-ready'; +import { subscribe } from '@wordpress/data'; import { addFilter } from '@wordpress/hooks'; import { unregisterBlockStyle, getBlockVariations, + getBlockType, unregisterBlockVariation, } from '@wordpress/blocks'; import './utils/beapi'; +const unregisterDisabledBlockStyles = () => { + if (!BFFEditorSettings.disabledBlocksStyles) { + return; + } + + Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach( + ([blockName, styles]) => { + [].concat(styles).forEach((styleName) => { + unregisterBlockStyle(blockName, styleName); + }); + } + ); +}; + +const unregisterDisallowedBlockVariations = () => { + if (!BFFEditorSettings.allowedBlocksVariations) { + return; + } + + Object.entries(BFFEditorSettings.allowedBlocksVariations).forEach( + ([blockName, allowedVariationNames]) => { + const blockVariations = getBlockVariations(blockName) || []; + + blockVariations.forEach((variation) => { + if (!allowedVariationNames.includes(variation.name)) { + unregisterBlockVariation(blockName, variation.name); + } + }); + } + ); +}; + +const whenBlocksRegistered = (blockNames, callback) => { + const areBlocksReady = () => + blockNames.every((blockName) => getBlockType(blockName)); + + if (areBlocksReady()) { + callback(); + return; + } + + const unsubscribe = subscribe(() => { + if (!areBlocksReady()) { + return; + } + + unsubscribe(); + callback(); + }); +}; + // Native Gutenberg domReady(() => { - // Disable specific block styles - if (BFFEditorSettings.disabledBlocksStyles) { - Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach( - ([block, styles]) => { - unregisterBlockStyle(block, styles); - } - ); - } + unregisterDisabledBlockStyles(); - // Allow blocks variations if (BFFEditorSettings.allowedBlocksVariations) { - Object.entries(BFFEditorSettings.allowedBlocksVariations).forEach( - ([block, variations]) => { - getBlockVariations(block).forEach((variant) => { - if (!variations.includes(variant.name)) { - unregisterBlockVariation(block, variant.name); - } - }); - } + const blockNames = Object.keys( + BFFEditorSettings.allowedBlocksVariations ); + + whenBlocksRegistered(blockNames, unregisterDisallowedBlockVariations); } }); From 788473f7e4c56ba9e1cd4049dedb6b2a17bee4d9 Mon Sep 17 00:00:00 2001 From: mricoul Date: Mon, 18 May 2026 14:29:30 +0200 Subject: [PATCH 2/2] refactor(editor): rename JavaScript editor settings constant --- inc/Services/Editor.php | 2 +- src/js/common/editor.js | 20 ++++++++++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/inc/Services/Editor.php b/inc/Services/Editor.php index efcc6e1..b650df1 100644 --- a/inc/Services/Editor.php +++ b/inc/Services/Editor.php @@ -262,7 +262,7 @@ public function admin_editor_script(): void { $this->assets_tools->add_inline_script( 'theme-admin-editor-script', - 'const BFFEditorSettings = ' . wp_json_encode( + 'const BEAPI_EDITOR_SETTINGS = ' . wp_json_encode( apply_filters( 'bff_editor_custom_settings', [ diff --git a/src/js/common/editor.js b/src/js/common/editor.js index 634a569..1580d54 100644 --- a/src/js/common/editor.js +++ b/src/js/common/editor.js @@ -1,6 +1,6 @@ -/* global BFFEditorSettings */ +/* global BEAPI_EDITOR_SETTINGS */ -/* Customize BFFEditorSettings in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */ +/* Customize BEAPI_EDITOR_SETTINGS in inc/Services/Editor.php or with `bff_editor_custom_settings` filter (see readme). */ import domReady from '@wordpress/dom-ready'; import { subscribe } from '@wordpress/data'; import { addFilter } from '@wordpress/hooks'; @@ -13,11 +13,11 @@ import { import './utils/beapi'; const unregisterDisabledBlockStyles = () => { - if (!BFFEditorSettings.disabledBlocksStyles) { + if (!BEAPI_EDITOR_SETTINGS.disabledBlocksStyles) { return; } - Object.entries(BFFEditorSettings.disabledBlocksStyles).forEach( + Object.entries(BEAPI_EDITOR_SETTINGS.disabledBlocksStyles).forEach( ([blockName, styles]) => { [].concat(styles).forEach((styleName) => { unregisterBlockStyle(blockName, styleName); @@ -27,11 +27,11 @@ const unregisterDisabledBlockStyles = () => { }; const unregisterDisallowedBlockVariations = () => { - if (!BFFEditorSettings.allowedBlocksVariations) { + if (!BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) { return; } - Object.entries(BFFEditorSettings.allowedBlocksVariations).forEach( + Object.entries(BEAPI_EDITOR_SETTINGS.allowedBlocksVariations).forEach( ([blockName, allowedVariationNames]) => { const blockVariations = getBlockVariations(blockName) || []; @@ -67,9 +67,9 @@ const whenBlocksRegistered = (blockNames, callback) => { domReady(() => { unregisterDisabledBlockStyles(); - if (BFFEditorSettings.allowedBlocksVariations) { + if (BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) { const blockNames = Object.keys( - BFFEditorSettings.allowedBlocksVariations + BEAPI_EDITOR_SETTINGS.allowedBlocksVariations ); whenBlocksRegistered(blockNames, unregisterDisallowedBlockVariations); @@ -87,8 +87,8 @@ addFilter( function (settings, name) { // Disable all styles if ( - BFFEditorSettings.disableAllBlocksStyles && - BFFEditorSettings.disableAllBlocksStyles.includes(name) + BEAPI_EDITOR_SETTINGS.disableAllBlocksStyles && + BEAPI_EDITOR_SETTINGS.disableAllBlocksStyles.includes(name) ) { settings.styles = []; }