11<?php
22/**
33 * @package WordPress Dynamic CSS
4- * @version 1.0.1
4+ * @version 1.0.2
55 * @author Askupa Software <[email protected] > 66 * @link https://github.com/askupasoftware/wp-dynamic-css
77 * @copyright 2016 Askupa Software
@@ -33,6 +33,11 @@ class DynamicCSSCompiler
3333 */
3434 private $ stylesheets = array ();
3535
36+ /**
37+ * @var array
38+ */
39+ private $ callbacks = array ();
40+
3641 /**
3742 * Returns the *Singleton* instance of this class.
3843 *
@@ -67,17 +72,18 @@ public function wp_enqueue_style()
6772 */
6873 public function compile_printed_styles ()
6974 {
70- $ precompiled_css = '' ;
75+ $ compiled_css = '' ;
7176 $ styles = array_filter ($ this ->stylesheets , array ( $ this , 'filter_print ' ) );
7277
7378 // Bail if there are no styles to be printed
7479 if ( count ( $ styles ) === 0 ) return ;
7580
7681 foreach ( $ styles as $ style )
7782 {
78- $ precompiled_css .= $ this ->get_file_contents ( $ style ['path ' ] )."\n" ;
83+ $ css = $ this ->get_file_contents ( $ style ['path ' ] );
84+ $ compiled_css .= $ this ->compile_css ( $ css , $ this ->callbacks [$ style ['handle ' ]] )."\n" ;
7985 }
80- $ css = $ this -> compile_css ( $ precompiled_css );
86+
8187 echo "<style id= \"wp-dynamic-css \"> \n" ;
8288 include 'style.phtml ' ;
8389 echo "</style> " ;
@@ -90,33 +96,47 @@ public function compile_printed_styles()
9096 public function compile_external_styles ()
9197 {
9298 header ( "Content-type: text/css; charset: UTF-8 " );
93- $ precompiled_css = '' ;
99+ $ compiled_css = '' ;
94100 $ styles = array_filter ($ this ->stylesheets , array ( $ this , 'filter_external ' ) );
95101
96102 foreach ( $ styles as $ style )
97103 {
98- $ precompiled_css .= $ this ->get_file_contents ( $ style ['path ' ] )."\n" ;
104+ $ css = $ this ->get_file_contents ( $ style ['path ' ] );
105+ $ compiled_css .= $ this ->compile_css ( $ css , $ this ->callbacks [$ style ['handle ' ]] )."\n" ;
99106 }
100- $ css = $ this -> compile_css ( $ precompiled_css );
107+
101108 include 'style.phtml ' ;
102109 wp_die ();
103110 }
104111
105112 /**
106113 * Add a style path to the pool of styles to be compiled
107114 *
115+ * @param string $handle The stylesheet's name/id
108116 * @param string $path The absolute path to the dynamic style
109117 * @param boolean $print Whether to print the compiled CSS to the document
110118 * head, or include it as an external CSS file
111119 */
112- public function enqueue_style ( $ path , $ print )
120+ public function enqueue_style ( $ handle , $ path , $ print )
113121 {
114122 $ this ->stylesheets [] = array (
123+ 'handle ' => $ handle ,
115124 'path ' => $ path ,
116125 'print ' => $ print
117126 );
118127 }
119128
129+ /**
130+ * Register a value retrieval function and associate it with the given handle
131+ *
132+ * @param type $handle The stylesheet's name/id
133+ * @param type $callback
134+ */
135+ public function register_callback ( $ handle , $ callback )
136+ {
137+ $ this ->callbacks [$ handle ] = $ callback ;
138+ }
139+
120140 /**
121141 * This filter is used to return only the styles that are set to be printed
122142 * in the document head
@@ -156,19 +176,19 @@ protected function get_file_contents( $path )
156176
157177 /**
158178 * Parse the given CSS string by converting the variables to their
159- * corresponding values retrieved by applying the filter
160- * wp_dynamic_css_get_variable_value.
179+ * corresponding values retrieved by applying the callback function
161180 *
181+ * @param callable $callback A function that replaces the variables with
182+ * their values. The function accepts the variable's name as a parameter
162183 * @param string $css A string containing dynamic CSS (pre-compiled CSS with
163184 * variables)
164- * @uses wp_dynamic_css_get_variable_value filter
165185 * @return string The compiled CSS after converting the variables to their
166186 * corresponding values
167187 */
168- protected function compile_css ( $ css )
188+ protected function compile_css ( $ css, $ callback )
169189 {
170- return preg_replace_callback ( '#\$([\w]+)# ' , function ( $ matches ) {
171- return apply_filters ( ' wp_dynamic_css_get_variable_value ' , $ matches [1 ] );
190+ return preg_replace_callback ( '#\$([\w]+)# ' , function ( $ matches ) use ( $ callback ) {
191+ return call_user_func_array ( $ callback , array ( $ matches [1 ]) );
172192 }, $ css );
173193 }
174194}
0 commit comments