| 1 |
<?php |
| 2 |
namespace Upress\EzCache\FileOptimizer; |
| 3 |
|
| 4 |
use MatthiasMullie\Minify\CSS; |
| 5 |
use Upress\EzCache\Cache; |
| 6 |
|
| 7 |
class CssMinifier extends BaseFileOptimizer { |
| 8 |
static $FILE_TYPE = 'css'; |
| 9 |
|
| 10 |
protected $settings; |
| 11 |
protected $minify_base_path; |
| 12 |
protected $minify_base_url; |
| 13 |
|
| 14 |
function __construct( $cache_dir, $cache_url ) { |
| 15 |
$this->minify_base_path = $cache_dir; |
| 16 |
$this->minify_base_url = $cache_url; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Minifies CSS files |
| 21 |
* |
| 22 |
* @param string $html HTML content. |
| 23 |
* |
| 24 |
* @return string |
| 25 |
*/ |
| 26 |
public function optimize( $html ) { |
| 27 |
$styles = $this->find( '<link\s+([^>]+[\s"\'])?href\s*=\s*[\'"]\s*?([^\'"]+\.css(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>', $html ); |
| 28 |
|
| 29 |
if ( ! $styles ) { |
| 30 |
return $html; |
| 31 |
} |
| 32 |
|
| 33 |
foreach ( $styles as $style ) { |
| 34 |
if ( preg_match( '/(?:-|\.)min.css/iU', $style[2] ) ) { |
| 35 |
continue; |
| 36 |
} |
| 37 |
|
| 38 |
if ( $this->is_external_file( $style[2] ) ) { |
| 39 |
continue; |
| 40 |
} |
| 41 |
|
| 42 |
if ( $this->is_minify_excluded_file( $style ) ) { |
| 43 |
continue; |
| 44 |
} |
| 45 |
|
| 46 |
$minify_url = $this->replace_url( $style[2] ); |
| 47 |
|
| 48 |
if ( ! $minify_url ) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
$replace_style = str_replace( $style[2], $minify_url, $style[0] ); |
| 53 |
$replace_style = str_replace( '<link', '<link data-minify="1"', $replace_style ); |
| 54 |
$html = str_replace( $style[0], $replace_style, $html ); |
| 55 |
} |
| 56 |
|
| 57 |
return $html; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Creates the minify URL if the minification is successful |
| 62 |
* |
| 63 |
* @param string $url Original file URL. |
| 64 |
|
| 65 |
* @return string|bool The minify URL if successful, false otherwise |
| 66 |
*/ |
| 67 |
private function replace_url( $url ) { |
| 68 |
if ( empty( $url ) ) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
$unique_id = md5( $url ); |
| 73 |
$filename = preg_replace( '/\.(css)$/', '-' . $unique_id . '.css', ltrim( Cache::realpath( $this->get_url_component( $url, PHP_URL_PATH ) ), '/' ) ); |
| 74 |
|
| 75 |
$minified_file = $this->minify_base_path . $filename; |
| 76 |
$minify_url = $this->minify_base_url . $filename; |
| 77 |
|
| 78 |
if ( file_exists( $minified_file ) ) { |
| 79 |
touch( $minified_file ); |
| 80 |
return $minify_url; |
| 81 |
} |
| 82 |
|
| 83 |
$file_path = $this->get_file_path( $url ); |
| 84 |
|
| 85 |
if ( ! $file_path ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
$minified_content = $this->minify( $file_path ); |
| 90 |
|
| 91 |
if ( ! $minified_content ) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
wp_mkdir_p( dirname( $minified_file ) ); |
| 96 |
$save_minify_file = file_put_contents( $minified_file, $minified_content ); |
| 97 |
|
| 98 |
if ( ! $save_minify_file ) { |
| 99 |
return false; |
| 100 |
} |
| 101 |
|
| 102 |
return $minify_url; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Minifies the content |
| 107 |
* |
| 108 |
* @param string|array $file File to minify. |
| 109 |
* @return string|bool Minified content, false if empty |
| 110 |
*/ |
| 111 |
protected function minify( $file ) { |
| 112 |
$file_content = file_get_contents( $file ); |
| 113 |
|
| 114 |
if ( ! $file_content ) { |
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
$file_content = $this->rewrite_paths( $file, $file_content ); |
| 119 |
$minifier = new CSS( $file_content ); |
| 120 |
$minified_content = $minifier->minify(); |
| 121 |
|
| 122 |
if ( empty( $minified_content ) ) { |
| 123 |
return false; |
| 124 |
} |
| 125 |
|
| 126 |
return $minified_content; |
| 127 |
} |
| 128 |
} |
| 129 |
|