Skip to content

Commit 6765046

Browse files
committed
feat: add validation for allowed blocks in registered patterns
1 parent 063a74f commit 6765046

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/wp-includes/class-wp-block-patterns-registry.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,41 @@ public function register( $pattern_name, $pattern_properties ) {
114114
);
115115
return false;
116116
}
117+
118+
// Block Validation to check if blocks used in the pattern are allowed.
119+
$blocks = parse_blocks( $pattern_properties['content'] );
120+
$block_names = wp_list_pluck( $blocks, 'blockName' );
121+
$allowed_blocks = apply_filters( 'allowed_block_types_all', true );
122+
123+
if ( is_array( $allowed_blocks ) ) {
124+
$allowed_blocks = array_flip( $allowed_blocks );
125+
foreach ( $block_names as $block_name ) {
126+
if ( ! isset( $allowed_blocks[ $block_name ] ) ) {
127+
_doing_it_wrong(
128+
__METHOD__,
129+
sprintf(
130+
/* translators: %1$s: Pattern name, %2$s: Block name. */
131+
__( 'Pattern "%1$s" contains disallowed block "%2$s".' ),
132+
$pattern_name,
133+
$block_name
134+
),
135+
'6.9.0'
136+
);
137+
return false;
138+
}
139+
}
140+
} else if ( ! $allowed_blocks ) {
141+
_doing_it_wrong(
142+
__METHOD__,
143+
sprintf(
144+
/* translators: %s: Pattern name. */
145+
__( 'Pattern "%s" contains blocks, but all blocks are disallowed.' ),
146+
$pattern_name
147+
),
148+
'6.9.0'
149+
);
150+
return false;
151+
}
117152
}
118153

119154
$pattern = array_merge(

0 commit comments

Comments
 (0)