From bb19d24fe1ea43506ca27fb6a8c78f1eabf70aea Mon Sep 17 00:00:00 2001 From: Luigi Date: Thu, 23 Dec 2021 10:47:43 +0100 Subject: [PATCH] add a comment on get_markup_with_classes_by_attributes --- src/BlockTypes/LegacyTemplate.php | 39 ++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 9 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 915c658e595..2b24438a018 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -23,6 +23,12 @@ class LegacyTemplate extends AbstractDynamicBlock { */ protected $api_version = '2'; + /** + * List of archive legacy template. + * + * @var array + */ + protected $archive_templates = array( 'archive-product', 'taxonomy-product_cat', 'taxonomy-product_tag' ); /** * Initialize this block. @@ -53,11 +59,9 @@ protected function render( $attributes, $content ) { $frontend_scripts::load_scripts(); } - $archive_templates = array( 'archive-product', 'taxonomy-product_cat', 'taxonomy-product_tag' ); - if ( 'single-product' === $attributes['template'] ) { return $this->render_single_product(); - } elseif ( in_array( $attributes['template'], $archive_templates, true ) ) { + } elseif ( in_array( $attributes['template'], $this->archive_templates, true ) ) { return $this->render_archive_product(); } else { ob_start(); @@ -194,23 +198,40 @@ protected function render_archive_product() { /** * Get HTML markup with the right classes by attributes. + * This function appends the classname at the first element that have the class attribute. + * Based on the experience, all the wrapper elements have a class attribute. * * @param string $content Block content. * @param array $block Parsed block data. * @return string Rendered block type output. */ public function get_markup_with_classes_by_attributes( string $content, array $block ) { - $pattern = '/(?<=class=\")[^"]+(?=\")/'; - $attributes = (array) $block['attrs']; - $align_class = StyleAttributesUtils::get_align_class_and_style( $attributes ); - $matches = array(); + if ( ! $this->is_legacy_template( $block ) ) { + return $content; + } + + $pattern = '/(?<=class=\")[^"]+(?=\")/'; + $attributes = (array) $block['attrs']; + $align_class_and_style = StyleAttributesUtils::get_align_class_and_style( $attributes ); + $matches = array(); preg_match( $pattern, $content, $matches ); - if ( ! isset( $matches[0] ) ) { + if ( ! isset( $matches[0] ) || ! isset( $align_class_and_style['class'] ) ) { return $content; } - return preg_replace( $pattern, $matches[0] . ' ' . $align_class['class'], $content, 1 ); + return preg_replace( $pattern, $matches[0] . ' ' . $align_class_and_style['class'], $content, 1 ); + } + + /** + * Check if the block is a legacy template. + * + * @param array $block Parsed block data. + * @return boolean + */ + protected function is_legacy_template( $block ) { + $attributes = (array) $block['attrs']; + return isset( $attributes['template'] ) && ( in_array( $attributes['template'], $this->archive_templates, true ) || 'single-product' === $attributes['template'] ); } }