Skip to content

Commit e4f7134

Browse files
authored
Merge pull request #506 from BeAPI/feature/blacklist-blocks
feat(editor): blacklist specific Gutenberg blocks
2 parents 84f9bad + 03f512b commit e4f7134

1 file changed

Lines changed: 100 additions & 32 deletions

File tree

‎inc/Services/Editor.php‎

Lines changed: 100 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,112 @@ 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+
// Code & raw content.
226+
'core/html',
227+
'core/freeform',
228+
'core/code',
229+
'core/preformatted',
230+
'core/verse',
231+
'core/footnotes',
232+
'core/shortcode',
233+
'core/math',
234+
// Layout & media (unused).
235+
'core/text-columns',
236+
'core/nextpage',
237+
'core/read-more',
238+
// Embeds & widgets.
242239
'core/embed',
243-
// custom
244-
'beapi/manual-block',
245-
'beapi/dynamic-block',
240+
'core/legacy-widget',
241+
'core/widget-group',
242+
// Accordion.
243+
'core/accordion',
244+
'core/accordion-heading',
245+
'core/accordion-item',
246+
'core/accordion-panel',
247+
'core/icon',
248+
// Theme & site (unused).
249+
'core/archives',
250+
'core/calendar',
251+
'core/categories',
252+
'core/latest-comments',
253+
'core/latest-posts',
254+
'core/rss',
255+
'core/search',
256+
'core/tag-cloud',
257+
'core/site-tagline',
258+
'core/site-title',
259+
'core/breadcrumbs',
260+
'core/navigation-overlay-close',
261+
// Post meta & author (unused).
262+
'core/post-author',
263+
'core/post-author-biography',
264+
'core/post-author-name',
265+
'core/post-navigation-link',
266+
'core/post-terms',
267+
'core/post-time-to-read',
268+
'core/avatar',
269+
// Comments (unused).
270+
'core/comments',
271+
'core/comments-pagination',
272+
'core/comments-pagination-next',
273+
'core/comments-pagination-numbers',
274+
'core/comments-pagination-previous',
275+
'core/comments-title',
276+
'core/comment-author-name',
277+
'core/comment-content',
278+
'core/comment-date',
279+
'core/comment-edit-link',
280+
'core/comment-reply-link',
281+
'core/comment-template',
282+
'core/post-comments',
283+
'core/post-comments-count',
284+
'core/post-comments-form',
285+
'core/post-comments-link',
286+
// Query loop (unused).
287+
'core/query',
288+
'core/query-no-results',
289+
'core/query-pagination',
290+
'core/query-pagination-next',
291+
'core/query-pagination-numbers',
292+
'core/query-pagination-previous',
293+
'core/query-title',
294+
'core/query-total',
295+
'core/post-template',
296+
// Terms (unused).
297+
'core/term-count',
298+
'core/term-description',
299+
// Lists & navigation (unused).
300+
'core/page-list',
301+
'core/page-list-item',
302+
// Reusable & misc.
303+
'core/loginout',
304+
'core/more',
305+
'core/social-link',
306+
'core/social-links',
246307
];
247308

248-
return ( is_array( $allowed_blocks ) ) ? array_merge( $allowed, $allowed_blocks ) : $allowed;
309+
// Remove disallowed blocks from allowed blocks.
310+
foreach ( $disallowed_blocks as $block ) {
311+
if ( in_array( $block, $allowed_blocks, true ) ) {
312+
unset( $allowed_blocks[ array_search( $block, $allowed_blocks, true ) ] );
313+
}
314+
}
315+
316+
return array_values( $allowed_blocks );
249317
}
250318
}

0 commit comments

Comments
 (0)