Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ A library for generating static stylesheets from dynamic content, to be used in

**Contributors:** ykadosh
**Tags:** theme, mods, wordpress, dynamic, css, stylesheet
**Tested up to:** 4.7
**Stable tag:** 1.0.5
**Tested up to:** 4.8
**Stable tag:** 1.0.6
**Requires:** PHP 5.3.0 or newer
**WordPress plugin:** [wordpress.org/plugins/wp-dynamic-css/](https://wordpress.org/plugins/wp-dynamic-css/)
**License:** GPLv3 or later
Expand Down Expand Up @@ -133,7 +133,7 @@ require_once 'path/to/wp-dynamic-css/bootstrap.php';

## Dynamic CSS Syntax

This library allows you to use special CSS syntax that is similar to regular CSS syntax with added support for variables with the syntax `$my_variable_name`. Since these variables are replaced by values during run time (when the page loads), files that are using this syntax are therefore called **dynamic CSS** files. Any variable in the dynamic CSS file is replaced by a value that is retrieved by a custom 'value callback' function.
This library allows you to use special CSS syntax that is similar to regular CSS syntax with added support for variables with the syntax `$my_variable_name`. Since these variables are replaced by values during run time (when the page loads), files that are using this syntax are therefore called **dynamic CSS** files. Any variable in the dynamic CSS file is replaced by a value that is retrieved by a custom 'value callback' function.

A **dynamic CSS** file will look exactly like a regular CSS file, only with variables. For example:

Expand All @@ -157,7 +157,7 @@ The callback function should accept a second variable that will hold an array of

Future releases may support a more compex syntax, so any suggestions are welcome. You can make a suggestion by creating an issue or submitting a pull request.

**Piped filters (since 1.0.5)**
**Piped filters (since 1.0.5)**

Version 1.0.5 added support for piped filters. Filters can be registered using the function `wp_dynamic_css_register_filter( $handle, $filter_name, $callback )` where `$filter_name` corresponds to the name fo the filter to be used in the stylesheet. For example, if a filter named `myFilter` was registered, it can be applied using the following syntax:

Expand Down Expand Up @@ -309,10 +309,10 @@ body {

## Registering Filters

Filters are functions the alter the value of the variables. Filters can be registered using the function `wp_dynamic_css_register_filter( $handle, $filter_name, $callback )`. A registered filter can only be used within the stylesheet whose handle is given. A filter callback function takes the value of the variable as a parameter and should return the filtered value. For example:
Filters are functions the alter the value of the variables. Filters can be registered using the function `wp_dynamic_css_register_filter( $handle, $filter_name, $callback )`. A registered filter can only be used within the stylesheet whose handle is given. A filter callback function takes the value of the variable as a parameter and should return the filtered value. For example:

```php
function my_filter_callback( $value )
function my_filter_callback( $value )
{
return trim( $value );
}
Expand All @@ -327,10 +327,10 @@ body {
}
```

Filters can also take additional arguments. For example:
Filters can also take additional arguments. For example:

```php
function my_filter_callback( $value, $arg1, $arg2 )
function my_filter_callback( $value, $arg1, $arg2 )
{
return $value.$arg1.$arg2;
}
Expand Down Expand Up @@ -359,24 +359,27 @@ body {

*Enqueue a dynamic stylesheet*

```php
function wp_dynamic_css_enqueue( $handle, $path, $print = true, $minify = false, $cache = false )
```php
function wp_dynamic_css_enqueue( $handle, $path, $deps = [], $print = true, $minify = false, $cache = false, $expiration = 0 )
```

This function will either print the compiled version of the stylesheet to the document's <head> section, or load it as an external stylesheet if `$print` is set to false. If `$cache` is set to true, a compiled version of the stylesheet will be stored in the database as soon as it is first compiled. The compiled version will be served thereafter until `wp_dynamic_css_clear_cache()` is called.

**Parameters**
* `$handle` (*string*) The stylesheet's name/id
* `$path` (*string*) The absolute path to the dynamic CSS file
* `$deps` (*array*) An array of registered stylesheet handles this stylesheet depends on.
* `$print` (*boolean*) Whether to print the compiled CSS to the document head, or load it as an external CSS file via an http request
* `$minify` (*boolean*) Whether to minify the CSS output
* `$cache` (*boolean*) Whether to store the compiled version of this stylesheet in cache to avoid compilation on every page load.
* `$cache` (*boolean*) Whether to store the compiled version of this stylesheet in cache to avoid
compilation on every page load.
* `$expiration` (*int*) Time until expiration in seconds.

### wp_dynamic_css_set_callback

*Set the value retrieval callback function*

```php
```php
function wp_dynamic_css_set_callback( $handle, $callback )
```

Expand All @@ -390,7 +393,7 @@ Set a callback function that will be used to convert variables to actual values.

*Clear the cached compiled CSS for the given handle.*

```php
```php
function wp_dynamic_css_clear_cache( $handle )
```

Expand All @@ -403,7 +406,7 @@ Registered dynamic stylesheets that have the `$cache` flag set to true are compi

*Register a filter function for a given stylesheet handle.*

```php
```php
function wp_dynamic_css_register_filter( $handle, $filter_name, $callback )
```

Expand Down
14 changes: 8 additions & 6 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,25 @@
/**
* Prevent loading the library more than once
*/
if( defined( 'WP_DYNAMIC_CSS' ) ) return;
if ( defined( 'WP_DYNAMIC_CSS' ) ) {
return;
}
define( 'WP_DYNAMIC_CSS', true );

/**
* Load required files
*/
require_once dirname(__FILE__).'/compiler.php';
require_once dirname(__FILE__).'/cache.php';
require_once dirname(__FILE__).'/functions.php';
require_once dirname( __FILE__ ) . '/compiler.php';
require_once dirname( __FILE__ ) . '/cache.php';
require_once dirname( __FILE__ ) . '/functions.php';

/**
* The following actions are used for printing or loading the compiled
* The following actions are used for printing or loading the compiled
* stylesheets externally.
* Priority is set to high (100) to allow the dynamic CSS to override static
* styles.
*/
$dcss = DynamicCSSCompiler::get_instance();
add_action( 'wp_enqueue_scripts', array( $dcss, 'enqueue_styles' ), 100 );
add_action( 'wp_ajax_wp_dynamic_css', array( $dcss, 'ajax_callback' ) );
add_action( 'wp_ajax_nopriv_wp_dynamic_css', array( $dcss, 'ajax_callback' ) );
add_action( 'wp_ajax_nopriv_wp_dynamic_css', array( $dcss, 'ajax_callback' ) );
178 changes: 90 additions & 88 deletions cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,91 +10,93 @@
/**
* Dynamic CSS Cache Handler
*/
class DynamicCSSCache
{
/**
* @var DynamicCSSCache The reference to *Singleton* instance of this class
*/
private static $instance;

/**
* @var array The reference to the cached compiled CSS that is stored in the database
*/
private static $cache;

/**
* Returns the *Singleton* instance of this class.
*
* @return DynamicCSSCache The *Singleton* instance.
*/
public static function get_instance()
{
if (null === static::$instance)
{
static::$instance = new static();
self::load_cache();
}
return static::$instance;
}

/**
* Returns the compiled CSS for the given handle if it exists in the cache.
* Returns false otherwise.
*
* @param string $handle The handle of the stylesheet
* @return boolean|string
*/
public function get( $handle )
{
if( array_key_exists( $handle, self::$cache ) )
{
return self::$cache[$handle];
}
return false;
}

/**
* Update the compiled CSS for the given handle.
*
* @param string $handle The handle of the stylesheet
* @param string $compiled_css
*/
public function update( $handle, $compiled_css )
{
self::$cache[$handle] = $compiled_css;
$this->update_option();
}

/**
* Clear the compiled CSS from cache for the given handle.
*
* @param string $handle The handle of the stylesheet
*/
public function clear( $handle )
{
unset(self::$cache[$handle]);
$this->update_option();
}

/**
* Load the cache value from the database or create an empty array if the
* cache is empty.
*/
private static function load_cache()
{
self::$cache = get_option('wp-dynamic-css-cache');

if( false === self::$cache )
{
self::$cache = array();
}
}

/**
* Update the database option with the local cache value.
*/
private function update_option()
{
update_option('wp-dynamic-css-cache', self::$cache);
}
}
class DynamicCSSCache {

/**
* @var DynamicCSSCache The reference to *Singleton* instance of this class
*/
private static $instance;

/**
* @var array The reference to the cached compiled CSS that is stored in the database
*/
private static $cache;

/**
* Returns the *Singleton* instance of this class.
*
* @return DynamicCSSCache The *Singleton* instance.
*/
public static function get_instance() {

if ( null === static::$instance ) {
static::$instance = new static();
self::load_cache();
}
return static::$instance;
}

/**
* Returns the compiled CSS for the given handle if it exists in the cache.
* Returns false otherwise.
*
* @param string $handle The handle of the stylesheet
* @return boolean|string
*/
public function get( $handle ) {

if ( array_key_exists( $handle, self::$cache ) ) {
return self::$cache[ $handle ];
}
return false;
}

/**
* Update the compiled CSS for the given handle.
*
* @param string $handle The handle of the stylesheet
* @param string $compiled_css
*/
public function update( $handle, $compiled_css, $expiration ) {

self::$cache[ $handle ] = $compiled_css;
$this->update_option( $expiration );
}

/**
* Clear the compiled CSS from cache for the given handle.
*
* @param string $handle The handle of the stylesheet
*/
public function clear( $handle ) {

unset( self::$cache[ $handle ] );
delete_transient( 'wp-dynamic-css-cache' );
}

/**
* Load the cache value from the database or create an empty array if the
* cache is empty.
*/
private static function load_cache() {

if ( false === ( self::$cache = get_transient( 'wp-dynamic-css-cache' ) ) ) {
self::$cache = [];
}
// self::$cache = get_option('wp-dynamic-css-cache');
//
// if( false === self::$cache )
// {
// self::$cache = array();
// }
}

/**
* Update the database option with the local cache value.
*/
private function update_option( $expiration ) {

// update_option('wp-dynamic-css-cache', self::$cache);
set_transient( 'wp-dynamic-css-cache', self::$cache, $expiration );
}
}
Loading