-
Notifications
You must be signed in to change notification settings - Fork 0
WP_HTML_Tag_Processor
Mark Howells-Mead edited this page Mar 10, 2026
·
1 revision
<?php
namespace SayHello\Theme\Blocks\CoreGroup;
use WP_HTML_Tag_Processor;
class Block
{
/**
* Initialize the controller by registering actions and filters.
*
* @return void
*/
public function run(): void
{
add_filter('render_block_core/group', [$this, 'blockGapClass'], 10, 2);
}
/**
* Adds a custom class to the group block if a custom block gap is set.
*
* @param string $html The HTML of the block.
* @param array $block The block data.
* @return string The modified HTML of the block.
*/
public function blockGapClass(string $html, array $block): string
{
$blockGap = false;
if (
isset($block['attrs']) && is_array($block['attrs']) &&
isset($block['attrs']['style']) && is_array($block['attrs']['style']) &&
isset($block['attrs']['style']['spacing']) && is_array($block['attrs']['style']['spacing']) &&
isset($block['attrs']['style']['spacing']['blockGap'])
) {
$blockGap = $block['attrs']['style']['spacing']['blockGap'];
}
if (!$blockGap) {
return $html;
}
$processor = new WP_HTML_Tag_Processor($html);
if ($processor->next_tag(['class_name' => 'wp-block-group'])) {
$processor->add_class('wp-block-group--sht-custom-block-gap');
return $processor->get_updated_html();
}
return $html;
}
}Mark Howells-Mead | https://permanenttourist.ch and https://sayhello.ch/ | Wiki since 2016
Use this code freely, widely and for free. Provision of this code provides and implies no guarantee.
Please respect the GPL v3 licence, which is available via http://www.gnu.org/licenses/gpl-3.0.html