Skip to content

Commit

Permalink
Rewrite: allowed_block_types - Change logic for allowed blocks: 'none…
Browse files Browse the repository at this point in the history
…', 'all', 'all-core-blocks', 'all-acf-blocks', Fixes #226
  • Loading branch information
ronilaukkarinen committed Sep 24, 2024
1 parent b05b398 commit e218eca
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 39 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 9.5.0rc: 2024-09-18

* Change logic for allowed blocks: None, selected, blocks, 'all', 'all-core-blocks' or 'acf'
* Rewrite: allowed_block_types - Change logic for allowed blocks: 'none', 'all', 'all-core-blocks', 'all-acf-blocks', Fixes #226 (thanks @villekujansuu)
* Allow options + specific blocks for allowed blocks
* Prepare for air-blocks-buildtool

### 9.4.4: 2024-09-13
Expand Down
18 changes: 17 additions & 1 deletion functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,25 @@
],

// Restrict to only selected blocks
//
// Options: 'none', 'all', 'all-core-blocks', 'all-acf-blocks',
// or any specific block or a combination of these
// Accepts both string (all*/none-options only) and array (options + specific blocks)
'allowed_blocks' => [
'post' => 'all-core-blocks', // Add array of blocks, 'all', 'all-core-blocks' or 'acf'
'post' => 'all-core-blocks',
'page' => [],
// 'page' => [
// 'all-acf-blocks',
// 'core/paragraph',
// ],
// 'post-type' => [
// 'acf/content-image',
// 'core/paragraph',
// ],
// 'example' => [
// 'all-core-blocks',
// 'acf/content-image',
// ],
],

// If you want to use classic editor somewhere, define it here
Expand Down
85 changes: 48 additions & 37 deletions inc/hooks/gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,53 @@
/**
* Restrict blocks to only allowed blocks in the settings
*/
function allowed_block_types( $allowed_blocks, $editor_context ) {
if ( ! isset( THEME_SETTINGS['allowed_blocks'] ) || 'all' === THEME_SETTINGS['allowed_blocks'] ) {
function allowed_block_types( $allowed_blocks, $editor_context ) { // phpcs:ignore
// If no allowed blocks are defined or it is set to none, return an empty array
if ( empty( THEME_SETTINGS['allowed_blocks'] ) || 'none' === THEME_SETTINGS['allowed_blocks'] ) {
return [];
}

// If the post type contains empty array or none, return an empty array for that post type post
if ( empty( THEME_SETTINGS['allowed_blocks'][ get_post_type() ] ) || 'none' === THEME_SETTINGS['allowed_blocks'][ get_post_type() ] ) {
return [];
}

// If post type block has been set to 'all', return all blocks, or if the array below it contains 'all', return all blocks
if ( 'all' === THEME_SETTINGS['allowed_blocks'][ get_post_type() ] || is_array( THEME_SETTINGS['allowed_blocks'][ get_post_type() ] ) && in_array( 'all', THEME_SETTINGS['allowed_blocks'][ get_post_type() ], true ) ) {

// If single blocks are defined in post type array, add them to allowed blocks on top of the existing blocks
if ( is_array( THEME_SETTINGS['allowed_blocks'][ get_post_type() ] ) ) {

$allowed_blocks = array_merge( $allowed_blocks, THEME_SETTINGS['allowed_blocks'][ get_post_type() ] );
}

return $allowed_blocks;
}

// Allow all core block types setting
if ( 'all-core-blocks' === THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ] ) {
// If post type block has been set to 'all-acf-blocks', return all ACF blocks, or if the array below it contains 'all-acf-blocks', return all ACF blocks
if ( 'all-acf-blocks' === THEME_SETTINGS['allowed_blocks'][ get_post_type() ] || is_array( THEME_SETTINGS['allowed_blocks'][ get_post_type() ] ) && in_array( 'all-acf-blocks', THEME_SETTINGS['allowed_blocks'][ get_post_type() ], true ) ) {

// Add ACF blocks
if ( isset( THEME_SETTINGS['acf_blocks'] ) ) {
$allowed_blocks = [];

foreach ( THEME_SETTINGS['acf_blocks'] as $custom_block ) {
$allowed_blocks[] = 'acf/' . $custom_block['name'];

}

// If single blocks are defined in post type array, add them to allowed blocks on top of the existing blocks
if ( is_array( THEME_SETTINGS['allowed_blocks'][ get_post_type() ] ) ) {

$allowed_blocks = array_merge( $allowed_blocks, THEME_SETTINGS['allowed_blocks'][ get_post_type() ] );
}

return $allowed_blocks;
}
}

// If post type block has been set to 'all-core-blocks', return all core blocks, or if the array below it contains 'all-core-blocks', return all core blocks
if ( 'all-core-blocks' === THEME_SETTINGS['allowed_blocks'][ get_post_type() ] || is_array( THEME_SETTINGS['allowed_blocks'][ get_post_type() ] ) && in_array( 'all-core-blocks', THEME_SETTINGS['allowed_blocks'][ get_post_type() ], true ) ) {
$allowed_blocks = array_map(function( $block ) {
return $block->name;
}, \WP_Block_Type_Registry::get_instance()->get_all_registered());
Expand All @@ -28,43 +68,14 @@ function allowed_block_types( $allowed_blocks, $editor_context ) {

// Get array values
$allowed_blocks = array_values( $allowed_blocks );
return $allowed_blocks;
}

// Allow all block types setting
if ( 'all' === THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ] ) {
$allowed_blocks = array_map(function( $block ) {
return $block->name;
}, \WP_Block_Type_Registry::get_instance()->get_all_registered());

return $allowed_blocks;
}

// If there is post type specific blocks, add them to the allowed blocks list
if ( isset( $editor_context->post->post_type ) && isset( THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ] ) ) {
// If setting is an array, use it as is
if ( is_array( THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ] ) ) {
return THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ];
}

// If acf-blocks, restrict to only ACF blocks
if ( 'acf' === THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ] ) {

// Add custom blocks
if ( isset( THEME_SETTINGS['acf_blocks'] ) ) {
$allowed_blocks = [];

foreach ( THEME_SETTINGS['acf_blocks'] as $custom_block ) {
$allowed_blocks[] = 'acf/' . $custom_block['name'];
}
// If single blocks are defined in post type array, add them to allowed blocks on top of the existing blocks
if ( is_array( THEME_SETTINGS['allowed_blocks'][ get_post_type() ] ) ) {

return $allowed_blocks;
}
$allowed_blocks = array_merge( $allowed_blocks, THEME_SETTINGS['allowed_blocks'][ get_post_type() ] );
}

if ( 'all' === THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ] ) {
return $allowed_blocks;
}
return $allowed_blocks;
}

return $allowed_blocks;
Expand Down

0 comments on commit e218eca

Please sign in to comment.