WebPageCache
7 months ago
external
2 months ago
ExcludeJsFromDelay.php
2 years ago
OptimizerBanner.php
11 months ago
OptimizerBase.php
2 years ago
OptimizerCSSMin.php
2 years ago
OptimizerCache.php
1 year ago
OptimizerCacheStructure.php
2 years ago
OptimizerCriticalCss.php
1 year ago
OptimizerElementor.php
2 years ago
OptimizerFonts.php
2 years ago
OptimizerImages.php
1 year ago
OptimizerLogger.php
1 year ago
OptimizerMain.php
2 months ago
OptimizerOnInit.php
11 months ago
OptimizerScripts.php
2 years ago
OptimizerSettings.php
2 months ago
OptimizerStyles.php
2 years ago
OptimizerUrl.php
1 year ago
OptimizerUtils.php
2 months ago
OptimizerWhiteLabel.php
2 years ago
OptimizerCSSMin.php
72 lines
| 1 | <?php |
| 2 | |
| 3 | namespace TenWebOptimizer; |
| 4 | |
| 5 | use MatthiasMullie\Minify; |
| 6 | |
| 7 | /* |
| 8 | * Thin wrapper around css minifiers to avoid rewriting a bunch of existing code. |
| 9 | */ |
| 10 | if (!defined('ABSPATH')) { |
| 11 | exit; |
| 12 | } |
| 13 | |
| 14 | class OptimizerCSSMin |
| 15 | { |
| 16 | protected $minifier = null; |
| 17 | |
| 18 | /** |
| 19 | * Runs the minifier on given string of $css. |
| 20 | * Returns the minified css. |
| 21 | * |
| 22 | * @param string $css CSS to minify |
| 23 | * @param bool $withMinifier Process CSS in minifier or not |
| 24 | * |
| 25 | * @return string |
| 26 | */ |
| 27 | public function run($css, $withMinifier = true) |
| 28 | { |
| 29 | $this->minifier = new Minify\CSS(); |
| 30 | |
| 31 | if (!empty(trim($css))) { |
| 32 | $css = $this->addDebugInfo($css); |
| 33 | $css = OptimizerUtils::replace_bg($css); |
| 34 | $css = OptimizerUtils::replace_font($css); |
| 35 | $css = OptimizerUtils::removeBgImageMarkers($css); |
| 36 | |
| 37 | if ($withMinifier) { |
| 38 | $this->minifier->add($css); |
| 39 | |
| 40 | return $this->minifier->minify(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return $css; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Static helper. |
| 49 | * |
| 50 | * @param string $css CSS to minify |
| 51 | * @param bool $withMinifier |
| 52 | * |
| 53 | * @return string |
| 54 | */ |
| 55 | public static function minify($css, $withMinifier = true) |
| 56 | { |
| 57 | $minifier = new self(); |
| 58 | |
| 59 | return $minifier->run($css, $withMinifier); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Adds a comment for just to be sure that optimizer worked on this style |
| 64 | * |
| 65 | * @return string |
| 66 | */ |
| 67 | private function addDebugInfo($css) |
| 68 | { |
| 69 | return "\n/* 10Web Booster optimized this CSS file */\r\n" . $css; |
| 70 | } |
| 71 | } |
| 72 |