🐞 fix: Migrate blocks to use helper function for wrapper classes

This commit is contained in:
Keith Solomon
2025-11-20 10:20:29 -06:00
parent 974cb70b5e
commit 38ec5f850d
11 changed files with 53 additions and 90 deletions

View File

@@ -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(

View File

@@ -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 );
?>
<section <?php echo wp_kses_post( $wrapper ); ?>>

View File

@@ -3,7 +3,7 @@
"title": "Block Boilerplate",
"description": "Boilerplate code to create ACF blocks.",
"style": [
"file:./boiilerplate.css"
"file:./boilerplate.css"
],
"category": "vdi-block",
"icon": "block-default",
@@ -20,7 +20,7 @@
"color": false,
"html": false,
"jsx": false,
"mode": false,
"mode": true,
"multiple": false
}
}

View File

@@ -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 );
?>
<section <?php echo esc_attr( $wrapper ); ?>>

View File

@@ -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 );
?>
<div id="<?php echo esc_attr( $block['id'] ); ?>" <?php echo esc_attr( $wrapper ); ?>>

View File

@@ -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 );
?>
<section <?php echo esc_attr( $wrapper ); ?>>

View File

@@ -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 );
?>
<section <?php echo wp_kses_post( $wrapper ); ?>>

View File

@@ -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, '<iframe' ) !== false ) {

View File

@@ -34,11 +34,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, '<iframe' ) !== false ) {

View File

@@ -31,11 +31,7 @@ $children = get_posts(
$classes = 'page-children container relative px-4 py-8 mx-auto sm:px-6 sm:py-12 lg:px-8';
// Set wrapper attributes
if ( ! $is_preview ) {
$wrapper = get_block_wrapper_attributes( array( 'class' => $classes ) );
} else {
$wrapper = 'class="' . $classes . '"';
}
$wrapper = blockWrapperAttributes( $classes, $is_preview );
?>
<?php if ( ! empty( $children ) ) : ?>

View File

@@ -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 );
?>
<section <?php echo wp_kses_post( $wrapper ); ?> style="<?php echo esc_attr( $styles ); ?>">