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_data_options', array( $this, 'add_lazyload_options' ), 10, 3 ); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add lazy load fields to the gallery template |
28
|
|
|
* |
29
|
|
|
* @uses "foogallery_override_gallery_template_fields" |
30
|
|
|
* @param $fields |
31
|
|
|
* @param $template |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
function add_lazyload_fields( $fields, $template ) { |
|
|
|
|
36
|
|
|
if ( $template && array_key_exists( 'lazyload_support', $template ) && true === $template['lazyload_support'] ) { |
37
|
|
|
$fields[] = array( |
38
|
|
|
'id' => 'lazyload', |
39
|
|
|
'title' => __( 'Lazy Loading', 'foogallery' ), |
40
|
|
|
'desc' => __( 'Lazy loading means images in your gallery are only loaded when they become visible in the browser. This significantly improves page loading times.', 'foogallery' ), |
41
|
|
|
'section' => __( 'Thumbnails', 'foogallery' ), |
42
|
|
|
'spacer' => '<span class="spacer"></span>', |
43
|
|
|
'type' => 'radio', |
44
|
|
|
'choices' => array( |
45
|
|
|
'yes' => __( 'Enabled', 'foogallery' ), |
46
|
|
|
'no' => __( 'Disabled', 'foogallery' ) |
47
|
|
|
) |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $fields; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Determine if the gallery has lazy loading enabled |
56
|
|
|
* |
57
|
|
|
* @param $foogallery |
58
|
|
|
* @param $post |
59
|
|
|
*/ |
60
|
|
|
function determine_lazyload( $foogallery, $post ) { |
|
|
|
|
61
|
|
|
//always enable lazyload by default |
62
|
|
|
$lazy_load = true; |
63
|
|
|
|
64
|
|
|
if ( true === foogallery_get_setting( 'disable_lazy_load', false ) ) { |
65
|
|
|
$lazy_load = false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$foogallery->lazyload = apply_filters( 'foogallery_lazy_load', $lazy_load, $foogallery ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $attr |
73
|
|
|
* @param array $args |
74
|
|
|
* @param FooGalleryAttachment $attachment |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
|
|
function change_src_attributes($attr, $args, $attachment) { |
|
|
|
|
78
|
|
|
global $current_foogallery; |
|
|
|
|
79
|
|
|
global $current_foogallery_template; |
80
|
|
|
|
81
|
|
|
$lazyload_support = isset($current_foogallery_template['lazyload_support']) && |
82
|
|
|
true === $current_foogallery_template['lazyload_support']; |
83
|
|
|
|
84
|
|
|
if ( $lazyload_support && |
85
|
|
|
isset( $current_foogallery->lazyload) && true === $current_foogallery->lazyload) { |
86
|
|
|
|
87
|
|
|
if ( isset( $attr['src'] ) ) { |
88
|
|
|
//rename src => data-src |
89
|
|
|
$src = $attr['src']; |
90
|
|
|
unset( $attr['src'] ); |
91
|
|
|
$attr['data-src'] = $src; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ( isset( $attr['srcset'] ) ) { |
95
|
|
|
//rename srcset => data-srcset |
96
|
|
|
$src = $attr['srcset']; |
97
|
|
|
unset( $attr['srcset'] ); |
98
|
|
|
$attr['data-srcset'] = $src; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $attr; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Add the required lazy load options if needed |
108
|
|
|
* |
109
|
|
|
* @param $attributes array |
110
|
|
|
* @param $gallery FooGallery |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
function add_lazyload_options($options, $gallery, $attributes) { |
|
|
|
|
115
|
|
|
if ( isset( $gallery->lazyload) && true === $gallery->lazyload) { |
116
|
|
|
$options['lazy'] = true; |
117
|
|
|
} |
118
|
|
|
return $options; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
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.