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
2 changes: 1 addition & 1 deletion inc/Services/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
[
Expand Down
85 changes: 63 additions & 22 deletions src/js/common/editor.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,78 @@
/* 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';
import {
unregisterBlockStyle,
getBlockVariations,
getBlockType,
unregisterBlockVariation,
} from '@wordpress/blocks';
import './utils/beapi';

const unregisterDisabledBlockStyles = () => {
if (!BEAPI_EDITOR_SETTINGS.disabledBlocksStyles) {
return;
}

Object.entries(BEAPI_EDITOR_SETTINGS.disabledBlocksStyles).forEach(
([blockName, styles]) => {
[].concat(styles).forEach((styleName) => {
unregisterBlockStyle(blockName, styleName);
});
}
);
};

const unregisterDisallowedBlockVariations = () => {
if (!BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) {
return;
}

Object.entries(BEAPI_EDITOR_SETTINGS.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();
});
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

All-or-nothing block check prevents partial unregistration and leaks subscription

Medium Severity

whenBlocksRegistered requires ALL blocks in allowedBlocksVariations to be registered before unregistering variations for ANY of them. Since settings are filterable via bff_editor_custom_settings, if any configured block is never registered (e.g., from a deactivated plugin or unsupported editor context), the subscribe listener persists for the entire session — firing areBlocksReady() on every single data store state change — and no variations are ever unregistered for any block. The previous code handled each block independently.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 788473f. Configure here.


// 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);
}
});
}
if (BEAPI_EDITOR_SETTINGS.allowedBlocksVariations) {
const blockNames = Object.keys(
BEAPI_EDITOR_SETTINGS.allowedBlocksVariations
);

whenBlocksRegistered(blockNames, unregisterDisallowedBlockVariations);
}
});

Expand All @@ -46,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 = [];
}
Expand Down