index.php
2.19 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
<?php
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! function_exists( 'stackable_load_carousel_frontend_script' ) ) {
function stackable_load_carousel_frontend_script() {
if ( ! is_admin() ) {
wp_enqueue_script(
'stk-frontend-carousel',
plugins_url( 'dist/frontend_block_carousel.js', STACKABLE_FILE ),
array(),
STACKABLE_VERSION,
true
);
}
}
add_action( 'stackable/carousel/enqueue_scripts', 'stackable_load_carousel_frontend_script' );
}
if ( ! get_option( 'stackable_enable_carousel_lazy_loading' ) && get_option( 'stackable_enable_carousel_lazy_loading' ) !== false ) {
// Lazy loaded images inside carousels make the carousel height buggy and show extra
// spaces because the height isn't available in lazy loaded images. Prevent images from
// hidden slides from lazy loading to prevent this.
if ( ! function_exists( 'stackable_carousel_add_class_images' ) ) {
function stackable_carousel_add_class_images( $block_content, $block ) {
if ( ! class_exists( 'WP_HTML_Tag_Processor' ) ) {
return $block_content;
}
$index = 1;
$slides_to_show = 1;
if ( ! empty( $block ) && isset( $block['attrs'] ) && is_array( $block['attrs'] ) ) {
if ( isset( $block['attrs']['slidesToShow'] ) ) {
$slides_to_show = $block['attrs']['slidesToShow'];
}
}
$html_tag = new WP_HTML_Tag_Processor( $block_content );
while ( $html_tag->next_tag( 'img' ) ) {
$img_classname = $html_tag->get_attribute( 'class' );
// add for images that are not immediately shown
if ( strpos( $img_classname, 'stk-img') !== false && $index > $slides_to_show ) {
$html_tag->add_class( 'stk-img-carousel' );
}
$index++;
}
return $html_tag->get_updated_html();
}
add_filter( 'render_block_stackable/carousel', 'stackable_carousel_add_class_images', 1, 2 );
}
if ( ! function_exists( 'stackable_skip_loading_lazy_carousel_image' ) ) {
function stackable_skip_loading_lazy_carousel_image( $value, $image ) {
if ( ! strpos( $image, 'stk-img-carousel' ) === false ) {
return false;
}
return $value;
}
add_filter( 'wp_img_tag_add_loading_attr', 'stackable_skip_loading_lazy_carousel_image', 10, 2 );
}
}