Skip to content

Honor legacy template block metadata in themes#515

Merged
sorinmarta merged 1 commit into
masterfrom
fix/duplicate-contact-form
Jul 7, 2026
Merged

Honor legacy template block metadata in themes#515
sorinmarta merged 1 commit into
masterfrom
fix/duplicate-contact-form

Conversation

@sorinmarta

@sorinmarta sorinmarta commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

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.

Prevents duplicate contact forms when BD themes manually render after blocks.
@sorinmarta sorinmarta added the run analysis Runs phpcs and phpunit label Jul 6, 2026
@coderabbitai

coderabbitai Bot commented Jul 6, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

The get_template_meta() function in includes/themes.php now falls back to parsing legacy __$__template__ declarations when standard file-data metadata is empty for blocks or variables keys, using two new private helper functions to extract and parse legacy values.

Changes

Legacy Template Metadata Fallback

Layer / File(s) Summary
Fallback logic and legacy parsing helpers
includes/themes.php
get_template_meta() populates empty blocks/variables from legacy metadata instead of defaulting to empty arrays; new private helpers get_legacy_template_meta() and parse_legacy_template_meta_values() read and regex-parse the legacy __$__template__ array declaration from template files.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change around honoring legacy template block metadata.
Description check ✅ Passed The description is directly related to the change and explains the legacy block metadata behavior fix.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/duplicate-contact-form

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 paths: directive.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 win

Legacy 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 whether blocks or variables are missing. Since get_template_meta() is called from render_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 standard get_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 value

Replace @since x.x placeholders 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 literal x.x placeholder 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 win

Consider 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->cache array for templates/rendered/vars; adding a template_meta cache keyed by $template_path would 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

📥 Commits

Reviewing files that changed from the base of the PR and between ec03299 and 7e49d0d.

📒 Files selected for processing (1)
  • includes/themes.php

@sorinmarta
sorinmarta merged commit 2b45392 into master Jul 7, 2026
8 checks passed
@sorinmarta
sorinmarta deleted the fix/duplicate-contact-form branch July 7, 2026 05:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run analysis Runs phpcs and phpunit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant