Ticket #23914: 23914.diff
File 23914.diff, 11.9 KB (added by , 12 years ago) |
---|
-
src/wp-includes/class.wp-dependencies.php
1 1 <?php 2 2 /** 3 * BackPress Scripts enqueue .3 * BackPress Scripts enqueue 4 4 * 5 * These classes were refactored from the WordPress WP_Scripts and WordPress 6 * script enqueue API. 5 * Classes were refactored from the WP_Scripts and WordPress script enqueue API. 7 6 * 8 * @package BackPress9 7 * @since r74 10 */11 12 /**13 * BackPress enqueued dependiences class.14 8 * 15 9 * @package BackPress 16 * @uses _WP_Dependency17 * @since r7418 10 */ 19 11 class WP_Dependencies { 12 /** 13 * An array of registered handle objects. 14 * 15 * @access public 16 * @since 2.6.8 17 * @var array 18 */ 20 19 var $registered = array(); 20 21 /** 22 * An array of queued _WP_Dependency handle objects. 23 * 24 * @access public 25 * @since 2.6.8 26 * @var array 27 */ 21 28 var $queue = array(); 29 30 /** 31 * An array of _WP_Dependency handle objects to queue. 32 * 33 * @access public 34 * @since 2.6.0 35 * @var array 36 */ 22 37 var $to_do = array(); 38 39 /** 40 * An array of _WP_Dependency handle objects already queued. 41 * 42 * @access public 43 * @since 2.6.0 44 * @var array 45 */ 23 46 var $done = array(); 47 48 /** 49 * An array of additional arguments passed when a handle is registered. 50 * 51 * Arguments are appended to the item query string. 52 * 53 * @access public 54 * @since 2.6.0 55 * @var array 56 */ 24 57 var $args = array(); 58 59 /** 60 * An array of handle groups to enqueue. 61 * 62 * @access public 63 * @since 2.8.0 64 * @var array 65 */ 25 66 var $groups = array(); 67 68 /** 69 * A handle group to enqueue. 70 * 71 * @access public 72 * @since 2.8.0 73 * @var int 74 */ 26 75 var $group = 0; 27 76 28 77 /** 29 * Do the dependencies 78 * Do the dependencies. 30 79 * 31 * Process the items passed to it or the queue. Processes all dependencies.80 * Processes the items passed to it or the queue. Processes all dependencies. 32 81 * 33 * @param mixed $handles (optional) items to be processed. (void) processes queue, (string) process that item, (array of strings) process those items 34 * @return array Items that have been processed 82 * @since Unknown 83 * 84 * @param mixed $handles (optional) Items to be processed: (false) Process queue, (string) process item, (array of strings) process items. 85 * @param mixed $group Group level: (int) level, (false) no groups. 86 * @return array Handles of items that have been processed. 35 87 */ 36 88 function do_items( $handles = false, $group = false ) { 37 // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.89 // Print the queue if nothing is passed. If a string is passed, print that item. If an array is passed, print those items. 38 90 $handles = false === $handles ? $this->queue : (array) $handles; 39 91 $this->all_deps( $handles ); 40 92 41 93 foreach( $this->to_do as $key => $handle ) { 42 94 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) { 43 95 44 if ( ! $this->registered[$handle]->src ) { // Defines a group. 96 /** 97 * A single item may alias a set of items, by having dependencies but no src. 98 * Queuing the item queues the dependencies. 99 * Example: the extending class WP_Scripts is used to register 'scriptaculous' as a set of registered handles: 100 * <code>add( 'scriptaculous', false, array( 'scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls' ) );</code> 101 * The src property is false. 102 **/ 103 if ( ! $this->registered[$handle]->src ) { 45 104 $this->done[] = $handle; 46 105 continue; 47 106 } … … 56 115 return $this->done; 57 116 } 58 117 118 /** 119 * Do a single dependency. 120 * 121 * Processes one item for dependencies. 122 * 123 * @since Unknown 124 * 125 * @param string $handle Unique item name. 126 * @return bool True on success, false on failure. 127 */ 59 128 function do_item( $handle ) { 60 129 return isset($this->registered[$handle]); 61 130 } 62 131 63 132 /** 64 * Determine s dependencies133 * Determine dependencies. 65 134 * 66 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops. 135 * Recursively builds array of items to process taking dependencies into account. 136 * Does NOT catch infinite loops. 67 137 * 138 * @since Unknown 68 139 * 69 * @param mixed $handles Accepts (string) dep name or (array of strings) dep names 70 * @param bool $recursion Used internally when function calls itself 140 * @param mixed $handles (string) Dependency handle and argument or (array of strings) Dependency handles and arguments. 141 * @param bool $recursion Internal flag that function is calling itself. 142 * @param mixed $group Group level: (int) level, (false) no groups. 143 * @return bool Success 71 144 */ 72 145 function all_deps( $handles, $recursion = false, $group = false ) { 73 146 if ( !$handles = (array) $handles ) … … 88 161 89 162 $keep_going = true; 90 163 if ( !isset($this->registered[$handle]) ) 91 $keep_going = false; // Scriptdoesn't exist164 $keep_going = false; // Item doesn't exist 92 165 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) ) 93 $keep_going = false; // Scriptrequires deps which don't exist (not a necessary check. efficiency?)166 $keep_going = false; // Item requires deps which don't exist (not a necessary check. efficiency?) 94 167 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) ) 95 $keep_going = false; // Scriptrequires deps which don't exist168 $keep_going = false; // Item requires deps which don't exist 96 169 97 if ( ! $keep_going ) { // Either scriptor its deps don't exist.170 if ( ! $keep_going ) { // Either item or its deps don't exist. 98 171 if ( $recursion ) 99 172 return false; // Abort this branch. 100 173 else 101 174 continue; // We're at the top level. Move on to the next one. 102 175 } 103 176 104 if ( $queued ) // Already gr obbed it and its deps177 if ( $queued ) // Already grabbed it and its deps 105 178 continue; 106 179 107 180 if ( isset($handle_parts[1]) ) … … 114 187 } 115 188 116 189 /** 117 * Adds item190 * Register an item. 118 191 * 119 * Adds the item only if no item of that name already exists192 * Registers the item if no item of that name already exists. 120 193 * 121 * @param string $handle Script name 122 * @param string $src Script url 123 * @param array $deps (optional) Array of script names on which this script depends 124 * @param string $ver (optional) Script version (used for cache busting) 125 * @return array Hierarchical array of dependencies 194 * @since Unknown 195 * 196 * @param string $handle Unique item name. 197 * @param string $src The item url. 198 * @param array $deps (optional) An array of item handle strings on which this item depends. 199 * @param string $ver (optional) Version (used for cache busting). 200 * @param mixed $args (optional) Custom property of the item. NOT the class property $args. Examples: $media, $in_footer. 201 * @return bool True on success, false on failure. 126 202 */ 127 203 function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { 128 204 if ( isset($this->registered[$handle]) ) … … 132 208 } 133 209 134 210 /** 135 * Add s extra data211 * Add extra item data. 136 212 * 137 * Adds data only if script has already been added.213 * Adds data to a registered item. 138 214 * 139 * @param string $handle Script name140 * @param string $key 141 * @param mixed $value142 * @return bool success215 * @param string $handle Unique item name. 216 * @param string $key Data key. 217 * @param mixed $value Data value. 218 * @return bool True on success, false on failure. 143 219 */ 144 220 function add_data( $handle, $key, $value ) { 145 221 if ( !isset( $this->registered[$handle] ) ) … … 149 225 } 150 226 151 227 /** 152 * Get extra data228 * Get extra item data. 153 229 * 154 * Gets data associated with a certain handle.230 * Gets data associated with a registered item. 155 231 * 156 * @since WP 3.3232 * @since 3.3.0 157 233 * 158 * @param string $handle Script name159 * @param string $key 160 * @return mixed 234 * @param string $handle Unique item name. 235 * @param string $key Data key. 236 * @return mixed (string) Extra item data, false otherwise. 161 237 */ 162 238 function get_data( $handle, $key ) { 163 239 if ( !isset( $this->registered[$handle] ) ) … … 169 245 return $this->registered[$handle]->extra[$key]; 170 246 } 171 247 248 /** 249 * Unregister and item or items. 250 * 251 * @since Unknown 252 * 253 * @param mixed $handles (string) Item handle or (array of strings) Item handles. 254 * @return void 255 */ 172 256 function remove( $handles ) { 173 257 foreach ( (array) $handles as $handle ) 174 258 unset($this->registered[$handle]); 175 259 } 176 260 261 /** 262 * Queue an item or items. 263 * 264 * Decodes handles and arguments, then queues handles and stores 265 * arguments in the class property $args. For example in extending 266 * classes, $args is appended to the item url as a query string. 267 * Note $args is NOT the $args property of items in the $registered array. 268 * 269 * @since Unknown 270 * 271 * @param mixed $handles (string) Item handle and argument or (array of strings) Item handles and arguments 272 * @return void 273 */ 177 274 function enqueue( $handles ) { 178 275 foreach ( (array) $handles as $handle ) { 179 276 $handle = explode('?', $handle); … … 185 282 } 186 283 } 187 284 285 /** 286 * Dequeue an item or items. 287 * 288 * Decodes handles and arguments, then dequeues handles and removes arguments from the class property $args. 289 * 290 * @since Unknown 291 * 292 * @param mixed $handles (string) Item handle and argument or (array of strings) Item handles and arguments 293 * @return void 294 */ 188 295 function dequeue( $handles ) { 189 296 foreach ( (array) $handles as $handle ) { 190 297 $handle = explode('?', $handle); … … 196 303 } 197 304 } 198 305 199 306 /** 307 * Query list for an item. 308 * 309 * @since Unknown 310 * 311 * @param string $handle The item handle. 312 * @param string $list Property name of list array. 313 * @return bool Found, or object Item data. 314 */ 200 315 function query( $handle, $list = 'registered' ) { 201 316 switch ( $list ) { 202 317 case 'registered' : … … 220 335 return false; 221 336 } 222 337 338 /** 339 * Set item group, unless already in a lower group. 340 * 341 * @since Unknown 342 * 343 * @param string $handle The item handle. 344 * @param bool $recursion Internal flag that calling function was called recursively. 345 * @param mixed $group Group level. 346 * @return bool Not already in the group or a lower group 347 */ 223 348 function set_group( $handle, $recursion, $group ) { 224 349 $group = (int) $group; 225 350 … … 237 362 238 363 } 239 364 365 /** 366 * Class _WP_Dependency 367 * 368 * Registers a handle and associated data. 369 * 370 * @access private 371 * @since 2.6.0 372 */ 240 373 class _WP_Dependency { 374 /** 375 * The handle name. 376 * 377 * @access public 378 * @since 2.6.0 379 * @var null 380 */ 241 381 var $handle; 382 383 /** 384 * The handle source. 385 * 386 * @access public 387 * @since 2.6.0 388 * @var null 389 */ 242 390 var $src; 391 392 /** 393 * An array of handle dependencies. 394 * 395 * @access public 396 * @since 2.6.0 397 * @var array 398 */ 243 399 var $deps = array(); 400 401 /** 402 * The handle version. 403 * 404 * Used for cache-busting. 405 * 406 * @access public 407 * @since 2.6.0 408 * @var bool|string 409 */ 244 410 var $ver = false; 245 var $args = null;246 411 412 /** 413 * Additional arguments for the handle. 414 * 415 * @access public 416 * @since 2.6.0 417 * @var null 418 */ 419 var $args = null; // Custom property, such as $in_footer or $media. 420 421 /** 422 * Extra data to supply to the handle. 423 * 424 * @access public 425 * @since 2.6.0 426 * @var array 427 */ 247 428 var $extra = array(); 248 429 430 /** 431 * Setup dependencies. 432 * 433 * @since 2.6.0 434 */ 249 435 function __construct() { 250 436 @list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = func_get_args(); 251 437 if ( ! is_array($this->deps) ) 252 438 $this->deps = array(); 253 439 } 254 440 441 /** 442 * Add handle data. 443 * 444 * @param string $name The data key to add. 445 * @param mixed $data The data value to add. 446 * @return bool False if not scalar, true otherwise. 447 */ 255 448 function add_data( $name, $data ) { 256 449 if ( !is_scalar($name) ) 257 450 return false;