Completed
Push — master ( adffc5...06e7fa )
by Jared
02:44
created
lib/timber-image-helper.php 1 patch
Spacing   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -72,8 +72,8 @@  discard block
 block discarded – undo
72 72
 			$w = $_wp_additional_image_sizes[$size]['width'];
73 73
 			$h = $_wp_additional_image_sizes[$size]['height'];
74 74
 		} else if ( in_array($size, array('thumbnail', 'medium', 'large')) ) {
75
-			$w = get_option($size.'_size_w');
76
-			$h = get_option($size.'_size_h');
75
+			$w = get_option($size . '_size_w');
76
+			$h = get_option($size . '_size_h');
77 77
 		}
78 78
 		if ( isset($w) && isset($h) && ($w || $h) ) {
79 79
 			return array('w' => $w, 'h' => $h);
@@ -106,7 +106,7 @@  discard block
 block discarded – undo
106 106
 			return false;
107 107
 		}
108 108
 		//its a gif so test
109
-		if( !($fh = @fopen($file, 'rb')) ) {
109
+		if ( !($fh = @fopen($file, 'rb')) ) {
110 110
 		  	return false;
111 111
 		}
112 112
 		$count = 0;
@@ -118,7 +118,7 @@  discard block
 block discarded – undo
118 118
 
119 119
 		// We read through the file til we reach the end of the file, or we've found
120 120
 		// at least 2 frame headers
121
-		while(!feof($fh) && $count < 2) {
121
+		while ( !feof($fh) && $count < 2 ) {
122 122
 			$chunk = fread($fh, 1024 * 100); //read 100kb at a time
123 123
 			$count += preg_match_all('#\x00\x21\xF9\x04.{4}\x00[\x2C\x21]#s', $chunk, $matches);
124 124
 	    }
@@ -159,13 +159,13 @@  discard block
 block discarded – undo
159 159
 	 * Deletes all resized versions of an image when the source is deleted
160 160
 	 */
161 161
 	protected static function add_actions() {
162
-		add_action( 'delete_attachment', function ( $post_id ) {
163
-			$post = get_post( $post_id );
164
-			$image_types = array( 'image/jpeg', 'image/png', 'image/gif', 'image/jpg' );
165
-			if ( in_array( $post->post_mime_type, $image_types ) ) {
166
-				$attachment = new TimberImage( $post_id );
162
+		add_action('delete_attachment', function( $post_id ) {
163
+			$post = get_post($post_id);
164
+			$image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/jpg');
165
+			if ( in_array($post->post_mime_type, $image_types) ) {
166
+				$attachment = new TimberImage($post_id);
167 167
 				if ( $attachment->file_loc ) {
168
-					TimberImageHelper::delete_generated_files( $attachment->file_loc );
168
+					TimberImageHelper::delete_generated_files($attachment->file_loc);
169 169
 				}
170 170
 			}
171 171
 		} );
@@ -176,9 +176,9 @@  discard block
 block discarded – undo
176 176
 	 * for example /wp-content or /content
177 177
 	 */
178 178
 	protected static function add_constants() {
179
-		if ( !defined( 'WP_CONTENT_SUBDIR' ) ) {
180
-			$wp_content_path = str_replace( home_url(), '', WP_CONTENT_URL );
181
-			define( 'WP_CONTENT_SUBDIR', $wp_content_path );
179
+		if ( !defined('WP_CONTENT_SUBDIR') ) {
180
+			$wp_content_path = str_replace(home_url(), '', WP_CONTENT_URL);
181
+			define('WP_CONTENT_SUBDIR', $wp_content_path);
182 182
 		}
183 183
 	}
184 184
 
@@ -188,8 +188,8 @@  discard block
 block discarded – undo
188 188
 	 * @return void
189 189
 	 */
190 190
 	static function add_filters() {
191
-		add_filter( 'upload_dir', function ( $arr ) {
192
-			$arr['relative'] = str_replace( home_url(), '', $arr['baseurl'] );
191
+		add_filter('upload_dir', function( $arr ) {
192
+			$arr['relative'] = str_replace(home_url(), '', $arr['baseurl']);
193 193
 			return $arr;
194 194
 		} );
195 195
 	}
@@ -201,17 +201,17 @@  discard block
 block discarded – undo
201 201
 	 *	                            or: http://example.org/wp-content/uploads/2015/my-pic.jpg
202 202
 	 */
203 203
 	static function delete_generated_files( $local_file ) {
204
-		if (TimberURLHelper::is_absolute( $local_file ) ) {
205
-			$local_file = TimberURLHelper::url_to_file_system( $local_file );
204
+		if ( TimberURLHelper::is_absolute($local_file) ) {
205
+			$local_file = TimberURLHelper::url_to_file_system($local_file);
206 206
 		}
207
-		$info = pathinfo( $local_file );
207
+		$info = pathinfo($local_file);
208 208
 		$dir = $info['dirname'];
209 209
 		$ext = $info['extension'];
210 210
 		$filename = $info['filename'];
211
-		self::process_delete_generated_files( $filename, $ext, $dir, '-[0-9999999]*', '-[0-9]*x[0-9]*-c-[a-z]*.' );
212
-		self::process_delete_generated_files( $filename, $ext, $dir, '-lbox-[0-9999999]*', '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.' );
213
-		self::process_delete_generated_files( $filename, 'jpg', $dir, '-tojpg.*' );
214
-		self::process_delete_generated_files( $filename, 'jpg', $dir, '-tojpg-[0-9999999]*' );
211
+		self::process_delete_generated_files($filename, $ext, $dir, '-[0-9999999]*', '-[0-9]*x[0-9]*-c-[a-z]*.');
212
+		self::process_delete_generated_files($filename, $ext, $dir, '-lbox-[0-9999999]*', '-lbox-[0-9]*x[0-9]*-[a-zA-Z0-9]*.');
213
+		self::process_delete_generated_files($filename, 'jpg', $dir, '-tojpg.*');
214
+		self::process_delete_generated_files($filename, 'jpg', $dir, '-tojpg-[0-9999999]*');
215 215
 	}
216 216
 
217 217
 	/**
@@ -230,12 +230,12 @@  discard block
 block discarded – undo
230 230
 	 */
231 231
 	protected static function process_delete_generated_files( $filename, $ext, $dir, $search_pattern, $match_pattern = null ) {
232 232
 		$searcher = '/' . $filename . $search_pattern;
233
-		foreach ( glob( $dir . $searcher ) as $found_file ) {
234
-			$regexdir = str_replace( '/', '\/', $dir );
235
-			$pattern = '/' . ( $regexdir ) . '\/' . $filename . $match_pattern . $ext . '/';
236
-			$match = preg_match( $pattern, $found_file );
237
-			if ( ! $match_pattern || $match ) {
238
-				unlink( $found_file );
233
+		foreach ( glob($dir . $searcher) as $found_file ) {
234
+			$regexdir = str_replace('/', '\/', $dir);
235
+			$pattern = '/' . ($regexdir) . '\/' . $filename . $match_pattern . $ext . '/';
236
+			$match = preg_match($pattern, $found_file);
237
+			if ( !$match_pattern || $match ) {
238
+				unlink($found_file);
239 239
 			}
240 240
 		}
241 241
 	}
@@ -249,7 +249,7 @@  discard block
 block discarded – undo
249 249
 	 */
250 250
 	public static function get_server_location( $url ) {
251 251
 		// if we're already an absolute dir, just return
252
-		if ( 0 === strpos( $url, ABSPATH ) ) {
252
+		if ( 0 === strpos($url, ABSPATH) ) {
253 253
 			return $url;
254 254
 		}
255 255
 		// otherwise, analyze URL then build mapping path
@@ -268,11 +268,11 @@  discard block
 block discarded – undo
268 268
 		$upload = wp_upload_dir();
269 269
 		$dir = $upload['path'];
270 270
 		$filename = $file;
271
-		$file = parse_url( $file );
272
-		$path_parts = pathinfo( $file['path'] );
273
-		$basename = md5( $filename );
271
+		$file = parse_url($file);
272
+		$path_parts = pathinfo($file['path']);
273
+		$basename = md5($filename);
274 274
 		$ext = 'jpg';
275
-		if ( isset( $path_parts['extension'] ) ) {
275
+		if ( isset($path_parts['extension']) ) {
276 276
 			$ext = $path_parts['extension'];
277 277
 		}
278 278
 		return $dir . '/' . $basename . '.' . $ext;
@@ -285,27 +285,27 @@  discard block
 block discarded – undo
285 285
 	 * @return string the URL to the downloaded file
286 286
 	 */
287 287
 	public static function sideload_image( $file ) {
288
-		$loc = self::get_sideloaded_file_loc( $file );
289
-		if ( file_exists( $loc ) ) {
290
-			return TimberURLHelper::preslashit( TimberURLHelper::get_rel_path( $loc ) );
288
+		$loc = self::get_sideloaded_file_loc($file);
289
+		if ( file_exists($loc) ) {
290
+			return TimberURLHelper::preslashit(TimberURLHelper::get_rel_path($loc));
291 291
 		}
292 292
 		// Download file to temp location
293
-		if ( !function_exists( 'download_url' ) ) {
293
+		if ( !function_exists('download_url') ) {
294 294
 			require_once ABSPATH . '/wp-admin/includes/file.php';
295 295
 		}
296
-		$tmp = download_url( $file );
297
-		preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
296
+		$tmp = download_url($file);
297
+		preg_match('/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches);
298 298
 		$file_array = array();
299
-		$file_array['name'] = basename( $matches[0] );
299
+		$file_array['name'] = basename($matches[0]);
300 300
 		$file_array['tmp_name'] = $tmp;
301 301
 		// If error storing temporarily, unlink
302
-		if ( is_wp_error( $tmp ) ) {
303
-			@unlink( $file_array['tmp_name'] );
302
+		if ( is_wp_error($tmp) ) {
303
+			@unlink($file_array['tmp_name']);
304 304
 			$file_array['tmp_name'] = '';
305 305
 		}
306 306
 		// do the validation and storage stuff
307
-		$locinfo = pathinfo( $loc );
308
-		$file = wp_upload_bits( $locinfo['basename'], null, file_get_contents( $file_array['tmp_name'] ) );
307
+		$locinfo = pathinfo($loc);
308
+		$file = wp_upload_bits($locinfo['basename'], null, file_get_contents($file_array['tmp_name']));
309 309
 		return $file['url'];
310 310
 	}
311 311
 
@@ -317,7 +317,7 @@  discard block
 block discarded – undo
317 317
 	 * @param  string $url an URL (absolute or relative) pointing to an image
318 318
 	 * @return array       an array (see keys in code below)
319 319
 	 */
320
-	private static function analyze_url($url) {
320
+	private static function analyze_url( $url ) {
321 321
 		$result = array(
322 322
 			'url' => $url, // the initial url
323 323
 			'absolute' => TimberURLHelper::is_absolute($url), // is the url absolute or relative (to home_url)
@@ -332,23 +332,23 @@  discard block
 block discarded – undo
332 332
 		if ( 0 === strpos($tmp, ABSPATH) ) { // we've been given a dir, not an url
333 333
 			$result['absolute'] = true;
334 334
 			if ( 0 === strpos($tmp, $upload_dir['basedir']) ) {
335
-				$result['base']= self::BASE_UPLOADS; // upload based
335
+				$result['base'] = self::BASE_UPLOADS; // upload based
336 336
 				$tmp = str_replace($upload_dir['basedir'], '', $tmp);
337 337
 			}
338 338
 			if ( 0 === strpos($tmp, WP_CONTENT_DIR) ) {
339
-				$result['base']= self::BASE_CONTENT; // content based
339
+				$result['base'] = self::BASE_CONTENT; // content based
340 340
 				$tmp = str_replace(WP_CONTENT_DIR, '', $tmp);
341 341
 			}
342 342
 		} else {
343
-			if (!$result['absolute']) {
344
-				$tmp = home_url().$tmp;
343
+			if ( !$result['absolute'] ) {
344
+				$tmp = home_url() . $tmp;
345 345
 			}
346
-			if (0 === strpos($tmp, $upload_dir['baseurl'])) {
347
-				$result['base']= self::BASE_UPLOADS; // upload based
346
+			if ( 0 === strpos($tmp, $upload_dir['baseurl']) ) {
347
+				$result['base'] = self::BASE_UPLOADS; // upload based
348 348
 				$tmp = str_replace($upload_dir['baseurl'], '', $tmp);
349 349
 			}
350
-			if (0 === strpos($tmp, content_url())) {
351
-				$result['base']= self::BASE_CONTENT; // content-based
350
+			if ( 0 === strpos($tmp, content_url()) ) {
351
+				$result['base'] = self::BASE_CONTENT; // content-based
352 352
 				$tmp = str_replace(content_url(), '', $tmp);
353 353
 			}
354 354
 		}
@@ -370,20 +370,20 @@  discard block
 block discarded – undo
370 370
 	 * @param  bool   $absolute should the returned URL be absolute (include protocol+host), or relative
371 371
 	 * @return string           the URL
372 372
 	 */
373
-	private static function _get_file_url($base, $subdir, $filename, $absolute) {
373
+	private static function _get_file_url( $base, $subdir, $filename, $absolute ) {
374 374
 		$url = '';
375
-		if( self::BASE_UPLOADS == $base ) {
375
+		if ( self::BASE_UPLOADS == $base ) {
376 376
 			$upload_dir = wp_upload_dir();
377 377
 			$url = $upload_dir['baseurl'];
378 378
 		}
379
-		if( self::BASE_CONTENT == $base ) {
379
+		if ( self::BASE_CONTENT == $base ) {
380 380
 			$url = content_url();
381 381
 		}
382
-		if(!empty($subdir)) {
382
+		if ( !empty($subdir) ) {
383 383
 			$url .= $subdir;
384 384
 		}
385
-		$url .= '/'.$filename;
386
-		if(!$absolute) {
385
+		$url .= '/' . $filename;
386
+		if ( !$absolute ) {
387 387
 			$url = str_replace(home_url(), '', $url);
388 388
 		}
389 389
 		// $url = TimberURLHelper::remove_double_slashes( $url);
@@ -398,19 +398,19 @@  discard block
 block discarded – undo
398 398
 	 * @param  string $filename file name, including extension (but no path)
399 399
 	 * @return string           the file location
400 400
 	 */
401
-	private static function _get_file_path($base, $subdir, $filename) {
401
+	private static function _get_file_path( $base, $subdir, $filename ) {
402 402
 		$path = '';
403
-		if(self::BASE_UPLOADS == $base) {
403
+		if ( self::BASE_UPLOADS == $base ) {
404 404
 			$upload_dir = wp_upload_dir();
405 405
 			$path = $upload_dir['basedir'];
406 406
 		}
407
-		if(self::BASE_CONTENT == $base) {
407
+		if ( self::BASE_CONTENT == $base ) {
408 408
 			$path = WP_CONTENT_DIR;
409 409
 		}
410
-		if(!empty($subdir)) {
410
+		if ( !empty($subdir) ) {
411 411
 			$path .= $subdir;
412 412
 		}
413
-		$path .= '/'.$filename;
413
+		$path .= '/' . $filename;
414 414
 		return $path;
415 415
 	}
416 416
 
@@ -429,14 +429,14 @@  discard block
 block discarded – undo
429 429
 	 *
430 430
 	 */
431 431
 	private static function _operate( $src, $op, $force = false ) {
432
-		if ( empty( $src ) ) {
432
+		if ( empty($src) ) {
433 433
 			return '';
434 434
 		}
435 435
 		$external = false;
436 436
 
437 437
 		// if external image, load it first
438
-		if ( TimberURLHelper::is_external_content( $src ) ) {
439
-			$src = self::sideload_image( $src );
438
+		if ( TimberURLHelper::is_external_content($src) ) {
439
+			$src = self::sideload_image($src);
440 440
 			$external = true;
441 441
 		}
442 442
 		// break down URL into components
@@ -459,19 +459,19 @@  discard block
 block discarded – undo
459 459
 			$au['basename']
460 460
 		);
461 461
 		// if already exists...
462
-		if ( file_exists( $new_server_path ) ) {
462
+		if ( file_exists($new_server_path) ) {
463 463
 			if ( $force ) {
464 464
 				// Force operation - warning: will regenerate the image on every pageload, use for testing purposes only!
465
-				unlink( $new_server_path );
465
+				unlink($new_server_path);
466 466
 			} else {
467 467
 				// return existing file (caching)
468 468
 				return $new_url;
469 469
 			}
470 470
 		}
471 471
 		// otherwise generate result file
472
-		if($op->run($old_server_path, $new_server_path)) {
473
-			if( get_class( $op ) === 'TimberImageOperationResize' && $external ) {
474
-				$new_url = strtolower( $new_url );
472
+		if ( $op->run($old_server_path, $new_server_path) ) {
473
+			if ( get_class($op) === 'TimberImageOperationResize' && $external ) {
474
+				$new_url = strtolower($new_url);
475 475
 			}
476 476
 			return $new_url;
477 477
 		} else {
@@ -483,7 +483,7 @@  discard block
 block discarded – undo
483 483
 
484 484
 // -- the below methods are just used for unit testing the URL generation code
485 485
 //
486
-	static function get_letterbox_file_url($url, $w, $h, $color) {
486
+	static function get_letterbox_file_url( $url, $w, $h, $color ) {
487 487
 		$au = self::analyze_url($url);
488 488
 		$op = new TimberImageOperationLetterbox($w, $h, $color);
489 489
 		$new_url = self::_get_file_url(
@@ -494,7 +494,7 @@  discard block
 block discarded – undo
494 494
 		);
495 495
 		return $new_url;
496 496
 	}
497
-	public static function get_letterbox_file_path($url, $w, $h, $color ) {
497
+	public static function get_letterbox_file_path( $url, $w, $h, $color ) {
498 498
 		$au = self::analyze_url($url);
499 499
 		$op = new TimberImageOperationLetterbox($w, $h, $color);
500 500
 		$new_path = self::_get_file_path(
@@ -504,7 +504,7 @@  discard block
 block discarded – undo
504 504
 		);
505 505
 		return $new_path;
506 506
 	}
507
-	static function get_resize_file_url($url, $w, $h, $crop) {
507
+	static function get_resize_file_url( $url, $w, $h, $crop ) {
508 508
 		$au = self::analyze_url($url);
509 509
 		$op = new TimberImageOperationResize($w, $h, $crop);
510 510
 		$new_url = self::_get_file_url(
@@ -515,7 +515,7 @@  discard block
 block discarded – undo
515 515
 		);
516 516
 		return $new_url;
517 517
 	}
518
-	static function get_resize_file_path($url, $w, $h, $crop) {
518
+	static function get_resize_file_path( $url, $w, $h, $crop ) {
519 519
 		$au = self::analyze_url($url);
520 520
 		$op = new TimberImageOperationResize($w, $h, $crop);
521 521
 		$new_path = self::_get_file_path(
Please login to merge, or discard this patch.