author | Anthony Ly <anthonyly.com@gmail.com> |
Wed, 19 Dec 2012 17:46:52 -0800 | |
changeset 204 | 09a1c134465b |
parent 194 | 32102edaa81b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* BackPress Scripts enqueue. |
|
4 |
* |
|
5 |
* These classes were refactored from the WordPress WP_Scripts and WordPress |
|
6 |
* script enqueue API. |
|
7 |
* |
|
8 |
* @package BackPress |
|
9 |
* @since r74 |
|
10 |
*/ |
|
11 |
||
12 |
/** |
|
13 |
* BackPress enqueued dependiences class. |
|
14 |
* |
|
15 |
* @package BackPress |
|
16 |
* @uses _WP_Dependency |
|
17 |
* @since r74 |
|
18 |
*/ |
|
19 |
class WP_Dependencies { |
|
20 |
var $registered = array(); |
|
21 |
var $queue = array(); |
|
22 |
var $to_do = array(); |
|
23 |
var $done = array(); |
|
24 |
var $args = array(); |
|
25 |
var $groups = array(); |
|
26 |
var $group = 0; |
|
27 |
||
28 |
/** |
|
29 |
* Do the dependencies |
|
30 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
31 |
* Process the items passed to it or the queue. Processes all dependencies. |
136 | 32 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
33 |
* @param mixed $handles (optional) items to be processed. (void) processes queue, (string) process that item, (array of strings) process those items |
136 | 34 |
* @return array Items that have been processed |
35 |
*/ |
|
36 |
function do_items( $handles = false, $group = false ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
37 |
// Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts. |
136 | 38 |
$handles = false === $handles ? $this->queue : (array) $handles; |
39 |
$this->all_deps( $handles ); |
|
40 |
||
41 |
foreach( $this->to_do as $key => $handle ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
42 |
if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) { |
136 | 43 |
|
44 |
if ( ! $this->registered[$handle]->src ) { // Defines a group. |
|
45 |
$this->done[] = $handle; |
|
46 |
continue; |
|
47 |
} |
|
48 |
||
49 |
if ( $this->do_item( $handle, $group ) ) |
|
50 |
$this->done[] = $handle; |
|
51 |
||
52 |
unset( $this->to_do[$key] ); |
|
53 |
} |
|
54 |
} |
|
55 |
||
56 |
return $this->done; |
|
57 |
} |
|
58 |
||
59 |
function do_item( $handle ) { |
|
60 |
return isset($this->registered[$handle]); |
|
61 |
} |
|
62 |
||
63 |
/** |
|
64 |
* Determines dependencies |
|
65 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
66 |
* Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
67 |
* |
136 | 68 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
69 |
* @param mixed $handles Accepts (string) dep name or (array of strings) dep names |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
70 |
* @param bool $recursion Used internally when function calls itself |
136 | 71 |
*/ |
72 |
function all_deps( $handles, $recursion = false, $group = false ) { |
|
73 |
if ( !$handles = (array) $handles ) |
|
74 |
return false; |
|
75 |
||
76 |
foreach ( $handles as $handle ) { |
|
77 |
$handle_parts = explode('?', $handle); |
|
78 |
$handle = $handle_parts[0]; |
|
79 |
$queued = in_array($handle, $this->to_do, true); |
|
80 |
||
81 |
if ( in_array($handle, $this->done, true) ) // Already done |
|
82 |
continue; |
|
83 |
||
84 |
$moved = $this->set_group( $handle, $recursion, $group ); |
|
85 |
||
86 |
if ( $queued && !$moved ) // already queued and in the right group |
|
87 |
continue; |
|
88 |
||
89 |
$keep_going = true; |
|
90 |
if ( !isset($this->registered[$handle]) ) |
|
91 |
$keep_going = false; // Script doesn't exist |
|
92 |
elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) ) |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
93 |
$keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?) |
136 | 94 |
elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) ) |
95 |
$keep_going = false; // Script requires deps which don't exist |
|
96 |
||
97 |
if ( !$keep_going ) { // Either script or its deps don't exist. |
|
98 |
if ( $recursion ) |
|
99 |
return false; // Abort this branch. |
|
100 |
else |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
101 |
continue; // We're at the top level. Move on to the next one. |
136 | 102 |
} |
103 |
||
104 |
if ( $queued ) // Already grobbed it and its deps |
|
105 |
continue; |
|
106 |
||
107 |
if ( isset($handle_parts[1]) ) |
|
108 |
$this->args[$handle] = $handle_parts[1]; |
|
109 |
||
110 |
$this->to_do[] = $handle; |
|
111 |
} |
|
112 |
||
113 |
return true; |
|
114 |
} |
|
115 |
||
116 |
/** |
|
117 |
* Adds item |
|
118 |
* |
|
119 |
* Adds the item only if no item of that name already exists |
|
120 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
121 |
* @param string $handle Script name |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
122 |
* @param string $src Script url |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
123 |
* @param array $deps (optional) Array of script names on which this script depends |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
124 |
* @param string $ver (optional) Script version (used for cache busting) |
136 | 125 |
* @return array Hierarchical array of dependencies |
126 |
*/ |
|
127 |
function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { |
|
128 |
if ( isset($this->registered[$handle]) ) |
|
129 |
return false; |
|
130 |
$this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args ); |
|
131 |
return true; |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* Adds extra data |
|
136 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
137 |
* Adds data only if script has already been added. |
136 | 138 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
139 |
* @param string $handle Script name |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
140 |
* @param string $key |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
141 |
* @param mixed $value |
136 | 142 |
* @return bool success |
143 |
*/ |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
144 |
function add_data( $handle, $key, $value ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
145 |
if ( !isset( $this->registered[$handle] ) ) |
136 | 146 |
return false; |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
147 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
148 |
return $this->registered[$handle]->add_data( $key, $value ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
149 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
150 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
151 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
152 |
* Get extra data |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
153 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
154 |
* Gets data associated with a certain handle. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
155 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
156 |
* @since WP 3.3 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
157 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
158 |
* @param string $handle Script name |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
159 |
* @param string $key |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
160 |
* @return mixed |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
161 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
162 |
function get_data( $handle, $key ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
163 |
if ( !isset( $this->registered[$handle] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
164 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
165 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
166 |
if ( !isset( $this->registered[$handle]->extra[$key] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
167 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
168 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
169 |
return $this->registered[$handle]->extra[$key]; |
136 | 170 |
} |
171 |
||
172 |
function remove( $handles ) { |
|
173 |
foreach ( (array) $handles as $handle ) |
|
174 |
unset($this->registered[$handle]); |
|
175 |
} |
|
176 |
||
177 |
function enqueue( $handles ) { |
|
178 |
foreach ( (array) $handles as $handle ) { |
|
179 |
$handle = explode('?', $handle); |
|
180 |
if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) { |
|
181 |
$this->queue[] = $handle[0]; |
|
182 |
if ( isset($handle[1]) ) |
|
183 |
$this->args[$handle[0]] = $handle[1]; |
|
184 |
} |
|
185 |
} |
|
186 |
} |
|
187 |
||
188 |
function dequeue( $handles ) { |
|
189 |
foreach ( (array) $handles as $handle ) { |
|
190 |
$handle = explode('?', $handle); |
|
191 |
$key = array_search($handle[0], $this->queue); |
|
192 |
if ( false !== $key ) { |
|
193 |
unset($this->queue[$key]); |
|
194 |
unset($this->args[$handle[0]]); |
|
195 |
} |
|
196 |
} |
|
197 |
} |
|
198 |
||
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
199 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
200 |
function query( $handle, $list = 'registered' ) { |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
201 |
switch ( $list ) { |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
202 |
case 'registered' : |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
203 |
case 'scripts': // back compat |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
204 |
if ( isset( $this->registered[ $handle ] ) ) |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
205 |
return $this->registered[ $handle ]; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
206 |
return false; |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
207 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
208 |
case 'enqueued' : |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
209 |
case 'queue' : |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
210 |
return in_array( $handle, $this->queue ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
211 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
212 |
case 'to_do' : |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
213 |
case 'to_print': // back compat |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
214 |
return in_array( $handle, $this->to_do ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
215 |
|
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
216 |
case 'done' : |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
217 |
case 'printed': // back compat |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
218 |
return in_array( $handle, $this->done ); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
219 |
} |
136 | 220 |
return false; |
221 |
} |
|
222 |
||
223 |
function set_group( $handle, $recursion, $group ) { |
|
224 |
$group = (int) $group; |
|
225 |
||
226 |
if ( $recursion ) |
|
227 |
$group = min($this->group, $group); |
|
228 |
else |
|
229 |
$this->group = $group; |
|
230 |
||
231 |
if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group ) |
|
232 |
return false; |
|
233 |
||
234 |
$this->groups[$handle] = $group; |
|
235 |
return true; |
|
236 |
} |
|
237 |
||
238 |
} |
|
239 |
||
240 |
class _WP_Dependency { |
|
241 |
var $handle; |
|
242 |
var $src; |
|
243 |
var $deps = array(); |
|
244 |
var $ver = false; |
|
245 |
var $args = null; |
|
246 |
||
247 |
var $extra = array(); |
|
248 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
249 |
function __construct() { |
204
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
250 |
@list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = func_get_args(); |
09a1c134465b
man wordpress + plugins order post + slideshow
Anthony Ly <anthonyly.com@gmail.com>
parents:
194
diff
changeset
|
251 |
if ( ! is_array($this->deps) ) |
136 | 252 |
$this->deps = array(); |
253 |
} |
|
254 |
||
255 |
function add_data( $name, $data ) { |
|
256 |
if ( !is_scalar($name) ) |
|
257 |
return false; |
|
258 |
$this->extra[$name] = $data; |
|
259 |
return true; |
|
260 |
} |
|
261 |
} |