Completed
Pull Request — master (#18)
by Rasmus
02:03
created
includes/class-images-via-imgix.php 1 patch
Spacing   +80 added lines, -80 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,39 +90,39 @@  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 );
93
+	public function replace_image_url($url) {
94
+		if ( ! empty ($this->options['cdn_link'])) {
95
+			$pathinfo = pathinfo($url);
96 96
 
97
-			if ( isset( $pathinfo['extension'] ) && in_array( $pathinfo['extension'], [
97
+			if (isset($pathinfo['extension']) && in_array($pathinfo['extension'], [
98 98
 					'jpg',
99 99
 					'gif',
100 100
 					'png',
101 101
 					'jpeg'
102
-				] )
102
+				])
103 103
 			) {
104
-				$parsed_url = parse_url( $url );
105
-				if ( isset( $parsed_url['host'] ) && $parsed_url['host'] === parse_url( home_url( '/' ), PHP_URL_HOST ) ) {
106
-					$cdn = parse_url( $this->options['cdn_link'] );
107
-					foreach ( [ 'scheme', 'host', 'port' ] as $url_part ) {
108
-						if ( isset( $cdn[ $url_part ] ) ) {
109
-							$parsed_url[ $url_part ] = $cdn[ $url_part ];
104
+				$parsed_url = parse_url($url);
105
+				if (isset($parsed_url['host']) && $parsed_url['host'] === parse_url(home_url('/'), PHP_URL_HOST)) {
106
+					$cdn = parse_url($this->options['cdn_link']);
107
+					foreach (['scheme', 'host', 'port'] as $url_part) {
108
+						if (isset($cdn[$url_part])) {
109
+							$parsed_url[$url_part] = $cdn[$url_part];
110 110
 						} else {
111
-							unset( $parsed_url[ $url_part ] );
111
+							unset($parsed_url[$url_part]);
112 112
 						}
113 113
 					}
114 114
 
115 115
 					$arguments = [];
116
-					if ( ! empty( $parsed_url['query'] ) ) {
117
-						parse_str( $parsed_url['query'], $arguments );
116
+					if ( ! empty($parsed_url['query'])) {
117
+						parse_str($parsed_url['query'], $arguments);
118 118
 					}
119
-					$arguments = array_merge( $arguments, $this->get_global_params() );
119
+					$arguments = array_merge($arguments, $this->get_global_params());
120 120
 
121
-					if ( ! empty( $arguments ) ) {
122
-						$parsed_url['query'] = build_query( urlencode_deep( $arguments ) );
121
+					if ( ! empty($arguments)) {
122
+						$parsed_url['query'] = build_query(urlencode_deep($arguments));
123 123
 					}
124 124
 
125
-					$url = http_build_url( $parsed_url );
125
+					$url = http_build_url($parsed_url);
126 126
 				}
127 127
 			}
128 128
 		}
@@ -139,40 +139,40 @@  discard block
 block discarded – undo
139 139
 	 *
140 140
 	 * @return false|array
141 141
 	 */
142
-	public function image_downsize( $return, $attachment_id, $size ) {
143
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
144
-			$img_url    = wp_get_attachment_url( $attachment_id );
145
-			$parsed_url = parse_url( $img_url );
142
+	public function image_downsize($return, $attachment_id, $size) {
143
+		if ( ! empty ($this->options['cdn_link'])) {
144
+			$img_url    = wp_get_attachment_url($attachment_id);
145
+			$parsed_url = parse_url($img_url);
146 146
 
147
-			if ( is_array( $parsed_url ) ) {
148
-				parse_str( $parsed_url['query'], $params );
147
+			if (is_array($parsed_url)) {
148
+				parse_str($parsed_url['query'], $params);
149 149
 
150
-				if ( is_array( $size ) ) {
151
-					$params['w'] = $width = isset( $size[0] ) ? $size[0] : 0;
152
-					$params['h'] = $height = isset( $size[1] ) ? $size[1] : 0;
150
+				if (is_array($size)) {
151
+					$params['w'] = $width = isset($size[0]) ? $size[0] : 0;
152
+					$params['h'] = $height = isset($size[1]) ? $size[1] : 0;
153 153
 				} else {
154 154
 					$available_sizes = $this->get_all_defined_sizes();
155
-					if ( isset( $available_sizes[ $size ] ) ) {
156
-						$size        = $available_sizes[ $size ];
155
+					if (isset($available_sizes[$size])) {
156
+						$size        = $available_sizes[$size];
157 157
 						$params['w'] = $width = $size['width'];
158 158
 						$params['h'] = $height = $size['height'];
159 159
 					}
160 160
 				}
161 161
 
162
-				$params = array_filter( $params );
162
+				$params = array_filter($params);
163 163
 
164
-				$parsed_url['query'] = build_query( urlencode_deep( $params ) );
164
+				$parsed_url['query'] = build_query(urlencode_deep($params));
165 165
 
166
-				$img_url = http_build_url( $parsed_url );
166
+				$img_url = http_build_url($parsed_url);
167 167
 
168
-				if ( ! isset( $width ) || ! isset( $height ) ) {
168
+				if ( ! isset($width) || ! isset($height)) {
169 169
 					// any other type: use the real image
170
-					$meta   = wp_get_attachment_metadata( $attachment_id );
171
-					$width  = isset( $width ) ? $width : $meta['width'];
172
-					$height = isset( $height ) ? $height : $meta['height'];
170
+					$meta   = wp_get_attachment_metadata($attachment_id);
171
+					$width  = isset($width) ? $width : $meta['width'];
172
+					$height = isset($height) ? $height : $meta['height'];
173 173
 				}
174 174
 
175
-				$return = [ $img_url, $width, $height, true ];
175
+				$return = [$img_url, $width, $height, true];
176 176
 			}
177 177
 		}
178 178
 
@@ -186,9 +186,9 @@  discard block
 block discarded – undo
186 186
 	 *
187 187
 	 * @return array $sources
188 188
 	 */
189
-	public function replace_host_in_srcset( $sources ) {
190
-		foreach ( $sources as $source ) {
191
-			$sources[ $source['value'] ]['url'] = apply_filters( 'imgix/add-image-url', $sources[ $source['value'] ]['url'] );
189
+	public function replace_host_in_srcset($sources) {
190
+		foreach ($sources as $source) {
191
+			$sources[$source['value']]['url'] = apply_filters('imgix/add-image-url', $sources[$source['value']]['url']);
192 192
 		}
193 193
 
194 194
 		return $sources;
@@ -201,11 +201,11 @@  discard block
 block discarded – undo
201 201
 	 *
202 202
 	 * @return string
203 203
 	 */
204
-	public function replace_images_in_content( $content ) {
205
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
206
-			$content = preg_replace_callback( '/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function ( $match ) {
207
-				return esc_url( apply_filters( 'imgix/add-image-url', $match[0] ) );
208
-			}, $content );
204
+	public function replace_images_in_content($content) {
205
+		if ( ! empty ($this->options['cdn_link'])) {
206
+			$content = preg_replace_callback('/(?<=\shref="|\ssrc="|\shref=\'|\ssrc=\').*(?=\'|")/', function($match) {
207
+				return esc_url(apply_filters('imgix/add-image-url', $match[0]));
208
+			}, $content);
209 209
 
210 210
 		}
211 211
 
@@ -216,12 +216,12 @@  discard block
 block discarded – undo
216 216
 	 * Add tag to dns prefetch cdn host
217 217
 	 */
218 218
 	public function prefetch_cdn() {
219
-		if ( ! empty ( $this->options['cdn_link'] ) ) {
220
-			$host = parse_url( $this->options['cdn_link'], PHP_URL_HOST );
219
+		if ( ! empty ($this->options['cdn_link'])) {
220
+			$host = parse_url($this->options['cdn_link'], PHP_URL_HOST);
221 221
 
222 222
 			printf(
223 223
 				'<link rel="dns-prefetch" href="%s"/>',
224
-				esc_attr( '//' . $host )
224
+				esc_attr('//' . $host)
225 225
 			);
226 226
 		}
227 227
 	}
@@ -230,9 +230,9 @@  discard block
 block discarded – undo
230 230
 	 * Start output buffer if auto retina is enabled
231 231
 	 */
232 232
 	public function buffer_start_for_retina() {
233
-		if ( ! empty ( $this->options['add_dpi2_srcset'] ) ) {
233
+		if ( ! empty ($this->options['add_dpi2_srcset'])) {
234 234
 			$this->buffer_started = true;
235
-			ob_start( [ $this, 'add_retina' ] );
235
+			ob_start([$this, 'add_retina']);
236 236
 		}
237 237
 	}
238 238
 
@@ -240,7 +240,7 @@  discard block
 block discarded – undo
240 240
 	 * Stop output buffer if it was enabled by the plugin
241 241
 	 */
242 242
 	public function buffer_end_for_retina() {
243
-		if ( $this->buffer_started === true ) {
243
+		if ($this->buffer_started === true) {
244 244
 			ob_end_flush();
245 245
 		}
246 246
 	}
@@ -256,16 +256,16 @@  discard block
 block discarded – undo
256 256
 
257 257
 		// For now, only "auto" is supported.
258 258
 		$auto = [];
259
-		if ( ! empty ( $this->options['auto_format'] ) ) {
260
-			array_push( $auto, 'format' );
259
+		if ( ! empty ($this->options['auto_format'])) {
260
+			array_push($auto, 'format');
261 261
 		}
262 262
 
263
-		if ( ! empty ( $this->options['auto_enhance'] ) ) {
264
-			array_push( $auto, 'enhance' );
263
+		if ( ! empty ($this->options['auto_enhance'])) {
264
+			array_push($auto, 'enhance');
265 265
 		}
266 266
 
267
-		if ( ! empty( $auto ) ) {
268
-			$params['auto'] = implode( '%2C', $auto );
267
+		if ( ! empty($auto)) {
268
+			$params['auto'] = implode('%2C', $auto);
269 269
 		}
270 270
 
271 271
 		return $params;
@@ -281,18 +281,18 @@  discard block
 block discarded – undo
281 281
 		$theme_image_sizes = wp_get_additional_image_sizes();
282 282
 
283 283
 		$sizes = [];
284
-		foreach ( get_intermediate_image_sizes() as $s ) {
285
-			$sizes[ $s ] = [ 'width' => '', 'height' => '', 'crop' => false ];
286
-			if ( isset( $theme_image_sizes[ $s ] ) ) {
284
+		foreach (get_intermediate_image_sizes() as $s) {
285
+			$sizes[$s] = ['width' => '', 'height' => '', 'crop' => false];
286
+			if (isset($theme_image_sizes[$s])) {
287 287
 				// For theme-added sizes
288
-				$sizes[ $s ]['width']  = intval( $theme_image_sizes[ $s ]['width'] );
289
-				$sizes[ $s ]['height'] = intval( $theme_image_sizes[ $s ]['height'] );
290
-				$sizes[ $s ]['crop']   = $theme_image_sizes[ $s ]['crop'];
288
+				$sizes[$s]['width']  = intval($theme_image_sizes[$s]['width']);
289
+				$sizes[$s]['height'] = intval($theme_image_sizes[$s]['height']);
290
+				$sizes[$s]['crop']   = $theme_image_sizes[$s]['crop'];
291 291
 			} else {
292 292
 				// For default sizes set in options
293
-				$sizes[ $s ]['width']  = get_option( "{$s}_size_w" );
294
-				$sizes[ $s ]['height'] = get_option( "{$s}_size_h" );
295
-				$sizes[ $s ]['crop']   = get_option( "{$s}_crop" );
293
+				$sizes[$s]['width']  = get_option("{$s}_size_w");
294
+				$sizes[$s]['height'] = get_option("{$s}_size_h");
295
+				$sizes[$s]['crop']   = get_option("{$s}_crop");
296 296
 			}
297 297
 		}
298 298
 
Please login to merge, or discard this patch.