Honor legacy template block metadata in themes#515
Conversation
Prevents duplicate contact forms when BD themes manually render after blocks.
WalkthroughThe ChangesLegacy Template Metadata Fallback
Estimated code review effort: 2 (Simple) | ~10 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 PHPStan (2.2.2)PHPStan was skipped because the user-provided config is missing the required Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
includes/themes.php (1)
663-681: 🚀 Performance & Scalability | 🟠 Major | ⚡ Quick winLegacy metadata is parsed unconditionally, even when file-data headers are already present.
get_legacy_template_meta()(SplFileObject open +fread(8192)+ two regex passes) is invoked on Line 669 before the loop even checks whetherblocksorvariablesare missing. Sinceget_template_meta()is called fromrender_template_file()for every template and part rendered (including recursive wrapper rendering), this doubles file I/O and adds regex overhead on a hot path for the common case where standardget_file_data()headers are already present.⚡ Proposed fix: compute legacy metadata lazily
- $template_meta = get_file_data( $template_path, $default_headers, 'business_directory_template' ); - $legacy_meta = $this->get_legacy_template_meta( $template_path ); - - foreach ( array_keys( $default_headers ) as $variable ) { - if ( ! $template_meta[ $variable ] ) { - $template_meta[ $variable ] = $legacy_meta[ $variable ]; - continue; - } + $template_meta = get_file_data( $template_path, $default_headers, 'business_directory_template' ); + $legacy_meta = null; + + foreach ( array_keys( $default_headers ) as $variable ) { + if ( ! $template_meta[ $variable ] ) { + if ( null === $legacy_meta ) { + $legacy_meta = $this->get_legacy_template_meta( $template_path ); + } + $template_meta[ $variable ] = $legacy_meta[ $variable ]; + continue; + }🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@includes/themes.php` around lines 663 - 681, get_template_meta() eagerly calls get_legacy_template_meta() for every template, even when get_file_data() already returned the needed headers. Move the legacy lookup inside the per-key fallback in get_template_meta() so it is only computed when a specific entry like blocks or variables is missing, and reuse the same lazy-loaded result for both keys if needed. This keeps render_template_file() and get_template_meta() on the fast path for templates that already use the standard headers.
🧹 Nitpick comments (2)
includes/themes.php (2)
683-739: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueReplace
@since x.xplaceholders with actual target version.Other methods in this file use concrete version numbers (e.g.
@since 6.4.22,@since 5.13.2). These two new methods still use the literalx.xplaceholder and should be updated to the actual release version before merge.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@includes/themes.php` around lines 683 - 739, Replace the `@since x.x` docblock placeholders on `get_legacy_template_meta()` and `parse_legacy_template_meta_values()` with the real release version used by the rest of `includes/themes.php`. Keep the change limited to those two method annotations and match the concrete versioning style already present in this class.
663-681: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick winConsider caching
get_template_meta()results per template path.Metadata parsing (whether via
get_file_data()or the new legacy fallback) is repeated every time the same template is rendered (e.g. a field template rendered once per field). The class already maintains a$this->cachearray for templates/rendered/vars; adding atemplate_metacache keyed by$template_pathwould avoid redundant file reads, especially now that the legacy fallback adds further I/O.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@includes/themes.php` around lines 663 - 681, Add per-template-path caching to get_template_meta() so repeated renders don’t re-read and re-parse the same file. Use the existing $this->cache pattern in the themes class to store template_meta keyed by $template_path, return the cached value on subsequent calls, and only run get_file_data() plus get_legacy_template_meta() when there is no cached entry. Keep the logic inside get_template_meta() and the surrounding cache structure consistent with the other cached template/rendered/vars data.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Outside diff comments:
In `@includes/themes.php`:
- Around line 663-681: get_template_meta() eagerly calls
get_legacy_template_meta() for every template, even when get_file_data() already
returned the needed headers. Move the legacy lookup inside the per-key fallback
in get_template_meta() so it is only computed when a specific entry like blocks
or variables is missing, and reuse the same lazy-loaded result for both keys if
needed. This keeps render_template_file() and get_template_meta() on the fast
path for templates that already use the standard headers.
---
Nitpick comments:
In `@includes/themes.php`:
- Around line 683-739: Replace the `@since x.x` docblock placeholders on
`get_legacy_template_meta()` and `parse_legacy_template_meta_values()` with the
real release version used by the rest of `includes/themes.php`. Keep the change
limited to those two method annotations and match the concrete versioning style
already present in this class.
- Around line 663-681: Add per-template-path caching to get_template_meta() so
repeated renders don’t re-read and re-parse the same file. Use the existing
$this->cache pattern in the themes class to store template_meta keyed by
$template_path, return the cached value on subsequent calls, and only run
get_file_data() plus get_legacy_template_meta() when there is no cached entry.
Keep the logic inside get_template_meta() and the surrounding cache structure
consistent with the other cached template/rendered/vars data.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: 4208dbba-6002-4083-b4ef-8e4963fa200a
📒 Files selected for processing (1)
includes/themes.php
fixes Strategy11/business-directory-premium#360
Core now honors legacy $template block metadata so themes that manually render after blocks no longer also get core auto-append.