|
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
|
|
|
if ( is_admin() ) { |
|
12
|
|
|
//add extra fields to the templates that support lazy loading |
|
13
|
|
|
add_filter( 'foogallery_override_gallery_template_fields', array( $this, 'add_lazyload_fields' ), 10, 2 ); |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
//adds the lazyload property to a FooGallery |
|
17
|
|
|
add_action( 'foogallery_foogallery_instance_after_load', array( $this, 'determine_lazyload' ), 10, 2 ); |
|
18
|
|
|
|
|
19
|
|
|
//change the image src attribute to data attributes if lazy loading is enabled |
|
20
|
|
|
add_filter( 'foogallery_attachment_html_image_attributes', array($this, 'change_src_attributes'), 99, 3); |
|
21
|
|
|
|
|
22
|
|
|
//add the lazy load attributes to the gallery container |
|
23
|
|
|
add_filter( 'foogallery_build_container_attributes', array( $this, 'add_lazyload_attributes' ), 10, 2 ); |
|
24
|
|
|
|
|
25
|
|
|
//add the appropriate lazy load class |
|
26
|
|
|
add_filter( 'foogallery_build_class_attribute', array( $this, 'add_lazyload_class' ), 10, 2 ); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Add lazy load fields to the gallery template |
|
31
|
|
|
* |
|
32
|
|
|
* @uses "foogallery_override_gallery_template_fields" |
|
33
|
|
|
* @param $fields |
|
34
|
|
|
* @param $template |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
function add_lazyload_fields( $fields, $template ) { |
|
|
|
|
|
|
39
|
|
|
if ( $template && array_key_exists( 'lazyload_support', $template ) && true === $template['lazyload_support'] ) { |
|
40
|
|
|
$fields[] = array( |
|
41
|
|
|
'id' => 'lazyload', |
|
42
|
|
|
'title' => __( 'Lazy Loading', 'foogallery' ), |
|
43
|
|
|
'desc' => __( 'Lazy loading means images in your gallery are only loaded when they are visible in browser. This significantly improves page loading times.', 'foogallery' ), |
|
44
|
|
|
'section' => __( 'Gallery Settings', 'foogallery' ), |
|
45
|
|
|
'type' => 'radio', |
|
46
|
|
|
'choices' => array( |
|
47
|
|
|
'yes' => __( 'Enable Lazy Loading', 'foogallery' ), |
|
48
|
|
|
'no' => __( 'Disabled', 'foogallery' ) |
|
49
|
|
|
), |
|
50
|
|
|
'spacer' => '<span class="spacer"></span>' |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $fields; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Determine if the gallery has lazy loading enabled |
|
59
|
|
|
* |
|
60
|
|
|
* @param $foogallery |
|
61
|
|
|
* @param $post |
|
62
|
|
|
*/ |
|
63
|
|
|
function determine_lazyload( $foogallery, $post ) { |
|
|
|
|
|
|
64
|
|
|
$gallery_template = $foogallery->gallery_template; |
|
65
|
|
|
|
|
66
|
|
|
$setting_key = "{$gallery_template}_lazyload"; |
|
67
|
|
|
|
|
68
|
|
|
if ( 'yes' === $foogallery->get_meta( $setting_key, 'yes' ) ) { |
|
69
|
|
|
$foogallery->lazyload = true; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $attr |
|
75
|
|
|
* @param array $args |
|
76
|
|
|
* @param FooGalleryAttachment $attachment |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
function change_src_attributes($attr, $args, $attachment) { |
|
|
|
|
|
|
80
|
|
|
global $current_foogallery; |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
if ( isset( $current_foogallery->lazyload) && true === $current_foogallery->lazyload) { |
|
83
|
|
|
|
|
84
|
|
|
if ( isset( $attr['src'] ) ) { |
|
85
|
|
|
//rename src => data-src |
|
86
|
|
|
$src = $attr['src']; |
|
87
|
|
|
unset( $attr['src'] ); |
|
88
|
|
|
$attr['data-src'] = $src; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
if ( isset( $attr['srcset'] ) ) { |
|
92
|
|
|
//rename srcset => data-srcset |
|
93
|
|
|
$src = $attr['srcset']; |
|
94
|
|
|
unset( $attr['srcset'] ); |
|
95
|
|
|
$attr['data-srcset'] = $src; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $attr; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add the required lazy load attributes onto the gallery container div |
|
105
|
|
|
* |
|
106
|
|
|
* @param $attributes array |
|
107
|
|
|
* @param $gallery FooGallery |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
function add_lazyload_attributes($attributes, $gallery) { |
|
|
|
|
|
|
112
|
|
|
if ( isset( $gallery->lazyload) && true === $gallery->lazyload) { |
|
113
|
|
|
$attributes['data-loader-options'] = '{\'lazy\':true}'; |
|
114
|
|
|
} |
|
115
|
|
|
return $attributes; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Add the required lazy load class to the gallery |
|
120
|
|
|
* |
|
121
|
|
|
* @param $classes array |
|
122
|
|
|
* @param $gallery FooGallery |
|
123
|
|
|
* |
|
124
|
|
|
* @return array |
|
125
|
|
|
*/ |
|
126
|
|
|
function add_lazyload_class($classes, $gallery) { |
|
|
|
|
|
|
127
|
|
|
if ( isset( $gallery->lazyload) && true === $gallery->lazyload) { |
|
128
|
|
|
$classes[] = apply_filters( 'foogallery_lazyload_class', 'loaded-fade-in' ); |
|
129
|
|
|
} |
|
130
|
|
|
return $classes; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
} |
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.