From 7e49d0d049b79c03e8cddeb9c66817db5e80b2db Mon Sep 17 00:00:00 2001 From: Sorin Marta Date: Mon, 6 Jul 2026 09:50:37 +0300 Subject: [PATCH] Honor legacy template block metadata in themes Prevents duplicate contact forms when BD themes manually render after blocks. --- includes/themes.php | 60 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/includes/themes.php b/includes/themes.php index 03185a02a..64d39c29e 100644 --- a/includes/themes.php +++ b/includes/themes.php @@ -666,10 +666,11 @@ private function get_template_meta( $template_path ) { 'variables' => 'Template Variables', ); $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 ] = array(); + $template_meta[ $variable ] = $legacy_meta[ $variable ]; continue; } @@ -679,6 +680,63 @@ private function get_template_meta( $template_path ) { return $template_meta; } + /** + * Gets template metadata from the old `$__template__` declaration. + * + * @since x.x + * + * @param string $template_path Path to the template file. + * + * @return array + */ + private function get_legacy_template_meta( $template_path ) { + $template_meta = array( + 'blocks' => array(), + 'variables' => array(), + ); + + if ( ! is_readable( $template_path ) ) { + return $template_meta; + } + + try { + $template_file = new SplFileObject( $template_path, 'r' ); + $template_contents = $template_file->fread( 8192 ); + } catch ( RuntimeException $e ) { + return $template_meta; + } + + if ( ! preg_match( '/\$__template__\s*=\s*array\s*\((.*?)\)\s*;/s', $template_contents, $template_match ) ) { + return $template_meta; + } + + foreach ( array_keys( $template_meta ) as $variable ) { + $pattern = '/[\'"]' . preg_quote( $variable, '/' ) . '[\'"]\s*=>\s*array\s*\(([^)]*)\)/s'; + if ( preg_match( $pattern, $template_match[1], $variable_match ) ) { + $template_meta[ $variable ] = $this->parse_legacy_template_meta_values( $variable_match[1] ); + } + } + + return $template_meta; + } + + /** + * Extracts quoted values from old template metadata arrays. + * + * @since x.x + * + * @param string $values List of values from a legacy template metadata array. + * + * @return array + */ + private function parse_legacy_template_meta_values( $values ) { + if ( ! preg_match_all( '/[\'"]([A-Za-z0-9_-]+)[\'"]/', $values, $matches ) ) { + return array(); + } + + return array_values( array_unique( $matches[1] ) ); + } + function render_part( $template_id, $additional_vars = array() ) { $output = '';