From fe21f86e8a26c6a6313b0df3b19faae1f155040e Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 21 Dec 2021 17:36:02 +0100 Subject: [PATCH 01/12] add align wide and full support for legacy template block --- assets/js/blocks/legacy-template/index.tsx | 2 +- src/BlockTypes/LegacyTemplate.php | 22 +++++++++ src/Utils/StyleAttributesUtils.php | 53 ++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/assets/js/blocks/legacy-template/index.tsx b/assets/js/blocks/legacy-template/index.tsx index b54fef312c3..e82717776e6 100644 --- a/assets/js/blocks/legacy-template/index.tsx +++ b/assets/js/blocks/legacy-template/index.tsx @@ -68,7 +68,7 @@ registerBlockType( 'woocommerce/legacy-template', { 'woo-gutenberg-products-block' ), supports: { - align: false, + align: [ 'wide', 'full' ], html: false, multiple: false, reusable: false, diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 4742ad8219a..a3a05733435 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -1,6 +1,8 @@ 'alignwide', + 'style' => null, + ); + } + + if ( 'full' === $align_attribute ) { + return array( + 'class' => 'alignfull', + 'style' => null, + ); + } + + if ( 'left' === $align_attribute ) { + return array( + 'class' => 'alignleft', + 'style' => null, + ); + } + + if ( 'right' === $align_attribute ) { + return array( + 'class' => 'alignright', + 'style' => null, + ); + } + + if ( 'center' === $align_attribute ) { + return array( + 'class' => 'aligncenter', + 'style' => null, + ); + } + + return null; + } + /** * Get classes and styles from attributes. * From 7a755d852e72cef3fe9f589cc2f04cfebd29b063 Mon Sep 17 00:00:00 2001 From: Luigi Date: Wed, 22 Dec 2021 09:55:01 +0100 Subject: [PATCH 02/12] fix PHP warning --- src/BlockTypes/LegacyTemplate.php | 15 +++++++++++++-- src/Utils/StyleAttributesUtils.php | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index a3a05733435..915c658e595 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -23,6 +23,15 @@ class LegacyTemplate extends AbstractDynamicBlock { */ protected $api_version = '2'; + + /** + * Initialize this block. + */ + protected function initialize() { + parent::initialize(); + add_filter( 'render_block', array( $this, 'get_markup_with_classes_by_attributes' ), 10, 2 ); + } + /** * Render method for the Legacy Template block. This method will determine which template to render. * @@ -66,7 +75,6 @@ protected function render( $attributes, $content ) { * @return string Rendered block type output. */ protected function render_single_product() { - add_filter( 'render_block', array( $this, 'get_markup_with_classes_by_attributes' ), 10, 2 ); ob_start(); /** @@ -102,7 +110,6 @@ protected function render_single_product() { * @return string Rendered block type output. */ protected function render_archive_product() { - add_filter( 'render_block', array( $this, 'get_markup_with_classes_by_attributes' ), 10, 2 ); ob_start(); /** @@ -199,6 +206,10 @@ public function get_markup_with_classes_by_attributes( string $content, array $b $matches = array(); preg_match( $pattern, $content, $matches ); + if ( ! isset( $matches[0] ) ) { + return $content; + } + return preg_replace( $pattern, $matches[0] . ' ' . $align_class['class'], $content, 1 ); } diff --git a/src/Utils/StyleAttributesUtils.php b/src/Utils/StyleAttributesUtils.php index c3eebca5b96..08ba95f0e6c 100644 --- a/src/Utils/StyleAttributesUtils.php +++ b/src/Utils/StyleAttributesUtils.php @@ -163,7 +163,7 @@ public static function get_background_color_class_and_style( $attributes ) { */ public static function get_align_class_and_style( $attributes ) { - $align_attribute = $attributes['align']; + $align_attribute = isset( $attributes['align'] ) ? $attributes['align'] : null; if ( ! $align_attribute ) { return null; From bb19d24fe1ea43506ca27fb6a8c78f1eabf70aea Mon Sep 17 00:00:00 2001 From: Luigi Date: Thu, 23 Dec 2021 10:47:43 +0100 Subject: [PATCH 03/12] 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'] ); } } From 61663068e82e8ae0ab80d1c789fa436d1b875a10 Mon Sep 17 00:00:00 2001 From: Luigi Date: Mon, 27 Dec 2021 11:30:12 +0100 Subject: [PATCH 04/12] rename function --- src/BlockTypes/LegacyTemplate.php | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 2b24438a018..969d668450c 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -23,19 +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. */ protected function initialize() { parent::initialize(); - add_filter( 'render_block', array( $this, 'get_markup_with_classes_by_attributes' ), 10, 2 ); + add_filter( 'render_block', array( $this, 'add_alignment_class_to_wrapper' ), 10, 2 ); } /** @@ -59,9 +52,11 @@ 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'], $this->archive_templates, true ) ) { + } elseif ( in_array( $attributes['template'], $archive_templates, true ) ) { return $this->render_archive_product(); } else { ob_start(); @@ -205,8 +200,8 @@ protected function render_archive_product() { * @param array $block Parsed block data. * @return string Rendered block type output. */ - public function get_markup_with_classes_by_attributes( string $content, array $block ) { - if ( ! $this->is_legacy_template( $block ) ) { + public function add_alignment_class_to_wrapper( string $content, array $block ) { + if ( ( 'woocommerce/' . $this->block_name ) !== $block['blockName'] ) { return $content; } @@ -223,15 +218,5 @@ public function get_markup_with_classes_by_attributes( string $content, array $b 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'] ); - } } From 012741e12e12c39909448802ded89ac6788d05e5 Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 21 Dec 2021 17:36:02 +0100 Subject: [PATCH 05/12] add align wide and full support for legacy template block --- assets/js/blocks/legacy-template/index.tsx | 2 +- src/BlockTypes/LegacyTemplate.php | 22 +++++++++ src/Utils/StyleAttributesUtils.php | 53 ++++++++++++++++++++++ 3 files changed, 76 insertions(+), 1 deletion(-) diff --git a/assets/js/blocks/legacy-template/index.tsx b/assets/js/blocks/legacy-template/index.tsx index b54fef312c3..e82717776e6 100644 --- a/assets/js/blocks/legacy-template/index.tsx +++ b/assets/js/blocks/legacy-template/index.tsx @@ -68,7 +68,7 @@ registerBlockType( 'woocommerce/legacy-template', { 'woo-gutenberg-products-block' ), supports: { - align: false, + align: [ 'wide', 'full' ], html: false, multiple: false, reusable: false, diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 4742ad8219a..a3a05733435 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -1,6 +1,8 @@ 'alignwide', + 'style' => null, + ); + } + + if ( 'full' === $align_attribute ) { + return array( + 'class' => 'alignfull', + 'style' => null, + ); + } + + if ( 'left' === $align_attribute ) { + return array( + 'class' => 'alignleft', + 'style' => null, + ); + } + + if ( 'right' === $align_attribute ) { + return array( + 'class' => 'alignright', + 'style' => null, + ); + } + + if ( 'center' === $align_attribute ) { + return array( + 'class' => 'aligncenter', + 'style' => null, + ); + } + + return null; + } + /** * Get classes and styles from attributes. * From 987445e0f856653fcec27ad026c8732a92847b76 Mon Sep 17 00:00:00 2001 From: Luigi Date: Wed, 22 Dec 2021 09:55:01 +0100 Subject: [PATCH 06/12] fix PHP warning --- src/BlockTypes/LegacyTemplate.php | 15 +++++++++++++-- src/Utils/StyleAttributesUtils.php | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index a3a05733435..915c658e595 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -23,6 +23,15 @@ class LegacyTemplate extends AbstractDynamicBlock { */ protected $api_version = '2'; + + /** + * Initialize this block. + */ + protected function initialize() { + parent::initialize(); + add_filter( 'render_block', array( $this, 'get_markup_with_classes_by_attributes' ), 10, 2 ); + } + /** * Render method for the Legacy Template block. This method will determine which template to render. * @@ -66,7 +75,6 @@ protected function render( $attributes, $content ) { * @return string Rendered block type output. */ protected function render_single_product() { - add_filter( 'render_block', array( $this, 'get_markup_with_classes_by_attributes' ), 10, 2 ); ob_start(); /** @@ -102,7 +110,6 @@ protected function render_single_product() { * @return string Rendered block type output. */ protected function render_archive_product() { - add_filter( 'render_block', array( $this, 'get_markup_with_classes_by_attributes' ), 10, 2 ); ob_start(); /** @@ -199,6 +206,10 @@ public function get_markup_with_classes_by_attributes( string $content, array $b $matches = array(); preg_match( $pattern, $content, $matches ); + if ( ! isset( $matches[0] ) ) { + return $content; + } + return preg_replace( $pattern, $matches[0] . ' ' . $align_class['class'], $content, 1 ); } diff --git a/src/Utils/StyleAttributesUtils.php b/src/Utils/StyleAttributesUtils.php index c3eebca5b96..08ba95f0e6c 100644 --- a/src/Utils/StyleAttributesUtils.php +++ b/src/Utils/StyleAttributesUtils.php @@ -163,7 +163,7 @@ public static function get_background_color_class_and_style( $attributes ) { */ public static function get_align_class_and_style( $attributes ) { - $align_attribute = $attributes['align']; + $align_attribute = isset( $attributes['align'] ) ? $attributes['align'] : null; if ( ! $align_attribute ) { return null; From 187bb61e28a463ffde90e06f6005f94dafe04519 Mon Sep 17 00:00:00 2001 From: Luigi Date: Thu, 23 Dec 2021 10:47:43 +0100 Subject: [PATCH 07/12] 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'] ); } } From b4cf934833bcfeb8c50c15be54d4337bbe593a7a Mon Sep 17 00:00:00 2001 From: Luigi Date: Mon, 27 Dec 2021 11:30:12 +0100 Subject: [PATCH 08/12] rename function --- src/BlockTypes/LegacyTemplate.php | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 2b24438a018..969d668450c 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -23,19 +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. */ protected function initialize() { parent::initialize(); - add_filter( 'render_block', array( $this, 'get_markup_with_classes_by_attributes' ), 10, 2 ); + add_filter( 'render_block', array( $this, 'add_alignment_class_to_wrapper' ), 10, 2 ); } /** @@ -59,9 +52,11 @@ 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'], $this->archive_templates, true ) ) { + } elseif ( in_array( $attributes['template'], $archive_templates, true ) ) { return $this->render_archive_product(); } else { ob_start(); @@ -205,8 +200,8 @@ protected function render_archive_product() { * @param array $block Parsed block data. * @return string Rendered block type output. */ - public function get_markup_with_classes_by_attributes( string $content, array $block ) { - if ( ! $this->is_legacy_template( $block ) ) { + public function add_alignment_class_to_wrapper( string $content, array $block ) { + if ( ( 'woocommerce/' . $this->block_name ) !== $block['blockName'] ) { return $content; } @@ -223,15 +218,5 @@ public function get_markup_with_classes_by_attributes( string $content, array $b 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'] ); - } } From f4689e61b78baea2d005b697431e732c811a77ed Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 4 Jan 2022 18:44:04 +0100 Subject: [PATCH 09/12] fix regex --- src/BlockTypes/LegacyTemplate.php | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 969d668450c..12cf046f3a5 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -205,17 +205,31 @@ public function add_alignment_class_to_wrapper( string $content, array $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] ) || ! isset( $align_class_and_style['class'] ) ) { + if ( ! isset( $align_class_and_style['class'] ) ) { return $content; } - return preg_replace( $pattern, $matches[0] . ' ' . $align_class_and_style['class'], $content, 1 ); + // Find the first tag. + $first_tag = '<[^<>]+>'; + $matches = array(); + preg_match( $first_tag, $content, $matches ); + + // If there is a tag, but it doesn't have a class attribute, add the class attribute. + if ( isset( $matches[0] ) && strpos( $matches[0], 'class' ) === false ) { + $pattern_before_tag_closing = '/.+?(?=>)/'; + $matches = array(); + preg_match( $pattern_before_tag_closing, $content, $matches ); + return preg_replace( $pattern_before_tag_closing, $matches[0] . ' class="' . $align_class_and_style['class'] . '"', $content, 1 ); + } + + // If there is a tag, and it has a class already, add the class attribute. + $pattern_get_class = '/(?<=class=\")[^"]+(?=\")/'; + $matches_class = array(); + preg_match( $pattern_get_class, $content, $matches_class ); + return preg_replace( $pattern_get_class, $matches_class[0] . ' ' . $align_class_and_style['class'], $content, 1 ); } From e291dc3db1d7d8c928a1570f6818dd02697602e8 Mon Sep 17 00:00:00 2001 From: Luigi Date: Wed, 5 Jan 2022 16:28:02 +0100 Subject: [PATCH 10/12] update regex --- src/BlockTypes/LegacyTemplate.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 12cf046f3a5..75a717cd587 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -218,7 +218,7 @@ public function add_alignment_class_to_wrapper( string $content, array $block ) preg_match( $first_tag, $content, $matches ); // If there is a tag, but it doesn't have a class attribute, add the class attribute. - if ( isset( $matches[0] ) && strpos( $matches[0], 'class' ) === false ) { + if ( isset( $matches[0] ) && strpos( $matches[0], ' class=' ) === false ) { $pattern_before_tag_closing = '/.+?(?=>)/'; $matches = array(); preg_match( $pattern_before_tag_closing, $content, $matches ); @@ -226,7 +226,7 @@ public function add_alignment_class_to_wrapper( string $content, array $block ) } // If there is a tag, and it has a class already, add the class attribute. - $pattern_get_class = '/(?<=class=\")[^"]+(?=\")/'; + $pattern_get_class = '/(?<=class=\"|\')[^"|\']+(?=\"|\')/'; $matches_class = array(); preg_match( $pattern_get_class, $content, $matches_class ); return preg_replace( $pattern_get_class, $matches_class[0] . ' ' . $align_class_and_style['class'], $content, 1 ); From 891edf729bb84ca957667a1ea2843e25d5b693d2 Mon Sep 17 00:00:00 2001 From: Luigi Date: Thu, 6 Jan 2022 17:14:42 +0100 Subject: [PATCH 11/12] update regex --- src/BlockTypes/LegacyTemplate.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 75a717cd587..3fe2f3b709a 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -220,9 +220,7 @@ public function add_alignment_class_to_wrapper( string $content, array $block ) // If there is a tag, but it doesn't have a class attribute, add the class attribute. if ( isset( $matches[0] ) && strpos( $matches[0], ' class=' ) === false ) { $pattern_before_tag_closing = '/.+?(?=>)/'; - $matches = array(); - preg_match( $pattern_before_tag_closing, $content, $matches ); - return preg_replace( $pattern_before_tag_closing, $matches[0] . ' class="' . $align_class_and_style['class'] . '"', $content, 1 ); + return preg_replace( $pattern_before_tag_closing, '$0 class="' . $align_class_and_style['class'] . '"', $content, 1 ); } // If there is a tag, and it has a class already, add the class attribute. From 6ed25c3a91d48ee873424baa3444faf3d3558770 Mon Sep 17 00:00:00 2001 From: Luigi Date: Fri, 7 Jan 2022 10:28:10 +0100 Subject: [PATCH 12/12] fix code styling --- src/BlockTypes/LegacyTemplate.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/BlockTypes/LegacyTemplate.php b/src/BlockTypes/LegacyTemplate.php index 3fe2f3b709a..cac9312ffa2 100644 --- a/src/BlockTypes/LegacyTemplate.php +++ b/src/BlockTypes/LegacyTemplate.php @@ -225,9 +225,7 @@ public function add_alignment_class_to_wrapper( string $content, array $block ) // If there is a tag, and it has a class already, add the class attribute. $pattern_get_class = '/(?<=class=\"|\')[^"|\']+(?=\"|\')/'; - $matches_class = array(); - preg_match( $pattern_get_class, $content, $matches_class ); - return preg_replace( $pattern_get_class, $matches_class[0] . ' ' . $align_class_and_style['class'], $content, 1 ); + return preg_replace( $pattern_get_class, '$0 ' . $align_class_and_style['class'], $content, 1 ); }