-
Notifications
You must be signed in to change notification settings - Fork 219
Add wide and full alignment support for legacy template block #5433
Changes from 10 commits
fe21f86
7a755d8
bb19d24
6166306
012741e
987445e
187bb61
b4cf934
2aeca80
f4689e6
e291dc3
891edf7
6ed25c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
<?php | ||
namespace Automattic\WooCommerce\Blocks\BlockTypes; | ||
|
||
use Automattic\WooCommerce\Blocks\Utils\StyleAttributesUtils; | ||
|
||
/** | ||
* Legacy Single Product class | ||
* | ||
|
@@ -21,6 +23,14 @@ class LegacyTemplate extends AbstractDynamicBlock { | |
*/ | ||
protected $api_version = '2'; | ||
|
||
/** | ||
* Initialize this block. | ||
*/ | ||
protected function initialize() { | ||
parent::initialize(); | ||
add_filter( 'render_block', array( $this, 'add_alignment_class_to_wrapper' ), 10, 2 ); | ||
} | ||
|
||
/** | ||
* Render method for the Legacy Template block. This method will determine which template to render. | ||
* | ||
|
@@ -180,4 +190,47 @@ protected function render_archive_product() { | |
wp_reset_postdata(); | ||
return ob_get_clean(); | ||
} | ||
|
||
/** | ||
* 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 add_alignment_class_to_wrapper( string $content, array $block ) { | ||
if ( ( 'woocommerce/' . $this->block_name ) !== $block['blockName'] ) { | ||
return $content; | ||
} | ||
|
||
$attributes = (array) $block['attrs']; | ||
$align_class_and_style = StyleAttributesUtils::get_align_class_and_style( $attributes ); | ||
|
||
if ( ! isset( $align_class_and_style['class'] ) ) { | ||
return $content; | ||
} | ||
|
||
// 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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use backreferences in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think with the current regex.
But the regex Potentially we can use
instead to use regex. I don't have a strong opinion, what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@gigitux I think we can use the fourth argument of <?php
$re = '/.+?(?=>)/m';
$str = '<div data-block-name="woocommerce/legacy-template" data-template="taxonomy-product_cat" id="primary" ><div class="inner">';
$subst = '$0 class="alignwide"';
$result = preg_replace($re, $subst, $str, 1);
echo "The result of the substitution is <pre>" . htmlspecialchars( $result ) . "</pre>"; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for sharing this! |
||
} | ||
|
||
// If there is a tag, and it has a class already, add the class attribute. | ||
$pattern_get_class = '/(?<=class=\")[^"]+(?=\")/'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the wrapper element uses single quotes for class, this regex will fail. |
||
$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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can follow the same approach as we use at line 223rd above. |
||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should check for the exact class attribute here. Elements can have
class
as a part of attribute name or attribute value, like:data-class
,data-type="class"
,id="fist-class"
.