Skip to content

Fix: guard $template->media reads against undefined property#835

Merged
bph merged 1 commit into
trunkfrom
fix/template-media-undefined-property
May 6, 2026
Merged

Fix: guard $template->media reads against undefined property#835
bph merged 1 commit into
trunkfrom
fix/template-media-undefined-property

Conversation

@bph
Copy link
Copy Markdown
Contributor

@bph bph commented May 5, 2026

Summary

CBT_Theme_Templates::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:

Deprecated: Creation of dynamic property … (or)
Undefined property: WP_Block_Template::$media

Change

Wrap both reads with ! empty(). This mirrors the convention used a few lines later for $template->pattern (line 238 / 266), which already uses isset().

- if ( $template->media ) {
+ if ( ! empty( $template->media ) ) {
      CBT_Theme_Media::add_media_to_local( $template->media );
  }

! empty() returns false for 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:php clean
  • npm 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 processOnlySavedTemplates notice 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

`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
@bph bph requested review from MaggieCabrera and mikachan May 6, 2026 10:00
@bph bph marked this pull request as ready for review May 6, 2026 10:00
Copy link
Copy Markdown
Member

@mikachan mikachan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes LGTM, thank you!

@bph bph merged commit 2164934 into trunk May 6, 2026
13 checks passed
@bph bph deleted the fix/template-media-undefined-property branch May 6, 2026 10:25
@bph
Copy link
Copy Markdown
Contributor Author

bph commented May 6, 2026

Thank you @mikachan

@bph bph added the bug Something isn't working label May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PHP deprecation: undefined $template->media property access during template save

2 participants