Skip to content

feat: add bs_is_editor() and bs_is_preview() - #140

Merged
dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/bs-is-editor
Oct 22, 2026
Merged

dnnsjsk merged 1 commit into
inline0:mainfrom
Contributolo:feat/bs-is-editor

Conversation

@Contributolo

Copy link
Copy Markdown
Collaborator

Templates get $isEditor and $isPreview from Block::render(). Code that runs during the same render but outside the template has no public way to know the render mode: a theme helper called from a template, a filter such as blockstudio/blocks/components/rich_text/render, a render_block filter. Themes read $_GET['blockstudioMode'] themselves, which is an internal detail of how the editor talks to the render endpoint.

Change

  • bs_is_editor(): bool and bs_is_preview(): bool in includes/functions/functions.php, with docblocks like the other bs_* functions. They return what the template receives as $isEditor and $isPreview.
  • The check moves into Block::is_render_mode( string $mode ) (public, @internal), and render() uses it too, so the functions and the template variables cannot drift apart.
  • Both read the same request value as before, so they follow the places that set or clear the mode for a moment: block tags, Render::block() and compositions clear it (frontend markup), the editor preload in Blocks sets editor. Inside a block rendered from a block tag during an editor request both functions return false, as the template variables do.
  • bs_is_preview() is included because $isPreview is a documented template variable (inserter preview, see Environment) and the editor sends preview as a mode of its own; leaving it out would keep $_GET reads for that case.
  • Added to the API collision list in placeholders.php and to the PHPStan stubs, with type assertions in the stub test data.
  • Docs: a section "Outside templates" on the Environment page, both functions in the PHPStan API stubs list, LLM context rebuilt.

Tests

tests/unit/block/BlockRenderModeTest.php registers a block whose template writes $isEditor, $isPreview, bs_is_editor() and bs_is_preview() into an attribute and checks:

  • no mode: all false,
  • blockstudioMode=editor: editor true, preview false, functions equal to the template variables,
  • blockstudioMode=preview: the reverse,
  • an unknown mode: both false,
  • a block tag rendered during an editor request: all false inside, and bs_is_editor() is true again afterwards.

Run in WordPress Playground (PHP 8.4, WordPress latest, plugin and test theme mounted as in tests/wp-env): the new test green (5 tests, 17 assertions). block (without GithubUpdaterTest, which needs separate processes), render, block-tags, islands, canvas and rest give the same results as unchanged main (the failures in block, block-tags and islands are the same tests on main, environment related). composer cs, PHPStan on the changed files, npm run check and the packages/phpstan tests clean.

@since 7.7.6 is a placeholder, please adjust.

Templates receive $isEditor and $isPreview from Block::render(), but code
that runs during the same render outside the template (theme helpers
called from a template, render filters such as
blockstudio/blocks/components/rich_text/render) had no public way to read
the render mode and had to inspect $_GET['blockstudioMode'] itself.

bs_is_editor() and bs_is_preview() return the values the template
receives. Both read Block::is_render_mode(), which render() now uses as
well, so the functions and the template variables cannot drift. Because
they read the same request value, they follow the places that set or
clear the mode temporarily, such as block tags (always frontend) and the
editor preload.

Adds the functions to the API collision list and the PHPStan stubs,
documents them under Environment, and adds unit tests that compare both
functions with the template variables in each mode and inside a block
tag rendered during an editor request.
@dnnsjsk

dnnsjsk commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Included in Blockstudio 7.7.6, now officially released. This PR is merged into main as part of the reviewed release, with full CI and distribution checks passing for the release commit.

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.

2 participants