Skip to content

Commit 2dcb85e

Browse files
committed
Add Allowed_Blocks library feature
1 parent f5e7b14 commit 2dcb85e

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Nothing yet.
1515
- `Ordered` feature.
1616
- `Each_Replaced` serialized blocks class.
1717
- `Alley\WP\Features\Library` namespace to hold a library of concrete feature implementations.
18+
- `Allowed_Blocks` library feature.
1819
- `Block_Content_Filter` library feature.
1920
- `Plugin_Loader` library feature.
2021

docs/feature.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ All `Features` implementations also implement `Feature`.
2525

2626
The `Library` subnamespace includes concrete implementations of common features. These can be used on their own or as part of a set of features that make up a larger integration.
2727

28+
- [Allowed_Blocks](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-allowed-blocks.php): Limit blocks allowed in the editor to those that are explicitly supported.
2829
- [Block_Content_Filter](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-block-content-filter.php): Filter block markup in `the_content` for the post being viewed.
2930
- [GTM_Script](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-gtm-script.php): Add the standard Google Tag Manager script and data layer.
3031
- [Plugin_Loader](https://github.com/alleyinteractive/wp-type-extensions/blob/main/src/alley/wp/features/class-plugin-loader.php): Makes the [Alley plugin loader](https://github.com/alleyinteractive/wp-plugin-loader) available in a feature.

docs/features.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,44 @@ use Alley\WP\Features\Template_Feature;
2727
// Main plugin features.
2828
$plugin = new Group();
2929

30+
$plugin->include(
31+
new Features\Allowed_Blocks(
32+
context: 'core/edit-post',
33+
allowed: new FastFailValidatorChain(
34+
[
35+
new AnyValidator(
36+
[
37+
// Allow all blocks from WP Curate.
38+
new Regex( '/^wp-curate/' ),
39+
// Allow these specific other blocks.
40+
new OneOf(
41+
[
42+
'haystack' => [
43+
'core/cover',
44+
'core/embed',
45+
'core/gallery',
46+
'core/group',
47+
'core/heading',
48+
'core/image',
49+
'core/list',
50+
'core/list-item',
51+
'core/paragraph',
52+
'core/quote',
53+
],
54+
],
55+
),
56+
],
57+
),
58+
new Not(
59+
new Regex( '#^core/post-#' ),
60+
'Post template blocks are not allowed in the post editor',
61+
),
62+
],
63+
),
64+
registry: WP_Block_Type_Registry::get_instance(),
65+
),
66+
)
67+
3068
// Load the Simple History plugin.
3169
$plugin->include(
3270
new Library\Plugin_Loader(
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Allowed_Blocks class file
4+
*
5+
* @package wp-type-extensions
6+
*/
7+
8+
namespace Alley\WP\Features\Library;
9+
10+
use Alley\WP\Types\Feature;
11+
use Laminas\Validator\ValidatorInterface;
12+
use WP_Block_Editor_Context;
13+
use WP_Block_Type;
14+
use WP_Block_Type_Registry;
15+
16+
/**
17+
* Limit blocks allowed in the editor to those that are explicitly supported.
18+
*/
19+
final class Allowed_Blocks implements Feature {
20+
/**
21+
* Set up.
22+
*
23+
* @param string|string[] $context Block editor context.
24+
* @param ValidatorInterface $allowed Allowed block names.
25+
* @param WP_Block_Type_Registry $registry Block type registry.
26+
*/
27+
public function __construct(
28+
private readonly string|array $context,
29+
private readonly ValidatorInterface $allowed,
30+
private readonly WP_Block_Type_Registry $registry,
31+
) {}
32+
33+
/**
34+
* Boot the feature.
35+
*/
36+
public function boot(): void {
37+
add_filter( 'allowed_block_types_all', [ $this, 'filter_allowed_block_types' ], 10, 2 );
38+
}
39+
40+
/**
41+
* Filters the allowed block types for all editor types.
42+
*
43+
* @param bool|string[] $allowed_block_types Array of block type slugs, or boolean to enable/disable all.
44+
* @param WP_Block_Editor_Context $block_editor_context The current block editor context.
45+
* @return bool|string[] Updated block type slugs.
46+
*/
47+
public function filter_allowed_block_types( $allowed_block_types, $block_editor_context ) {
48+
if ( in_array( $block_editor_context->name, (array) $this->context, true ) ) {
49+
$updated = array_reduce(
50+
$this->registry->get_all_registered(),
51+
function ( array $carry, WP_Block_Type $type ) {
52+
if ( $this->allowed->isValid( $type->name ) ) {
53+
$carry[] = $type->name;
54+
}
55+
56+
return $carry;
57+
},
58+
[],
59+
);
60+
61+
// If the filter already had an array of names, honor those, but test them.
62+
if ( is_array( $allowed_block_types ) && count( $allowed_block_types ) > 0 ) {
63+
array_push( $updated, ...array_filter( $allowed_block_types, [ $this->allowed, 'isValid' ] ) );
64+
}
65+
66+
$allowed_block_types = $updated;
67+
}
68+
69+
return $allowed_block_types;
70+
}
71+
}

0 commit comments

Comments
 (0)