activation.class.php
8 years ago
api.class.php
8 years ago
cdn.class.php
8 years ago
config.class.php
8 years ago
control.class.php
8 years ago
crawler-sitemap.class.php
8 years ago
crawler.class.php
8 years ago
data.class.php
8 years ago
esi.class.php
8 years ago
gui.class.php
8 years ago
import.class.php
8 years ago
litespeed-cache.class.php
8 years ago
litespeed.autoload.php
8 years ago
log.class.php
8 years ago
media.class.php
8 years ago
object.class.php
8 years ago
object.lib.php
8 years ago
optimize.class.php
8 years ago
optimizer.class.php
8 years ago
purge.class.php
8 years ago
router.class.php
8 years ago
tag.class.php
8 years ago
task.class.php
8 years ago
utility.class.php
8 years ago
vary.class.php
8 years ago
optimizer.class.php
280 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * The optimize4 class. |
| 5 | * |
| 6 | * @since 1.9 |
| 7 | * @package LiteSpeed_Cache |
| 8 | * @subpackage LiteSpeed_Cache/inc |
| 9 | * @author LiteSpeed Technologies <info@litespeedtech.com> |
| 10 | */ |
| 11 | require_once LSCWP_DIR . 'lib/js_min.class.php' ; |
| 12 | require_once LSCWP_DIR . 'lib/css_min.class.php' ; |
| 13 | require_once LSCWP_DIR . 'lib/css_min.colors.class.php' ; |
| 14 | require_once LSCWP_DIR . 'lib/css_min.utils.class.php' ; |
| 15 | require_once LSCWP_DIR . 'lib/url_rewritter.class.php' ; |
| 16 | |
| 17 | use tubalmartin\CssMin\Minifier as CSSmin; |
| 18 | use tubalmartin\CssMin\Colors as Colors; |
| 19 | use tubalmartin\CssMin\Utils as Utils; |
| 20 | |
| 21 | |
| 22 | class LiteSpeed_Cache_Optimizer |
| 23 | { |
| 24 | private static $_instance ; |
| 25 | |
| 26 | /** |
| 27 | * Init optimizer |
| 28 | * |
| 29 | * @since 1.9 |
| 30 | * @access private |
| 31 | */ |
| 32 | private function __construct() |
| 33 | { |
| 34 | $this->cfg_css_inline_minify = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_CSS_INLINE_MINIFY ) ; |
| 35 | $this->cfg_js_inline_minify = LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_JS_INLINE_MINIFY ) ; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Run HTML minify process and return final content |
| 40 | * |
| 41 | * @since 1.9 |
| 42 | * @access public |
| 43 | */ |
| 44 | public function html_min( $content ) |
| 45 | { |
| 46 | $options = array() ; |
| 47 | if ( $this->cfg_css_inline_minify ) { |
| 48 | $options[ 'cssMinifier' ] = array( new CSSmin(), 'run' ) ; |
| 49 | } |
| 50 | |
| 51 | if ( $this->cfg_js_inline_minify ) { |
| 52 | $options[ 'jsMinifier' ] = 'JSMin\JSMin::minify' ; |
| 53 | } |
| 54 | |
| 55 | $obj = new Minify_HTML( $content, $options ) ; |
| 56 | return $obj->process() ; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Run minify process and return final content |
| 61 | * |
| 62 | * @since 1.9 |
| 63 | * @access public |
| 64 | * @return string The final content |
| 65 | */ |
| 66 | public function serve( $filename, $concat_only ) |
| 67 | { |
| 68 | // Search filename in db for src URLs |
| 69 | $urls = LiteSpeed_Cache_Data::optm_hash2src( $filename ) ; |
| 70 | if ( ! $urls || ! is_array( $urls ) ) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | |
| 75 | // Parse real file path |
| 76 | $real_files = array() ; |
| 77 | foreach ( $urls as $url ) { |
| 78 | $real_file = LiteSpeed_Cache_Utility::is_internal_file( $url ) ; |
| 79 | if ( ! $real_file ) { |
| 80 | continue ; |
| 81 | } |
| 82 | $real_files[] = $real_file[ 0 ] ; |
| 83 | } |
| 84 | |
| 85 | if ( ! $real_files ) { |
| 86 | return false; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | set_error_handler( 'litespeed_exception_handler' ) ; |
| 91 | |
| 92 | $headers = array() ; |
| 93 | $content = '' ; |
| 94 | $file_type = substr( $filename, strrpos( $filename, '.' ) + 1 ) ; |
| 95 | try { |
| 96 | // Handle CSS |
| 97 | if ( $file_type === 'css' ) { |
| 98 | $content = $this->_serve_css( $real_files, $concat_only ) ; |
| 99 | $headers[ 'Content-Type' ] = 'text/css; charset=utf-8' ; |
| 100 | } |
| 101 | // Handle JS |
| 102 | else { |
| 103 | $content = $this->_serve_js( $real_files, $concat_only ) ; |
| 104 | $headers[ 'Content-Type' ] = 'application/x-javascript' ; |
| 105 | } |
| 106 | |
| 107 | } catch ( ErrorException $e ) { |
| 108 | LiteSpeed_Cache_Log::debug( 'Error when serving from optimizer: ' . $e->getMessage() ) ; |
| 109 | error_log( 'LiteSpeed Optimizer serving Error: ' . $e->getMessage() ) ; |
| 110 | return false ; |
| 111 | } |
| 112 | restore_error_handler() ; |
| 113 | |
| 114 | /** |
| 115 | * Clean comment when minify |
| 116 | * @since 1.7.1 |
| 117 | */ |
| 118 | if ( LiteSpeed_Cache::config( LiteSpeed_Cache_Config::OPID_OPTM_RM_COMMENT ) ) { |
| 119 | $content = $this->_remove_comment( $content, $file_type ) ; |
| 120 | } |
| 121 | |
| 122 | LiteSpeed_Cache_Log::debug( 'Optm: Generated content' ) ; |
| 123 | |
| 124 | $headers[ 'Content-Length' ] = strlen( $content ) ; |
| 125 | |
| 126 | foreach ( $headers as $key => $val ) { |
| 127 | header( $key . ': ' . $val ) ; |
| 128 | LiteSpeed_Cache_Log::debug( 'HEADER ' . $key . ': ' . $val ) ; |
| 129 | } |
| 130 | |
| 131 | return $content ; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Serve css with/without minify |
| 136 | * |
| 137 | * @since 1.9 |
| 138 | * @access private |
| 139 | */ |
| 140 | private function _serve_css( $files, $concat_only = false ) |
| 141 | { |
| 142 | $con = array() ; |
| 143 | foreach ( $files as $real_path ) { |
| 144 | LiteSpeed_Cache_Log::debug( 'Optimizer: [real_path] ' . $real_path ) ; |
| 145 | $data = $this->_read( $real_path ) ; |
| 146 | |
| 147 | $data = preg_replace( '/@charset[^;]+;\\s*/', '', $data ) ; |
| 148 | |
| 149 | if ( ! $concat_only && ! $this->_is_min( $real_path ) ) { |
| 150 | $obj = new CSSmin() ; |
| 151 | $data = $obj->run( $data ) ; |
| 152 | } |
| 153 | |
| 154 | $data = Minify_CSS_UriRewriter::rewrite( $data, dirname( $real_path ) ) ; |
| 155 | |
| 156 | $con[] = $data ; |
| 157 | } |
| 158 | |
| 159 | return implode( '', $con ) ; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Serve JS with/without minify |
| 164 | * |
| 165 | * @since 1.9 |
| 166 | * @access private |
| 167 | */ |
| 168 | private function _serve_js( $files, $concat_only ) |
| 169 | { |
| 170 | $con = array() ; |
| 171 | foreach ( $files as $real_path ) { |
| 172 | $data = $this->_read( $real_path ) ; |
| 173 | |
| 174 | if ( ! $concat_only && ! $this->_is_min( $real_path ) ) { |
| 175 | $data = JSMin\JSMin::minify( $data ) ; |
| 176 | } |
| 177 | else { |
| 178 | $data = $this->_null_minifier( $data ) ; |
| 179 | } |
| 180 | |
| 181 | $con[] = $data ; |
| 182 | } |
| 183 | |
| 184 | return implode( "\n;", $con ) ; |
| 185 | } |
| 186 | |
| 187 | private function _null_minifier( $content ) |
| 188 | { |
| 189 | $content = str_replace( "\r\n", "\n", $content ) ; |
| 190 | |
| 191 | return trim( $content ) ; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Check if the file is already min file |
| 196 | * |
| 197 | * @since 1.9 |
| 198 | * @access private |
| 199 | */ |
| 200 | private function _is_min( $filename ) |
| 201 | { |
| 202 | $basename = basename( $filename ) ; |
| 203 | if ( preg_match( '|[-\.]min\.(?:[a-zA-Z]+)$|i', $basename ) ) { |
| 204 | return true ; |
| 205 | } |
| 206 | |
| 207 | return false ; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Read content and remove UTF-8 BOM if present |
| 212 | * |
| 213 | * @since 1.9 |
| 214 | * @access private |
| 215 | */ |
| 216 | private function _read( $file ) |
| 217 | { |
| 218 | $content = file_get_contents( $file ) ; |
| 219 | if ( substr( $content, 0, 3 ) === "\xEF\xBB\xBF" ) { |
| 220 | $content = substr( $content, 3 ) ; |
| 221 | } |
| 222 | return $content ; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Remove comment when minify |
| 227 | * |
| 228 | * @since 1.7.1 |
| 229 | * @since 1.9 Moved here from optiize.cls |
| 230 | * @access private |
| 231 | */ |
| 232 | private function _remove_comment( $content, $type ) |
| 233 | { |
| 234 | $_from = array( |
| 235 | '|\/\*.*\*\/|U', |
| 236 | '|\/\*.*\*\/|sU', |
| 237 | "|\n+|", |
| 238 | // "|;+\n*;+|", |
| 239 | // "|\n+;|", |
| 240 | // "|;\n+|" |
| 241 | ) ; |
| 242 | |
| 243 | $_to = array( |
| 244 | '', |
| 245 | "\n", |
| 246 | "\n", |
| 247 | // ';', |
| 248 | // ';', |
| 249 | // ';', |
| 250 | ) ; |
| 251 | |
| 252 | $content = preg_replace( $_from, $_to, $content ) ; |
| 253 | if ( $type == 'css' ) { |
| 254 | $content = preg_replace( "|: *|", ':', $content ) ; |
| 255 | $content = preg_replace( "| */ *|", '/', $content ) ; |
| 256 | } |
| 257 | $content = trim( $content ) ; |
| 258 | return $content ; |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Get the current instance object. |
| 263 | * |
| 264 | * @since 1.9 |
| 265 | * @access public |
| 266 | * @return Current class instance. |
| 267 | */ |
| 268 | public static function get_instance() |
| 269 | { |
| 270 | if ( ! isset(self::$_instance) ) { |
| 271 | self::$_instance = new self() ; |
| 272 | } |
| 273 | |
| 274 | return self::$_instance ; |
| 275 | } |
| 276 | |
| 277 | } |
| 278 | |
| 279 | |
| 280 |