PluginProbe
ezCache / 1.3
ezCache v1.3
2.6.5 2.6.4 2.6.2 2.6.3 2.6.1 2.6.0 2.5.6 2.5.5 2.5.4 2.5.3 2.5.2 2.5.1 2.5 2.2.1 2.2.2 trunk 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.3 1.3.1 1.3.10 1.3.11 All 49 releases
ezcache / includes / FileOptimizer / CssCombiner.php

CssCombiner.php in ezCache 1.3, at includes/FileOptimizer/CssCombiner.php

135 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ( $this->is_external_file( $style[2] ) ) {
37 return '';
38 }
39
40 if ( $this->is_minify_excluded_file( $style ) ) {
41 return '';
42 }
43
44 $style_filepath = $this->get_file_path( $style[2] );
45
46 if ( ! $style_filepath ) {
47 return '';
48 }
49
50 $files[] = $style_filepath;
51
52 return $style;
53 }, $styles );
54
55 if ( empty( $styles ) ) {
56 return $html;
57 }
58
59 $minify_url = $this->combine( $files );
60
61 if ( ! $minify_url ) {
62 return $html;
63 }
64
65 $html = str_replace( '</title>', '</title><link rel="stylesheet" href="' . $minify_url . '" data-minify="1" />', $html );
66
67 $styles = array_filter( $styles );
68 foreach ( $styles as $style ) {
69 $html = str_replace( $style[0], '', $html );
70 }
71
72 return $html;
73 }
74
75 /**
76 * Creates the minify URL if the minification is successful
77 *
78 * @param array $files Files to minify.
79
80 * @return string|bool The minify URL if successful, false otherwise
81 */
82 protected function combine( $files ) {
83 if ( empty( $files ) ) {
84 return false;
85 }
86
87 $file_hash = implode( ',', $files );
88 $filename = md5( $file_hash ) . '.css';
89
90 $minified_file = $this->minify_base_path . $filename;
91
92 if ( file_exists( $minified_file ) ) {
93 touch( $minified_file );
94 } else {
95 $minified_content = $this->minify( $files );
96
97 if ( ! $minified_content ) {
98 return false;
99 }
100
101 wp_mkdir_p( dirname( $minified_file ) );
102 $minify_filepath = file_put_contents( $minified_file, $minified_content );
103
104 if ( ! $minify_filepath ) {
105 return false;
106 }
107 }
108
109 return $this->minify_base_url . $filename;
110 }
111
112 /**
113 * Minifies the content
114 *
115 * @param string|array $files Files to minify.
116 * @return string|bool Minified content, false if empty
117 */
118 protected function minify( $files ) {
119 foreach ( $files as $file ) {
120 $file_content = file_get_contents( $file );
121 $file_content = $this->rewrite_paths( $file, $file_content );
122
123 $this->minifier->add( $file_content );
124 }
125
126 $minified_content = $this->minifier->minify();
127
128 if ( empty( $minified_content ) ) {
129 return false;
130 }
131
132 return $minified_content;
133 }
134 }
135