Skip to content

Commit

Permalink
Change logic for allowed blocks: None, selected, blocks, 'all', 'all-…
Browse files Browse the repository at this point in the history
…core-blocks' or 'acf'
  • Loading branch information
ronilaukkarinen committed Sep 18, 2024
1 parent 57fdbdf commit b05b398
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 71 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
42 changes: 3 additions & 39 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 43 additions & 10 deletions inc/hooks/gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
23 changes: 1 addition & 22 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
<exclude name="Universal.WhiteSpace.DisallowInlineTabs.NonIndentTabsUsed" />
<exclude name="WordPress.Arrays.ArrayIndentation.CloseBraceNotAligned" />
<exclude name="PEAR.Functions.FunctionCallSignature.OpeningIndent" />

<!-- Don't require too strict inline commenting, it's a good thing to documentate, let's not make it frustrating -->
<exclude name="Squiz.Commenting.FunctionComment.WrongStyle" />
<exclude name="Squiz.Commenting.ClassComment.WrongStyle" />
<exclude name="Squiz.Commenting.InlineComment.InvalidEndChar" />
Expand All @@ -46,8 +44,6 @@
<exclude name="Squiz.Commenting.FileComment.Missing" />
<exclude name="Squiz.Commenting.LongConditionClosingComment.Missing" />
<exclude name="Squiz.Commenting.FunctionComment.ParamCommentFullStop" />

<!-- General code style related excludes -->
<exclude name="Squiz.PHP.DisallowMultipleAssignments.Found" />
<exclude name="Squiz.PHP.EmbeddedPhp.MultipleStatements" />
<exclude name="Squiz.PHP.EmbeddedPhp.ContentAfterOpen" />
Expand All @@ -57,11 +53,7 @@
<exclude name="WordPress.Arrays.ArrayDeclarationSpacing.AssociativeArrayFound" />
<exclude name="Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace" />
<exclude name="PHPCompatibility.PHP.NewFunctionArrayDereferencing.Found" />

<!-- General WordPress stuff we like to overrule -->
<exclude name="WordPress.Files.FileName.InvalidClassFileName" />

<!-- Very strict VIP/discouraged rules that are not needed -->
<exclude name="WordPress.XSS.EscapeOutput.OutputNotEscaped" />
<exclude name="WordPress.Functions.DontExtract.extract_extract" />
<exclude name="Generic.Functions.FunctionCallArgumentSpacing.TooMuchSpaceAfterComma" />
Expand All @@ -73,17 +65,9 @@
<exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine" />
<exclude name="PEAR.Functions.FunctionCallSignature.MultipleArguments" />
<exclude name="WordPress.PHP.DontExtract.extract_extract" />

<!-- Translations related, not always necessary, too strict -->
<exclude name="WordPress.WP.I18n.MissingTranslatorsComment" />

<!-- EditorConfig makes sure this happens anyway & false positives happen with this -->
<exclude name="Generic.Files.EndFileNewline.NotFound" />

<!-- Many times when developing a customer case we have placeholders for SVG includes, so unnecessary error reports happen with this rule -->
<exclude name="Squiz.PHP.EmbeddedPhp.Empty" />

<!-- Other useful excludes -->
<exclude name="WordPress.Arrays.MultipleStatementAlignment.DoubleArrowNotAligned" />
<exclude name="WordPress.WP.GlobalVariablesOverride.Prohibited" />
<exclude name="PEAR.NamingConventions.ValidClassName.StartWithCapital" />
Expand All @@ -101,14 +85,9 @@
<exclude name="WordPress.PHP.DisallowShortTernary.Found" />
<exclude name="Generic.Arrays.DisallowShortArraySyntax.Found" />
<exclude name="Universal.Arrays.DisallowShortArraySyntax.Found" />

<!-- Whitespace rules -->
<exclude name="WordPress.WhiteSpace.PrecisionAlignment.Found" />
<exclude name="WordPress.WhiteSpace.DisallowInlineTabs.NonIndentTabsUsed" />
<exclude name="Universal.WhiteSpace.PrecisionAlignment.Found" />

<!-- Project based -->
<exclude name="Squiz.PHP.CommentedOutCode.Found" />
<exclude name="Universal.WhiteSpace.PrecisionAlignment.Found" />
</rule>

<!-- Verify that the text_domain is set to the desired text-domain.
Expand Down

0 comments on commit b05b398

Please sign in to comment.