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