Skip to content

Commit b001a42

Browse files
committed
Separate enqueued styles instead of concatinating them
1 parent 7d79a2b commit b001a42

File tree

2 files changed

+51
-67
lines changed

2 files changed

+51
-67
lines changed

bootstrap.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* styles.
4444
*/
4545
$dcss = DynamicCSSCompiler::get_instance();
46-
add_action( 'wp_print_styles', array( $dcss, 'compile_printed_styles' ), 100 );
47-
add_action( 'wp_enqueue_scripts', array( $dcss, 'wp_enqueue_style' ), 100 );
48-
add_action( 'wp_ajax_wp_dynamic_css', array( $dcss, 'compile_external_styles' ) );
49-
add_action( 'wp_ajax_nopriv_wp_dynamic_css', array( $dcss, 'compile_external_styles' ) );
46+
add_action( 'wp_print_styles', array( $dcss, 'print_styles' ), 100 );
47+
add_action( 'wp_enqueue_scripts', array( $dcss, 'enqueue_styles' ), 100 );
48+
add_action( 'wp_ajax_wp_dynamic_css', array( $dcss, 'ajax_callback' ) );
49+
add_action( 'wp_ajax_nopriv_wp_dynamic_css', array( $dcss, 'ajax_callback' ) );

compiler.php

Lines changed: 47 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -53,47 +53,61 @@ public static function get_instance()
5353
}
5454

5555
/**
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
5857
*/
59-
public function wp_enqueue_style()
58+
public function enqueue_styles()
6059
{
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 )
6461
{
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+
}
6672
}
6773
}
6874

6975
/**
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
7277
*/
73-
public function compile_printed_styles()
78+
public function print_styles()
7479
{
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 )
7781
{
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 );
7985

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+
}
8390
}
8491
}
8592

8693
/**
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.
8996
*/
90-
public function compile_external_styles()
97+
public function ajax_callback()
9198
{
9299
header( "Content-type: text/css; charset: UTF-8" );
100+
$handle = filter_input( INPUT_GET, 'handle' );
93101

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+
}
95110

96-
include 'style.phtml';
97111
wp_die();
98112
}
99113

@@ -129,31 +143,6 @@ public function register_callback( $handle, $callback )
129143
{
130144
$this->callbacks[$handle] = $callback;
131145
}
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-
}
157146

158147
/**
159148
* Get the compiled CSS for the given style. Skips compilation if the compiled
@@ -194,29 +183,24 @@ protected function minify_css( $css )
194183
{
195184
return preg_replace( '@({)\s+|(\;)\s+|/\*.+?\*\/|\R@is', '$1$2 ', $css );
196185
}
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-
}
209186

210187
/**
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.
213189
*
214-
* @param array $style
190+
* @param string $handle
215191
* @return boolean
216192
*/
217-
protected function filter_external( $style )
193+
protected function callback_exists( $handle )
218194
{
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;
220204
}
221205

222206
/**

0 commit comments

Comments
 (0)