feat: add restrictBlocksForPostTypes to Gutenberg.php#55
Conversation
There was a problem hiding this comment.
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_allfilter handler to derive an allowed block list fromgutenberg.postTypeBlockRestrictions. - Support composing the final allowlist from a configured block set plus optional
add/removeoverrides. - Attempt to respect earlier filters by intersecting when
$allowedBlockTypesis already an array.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| return [] !== $intersected ? $intersected : $allowedBlockTypes; | ||
| } | ||
|
|
There was a problem hiding this comment.
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).
| // 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. |
| $blockSet = $restriction['blockSet'] ?? null; | ||
|
|
||
| if (! is_string($blockSet) || '' === trim($blockSet)) { | ||
| return $allowedBlockTypes; | ||
| } | ||
|
|
||
| $baseBlocks = config("gutenberg.blockSets.{$restriction['blockSet']}", []); |
There was a problem hiding this comment.
$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.
| $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}", []); |
| $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) | ||
| )); |
There was a problem hiding this comment.
$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.
| } | ||
|
|
||
| /** | ||
| * Restrict blocks based for post types through the gutenberg.php config file. |
There was a problem hiding this comment.
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").
| * Restrict blocks based for post types through the gutenberg.php config file. | |
| * Restrict blocks for post types through the gutenberg.php config file. |
mvdhoek1
left a comment
There was a problem hiding this comment.
AI doet het reviewen al voor ons haha, ik approve hem vast onder voorbehoud van de comments.
| $blockSet = $restriction['blockSet'] ?? null; | ||
|
|
||
| if (! is_string($blockSet) || '' === trim($blockSet)) { | ||
| return $allowedBlockTypes; | ||
| } | ||
|
|
||
| $baseBlocks = config("gutenberg.blockSets.{$restriction['blockSet']}", []); |
Voor context, zie deze PR: yardinternet/brave#79
Je kunt nu via de
gutenberg.phpconfig file blocks restricten op basis van post type.