Skip to content

Commit 0e1ac43

Browse files
author
Askupa Software
committed
Initial commit
0 parents  commit 0e1ac43

File tree

4 files changed

+163
-0
lines changed

4 files changed

+163
-0
lines changed

bootstrap.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* WordPress Dynamic CSS
4+
*
5+
* Render CSS using dynamic data from WordPress
6+
*
7+
* @package wp-dynamic-css
8+
* @author Askupa Software <[email protected]>
9+
* @link https://github.com/askupasoftware/wp-dynamic-css
10+
* @copyright 2016 Askupa Software
11+
*
12+
* @wordpress-plugin
13+
* Plugin Name: WordPress Dynamic CSS
14+
* Plugin URI: https://github.com/askupasoftware/wp-dynamic-css
15+
* Description: Render CSS using dynamic data from WordPress
16+
* Version: 1.0.0
17+
* Author: Askupa Software
18+
* Author URI: http://www.askupasoftware.com
19+
* Text Domain: wp-dynamic-css
20+
* Domain Path: /languages
21+
*/
22+
23+
require_once 'renderer.php';
24+
25+
if( !function_exists('wp_dynamic_css_enqueue') )
26+
{
27+
function wp_dynamic_css_enqueue( $path )
28+
{
29+
$dcss = DynamicCSSRenderer::get_instance();
30+
$dcss->enqueue_style( $path );
31+
}
32+
}
33+
34+
if( !function_exists('wp_dynamic_css_set_callback') )
35+
{
36+
function wp_dynamic_css_set_callback( $callback )
37+
{
38+
add_filter( 'wp_dynamic_css_get_variable_value', $callback );
39+
}
40+
}

composer.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "askupa-software/wp-dynamic-css",
3+
"description": "Render CSS using dynamic data from WordPress",
4+
"type": "wordpress-plugin",
5+
"keywords": ["wordpress","plugin","dynamic css","dynamic style"],
6+
"homepage": "https://github.com/askupasoftware/wp-dynamic-css",
7+
"authors": [
8+
{
9+
"name": "Askupa Software",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {}
14+
}

renderer.php

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
/**
3+
* @package WordPress Dynamic CSS
4+
* @version 1.0.0
5+
* @author Askupa Software <[email protected]>
6+
* @link https://github.com/askupasoftware/wp-dynamic-css
7+
* @copyright 2016 Askupa Software
8+
*/
9+
10+
/**
11+
* Dynamic CSS Renderer Utility Class
12+
*
13+
*
14+
* Dynamic CSS Syntax
15+
* ------------------
16+
* <pre>
17+
* body {color: $body_color;}
18+
* </pre>
19+
* 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.
23+
*/
24+
class DynamicCSSRenderer
25+
{
26+
/**
27+
* @var Singleton The reference to *Singleton* instance of this class
28+
*/
29+
private static $instance;
30+
31+
/**
32+
* @var array The list of dynamic styles paths to render
33+
*/
34+
private $styles = array();
35+
36+
/**
37+
* Returns the *Singleton* instance of this class.
38+
*
39+
* @return Singleton The *Singleton* instance.
40+
*/
41+
public static function get_instance()
42+
{
43+
if (null === static::$instance)
44+
{
45+
static::$instance = new static();
46+
static::$instance->init();
47+
}
48+
49+
return static::$instance;
50+
}
51+
52+
/**
53+
* Initiate the renderer by hooking to wp_print_styles
54+
*/
55+
public function init()
56+
{
57+
add_action( 'wp_print_styles', array( $this, 'print_rendered_style' ) );
58+
}
59+
60+
/**
61+
* Add a style path to the pool of styles to be rendered
62+
*
63+
* @param type $path The absolute path to the dynamic style
64+
*/
65+
public function enqueue_style( $path )
66+
{
67+
$this->styles[] = $path;
68+
}
69+
70+
/**
71+
* Parse all styles in $this->styles and print them
72+
*/
73+
public function print_rendered_style()
74+
{
75+
ob_start();
76+
foreach( $this->styles as $style )
77+
{
78+
include $style;
79+
echo "\n";
80+
}
81+
$css = $this->parse_css( ob_get_clean() );
82+
include 'style.phtml';
83+
}
84+
85+
/**
86+
* Parse the given CSS string by converting the variables to their
87+
* corresponding values retrieved by applying the filter
88+
* wp_dynamic_css_get_variable_value.
89+
*
90+
* @param string $css A string containing dynamic CSS (pre-rendered CSS with
91+
* variables)
92+
* @uses wp_dynamic_css_get_variable_value filter
93+
* @return string The rendered CSS after converting the variables to their
94+
* corresponding values
95+
*/
96+
public function parse_css( $css )
97+
{
98+
return preg_replace_callback('#\$([\w]+)#', function($matches) {
99+
return apply_filters( 'wp_dynamic_css_get_variable_value', $matches[1]);
100+
}, $css);
101+
}
102+
}

style.phtml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<style id="wp-dynamic-css">
2+
/**
3+
* Rendered using wp-dynamic-css
4+
* https://github.com/askupasoftware/wp-dynamic-css
5+
*/
6+
<?php echo $css; ?>
7+
</style>

0 commit comments

Comments
 (0)