Completed
Push — master ( 56e490...678d08 )
by LA
05:22 queued 02:44
created
includes/class-images-via-imgix.php 1 patch
Spacing   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -27,20 +27,20 @@  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
-		add_filter( 'wp_get_attachment_url', [ $this, 'replace_image_url' ] );
33
-		add_filter( 'imgix/add-image-url', [ $this, 'replace_image_url' ] );
32
+		add_filter('wp_get_attachment_url', [$this, 'replace_image_url']);
33
+		add_filter('imgix/add-image-url', [$this, 'replace_image_url']);
34 34
 
35
-		add_filter( 'image_downsize', [ $this, 'image_downsize' ], 10, 3 );
35
+		add_filter('image_downsize', [$this, 'image_downsize'], 10, 3);
36 36
 
37
-		add_filter( 'wp_calculate_image_srcset', [ $this, 'calculate_image_srcset' ], 10, 3 );
37
+		add_filter('wp_calculate_image_srcset', [$this, 'calculate_image_srcset'], 10, 3);
38 38
 
39
-		add_filter( 'the_content', [ $this, 'replace_images_in_content' ] );
40
-		add_action( 'wp_head', [ $this, 'prefetch_cdn' ], 1 );
39
+		add_filter('the_content', [$this, 'replace_images_in_content']);
40
+		add_action('wp_head', [$this, 'prefetch_cdn'], 1);
41 41
 
42
-		add_action( 'after_setup_theme', [ $this, 'buffer_start_for_retina' ] );
43
-		add_action( 'shutdown', [ $this, 'buffer_end_for_retina' ] );
42
+		add_action('after_setup_theme', [$this, 'buffer_start_for_retina']);
43
+		add_action('shutdown', [$this, 'buffer_end_for_retina']);
44 44
 	}
45 45
 
46 46
 	/**
@@ -49,7 +49,7 @@  discard block
 block discarded – undo
49 49
 	 * @return Images_Via_Imgix
50 50
 	 */
