From 38ec5f850d4421bff6bd6caa681ec113bfb0e9e6 Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Thu, 20 Nov 2025 10:20:29 -0600 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9E=20fix:=20Migrate=20blocks=20to=20u?= =?UTF-8?q?se=20helper=20function=20for=20wrapper=20classes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/helpers.php | 20 ++++++++ views/blocks/accordion/accordion.php | 9 +--- views/blocks/boilerplate/block.json | 48 +++++++++---------- views/blocks/boilerplate/boilerplate.php | 11 +---- views/blocks/buttons/buttons.php | 9 +--- views/blocks/contact-info/contact-info.php | 11 +---- views/blocks/homepage-hero/homepage-hero.php | 11 +---- .../media-text-innerblocks.php | 6 +-- views/blocks/media-text/media-text.php | 6 +-- views/blocks/page-children/page-children.php | 6 +-- views/blocks/section/section.php | 6 +-- 11 files changed, 53 insertions(+), 90 deletions(-) diff --git a/lib/helpers.php b/lib/helpers.php index 83f27fd..ad1e75e 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -31,6 +31,26 @@ function getFieldValue( $field_path ) { return $field; } +/** + * Returns wrapper attributes for a block for both preview and front-end contexts. + * + * @param string $classes Space separated class list. + * @param bool $is_preview Whether the block is being previewed in the editor. + * + * @return string Wrapper attributes ready for output. + */ +function blockWrapperAttributes( $classes, $is_preview ) { + if ( $is_preview ) { + return 'class="' . $classes . '"'; + } + + return get_block_wrapper_attributes( + array( + 'class' => $classes, + ) + ); +} + // Add Global Fields options page. if ( function_exists( 'acf_add_options_page' ) ) { add_action( diff --git a/views/blocks/accordion/accordion.php b/views/blocks/accordion/accordion.php index 3b168a6..99788c6 100644 --- a/views/blocks/accordion/accordion.php +++ b/views/blocks/accordion/accordion.php @@ -15,14 +15,7 @@ $accItems = get_field( 'accordion_items' ); if ( $accItems ) : $classes = 'accordion'; - - if ( ! $is_preview ) { - $wrapper = get_block_wrapper_attributes( - array( 'class' => $classes ) - ); - } else { - $wrapper = 'class="' . $classes . '"'; - } + $wrapper = blockWrapperAttributes( $classes, $is_preview ); ?>
> diff --git a/views/blocks/boilerplate/block.json b/views/blocks/boilerplate/block.json index 28bc244..a576127 100644 --- a/views/blocks/boilerplate/block.json +++ b/views/blocks/boilerplate/block.json @@ -1,26 +1,26 @@ { - "name": "acf/boilerplate", - "title": "Block Boilerplate", - "description": "Boilerplate code to create ACF blocks.", - "style": [ - "file:./boiilerplate.css" - ], - "category": "vdi-block", - "icon": "block-default", - "keywords": [ - "boilerplate" - ], - "acf": { - "mode": "preview", - "renderTemplate": "boilerplate.php" - }, - "supports": { - "align": false, - "anchor": false, - "color": false, - "html": false, - "jsx": false, - "mode": false, - "multiple": false - } + "name": "acf/boilerplate", + "title": "Block Boilerplate", + "description": "Boilerplate code to create ACF blocks.", + "style": [ + "file:./boilerplate.css" + ], + "category": "vdi-block", + "icon": "block-default", + "keywords": [ + "boilerplate" + ], + "acf": { + "mode": "preview", + "renderTemplate": "boilerplate.php" + }, + "supports": { + "align": false, + "anchor": false, + "color": false, + "html": false, + "jsx": false, + "mode": true, + "multiple": false + } } diff --git a/views/blocks/boilerplate/boilerplate.php b/views/blocks/boilerplate/boilerplate.php index 675df7e..2ec97d3 100644 --- a/views/blocks/boilerplate/boilerplate.php +++ b/views/blocks/boilerplate/boilerplate.php @@ -10,16 +10,7 @@ namespace BasicWP; $classes = 'boilerplate'; - -if ( ! $is_preview ) { - $wrapper = get_block_wrapper_attributes( - array( - 'class' => $classes, - ) - ); -} else { - $wrapper = 'class="' . $classes . '"'; -} +$wrapper = blockWrapperAttributes( $classes, $is_preview ); ?>
> diff --git a/views/blocks/buttons/buttons.php b/views/blocks/buttons/buttons.php index 7c6cd0c..28bb5ba 100644 --- a/views/blocks/buttons/buttons.php +++ b/views/blocks/buttons/buttons.php @@ -12,14 +12,7 @@ namespace BasicWP; $ibClasses = 'flex flex-wrap gap-4 w-full justify-center sm:justify-start'; $classes = 'align-with-content my-[1.2em]'; - -if ( ! $is_preview ) { - $wrapper = get_block_wrapper_attributes( - array( 'class' => $classes ) - ); -} else { - $wrapper = 'class="' . $classes . '"'; -} +$wrapper = blockWrapperAttributes( $classes, $is_preview ); ?>
> diff --git a/views/blocks/contact-info/contact-info.php b/views/blocks/contact-info/contact-info.php index e751d0d..c2e9a39 100644 --- a/views/blocks/contact-info/contact-info.php +++ b/views/blocks/contact-info/contact-info.php @@ -10,16 +10,7 @@ namespace BasicWP; $classes = 'contact-info'; - -if ( ! $is_preview ) { - $wrapper = get_block_wrapper_attributes( - array( - 'class' => $classes, - ) - ); -} else { - $wrapper = 'class="' . $classes . '"'; -} +$wrapper = blockWrapperAttributes( $classes, $is_preview ); ?>
> diff --git a/views/blocks/homepage-hero/homepage-hero.php b/views/blocks/homepage-hero/homepage-hero.php index e7e844b..3cad658 100644 --- a/views/blocks/homepage-hero/homepage-hero.php +++ b/views/blocks/homepage-hero/homepage-hero.php @@ -13,16 +13,7 @@ $intro = get_field( 'intro' ); $ctas = get_field( 'calls_to_action' ); $classes = 'homepage-hero mx-break-out bg-black bg-cover bg-no-repeat text-light py-12 lg:py-16 overflow-hidden'; - -if ( ! $is_preview ) { - $wrapper = get_block_wrapper_attributes( - array( - 'class' => $classes, - ) - ); -} else { - $wrapper = 'class="' . $classes . '"'; -} +$wrapper = blockWrapperAttributes( $classes, $is_preview ); ?>
> diff --git a/views/blocks/media-text-innerblocks/media-text-innerblocks.php b/views/blocks/media-text-innerblocks/media-text-innerblocks.php index f6f516a..4c56f05 100644 --- a/views/blocks/media-text-innerblocks/media-text-innerblocks.php +++ b/views/blocks/media-text-innerblocks/media-text-innerblocks.php @@ -30,11 +30,7 @@ if ( $isDark ) { } // Set wrapper attributes -if ( ! $is_preview ) { - $wrapper = get_block_wrapper_attributes( array( 'class' => $classes ) ); -} else { - $wrapper = 'class="' . $classes . '"'; -} +$wrapper = blockWrapperAttributes( $classes, $is_preview ); // if the video block contains an iframe, add the role="presentation" attribute if ( $vidBlock && strpos( $vidBlock, ' $classes ) ); -} else { - $wrapper = 'class="' . $classes . '"'; -} +$wrapper = blockWrapperAttributes( $classes, $is_preview ); // if the video block contains an iframe, add the role="presentation" attribute if ( $vidBlock && strpos( $vidBlock, ' $classes ) ); -} else { - $wrapper = 'class="' . $classes . '"'; -} +$wrapper = blockWrapperAttributes( $classes, $is_preview ); ?> diff --git a/views/blocks/section/section.php b/views/blocks/section/section.php index 254888c..9659e5c 100644 --- a/views/blocks/section/section.php +++ b/views/blocks/section/section.php @@ -64,11 +64,7 @@ if ( $ovlImage ) { $overlayStyles .= " opacity: $ovlOpacity;"; // Set wrapper attributes -if ( ! $is_preview ) { - $wrapper = get_block_wrapper_attributes( array( 'class' => $classes ) ); -} else { - $wrapper = 'class="' . $classes . '"'; -} +$wrapper = blockWrapperAttributes( $classes, $is_preview ); ?>
style="">