I'm learning and want to improve as much as possible. In doing so I have written this custom block that has an image on one side and an inner block on the other. It's using ACF to get the variables and comments should state most of the items.
I'm looking for recommendations to improve, readability, functionality, or security issues I may not have addressed. I made an image function that should be responsive. Everything functions fine but I'd like to make it cleaner and faster.
Looking forward to seeing recommendations - I want to improve in any way possible!
First off the block file
<?php
/**
* Two Column Block with Image & Innerblock
*
* A two column block with innerblock on one side and image on the other
* options of 50/50 and 40/60 sizes available
* Can switch content sides and image is always on top
*
* @param function wg_child_acf_image_block() to return images output
* @author Andrew
*/
$padding = get_field( 'padding' ); //Padding
$top = get_field( 'top' ); //Custom padding top
$bottom = get_field( 'bottom' ); //Custom Padding bottom
$image = get_field( 'image' ); //Image field
$sizing = get_field( 'sizing' ) ?: 'fiftyFifty'; //Select field that sets sizes to 50/50 or 40/60
$side = get_field( 'side' ) ?: 'normal'; //Switch Content Sides
$combined = $sizing . ' ' . $side; //Combined for output
$image_fallback = 'wg-align-image-left'; //Set Default Image alignment
$inlineStyle = ''; //Create variable for optional custom padding
$anchor = ''; //Create variable for custom anchor option
$center = ''; //Create variable for align-self innerblock
//Change default image alignment if reversed
if( $side == 'reverse' ) {
$image_fallback = 'wg-align-image-right';
}
//Alignment of image w/ fallback
$align_image = get_field( 'alignimage' ) ?: $image_fallback;
//Determining Padding For Block - default is else
if( 'narrow' == $padding ) {
$padding = 'blockPadding-narrow';
} elseif( 'none' == $padding ) {
$padding = 'blockPadding-none';
} elseif( 'custom' == $padding ) {
$inlineStyle = 'style="padding-top: ' . $top . 'vh; padding-bottom: ' . $bottom . 'vh;"';
$padding = false;
} else {
$padding = 'blockPadding';
}
//Determines if padding is a class or inline and created classes array
if( $padding ) {
$classes = [$padding];
} else {
$classes = [];
}
//Adds custom class if provided
if( !empty( $block['className'] ) )
$classes = array_merge( $classes, explode( ' ', $block['className'] ) );
//Adds custom anchor if provided
if( !empty( $block['anchor'] ) )
$anchor = ' id="' . sanitize_title( $block['anchor'] ) . '"';
//Default data that goes inside innerblock when created but can be removed
$template = array(
array('core/heading', array(
'content' => 'Title Text Goes Here',
)),
array( 'core/paragraph', array(
'content' => 'Enter in your paragraph text here for further information',
) )
);
//Option to turn off align-self: center;
if( get_field( 'center' ) == false ) {
$center = ' center-self';
}
//Opening Div - determines if class or inline for padding
if( $inlineStyle ) {
echo '<section class="' . join( ' ', $classes ) . '"' . esc_attr($anchor) . ' ' . esc_attr($inlineStyle) . '>';
} else {
echo '<section class="' . join( ' ', $classes ) . '"' . esc_attr($anchor) . '>';
}
?>
<div class="wrapper">
<div class="twoColumns <?php echo esc_attr($combined); ?>">
<div class="leftSide">
<div class="imageWrap">
<?php //arguments are image array, optional image class, optional image size ?>
<?php wg_child_acf_image_block( $image, $align_image ); ?>
</div>
</div>
<div class="rightSide<?php if($center) { echo esc_attr($center); } ?>">
<?php echo '<InnerBlocks template="' . esc_attr( wp_json_encode( $template ) ) . '" />'; ?>
</div>
</div>
</div>
<?php
//closing div
echo '</section>';
?>
Next is the image function which is used in the above file:
<?php
/**
* Function to call ACF Image array and convert it to a SEO friendly image result
*
* Image function that takes an image array from ACF (or other similiar plugins) and
* converts it into a responsive image.
* Required argument of $image which is the array
* Optional argument of $imageClass to pass an optional class
* Optional argument of $imgsize that passes a different size iamge
*
* @param array $image uses ACF image array
* @since 1.0.0
* @author: Andrew
*/
function wg_child_acf_image_block($image, $imageClass = '', $imgsize = 'large') {
if( $image ) {
// Image attribuates.
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'] ?: $title;
$caption = $image['caption'];
$imgsize = 'large';
//adds w on the end of sizes
$w = 'w';
// Image sizes and src sets.
$thumb = $image['sizes'][ $imgsize ];
$width = $image['sizes'][ $imgsize . '-width' ];
$height = $image['sizes'][ $imgsize . '-height' ];
$medlg = $image['sizes']['medium_large'];
$medlg_width = $image['sizes']['medium_large-width'] . $w;
$med = $image['sizes']['medium'];
$med_width = $image['sizes']['medium-width'] . $w;
$tn = $image['sizes']['thumbnail'];
$tn_width = $image['sizes']['thumbnail-width'] . $w;
//Returned Result
$result = '';
// Begin caption wrap.
if( $caption ):
$result .= '<div class="wp-caption">';
endif;
//img item
$result .= '<img src="' . esc_url($thumb) . '" srcset="' . esc_url($medlg) .' '. esc_attr($medlg_width) . ', ' . esc_url($med) . ' ' . esc_attr($med_width) . ', ' . esc_url($tn) . ' ' . esc_attr($tn_width) . '" alt="' . esc_attr($alt) . '" width="' . esc_attr($width) . '" height="' . esc_attr($height) . '" title="' . esc_attr($title) . '" class="' . esc_attr($imageClass) . ' wg-image-class" />';
// End caption wrap.
if( $caption ):
$result .= '<p class="wp-caption-text">' . esc_html($caption) . '</p>';
$result .= '</div>';
?>
<?php endif; ?>
<?php
//end if($image) statement
} else {
//Fallback image if no image
$result = '<img src="' . get_stylesheet_directory_uri() . '/assets/images/filler.jpg' . '" alt="Filler Image" class="' . esc_attr($imageClass) . ' wg-image-class">';
}
//Output the image
echo $result;
}