carousel.php
7.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?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-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; ?>
</div>