Skip to content

Commit

Permalink
intial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Jul 4, 2018
0 parents commit 0a96a52
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
52 changes: 52 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# wp_enqueue_less

wp_enqueue_less provides a function to enqueue less stylesheets in WordPress.

## Install

### Composer

Composer is the best way to install wp_enqueue_less so you get updates in the future easily.

```
composer require ed-itsolutions/wp_enqueue_less
```

and then in your `functions.php` of `plugin.php`

```php
require_once('vendor/autoload.php');
```

### Manually

Download a copy of wp_enqueue_less.php and require it in your theme/plugin.

## Usage

In your normal `wp_enqueue_scripts` action simply call `wp_enqueue_less`

`wp_enqueue_less` takes 3 arguments.

- _key_ - The key name to use for this stylesheet.
- _filePath_ - The on disk path to the .less file.
- _variables_ - A key->value array of variables to be passed to the less compiler.

```php
add_action('wp_enqueue_scripts', function(){
wp_enqueue_style('theme-stylesheet', get_stylesheet_uri());

wp_enqueue_less('theme-main', get_template_directory() . '/less/main.less', array(
'main-color' => '#99bbff' // becomes @main-color in your less stylesheet.
));
});
```

Thats it!

wp_enqueue_less will:

- Compile this less file and write the output to `/wp-content/uploads/less/key-hash.css` (this can be changed with the filter `wp_enqueue_less_css_dir`).
- Record the current hashes of all the less files used and the variables into the database.
- On the next call if none of the hashes have changed it will skip parsing.
- On a daily basis it will clean out its directory of everything but the current hash version of the stylesheet.
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "ed-itsolutions/wup-client",
"description": "WordPress Update Provider client for use in plugins / themes.",
"require": {
"oyejorge/less.php": "v1.7.0.14"
},
"license": "MIT",
"authors": [
{
"name": "Adam Laycock",
"email": "[email protected]",
"homepage": "https://arcath.net",
"role": "Developer"
}
],
"autoload": {
"files": ["wp_enqueue_less.php"]
}
}
80 changes: 80 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 87 additions & 0 deletions wp_enqueue_less.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
function wp_enqueue_less($key, $file, $variables){
$details = get_option('wp_enqueue_less_' . $key, new StdClass);

if(!$details->key){
$details->key = $key;
$details->file = $file;
$details->setting = 'wp_enqueue_less_' . $key;

do_action('wp_enqueue_less_compile', $details, $variables);
}else{
if(md5(json_encode($variables)) != $details->variablesHash){
do_action('wp_enqueue_less_compile', $details, $variables);
}else{
$filesChanged = false;

foreach(array_keys($details->fileHashes) as $fileName){
if(!$filesChanged && md5_file($fileName) != $details->fileHashes[$fileName]){
$filesChanged = true;
}
}

if($filesChanged){
do_action('wp_enqueue_less_compile', $details, $variables);
}
}
}

$baseUri = apply_filters('wp_enqueue_less_css_dir', wp_upload_dir()['baseurl'] . '/less');
wp_enqueue_style($key, $baseUri . '/' . $details->key . '-' . $details->hash . '.css');

if(!wp_next_scheduled('wp_enqueue_less_clean')){
wp_schedule_event(time(), 'daily', 'wp_enqueue_less_clean');
}
}

add_action('wp_enqueue_less_compile', function($details, $variables){
$parser = new Less_Parser();
$parser->parseFile($details->file, get_template_directory_uri());
$parser->ModifyVars($variables);
$css = $parser->getCss();

$hash = md5($css);

$dir = apply_filters('wp_enqueue_less_css_dir', wp_upload_dir()['basedir'] . '/less');
if(!file_exists($dir)){
mkdir($dir);
}

$file = $dir . '/' . $details->key . '-' . $hash . '.css';

file_put_contents($file, $css);

$details->hash = $hash;

$details->fileHashes = array();
foreach($parser->allParsedFiles() as $fileName){
$details->fileHashes[$fileName] = md5_file($fileName);
}

$details->variablesHash = md5(json_encode($variables));

update_option($details->setting, $details);
}, 10, 2);

add_action('wp_enqueue_less_clean', function(){
$dir = apply_filters('wp_enqueue_less_css_dir', wp_upload_dir()['basedir'] . '/less');

$files = scandir($dir);

$keyDetails = array();

foreach($files as $file){
if($file != '.' && $file != '..'){
$key = substr($file, 0, -37);
$hash = substr($file, -36, -4);

if(!isset($keyDetails[$key])){
$keyDetails[$key] = get_option('wp_enqueue_less_' . $key, new StdClass);
}

if($hash != $keyDetails[$key]->hash){
unlink($dir . '/' . $file);
}
}
}
});

0 comments on commit 0a96a52

Please sign in to comment.