-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0a96a52
Showing
5 changed files
with
239 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
}); |