@@ -53,47 +53,61 @@ public static function get_instance()
53
53
}
54
54
55
55
/**
56
- * Enqueue the PHP script used for compiling dynamic stylesheets that are
57
- * loaded externally
56
+ * Enqueue the stylesheets that are registered to be loaded externally
58
57
*/
59
- public function wp_enqueue_style ()
58
+ public function enqueue_styles ()
60
59
{
61
- // Only enqueue if there is at least one dynamic stylesheet that is
62
- // set to be loaded externally
63
- if ( 0 < count ( array_filter ($ this ->stylesheets , array ( $ this , 'filter_external ' ) ) ) )
60
+ foreach ( $ this ->stylesheets as $ stylesheet )
64
61
{
65
- wp_enqueue_style ( 'wp-dynamic-css ' , admin_url ( 'admin-ajax.php?action=wp_dynamic_css ' ) );
62
+ if ( !$ stylesheet ['print ' ] && $ this ->callback_exists ( $ stylesheet ['handle ' ] ) )
63
+ {
64
+ wp_enqueue_style (
65
+ 'wp-dynamic-css- ' .$ stylesheet ['handle ' ],
66
+ esc_url_raw ( add_query_arg (array (
67
+ 'action ' => 'wp_dynamic_css ' ,
68
+ 'handle ' => $ stylesheet ['handle ' ]
69
+ ), admin_url ( 'admin-ajax.php ' )))
70
+ );
71
+ }
66
72
}
67
73
}
68
74
69
75
/**
70
- * Parse all styles in $this->stylesheets and print them if the flag 'print'
71
- * is set to true. Used for printing styles to the document head.
76
+ * Print the stylesheets that are registered to be printed to the document head
72
77
*/
73
- public function compile_printed_styles ()
78
+ public function print_styles ()
74
79
{
75
- // Compile only if there are styles to be printed
76
- if ( 0 < count ( array_filter ($ this ->stylesheets , array ( $ this , 'filter_print ' ) ) ) )
80
+ foreach ( $ this ->stylesheets as $ stylesheet )
77
81
{
78
- $ compiled_css = $ this ->get_compiled_styles ( true );
82
+ if ( $ stylesheet ['print ' ] && $ this ->callback_exists ( $ stylesheet ['handle ' ] ) )
83
+ {
84
+ $ compiled_css = $ this ->get_compiled_style ( $ stylesheet );
79
85
80
- echo "<style id= \"wp-dynamic-css \"> \n" ;
81
- include 'style.phtml ' ;
82
- echo "</style> " ;
86
+ echo "<style id= \"wp-dynamic-css- " .$ stylesheet ['handle ' ]."\"> \n" ;
87
+ include 'style.phtml ' ;
88
+ echo "\n</style> \n" ;
89
+ }
83
90
}
84
91
}
85
92
86
93
/**
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.
94
+ * This is the AJAX callback used for loading styles externally via an http
95
+ * request.
89
96
*/
90
- public function compile_external_styles ()
97
+ public function ajax_callback ()
91
98
{
92
99
header ( "Content-type: text/css; charset: UTF-8 " );
100
+ $ handle = filter_input ( INPUT_GET , 'handle ' );
93
101
94
- $ compiled_css = $ this ->get_compiled_styles ( false );
102
+ foreach ( $ this ->stylesheets as $ stylesheet )
103
+ {
104
+ if ( $ handle === $ stylesheet ['handle ' ] )
105
+ {
106
+ $ compiled_css = $ this ->get_compiled_style ( $ stylesheet );
107
+ include 'style.phtml ' ;
108
+ }
109
+ }
95
110
96
- include 'style.phtml ' ;
97
111
wp_die ();
98
112
}
99
113
@@ -129,31 +143,6 @@ public function register_callback( $handle, $callback )
129
143
{
130
144
$ this ->callbacks [$ handle ] = $ callback ;
131
145
}
132
-
133
- /**
134
- * Compile multiple dynamic stylesheets
135
- *
136
- * @param boolean $printed
137
- * @return string Compiled CSS
138
- */
139
- protected function get_compiled_styles ( $ printed )
140
- {
141
- $ compiled_css = '' ;
142
- foreach ( $ this ->stylesheets as $ style )
143
- {
144
- if ( !array_key_exists ( $ style ['handle ' ], $ this ->callbacks ) )
145
- {
146
- trigger_error ( 'There is no callback function associated with the handle " ' .$ style ['handle ' ].'". Use <b>wp_dynamic_css_set_callback()</b> to register a callback function for this handle. ' );
147
- continue ;
148
- }
149
-
150
- if ( $ style ['print ' ] === $ printed )
151
- {
152
- $ compiled_css .= $ this ->get_compiled_style ( $ style )."\n" ;
153
- }
154
- }
155
- return $ compiled_css ;
156
- }
157
146
158
147
/**
159
148
* Get the compiled CSS for the given style. Skips compilation if the compiled
@@ -194,29 +183,24 @@ protected function minify_css( $css )
194
183
{
195
184
return preg_replace ( '@({)\s+|(\;)\s+|/\*.+?\*\/|\R@is ' , '$1$2 ' , $ css );
196
185
}
197
-
198
- /**
199
- * This filter is used to return only the styles that are set to be printed
200
- * in the document head
201
- *
202
- * @param array $style
203
- * @return boolean
204
- */
205
- protected function filter_print ( $ style )
206
- {
207
- return true === $ style ['print ' ];
208
- }
209
186
210
187
/**
211
- * This filter is used to return only the styles that are set to be loaded
212
- * externally
188
+ * Check if a callback function has been register for the given handle.
213
189
*
214
- * @param array $style
190
+ * @param string $handle
215
191
* @return boolean
216
192
*/
217
- protected function filter_external ( $ style )
193
+ protected function callback_exists ( $ handle )
218
194
{
219
- return true !== $ style ['print ' ];
195
+ if ( array_key_exists ( $ handle , $ this ->callbacks ) )
196
+ {
197
+ return true ;
198
+ }
199
+ trigger_error (
200
+ "There is no callback function associated with the handle ' $ handle'. " .
201
+ "Use <b>wp_dynamic_css_set_callback()</b> to register a callback function for this handle. "
202
+ );
203
+ return false ;
220
204
}
221
205
222
206
/**
0 commit comments