PluginProbe ʕ •ᴥ•ʔ
Gallery : FooGallery / 3.3.3
Gallery : FooGallery v3.3.3
3.3.2 3.3.3 3.3.0 3.1.31 3.1.32 3.1.34 3.1.36 3.2.0 3.2.6 trunk 1.10.3 2.0.24 2.1.34 2.2.44 2.3.3 2.4.32 3.0.6 3.1.11 3.1.12 3.1.13 3.1.20 3.1.25 3.1.26 3.1.26.1 3.1.26.2
foogallery / includes / class-foogallery-lazyload.php
foogallery / includes Last commit date
abilities 1 day ago admin 1 day ago compatibility 1 day ago extensions 1 day ago foopluginbase 1 day ago public 1 day ago thumbs 1 day ago asset-manifest.php 1 day ago class-attachment-filters.php 1 day ago class-command-palette.php 1 day ago class-foogallery-animated-gif-support.php 1 day ago class-foogallery-attachment-custom-class.php 1 day ago class-foogallery-attachment-filename.php 1 day ago class-foogallery-attachment-type.php 1 day ago class-foogallery-attachment.php 1 day ago class-foogallery-cache.php 1 day ago class-foogallery-common-fields.php 1 day ago class-foogallery-crop-position.php 1 day ago class-foogallery-datasource-media_library.php 1 day ago class-foogallery-debug.php 1 day ago class-foogallery-delayed-runtime-loader.php 1 day ago class-foogallery-extensions-compatibility.php 1 day ago class-foogallery-force-https.php 1 day ago class-foogallery-lazyload.php 1 day ago class-foogallery-license-constant-handler.php 1 day ago class-foogallery-lightbox.php 1 day ago class-foogallery-no-javascript-fallback.php 1 day ago class-foogallery-paging.php 1 day ago class-foogallery-password-protect.php 1 day ago class-foogallery-sitemaps.php 1 day ago class-foogallery-widget.php 1 day ago class-foogallery.php 1 day ago class-gallery-advanced-settings.php 1 day ago class-il8n.php 1 day ago class-override-thumbnail.php 1 day ago class-posttypes.php 1 day ago class-previews.php 1 day ago class-retina.php 1 day ago class-thumbnail-dimensions.php 1 day ago class-thumbnails.php 1 day ago class-version-check.php 1 day ago constants.php 1 day ago functions.php 1 day ago gallery-management-functions.php 1 day ago includes.php 1 day ago index.php 1 day ago render-functions.php 1 day ago
class-foogallery-lazyload.php
403 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Class used to handle lazy loading for gallery templates
9 * Date: 20/03/2017
10 */
11 if ( ! class_exists( 'FooGallery_LazyLoad' ) ) {
12
13 /**
14 * Class FooGallery_LazyLoad
15 */
16 class FooGallery_LazyLoad {
17
18 const MODE_SEO = '';
19 const MODE_LEGACY = 'legacy';
20
21 /**
22 * FooGallery_LazyLoad constructor.
23 */
24 public function __construct() {
25 // determine lazy loading for the gallery once up front before the template is loaded.
26 add_action( 'foogallery_located_template', array( $this, 'determine_lazyloading_for_gallery' ) );
27
28 // force lazy loading for stack album.
29 add_action( 'foogallery_located_album_template-stack', array( $this, 'force_lazyloading_for_galleries' ) );
30
31 // change the image src attribute to data attributes if lazy loading is enabled.
32 add_filter( 'foogallery_attachment_html_image_attributes', array( $this, 'change_src_attributes' ), 99, 3 );
33
34 // set the native image loading attribute for SEO friendly lazy loading.
35 add_filter( 'foogallery_attachment_html_image_loading_attribute', array( $this, 'image_loading_attribute' ), 10, 4 );
36
37 // add the lazy load attributes to the gallery container.
38 add_filter( 'foogallery_build_container_data_options', array( $this, 'add_lazyload_options' ), 10, 3 );
39
40 // add common fields to the templates that support it.
41 add_filter( 'foogallery_override_gallery_template_fields', array( $this, 'add_lazyload_field' ), 100, 2 );
42
43 // add some settings to allow forcing of the lazy loading to be disabled.
44 add_filter( 'foogallery_admin_settings_override', array( $this, 'add_settings' ) );
45
46 add_filter( 'foogallery_attachment_html_item_classes', array( $this, 'add_item_classes_for_lazy_loading' ), 10, 3 );
47 }
48
49 /**
50 * Add the classes needed for lazy loading onto the items
51 *
52 * @param array $classes The array of classes to add to the item.
53 * @param FooGalleryAttachment $foogallery_attachment The current attachment we are working with.
54 * @param array $args Any extra args.
55 *
56 * @return array The array of classes to add to the item.
57 */
58 public function add_item_classes_for_lazy_loading( $classes, $foogallery_attachment, $args ) {
59 global $current_foogallery;
60 if ( $this->gallery_seo_lazyload_enabled( $current_foogallery ) ) {
61 $classes[] = 'fg-loading';
62 } elseif ( $this->gallery_lazyload_enabled( $current_foogallery ) ) {
63 $classes[] = 'fg-idle';
64 } else {
65 $classes[] = 'fg-loaded';
66 }
67
68 return $classes;
69 }
70
71 /**
72 * Force lazy loading for all galleries in the stack album
73 *
74 * @param FooGalleryAlbum $current_foogallery_album The current album that is being rendered.
75 */
76 public function force_lazyloading_for_galleries( $current_foogallery_album ) {
77 foreach ( $current_foogallery_album->galleries() as $gallery ) {
78 $gallery->lazyload_support = true;
79 $gallery->lazyload_enabled = true;
80 $gallery->lazyload_forced_disabled = false;
81 }
82 }
83
84 /**
85 * Determine all the lazy loading variables that can be set on a gallery
86 *
87 * @param $foogallery
88 */
89 function determine_lazyloading_for_gallery( $foogallery ) {
90 global $current_foogallery;
91 global $current_foogallery_template;
92
93 if ( $current_foogallery !== null ) {
94 //make sure we only do this once for better performance
95 if ( ! isset( $current_foogallery->lazyload_support ) ) {
96
97 //load the gallery template
98 $template_info = foogallery_get_gallery_template( $current_foogallery_template );
99
100 //check if the template supports lazy loading
101 $lazyloading_support = isset( $template_info['lazyload_support'] ) && true === $template_info['lazyload_support'];
102
103 //set if lazy loading is supported for the gallery
104 $current_foogallery->lazyload_support = apply_filters( 'foogallery_lazy_load', $lazyloading_support, $current_foogallery, $current_foogallery_template );
105
106 //set if lazy loading is enabled for the gallery
107 $lazyloading_default = '';
108 $lazyloading_enabled = foogallery_gallery_template_setting( 'lazyload', $lazyloading_default ) === '';
109 $current_foogallery->lazyload_enabled = $lazyloading_enabled;
110
111 //set if lazy loading is forced to disabled for all galleries
112 $lazyloading_forced_disabled = foogallery_get_setting( 'disable_lazy_loading' ) === 'on';
113 $current_foogallery->lazyload_forced_disabled = $lazyloading_forced_disabled;
114
115 //set the lazy loading mode for the gallery
116 $current_foogallery->lazyload_mode = $this->lazyload_mode();
117
118 //check if we are inside a feed
119 if ( is_feed() ) {
120 $current_foogallery->is_feed = true;
121 }
122 }
123 }
124 }
125
126 /**
127 * Change the src and srcset attributes for lazy loading
128 *
129 * @param array $attr
130 * @param array $args
131 * @param FooGalleryAttachment $attachment
132 *
133 * @return mixed
134 */
135 function change_src_attributes( $attr, $args, $attachment ) {
136 if ( $this->legacy_lazyload_enabled_for_current_context() ) {
137 if ( isset( $attr['src'] ) ) {
138 // rename src => data-src-fg.
139 $src = $attr['src'];
140 unset( $attr['src'] );
141 $attr['data-src-fg'] = $src;
142 }
143
144 if ( isset( $attr['srcset'] ) ) {
145 // rename srcset => data-srcset-fg.
146 $src = $attr['srcset'];
147 unset( $attr['srcset'] );
148 $attr['data-srcset-fg'] = $src;
149 }
150
151 // add a placeholder src.
152 if ( isset( $attr['width'] ) && isset( $attr['height'] ) ) {
153 // set the src to a transparent SVG that has the correct width and height.
154 $attr['src'] = foogallery_get_svg_placeholder_image( $attr['width'], $attr['height'] );
155 }
156 } else if ( $this->seo_lazyload_enabled_for_current_context() ) {
157 $attr['decoding'] = 'async';
158 }
159
160 return $attr;
161 }
162
163 /**
164 * Set the image loading attribute for SEO friendly lazy loading.
165 *
166 * @param string $loading The loading attribute.
167 * @param array $attr The image attributes.
168 * @param array $args Any extra args.
169 * @param FooGalleryAttachment $attachment The current attachment we are working with.
170 *
171 * @return string The loading attribute.
172 */
173 function image_loading_attribute( $loading, $attr, $args, $attachment ) {
174 if ( $this->seo_lazyload_enabled_for_current_context() ) {
175 return 'lazy';
176 }
177
178 return $loading;
179 }
180
181 /**
182 * Add the required lazy load options if needed
183 *
184 * @param $attributes array
185 * @param $gallery FooGallery
186 *
187 * @return array
188 */
189 function add_lazyload_options( $options, $gallery, $attributes ) {
190 $lazyload_enabled = $this->gallery_lazyload_enabled( $gallery );
191 $options['lazy'] = $lazyload_enabled;
192 if ( ! $lazyload_enabled || $this->gallery_seo_lazyload_enabled( $gallery ) ) {
193 $options['src'] = 'src';
194 $options['srcset'] = 'srcset';
195 }
196
197 return $options;
198 }
199
200 /**
201 * @param $gallery FooGallery
202 */
203 private function gallery_lazyload_enabled( $gallery ) {
204 if ( isset( $gallery->lazyload_support ) && true === $gallery->lazyload_support ) {
205 return $gallery->lazyload_enabled && ! $gallery->lazyload_forced_disabled;
206 }
207
208 return false;
209 }
210
211 /**
212 * Check if a gallery should use SEO friendly lazy loading.
213 *
214 * @param FooGallery $gallery The gallery to check.
215 *
216 * @return bool
217 */
218 private function gallery_seo_lazyload_enabled( $gallery ) {
219 return $this->gallery_lazyload_enabled( $gallery ) && self::MODE_SEO === $this->gallery_lazyload_mode( $gallery );
220 }
221
222 /**
223 * Check if a gallery should use legacy lazy loading.
224 *
225 * @param FooGallery $gallery The gallery to check.
226 *
227 * @return bool
228 */
229 private function gallery_legacy_lazyload_enabled( $gallery ) {
230 return $this->gallery_lazyload_enabled( $gallery ) && self::MODE_LEGACY === $this->gallery_lazyload_mode( $gallery );
231 }
232
233 /**
234 * Get the lazy loading mode for a gallery.
235 *
236 * @param FooGallery $gallery The gallery to check.
237 *
238 * @return string
239 */
240 private function gallery_lazyload_mode( $gallery ) {
241 if ( isset( $gallery->lazyload_mode ) ) {
242 return $this->sanitize_lazyload_mode( $gallery->lazyload_mode );
243 }
244
245 return $this->lazyload_mode();
246 }
247
248 /**
249 * Get the global lazy loading mode.
250 *
251 * @return string
252 */
253 private function lazyload_mode() {
254 return $this->sanitize_lazyload_mode( foogallery_get_setting( 'lazy_loading_mode', self::MODE_SEO ) );
255 }
256
257 /**
258 * Sanitize the lazy loading mode.
259 *
260 * @param string $mode The mode value.
261 *
262 * @return string
263 */
264 private function sanitize_lazyload_mode( $mode ) {
265 if ( self::MODE_LEGACY === $mode ) {
266 return self::MODE_LEGACY;
267 }
268
269 return self::MODE_SEO;
270 }
271
272 /**
273 * Check if the current rendering context should use SEO friendly lazy loading.
274 *
275 * @return bool
276 */
277 private function seo_lazyload_enabled_for_current_context() {
278 global $current_foogallery;
279 global $current_foogallery_album;
280
281 if ( null !== $current_foogallery ) {
282 if ( isset( $current_foogallery->is_feed ) && true === $current_foogallery->is_feed ) {
283 return false;
284 }
285
286 return $this->gallery_seo_lazyload_enabled( $current_foogallery );
287 }
288
289 if ( null !== $current_foogallery_album ) {
290 return foogallery_get_setting( 'disable_lazy_loading' ) !== 'on' && self::MODE_SEO === $this->lazyload_mode();
291 }
292
293 return false;
294 }
295
296 /**
297 * Check if the current rendering context should use legacy lazy loading.
298 *
299 * @return bool
300 */
301 private function legacy_lazyload_enabled_for_current_context() {
302 global $current_foogallery;
303 global $current_foogallery_album;
304
305 if ( null !== $current_foogallery ) {
306 if ( isset( $current_foogallery->is_feed ) && true === $current_foogallery->is_feed ) {
307 return false;
308 }
309
310 return $this->gallery_legacy_lazyload_enabled( $current_foogallery );
311 }
312
313 if ( null !== $current_foogallery_album ) {
314 return foogallery_get_setting( 'disable_lazy_loading' ) !== 'on' && self::MODE_LEGACY === $this->lazyload_mode();
315 }
316
317 return false;
318 }
319
320 /**
321 * Add lazyload field to the gallery template if supported
322 *
323 * @param $fields
324 * @param $template
325 *
326 * @return array
327 */
328 function add_lazyload_field( $fields, $template ) {
329 //check if the template supports lazy loading
330 if ( $template && array_key_exists( 'lazyload_support', $template ) && true === $template['lazyload_support'] ) {
331
332 $fields[] = array(
333 'id' => 'lazyload',
334 'title' => __( 'Lazy Loading', 'foogallery' ),
335 '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' ),
336 'section_id' => 'advanced',
337 'type' => 'radio',
338 'default' => '',
339 'choices' => array(
340 'disabled' => __( 'Disabled', 'foogallery' ),
341 '' => __( 'Enabled', 'foogallery' ),
342 ),
343 'row_data' => array(
344 'data-foogallery-change-selector' => 'input:radio',
345 'data-foogallery-preview' => 'shortcode',
346 ),
347 );
348 }
349
350 return $fields;
351 }
352
353 /**
354 * Add some global settings
355 *
356 * @param $settings
357 *
358 * @return array
359 */
360 function add_settings( $settings ) {
361
362 $lazy_loading_mode_setting = array(
363 'id' => 'lazy_loading_mode',
364 'title' => __( 'Lazy Loading Mode', 'foogallery' ),
365 'desc' => __( 'SEO Friendly outputs real image URLs and uses native browser lazy loading. Legacy uses JavaScript placeholder lazy loading for compatibility with older setups.', 'foogallery' ),
366 'type' => 'radio',
367 'default' => self::MODE_SEO,
368 'choices' => array(
369 self::MODE_SEO => __( 'SEO Friendly', 'foogallery' ),
370 self::MODE_LEGACY => __( 'Legacy', 'foogallery' ),
371 ),
372 'tab' => 'advanced',
373 'section' => __( 'Gallery Loading & Compatibility', 'foogallery' ),
374 );
375
376 $lazy_settings[] = array(
377 'id' => 'disable_lazy_loading',
378 'title' => __( 'Disable Lazy Loading', 'foogallery' ),
379 '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' ),
380 'type' => 'checkbox',
381 'tab' => 'general',
382 'section' => __( 'Performance', 'foogallery' ),
383 );
384
385 $new_settings = array_merge( $lazy_settings, $settings['settings'] );
386
387 $settings['settings'] = $new_settings;
388
389 foreach ( $settings['settings'] as $index => $setting ) {
390 if ( isset( $setting['tab'] ) && 'advanced' === $setting['tab'] ) {
391 array_splice( $settings['settings'], $index, 0, array( $lazy_loading_mode_setting ) );
392
393 return $settings;
394 }
395 }
396
397 $settings['settings'][] = $lazy_loading_mode_setting;
398
399 return $settings;
400 }
401 }
402 }
403