Skip to content

Commit 80f7b9d

Browse files
author
Askupa Software
committed
Added support for multiple value callback functions
1 parent 362a7ba commit 80f7b9d

File tree

6 files changed

+59
-22
lines changed

6 files changed

+59
-22
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
**Contributors:** ykadosh
77
**Tags:** theme, mods, wordpress, dynamic, css, stylesheet
88
**Tested up to:** 4.4.2
9-
**Stable tag:** 1.0.1
9+
**Stable tag:** 1.0.2
10+
**Requires:** PHP 5.3.0 or newer
1011
**License:** GPLv3 or later
1112
**License URI:** http://www.gnu.org/licenses/gpl-3.0.html
1213

bootstrap.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@
2020
* Domain Path: /languages
2121
*/
2222

23+
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
24+
25+
26+
/**
27+
* Prevent loading the library more than once
28+
*/
29+
if( defined( 'WP_DYNAMIC_CSS' ) ) return;
30+
define( 'WP_DYNAMIC_CSS', true );
31+
32+
/**
33+
* Load required files
34+
*/
2335
require_once 'compiler.php';
2436
require_once 'functions.php';
2537

compiler.php

Lines changed: 34 additions & 14 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.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
}

functions.php

Lines changed: 9 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.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
@@ -16,14 +16,15 @@
1616
* document's <head> section, or load it as an external stylesheet if the
1717
* second parameter is set to false
1818
*
19+
* @param string $handle The stylesheet's name/id
1920
* @param string $path The absolute path to the dynamic CSS file
2021
* @paran boolean $print Whether to print the compiled CSS to the document
2122
* head, or include it as an external CSS file
2223
*/
23-
function wp_dynamic_css_enqueue( $path, $print = true )
24+
function wp_dynamic_css_enqueue( $handle, $path, $print = true )
2425
{
2526
$dcss = DynamicCSSCompiler::get_instance();
26-
$dcss->enqueue_style( $path, $print );
27+
$dcss->enqueue_style( $handle, $path, $print );
2728
}
2829
}
2930

@@ -36,12 +37,15 @@ function wp_dynamic_css_enqueue( $path, $print = true )
3637
* variables when the dynamic CSS file is compiled. The function accepts 1
3738
* parameter which is the name of the variable, without the $ sign
3839
*
40+
* @param string $handle The name of the stylesheet to be associated with this
41+
* callback function
3942
* @param string|array $callback A callback (or "callable" as of PHP 5.4)
4043
* can either be a reference to a function name or method within an
4144
* class/object.
4245
*/
43-
function wp_dynamic_css_set_callback( $callback )
46+
function wp_dynamic_css_set_callback( $handle, $callback )
4447
{
45-
add_filter( 'wp_dynamic_css_get_variable_value', $callback );
48+
$dcss = DynamicCSSCompiler::get_instance();
49+
$dcss->register_callback( $handle, $callback );
4650
}
4751
}

readme.txt

Lines changed: 1 addition & 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.1
6+
Stable tag: 1.0.2
77
License: GPLv3 or later
88
License URI: http://www.gnu.org/licenses/gpl-3.0.html
99

style.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
* https://github.com/askupasoftware/wp-dynamic-css
44
*/
55

6-
<?php echo $css; ?>
6+
<?php echo $compiled_css; ?>

0 commit comments

Comments
 (0)