Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
fix regex
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Jan 4, 2022
1 parent 2aeca80 commit 8723818
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/BlockTypes/LegacyTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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 );
}


Expand Down

0 comments on commit 8723818

Please sign in to comment.