-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwp_enqueue_less.php
93 lines (71 loc) · 2.64 KB
/
wp_enqueue_less.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
function wp_enqueue_less($key, $file, $variables){
$details = get_option('wp_enqueue_less_' . $key, new StdClass);
if(!isset($details->key)){
$details->key = $key;
$details->file = $file;
$details->setting = 'wp_enqueue_less_' . $key;
do_action('wp_enqueue_less_compile', $details, $variables);
}else{
$details->file = $file;
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_url', 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');
}
}
if(function_exists('add_action')){
add_action('wp_enqueue_less_compile', function($details, $variables){
$parser = new Less_Parser(array(
'compress' => apply_filters('wp_enqueue_less_compress', true)
));
$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);
}
}
}
});
}