PluginProbe
ezCache / 2.5.3
ezCache v2.5.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 2.5.3, at includes/FileOptimizer/CssCombiner.php

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