Fix: guard $template->media reads against undefined property#835
Merged
Conversation
`add_templates_to_local()` reads `$template->media` in two places
(templates loop, template parts loop) without checking whether the
property exists. The `media` property is only attached by
`prepare_template_for_export()` on code paths where there is image
media to localize — on every other path, accessing it raises a PHP
8.2+ deprecation notice ("Creation of dynamic property" /
"Undefined property: WP_Block_Template::\$media").
Wrap both reads with `! empty()`, mirroring the convention already
used a few lines later for `$template->pattern`. Existing behavior
is preserved: `! empty()` returns false for both undefined and
falsy values.
Closes #834
Contributor
Author
|
Thank you @mikachan |
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.
Summary
CBT_Theme_Templates::add_templates_to_local()reads$template->mediain two places (templates loop, template parts loop) without checking whether the property exists. Themediaproperty is only attached byprepare_template_for_export()on code paths where there is image media to localize — on every other path, accessing it raises a PHP 8.2+ deprecation notice:Change
Wrap both reads with
! empty(). This mirrors the convention used a few lines later for$template->pattern(line 238 / 266), which already usesisset().! empty()returnsfalsefor both undefined and falsy values, so the existing runtime behavior is preserved.Why this is two lines, not one
There are two near-identical copies of the same loop body — one for templates, one for template parts. Both need the same guard. Worth consolidating eventually, but that is a larger refactor and not the goal here.
Test plan
npm run lint:phpcleannpm run test:unit:php— 94 tests pass on this branch (off trunk)No new tests added: the bug is a runtime deprecation that doesn't change observable PHPUnit outcomes on PHP 7.4 / 8.0 / 8.1, where the dynamic property assignment is still allowed without a notice. PHP 8.2+ runs in CI cover the deprecation surface.
How this was discovered
Surfaced by a strict error handler in #833's regression test. That test was narrowed to only catch the
processOnlySavedTemplatesnotice so it would not fail on this unrelated pre-existing issue. With this PR merged, the narrowed matcher in #833 could be broadened back to the original strict handler — small follow-up, not blocking.Closes #834