Skip to content

Commit 57c1cb3

Browse files
committed
feat(editor): blacklist specific Gutenberg blocks
Switches the block management strategy from a whitelist to a blacklist. Previously, only explicitly allowed blocks were available. This change enables all blocks by default, but specifically disallows a defined set of core blocks. This approach provides greater flexibility and simplifies block management by not requiring explicit whitelisting for the majority of desired blocks.
1 parent fe590c1 commit 57c1cb3

1 file changed

Lines changed: 27 additions & 32 deletions

File tree

‎inc/Services/Editor.php‎

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function boot( Service_Container $container ): void {
6464
*/
6565
add_action( 'enqueue_block_assets', [ $this, 'admin_editor_script' ] );
6666
/**
67-
* White list of gutenberg blocks
67+
* Black list of Gutenberg blocks
6868
*/
6969
add_filter( 'allowed_block_types_all', [ $this, 'gutenberg_blocks_allowed' ], 10, 2 );
7070
}
@@ -207,44 +207,39 @@ private function register_custom_block_styles() {
207207
}
208208

209209
/**
210-
* Allow some core Gutenberg blocks
210+
* Disallow some Gutenberg blocks (blacklist).
211211
*
212-
* @param bool|array $allowed_blocks
213-
* @param \WP_Block_Editor_Context $block_editor_context
212+
* @param bool|array $allowed_blocks The allowed blocks.
213+
* @param \WP_Block_Editor_Context $block_editor_context The block editor context.
214214
*
215-
* @return array
215+
* @return array The allowed blocks.
216216
*/
217217
public function gutenberg_blocks_allowed( $allowed_blocks, \WP_Block_Editor_Context $block_editor_context ): array {
218+
// If boolean, get explicit list of allowed blocks.
219+
if ( is_bool( $allowed_blocks ) ) {
220+
$allowed_blocks = $allowed_blocks ? array_keys( \WP_Block_Type_Registry::get_instance()->get_all_registered() ) : [];
221+
}
218222

219-
$allowed = [
220-
//base
221-
'core/block',
222-
'core/heading',
223-
'core/paragraph',
224-
'core/image',
225-
'core/list',
226-
'core/list-item',
227-
'core/quote',
228-
'core/pullquote',
229-
'core/table',
230-
'core/buttons',
231-
'core/button',
232-
'core/group',
233-
'core/columns',
234-
'core/column',
235-
'core/media-text',
236-
'core/spacer',
237-
'core/separator',
238-
'core/cover',
239-
'core/gallery',
240-
'core/video',
241-
'core/file',
223+
// List of disallowed blocks.
224+
$disallowed_blocks = [
225+
'core/html',
226+
'core/freeform',
227+
'core/code',
228+
'core/preformatted',
229+
'core/verse',
230+
'core/footnotes',
231+
'core/more',
232+
'core/loginout',
242233
'core/embed',
243-
// custom
244-
'beapi/manual-block',
245-
'beapi/dynamic-block',
246234
];
247235

248-
return ( is_array( $allowed_blocks ) ) ? array_merge( $allowed, $allowed_blocks ) : $allowed;
236+
// Remove disallowed blocks from allowed blocks.
237+
foreach ( $disallowed_blocks as $block ) {
238+
if ( in_array( $block, $allowed_blocks, true ) ) {
239+
unset( $allowed_blocks[ array_search( $block, $allowed_blocks, true ) ] );
240+
}
241+
}
242+
243+
return array_values( $allowed_blocks );
249244
}
250245
}

0 commit comments

Comments
 (0)