|
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
|
|
|
|
|
11
|
|
|
function __construct() |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
//determine lazy loading for the gallery once up front before the template is loaded |
|
14
|
|
|
add_action('foogallery_located_template', array($this, 'determine_lazyloading_for_gallery')); |
|
15
|
|
|
|
|
16
|
|
|
//change the image src attribute to data attributes if lazy loading is enabled |
|
17
|
|
|
add_filter('foogallery_attachment_html_image_attributes', array($this, 'change_src_attributes'), 99, 3); |
|
18
|
|
|
|
|
19
|
|
|
//add the lazy load attributes to the gallery container |
|
20
|
|
|
add_filter('foogallery_build_container_data_options', array($this, 'add_lazyload_options'), 10, 3); |
|
21
|
|
|
|
|
22
|
|
|
//add common fields to the templates that support it |
|
23
|
|
|
add_filter('foogallery_override_gallery_template_fields', array($this, 'add_lazyload_field'), 100, 2); |
|
24
|
|
|
|
|
25
|
|
|
//build up preview arguments |
|
26
|
|
|
add_filter( 'foogallery_preview_arguments', array( $this, 'preview_arguments' ), 10, 3 ); |
|
27
|
|
|
|
|
28
|
|
|
//add some settings to allow forcing of the lazy loading to be disabled |
|
29
|
|
|
add_filter( 'foogallery_admin_settings_override', array( $this, 'add_settings' ) ); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Determine all the lazu loading variables that can be set on a gallery |
|
34
|
|
|
* @param $foogallery |
|
35
|
|
|
*/ |
|
36
|
|
|
function determine_lazyloading_for_gallery($foogallery) { |
|
|
|
|
|
|
37
|
|
|
global $current_foogallery; |
|
|
|
|
|
|
38
|
|
|
global $current_foogallery_template; |
|
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
if ($current_foogallery !== null) { |
|
41
|
|
|
//make sure we only do this once for better performance |
|
42
|
|
|
if (!isset($current_foogallery->lazyload_support)) { |
|
43
|
|
|
|
|
44
|
|
|
//load the gallery template |
|
45
|
|
|
$template_info = foogallery_get_gallery_template($current_foogallery_template); |
|
46
|
|
|
|
|
47
|
|
|
//check if the template supports lazy loading |
|
48
|
|
|
$lazyloading_support = isset($template_info['lazyload_support']) && |
|
49
|
|
|
true === $template_info['lazyload_support']; |
|
50
|
|
|
|
|
51
|
|
|
//set if lazy loading is supported for the gallery |
|
52
|
|
|
$current_foogallery->lazyload_support = apply_filters('foogallery_lazy_load', $lazyloading_support, $current_foogallery, $current_foogallery_template); |
|
53
|
|
|
|
|
54
|
|
|
//set if lazy loading is enabled for the gallery |
|
55
|
|
|
$lazyloading_default = ''; |
|
56
|
|
|
$lazyloading_enabled = foogallery_gallery_template_setting('lazyload', $lazyloading_default) === ''; |
|
57
|
|
|
$current_foogallery->lazyload_enabled = $lazyloading_enabled; |
|
58
|
|
|
|
|
59
|
|
|
//set if lazy loading is forced to disabled for all galleries |
|
60
|
|
|
$lazyloading_forced_disabled = foogallery_get_setting('disable_lazy_loading') === 'on'; |
|
61
|
|
|
$current_foogallery->lazyload_forced_disabled = $lazyloading_forced_disabled; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $attr |
|
68
|
|
|
* @param array $args |
|
69
|
|
|
* @param FooGalleryAttachment $attachment |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
function change_src_attributes($attr, $args, $attachment) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
global $current_foogallery; |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if ($current_foogallery !== null) { |
|
77
|
|
|
|
|
78
|
|
|
if (isset($current_foogallery->lazyload_support) && true === $current_foogallery->lazyload_support) { |
|
79
|
|
|
if (isset($attr['src'])) { |
|
80
|
|
|
//rename src => data-src |
|
81
|
|
|
$src = $attr['src']; |
|
82
|
|
|
unset($attr['src']); |
|
83
|
|
|
$attr['data-src-fg'] = $src; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (isset($attr['srcset'])) { |
|
87
|
|
|
//rename srcset => data-srcset |
|
88
|
|
|
$src = $attr['srcset']; |
|
89
|
|
|
unset($attr['srcset']); |
|
90
|
|
|
$attr['data-srcset-fg'] = $src; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $attr; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Add the required lazy load options if needed |
|
101
|
|
|
* |
|
102
|
|
|
* @param $attributes array |
|
103
|
|
|
* @param $gallery FooGallery |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
function add_lazyload_options($options, $gallery, $attributes) |
|
|
|
|
|
|
108
|
|
|
{ |
|
109
|
|
|
if ( isset( $gallery->lazyload_support ) && true === $gallery->lazyload_support ) { |
|
110
|
|
|
$options['lazy'] = $gallery->lazyload_enabled && !$gallery->lazyload_forced_disabled; |
|
111
|
|
|
$options['src'] = 'data-src-fg'; |
|
112
|
|
|
$options['srcset'] = 'data-srcset-fg'; |
|
113
|
|
|
} |
|
114
|
|
|
return $options; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Add lazyload field to the gallery template if supported |
|
119
|
|
|
* |
|
120
|
|
|
* @param $fields |
|
121
|
|
|
* @param $template |
|
122
|
|
|
* |
|
123
|
|
|
* @return array |
|
124
|
|
|
*/ |
|
125
|
|
|
function add_lazyload_field($fields, $template) |
|
|
|
|
|
|
126
|
|
|
{ |
|
127
|
|
|
//check if the template supports lazy loading |
|
128
|
|
|
if ( $template && array_key_exists( 'lazyload_support', $template ) && true === $template['lazyload_support'] ) { |
|
129
|
|
|
|
|
130
|
|
|
$fields[] = array( |
|
131
|
|
|
'id' => 'lazyload', |
|
132
|
|
|
'title' => __( 'Lazy Loading', 'foogallery' ), |
|
133
|
|
|
'desc' => __( 'If you choose to disable lazy loading, then all thumbnails will be loaded at once. This means you will lose the performance improvements that lazy loading gives you.', 'foogallery' ), |
|
134
|
|
|
'section' => __( 'Advanced', 'foogallery' ), |
|
135
|
|
|
'type' => 'radio', |
|
136
|
|
|
'spacer' => '<span class="spacer"></span>', |
|
137
|
|
|
'default' => '', |
|
138
|
|
|
'choices' => array( |
|
139
|
|
|
'' => __( 'Enable Lazy Loading', 'foogallery' ), |
|
140
|
|
|
'disabled' => __( 'Disable Lazy Loading', 'foogallery' ), |
|
141
|
|
|
), |
|
142
|
|
|
'row_data' => array( |
|
143
|
|
|
'data-foogallery-change-selector' => 'input:radio', |
|
144
|
|
|
'data-foogallery-preview' => 'shortcode' |
|
145
|
|
|
) |
|
146
|
|
|
); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $fields; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Build up a arguments used in the preview of the gallery |
|
154
|
|
|
* @param $args |
|
155
|
|
|
* @param $post_data |
|
156
|
|
|
* @param $template |
|
157
|
|
|
* |
|
158
|
|
|
* @return mixed |
|
159
|
|
|
*/ |
|
160
|
|
|
function preview_arguments( $args, $post_data, $template ) { |
|
|
|
|
|
|
161
|
|
|
if ( isset( $post_data[FOOGALLERY_META_SETTINGS][$template . '_lazyload'] ) ) { |
|
162
|
|
|
$args['lazyload'] = $post_data[FOOGALLERY_META_SETTINGS][$template . '_lazyload']; |
|
163
|
|
|
} |
|
164
|
|
|
return $args; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Add some global settings |
|
170
|
|
|
* @param $settings |
|
171
|
|
|
* |
|
172
|
|
|
* @return array |
|
173
|
|
|
*/ |
|
174
|
|
|
function add_settings( $settings ) { |
|
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
$lazy_settings[] = array( |
|
|
|
|
|
|
177
|
|
|
'id' => 'disable_lazy_loading', |
|
178
|
|
|
'title' => __( 'Disable Lazy Loading', 'foogallery' ), |
|
179
|
|
|
'desc' => __( 'This will disable lazy loading for ALL galleries. This is not recommended, but is sometimes needed when there are problems with the galleries displaying on some installs.', 'foogallery' ), |
|
180
|
|
|
'type' => 'checkbox', |
|
181
|
|
|
'tab' => 'general', |
|
182
|
|
|
'section' => __( 'Lazy Loading', 'foogallery' ) |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
$new_settings = array_merge( $lazy_settings, $settings['settings'] ); |
|
186
|
|
|
|
|
187
|
|
|
$settings['settings'] = $new_settings; |
|
188
|
|
|
|
|
189
|
|
|
return $settings; |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
} |
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.