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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
<?php /** * Class used to handle lazy loading for gallery templates * Date: 20/03/2017 */ if ( ! class_exists( 'FooGallery_LazyLoad' ) ) {
class FooGallery_LazyLoad {
function __construct() { //determine lazy loading for the gallery once up front before the template is loaded add_action('foogallery_located_template', array($this, 'determine_lazyloading_for_gallery'));
//change the image src attribute to data attributes if lazy loading is enabled add_filter('foogallery_attachment_html_image_attributes', array($this, 'change_src_attributes'), 99, 3);
//add the lazy load attributes to the gallery container add_filter('foogallery_build_container_data_options', array($this, 'add_lazyload_options'), 10, 3);
//add common fields to the templates that support it add_filter('foogallery_override_gallery_template_fields', array($this, 'add_lazyload_field'), 100, 2);
//build up preview arguments add_filter( 'foogallery_preview_arguments', array( $this, 'preview_arguments' ), 10, 3 );
//add some settings to allow forcing of the lazy loading to be disabled add_filter( 'foogallery_admin_settings_override', array( $this, 'add_settings' ) ); }
/** * Determine all the lazu loading variables that can be set on a gallery * @param $foogallery */ function determine_lazyloading_for_gallery($foogallery) { global $current_foogallery; global $current_foogallery_template;
if ($current_foogallery !== null) { //make sure we only do this once for better performance if (!isset($current_foogallery->lazyload_support)) {
//load the gallery template $template_info = foogallery_get_gallery_template($current_foogallery_template);
//check if the template supports lazy loading $lazyloading_support = isset($template_info['lazyload_support']) && true === $template_info['lazyload_support'];
//set if lazy loading is supported for the gallery $current_foogallery->lazyload_support = apply_filters('foogallery_lazy_load', $lazyloading_support, $current_foogallery, $current_foogallery_template);
//set if lazy loading is enabled for the gallery $lazyloading_default = ''; $lazyloading_enabled = foogallery_gallery_template_setting('lazyload', $lazyloading_default) === ''; $current_foogallery->lazyload_enabled = $lazyloading_enabled;
//set if lazy loading is forced to disabled for all galleries $lazyloading_forced_disabled = foogallery_get_setting('disable_lazy_loading') === 'on'; $current_foogallery->lazyload_forced_disabled = $lazyloading_forced_disabled;
//check if we are inside a feed if ( is_feed() ) { $current_foogallery->is_feed = true; } } } }
/** * @param array $attr * @param array $args * @param FooGalleryAttachment $attachment * @return mixed */ function change_src_attributes($attr, $args, $attachment) { global $current_foogallery;
if ($current_foogallery !== null) {
//check if we inside a feed and exit early if ( isset( $current_foogallery->is_feed ) && true === $current_foogallery->is_feed ) { return $attr; }
if (isset($current_foogallery->lazyload_support) && true === $current_foogallery->lazyload_support) { if (isset($attr['src'])) { //rename src => data-src $src = $attr['src']; unset($attr['src']); $attr['data-src-fg'] = $src; }
if (isset($attr['srcset'])) { //rename srcset => data-srcset $src = $attr['srcset']; unset($attr['srcset']); $attr['data-srcset-fg'] = $src; }
//do not add the src attribute if ( false ) { //set the src to a 1x1 transparent gif $attr['src'] = FOOGALLERY_URL . 'assets/1x1.gif'; } } }
return $attr; }
/** * Add the required lazy load options if needed * * @param $attributes array * @param $gallery FooGallery * * @return array */ function add_lazyload_options($options, $gallery, $attributes) { if ( isset( $gallery->lazyload_support ) && true === $gallery->lazyload_support ) { $options['lazy'] = $gallery->lazyload_enabled && !$gallery->lazyload_forced_disabled; $options['src'] = 'data-src-fg'; $options['srcset'] = 'data-srcset-fg'; } return $options; }
/** * Add lazyload field to the gallery template if supported * * @param $fields * @param $template * * @return array */ function add_lazyload_field($fields, $template) { //check if the template supports lazy loading if ( $template && array_key_exists( 'lazyload_support', $template ) && true === $template['lazyload_support'] ) {
$fields[] = array( 'id' => 'lazyload', 'title' => __( 'Lazy Loading', 'foogallery' ), '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' ), 'section' => __( 'Advanced', 'foogallery' ), 'type' => 'radio', 'spacer' => '<span class="spacer"></span>', 'default' => '', 'choices' => array( '' => __( 'Enable Lazy Loading', 'foogallery' ), 'disabled' => __( 'Disable Lazy Loading', 'foogallery' ), ), 'row_data' => array( 'data-foogallery-change-selector' => 'input:radio', 'data-foogallery-preview' => 'shortcode' ) ); }
return $fields; }
/** * Build up a arguments used in the preview of the gallery * @param $args * @param $post_data * @param $template * * @return mixed */ function preview_arguments( $args, $post_data, $template ) { if ( isset( $post_data[FOOGALLERY_META_SETTINGS][$template . '_lazyload'] ) ) { $args['lazyload'] = $post_data[FOOGALLERY_META_SETTINGS][$template . '_lazyload']; } return $args; }
/** * Add some global settings * @param $settings * * @return array */ function add_settings( $settings ) {
$lazy_settings[] = array( 'id' => 'disable_lazy_loading', 'title' => __( 'Disable Lazy Loading', 'foogallery' ), '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' ), 'type' => 'checkbox', 'tab' => 'general', 'section' => __( 'Lazy Loading', 'foogallery' ) );
$new_settings = array_merge( $lazy_settings, $settings['settings'] );
$settings['settings'] = $new_settings;
return $settings; } } }
|