|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class used to handle lazy loading for gallery templates |
|
4
|
|
|
* Date: 20/03/2017 |
|
5
|
|
|
*/ |
|
6
|
|
|
if ( ! class_exists( 'FooGallery_LazyLoad' ) ) { |
|
7
|
|
|
|
|
8
|
|
|
class FooGallery_LazyLoad { |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
function __construct() { |
|
|
|
|
|
|
11
|
|
|
//change the image src attribute to data attributes if lazy loading is enabled |
|
12
|
|
|
add_filter( 'foogallery_attachment_html_image_attributes', array($this, 'change_src_attributes'), 99, 3); |
|
13
|
|
|
|
|
14
|
|
|
//add the lazy load attributes to the gallery container |
|
15
|
|
|
add_filter( 'foogallery_build_container_data_options', array( $this, 'add_lazyload_options' ), 10, 3 ); |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Determine if the gallery has lazy loading support |
|
20
|
|
|
* |
|
21
|
|
|
* @param $foogallery |
|
22
|
|
|
* @param $foogallery_template |
|
23
|
|
|
*/ |
|
24
|
|
|
function determine_lazyloading_support( $foogallery, $foogallery_template ) { |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
//make sure we only do this once for better performance |
|
27
|
|
|
if ( !isset( $foogallery->lazyload ) ) { |
|
28
|
|
|
|
|
29
|
|
|
//load the gallery template |
|
30
|
|
|
$template_info = foogallery_get_gallery_template( $foogallery_template ); |
|
31
|
|
|
|
|
32
|
|
|
//check if the template supports lazy loading |
|
33
|
|
|
$lazy_load = isset($template_info['lazyload_support']) && |
|
34
|
|
|
true === $template_info['lazyload_support']; |
|
35
|
|
|
|
|
36
|
|
|
$foogallery->lazyload = apply_filters( 'foogallery_lazy_load', $lazy_load, $foogallery, $foogallery_template ); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $attr |
|
42
|
|
|
* @param array $args |
|
43
|
|
|
* @param FooGalleryAttachment $attachment |
|
44
|
|
|
* @return mixed |
|
45
|
|
|
*/ |
|
46
|
|
|
function change_src_attributes($attr, $args, $attachment) { |
|
|
|
|
|
|
47
|
|
|
global $current_foogallery; |
|
|
|
|
|
|
48
|
|
|
global $current_foogallery_template; |
|
49
|
|
|
|
|
50
|
|
|
if ( $current_foogallery !== null ) { |
|
51
|
|
|
|
|
52
|
|
|
$this->determine_lazyloading_support( $current_foogallery, $current_foogallery_template ); |
|
53
|
|
|
|
|
54
|
|
|
if ( isset( $current_foogallery->lazyload ) && true === $current_foogallery->lazyload ) { |
|
55
|
|
|
|
|
56
|
|
|
if ( isset( $attr['src'] ) ) { |
|
57
|
|
|
//rename src => data-src |
|
58
|
|
|
$src = $attr['src']; |
|
59
|
|
|
unset( $attr['src'] ); |
|
60
|
|
|
$attr['data-src'] = $src; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ( isset( $attr['srcset'] ) ) { |
|
64
|
|
|
//rename srcset => data-srcset |
|
65
|
|
|
$src = $attr['srcset']; |
|
66
|
|
|
unset( $attr['srcset'] ); |
|
67
|
|
|
$attr['data-srcset'] = $src; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $attr; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Add the required lazy load options if needed |
|
78
|
|
|
* |
|
79
|
|
|
* @param $attributes array |
|
80
|
|
|
* @param $gallery FooGallery |
|
81
|
|
|
* |
|
82
|
|
|
* @return array |
|
83
|
|
|
*/ |
|
84
|
|
|
function add_lazyload_options($options, $gallery, $attributes) { |
|
|
|
|
|
|
85
|
|
|
global $current_foogallery_template; |
|
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
$this->determine_lazyloading_support( $gallery, $current_foogallery_template ); |
|
88
|
|
|
|
|
89
|
|
|
if ( isset( $gallery->lazyload) && true === $gallery->lazyload) { |
|
90
|
|
|
$options['lazy'] = true; |
|
91
|
|
|
} |
|
92
|
|
|
return $options; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.