carousel.php 10.7 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();
                            $iframe = get_sub_field('video');
                            preg_match('/src="(.+?)"/', $iframe, $matches);
                            $src =  explode('embed/', $matches[1]);
                            $src =  explode('?',  $src[1]);
                             $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 else: ?>
                                                    <img 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 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;
             case 'featured':  ?>
                <?php if( have_rows('promo_items') ): ?>
                        <div class=" carousel-items promo-carousel featured-carousel">
                            <div class='swiper-wrapper'>
                            <?php while( the_repeater_field('promo_items') ): ?>
                            <?php $page = get_sub_field('featured_post'); ?>
                                <div class="swiper-slide">
                                    <div class="carousel-content">

                                            <div class="promo-image" >
                                           <?php $image =  get_the_post_thumbnail_url( $page->ID, 'medium' );
                                                 $thumbnail_id = get_post_meta( $page->ID, '_thumbnail_id', true );
                                                 $img_alt = get_post_meta ( $thumbnail_id, '_wp_attachment_image_alt', true );                    
                                                    if(!$img_alt){
                                                        $img_alt = get_the_title($thumbnail_id);
                                                    }
                                            ?>
                                                    <img src="<?php echo $image; ?>" alt="<?php echo $img_alt ?>" loading="eager">
                                              </div>
                                           <div class="promo-text"><h2><?php echo  get_the_title($page->ID); ?></h2><p><?php echo get_the_excerpt($page->ID); ?></p><a class="understrap-read-more-link" href="<?php echo get_permalink($page->ID);?>"><?= __('READ MORE', 'msf') ?></a></div>
                                            
                             
                                    </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>