Completed
Push — master ( 4bf2d8...3cc0fa )
by
unknown
9s
created
includes/class-images-via-imgix.php 2 patches
Indentation   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -259,11 +259,11 @@
 block discarded – undo
259 259
 				}
260 260
 			}
261 261
 
262
-      if ( preg_match_all('/url\(([\s])?([\"|\'])?(.*?)([\"|\'])?([\s])?\)/i', $content, $matches ) ) {
263
-        foreach ( $matches[3] as $image_src ) {
264
-          $content = str_replace( $image_src, apply_filters( 'wp_get_attachment_url', $image_src, null ), $content );
265
-        }
266
-      }
262
+	  if ( preg_match_all('/url\(([\s])?([\"|\'])?(.*?)([\"|\'])?([\s])?\)/i', $content, $matches ) ) {
263
+		foreach ( $matches[3] as $image_src ) {
264
+		  $content = str_replace( $image_src, apply_filters( 'wp_get_attachment_url', $image_src, null ), $content );
265
+		}
266
+	  }
267 267
 		}
268 268
 		return $content;
269 269
 	}
Please login to merge, or discard this patch.
Spacing   +100 added lines, -100 removed lines patch added patch discarded remove patch
@@ -27,21 +27,21 @@  discard block
 block discarded – undo
27 27
 	 * ImagesViaImgix constructor.
28 28
 	 */
29 29
 	public function __construct() {
30
-		$this->options = get_option( 'imgix_settings', [] );
30
+		$this->options = get_option('imgix_settings', []);
31 31
 
32 32
 		// Change filter load order to ensure it loads after other CDN url transformations i.e. Amazon S3 which loads at position 99.
33
-		add_filter( 'wp_get_attachment_url', [ $this, 'replace_image_url' ], 100 );
34
-		add_filter( 'imgix/add-image-url', [ $this, 'replace_image_url' ] );
33
+		add_filter('wp_get_attachment_url', [$this, 'replace_image_url'], 100);
34
+		add_filter('imgix/add-image-url', [$this, 'replace_image_url']);
35 35
 
36
-		add_filter( 'image_downsize', [ $this, 'image_downsize' ], 10, 3 );
36
+		add_filter('image_downsize', [$this, 'image_downsize'], 10, 3);
37 37
 
38
-		add_filter( 'wp_calculate_image_srcset', [ $this, 'calculate_image_srcset' ], 10, 5 );
38
+		add_filter('wp_calculate_image_srcset', [$this, 'calculate_image_srcset'], 10, 5);
39 39
 
40
-		add_filter( 'the_content', [ $this, 'replace_images_in_content' ] );
41
-		add_action( 'wp_head', [ $this, 'prefetch_cdn' ], 1 );
40
+		add_filter('the_content', [$this, 'replace_images_in_content']);
41
+		add_action('wp_head', [$this, 'prefetch_cdn'], 1);
42 42
 
43
-		add_action( 'after_setup_theme', [ $this, 'buffer_start_for_retina' ] );
44
-		add_action( 'shutdown', [ $this, 'buffer_end_for_retina' ], 0 );
43
+		add_action('after_setup_theme', [$this, 'buffer_start_for_retina']);
44
+		add_action('shutdown', [$this, 'buffer_end_for_retina'], 0);
45 45
 	}
46 46
 
47 47
 	/**
@@ -50,7 +50,7 @@  discard block
 block discarded – undo
50 50
 	 * @return Images_Via_Imgix
51 51
 	 */
