Skip to content

Commit fa66abf

Browse files
author
Askupa Software
committed
Added support for CSS minification
1 parent 6c3a27f commit fa66abf

File tree

5 files changed

+38
-13
lines changed

5 files changed

+38
-13
lines changed

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
**Contributors:** ykadosh
77
**Tags:** theme, mods, wordpress, dynamic, css, stylesheet
88
**Tested up to:** 4.5.0
9-
**Stable tag:** 1.0.3
9+
**Stable tag:** 1.0.4
1010
**Requires:** PHP 5.3.0 or newer
1111
**WordPress plugin:** [wordpress.org/plugins/wp-dynamic-css/](https://wordpress.org/plugins/wp-dynamic-css/)
1212
**License:** GPLv3 or later
@@ -271,7 +271,7 @@ body {
271271
*Enqueue a dynamic stylesheet*
272272

273273
```php
274-
function wp_dynamic_css_enqueue( $handle, $path, $print = true )
274+
function wp_dynamic_css_enqueue( $handle, $path, $print = true, $minify = false )
275275
```
276276

277277
This function will either print the compiled version of the stylesheet to the document's <head> section, or load it as an external stylesheet if `$print` is set to false.
@@ -280,6 +280,7 @@ This function will either print the compiled version of the stylesheet to the do
280280
* `$handle` (*string*) The stylesheet's name/id
281281
* `$path` (*string*) The absolute path to the dynamic CSS file
282282
* `$print` (*boolean*) Whether to print the compiled CSS to the document head, or load it as an external CSS file via an http request
283+
* `$minify` (*boolean*) Whether to minify the CSS output
283284

284285
### wp_dynamic_css_set_callback
285286

@@ -299,11 +300,15 @@ Set a callback function that will be used to convert variables to actual values.
299300

300301
* ~~Add support for loading the compiled CSS externally instead of printing to the document head~~ (Added in 1.0.1)
301302
* ~~Add support for multiple value callback functions~~ (Added in 1.0.2)
303+
* ~~Add option for minification~~ (Added in 1.0.4)
302304
* Add support for caching and improve performance
303-
* Add option for minification
304305

305306
## Changelog
306307

308+
**1.0.4**
309+
* Set cache-control to no-cache so that changes to options are reflected immediately
310+
* Added support for CSS minification
311+
307312
**1.0.3**
308313
* Added support for variable subscripts
309314

bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Plugin Name: WordPress Dynamic CSS
1414
* Plugin URI: https://github.com/askupasoftware/wp-dynamic-css
1515
* Description: Dynamic CSS compiler for WordPress
16-
* Version: 1.0.3
16+
* Version: 1.0.4
1717
* Author: Askupa Software
1818
* Author URI: http://www.askupasoftware.com
1919
* Text Domain: wp-dynamic-css

compiler.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @package WordPress Dynamic CSS
4-
* @version 1.0.3
4+
* @version 1.0.4
55
* @author Askupa Software <[email protected]>
66
* @link https://github.com/askupasoftware/wp-dynamic-css
77
* @copyright 2016 Askupa Software
@@ -91,7 +91,7 @@ public function compile_printed_styles()
9191
public function compile_external_styles()
9292
{
9393
header( "Content-type: text/css; charset: UTF-8" );
94-
header( "Cache-Control: no-cache, must-revalidate" ); //set headers to NOT cache a page so that changes to options are reflected immediately
94+
header( "Cache-Control: no-cache, must-revalidate" ); //set headers to NOT cache so that changes to options are reflected immediately
9595

9696
$compiled_css = $this->compile_styles( array_filter($this->stylesheets, array( $this, 'filter_external' ) ) );
9797

@@ -104,15 +104,17 @@ public function compile_external_styles()
104104
*
105105
* @param string $handle The stylesheet's name/id
106106
* @param string $path The absolute path to the dynamic style
107-
* @param boolean $print Whether to print the compiled CSS to the document
107+
* @param boolean $print Whether to print the compiled CSS to the document
108108
* head, or include it as an external CSS file
109+
* @param boolean $minify Whether to minify the CSS output
109110
*/
110-
public function enqueue_style( $handle, $path, $print )
111+
public function enqueue_style( $handle, $path, $print, $minify )
111112
{
112113
$this->stylesheets[] = array(
113114
'handle'=> $handle,
114115
'path' => $path,
115-
'print' => $print
116+
'print' => $print,
117+
'minify'=> $minify
116118
);
117119
}
118120

@@ -140,11 +142,24 @@ protected function compile_styles( $styles )
140142
foreach( $styles as $style )
141143
{
142144
$css = file_get_contents( $style['path'] );
145+
if( $style['minify'] ) $css = $this->minify_css ( $css );
143146
$compiled_css .= $this->compile_css( $css, $this->callbacks[$style['handle']] )."\n";
144147
}
145148
return $compiled_css;
146149
}
147150

151+
/**
152+
* Minify a given CSS string by removing comments, whitespaces and newlines
153+
*
154+
* @see http://stackoverflow.com/a/6630103/1096470
155+
* @param string $css CSS style to minify
156+
* @return string Minified CSS
157+
*/
158+
protected function minify_css( $css )
159+
{
160+
return preg_replace( '@({)\s+|(\;)\s+|/\*.+?\*\/|\R@is', '$1$2 ', $css );
161+
}
162+
148163
/**
149164
* This filter is used to return only the styles that are set to be printed
150165
* in the document head

functions.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @package WordPress Dynamic CSS
4-
* @version 1.0.3
4+
* @version 1.0.4
55
* @author Askupa Software <[email protected]>
66
* @link https://github.com/askupasoftware/wp-dynamic-css
77
* @copyright 2016 Askupa Software
@@ -20,11 +20,12 @@
2020
* @param string $path The absolute path to the dynamic CSS file
2121
* @paran boolean $print Whether to print the compiled CSS to the document
2222
* head, or include it as an external CSS file
23+
* @param boolean $minify Whether to minify the CSS output
2324
*/
24-
function wp_dynamic_css_enqueue( $handle, $path, $print = true )
25+
function wp_dynamic_css_enqueue( $handle, $path, $print = true, $minify = false )
2526
{
2627
$dcss = DynamicCSSCompiler::get_instance();
27-
$dcss->enqueue_style( $handle, $path, $print );
28+
$dcss->enqueue_style( $handle, $path, $print, $minify );
2829
}
2930
}
3031

readme.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Contributors: Askupa Software
33
Tags: dynamic css, css, customizer, get_theme_mod, css variables, css compiler
44
Requires at least: 3.0
55
Tested up to: 4.4.2
6-
Stable tag: 1.0.3
6+
Stable tag: 1.0.4
77
License: GPLv3 or later
88
License URI: http://www.gnu.org/licenses/gpl-3.0.html
99

@@ -70,6 +70,10 @@ Please follow the instructions on the plugin's [GitHub page](https://github.com/
7070

7171
== Changelog ==
7272

73+
= 1.0.4 =
74+
* (FIX) Set cache-control to no-cache so that changes to options are reflected immediately
75+
* (NEW) Added support for CSS minification
76+
7377
= 1.0.3 =
7478
* (NEW) Added support for variable subscripts
7579

0 commit comments

Comments
 (0)