51 51
 	public static function instance() {
52
-		if ( ! isset( self::$instance ) ) {
52
+		if ( ! isset(self::$instance)) {
53 53
 			self::$instance = new self;
54 54
 		}
55 55
 
@@ -62,7 +62,7 @@  discard block
 block discarded – undo
62 62
 	 *
63 63
 	 * @param array $options
64 64
 	 */
65
-	public function set_options( $options ) {
65
+	public function set_options($options) {
66 66
 		$this->options = $options;
67 67
 	}
68 68
 
@@ -75,13 +75,13 @@  discard block
 block discarded – undo
75 75
 	 *
76 76
 	 * @return string Content with retina-enriched image tags.
77 77
 	 */
78
-	public function add_retina( $content ) {
78
+	public function add_retina($content) {
79 79
 		$pattern = '/<img((?![^>]+srcset )([^>]*)';
80 80
 		$pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i';
81 81
 		$repl    = '<img$2src="$3" srcset="${3}, ${3}&amp;dpr=2 2x, ${3}&amp;dpr=3 3x,"$4>';
82
-		$content = preg_replace( $pattern, $repl, $content );
82
+		$content = preg_replace($pattern, $repl, $content);
83 83
 
84
-		return preg_replace( $pattern, $repl, $content );
84
+		return preg_replace($pattern, $repl, $content);
85 85
 	}
86 86
 
87 87
 	/**
@@ -91,21 +91,21 @@  discard block
 block discarded – undo
91 91
 	 *
92 92
 	 * @return string
93 93
 	 */
94
-	public function replace_image_url( $url ) {
95
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
96
-			$parsed_url = parse_url( $url );
97
-
98
-			if ( isset( $parsed_url['host'], $parsed_url['path'] ) && $parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) && preg_match( '/\.(jpg|jpeg|gif|png)$/', $parsed_url['path'] ) ) {
99
-				$cdn = parse_url( $this->options['cdn_link'] );
100
-				foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) {
101
-					if ( isset( $cdn[ $url_part ] ) ) {
102
-						$parsed_url[ $url_part ] = $cdn[ $url_part ];
94
+	public function replace_image_url($url) {
95
+		if ( ! empty ($this->options['cdn_link'])) {
96
+			$parsed_url = parse_url($url);
97
+
98
+			if (isset($parsed_url['host'], $parsed_url['path']) && $parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST) && preg_match('/\.(jpg|jpeg|gif|png)$/', $parsed_url['path'])) {
99
+				$cdn = parse_url($this->options['cdn_link']);
100
+				foreach (['scheme', 'host', 'port'] as $url_part) {
101
+					if (isset($cdn[$url_part])) {
102
+						$parsed_url[$url_part] = $cdn[$url_part];
103 103
 					} else {
104
-						unset( $parsed_url[ $url_part ] );
104
+						unset($parsed_url[$url_part]);
105 105
 					}
106
-					$url = http_build_url( $parsed_url );
106
+					$url = http_build_url($parsed_url);
107 107
 
108
-					$url = add_query_arg( $this->get_global_params(), $url );
108
+					$url = add_query_arg($this->get_global_params(), $url);
109 109
 				}
110 110
 			}
111 111
 		}
@@ -123,35 +123,35 @@  discard block
 block discarded – undo
123 123
 	 *
124 124
 	 * @return false|array
125 125
 	 */
126
-	public function image_downsize( $return, $attachment_id, $size ) {
127
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
128
-			$img_url = wp_get_attachment_url( $attachment_id );
126
+	public function image_downsize($return, $attachment_id, $size) {
127
+		if ( ! empty ($this->options['cdn_link'])) {
128
+			$img_url = wp_get_attachment_url($attachment_id);
129 129
 
130 130
 			$params = [];
131
-			if ( is_array( $size ) ) {
132
-				$params['w'] = $width = isset( $size[0] ) ? $size[0] : 0;
133
-				$params['h'] = $height = isset( $size[1] ) ? $size[1] : 0;
131
+			if (is_array($size)) {
132
+				$params['w'] = $width = isset($size[0]) ? $size[0] : 0;
133
+				$params['h'] = $height = isset($size[1]) ? $size[1] : 0;
134 134
 			} else {
135 135
 				$available_sizes = $this->get_all_defined_sizes();
136
-				if ( isset( $available_sizes[ $size ] ) ) {
137
-					$size        = $available_sizes[ $size ];
136
+				if (isset($available_sizes[$size])) {
137
+					$size        = $available_sizes[$size];
138 138
 					$params['w'] = $width = $size['width'];
139 139
 					$params['h'] = $height = $size['height'];
140 140
 				}
141 141
 			}
142 142
 
143
-			$params = array_filter( $params );
143
+			$params = array_filter($params);
144 144
 
145
-			$img_url = add_query_arg( $params, $img_url );
145
+			$img_url = add_query_arg($params, $img_url);
146 146
 
147
-			if ( ! isset( $width ) || ! isset( $height ) ) {
147
+			if ( ! isset($width) || ! isset($height)) {
148 148
 				// any other type: use the real image
149
-				$meta   = wp_get_attachment_metadata( $attachment_id );
150
-				$width  = isset( $width ) ? $width : $meta['width'];
151
-				$height = isset( $height ) ? $height : $meta['height'];
149
+				$meta   = wp_get_attachment_metadata($attachment_id);
150
+				$width  = isset($width) ? $width : $meta['width'];
151
+				$height = isset($height) ? $height : $meta['height'];
152 152
 			}
153 153
 
154
-			$return = [ $img_url, $width, $height, true ];
154
+			$return = [$img_url, $width, $height, true];
155 155
 		}
156 156
 
157 157
 		return $return;
@@ -166,11 +166,11 @@  discard block
 block discarded – undo
166 166
 	 *
167 167
 	 * @return array
168 168
 	 */
169
-	public function calculate_image_srcset( $image_meta, $size_array, $image_src ) {
170
-		foreach ( $image_meta as $i => $image_size ) {
171
-			if ( $image_size['descriptor'] === 'w' ) {
172
-				$image_src = remove_query_arg( 'h', $image_src );
173
-				$image_meta[ $i ]['url'] = add_query_arg( 'w', $image_size['value'], $image_src );
169
+	public function calculate_image_srcset($image_meta, $size_array, $image_src) {
170
+		foreach ($image_meta as $i => $image_size) {
171
+			if ($image_size['descriptor'] === 'w') {
172
+				$image_src = remove_query_arg('h', $image_src);
173
+				$image_meta[$i]['url'] = add_query_arg('w', $image_size['value'], $image_src);
174 174
 			}
175 175
 		}
176 176
 
@@ -184,28 +184,28 @@  discard block
 block discarded – undo
184 184
 	 *
185 185
 	 * @return string
186 186
 	 */
187
-	public function replace_images_in_content( $content ) {
188
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
189
-			if ( preg_match_all( '/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches ) ) {
190
-				foreach ( $matches[2] as $image_src ) {
191
-					$content = str_replace( $image_src, $this->replace_image_url( $image_src ), $content );
187
+	public function replace_images_in_content($content) {
188
+		if ( ! empty ($this->options['cdn_link'])) {
189
+			if (preg_match_all('/<img\s[^>]*src=([\"\']??)([^\" >]*?)\1[^>]*>/iU', $content, $matches)) {
190
+				foreach ($matches[2] as $image_src) {
191
+					$content = str_replace($image_src, $this->replace_image_url($image_src), $content);
192 192
 				}
193 193
 			}
194 194
 
195
-			if ( preg_match_all( '/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches ) ) {
195
+			if (preg_match_all('/<img\s[^>]*srcset=([\"\']??)([^\">]*?)\1[^>]*\/?>/iU', $content, $matches)) {
196 196
 
197
-				foreach ( $matches[2] as $image_srcset ) {
198
-					$new_image_srcset = preg_replace_callback( '/(\S+)(\s\d+\w)/', function ( $srcset_matches ) {
199
-						return $this->replace_image_url( $srcset_matches[1] ) . $srcset_matches[2];
200
-					}, $image_srcset );
197
+				foreach ($matches[2] as $image_srcset) {
198
+					$new_image_srcset = preg_replace_callback('/(\S+)(\s\d+\w)/', function($srcset_matches) {
199
+						return $this->replace_image_url($srcset_matches[1]) . $srcset_matches[2];
200
+					}, $image_srcset);
201 201
 
202
-					$content = str_replace( $image_srcset, $new_image_srcset, $content );
202
+					$content = str_replace($image_srcset, $new_image_srcset, $content);
203 203
 				}
204 204
 			}
205 205
 
206
-			if ( preg_match_all( '/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches ) ) {
207
-				foreach ( $matches[0] as $link ) {
208
-					$content = str_replace( $link[2], $this->replace_image_url( $link[2] ), $content );
206
+			if (preg_match_all('/<a\s[^>]*href=([\"\']??)([^\" >]*?)\1[^>]*>(.*)<\/a>/iU', $content, $matches)) {
207
+				foreach ($matches[0] as $link) {
208
+					$content = str_replace($link[2], $this->replace_image_url($link[2]), $content);
209 209
 				}
210 210
 			}
211 211
 		}
@@ -217,12 +217,12 @@  discard block
 block discarded – undo
217 217
 	 * Add tag to dns prefetch cdn host
218 218
 	 */
219 219
 	public function prefetch_cdn() {
220
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
221
-			$host = parse_url( $this->options['cdn_link'], PHP_URL_HOST );
220
+		if ( ! empty ($this->options['cdn_link'])) {
221
+			$host = parse_url($this->options['cdn_link'], PHP_URL_HOST);
222 222
 
223 223
 			printf(
224 224
 				'<link rel="dns-prefetch" href="%s"/>',
225
-				esc_attr( '//' . $host )
225
+				esc_attr('//' . $host)
226 226
 			);
227 227
 		}
228 228
 	}
@@ -231,9 +231,9 @@  discard block
 block discarded – undo
231 231
 	 * Start output buffer if auto retina is enabled
232 232
 	 */
233 233
 	public function buffer_start_for_retina() {
234
-		if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) {
234
+		if ( ! empty ($this->options['add_dpi2_srcset'])) {
235 235
 			$this->buffer_started = true;
236
-			ob_start( [ $this, 'add_retina' ] );
236
+			ob_start([$this, 'add_retina']);
237 237
 		}
238 238
 	}
239 239
 
@@ -241,7 +241,7 @@  discard block
 block discarded – undo
241 241
 	 * Stop output buffer if it was enabled by the plugin
242 242
 	 */
243 243
 	public function buffer_end_for_retina() {
244
-		if ( $this->buffer_started === true ) {
244
+		if ($this->buffer_started === true) {
245 245
 			ob_end_flush();
246 246
 		}
247 247
 	}
@@ -257,20 +257,20 @@  discard block
 block discarded – undo
257 257
 
258 258
 		// For now, only "auto" is supported.
259 259
 		$auto = [];
260
-		if ( ! empty ( $this->options['auto_format'] ) ) {
261
-			array_push( $auto, 'format' );
260
+		if ( ! empty ($this->options['auto_format'])) {
261
+			array_push($auto, 'format');
262 262
 		}
263 263
 
264
-		if ( ! empty ( $this->options['auto_enhance'] ) ) {
265
-			array_push( $auto, 'enhance' );
264
+		if ( ! empty ($this->options['auto_enhance'])) {
265
+			array_push($auto, 'enhance');
266 266
 		}
267 267
 
268
-		if ( ! empty ( $this->options['auto_compress'] ) ) {
269
-			array_push( $auto, 'compress' );
268
+		if ( ! empty ($this->options['auto_compress'])) {
269
+			array_push($auto, 'compress');
270 270
 		}
271 271
 
272
-		if ( ! empty( $auto ) ) {
273
-			$params['auto'] = implode( '%2C', $auto );
272
+		if ( ! empty($auto)) {
273
+			$params['auto'] = implode('%2C', $auto);
274 274
 		}
275 275
 
276 276
 		return $params;
@@ -286,18 +286,18 @@  discard block
 block discarded – undo
286 286
 		$theme_image_sizes = wp_get_additional_image_sizes();
287 287
 
288 288
 		$sizes = [];
289
-		foreach ( get_intermediate_image_sizes() as $s ) {
290
-			$sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ];
291
-			if ( isset( $theme_image_sizes[ $s ] ) ) {
289
+		foreach (get_intermediate_image_sizes() as $s) {
290
+			$sizes[$s] = ['width' => '', 'height' => '', 'crop' => false];
291
+			if (isset($theme_image_sizes[$s])) {
292 292
 				// For theme-added sizes
293
-				$sizes[ $s ]['width']  = intval( $theme_image_sizes[ $s ]['width'] );
294
-				$sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] );
295
-				$sizes[ $s ]['crop']   = $theme_image_sizes[ $s ]['crop'];
293
+				$sizes[$s]['width']  = intval($theme_image_sizes[$s]['width']);
294
+				$sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']);
295
+				$sizes[$s]['crop']   = $theme_image_sizes[$s]['crop'];
296 296
 			} else {
297 297
 				// For default sizes set in options
298
-				$sizes[ $s ]['width']  = get_option( "{$s}_size_w" );
299
-				$sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
300
-				$sizes[ $s ]['crop']   = get_option( "{$s}_crop" );
298
+				$sizes[$s]['width']  = get_option("{$s}_size_w");
299
+				$sizes[$s]['height'] = get_option("{$s}_size_h");
300
+				$sizes[$s]['crop']   = get_option("{$s}_crop");
301 301
 			}
302 302
 		}
303 303
 
Please login to merge, or discard this patch.