Skip to content

fix(blocks): reset local meta after preview render - #526

Open
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:fix/455-preview-meta-leak
Open

fix(blocks): reset local meta after preview render#526
faisalahammad wants to merge 1 commit into
WordPress:trunkfrom
faisalahammad:fix/455-preview-meta-leak

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Jul 28, 2026

Copy link
Copy Markdown

The preview paths of acf_rendered_block() and acf_rendered_block_v3() call acf_setup_meta() for post-render validation without a matching acf_reset_meta(). While local meta is active for a post id, ACF_Local_Meta::pre_load_metadata() (includes/local-meta.php:194-203) returns null for any meta name not present in the local set, so unrelated get_field() calls for the same post id silently return null after a block preview render.

This affects three sites in includes/blocks.php:

  • acf_rendered_block(), the $form branch (preview edit form)
  • acf_rendered_block(), the preloaded preview re-setup after the inner render call
  • acf_rendered_block_v3(), the unconditional setup used by v3 block previews

Fix wraps each acf_setup_meta() in a try/finally block with acf_reset_meta( $block['id'] ), matching the existing pair inside the inner acf_render_block() helper. The regression test test_rendered_block_does_not_leak_meta inserts a post with a known DB value, exercises all three preview paths, and asserts get_post_meta() still returns the DB value. The PHPUnit workaround that tracked leaked block ids and reset them manually in tear_down is removed from tests/php/includes/blocks/test-blocks-render.php and tests/php/includes/blocks/test-blocks-auto-inline-editing.php since the fix prevents the leak.

Closes #455

Use of AI Tools

This pull request was prepared with assistance from Claude Code. The change was implemented, tested, and reviewed by hand before submission; the AI was used to find the relevant code paths and run the verification steps outlined in the issue.

The preview paths of acf_rendered_block() and acf_rendered_block_v3()
call acf_setup_meta() for post-render validation without a matching
acf_reset_meta(). While local meta is active for a post id,
ACF_Local_Meta::pre_load_metadata() returns null for any meta name not
in the local set, so unrelated get_field() calls for the same post id
silently return null after a block preview render.

- Wrap the three preview acf_setup_meta() call sites in try/finally
  with acf_reset_meta(), matching the existing pair in the inner
  acf_render_block() helper
- Remove the tear_down workaround in the block render tests that
  tracked leaked block ids and reset them manually
- Add a regression test asserting unrelated post meta survives all
  three preview render paths

Fixes WordPress#455
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props faisalahammad, cbravobernal.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

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.

acf_rendered_block() preview render leaks acf_setup_meta() state (no matching acf_reset_meta)

1 participant