diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 782ee9030c19e..59164d5b8e0e5 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -114,6 +114,41 @@ public function register( $pattern_name, $pattern_properties ) { ); return false; } + + // Block Validation to check if blocks used in the pattern are allowed. + $blocks = parse_blocks( $pattern_properties['content'] ); + $block_names = wp_list_pluck( $blocks, 'blockName' ); + $allowed_blocks = apply_filters( 'allowed_block_types_all', true ); + + if ( is_array( $allowed_blocks ) ) { + $allowed_blocks = array_flip( $allowed_blocks ); + foreach ( $block_names as $block_name ) { + if ( ! isset( $allowed_blocks[ $block_name ] ) ) { + _doing_it_wrong( + __METHOD__, + sprintf( + /* translators: %1$s: Pattern name, %2$s: Block name. */ + __( 'Pattern "%1$s" contains disallowed block "%2$s".' ), + $pattern_name, + $block_name + ), + '6.9.0' + ); + return false; + } + } + } elseif ( ! $allowed_blocks ) { + _doing_it_wrong( + __METHOD__, + sprintf( + /* translators: %s: Pattern name. */ + __( 'Pattern "%s" contains blocks, but all blocks are disallowed.' ), + $pattern_name + ), + '6.9.0' + ); + return false; + } } $pattern = array_merge(