Passed
Push — master ( 44554e...f50af0 )
by Warwick
02:48
created
includes/classes/class-lsx-lazy-load-images.php 1 patch
Spacing   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -6,11 +6,11 @@  discard block
 block discarded – undo
6 6
  * @subpackage lazyload
7 7
  */
8 8
 
9
-if ( ! defined( 'ABSPATH' ) ) {
9
+if ( ! defined('ABSPATH')) {
10 10
 	exit;
11 11
 }
12 12
 
13
-if ( ! class_exists( 'LSX_Lazy_Load_Images' ) ) :
13
+if ( ! class_exists('LSX_Lazy_Load_Images')) :
14 14
 
15 15
 	/*
16 16
 	 * LSX Lazy Load Images Class
@@ -39,143 +39,143 @@  discard block
 block discarded – undo
39 39
 		protected static $noscripts   = array();
40 40
 
41 41
 		static function init() {
42
-			if ( is_admin() ) {
42
+			if (is_admin()) {
43 43
 				return;
44 44
 			}
45 45
 
46
-			if ( get_theme_mod( 'lsx_lazyload_status', '1' ) === false ) {
46
+			if (get_theme_mod('lsx_lazyload_status', '1') === false) {
47 47
 				self::$enabled = false;
48 48
 				return;
49 49
 			}
50 50
 
51
-			if ( ! apply_filters( 'lsx_lazyload_is_enabled', true ) ) {
51
+			if ( ! apply_filters('lsx_lazyload_is_enabled', true)) {
52 52
 				self::$enabled = false;
53 53
 				return;
54 54
 			}
55 55
 
56
-			add_action( 'wp_enqueue_scripts', array( 'LSX_Lazy_Load_Images', 'add_scripts' ) );
57
-			add_action( 'wp_head', array( 'LSX_Lazy_Load_Images', 'setup_filters' ), 9999 );
58
-			add_filter( 'wp_kses_allowed_html', array( 'LSX_Lazy_Load_Images', 'kses_allowed_html' ), 10, 2 );
56
+			add_action('wp_enqueue_scripts', array('LSX_Lazy_Load_Images', 'add_scripts'));
57
+			add_action('wp_head', array('LSX_Lazy_Load_Images', 'setup_filters'), 9999);
58
+			add_filter('wp_kses_allowed_html', array('LSX_Lazy_Load_Images', 'kses_allowed_html'), 10, 2);
59 59
 		}
60 60
 
61 61
 		static function setup_filters() {
62 62
 			// WordPress.
63
-			add_filter( 'the_content', array( 'LSX_Lazy_Load_Images', 'filter_images' ), 200 );
64
-			add_filter( 'widget_text', array( 'LSX_Lazy_Load_Images', 'filter_images' ), 200 );
65
-			add_filter( 'post_thumbnail_html', array( 'LSX_Lazy_Load_Images', 'filter_images' ), 200 );
66
-			add_filter( 'get_avatar', array( 'LSX_Lazy_Load_Images', 'filter_images' ), 200 );
63
+			add_filter('the_content', array('LSX_Lazy_Load_Images', 'filter_images'), 200);
64
+			add_filter('widget_text', array('LSX_Lazy_Load_Images', 'filter_images'), 200);
65
+			add_filter('post_thumbnail_html', array('LSX_Lazy_Load_Images', 'filter_images'), 200);
66
+			add_filter('get_avatar', array('LSX_Lazy_Load_Images', 'filter_images'), 200);
67 67
 
68 68
 			// LSX.
69
-			add_filter( 'lsx_lazyload_filter_images', array( 'LSX_Lazy_Load_Images', 'filter_images' ), 200 );
69
+			add_filter('lsx_lazyload_filter_images', array('LSX_Lazy_Load_Images', 'filter_images'), 200);
70 70
 
71 71
 			// Envira Gallery.
72
-			add_filter( 'envira_gallery_output_image', array( 'LSX_Lazy_Load_Images', 'filter_images' ), 200 );
72
+			add_filter('envira_gallery_output_image', array('LSX_Lazy_Load_Images', 'filter_images'), 200);
73 73
 		}
74 74
 
75 75
 		static function add_scripts() {
76
-			wp_enqueue_script( 'lazysizes', get_template_directory_uri() . '/assets/js/vendor/lazysizes.min.js', array( 'jquery' ), LSX_VERSION, true );
76
+			wp_enqueue_script('lazysizes', get_template_directory_uri() . '/assets/js/vendor/lazysizes.min.js', array('jquery'), LSX_VERSION, true);
77 77
 			// Plugin that enables use lazysizes in brackground images
78 78
 			// wp_enqueue_script( 'lazysizes', get_template_directory_uri() . '/assets/js/vendor/ls.unveilhooks.min.js', array( 'jquery', 'lazysizes' ), LSX_VERSION, true );
79 79
 		}
80 80
 
81
-		static function filter_images( $content ) {
82
-			if ( ! self::is_enabled() ) {
81
+		static function filter_images($content) {
82
+			if ( ! self::is_enabled()) {
83 83
 				return $content;
84 84
 			}
85 85
 
86
-			$http_user_agent = sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) );
87
-			$http_user_agent = ! empty( $http_user_agent ) ? $http_user_agent : '';
86
+			$http_user_agent = sanitize_text_field(wp_unslash($_SERVER['HTTP_USER_AGENT']));
87
+			$http_user_agent = ! empty($http_user_agent) ? $http_user_agent : '';
88 88
 
89
-			if ( is_feed()
89
+			if (is_feed()
90 90
 				|| is_preview()
91
-				|| 1 === intval( get_query_var( 'print' ) )
92
-				|| 1 === intval( get_query_var( 'printpage' ) )
93
-				|| strpos( $http_user_agent, 'Opera Mini' ) !== false
91
+				|| 1 === intval(get_query_var('print'))
92
+				|| 1 === intval(get_query_var('printpage'))
93
+				|| strpos($http_user_agent, 'Opera Mini') !== false
94 94
 			) {
95 95
 				return $content;
96 96
 			}
97 97
 
98 98
 			$skip_images_regex = '/class=".*(lazyload|disable-lazyload).*"/';
99
-			$placeholder_image = apply_filters( 'lsx_lazyload_placeholder_image', get_template_directory_uri() . '/assets/images/empty.gif' );
99
+			$placeholder_image = apply_filters('lsx_lazyload_placeholder_image', get_template_directory_uri() . '/assets/images/empty.gif');
100 100
 
101 101
 			$matches = array();
102 102
 			$search  = array();
103 103
 			$replace = array();
104 104
 
105
-			$content = preg_replace_callback( '~<noscript.+?</noscript>~s', 'self::noscripts_remove', $content );
106
-			preg_match_all( '/^<img[^>]*>$/', $content, $matches );
105
+			$content = preg_replace_callback('~<noscript.+?</noscript>~s', 'self::noscripts_remove', $content);
106
+			preg_match_all('/^<img[^>]*>$/', $content, $matches);
107 107
 
108
-			foreach ( $matches[0] as $img_html ) {
109
-				if ( ! ( preg_match( $skip_images_regex, $img_html ) ) ) {
108
+			foreach ($matches[0] as $img_html) {
109
+				if ( ! (preg_match($skip_images_regex, $img_html))) {
110 110
 					$add_class = false;
111 111
 
112
-					if ( ! preg_match( '/^src=[\'"]([^\'"]+)[\'"]$/', $img_html ) && preg_match( '/^srcset=[\'"]([^\'"]+)[\'"]$/', $img_html ) ) {
113
-						$replace_html = preg_replace( '/<img(.*?)srcset=/i', '<img$1srcset="' . $placeholder_image . '" data-srcset=', $img_html );
112
+					if ( ! preg_match('/^src=[\'"]([^\'"]+)[\'"]$/', $img_html) && preg_match('/^srcset=[\'"]([^\'"]+)[\'"]$/', $img_html)) {
113
+						$replace_html = preg_replace('/<img(.*?)srcset=/i', '<img$1srcset="' . $placeholder_image . '" data-srcset=', $img_html);
114 114
 
115
-						if ( preg_match( '/^sizes=[\'"]([^\'"]+)[\'"]$/', $img_html ) ) {
116
-							$replace_html = preg_replace( '/sizes=/i', 'data-sizes=', $replace_html );
115
+						if (preg_match('/^sizes=[\'"]([^\'"]+)[\'"]$/', $img_html)) {
116
+							$replace_html = preg_replace('/sizes=/i', 'data-sizes=', $replace_html);
117 117
 						} else {
118
-							$replace_html = preg_replace( '/data-srcset=/i', 'data-sizes="auto" data-srcset=', $replace_html );
118
+							$replace_html = preg_replace('/data-srcset=/i', 'data-sizes="auto" data-srcset=', $replace_html);
119 119
 						}
120 120
 
121 121
 						$add_class = true;
122
-					} elseif ( preg_match( '/^src=[\'"]([^\'"]+)[\'"]$/', $img_html ) ) {
123
-						$replace_html = preg_replace( '/<img(.*?)src=/i', '<img$1src="' . $placeholder_image . '" data-src=', $img_html );
122
+					} elseif (preg_match('/^src=[\'"]([^\'"]+)[\'"]$/', $img_html)) {
123
+						$replace_html = preg_replace('/<img(.*?)src=/i', '<img$1src="' . $placeholder_image . '" data-src=', $img_html);
124 124
 
125
-						if ( preg_match( '/^srcset=[\'"]([^\'"]+)[\'"]$/', $img_html ) ) {
126
-							if ( preg_match( '/^sizes=[\'"]([^\'"]+)[\'"]$/', $img_html ) ) {
127
-								$replace_html = preg_replace( '/srcset=/i', 'data-srcset=', $replace_html );
128
-								$replace_html = preg_replace( '/sizes=/i', 'data-sizes=', $replace_html );
125
+						if (preg_match('/^srcset=[\'"]([^\'"]+)[\'"]$/', $img_html)) {
126
+							if (preg_match('/^sizes=[\'"]([^\'"]+)[\'"]$/', $img_html)) {
127
+								$replace_html = preg_replace('/srcset=/i', 'data-srcset=', $replace_html);
128
+								$replace_html = preg_replace('/sizes=/i', 'data-sizes=', $replace_html);
129 129
 							} else {
130
-								$replace_html = preg_replace( '/srcset=/i', 'data-sizes="auto" data-srcset=', $replace_html );
130
+								$replace_html = preg_replace('/srcset=/i', 'data-sizes="auto" data-srcset=', $replace_html);
131 131
 							}
132 132
 						}
133 133
 
134 134
 						$add_class = true;
135 135
 					}
136 136
 
137
-					if ( $add_class ) {
138
-						$replace_html  = self::add_class( $replace_html, 'lazyload' );
137
+					if ($add_class) {
138
+						$replace_html  = self::add_class($replace_html, 'lazyload');
139 139
 						$replace_html .= '<noscript>' . $img_html . '</noscript>';
140 140
 
141
-						array_push( $search, $img_html );
142
-						array_push( $replace, $replace_html );
141
+						array_push($search, $img_html);
142
+						array_push($replace, $replace_html);
143 143
 					}
144 144
 				}
145 145
 			}
146 146
 
147
-			$content = str_replace( $search, $replace, $content );
148
-			$content = preg_replace_callback( '~' . chr( 20 ) . '([0-9]+)' . chr( 20 ) . '~', 'self::noscripts_restore', $content );
147
+			$content = str_replace($search, $replace, $content);
148
+			$content = preg_replace_callback('~' . chr(20) . '([0-9]+)' . chr(20) . '~', 'self::noscripts_restore', $content);
149 149
 			return $content;
150 150
 		}
151 151
 
152
-		static function noscripts_remove( $match ) {
152
+		static function noscripts_remove($match) {
153 153
 			self::$noscript_id++;
154
-			self::$noscripts[ self::$noscript_id ] = $match[0];
155
-			return chr( 20 ) . self::$noscript_id . chr( 20 );
154
+			self::$noscripts[self::$noscript_id] = $match[0];
155
+			return chr(20) . self::$noscript_id . chr(20);
156 156
 		}
157 157
 
158
-		static function noscripts_restore( $match ) {
159
-			return self::$noscripts[ (int) $match[1] ];
158
+		static function noscripts_restore($match) {
159
+			return self::$noscripts[(int) $match[1]];
160 160
 		}
161 161
 
162
-		static function add_class( $html_string = '', $new_class ) {
162
+		static function add_class($html_string = '', $new_class) {
163 163
 			$pattern = '/^class=[\'"]([^\'"]*)[\'"]$/';
164 164
 
165
-			if ( preg_match( $pattern, $html_string, $matches ) ) {
166
-				$defined_classes = explode( ' ', $matches[1] );
165
+			if (preg_match($pattern, $html_string, $matches)) {
166
+				$defined_classes = explode(' ', $matches[1]);
167 167
 
168
-				if ( ! in_array( $new_class, $defined_classes ) ) {
168
+				if ( ! in_array($new_class, $defined_classes)) {
169 169
 					$defined_classes[] = $new_class;
170 170
 
171 171
 					$html_string = str_replace(
172 172
 						$matches[0],
173
-						sprintf( 'class="%s"', implode( ' ', $defined_classes ) ),
173
+						sprintf('class="%s"', implode(' ', $defined_classes)),
174 174
 						$html_string
175 175
 					);
176 176
 				}
177 177
 			} else {
178
-				$html_string = preg_replace( '/(\<.+\s)/', sprintf( '$1class="%s" ', $new_class ), $html_string );
178
+				$html_string = preg_replace('/(\<.+\s)/', sprintf('$1class="%s" ', $new_class), $html_string);
179 179
 			}
180 180
 
181 181
 			return $html_string;
@@ -185,7 +185,7 @@  discard block
 block discarded – undo
185 185
 			return self::$enabled;
186 186
 		}
187 187
 
188
-		static function kses_allowed_html( $allowedtags, $context ) {
188
+		static function kses_allowed_html($allowedtags, $context) {
189 189
 			$allowedtags['noscript'] = array();
190 190
 
191 191
 			$allowedtags['img']['data-src']    = true;
@@ -198,4 +198,4 @@  discard block
 block discarded – undo
198 198
 
199 199
 endif;
200 200
 
201
-add_action( 'init', array( 'LSX_Lazy_Load_Images', 'init' ) );
201
+add_action('init', array('LSX_Lazy_Load_Images', 'init'));
Please login to merge, or discard this patch.