-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass-eifyQueueScriptStyle.php
More file actions
321 lines (282 loc) · 8.85 KB
/
Copy pathclass-eifyQueueScriptStyle.php
File metadata and controls
321 lines (282 loc) · 8.85 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
<?php
/**
* The eify_QueueScriptStyles is based on wordpress wp_enqueue_script / wp_enqueue_style
*
* @version 0.0.1
* @copyright 2014 - 2014
* @author Varun Sridharan (email: varunsridharan23@gmail.com)
* @link http://varunsridharan.in
*
* @license GNU General Public LIcense v3.0 - license.txt
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package eify_QueueScriptStyles
*/
class eify_QueueScriptStyles{
protected $style = array();
protected $script = array();
protected $generated = array();
protected $depended_script = array();
protected $depended_style = array();
public $currentPage = '';
/**
* Enqueue A Script / Style Based On The Input Given
* @param string $type
* @param string $handle
* @param string $src
* @param string | int $version
* @param array $page
* @param array $attr
* @param boolean $footer
* @return boolean
*/
private function enqueue($type,$handle,$src,$version,$dependency= '',$page = '',$attr = '',$footer = false) {
if(!empty($src)) {
if(!array_key_exists($handle, $this->{$type})) {
$this->{$type}[$handle] = array();
$this->{$type}[$handle] = array();
$this->{$type}[$handle]['handler'] = $handle;
$this->{$type}[$handle]['src'] = $src;
$this->{$type}[$handle]['version'] = $version;
$this->{$type}[$handle]['dependency'] = $dependency;
if(!empty($page) && is_array($page)) { $this->{$type}[$handle]['page'] = $page; }
if(!empty($attr) && is_array($attr)) { $this->{$type}[$handle]['attr'] = $attr; }
$this->{$type}[$handle]['is_footer'] = $footer;
return true;
} else {
return false;
}
}
}
/**
* Check For Existing In Style / Script
* @param string $type
* @param string $handler
* @return array | boolean
*/
private function has_enqueue($type,$handler) {
if(!empty($handler)) {
if(array_key_exists($handler, $this->{$type})) {
return $this->{$type}[$handler];
} else { return false; }
} else {
return false;
}
}
/**
* Removes A Script or Style From Queue
* @param string $type
* @param string $handler
* @return boolean
*/
private function dequeue($type,$handler){
if(!empty($handler)) {
if(array_key_exists($handler, $this->{$type})) {
unset($this->{$type}[$handler]);
return true;
} else { return false; }
} else {
return false;
}
}
/**
* Generates Attributes if avaiable
*/
private function generateAttr($attrs){
$attr = '';
$attrs = $attrs;
if(!empty($attrs)){
foreach($attrs as $atKey => $atVal) {
$attr .= $atKey.'="'.$atVal.'" ';
}
return $attr;
}
}
/*
* Generates Tag For The Given Values
*/
private function generateTag($type,$footer,$val){
if(isset($val['page']) && is_array($val['page'])){
if(isset($this->currentPage) && !in_array($this->currentPage, $val['page'])) {
continue;
}
} else if(isset($val['page']) && is_string($val['page'])){
if($this->currentPage !== $val['page']) {
continue;
}
}
if($footer == $val['is_footer']) {
$tag = '';
$attr = '';
if(isset($val['attr'])){
$attr = $this->generateAttr($val['attr']);
}
if($type == 'style') {
$tag = '<link rel="stylesheet" href="'.$val['src'].'?v='.$val['version'].'" '.$attr.' />'.PHP_EOL;
} else if ($type = 'script') {
$tag = '<script src="'.$val['src'].'?v='.$val['version'];
$tag .= '" type="text/javascript" '.$attr.'> </script>'.PHP_EOL ;
}
return $tag;
}
}
/**
* Checks For Dependency
*/
private function checkDepn($type,$val){
if(isset($val['dependency']) && !empty($val['dependency'])) {
if(! in_array($val['dependency'], $this->generated)){
$tempArr = array();
$tempArr['handler'] = $val['handler'];
$tempArr['dependency'] = $val['dependency'];
if(!isset($this->{"depended_".$type}[$val['handler']]))
$this->depended[$val['handler']] = $tempArr;
$generate = false;
} else {
unset($this->{"depended_".$type}[$val['handler']]);
$generate = true;
}
} else {
$generate = true;
}
return $generate;
}
/**
* Checks for dependency to load
*/
private function checkLoad($type,$footer){
$data = $this->{"depended_".$type};
$type = $type;
$tags = '';
foreach($data as $key => $value) {
$val = $this->{$type}[$value['handler']];
if($this->checkDepn($val)) {
unset($this->{"depended_".$type}[$key]);
$tags .= $this->generateTag($type,$footer,$this->{$type}[$value['handler']],$this->currentPage);
$this->generated[] = $value['handler'];
} else {
continue;
}
}
return $tags;
}
/**
* Generate HTML Output for Script / Style
* @param string $type
* @param string $footer
* @return string|boolean
*/
public function generate($type,$footer = false) {
$data = $this->{$type};
$generated_{$type} = '';
$this->currentPage = requestHander();
if(!empty($data)) {
foreach($data as $key => $val) {
$generate = true;
$generate = $this->checkDepn($type,$val);
if($generate){
$generated_{$type} .= $this->generateTag($type,$footer,$val);
$this->generated[] = $val['handler'];
$check = $this->checkLoad($type,$footer);
if($check) {
$generated_{$type} .= $check;
}
}
}
$reCount = count($this->{"depended_".$type});
$i = 0;
while($i<$reCount) {
$check = $this->checkLoad($type,$footer);
if($check) {
$generated_{$type} .= $check;
}
$i++;
}
return $generated_{$type};
}
return false;
}
/**
* Enqueue A Script to Queue
* @param string $type
* @param string $handle
* @param string $src
* @param string | int $version
* @param array $page
* @param array $attr
* @param boolean $footer
* @return boolean
*/
public function script_enqueue($handle,$src,$version,$dependency= '',$page = '',$attr = '',$footer = false) {
return $this->enqueue('script',$handle,$src,$version,$dependency,$page,$attr,$footer);
}
/**
* Enqueue A Style to Queue
* @param string $type
* @param string $handle
* @param string $src
* @param string | int $version
* @param array $page
* @param array $attr
* @param boolean $footer
* @return boolean
*/
public function style_enqueue($handle,$src,$version,$dependency= '',$page = '',$attr = '',$footer = false) {
return $this->enqueue('style',$handle,$src,$version,$dependency,$page,$attr,$footer);
}
/**
* Checks For Existing Script In Queue
* @param string $handler
* @return boolean | Array
*/
public function has_script($handler){
return $this->has_enqueue('script',$handler);
}
/**
* Checks For Existing Style In Queue
* @param string $handler
* @return boolean | Array
*/
public function has_style($handler){
return $this->has_enqueue('style',$handler);
}
/**
* Removes A Script if Existing In Queue
* @param string $handler
* @return boolean | Array
*/
public function dequeue_script($handler){
return $this->dequeue('script',$handler);
}
/**
* Removes A Style if Existing In Queue
* @param string $handler
* @return boolean | Array
*/
public function dequeue_style($handler){
return $this->dequeue('style',$handler);
}
/**
* Generates HTML For All Script In Queue
* @param string $handler
* @return string | boolean
*/
public function get_script($footer = false) {
return $this->generate('script',$footer);
}
/**
* Generates HTML For All Style In Queue
* @param string $handler
* @return string | boolean
*/
public function get_style($footer = false) {
return $this->generate('style',$footer);
}
}
?>