52 52
 	public static function instance() {
53
-		if ( ! isset( self::$instance ) ) {
53
+		if ( ! isset(self::$instance)) {
54 54
 			self::$instance = new self;
55 55
 		}
56 56
 
@@ -63,8 +63,8 @@  discard block
 block discarded – undo
63 63
 	 * @param string $key
64 64
 	 * @param mixed $value
65 65
 	 */
66
-	public function set_option( $key, $value ) {
67
-		$this->options[ $key ] = $value;
66
+	public function set_option($key, $value) {
67
+		$this->options[$key] = $value;
68 68
 	}
69 69
 
70 70
 	/**
@@ -74,8 +74,8 @@  discard block
 block discarded – undo
74 74
 	 * @param  mixed $default
75 75
 	 * @return mixed
76 76
 	 */
77
-	public function get_option( $key, $default = '' ) {
78
-		return array_key_exists( $key, $this->options ) ? $this->options[ $key ] : $default;
77
+	public function get_option($key, $default = '') {
78
+		return array_key_exists($key, $this->options) ? $this->options[$key] : $default;
79 79
 	}
80 80
 
81 81
 	/**
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 	 *
85 85
 	 * @param array $options
86 86
 	 */
87
-	public function set_options( $options ) {
87
+	public function set_options($options) {
88 88
 		$this->options = $options;
89 89
 	}
90 90
 
@@ -97,13 +97,13 @@  discard block
 block discarded – undo
97 97
 	 *
98 98
 	 * @return string Content with retina-enriched image tags.
99 99
 	 */
100
-	public function add_retina( $content ) {
100
+	public function add_retina($content) {
101 101
 		$pattern = '/<img((?![^>]+srcset )([^>]*)';
102 102
 		$pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i';
103 103
 		$repl    = '<img$2src="$3" srcset="${3}, ${3}&amp;dpr=2 2x, ${3}&amp;dpr=3 3x,"$4>';
104
-		$content = preg_replace( $pattern, $repl, $content );
104
+		$content = preg_replace($pattern, $repl, $content);
105 105
 
106
-		return preg_replace( $pattern, $repl, $content );
106
+		return preg_replace($pattern, $repl, $content);
107 107
 	}
108 108
 
109 109
 	/**
@@ -113,37 +113,37 @@  discard block
 block discarded – undo
113 113
 	 *
114 114
 	 * @return string
115 115
 	 */
116
-	public function replace_image_url( $url ) {
117
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
118
-			$parsed_url = parse_url( $url );
116
+	public function replace_image_url($url) {
117
+		if ( ! empty ($this->options['cdn_link'])) {
118
+			$parsed_url = parse_url($url);
119 119
 
120 120
 			//Check if image is hosted on current site url -OR- the CDN url specified. Using strpos because we're comparing the host to a full CDN url.
121 121
 			if (
122
-				isset( $parsed_url['host'], $parsed_url['path'] )
123
-				&& ($parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) || ( isset($this->options['external_cdn_link']) && ! empty($this->options['external_cdn_link']) && strpos( $this->options['external_cdn_link'], $parsed_url['host']) !== false ) )
124
-				&& preg_match( '/\.(jpg|jpeg|gif|png)$/i', $parsed_url['path'] )
122
+				isset($parsed_url['host'], $parsed_url['path'])
123
+				&& ($parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST) || (isset($this->options['external_cdn_link']) && ! empty($this->options['external_cdn_link']) && strpos($this->options['external_cdn_link'], $parsed_url['host']) !== false))
124
+				&& preg_match('/\.(jpg|jpeg|gif|png)$/i', $parsed_url['path'])
125 125
 			) {
126
-				$cdn = parse_url( $this->options['cdn_link'] );
126
+				$cdn = parse_url($this->options['cdn_link']);
127 127
 
128
-				foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) {
129
-					if ( isset( $cdn[ $url_part ] ) ) {
130
-						$parsed_url[ $url_part ] = $cdn[ $url_part ];
128
+				foreach (['scheme', 'host', 'port'] as $url_part) {
129
+					if (isset($cdn[$url_part])) {
130
+						$parsed_url[$url_part] = $cdn[$url_part];
131 131
 					} else {
132
-						unset( $parsed_url[ $url_part ] );
132
+						unset($parsed_url[$url_part]);
133 133
 					}
134 134
 				}
135 135
 
136
-				if ( ! empty( $this->options['external_cdn_link'] ) ) {
137
-					$cdn_path = parse_url( $this->options['external_cdn_link'],  PHP_URL_PATH );
136
+				if ( ! empty($this->options['external_cdn_link'])) {
137
+					$cdn_path = parse_url($this->options['external_cdn_link'], PHP_URL_PATH);
138 138
 
139
-					if ( isset( $cdn_path, $parsed_url['path'] ) && $cdn_path !== '/' && ! empty( $parsed_url['path'] ) ) {
140
-						$parsed_url['path'] = str_replace( $cdn_path, '', $parsed_url['path'] );
139
+					if (isset($cdn_path, $parsed_url['path']) && $cdn_path !== '/' && ! empty($parsed_url['path'])) {
140
+						$parsed_url['path'] = str_replace($cdn_path, '', $parsed_url['path']);
141 141
 					}
142 142
 				}
143 143
 
144
-				$url = http_build_url( $parsed_url );
144
+				$url = http_build_url($parsed_url);
145 145
 
146
-				$url = add_query_arg( $this->get_global_params(), $url );
146
+				$url = add_query_arg($this->get_global_params(), $url);
147 147
 			}
148 148
 		}
149 149
 
@@ -159,40 +159,40 @@  discard block
 block discarded – undo
159 159
 	 *
160 160
 	 * @return false|array
161 161
 	 */
162
-	public function image_downsize( $return, $attachment_id, $size ) {
163
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
164
-			$img_url = wp_get_attachment_url( $attachment_id );
162
+	public function image_downsize($return, $attachment_id, $size) {
163
+		if ( ! empty ($this->options['cdn_link'])) {
164
+			$img_url = wp_get_attachment_url($attachment_id);
165 165
 
166 166
 			$params = [];
167
-			if ( is_array( $size ) ) {
168
-				$params['w'] = $width = isset( $size[0] ) ? $size[0] : 0;
169
-				$params['h'] = $height = isset( $size[1] ) ? $size[1] : 0;
167
+			if (is_array($size)) {
168
+				$params['w'] = $width = isset($size[0]) ? $size[0] : 0;
169
+				$params['h'] = $height = isset($size[1]) ? $size[1] : 0;
170 170
 			} else {
171 171
 				$available_sizes = $this->get_all_defined_sizes();
172
-				if ( isset( $available_sizes[ $size ] ) ) {
173
-					$size        = $available_sizes[ $size ];
172
+				if (isset($available_sizes[$size])) {
173
+					$size        = $available_sizes[$size];
174 174
 					$params['w'] = $width = $size['width'];
175 175
 					$params['h'] = $height = $size['height'];
176 176
 				}
177 177
 			}
178 178
 
179
-			$params = array_filter( $params );
179
+			$params = array_filter($params);
180 180
 
181
-			$img_url = add_query_arg( $params, $img_url );
181
+			$img_url = add_query_arg($params, $img_url);
182 182
 
183
-			if ( ! isset( $width ) || ! isset( $height ) ) {
183
+			if ( ! isset($width) || ! isset($height)) {
184 184
 				// any other type: use the real image
185
-				$meta   = wp_get_attachment_metadata( $attachment_id );
185
+				$meta = wp_get_attachment_metadata($attachment_id);
186 186
 
187 187
 				// Image sizes is missing for pdf thumbnails
188
-				$meta['width']  = isset( $meta['width'] ) ? $meta['width'] : 0;
189
-				$meta['height'] = isset( $meta['height'] ) ? $meta['height'] : 0;
188
+				$meta['width']  = isset($meta['width']) ? $meta['width'] : 0;
189
+				$meta['height'] = isset($meta['height']) ? $meta['height'] : 0;
190 190
 
191
-				$width  = isset( $width ) ? $width : $meta['width'];
192
-				$height = isset( $height ) ? $height : $meta['height'];
191
+				$width  = isset($width) ? $width : $meta['width'];
192
+				$height = isset($height) ? $height : $meta['height'];
193 193
 			}
194 194
 
195
-			$return = [ $img_url, $width, $height, true ];
195
+			$return = [$img_url, $width, $height, true];
196 196
 		}
197 197
 
198 198
 		return $return;
@@ -209,16 +209,16 @@  discard block
 block discarded – undo
209 209
 	 *
210 210
 	 * @return array
211 211
 	 */
212
-	public function calculate_image_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
213
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
214
-			foreach ( $sources as $i => $image_size ) {
215
-				if ( $image_size['descriptor'] === 'w' ) {
216
-					if ( $attachment_id ) {
217
-						$image_src = wp_get_attachment_url( $attachment_id );
212
+	public function calculate_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) {
213
+		if ( ! empty ($this->options['cdn_link'])) {
214
+			foreach ($sources as $i => $image_size) {
215
+				if ($image_size['descriptor'] === 'w') {
216
+					if ($attachment_id) {
217
+						$image_src = wp_get_attachment_url($attachment_id);
218 218
 					}
219 219
 
220
-					$image_src            = remove_query_arg( 'h', $image_src );
221
-					$sources[ $i ]['url'] = add_query_arg( 'w', $image_size['value'], $image_src );
220
+					$image_src            = remove_query_arg('h', $image_src);
221
+					$sources[$i]['url'] = add_query_arg('w', $image_size['value'], $image_src);
222 222
 				}
223 223
 			}
224 224
 		}
@@ -233,35 +233,35 @@  discard block
 block discarded – undo
233 233
 	 *
234 234
 	 * @return string
235 235
 	 */
236
-	public function replace_images_in_content( $content ) {
236
+	public function replace_images_in_content($content) {
237 237
 		// Added null to apply filters wp_get_attachment_url to improve compatibility with https://en-gb.wordpress.org/plugins/amazon-s3-and-cloudfront/ - does not break wordpress if the plugin isn't present.
238
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
239
-			if ( preg_match_all( '/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches ) ) {
240
-				foreach ( $matches[2] as $image_src ) {
241
-					$content = str_replace( $image_src, apply_filters( 'wp_get_attachment_url', $image_src, null ), $content );
238
+		if ( ! empty ($this->options['cdn_link'])) {
239
+			if (preg_match_all('/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches)) {
240
+				foreach ($matches[2] as $image_src) {
241
+					$content = str_replace($image_src, apply_filters('wp_get_attachment_url', $image_src, null), $content);
242 242
 				}
243 243
 			}
244 244
 
245
-			if ( preg_match_all( '/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches ) ) {
245
+			if (preg_match_all('/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches)) {
246 246
 
247
-				foreach ( $matches[2] as $image_srcset ) {
248
-					$new_image_srcset = preg_replace_callback( '/(\S+)(\s\d+\w)/', function ( $srcset_matches ) {
249
-						return apply_filters( 'wp_get_attachment_url', $srcset_matches[1], null ) . $srcset_matches[2];
250
-					}, $image_srcset );
247
+				foreach ($matches[2] as $image_srcset) {
248
+					$new_image_srcset = preg_replace_callback('/(\S+)(\s\d+\w)/', function($srcset_matches) {
249
+						return apply_filters('wp_get_attachment_url', $srcset_matches[1], null) . $srcset_matches[2];
250
+					}, $image_srcset);
251 251
 
252
-					$content = str_replace( $image_srcset, $new_image_srcset, $content );
252
+					$content = str_replace($image_srcset, $new_image_srcset, $content);
253 253
 				}
254 254
 			}
255 255
 
256
-			if ( preg_match_all( '/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches ) ) {
257
-				foreach ( $matches[0] as $link ) {
258
-					$content = str_replace( $link[2], apply_filters( 'wp_get_attachment_url', $link[2], null ), $content );
256
+			if (preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches)) {
257
+				foreach ($matches[0] as $link) {
258
+					$content = str_replace($link[2], apply_filters('wp_get_attachment_url', $link[2], null), $content);
259 259
 				}
260 260
 			}
261 261
 
262
-      if ( preg_match_all('/url\(([\s])?([\"|\'])?(.*?)([\"|\'])?([\s])?\)/i', $content, $matches ) ) {
263
-        foreach ( $matches[3] as $image_src ) {
264
-          $content = str_replace( $image_src, apply_filters( 'wp_get_attachment_url', $image_src, null ), $content );
262
+      if (preg_match_all('/url\(([\s])?([\"|\'])?(.*?)([\"|\'])?([\s])?\)/i', $content, $matches)) {
263
+        foreach ($matches[3] as $image_src) {
264
+          $content = str_replace($image_src, apply_filters('wp_get_attachment_url', $image_src, null), $content);
265 265
         }
266 266
       }
267 267
 		}
@@ -272,12 +272,12 @@  discard block
 block discarded – undo
272 272
 	 * Add tag to dns prefetch cdn host
273 273
 	 */
274 274
 	public function prefetch_cdn() {
275
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
276
-			$host = parse_url( $this->options['cdn_link'], PHP_URL_HOST );
275
+		if ( ! empty ($this->options['cdn_link'])) {
276
+			$host = parse_url($this->options['cdn_link'], PHP_URL_HOST);
277 277
 
278 278
 			printf(
279 279
 				'<link rel="dns-prefetch" href="%s"/>',
280
-				esc_attr( '//' . $host )
280
+				esc_attr('//' . $host)
281 281
 			);
282 282
 		}
283 283
 	}
@@ -286,8 +286,8 @@  discard block
 block discarded – undo
286 286
 	 * Start output buffer if auto retina is enabled
287 287
 	 */
288 288
 	public function buffer_start_for_retina() {
289
-		if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) {
290
-			$this->buffer_started = ob_start( [ $this, 'add_retina' ] );
289
+		if ( ! empty ($this->options['add_dpi2_srcset'])) {
290
+			$this->buffer_started = ob_start([$this, 'add_retina']);
291 291
 		}
292 292
 	}
293 293
 
@@ -295,7 +295,7 @@  discard block
 block discarded – undo
295 295
 	 * Stop output buffer if it was enabled by the plugin
296 296
 	 */
297 297
 	public function buffer_end_for_retina() {
298
-		if ( $this->buffer_started && ob_get_level() ) {
298
+		if ($this->buffer_started && ob_get_level()) {
299 299
 			ob_end_flush();
300 300
 		}
301 301
 	}
@@ -311,20 +311,20 @@  discard block
 block discarded – undo
311 311
 
312 312
 		// For now, only "auto" is supported.
313 313
 		$auto = [];
314
-		if ( ! empty ( $this->options['auto_format'] ) ) {
315
-			array_push( $auto, 'format' );
314
+		if ( ! empty ($this->options['auto_format'])) {
315
+			array_push($auto, 'format');
316 316
 		}
317 317
 
318
-		if ( ! empty ( $this->options['auto_enhance'] ) ) {
319
-			array_push( $auto, 'enhance' );
318
+		if ( ! empty ($this->options['auto_enhance'])) {
319
+			array_push($auto, 'enhance');
320 320
 		}
321 321
 
322
-		if ( ! empty ( $this->options['auto_compress'] ) ) {
323
-			array_push( $auto, 'compress' );
322
+		if ( ! empty ($this->options['auto_compress'])) {
323
+			array_push($auto, 'compress');
324 324
 		}
325 325
 
326
-		if ( ! empty( $auto ) ) {
327
-			$params['auto'] = implode( '%2C', $auto );
326
+		if ( ! empty($auto)) {
327
+			$params['auto'] = implode('%2C', $auto);
328 328
 		}
329 329
 
330 330
 		return $params;
@@ -340,18 +340,18 @@  discard block
 block discarded – undo
340 340
 		$theme_image_sizes = wp_get_additional_image_sizes();
341 341
 
342 342
 		$sizes = [];
343
-		foreach ( get_intermediate_image_sizes() as $s ) {
344
-			$sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ];
345
-			if ( isset( $theme_image_sizes[ $s ] ) ) {
343
+		foreach (get_intermediate_image_sizes() as $s) {
344
+			$sizes[$s] = ['width' => '', 'height' => '', 'crop' => false];
345
+			if (isset($theme_image_sizes[$s])) {
346 346
 				// For theme-added sizes
347
-				$sizes[ $s ]['width']  = intval( $theme_image_sizes[ $s ]['width'] );
348
-				$sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] );
349
-				$sizes[ $s ]['crop']   = $theme_image_sizes[ $s ]['crop'];
347
+				$sizes[$s]['width']  = intval($theme_image_sizes[$s]['width']);
348
+				$sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']);
349
+				$sizes[$s]['crop']   = $theme_image_sizes[$s]['crop'];
350 350
 			} else {
351 351
 				// For default sizes set in options
352
-				$sizes[ $s ]['width']  = get_option( "{$s}_size_w" );
353
-				$sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
354
-				$sizes[ $s ]['crop']   = get_option( "{$s}_crop" );
352
+				$sizes[$s]['width']  = get_option("{$s}_size_w");
353
+				$sizes[$s]['height'] = get_option("{$s}_size_h");
354
+				$sizes[$s]['crop']   = get_option("{$s}_crop");
355 355
 			}
356 356
 		}
357 357
 
Please login to merge, or discard this patch.