From b05b3980e85a454b1f0297c4d90c6d78ce652145 Mon Sep 17 00:00:00 2001 From: Roni Laukkarinen Date: Wed, 18 Sep 2024 19:37:38 +0300 Subject: [PATCH] Change logic for allowed blocks: None, selected, blocks, 'all', 'all-core-blocks' or 'acf' --- CHANGELOG.md | 5 ++++ functions.php | 42 +++----------------------------- inc/hooks/gutenberg.php | 53 +++++++++++++++++++++++++++++++++-------- phpcs.xml | 23 +----------------- 4 files changed, 52 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 04f9463b..70deabf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### 9.5.0rc: 2024-09-18 + +* Change logic for allowed blocks: None, selected, blocks, 'all', 'all-core-blocks' or 'acf' +* Prepare for air-blocks-buildtool + ### 9.4.4: 2024-09-13 * Add unit tests for gulp devstyles diff --git a/functions.php b/functions.php index f4f796fc..9516e873 100644 --- a/functions.php +++ b/functions.php @@ -135,45 +135,9 @@ ], // Restrict to only selected blocks - // Set the value to 'all' to allow all blocks everywhere - 'allowed_blocks' => [ - 'default' => [], - 'post' => [ - 'core/archives', - 'core/audio', - 'core/buttons', - 'core/categories', - 'core/code', - 'core/column', - 'core/columns', - 'core/coverImage', - 'core/embed', - 'core/file', - 'core/freeform', - 'core/gallery', - 'core/heading', - 'core/html', - 'core/image', - 'core/latestComments', - 'core/latestPosts', - 'core/list', - 'core/list-item', - 'core/more', - 'core/nextpage', - 'core/paragraph', - 'core/preformatted', - 'core/pullquote', - 'core/quote', - 'core/block', - 'core/separator', - 'core/shortcode', - 'core/spacer', - 'core/subhead', - 'core/table', - 'core/textColumns', - 'core/verse', - 'core/video', - ], + 'allowed_blocks' => [ + 'post' => 'all-core-blocks', // Add array of blocks, 'all', 'all-core-blocks' or 'acf' + 'page' => [], ], // If you want to use classic editor somewhere, define it here diff --git a/inc/hooks/gutenberg.php b/inc/hooks/gutenberg.php index b07c78b8..7d9c47f3 100644 --- a/inc/hooks/gutenberg.php +++ b/inc/hooks/gutenberg.php @@ -15,22 +15,55 @@ function allowed_block_types( $allowed_blocks, $editor_context ) { return $allowed_blocks; } - // Add the default allowed blocks - $allowed_blocks = isset( THEME_SETTINGS['allowed_blocks']['default'] ) ? THEME_SETTINGS['allowed_blocks']['default'] : []; + // Allow all core block types setting + if ( 'all-core-blocks' === 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()); + + // Remove all but core/* blocks from array + $allowed_blocks = array_filter( $allowed_blocks, function( $block ) { + return strpos( $block, 'core/' ) === 0; + }); + + // 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 ( 'all' === THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ] ) { - return $allowed_blocks; + // 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 ]; } - $allowed_blocks = array_merge( $allowed_blocks, 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 = []; - // Add custom blocks - if ( isset( THEME_SETTINGS['acf_blocks'] ) ) { - foreach ( THEME_SETTINGS['acf_blocks'] as $custom_block ) { - $allowed_blocks[] = 'acf/' . $custom_block['name']; + foreach ( THEME_SETTINGS['acf_blocks'] as $custom_block ) { + $allowed_blocks[] = 'acf/' . $custom_block['name']; + } + + return $allowed_blocks; + } + } + + if ( 'all' === THEME_SETTINGS['allowed_blocks'][ $editor_context->post->post_type ] ) { + return $allowed_blocks; } } diff --git a/phpcs.xml b/phpcs.xml index ba5f5c36..c94bb423 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -35,8 +35,6 @@ - - @@ -46,8 +44,6 @@ - - @@ -57,11 +53,7 @@ - - - - @@ -73,17 +65,9 @@ - - - - - - - - @@ -101,14 +85,9 @@ - - - - - - +