Completed
Pull Request — master (#18)
by Rasmus
02:38
created
includes/class-images-via-imgix.php 1 patch
Spacing   +84 added lines, -84 removed lines patch added patch discarded remove patch
@@ -27,19 +27,19 @@  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, 'replace_host_in_srcset' ], 10 );
38
-		add_filter( 'the_content', [ $this, 'replace_images_in_content' ] );
39
-		add_action( 'wp_head', [ $this, 'prefetch_cdn' ], 1 );
37
+		add_filter('wp_calculate_image_srcset', [$this, 'replace_host_in_srcset'], 10);
38
+		add_filter('the_content', [$this, 'replace_images_in_content']);
39
+		add_action('wp_head', [$this, 'prefetch_cdn'], 1);
40 40
 
41
-		add_action( 'after_setup_theme', [ $this, 'buffer_start_for_retina' ] );
42
-		add_action( 'shutdown', [ $this, 'buffer_end_for_retina' ] );
41
+		add_action('after_setup_theme', [$this, 'buffer_start_for_retina']);
42
+		add_action('shutdown', [$this, 'buffer_end_for_retina']);
43 43
 	}
44 44
 
45 45
 	/**
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
 	 * @return Images_Via_Imgix
49 49
 	 */
50 50
 	public static function instance() {
51
-		if ( ! isset( self::$instance ) ) {
51
+		if ( ! isset(self::$instance)) {
52 52
 			self::$instance = new self;
53 53
 		}
54 54
 
@@ -61,7 +61,7 @@  discard block
 block discarded – undo
61 61
 	 *
62 62
 	 * @param array $options
63 63
 	 */
64
-	public function set_options( $options ) {
64
+	public function set_options($options) {
65 65
 		$this->options = $options;
66 66
 	}
67 67
 
@@ -74,13 +74,13 @@  discard block
 block discarded – undo
74 74
 	 *
75 75
 	 * @return string Content with retina-enriched image tags.
76 76
 	 */
77
-	public function add_retina( $content ) {
77
+	public function add_retina($content) {
78 78
 		$pattern = '/<img((?![^>]+srcset )([^>]*)';
79 79
 		$pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i';
80 80
 		$repl    = '<img$2src="$3" srcset="${3}, ${3}&amp;dpr=2 2x, ${3}&amp;dpr=3 3x,"$4>';
81
-		$content = preg_replace( $pattern, $repl, $content );
81
+		$content = preg_replace($pattern, $repl, $content);
82 82
 
83
-		return preg_replace( $pattern, $repl, $content );
83
+		return preg_replace($pattern, $repl, $content);
84 84
 	}
85 85
 
86 86
 	/**
@@ -90,33 +90,33 @@  discard block
 block discarded – undo
90 90
 	 *
91 91
 	 * @return string
92 92
 	 */
93
-	public function replace_image_url( $url ) {
94
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
95
-			$pathinfo = pathinfo( $url );
96
-
97
-			if ( in_array( $pathinfo['extension'], [ 'jpg', 'gif', 'png', 'jpeg' ] ) ) {
98
-				$parsed_url = parse_url( $url );
99
-				if ( isset( $parsed_url['host'] ) && $parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) ) {
100
-					$cdn = parse_url( $this->options['cdn_link'] );
101
-					foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) {
102
-						if ( isset( $cdn[ $url_part ] ) ) {
103
-							$parsed_url[ $url_part ] = $cdn[ $url_part ];
93
+	public function replace_image_url($url) {
94
+		if ( ! empty ($this->options['cdn_link'])) {
95
+			$pathinfo = pathinfo($url);
96
+
97
+			if (in_array($pathinfo['extension'], ['jpg', 'gif', 'png', 'jpeg'])) {
98
+				$parsed_url = parse_url($url);
99
+				if (isset($parsed_url['host']) && $parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST)) {
100
+					$cdn = parse_url($this->options['cdn_link']);
101
+					foreach (['scheme', 'host', 'port'] as $url_part) {
102
+						if (isset($cdn[$url_part])) {
103
+							$parsed_url[$url_part] = $cdn[$url_part];
104 104
 						} else {
105
-							unset( $parsed_url[ $url_part ] );
105
+							unset($parsed_url[$url_part]);
106 106
 						}
107 107
 					}
108 108
 
109
-					list( $filename, $arguments ) = $this->convert_filename_to_size_args( $pathinfo['basename'] );
109
+					list($filename, $arguments) = $this->convert_filename_to_size_args($pathinfo['basename']);
110 110
 
111
-					$arguments = array_merge( $arguments, $this->get_global_params() );
111
+					$arguments = array_merge($arguments, $this->get_global_params());
112 112
 
113
-					$parsed_url['path'] = trailingslashit( dirname( $parsed_url['path'] ) ) . $filename;
113
+					$parsed_url['path'] = trailingslashit(dirname($parsed_url['path'])) . $filename;
114 114
 
115
-					if ( ! empty( $arguments ) ) {
116
-						$parsed_url['query'] = empty( $parsed_url['query'] ) ? build_query( $arguments ) : $parsed_url['query'] . '&' . build_query( $arguments );
115
+					if ( ! empty($arguments)) {
116
+						$parsed_url['query'] = empty($parsed_url['query']) ? build_query($arguments) : $parsed_url['query'] . '&' . build_query($arguments);
117 117
 					}
118 118
 
119
-					$url = http_build_url( $parsed_url );
119
+					$url = http_build_url($parsed_url);
120 120
 				}
121 121
 			}
122 122
 		}
@@ -133,38 +133,38 @@  discard block
 block discarded – undo
133 133
 	 *
134 134
 	 * @return false|array
135 135
 	 */
136
-	public function image_downsize( $return, $attachment_id, $size ) {
137
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
138
-			$img_url    = wp_get_attachment_url( $attachment_id );
139
-			$parsed_url = parse_url( $img_url );
140
-			parse_str( $parsed_url['query'], $params );
141
-
142
-			if ( is_array( $size ) ) {
143
-				$params['w'] = $width = isset( $size[0] ) ? $size[0] : 0;
144
-				$params['h'] = $height = isset( $size[1] ) ? $size[1] : 0;
136
+	public function image_downsize($return, $attachment_id, $size) {
137
+		if ( ! empty ($this->options['cdn_link'])) {
138
+			$img_url    = wp_get_attachment_url($attachment_id);
139
+			$parsed_url = parse_url($img_url);
140
+			parse_str($parsed_url['query'], $params);
141
+
142
+			if (is_array($size)) {
143
+				$params['w'] = $width = isset($size[0]) ? $size[0] : 0;
144
+				$params['h'] = $height = isset($size[1]) ? $size[1] : 0;
145 145
 			} else {
146 146
 				$available_sizes = $this->get_all_defined_sizes();
147
-				if ( isset( $available_sizes[ $size ] ) ) {
148
-					$size        = $available_sizes[ $size ];
147
+				if (isset($available_sizes[$size])) {
148
+					$size        = $available_sizes[$size];
149 149
 					$params['w'] = $width = $size['width'];
150 150
 					$params['h'] = $height = $size['height'];
151 151
 				}
152 152
 			}
153 153
 
154
-			$params = array_filter( $params );
154
+			$params = array_filter($params);
155 155
 
156
-			$parsed_url['query'] = build_query( $params );
156
+			$parsed_url['query'] = build_query($params);
157 157
 
158
-			$img_url = http_build_url( $parsed_url );
158
+			$img_url = http_build_url($parsed_url);
159 159
 
160
-			if ( ! isset( $width ) && ! isset( $height ) ) {
160
+			if ( ! isset($width) && ! isset($height)) {
161 161
 				// any other type: use the real image
162
-				$meta   = wp_get_attachment_metadata( $attachment_id );
162
+				$meta   = wp_get_attachment_metadata($attachment_id);
163 163
 				$width  = $meta['width'];
164 164
 				$height = $meta['height'];
165 165
 			}
166 166
 
167
-			$return = [ $img_url, $width, $height, true ];
167
+			$return = [$img_url, $width, $height, true];
168 168
 		}
169 169
 
170 170
 		return $return;
@@ -177,9 +177,9 @@  discard block
 block discarded – undo
177 177
 	 *
178 178
 	 * @return array $sources
179 179
 	 */
180
-	public function replace_host_in_srcset( $sources ) {
181
-		foreach ( $sources as $source ) {
182
-			$sources[ $source['value'] ]['url'] = apply_filters( 'imgix/add-image-url', $sources[ $source['value'] ]['url'] );
180
+	public function replace_host_in_srcset($sources) {
181
+		foreach ($sources as $source) {
182
+			$sources[$source['value']]['url'] = apply_filters('imgix/add-image-url', $sources[$source['value']]['url']);
183 183
 		}
184 184
 
185 185
 		return $sources;
@@ -192,11 +192,11 @@  discard block
 block discarded – undo
192 192
 	 *
193 193
 	 * @return string
194 194
 	 */
195
-	public function replace_images_in_content( $content ) {
196
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
197
-			$content = preg_replace_callback( '/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function ( $match ) {
198
-				return esc_url( apply_filters( 'imgix/add-image-url', $match[0] ) );
199
-			}, $content );
195
+	public function replace_images_in_content($content) {
196
+		if ( ! empty ($this->options['cdn_link'])) {
197
+			$content = preg_replace_callback('/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function($match) {
198
+				return esc_url(apply_filters('imgix/add-image-url', $match[0]));
199
+			}, $content);
200 200
 
201 201
 		}
202 202
 
@@ -207,12 +207,12 @@  discard block
 block discarded – undo
207 207
 	 * Add tag to dns prefetch cdn host
208 208
 	 */
209 209
 	public function prefetch_cdn() {
210
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
211
-			$host = parse_url( $this->options['cdn_link'], PHP_URL_HOST );
210
+		if ( ! empty ($this->options['cdn_link'])) {
211
+			$host = parse_url($this->options['cdn_link'], PHP_URL_HOST);
212 212
 
213 213
 			printf(
214 214
 				'<link rel="dns-prefetch" href="%s"/>',
215
-				esc_attr( '//' . $host )
215
+				esc_attr('//' . $host)
216 216
 			);
217 217
 		}
218 218
 	}
@@ -221,9 +221,9 @@  discard block
 block discarded – undo
221 221
 	 * Start output buffer if auto retina is enabled
222 222
 	 */
223 223
 	public function buffer_start_for_retina() {
224
-		if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) {
224
+		if ( ! empty ($this->options['add_dpi2_srcset'])) {
225 225
 			$this->buffer_started = true;
226
-			ob_start( [ $this, 'add_retina' ] );
226
+			ob_start([$this, 'add_retina']);
227 227
 		}
228 228
 	}
229 229
 
@@ -231,7 +231,7 @@  discard block
 block discarded – undo
231 231
 	 * Stop output buffer if it was enabled by the plugin
232 232
 	 */
233 233
 	public function buffer_end_for_retina() {
234
-		if ( $this->buffer_started === true ) {
234
+		if ($this->buffer_started === true) {
235 235
 			ob_end_flush();
236 236
 		}
237 237
 	}
@@ -247,16 +247,16 @@  discard block
 block discarded – undo
247 247
 
248 248
 		// For now, only "auto" is supported.
249 249
 		$auto = [];
250
-		if ( ! empty ( $this->options['auto_format'] ) ) {
251
-			array_push( $auto, 'format' );
250
+		if ( ! empty ($this->options['auto_format'])) {
251
+			array_push($auto, 'format');
252 252
 		}
253 253
 
254
-		if ( ! empty ( $this->options['auto_enhance'] ) ) {
255
-			array_push( $auto, 'enhance' );
254
+		if ( ! empty ($this->options['auto_enhance'])) {
255
+			array_push($auto, 'enhance');
256 256
 		}
257 257
 
258
-		if ( ! empty( $auto ) ) {
259
-			$params['auto'] = implode( ',', $auto );
258
+		if ( ! empty($auto)) {
259
+			$params['auto'] = implode(',', $auto);
260 260
 		}
261 261
 
262 262
 		return $params;
@@ -270,19 +270,19 @@  discard block
 block discarded – undo
270 270
 	 *
271 271
 	 * @return array with filename and size arguments.
272 272
 	 */
273
-	protected function convert_filename_to_size_args( $filename ) {
273
+	protected function convert_filename_to_size_args($filename) {
274 274
 		$arguments = [];
275 275
 
276
-		$filename = preg_replace_callback( '/-(?<width>\d+)x(?<height>\d+)(?<extension>\.\w{3,4}$)/', function ( $match ) use ( &$arguments ) {
276
+		$filename = preg_replace_callback('/-(?<width>\d+)x(?<height>\d+)(?<extension>\.\w{3,4}$)/', function($match) use (&$arguments) {
277 277
 			$arguments = [
278 278
 				'w' => $match['width'],
279 279
 				'h' => $match['height']
280 280
 			];
281 281
 
282 282
 			return $match['extension'];
283
-		}, $filename );
283
+		}, $filename);
284 284
 
285
-		return [ $filename, $arguments ];
285
+		return [$filename, $arguments];
286 286
 	}
287 287
 
288 288
 	/**
@@ -295,30 +295,30 @@  discard block
 block discarded – undo
295 295
 		$theme_image_sizes = wp_get_additional_image_sizes();
296 296
 
297 297
 		$sizes = [];
298
-		foreach ( get_intermediate_image_sizes() as $s ) {
299
-			$sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ];
300
-			if ( isset( $theme_image_sizes[ $s ]['width'] ) ) {
298
+		foreach (get_intermediate_image_sizes() as $s) {
299
+			$sizes[$s] = ['width' => '', 'height' => '', 'crop' => false];
300
+			if (isset($theme_image_sizes[$s]['width'])) {
301 301
 				// For theme-added sizes
302
-				$sizes[ $s ]['width'] = intval( $theme_image_sizes[ $s ]['width'] );
302
+				$sizes[$s]['width'] = intval($theme_image_sizes[$s]['width']);
303 303
 			} else {
304 304
 				// For default sizes set in options
305
-				$sizes[ $s ]['width'] = get_option( "{$s}_size_w" );
305
+				$sizes[$s]['width'] = get_option("{$s}_size_w");
306 306
 			}
307 307
 
308
-			if ( isset( $theme_image_sizes[ $s ]['height'] ) ) {
308
+			if (isset($theme_image_sizes[$s]['height'])) {
309 309
 				// For theme-added sizes
310
-				$sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] );
310
+				$sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']);
311 311
 			} else {
312 312
 				// For default sizes set in options
313
-				$sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
313
+				$sizes[$s]['height'] = get_option("{$s}_size_h");
314 314
 			}
315 315
 
316
-			if ( isset( $theme_image_sizes[ $s ]['crop'] ) ) {
316
+			if (isset($theme_image_sizes[$s]['crop'])) {
317 317
 				// For theme-added sizes
318
-				$sizes[ $s ]['crop'] = $theme_image_sizes[ $s ]['crop'];
318
+				$sizes[$s]['crop'] = $theme_image_sizes[$s]['crop'];
319 319
 			} else {
320 320
 				// For default sizes set in options
321
-				$sizes[ $s ]['crop'] = get_option( "{$s}_crop" );
321
+				$sizes[$s]['crop'] = get_option("{$s}_crop");
322 322
 			}
323 323
 		}
324 324
 
Please login to merge, or discard this patch.