Skip to content

Commit

Permalink
Revert "Block Settings/Support: Use Tag Processor to inject class nam…
Browse files Browse the repository at this point in the history
…e on wrapper." (#47350)

This reverts commit b187df8.
  • Loading branch information
ockham authored Jan 23, 2023
1 parent f8d9bfd commit e1ccb61
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
25 changes: 20 additions & 5 deletions lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,29 @@ function gutenberg_render_elements_support( $block_content, $block ) {
return $block_content;
}

$class_name = gutenberg_get_elements_class_name( $block );

// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
// Add the class name to the first element, presuming it's the wrapper, if it exists.
$tags = new WP_HTML_Tag_Processor( $block_content );
if ( $tags->next_tag() ) {
$tags->add_class( gutenberg_get_elements_class_name( $block ) );
// Retrieve the opening tag of the first HTML element.
$html_element_matches = array();
preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE );
$first_element = $html_element_matches[0][0];
// If the first HTML element has a class attribute just add the new class
// as we do on layout and duotone.
if ( str_contains( $first_element, 'class="' ) ) {
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="' . $class_name . ' ',
$block_content,
1
);
} else {
// If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
$first_element_offset = $html_element_matches[0][1];
$content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
}

return $tags->get_updated_html();
return $content;
}

/**
Expand Down
25 changes: 20 additions & 5 deletions lib/block-supports/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,29 @@ function _gutenberg_add_block_level_presets_class( $block_content, $block ) {
return $block_content;
}

$class_name = _gutenberg_get_presets_class_name( $block );

// Like the layout hook this assumes the hook only applies to blocks with a single wrapper.
// Add the class name to the first element, presuming it's the wrapper, if it exists.
$tags = new WP_HTML_Tag_Processor( $block_content );
if ( $tags->next_tag() ) {
$tags->add_class( _gutenberg_get_presets_class_name( $block ) );
// Retrieve the opening tag of the first HTML element.
$html_element_matches = array();
preg_match( '/<[^>]+>/', $block_content, $html_element_matches, PREG_OFFSET_CAPTURE );
$first_element = $html_element_matches[0][0];
// If the first HTML element has a class attribute just add the new class
// as we do on layout and duotone.
if ( strpos( $first_element, 'class="' ) !== false ) {
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="' . $class_name . ' ',
$block_content,
1
);
} else {
// If the first HTML element has no class attribute we should inject the attribute before the attribute at the end.
$first_element_offset = $html_element_matches[0][1];
$content = substr_replace( $block_content, ' class="' . $class_name . '"', $first_element_offset + strlen( $first_element ) - 1, 0 );
}

return $tags->get_updated_html();
return $content;
}

/**
Expand Down

0 comments on commit e1ccb61

Please sign in to comment.