Skip to content

Add vip_governance__deny_message filter for custom block-deny snackbar#137

Open
smithjw1 wants to merge 1 commit into
trunkfrom
smithjw1/deny-message-filter
Open

Add vip_governance__deny_message filter for custom block-deny snackbar#137
smithjw1 wants to merge 1 commit into
trunkfrom
smithjw1/deny-message-filter

Conversation

@smithjw1

@smithjw1 smithjw1 commented May 1, 2026

Copy link
Copy Markdown
Contributor

Description

Closes #136.

When a block is restricted by governance rules, Gutenberg's block inserter shows a generic "Block 'X' can't be inserted." snackbar that gives editors no context — non-technical users interpret it as a bug. This PR adds a JS filter, vip_governance__deny_message, that integrators can use to replace that copy with something meaningful (e.g. who to contact).

How it works

  • A new subscription on core/notices watches for Gutenberg's inserter-notice snackbar (dispatched in @wordpress/block-editor's use-block-types-state.js when an inserter selection is denied).
  • When seen, the plugin parses the block title from the localised message (regex built from __() so it follows the active locale; supports both %s and numbered %1$s placeholders), resolves the matching blockName via the blocks store, runs everything through the new vip_governance__deny_message filter, removes WP's snackbar, and dispatches our own.
  • If no integrator filter is registered, a sensible default is shown: "The 'Audio' block is restricted by your site's governance rules."
  • The filter signature mirrors the existing vip_governance__is_block_allowed_for_insertion filter — (message, blockName, blockTitle, governanceRules).

Docs

  • New vip_governance__deny_message section added to README.md (Code Filters) with signature and an integrator example.
  • Added a row to the filters table in AGENTS.md.

Open question for the team

Should we also add a denyMessage field to the governance rules JSON schema (per-rule, alongside allowedBlocks/blockSettings), so non-technical admins can configure the copy without writing JS? The filter shipped here lets integrators override programmatically; a schema field would complement it for the no-code case mentioned in the issue. Happy to do as a small follow-up PR if there's appetite.

Steps to Test

  1. Check out PR.
  2. npm install && npm run build.
  3. wp-env start and log in at http://localhost:8889 (admin/password).
  4. Open a new post. In the browser console, dispatch the same payload Gutenberg dispatches when a block is denied:
    wp.data.dispatch('core/notices').createErrorNotice(
      `Block "Audio" can't be inserted.`,
      { type: 'snackbar', id: 'inserter-notice' }
    );
  5. Verify the snackbar reads "The 'Audio' block is restricted by your site's governance rules." (and Gutenberg's original inserter-notice is gone — wp.data.select('core/notices').getNotices() should not contain it).
  6. Register a filter and re-dispatch:
    wp.hooks.addFilter(
      'vip_governance__deny_message',
      'example/custom',
      (msg, blockName, blockTitle) => `Reach out to #content if you need "${ blockTitle }".`
    );
    wp.data.dispatch('core/notices').removeNotice('wpcomvip-governance-deny');
    wp.data.dispatch('core/notices').createErrorNotice(
      `Block "Audio" can't be inserted.`,
      { type: 'snackbar', id: 'inserter-notice' }
    );
    Verify the snackbar reflects the override.
  7. npm run test:js — 33 tests pass.
  8. wp-env start && npx playwright install chromium && npx playwright test --grep "deny-snackbar|vip_governance__deny_message" — both new E2E tests pass.

🤖 Generated with Claude Code

…ackbar

Replaces Gutenberg's generic "Block 'X' can't be inserted." snackbar with one
that integrators can override via the new vip_governance__deny_message JS
filter, giving editors meaningful context (e.g. who to contact) instead of a
message that reads like a bug. Closes #136.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@smithjw1 smithjw1 self-assigned this May 1, 2026
@smithjw1 smithjw1 marked this pull request as ready for review May 1, 2026 17:17
@smithjw1 smithjw1 requested a review from a team as a code owner May 1, 2026 17:17
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.

Allow customising the deny message shown when a disallowed block is inserted

1 participant