<?php
/**
 * Gallery Outlined Grid Block
 *
 * This block was generated by create-acf-block-json.
 *
 * @package WordPress
 *
 * @param   array $block The block settings and attributes.
 * @param   string $content The block inner HTML (empty).
 * @param   bool $is_preview True during AJAX preview.
 * @param   (int|string) $post_id The post ID this block is saved to.
 */

// Block Name (sluggified).
$block_name = 'gallery-outlined-grid';

$namespace = 'lf';

// Create full block name to use in classes.
$block_name = 'wp-block-' . $namespace . '-' . $block_name;

// setup various classes sent through from $block.
$background = ( empty( $block['backgroundColor'] ) ) ? '' : 'has-' . $block['backgroundColor'] . '-background-color';

$has_background = $background ? 'has-background' : '';

$font_size = ( empty( $block['fontSize'] ) ) ? '' : 'has-' . $block['fontSize'] . '-font-size';

$align = ( empty( $block['align'] ) ) ? '' : 'align' . $block['align'];

$additional_classes = $block['className'] ?? '';

$gallery_outlined_grid_image_array = get_field( 'gallery_outlined_grid_image_array' ) ?? null;

$gallery_outlined_grid_columns = get_field( 'gallery_outlined_grid_columns' ) ?? 6;

$all_classes = array(
	$block_name,
	$background,
	$has_background,
	$font_size,
	$align,
	$additional_classes,
	'columns-' . $gallery_outlined_grid_columns,
);

$classes = implode( ' ', $all_classes );

if ( is_admin() && empty( get_field( 'gallery_outlined_grid_image_array' ) ) ) :
	echo '<div style="grid-column: 1 / -1; border: 1px solid black;"><p><strong>Gallery Outlined Grid Block</strong>.<br><br>Select this block and then add images in the sidebar to create a gallery. →</p></div>';
endif;

if ( $gallery_outlined_grid_image_array ) :
	?>

<div class="<?php echo esc_attr( $classes ); ?>">
<div class="wp-block-lf-gallery-outlined-grid__container">
	<?php
	foreach ( $gallery_outlined_grid_image_array as $image_array ) {

		$raw_alt_text = trim( $image_array['alt'] ) !== '' ? $image_array['alt'] : ( $image_array['name'] ?? $image_array['title'] ?? null );
		$alt          = ucwords( str_replace( '_', ' ', $raw_alt_text ) );

		?>
				<div class="wp-block-lf-gallery-outlined-grid__box">
				<?php LF_Utils::display_responsive_images( esc_html( $image_array['id'] ), 'medium', '300px', 'wp-block-lf-gallery-outlined-grid__image', 'lazy', esc_attr( $alt ) ); ?>
				</div>
		<?php
	}
	?>
	</div>
</div>
	<?php
endif;
