carousel.php 7.07 KB
<?php

/**
 * Carousel Block Template.
 *
 * @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.
 */

// Create id attribute allowing for custom "anchor" value.
$id = "carousel-" . $block["id"];
if (!empty($block["anchor"])) {
    $id = $block["anchor"];
}

// Create class attribute allowing for custom "className" and "align" values.
$className = "carousel";

if (!empty($block["className"])) {
    $className .= " " . $block["className"];
}
if (!empty($block["align"])) {
    $className .= " align" . $block["align"];
}
if ($is_preview) {
    $className .= " is-admin";
}
$ran = getName();

?>
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr(
    $className
); ?> ">
	<?php if (have_rows("promo_items")): ?>
	<div class=" carousel-items promo-carousel">
		<div class='swiper-wrapper'>
			<?php while (the_repeater_field("promo_items")): ?>
                <?php $carousel_style = get_sub_field("carousel_style"); 
                    switch ($carousel_style) { 
                    case "video": ?>
                <?php
                    $index = get_row_index();
                    $iframe = get_sub_field('video');
                    preg_match('/src="(.+?)"/', $iframe, $matches);
                    $src =  explode('embed/', $matches[1]);
                    $src =  explode('?',  $src[1]);
                    $modalId = "modal-" . $index.$ran;
                    ?>
                    <div class="swiper-slide">
                        <div class="carousel-content">
                            <a href="javascript:void(0)" class="video_open" data-bs-toggle="modal" data-bs-target="#<?php echo $modalId; ?>">
                                <div class="promo-image video">
                                    <?php
                                    $image = get_sub_field("place_holder");
                                    if( !empty( $image ) ): ?>
                                        <img width="340" height="226" src="<?php echo esc_url($image['sizes']['medium_large']); ?>" alt="<?php echo esc_attr($image['title']); ?>" loading="eager" />
                                  <?php else: ?>
                                      <img  width="340" height="226" src="https://img.youtube.com/vi/<?php echo $src[0];?>/mqdefault.jpg" alt="Youtube placeholder" loading="eager" />
                                <?php  endif;?>
                                </div>
                                <div class="promo-text"><?php echo get_sub_field("title"); ?></div>
                            </a>
                        </div>
                    </div>
			<?php break;
            case "pdf": ?>
			<?php $page = get_sub_field("pdf"); ?>
			<div class="swiper-slide">
				<div class="carousel-content">
					<a href="<?php echo $page['link']; ?>"  target="_blank" class="pdf_open">
						<div class="promo-image pdf">
							<?php
                                $image = get_sub_field("place_holder");
                                if(!$image){
                                    $image =  $page['icon'];
                                    $img_alt  =  $page['title'];
                                   }
                                   ?>
                                   <img src="<?php echo $image; ?>" alt="<?php echo $img_alt ?>" loading="eager">
						</div>
                       <?php
                        $title = get_sub_field("title");
                        if(!$title){
                            $title =  $page['title'];
                        } ?>
						<div class="promo-text"><?php echo $title ; ?></div>
					</a>
				</div>
			</div>

			<?php break;
            case "outside": ?>
			<?php $page = get_sub_field("outside_url"); ?>
            <div class="swiper-slide">
				<div class="carousel-content">
					<a href="<?php echo $page; ?>"  target="_blank" >
						<div class="promo-image ">
							<?php
                                $image = get_sub_field("place_holder");
                                if (!empty($image)): ?>
                                     <img width="340" height="226" src="<?php echo esc_url($image['sizes']['medium_large']); ?>" alt="<?php echo esc_attr($image['title']); ?>" loading="eager" />
							    <?php endif;?>
					
						</div>
						<div class="promo-text"><?php echo get_sub_field("title"); ?></div>
					</a>
				</div>
			</div>

			<?php break;
            default: ?>

			<?php } ?>
			<?php endwhile; ?>


		</div>
		<div class="swiper-button-prev" data-id="<?= $id ?>"></div>
		<div class="swiper-button-next" data-id="<?= $id ?>"></div>
	</div>
	<?php if (have_rows("promo_items")): ?>
	<?php while (the_repeater_field("promo_items")):
         $carousel_style = get_sub_field("carousel_style"); 
        if($carousel_style == 'video'){  ?>
	      <?php   $index = get_row_index();
                  $modalId = "modal-" . $index.$ran;
                ?>
                <?php
            $iframe = get_sub_field("video");

            // Use preg_match to find iframe src.
            preg_match('/src="(.+?)"/', $iframe, $matches);
            $src = $matches[1];

            // Add extra parameters to src and replace HTML.
            $params = [
                "controls" => 1,
                "hd" => 1,
                "autohide" => 1,
                "enablejsapi" => 1,
                "version" => 3,
            ];
            $new_src = add_query_arg($params, $src);
            $iframe = str_replace($src, $new_src, $iframe);

            // Add extra attributes to iframe HTML.
            $attributes = 'frameborder="0"';
            $iframe = str_replace("></iframe>", " " . $attributes . "></iframe>", $iframe);
            $iframe = str_replace(
                "<iframe",
                '<iframe class="yt_player_iframe"  ',
                $iframe
            );
            ?>
            <div class="modal" id="<?php echo $modalId; ?>" tabindex="-1" aria-hidden="true">
                <div class="modal-dialog  modal-dialog-centered modal-xl">
                    <div class="modal-content">
                        <div class="modal-header">
                            <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                        </div>
                        <div class="modal-body">
                            <?php echo $iframe; ?>
                        </div>
                    </div>
                </div>
            </div>
            <script>
                    var myModalEl = document.getElementById('<?php echo $modalId; ?>')
                    myModalEl.addEventListener('hidden.bs.modal', function(event) {
                        jQuery('.yt_player_iframe').each(function() {
                            this.contentWindow.postMessage('{"event":"command","func":"stopVideo","args":""}', '*')
                        });
                    });
            </script>
	<?php
        }   
        endwhile; ?>
	<?php endif; ?>
	<?php else: ?>
	<p>Please add some slides.</p>
	<?php endif; ?>

</div>