PluginProbe
ezCache / 1.3.11
ezCache v1.3.11
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 / CssMinifier.php

CssMinifier.php in ezCache 1.3.11, at includes/FileOptimizer/CssMinifier.php

133 lines 3.1 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 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/iuU', $style[2] ) ) {
35 continue;
36 }
37
38 if ( preg_match( '/^\/\//i', $style[2] ) ) {
39 $style[2] = 'http' . (is_ssl() ? 's' : '') . ':' . $style[2];
40 }
41
42 if ( $this->is_external_file( $style[2] ) ) {
43 continue;
44 }
45
46 if ( $this->is_minify_excluded_file( $style ) ) {
47 continue;
48 }
49
50 $minify_url = $this->replace_url( $style[2] );
51
52 if ( ! $minify_url ) {
53 continue;
54 }
55
56 $replace_style = str_replace( $style[2], $minify_url, $style[0] );
57 $replace_style = str_replace( '<link', '<link data-minify="1"', $replace_style );
58 $html = str_replace( $style[0], $replace_style, $html );
59 }
60
61 return $html;
62 }
63
64 /**
65 * Creates the minify URL if the minification is successful
66 *
67 * @param string $url Original file URL.
68
69 * @return string|bool The minify URL if successful, false otherwise
70 */
71 private function replace_url( $url ) {
72 if ( empty( $url ) ) {
73 return false;
74 }
75
76 $file_path = $this->get_file_path( $url );
77
78 if ( ! $file_path ) {
79 return false;
80 }
81
82 $minified_content = $this->minify( $file_path );
83
84 if ( ! $minified_content ) {
85 return false;
86 }
87
88 $unique_id = md5( $minified_content );
89 $filename = preg_replace( '/\.(css)$/', '-' . $unique_id . '.css', ltrim( Cache::realpath( $this->get_url_component( $url, PHP_URL_PATH ) ), '/' ) );
90
91 $minified_file = $this->minify_base_path . $filename;
92 $minify_url = $this->minify_base_url . $filename;
93
94 if ( file_exists( $minified_file ) ) {
95 touch( $minified_file );
96 return $minify_url;
97 }
98
99 wp_mkdir_p( dirname( $minified_file ) );
100 $save_minify_file = file_put_contents( $minified_file, $minified_content );
101
102 if ( ! $save_minify_file ) {
103 return false;
104 }
105
106 return $minify_url;
107 }
108
109 /**
110 * Minifies the content
111 *
112 * @param string|array $file File to minify.
113 * @return string|bool Minified content, false if empty
114 */
115 protected function minify( $file ) {
116 $file_content = file_get_contents( $file );
117
118 if ( ! $file_content ) {
119 return false;
120 }
121
122 $file_content = $this->rewrite_paths( $file, $file_content );
123 $minifier = new CSS( $file_content );
124 $minified_content = $minifier->minify();
125
126 if ( empty( $minified_content ) ) {
127 return false;
128 }
129
130 return $minified_content;
131 }
132 }
133