-
Notifications
You must be signed in to change notification settings - Fork 0
/
enqueues.php
executable file
·202 lines (141 loc) · 3.61 KB
/
enqueues.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* These two functions can be used to enqueue CSS and JS
* files from two static files named .js-enqueues
* and .css-enqueues from the theme's root directory
*
* PHP Version 7.4.8
*
* @category WordPress
* @package WordPress
* @author Ahmad Karim <ahmu83@gmail.com>
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License v3.0
* @link https://ahmadkarim.com/
*/
/**
* Get the enqueues list from CSS or JS static files
*
* @param string $type Type of the file
*
* @return array
*/
function enqueues_list($type = 'js') {
$filename = get_stylesheet_directory() . "/.{$type}-enqueues";
$files_list = array();
if (file_exists($filename)) {
$names = file($filename);
$names = array_filter($names, function($n) {
if (empty(trim($n)) || substr(trim($n), 0, 1) == '#') {
return false;
} else {
return true;
}
});
$files_list = array_values($names);
}
return $files_list;
}
/**
* Handler function for wp_enqueue_scripts hook
*
* @return array List of enqueue files
*/
function enqueues() {
$files = array();
$uniqid = uniqid();
$delimiter = '!!';
/*
* Enqueue JS files
*/
foreach (enqueues_list('js') as $file) {
/*
* Trim the arguments
*/
$file = array_map(function($n) {
return trim($n);
}, explode($delimiter, $file));
/*
* Check if the src is relative or absolute
*/
if (isset($file[1])) {
$url = parse_url($file[1]);
$file[1] = isset($url['host']) ? $file[1] : get_stylesheet_directory_uri() . $file[1];
}
/*
* Check if the deps argument is set
*/
if (isset($file[2])) {
/*
* Trim the deps argument
*/
$deps = array_map(function($n) {
return trim($n);
}, explode(',', $file[2]));
$deps = array_filter($deps);
$file[2] = $deps;
}
/*
* Check if the ver argument is set and
* if it is set to rand in order to un-cache
* a file every-time the page is loaded
*/
if (isset($file[3]) && $file[3] == 'rand') {
$file[3] = $uniqid;
}
$handle = $file[0];
$src = $file[1];
$deps = $file[2] ?? array();
$ver = $file[3] ?? false;
$in_footer = $file[4] ?? false;
wp_enqueue_script($handle, $src, $deps, $ver, $in_footer);
$files[] = $file;
}
/*
* Enqueue CSS files
*/
foreach (enqueues_list('css') as $file) {
/*
* Trim the arguments
*/
$file = array_map(function($n) {
return trim($n);
}, explode($delimiter, $file));
/*
* Check if the src is relative or absolute
*/
if (isset($file[1])) {
$url = parse_url($file[1]);
$file[1] = isset($url['host']) ? $file[1] : get_stylesheet_directory_uri() . $file[1];
}
/*
* Check if the deps argument is set
*/
if (isset($file[2])) {
/*
* Trim the deps argument
*/
$deps = array_map(function($n) {
return trim($n);
}, explode(',', $file[2]));
$deps = array_filter($deps);
$file[2] = $deps;
}
/*
* Check if the ver argument is set and
* if it is set to rand in order to un-cache
* a file every-time the page is loaded
*/
if (isset($file[3]) && $file[3] == 'rand') {
$file[3] = $uniqid;
}
$handle = $file[0];
$src = $file[1];
$deps = $file[2] ?? array();
$ver = $file[3] ?? false;
$media = $file[4] ?? false;
wp_enqueue_style($handle, $src, $deps, $ver, $media);
$files[] = $file;
}
return $files;
}
add_action( 'wp_enqueue_scripts', 'enqueues' );