Completed
Pull Request — master (#12)
by Rasmus
09:21
created
includes/do-functions.php 1 patch
Spacing   +107 added lines, -107 removed lines patch added patch discarded remove patch
@@ -13,13 +13,13 @@  discard block
 block discarded – undo
13 13
  *
14 14
  * @return string Content with retina-enriched image tags.
15 15
  */
16
-function add_retina( $content ) {
16
+function add_retina($content) {
17 17
 	$pattern = '/<img((?![^>]+srcset )([^>]*)';
18 18
 	$pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i';
19 19
 	$repl = '<img$2src="$3" srcset="${3}, ${3}&amp;dpr=2 2x, ${3}&amp;dpr=3 3x,"$4>';
20
-	$content = preg_replace( $pattern, $repl, $content );
20
+	$content = preg_replace($pattern, $repl, $content);
21 21
 
22
-	return preg_replace( $pattern, $repl, $content );
22
+	return preg_replace($pattern, $repl, $content);
23 23
 }
24 24
 
25 25
 /**
@@ -29,13 +29,13 @@  discard block
 block discarded – undo
29 29
  *
30 30
  * @return array An array of matching arrays with two keys: 'url' and 'params'.
31 31
  */
32
-function imgix_extract_imgs( $content ) {
33
-	preg_match_all( '/src=["\']http.+\/([^\s]+?)["\']/', $content, $matches );
32
+function imgix_extract_imgs($content) {
33
+	preg_match_all('/src=["\']http.+\/([^\s]+?)["\']/', $content, $matches);
34 34
 	$results = array();
35 35
 
36
-	if ( $matches ) {
37
-		foreach ( $matches[1] as $url ) {
38
-			array_push( $results, $url );
36
+	if ($matches) {
37
+		foreach ($matches[1] as $url) {
38
+			array_push($results, $url);
39 39
 		}
40 40
 	}
41 41
 
@@ -53,17 +53,17 @@  discard block
 block discarded – undo
53 53
  *
54 54
  * @return string Content with matching URLs having the new querystrings.
55 55
  */
56
-function apply_parameters_to_url( $url, $params, $content ) {
57
-	$parts = explode( '?', $url . '?' );
56
+function apply_parameters_to_url($url, $params, $content) {
57
+	$parts = explode('?', $url . '?');
58 58
 
59
-	list( $base_url, $base_params ) = array( $parts[0], $parts[1] );
59
+	list($base_url, $base_params) = array($parts[0], $parts[1]);
60 60
 
61 61
 	$new_url = $old_url = $base_url;
62 62
 	$new_url .= '?' . $params;
63 63
 	$new_url .= $base_params ? '&amp;' . $base_params : '';
64 64
 	$old_url .= $base_params ? '?' . $base_params : '';
65 65
 
66
-	return str_replace( $old_url, $new_url, $content );
66
+	return str_replace($old_url, $new_url, $content);
67 67
 }
68 68
 
69 69
 /**
@@ -78,19 +78,19 @@  discard block
 block discarded – undo
78 78
 	// For now, only "auto" is supported.
79 79
 	$auto = array();
80 80
 
81
-	if ( isset( $imgix_options['auto_format'] ) && $imgix_options['auto_format'] ) {
82
-		array_push( $auto, 'format' );
81
+	if (isset($imgix_options['auto_format']) && $imgix_options['auto_format']) {
82
+		array_push($auto, 'format');
83 83
 	}
84 84
 
85
-	if ( isset( $imgix_options['auto_enhance'] ) && $imgix_options['auto_enhance'] ) {
86
-		array_push( $auto, 'enhance' );
85
+	if (isset($imgix_options['auto_enhance']) && $imgix_options['auto_enhance']) {
86
+		array_push($auto, 'enhance');
87 87
 	}
88 88
 
89
-	if ( ! empty( $auto ) ) {
90
-		array_push( $params, 'auto=' . implode( '%2C', $auto ) );
89
+	if ( ! empty($auto)) {
90
+		array_push($params, 'auto=' . implode('%2C', $auto));
91 91
 	}
92 92
 
93
-	return implode( '&amp;', $params );
93
+	return implode('&amp;', $params);
94 94
 }
95 95
 
96 96
 /**
@@ -101,24 +101,24 @@  discard block
 block discarded – undo
101 101
  *
102 102
  * @return string A sanitized URL.
103 103
  */
104
-function ensure_valid_url( $url ) {
105
-	$slash = strpos( $url, '//' ) == 0 ? '//' : '';
104
+function ensure_valid_url($url) {
105
+	$slash = strpos($url, '//') == 0 ? '//' : '';
106 106
 
107
-	if ( $slash ) {
108
-		$url = substr( $url, 2 );
107
+	if ($slash) {
108
+		$url = substr($url, 2);
109 109
 	}
110 110
 
111
-	$urlp = wp_parse_url( $url );
112
-	$pref = array_key_exists( 'scheme', $urlp ) ? $urlp['scheme'] . '://' : $slash;
111
+	$urlp = wp_parse_url($url);
112
+	$pref = array_key_exists('scheme', $urlp) ? $urlp['scheme'] . '://' : $slash;
113 113
 
114
-	if ( ! $slash && strpos( $pref, 'http' ) !== 0 ) {
114
+	if ( ! $slash && strpos($pref, 'http') !== 0) {
115 115
 		$pref = 'http://';
116 116
 	}
117 117
 
118 118
 	$result = $urlp['host'] ? $pref . $urlp['host'] : false;
119 119
 
120
-	if ( $result ) {
121
-		return trailingslashit( $result );
120
+	if ($result) {
121
+		return trailingslashit($result);
122 122
 	}
123 123
 
124 124
 	return null;
@@ -131,32 +131,32 @@  discard block
 block discarded – undo
131 131
  *
132 132
  * @return array Size's width, height and crop values.
133 133
  */
134
-function get_size_info( $size ) {
134
+function get_size_info($size) {
135 135
 	global $_wp_additional_image_sizes;
136 136
 
137
-	if ( $size === 'original' ) {
137
+	if ($size === 'original') {
138 138
 		return array(
139 139
 			'width' => '',
140 140
 			'height' => '',
141 141
 			'crop' => false,
142 142
 		);
143
-	} elseif ( is_array( $size ) ) {
143
+	} elseif (is_array($size)) {
144 144
 		return array(
145 145
 			'width' => $size[1],
146 146
 			'height' => $size[0],
147 147
 			'crop' => false,
148 148
 		);
149
-	} elseif ( in_array( $size, array( 'thumbnail', 'medium', 'large' ), true ) ) {
149
+	} elseif (in_array($size, array('thumbnail', 'medium', 'large'), true)) {
150 150
 		return array(
151
-			'width' => get_option( $size . '_size_w' ),
152
-			'height' => get_option( $size . '_size_h' ),
153
-			'crop' => (boolean) get_option( $size . '_crop' ),
151
+			'width' => get_option($size . '_size_w'),
152
+			'height' => get_option($size . '_size_h'),
153
+			'crop' => (boolean) get_option($size . '_crop'),
154 154
 		);
155
-	} elseif ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
155
+	} elseif (isset($_wp_additional_image_sizes[$size])) {
156 156
 		return array(
157
-			'width' => $_wp_additional_image_sizes[ $size ]['width'],
158
-			'height' => $_wp_additional_image_sizes[ $size ]['height'],
159
-			'crop' => $_wp_additional_image_sizes[ $size ]['crop'],
157
+			'width' => $_wp_additional_image_sizes[$size]['width'],
158
+			'height' => $_wp_additional_image_sizes[$size]['height'],
159
+			'crop' => $_wp_additional_image_sizes[$size]['crop'],
160 160
 		);
161 161
 	}
162 162
 
@@ -183,33 +183,33 @@  discard block
 block discarded – undo
183 183
  * @return array An array of arrays that has extracted the URL's inferred w',
184 184
  * 'h', and 'type'
185 185
  */
186
-function imgix_extract_img_details( $content ) {
187
-	preg_match_all( '/-([0-9]+)x([0-9]+)\.([^"\']+)/', $content, $matches );
186
+function imgix_extract_img_details($content) {
187
+	preg_match_all('/-([0-9]+)x([0-9]+)\.([^"\']+)/', $content, $matches);
188 188
 
189
-	$lookup = array( 'raw', 'w', 'h', 'type' );
189
+	$lookup = array('raw', 'w', 'h', 'type');
190 190
 	$data = array();
191 191
 
192
-	foreach ( $matches as $k => $v ) {
192
+	foreach ($matches as $k => $v) {
193 193
 
194
-		foreach ( $v as $index => $value ) {
194
+		foreach ($v as $index => $value) {
195 195
 
196
-			if ( ! array_key_exists( $index, $data ) ) {
197
-				$data[ $index ] = array();
196
+			if ( ! array_key_exists($index, $data)) {
197
+				$data[$index] = array();
198 198
 			}
199 199
 
200
-			$key = $lookup[ $k ];
200
+			$key = $lookup[$k];
201 201
 
202
-			if ( $key === 'type' ) {
203
-				if ( strpos( $value, '?' ) !== false ) {
204
-					$parts = explode( '?', $value );
205
-					$data[ $index ]['type'] = $parts[0];
206
-					$data[ $index ]['extra'] = $parts[1];
202
+			if ($key === 'type') {
203
+				if (strpos($value, '?') !== false) {
204
+					$parts = explode('?', $value);
205
+					$data[$index]['type'] = $parts[0];
206
+					$data[$index]['extra'] = $parts[1];
207 207
 				} else {
208
-					$data[ $index ]['type'] = $value;
209
-					$data[ $index ]['extra'] = '';
208
+					$data[$index]['type'] = $value;
209
+					$data[$index]['extra'] = '';
210 210
 				}
211 211
 			} else {
212
-				$data[ $index ][ $key ] = $value;
212
+				$data[$index][$key] = $value;
213 213
 			}
214 214
 		}
215 215
 	}
@@ -227,26 +227,26 @@  discard block
 block discarded – undo
227 227
  * @return array An array countaining the final string, and a boolean value
228 228
  * indicating if it's different from the given input string.
229 229
  */
230
-function replace_host( $str, $require_prefix = false ) {
230
+function replace_host($str, $require_prefix = false) {
231 231
 	global $imgix_options;
232 232
 
233
-	if ( ! isset( $imgix_options['cdn_link'] ) || ! $imgix_options['cdn_link'] ) {
234
-		return array( $str, false );
233
+	if ( ! isset($imgix_options['cdn_link']) || ! $imgix_options['cdn_link']) {
234
+		return array($str, false);
235 235
 	}
236 236
 
237
-	$new_host = ensure_valid_url( $imgix_options['cdn_link'] );
238
-	if ( ! $new_host ) {
239
-		return array( $str, false );
237
+	$new_host = ensure_valid_url($imgix_options['cdn_link']);
238
+	if ( ! $new_host) {
239
+		return array($str, false);
240 240
 	}
241 241
 
242 242
 	// As soon as srcset is supported…
243 243
 	// $prefix = $require_prefix? 'srcs?e?t?=[\'"]|,[\S+\n\r\s]*': '';
244
-	$prefix = $require_prefix? 'src=[\'"]': '';
245
-	$src = '(' . preg_quote( home_url( '/' ), '/' ) . '|\/\/)';
244
+	$prefix = $require_prefix ? 'src=[\'"]' : '';
245
+	$src = '(' . preg_quote(home_url('/'), '/') . '|\/\/)';
246 246
 	$patt = '/(' . $prefix . ' )' . $src . '/i';
247
-	$str = preg_replace( $patt, '$1' . $new_host, $str, -1, $count );
247
+	$str = preg_replace($patt, '$1' . $new_host, $str, -1, $count);
248 248
 
249
-	return array( $str, (boolean) $count );
249
+	return array($str, (boolean) $count);
250 250
 }
251 251
 
252 252
 /**
@@ -258,32 +258,32 @@  discard block
 block discarded – undo
258 258
  *
259 259
  * @return string equivalent imgix source with correct parameters.
260 260
  */
261
-function replace_src( $src, $size ) {
262
-	$size_info = get_size_info( $size );
261
+function replace_src($src, $size) {
262
+	$size_info = get_size_info($size);
263 263
 
264
-	if ( $size_info ) {
264
+	if ($size_info) {
265 265
 
266
-		list( $src, $match_src ) = replace_host( $src, false );
266
+		list($src, $match_src) = replace_host($src, false);
267 267
 
268
-		if ( $match_src ) {
268
+		if ($match_src) {
269 269
 			$g_params = get_global_params_string();
270 270
 			$params = array();
271 271
 
272
-			if ( isset( $size_info['crop'] ) && $size_info['crop'] ) {
273
-				array_push( $params, 'fit=crop' );
272
+			if (isset($size_info['crop']) && $size_info['crop']) {
273
+				array_push($params, 'fit=crop');
274 274
 			}
275 275
 
276
-			if ( isset( $size_info['width'] ) && $size_info['width'] ) {
277
-				array_push( $params, 'w=' . $size_info['width'] );
276
+			if (isset($size_info['width']) && $size_info['width']) {
277
+				array_push($params, 'w=' . $size_info['width']);
278 278
 			}
279 279
 
280
-			if ( isset( $size_info['height'] ) && $size_info['height'] ) {
281
-				array_push( $params, 'h=' . $size_info['height'] );
280
+			if (isset($size_info['height']) && $size_info['height']) {
281
+				array_push($params, 'h=' . $size_info['height']);
282 282
 			}
283 283
 
284
-			$p = implode( '&amp;', $params );
285
-			$p = ( $p && $g_params ) ? $p . '&amp;' . $g_params : $p . $g_params;
286
-			$src = apply_parameters_to_url( $src, $p, $src );
284
+			$p = implode('&amp;', $params);
285
+			$p = ($p && $g_params) ? $p . '&amp;' . $g_params : $p . $g_params;
286
+			$src = apply_parameters_to_url($src, $p, $src);
287 287
 		}
288 288
 	}
289 289
 
@@ -297,24 +297,24 @@  discard block
 block discarded – undo
297 297
  *
298 298
  * @return string
299 299
  */
300
-function imgix_file_url( $url ) {
300
+function imgix_file_url($url) {
301 301
 	global $imgix_options;
302 302
 
303 303
 	$imgix_url = $imgix_options['cdn_link'];
304
-	$file = pathinfo( $url );
304
+	$file = pathinfo($url);
305 305
 
306
-	if ( ! $imgix_url ) {
306
+	if ( ! $imgix_url) {
307 307
 		return $url;
308 308
 	}
309 309
 
310
-	if ( in_array( $file['extension'], array( 'jpg', 'gif', 'png', 'jpeg' ) ) ) {
311
-		return str_replace( get_bloginfo( 'wpurl' ), $imgix_url, $url );
310
+	if (in_array($file['extension'], array('jpg', 'gif', 'png', 'jpeg'))) {
311
+		return str_replace(get_bloginfo('wpurl'), $imgix_url, $url);
312 312
 	}
313 313
 
314 314
 	return $url;
315 315
 }
316
-add_filter( 'wp_get_attachment_url', 'imgix_file_url' );
317
-add_filter( 'imgix/add-image-url', 'imgix_file_url' );
316
+add_filter('wp_get_attachment_url', 'imgix_file_url');
317
+add_filter('imgix/add-image-url', 'imgix_file_url');
318 318
 
319 319
 /**
320 320
  * Replace srcset urls with imgix urls.
@@ -324,14 +324,14 @@  discard block
 block discarded – undo
324 324
  * @return array $sources
325 325
  *
326 326
  */
327
-function imgix_cdn_srcset( $sources ) {
328
-	foreach ( $sources as $source ) {
329
-		$sources[ $source['value'] ]['url'] = apply_filters( 'imgix/add-image-url', $sources[ $source['value'] ]['url'] );
327
+function imgix_cdn_srcset($sources) {
328
+	foreach ($sources as $source) {
329
+		$sources[$source['value']]['url'] = apply_filters('imgix/add-image-url', $sources[$source['value']]['url']);
330 330
 	}
331 331
 
332 332
 	return $sources;
333 333
 }
334
-add_filter( 'wp_calculate_image_srcset', 'imgix_cdn_srcset', 10 );
334
+add_filter('wp_calculate_image_srcset', 'imgix_cdn_srcset', 10);
335 335
 
336 336
 /**
337 337
  * Replace image urls in content with imgix urls.
@@ -340,27 +340,27 @@  discard block
 block discarded – undo
340 340
  *
341 341
  * @return string
342 342
  */
343
-function imgix_replace_non_wp_images( $content ) {
344
-	list( $content, $match ) = replace_host( $content, true );
343
+function imgix_replace_non_wp_images($content) {
344
+	list($content, $match) = replace_host($content, true);
345 345
 
346
-	if ( $match ) {
346
+	if ($match) {
347 347
 		// Apply image-tag-encoded params for every image in $content.
348
-		foreach ( imgix_extract_img_details( $content ) as $img ) {
348
+		foreach (imgix_extract_img_details($content) as $img) {
349 349
 			$to_replace = $img['raw'];
350 350
 			$extra_params = $img['extra'] ? '&amp;' . $img['extra'] : '';
351 351
 			$new_url = '.' . $img['type'] . '?h=' . $img['h'] . '&amp;w=' . $img['w'] . $extra_params;
352
-			$content = str_replace( $to_replace, $new_url, $content );
352
+			$content = str_replace($to_replace, $new_url, $content);
353 353
 		}
354 354
 
355 355
 		// Apply global parameters.
356 356
 		$g_params = get_global_params_string();
357
-		foreach ( imgix_extract_imgs( $content ) as $img_url ) {
358
-			$content = apply_parameters_to_url( $img_url, $g_params, $content );
357
+		foreach (imgix_extract_imgs($content) as $img_url) {
358
+			$content = apply_parameters_to_url($img_url, $g_params, $content);
359 359
 		}
360 360
 	}
361 361
 	return $content;
362 362
 }
363
-add_filter( 'the_content', 'imgix_replace_non_wp_images' );
363
+add_filter('the_content', 'imgix_replace_non_wp_images');
364 364
 
365 365
 /**
366 366
  * Add tag to prefetch imgix url to head.
@@ -368,20 +368,20 @@  discard block
 block discarded – undo
368 368
 function imgix_wp_head() {
369 369
 	global $imgix_options;
370 370
 
371
-	if ( isset( $imgix_options['cdn_link'] ) && $imgix_options['cdn_link'] ) {
372
-		printf( "<link rel='dns-prefetch' href='%s'/>",
373
-			esc_url( preg_replace( '/^https?:/', '', untrailingslashit( $imgix_options['cdn_link'] ) ) )
371
+	if (isset($imgix_options['cdn_link']) && $imgix_options['cdn_link']) {
372
+		printf("<link rel='dns-prefetch' href='%s'/>",
373
+			esc_url(preg_replace('/^https?:/', '', untrailingslashit($imgix_options['cdn_link'])))
374 374
 		);
375 375
 	}
376 376
 }
377
-add_action( 'wp_head', 'imgix_wp_head', 1 );
377
+add_action('wp_head', 'imgix_wp_head', 1);
378 378
 
379
-if ( isset( $imgix_options['add_dpi2_srcset'] ) && $imgix_options['add_dpi2_srcset'] ) {
379
+if (isset($imgix_options['add_dpi2_srcset']) && $imgix_options['add_dpi2_srcset']) {
380 380
 	function buffer_start() {
381
-		ob_start( 'add_retina' ); }
381
+		ob_start('add_retina'); }
382 382
 	function buffer_end() {
383 383
 		ob_end_flush(); }
384
-	add_action( 'after_setup_theme', 'buffer_start' );
385
-	add_action( 'shutdown', 'buffer_end' );
386
-	add_filter( 'the_content', 'add_retina' );
384
+	add_action('after_setup_theme', 'buffer_start');
385
+	add_action('shutdown', 'buffer_end');
386
+	add_filter('the_content', 'add_retina');
387 387
 }
Please login to merge, or discard this patch.