PluginProbe
WP Coder – Insert & Manage Code Snippets / 3.3
WP Coder – Insert & Manage Code Snippets v3.3
4.5.1 1.1 2.3.1 2.3.2 2.4.1 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1 3.1.1 3.2 3.2.1 3.3 3.4 3.5 3.5.1 All 39 releases
wp-coder / classes / Optimization / CSSMinifier.php

CSSMinifier.php in WP Coder – Insert & Manage Code Snippets 3.3, at classes/Optimization/CSSMinifier.php

55 lines 1.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPCoder\Optimization;
4
5 // Exit if accessed directly.
6 defined( 'ABSPATH' ) || exit;
7
8 class CSSMinifier {
9
10 public static function minify($input): string {
11 if(trim($input) === "") {
12 return $input;
13 }
14 $replaced = preg_replace(
15 array(
16 // Remove comment(s)
17 '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')|\/\*(?!\!)(?>.*?\*\/)|^\s*|\s*$#s',
18 // Remove unused white-space(s)
19 '#("(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\'|\/\*(?>.*?\*\/))|\s*+;\s*+(})\s*+|\s*+([*$~^|]?+=|[{};,>~]|\s(?![0-9\.])|!important\b)\s*+|([[(:])\s++|\s++([])])|\s++(:)\s*+(?!(?>[^{}"\']++|"(?:[^"\\\]++|\\\.)*+"|\'(?:[^\'\\\\]++|\\\.)*+\')*+{)|^\s++|\s++\z|(\s)\s+#si',
20 // Replace `0(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)` with `0`
21 '#(?<=[\s:])(0)(cm|em|ex|in|mm|pc|pt|px|vh|vw|%)#si',
22 // Replace `:0 0 0 0` with `:0`
23 '#:(0\s+0|0\s+0\s+0\s+0)(?=[;\}]|\!important)#i',
24 // Replace `background-position:0` with `background-position:0 0`
25 '#(background-position):0(?=[;\}])#si',
26 // Replace `0.6` with `.6`, but only when preceded by `:`, `,`, `-` or a white-space
27 '#(?<=[\s:,\-])0+\.(\d+)#s',
28 // Minify string value
29 '#(\/\*(?>.*?\*\/))|(?<!content\:)([\'"])([a-z_][a-z0-9\-_]*?)\2(?=[\s\{\}\];,])#si',
30 '#(\/\*(?>.*?\*\/))|(\burl\()([\'"])([^\s]+?)\3(\))#si',
31 // Minify HEX color code
32 '#(?<=[\s:,\-]\#)([a-f0-6]+)\1([a-f0-6]+)\2([a-f0-6]+)\3#i',
33 // Replace `(border|outline):none` with `(border|outline):0`
34 '#(?<=[\{;])(border|outline):none(?=[;\}\!])#',
35 // Remove empty selector(s)
36 '#(\/\*(?>.*?\*\/))|(^|[\{\}])(?:[^\s\{\}]+)\{\}#s'
37 ),
38 array(
39 '$1',
40 '$1$2$3$4$5$6$7',
41 '$1',
42 ':0',
43 '$1:0 0',
44 '.$1',
45 '$1$3',
46 '$1$2$4$5',
47 '$1$2$3',
48 '$1:0',
49 '$1$2'
50 ),
51 $input);
52 return str_replace('fill=none', '', $replaced); // fix for url("data:image/svg+xml,%3Csvg ... fill='none'
53 }
54
55 }