11<?php
22/**
33 * @package WordPress Dynamic CSS
4- * @version 1.0.0
4+ * @version 1.0.1
55 * @author Askupa Software <[email protected] > 66 * @link https://github.com/askupasoftware/wp-dynamic-css
77 * @copyright 2016 Askupa Software
1717 * body {color: $body_color;}
1818 * </pre>
1919 * In the above example, the variable $body_color is replaced by a value
20- * that is retrieved by the filter wp_dynamic_css_get_variable_value . The filter
21- * is passed the variable name without the dollar sign, which can be used with
22- * get_option() or get_theme_mod() etc.
20+ * retrieved by the value callback function . The function is passed the variable
21+ * name without the dollar sign, which can be used with get_option() or
22+ * get_theme_mod() etc.
2323 */
2424class DynamicCSSCompiler
2525{
@@ -43,24 +43,71 @@ public static function get_instance()
4343 if (null === static ::$ instance )
4444 {
4545 static ::$ instance = new static ();
46- static ::$ instance ->init ();
4746 }
48-
4947 return static ::$ instance ;
5048 }
5149
5250 /**
53- * Initiate the compiler by hooking to wp_print_styles
51+ * Enqueue the PHP script used for compiling dynamic stylesheets that are
52+ * loaded externally
5453 */
55- public function init ()
54+ public function wp_enqueue_style ()
5655 {
57- add_action ( 'wp_print_styles ' , array ( $ this , 'print_compiled_style ' ) );
56+ // Only enqueue if there is at least one dynamic stylesheet that is
57+ // set to be loaded externally
58+ if ( 0 < count ( array_filter ($ this ->stylesheets , array ( $ this , 'filter_external ' ) ) ) )
59+ {
60+ wp_enqueue_style ( 'wp-dynamic-css ' , admin_url ( 'admin-ajax.php?action=wp_dynamic_css ' ) );
61+ }
62+ }
63+
64+ /**
65+ * Parse all styles in $this->stylesheets and print them if the flag 'print'
66+ * is set to true. Used for printing styles to the document head.
67+ */
68+ public function compile_printed_styles ()
69+ {
70+ $ precompiled_css = '' ;
71+ $ styles = array_filter ($ this ->stylesheets , array ( $ this , 'filter_print ' ) );
72+
73+ // Bail if there are no styles to be printed
74+ if ( count ( $ styles ) === 0 ) return ;
75+
76+ foreach ( $ styles as $ style )
77+ {
78+ $ precompiled_css .= $ this ->get_file_contents ( $ style ['path ' ] )."\n" ;
79+ }
80+ $ css = $ this ->compile_css ( $ precompiled_css );
81+ echo "<style id= \"wp-dynamic-css \"> \n" ;
82+ include 'style.phtml ' ;
83+ echo "</style> " ;
84+ }
85+
86+ /**
87+ * Parse all styles in $this->stylesheets and print them if the flag 'print'
88+ * is not set to true. Used for loading styles externally via an http request.
89+ */
90+ public function compile_external_styles ()
91+ {
92+ header ( "Content-type: text/css; charset: UTF-8 " );
93+ $ precompiled_css = '' ;
94+ $ styles = array_filter ($ this ->stylesheets , array ( $ this , 'filter_external ' ) );
95+
96+ foreach ( $ styles as $ style )
97+ {
98+ $ precompiled_css .= $ this ->get_file_contents ( $ style ['path ' ] )."\n" ;
99+ }
100+ $ css = $ this ->compile_css ( $ precompiled_css );
101+ include 'style.phtml ' ;
102+ wp_die ();
58103 }
59104
60105 /**
61106 * Add a style path to the pool of styles to be compiled
62107 *
63- * @param type $path The absolute path to the dynamic style
108+ * @param string $path The absolute path to the dynamic style
109+ * @param boolean $print Whether to print the compiled CSS to the document
110+ * head, or include it as an external CSS file
64111 */
65112 public function enqueue_style ( $ path , $ print )
66113 {
@@ -71,22 +118,40 @@ public function enqueue_style( $path, $print )
71118 }
72119
73120 /**
74- * Parse all styles in $this->stylesheets and print them if the flag 'print'
75- * is set to true
121+ * This filter is used to return only the styles that are set to be printed
122+ * in the document head
123+ *
124+ * @param array $style
125+ * @return boolean
126+ */
127+ protected function filter_print ( $ style )
128+ {
129+ return true === $ style ['print ' ];
130+ }
131+
132+ /**
133+ * This filter is used to return only the styles that are set to be loaded
134+ * externally
135+ *
136+ * @param array $style
137+ * @return boolean
138+ */
139+ protected function filter_external ( $ style )
140+ {
141+ return true !== $ style ['print ' ];
142+ }
143+
144+ /**
145+ * Get the contents of a given file
146+ *
147+ * @param string $path The absolute path to the file
148+ * @return string The file contents
76149 */
77- public function print_compiled_style ( )
150+ protected function get_file_contents ( $ path )
78151 {
79152 ob_start ();
80- foreach ( $ this ->stylesheets as $ style )
81- {
82- if ( true === $ style ['print ' ] )
83- {
84- include $ style ;
85- echo "\n" ;
86- }
87- }
88- $ css = $ this ->parse_css ( ob_get_clean () );
89- include 'style.phtml ' ;
153+ include $ path ;
154+ return ob_get_clean ();
90155 }
91156
92157 /**
@@ -100,10 +165,10 @@ public function print_compiled_style()
100165 * @return string The compiled CSS after converting the variables to their
101166 * corresponding values
102167 */
103- public function parse_css ( $ css )
168+ protected function compile_css ( $ css )
104169 {
105- return preg_replace_callback ('#\$([\w]+)# ' , function ($ matches ) {
106- return apply_filters ( 'wp_dynamic_css_get_variable_value ' , $ matches [1 ]);
170+ return preg_replace_callback ( '#\$([\w]+)# ' , function ( $ matches ) {
171+ return apply_filters ( 'wp_dynamic_css_get_variable_value ' , $ matches [1 ] );
107172 }, $ css );
108173 }
109174}
0 commit comments