From e218eca967c01b59029a8298829a14ba39769c2f Mon Sep 17 00:00:00 2001 From: Roni Laukkarinen Date: Tue, 24 Sep 2024 19:09:31 +0300 Subject: [PATCH] Rewrite: allowed_block_types - Change logic for allowed blocks: 'none', 'all', 'all-core-blocks', 'all-acf-blocks', Fixes #226 --- CHANGELOG.md | 3 +- functions.php | 18 ++++++++- inc/hooks/gutenberg.php | 85 +++++++++++++++++++++++------------------ 3 files changed, 67 insertions(+), 39 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 70deabf7..d2dce2a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/functions.php b/functions.php index 9516e873..67c3b74d 100644 --- a/functions.php +++ b/functions.php @@ -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 diff --git a/inc/hooks/gutenberg.php b/inc/hooks/gutenberg.php index 7d9c47f3..e38e728f 100644 --- a/inc/hooks/gutenberg.php +++ b/inc/hooks/gutenberg.php @@ -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()); @@ -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;