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