Completed
Branch plugin-submission (f13429)
by LA
03:13 queued 01:12
created
includes/do-functions.php 1 patch
Spacing   +83 added lines, -83 removed lines patch added patch discarded remove patch
@@ -12,13 +12,13 @@  discard block
 block discarded – undo
12 12
  * @param $content
13 13
  * @return string Content with retina-enriched image tags.
14 14
  */
15
-function imgix_add_retina( $content ) {
15
+function imgix_add_retina($content) {
16 16
 	$pattern = '/<img((?![^>]+srcset )([^>]*)';
17 17
 	$pattern .= 'src=[\'"]([^\'"]*imgix.net[^\'"]*\?[^\'"]*w=[^\'"]*)[\'"]([^>]*)*?)>/i';
18 18
 	$repl = '<img$2src="$3" srcset="${3}, ${3}&amp;dpr=2 2x, ${3}&amp;dpr=3 3x,"$4>';
19
-	$content = preg_replace( $pattern, $repl, $content );
19
+	$content = preg_replace($pattern, $repl, $content);
20 20
 
21
-	return preg_replace( $pattern, $repl, $content );
21
+	return preg_replace($pattern, $repl, $content);
22 22
 }
23 23
 
24 24
 /**
@@ -26,13 +26,13 @@  discard block
 block discarded – undo
26 26
  *
27 27
  * @return array An array of matching arrays with two keys: 'url' and 'params'
28 28
  */
29
-function imgix_extract_imgs( $content ) {
30
-	preg_match_all( '/src=["\']http.+\/([^\s]+?)["\']/', $content, $matches );
29
+function imgix_extract_imgs($content) {
30
+	preg_match_all('/src=["\']http.+\/([^\s]+?)["\']/', $content, $matches);
31 31
 	$results = array();
32 32
 
33
-	if ( $matches ) {
34
-		foreach ( $matches[1] as $url ) {
35
-			array_push( $results, $url );
33
+	if ($matches) {
34
+		foreach ($matches[1] as $url) {
35
+			array_push($results, $url);
36 36
 		}
37 37
 	}
38 38
 
@@ -46,17 +46,17 @@  discard block
 block discarded – undo
46 46
  *
47 47
  * @return string Content with matching URLs having the new querystrings.
48 48
  */
49
-function imgix_apply_parameters_to_url( $url, $params, $content ) {
50
-	$parts = explode( '?', $url . '?' );
49
+function imgix_apply_parameters_to_url($url, $params, $content) {
50
+	$parts = explode('?', $url . '?');
51 51
 
52
-	list( $base_url, $base_params ) = array( $parts[0], $parts[1] );
52
+	list($base_url, $base_params) = array($parts[0], $parts[1]);
53 53
 
54 54
 	$new_url = $old_url = $base_url;
55 55
 	$new_url .= '?' . $params;
56 56
 	$new_url .= $base_params ? '&amp;' . $base_params : '';
57 57
 	$old_url .= $base_params ? '?' . $base_params : '';
58 58
 
59
-	return str_replace( $old_url, $new_url, $content );
59
+	return str_replace($old_url, $new_url, $content);
60 60
 }
61 61
 
62 62
 /**
@@ -71,19 +71,19 @@  discard block
 block discarded – undo
71 71
 	// For now, only "auto" is supported.
72 72
 	$auto = array();
73 73
 
74
-	if ( isset( $imgix_options['auto_format'] ) && $imgix_options['auto_format'] ) {
75
-		array_push( $auto, 'format' );
74
+	if (isset($imgix_options['auto_format']) && $imgix_options['auto_format']) {
75
+		array_push($auto, 'format');
76 76
 	}
77 77
 
78
-	if ( isset( $imgix_options['auto_enhance'] ) && $imgix_options['auto_enhance'] ) {
79
-		array_push( $auto, 'enhance' );
78
+	if (isset($imgix_options['auto_enhance']) && $imgix_options['auto_enhance']) {
79
+		array_push($auto, 'enhance');
80 80
 	}
81 81
 
82
-	if ( ! empty( $auto ) ) {
83
-		array_push( $params, 'auto=' . implode( '%2C', $auto ) );
82
+	if ( ! empty($auto)) {
83
+		array_push($params, 'auto=' . implode('%2C', $auto));
84 84
 	}
85 85
 
86
-	return implode( '&amp;', $params );
86
+	return implode('&amp;', $params);
87 87
 }
88 88
 
89 89
 /**
@@ -92,24 +92,24 @@  discard block
 block discarded – undo
92 92
  *
93 93
  * @return string A sanitized URL.
94 94
  */
95
-function imgix_ensure_valid_url( $url ) {
96
-	$slash = strpos( $url, '//' ) == 0 ? '//' : '';
95
+function imgix_ensure_valid_url($url) {
96
+	$slash = strpos($url, '//') == 0 ? '//' : '';
97 97
 
98
-	if ( $slash ) {
99
-		$url = substr( $url, 2 );
98
+	if ($slash) {
99
+		$url = substr($url, 2);
100 100
 	}
101 101
 
102
-	$urlp = wp_parse_url( $url );
103
-	$pref = array_key_exists( 'scheme', $urlp ) ? $urlp['scheme'] . '://' : $slash;
102
+	$urlp = wp_parse_url($url);
103
+	$pref = array_key_exists('scheme', $urlp) ? $urlp['scheme'] . '://' : $slash;
104 104
 
105
-	if ( ! $slash && strpos( $pref, 'http' ) !== 0 ) {
105
+	if ( ! $slash && strpos($pref, 'http') !== 0) {
106 106
 		$pref = 'http://';
107 107
 	}
108 108
 
109 109
 	$result = $urlp['host'] ? $pref . $urlp['host'] : false;
110 110
 
111
-	if ( false !== $result ) {
112
-		return trailingslashit( $result );
111
+	if (false !== $result) {
112
+		return trailingslashit($result);
113 113
 	}
114 114
 
115 115
 	return null;
@@ -133,34 +133,34 @@  discard block
 block discarded – undo
133 133
  * @return array An array of arrays that has extracted the URL's inferred w',
134 134
  * 'h', and 'type'
135 135
  */
136
-function imgix_extract_img_details( $content ) {
137
-	preg_match_all( '/-([0-9]+)x([0-9]+)\.([^"\']+)/', $content, $matches );
136
+function imgix_extract_img_details($content) {
137
+	preg_match_all('/-([0-9]+)x([0-9]+)\.([^"\']+)/', $content, $matches);
138 138
 
139
-	$lookup = array( 'raw', 'w', 'h', 'type' );
139
+	$lookup = array('raw', 'w', 'h', 'type');
140 140
 	$data = array();
141 141
 
142
-	if ( is_array( $matches ) ) {
143
-		foreach ( $matches as $k => $v ) {
142
+	if (is_array($matches)) {
143
+		foreach ($matches as $k => $v) {
144 144
 
145
-			foreach ( $v as $index => $value ) {
145
+			foreach ($v as $index => $value) {
146 146
 
147
-				if ( ! array_key_exists( $index, $data ) ) {
148
-					$data[ $index ] = array();
147
+				if ( ! array_key_exists($index, $data)) {
148
+					$data[$index] = array();
149 149
 				}
150 150
 
151
-				$key = $lookup[ $k ];
151
+				$key = $lookup[$k];
152 152
 
153
-				if ( $key === 'type' ) {
154
-					if ( strpos( $value, '?' ) !== false ) {
155
-						$parts = explode( '?', $value );
156
-						$data[ $index ]['type'] = $parts[0];
157
-						$data[ $index ]['extra'] = $parts[1];
153
+				if ($key === 'type') {
154
+					if (strpos($value, '?') !== false) {
155
+						$parts = explode('?', $value);
156
+						$data[$index]['type'] = $parts[0];
157
+						$data[$index]['extra'] = $parts[1];
158 158
 					} else {
159
-						$data[ $index ]['type'] = $value;
160
-						$data[ $index ]['extra'] = '';
159
+						$data[$index]['type'] = $value;
160
+						$data[$index]['extra'] = '';
161 161
 					}
162 162
 				} else {
163
-					$data[ $index ][ $key ] = $value;
163
+					$data[$index][$key] = $value;
164 164
 				}
165 165
 			}
166 166
 		}
@@ -176,47 +176,47 @@  discard block
 block discarded – undo
176 176
  * @return array An array countaining the final string, and a boolean value
177 177
  * indicating if it's different from the given input string.
178 178
  */
179
-function imgix_replace_host( $str, $require_prefix = false ) {
179
+function imgix_replace_host($str, $require_prefix = false) {
180 180
 	global $imgix_options;
181 181
 
182
-	if ( ! isset( $imgix_options['cdn_link'] ) || ! $imgix_options['cdn_link'] ) {
183
-		return array( $str, false );
182
+	if ( ! isset($imgix_options['cdn_link']) || ! $imgix_options['cdn_link']) {
183
+		return array($str, false);
184 184
 	}
185 185
 
186
-	$new_host = imgix_ensure_valid_url( $imgix_options['cdn_link'] );
187
-	if ( ! $new_host ) {
188
-		return array( $str, false );
186
+	$new_host = imgix_ensure_valid_url($imgix_options['cdn_link']);
187
+	if ( ! $new_host) {
188
+		return array($str, false);
189 189
 	}
190 190
 
191 191
 	// As soon as srcset is supported…
192 192
 	// $prefix = $require_prefix? 'srcs?e?t?=[\'"]|,[\S+\n\r\s]*': '';
193
-	$prefix = $require_prefix? 'src=[\'"]': '';
194
-	$src = '(' . preg_quote( home_url( '/' ), '/' ) . '|\/\/)';
193
+	$prefix = $require_prefix ? 'src=[\'"]' : '';
194
+	$src = '(' . preg_quote(home_url('/'), '/') . '|\/\/)';
195 195
 	$patt = '/(' . $prefix . ' )' . $src . '/i';
196
-	$str = preg_replace( $patt, '$1' . $new_host, $str, -1, $count );
196
+	$str = preg_replace($patt, '$1' . $new_host, $str, -1, $count);
197 197
 
198
-	return array( $str, (boolean) $count );
198
+	return array($str, (boolean) $count);
199 199
 }
200 200
 
201
-function imgix_file_url( $url ) {
201
+function imgix_file_url($url) {
202 202
 
203 203
 	global $imgix_options;
204 204
 
205 205
 	$imgix_url = $imgix_options['cdn_link'];
206
-	$file = pathinfo( $url );
206
+	$file = pathinfo($url);
207 207
 
208
-	if ( ! $imgix_url ) {
208
+	if ( ! $imgix_url) {
209 209
 		return $url;
210 210
 	}
211 211
 
212
-	if ( in_array( $file['extension'], array( 'jpg', 'gif', 'png', 'jpeg' ) ) ) {
213
-		return str_replace( get_bloginfo( 'wpurl' ), $imgix_url, $url );
212
+	if (in_array($file['extension'], array('jpg', 'gif', 'png', 'jpeg'))) {
213
+		return str_replace(get_bloginfo('wpurl'), $imgix_url, $url);
214 214
 	}
215 215
 
216 216
 	return $url;
217 217
 }
218
-add_filter( 'wp_get_attachment_url', 'imgix_file_url' );
219
-add_filter( 'imgix/add-image-url', 'imgix_file_url' );
218
+add_filter('wp_get_attachment_url', 'imgix_file_url');
219
+add_filter('imgix/add-image-url', 'imgix_file_url');
220 220
 
221 221
 /**
222 222
  *
@@ -228,63 +228,63 @@  discard block
 block discarded – undo
228 228
  *
229 229
  * @return array $sources
230 230
  */
231
-function imgix_cdn_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
231
+function imgix_cdn_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) {
232 232
 
233
-	foreach ( $sources as $source ) {
233
+	foreach ($sources as $source) {
234 234
 
235
-		$sources[ $source['value'] ]['url'] = apply_filters( 'imgix/add-image-url', $sources[ $source['value'] ]['url'] );
235
+		$sources[$source['value']]['url'] = apply_filters('imgix/add-image-url', $sources[$source['value']]['url']);
236 236
 
237 237
 	}
238 238
 
239 239
 	return $sources;
240 240
 }
241
-add_filter( 'wp_calculate_image_srcset', 'imgix_cdn_srcset', 10, 5 );
241
+add_filter('wp_calculate_image_srcset', 'imgix_cdn_srcset', 10, 5);
242 242
 
243
-function imgix_replace_non_wp_images( $content ) {
244
-	list( $content, $match ) = imgix_replace_host( $content, true );
243
+function imgix_replace_non_wp_images($content) {
244
+	list($content, $match) = imgix_replace_host($content, true);
245 245
 
246
-	if ( $match ) {
246
+	if ($match) {
247 247
 		// Apply image-tag-encoded params for every image in $content.
248
-		foreach ( imgix_extract_img_details( $content ) as $img ) {
248
+		foreach (imgix_extract_img_details($content) as $img) {
249 249
 			$to_replace = $img['raw'];
250 250
 			$extra_params = $img['extra'] ? '&amp;' . $img['extra'] : '';
251 251
 			$new_url = '.' . $img['type'] . '?h=' . $img['h'] . '&amp;w=' . $img['w'] . $extra_params;
252
-			$content = str_replace( $to_replace, $new_url, $content );
252
+			$content = str_replace($to_replace, $new_url, $content);
253 253
 		}
254 254
 
255 255
 		// Apply global parameters.
256 256
 		$g_params = imgix_get_global_params_string();
257
-		foreach ( imgix_extract_imgs( $content ) as $img_url ) {
258
-			$content = imgix_apply_parameters_to_url( $img_url, $g_params, $content );
257
+		foreach (imgix_extract_imgs($content) as $img_url) {
258
+			$content = imgix_apply_parameters_to_url($img_url, $g_params, $content);
259 259
 		}
260 260
 	}
261 261
 	return $content;
262 262
 }
263 263
 
264
-add_filter( 'the_content', 'imgix_replace_non_wp_images' );
264
+add_filter('the_content', 'imgix_replace_non_wp_images');
265 265
 
266 266
 function imgix_wp_head() {
267 267
 	global $imgix_options;
268 268
 
269
-	if ( isset( $imgix_options['cdn_link'] ) && $imgix_options['cdn_link'] ) {
270
-		printf( "<link rel='dns-prefetch' href='%s'/>",
271
-			esc_url( preg_replace( '/^https?:/', '', untrailingslashit( $imgix_options['cdn_link'] ) ) )
269
+	if (isset($imgix_options['cdn_link']) && $imgix_options['cdn_link']) {
270
+		printf("<link rel='dns-prefetch' href='%s'/>",
271
+			esc_url(preg_replace('/^https?:/', '', untrailingslashit($imgix_options['cdn_link'])))
272 272
 		);
273 273
 	}
274 274
 }
275 275
 
276
-add_action( 'wp_head', 'imgix_wp_head', 1 );
276
+add_action('wp_head', 'imgix_wp_head', 1);
277 277
 
278
-if ( isset( $imgix_options['add_dpi2_srcset'] ) && $imgix_options['add_dpi2_srcset'] ) {
278
+if (isset($imgix_options['add_dpi2_srcset']) && $imgix_options['add_dpi2_srcset']) {
279 279
 	function imgix_buffer_start() {
280
-		ob_start( 'imgix_add_retina' );
280
+		ob_start('imgix_add_retina');
281 281
 	}
282 282
 
283 283
 	function imgix_buffer_end() {
284 284
 		ob_end_flush();
285 285
 	}
286 286
 
287
-	add_action( 'after_setup_theme', 'imgix_buffer_start' );
288
-	add_action( 'shutdown', 'imgix_buffer_end' );
289
-	add_filter( 'the_content', 'imgix_add_retina' );
287
+	add_action('after_setup_theme', 'imgix_buffer_start');
288
+	add_action('shutdown', 'imgix_buffer_end');
289
+	add_filter('the_content', 'imgix_add_retina');
290 290
 }
Please login to merge, or discard this patch.