PluginProbe
ezCache / 1.2
ezCache v1.2
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 / JsMinifier.php

JsMinifier.php in ezCache 1.2, at includes/FileOptimizer/JsMinifier.php

137 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\JS;
5 use Upress\EzCache\Cache;
6
7 class JsMinifier extends BaseFileOptimizer {
8 static $FILE_TYPE = 'js';
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 $scripts = $this->find( '<script\s+([^>]+[\s\'"])?src\s*=\s*[\'"]\s*?([^\'"]+\.js(?:\?[^\'"]*)?)\s*?[\'"]([^>]+)?\/?>', $html );
28
29 if ( ! $scripts ) {
30 return $html;
31 }
32
33 foreach ( $scripts as $script ) {
34 global $wp_scripts;
35
36 if ( preg_match( '/[-.]min\.js/iU', $script[2] ) ) {
37 continue;
38 }
39
40 if ( $this->is_external_file( $script[2] ) ) {
41 continue;
42 }
43
44 if ( $this->is_minify_excluded_file( $script ) ) {
45 continue;
46 }
47
48 // Don't minify jQuery included in WP core since it's already minified but without .min in the filename.
49 if ( ! empty( $wp_scripts->registered['jquery-core']->src ) && false !== strpos( $script[2], $wp_scripts->registered['jquery-core']->src ) ) {
50 continue;
51 }
52
53 $minify_url = $this->replace_url( $script[2] );
54
55 if ( ! $minify_url ) {
56 continue;
57 }
58
59 $replace_script = str_replace( $script[2], $minify_url, $script[0] );
60 $replace_script = str_replace( '<script', '<script data-minify="1"', $replace_script );
61 $html = str_replace( $script[0], $replace_script, $html );
62 }
63
64 return $html;
65 }
66
67 /**
68 * Creates the minify URL if the minification is successful
69 *
70 * @param string $url Original file URL.
71
72 * @return string|bool The minify URL if successful, false otherwise
73 */
74 private function replace_url( $url ) {
75 if ( empty( $url ) ) {
76 return false;
77 }
78
79 $unique_id = md5( $url );
80 $uri_path = $this->get_url_component( $url, PHP_URL_PATH );
81 $realpath = ltrim( Cache::realpath( $uri_path ), '/' );
82 $filename = preg_replace( '/\.js$/', '-' . $unique_id . '.js', $realpath );
83 $minified_file = $this->minify_base_path . $filename;
84 $minified_url = $this->minify_base_url . $filename;
85
86 if ( file_exists( $minified_file ) ) {
87 touch( $minified_file );
88 return $minified_url;
89 }
90
91 $file_path = $this->get_file_path( $url );
92
93 if ( ! $file_path ) {
94 return false;
95 }
96
97 $minified_content = $this->minify( $file_path );
98
99 if ( ! $minified_content ) {
100 return false;
101 }
102
103 wp_mkdir_p( dirname( $minified_file ) );
104 $save_minify_file = file_put_contents( $minified_file, $minified_content );
105
106 if ( ! $save_minify_file ) {
107 return false;
108 }
109
110 return $minified_url;
111 }
112
113 /**
114 * Minifies the content
115 *
116 * @param string|array $file File to minify.
117 *
118 * @return string|bool Minified content, false if empty
119 */
120 protected function minify( $file ) {
121 $file_content = file_get_contents( $file );
122
123 if ( ! $file_content ) {
124 return false;
125 }
126
127 $minifier = new JS( $file_content );
128 $minified_content = $minifier->minify();
129
130 if ( empty( $minified_content ) ) {
131 return false;
132 }
133
134 return $minified_content;
135 }
136 }
137