Completed
Branch plugin-submission (ccb0d8)
by LA
01:53
created
includes/do-functions.php 1 patch
Spacing   +82 added lines, -82 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 ( $result ) {
112
-		return trailingslashit( $result );
111
+	if ($result) {
112
+		return trailingslashit($result);
113 113
 	}
114 114
 
115 115
 	return null;
@@ -133,33 +133,33 @@  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
-	foreach ( $matches as $k => $v ) {
142
+	foreach ($matches as $k => $v) {
143 143
 
144
-		foreach ( $v as $index => $value ) {
144
+		foreach ($v as $index => $value) {
145 145
 
146
-			if ( ! array_key_exists( $index, $data ) ) {
147
-				$data[ $index ] = array();
146
+			if ( ! array_key_exists($index, $data)) {
147
+				$data[$index] = array();
148 148
 			}
149 149
 
150
-			$key = $lookup[ $k ];
150
+			$key = $lookup[$k];
151 151
 
152
-			if ( $key === 'type' ) {
153
-				if ( strpos( $value, '?' ) !== false ) {
154
-					$parts = explode( '?', $value );
155
-					$data[ $index ]['type'] = $parts[0];
156
-					$data[ $index ]['extra'] = $parts[1];
152
+			if ($key === 'type') {
153
+				if (strpos($value, '?') !== false) {
154
+					$parts = explode('?', $value);
155
+					$data[$index]['type'] = $parts[0];
156
+					$data[$index]['extra'] = $parts[1];
157 157
 				} else {
158
-					$data[ $index ]['type'] = $value;
159
-					$data[ $index ]['extra'] = '';
158
+					$data[$index]['type'] = $value;
159
+					$data[$index]['extra'] = '';
160 160
 				}
161 161
 			} else {
162
-				$data[ $index ][ $key ] = $value;
162
+				$data[$index][$key] = $value;
163 163
 			}
164 164
 		}
165 165
 	}
@@ -174,47 +174,47 @@  discard block
 block discarded – undo
174 174
  * @return array An array countaining the final string, and a boolean value
175 175
  * indicating if it's different from the given input string.
176 176
  */
177
-function imgix_replace_host( $str, $require_prefix = false ) {
177
+function imgix_replace_host($str, $require_prefix = false) {
178 178
 	global $imgix_options;
179 179
 
180
-	if ( ! isset( $imgix_options['cdn_link'] ) || ! $imgix_options['cdn_link'] ) {
181
-		return array( $str, false );
180
+	if ( ! isset($imgix_options['cdn_link']) || ! $imgix_options['cdn_link']) {
181
+		return array($str, false);
182 182
 	}
183 183
 
184
-	$new_host = imgix_ensure_valid_url( $imgix_options['cdn_link'] );
185
-	if ( ! $new_host ) {
186
-		return array( $str, false );
184
+	$new_host = imgix_ensure_valid_url($imgix_options['cdn_link']);
185
+	if ( ! $new_host) {
186
+		return array($str, false);
187 187
 	}
188 188
 
189 189
 	// As soon as srcset is supported…
190 190
 	// $prefix = $require_prefix? 'srcs?e?t?=[\'"]|,[\S+\n\r\s]*': '';
191
-	$prefix = $require_prefix? 'src=[\'"]': '';
192
-	$src = '(' . preg_quote( home_url( '/' ), '/' ) . '|\/\/)';
191
+	$prefix = $require_prefix ? 'src=[\'"]' : '';
192
+	$src = '(' . preg_quote(home_url('/'), '/') . '|\/\/)';
193 193
 	$patt = '/(' . $prefix . ' )' . $src . '/i';
194
-	$str = preg_replace( $patt, '$1' . $new_host, $str, -1, $count );
194
+	$str = preg_replace($patt, '$1' . $new_host, $str, -1, $count);
195 195
 
196
-	return array( $str, (boolean) $count );
196
+	return array($str, (boolean) $count);
197 197
 }
198 198
 
199
-function imgix_file_url( $url ) {
199
+function imgix_file_url($url) {
200 200
 
201 201
 	global $imgix_options;
202 202
 
203 203
 	$imgix_url = $imgix_options['cdn_link'];
204
-	$file = pathinfo( $url );
204
+	$file = pathinfo($url);
205 205
 
206
-	if ( ! $imgix_url ) {
206
+	if ( ! $imgix_url) {
207 207
 		return $url;
208 208
 	}
209 209
 
210
-	if ( in_array( $file['extension'], array( 'jpg', 'gif', 'png', 'jpeg' ) ) ) {
211
-		return str_replace( get_bloginfo( 'wpurl' ), $imgix_url, $url );
210
+	if (in_array($file['extension'], array('jpg', 'gif', 'png', 'jpeg'))) {
211
+		return str_replace(get_bloginfo('wpurl'), $imgix_url, $url);
212 212
 	}
213 213
 
214 214
 	return $url;
215 215
 }
216
-add_filter( 'wp_get_attachment_url', 'imgix_file_url' );
217
-add_filter( 'imgix/add-image-url', 'imgix_file_url' );
216
+add_filter('wp_get_attachment_url', 'imgix_file_url');
217
+add_filter('imgix/add-image-url', 'imgix_file_url');
218 218
 
219 219
 /**
220 220
  *
@@ -226,67 +226,67 @@  discard block
 block discarded – undo
226 226
  *
227 227
  * @return array $sources
228 228
  */
229
-function imgix_cdn_srcset( $sources, $size_array, $image_src, $image_meta, $attachment_id ) {
229
+function imgix_cdn_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) {
230 230
 
231 231
 	global $imgix_options;
232 232
 
233 233
 	$imgix_url = $imgix_options['cdn_link'];
234 234
 
235
-	foreach ( $sources as $source ) {
235
+	foreach ($sources as $source) {
236 236
 
237
-		$sources[ $source['value'] ]['url'] = apply_filters( 'imgix/add-image-url', $sources[ $source['value'] ]['url'] );
237
+		$sources[$source['value']]['url'] = apply_filters('imgix/add-image-url', $sources[$source['value']]['url']);
238 238
 
239 239
 	}
240 240
 
241 241
 	return $sources;
242 242
 }
243
-add_filter( 'wp_calculate_image_srcset', 'imgix_cdn_srcset', 10, 5 );
243
+add_filter('wp_calculate_image_srcset', 'imgix_cdn_srcset', 10, 5);
244 244
 
245
-function imgix_replace_non_wp_images( $content ) {
246
-	list( $content, $match ) = imgix_replace_host( $content, true );
245
+function imgix_replace_non_wp_images($content) {
246
+	list($content, $match) = imgix_replace_host($content, true);
247 247
 
248
-	if ( $match ) {
248
+	if ($match) {
249 249
 		// Apply image-tag-encoded params for every image in $content.
250
-		foreach ( imgix_extract_img_details( $content ) as $img ) {
250
+		foreach (imgix_extract_img_details($content) as $img) {
251 251
 			$to_replace = $img['raw'];
252 252
 			$extra_params = $img['extra'] ? '&amp;' . $img['extra'] : '';
253 253
 			$new_url = '.' . $img['type'] . '?h=' . $img['h'] . '&amp;w=' . $img['w'] . $extra_params;
254
-			$content = str_replace( $to_replace, $new_url, $content );
254
+			$content = str_replace($to_replace, $new_url, $content);
255 255
 		}
256 256
 
257 257
 		// Apply global parameters.
258 258
 		$g_params = imgix_get_global_params_string();
259
-		foreach ( imgix_extract_imgs( $content ) as $img_url ) {
260
-			$content = imgix_apply_parameters_to_url( $img_url, $g_params, $content );
259
+		foreach (imgix_extract_imgs($content) as $img_url) {
260
+			$content = imgix_apply_parameters_to_url($img_url, $g_params, $content);
261 261
 		}
262 262
 	}
263 263
 	return $content;
264 264
 }
265 265
 
266
-add_filter( 'the_content', 'imgix_replace_non_wp_images' );
266
+add_filter('the_content', 'imgix_replace_non_wp_images');
267 267
 
268 268
 function imgix_wp_head() {
269 269
 	global $imgix_options;
270 270
 
271
-	if ( isset( $imgix_options['cdn_link'] ) && $imgix_options['cdn_link'] ) {
272
-		printf( "<link rel='dns-prefetch' href='%s'/>",
273
-			esc_url( preg_replace( '/^https?:/', '', untrailingslashit( $imgix_options['cdn_link'] ) ) )
271
+	if (isset($imgix_options['cdn_link']) && $imgix_options['cdn_link']) {
272
+		printf("<link rel='dns-prefetch' href='%s'/>",
273
+			esc_url(preg_replace('/^https?:/', '', untrailingslashit($imgix_options['cdn_link'])))
274 274
 		);
275 275
 	}
276 276
 }
277 277
 
278
-add_action( 'wp_head', 'imgix_wp_head', 1 );
278
+add_action('wp_head', 'imgix_wp_head', 1);
279 279
 
280
-if ( isset( $imgix_options['add_dpi2_srcset'] ) && $imgix_options['add_dpi2_srcset'] ) {
280
+if (isset($imgix_options['add_dpi2_srcset']) && $imgix_options['add_dpi2_srcset']) {
281 281
 	function imgix_buffer_start() {
282
-		ob_start( 'imgix_add_retina' );
282
+		ob_start('imgix_add_retina');
283 283
 	}
284 284
 
285 285
 	function imgix_buffer_end() {
286 286
 		ob_end_flush();
287 287
 	}
288 288
 
289
-	add_action( 'after_setup_theme', 'imgix_buffer_start' );
290
-	add_action( 'shutdown', 'imgix_buffer_end' );
291
-	add_filter( 'the_content', 'imgix_add_retina' );
289
+	add_action('after_setup_theme', 'imgix_buffer_start');
290
+	add_action('shutdown', 'imgix_buffer_end');
291
+	add_filter('the_content', 'imgix_add_retina');
292 292
 }
Please login to merge, or discard this patch.