carousel.php 8.06 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';
}

$carousel_style = get_field('carousel_style');


?>
<div id="<?php echo esc_attr($id); ?>" class="<?php echo esc_attr($className); ?> ">
        <?php switch($carousel_style){  
          
             case 'video':  ?>
             
                <?php if( have_rows('promo_items') ): ?>
                        <div class=" carousel-items promo-carousel">
                            <div class='swiper-wrapper'>
                            <?php while( the_repeater_field('promo_items') ): ?>
                            <?php $index = get_row_index();
                             $modalId = 'modal-' .$index; ?>   
                                <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 src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" loading="eager" />
                                                <?php endif; ?>
                                                    </div>
                                                    <div class="promo-text"><?php echo get_sub_field('title'); ?></div>
                                                    </a>
                                    </div>
                                </div>
                   
                               
                            <?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') ): ?>
                            <?php $index = get_row_index();
                             $modalId = 'modal-' .$index; ?>    
                            <?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 = array(
                                    '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-labelledby="exampleModalLabel" aria-hidden="true">
                                    <div class="modal-dialog  modal-dialog-centered modal-xl">
                                        <div class="modal-content">
                                        <div class="modal-header">
                                            <h5 class="modal-title"></h5>
                                            <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; ?>

            <?php break;
             case 'pdf':  ?>
             
                <?php if( have_rows('promo_items') ): ?>
                        <div class=" carousel-items promo-carousel pdf-carousel">
                            <div class='swiper-wrapper'>
                            <?php while( the_repeater_field('promo_items') ): ?>
                            <?php $page = get_sub_field('pdf'); ?>
                                <div class="swiper-slide">
                                    <div class="carousel-content">
                                    <a href="<?php echo $page ?>" class="pdf_open" >
                                            <div class="promo-image pdf" >
                                            <?php $image = get_sub_field('place_holder');
                                                if( !empty( $image ) ): ?>
                                                    <img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" loading="eager" />
                                                <?php endif; ?>
                                                    <img src="<?php echo $image; ?>" alt="<?php echo $img_alt ?>" loading="eager">
                                                    </div>
                                                    <div class="promo-text"><?php echo get_sub_field('title'); ?></div>
                                                    </a>
                                                   
                   
                                    </div>
                                </div>
                            <?php endwhile; ?>
                        </div>                    
                        <div class="swiper-button-prev" data-id="<?= $id ?>"></div>
                        <div class="swiper-button-next" data-id="<?= $id ?>"></div>
                    </div>
                  
                <?php else: ?>
                        <p>Please add some slides.</p>
                <?php endif; ?>

            <?php break;
                default: ?>    
                <p>Please select style.</p>  
                       <?php }; ?> 
 </div>