Repository navigation
feat: add bs_is_editor() and bs_is_preview() - #140
Merged
Merged
Conversation
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.
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Templates get
$isEditorand$isPreviewfromBlock::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 asblockstudio/blocks/components/rich_text/render, arender_blockfilter. Themes read$_GET['blockstudioMode']themselves, which is an internal detail of how the editor talks to the render endpoint.Change
bs_is_editor(): boolandbs_is_preview(): boolinincludes/functions/functions.php, with docblocks like the otherbs_*functions. They return what the template receives as$isEditorand$isPreview.Block::is_render_mode( string $mode )(public,@internal), andrender()uses it too, so the functions and the template variables cannot drift apart.Render::block()and compositions clear it (frontend markup), the editor preload inBlockssetseditor. Inside a block rendered from a block tag during an editor request both functions returnfalse, as the template variables do.bs_is_preview()is included because$isPreviewis a documented template variable (inserter preview, see Environment) and the editor sendspreviewas a mode of its own; leaving it out would keep$_GETreads for that case.placeholders.phpand to the PHPStan stubs, with type assertions in the stub test data.Tests
tests/unit/block/BlockRenderModeTest.phpregisters a block whose template writes$isEditor,$isPreview,bs_is_editor()andbs_is_preview()into an attribute and checks:blockstudioMode=editor: editor true, preview false, functions equal to the template variables,blockstudioMode=preview: the reverse,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(withoutGithubUpdaterTest, which needs separate processes),render,block-tags,islands,canvasandrestgive the same results as unchangedmain(the failures inblock,block-tagsandislandsare the same tests onmain, environment related).composer cs, PHPStan on the changed files,npm run checkand thepackages/phpstantests clean.@since 7.7.6is a placeholder, please adjust.