Skip to content

feat: add restrictBlocksForPostTypes to Gutenberg.php#55

Merged
YvetteNikolov merged 3 commits into
mainfrom
feat/add-gutenberg-config-block-restrictions
Mar 30, 2026
Merged

feat: add restrictBlocksForPostTypes to Gutenberg.php#55
YvetteNikolov merged 3 commits into
mainfrom
feat/add-gutenberg-config-block-restrictions

Conversation

@YvetteNikolov
Copy link
Copy Markdown
Contributor

Voor context, zie deze PR: yardinternet/brave#79

Je kunt nu via de gutenberg.php config file blocks restricten op basis van post type.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a Gutenberg hook to restrict allowed blocks per WordPress post type using values from the gutenberg.php configuration, extending the existing Gutenberg-related config-driven behavior in this package.

Changes:

  • Add an allowed_block_types_all filter handler to derive an allowed block list from gutenberg.postTypeBlockRestrictions.
  • Support composing the final allowlist from a configured block set plus optional add/remove overrides.
  • Attempt to respect earlier filters by intersecting when $allowedBlockTypes is already an array.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Gutenberg.php

return [] !== $intersected ? $intersected : $allowedBlockTypes;
}

Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When $allowedBlockTypes is true (WordPress default: allow all blocks), this method computes $finalAllowedBlocks but ultimately returns the original boolean, so the post type restriction is never applied. Consider returning $finalAllowedBlocks when $allowedBlockTypes === true, and preserving false when $allowedBlockTypes === false (and still intersecting when it is an array).

Suggested change
// When WordPress passes `true` (allow all by default), apply our computed restriction.
if ($allowedBlockTypes === true) {
return $finalAllowedBlocks;
}
// Preserve explicit `false` (no blocks allowed) from earlier filters.

Copilot uses AI. Check for mistakes.
Comment thread src/Gutenberg.php Outdated
Comment on lines +58 to +64
$blockSet = $restriction['blockSet'] ?? null;

if (! is_string($blockSet) || '' === trim($blockSet)) {
return $allowedBlockTypes;
}

$baseBlocks = config("gutenberg.blockSets.{$restriction['blockSet']}", []);
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$blockSet is validated (including trim()), but the config lookup uses $restriction['blockSet'] instead. This can cause unexpected behavior (e.g., whitespace in the config value passes validation but looks up the wrong key) and duplicates logic. Use the sanitized $blockSet variable consistently for the gutenberg.blockSets.* lookup.

Suggested change
$blockSet = $restriction['blockSet'] ?? null;
if (! is_string($blockSet) || '' === trim($blockSet)) {
return $allowedBlockTypes;
}
$baseBlocks = config("gutenberg.blockSets.{$restriction['blockSet']}", []);
$blockSet = isset($restriction['blockSet']) ? trim((string) $restriction['blockSet']) : null;
if (! is_string($blockSet) || '' === $blockSet) {
return $allowedBlockTypes;
}
$baseBlocks = config("gutenberg.blockSets.{$blockSet}", []);

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/Gutenberg.php Outdated
Comment on lines +64 to +70
$baseBlocks = config("gutenberg.blockSets.{$restriction['blockSet']}", []);
$add = isset($restriction['add']) && is_array($restriction['add']) ? $restriction['add'] : [];
$remove = isset($restriction['remove']) && is_array($restriction['remove']) ? $restriction['remove'] : [];

$finalAllowedBlocks = array_values(array_unique(
array_diff([...$baseBlocks, ...$add], $remove)
));
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$baseBlocks is assumed to be an array, but if the gutenberg.blockSets.* config value is misconfigured (non-array), the array unpack (...$baseBlocks) will throw a TypeError. Add an is_array($baseBlocks) guard (or cast to array) before unpacking.

Copilot uses AI. Check for mistakes.
Comment thread src/Gutenberg.php Outdated
}

/**
* Restrict blocks based for post types through the gutenberg.php config file.
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docblock grammar: "Restrict blocks based for post types" reads incorrectly. Consider changing to "Restrict blocks based on post types" (or "Restrict blocks for post types").

Suggested change
* Restrict blocks based for post types through the gutenberg.php config file.
* Restrict blocks for post types through the gutenberg.php config file.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown

@mvdhoek1 mvdhoek1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI doet het reviewen al voor ons haha, ik approve hem vast onder voorbehoud van de comments.

Comment thread src/Gutenberg.php Outdated
Comment on lines +58 to +64
$blockSet = $restriction['blockSet'] ?? null;

if (! is_string($blockSet) || '' === trim($blockSet)) {
return $allowedBlockTypes;
}

$baseBlocks = config("gutenberg.blockSets.{$restriction['blockSet']}", []);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YvetteNikolov YvetteNikolov merged commit fc33905 into main Mar 30, 2026
@YvetteNikolov YvetteNikolov deleted the feat/add-gutenberg-config-block-restrictions branch March 30, 2026 07:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants