Ticket #23914: 23914.2.diff
File 23914.2.diff, 11.8 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 BackPress 9 * @since r74 10 */ 11 12 /** 13 * BackPress enqueued dependiences class. 7 * @since BackPress r74 14 8 * 15 * @package BackPress 16 * @uses _WP_Dependency 17 * @since r74 9 * @package WordPress 10 * @subpackage BackPress 18 11 */ 19 12 class WP_Dependencies { 20 13 var $registered = array(); … … 26 19 var $group = 0; 27 20 28 21 /** 29 * Do the dependencies22 * Process the dependencies. 30 23 * 31 * Process the items passed to it or the queue. Processes all dependencies.24 * Processes the items passed to it or the queue. Processes all dependencies. 32 25 * 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 26 * @access public 27 * @since Unknown 28 * 29 * @param mixed $handles Optional. Items to be processed: (false) Process queue, (string) process item, (array of strings) process items. 30 * @param mixed $group Group level: (int) level, (false) no groups. 31 * @return array Handles of items that have been processed. 35 32 */ 36 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. 33 public function do_items( $handles = false, $group = false ) { 34 /** 35 * Print the queue if nothing is passed. If a string is passed, 36 * print that item. If an array is passed, print those items. 37 */ 38 38 $handles = false === $handles ? $this->queue : (array) $handles; 39 39 $this->all_deps( $handles ); 40 40 41 41 foreach( $this->to_do as $key => $handle ) { 42 42 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) { 43 43 44 if ( ! $this->registered[$handle]->src ) { // Defines a group. 44 /** 45 * A single item may alias a set of items, by having dependencies, 46 * but no source. Queuing the item queues the dependencies. 47 * 48 * Example: The extending class WP_Scripts is used to register 'scriptaculous' as a set of registered handles: 49 * <code>add( 'scriptaculous', false, array( 'scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls' ) );</code> 50 * 51 * The src property is false. 52 **/ 53 if ( ! $this->registered[$handle]->src ) { 45 54 $this->done[] = $handle; 46 55 continue; 47 56 } 48 57 58 /** 59 * Attempt to process the item. If successful, 60 * add the handle to the 'done' array. 61 */ 49 62 if ( $this->do_item( $handle, $group ) ) 50 63 $this->done[] = $handle; 51 64 … … 56 69 return $this->done; 57 70 } 58 71 59 function do_item( $handle ) { 72 /** 73 * Process a dependency. 74 * 75 * @access public 76 * @since Unknown 77 * 78 * @param string $handle Unique item name. 79 * @return bool True on success, false on failure. 80 */ 81 public function do_item( $handle ) { 60 82 return isset($this->registered[$handle]); 61 83 } 62 84 63 85 /** 64 * Determine s dependencies86 * Determine dependencies. 65 87 * 66 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops. 88 * Recursively builds an array of items to process taking 89 * dependencies into account. Does NOT catch infinite loops. 67 90 * 91 * @access public 92 * @since Unknown 68 93 * 69 * @param mixed $handles Accepts (string) dep name or (array of strings) dep names 70 * @param bool $recursion Used internally when function calls itself 94 * @param mixed $handles (string) Dependency handle and argument or (array of strings) Dependency handles and arguments. 95 * @param bool $recursion Internal flag that function is calling itself. 96 * @param mixed $group Group level: (int) level, (false) no groups. 97 * @return bool Success 71 98 */ 72 function all_deps( $handles, $recursion = false, $group = false ) {99 public function all_deps( $handles, $recursion = false, $group = false ) { 73 100 if ( !$handles = (array) $handles ) 74 101 return false; 75 102 … … 88 115 89 116 $keep_going = true; 90 117 if ( !isset($this->registered[$handle]) ) 91 $keep_going = false; // Script doesn't exist118 $keep_going = false; // Item doesn't exist. 92 119 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) ) 93 $keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?)120 $keep_going = false; // Item requires dependencies that don't exist. 94 121 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) ) 95 $keep_going = false; // Script requires deps which don't exist122 $keep_going = false; // Item requires dependencies that don't exist. 96 123 97 if ( ! $keep_going ) { // Either script or its deps don't exist.124 if ( ! $keep_going ) { // Either item or its dependencies don't exist. 98 125 if ( $recursion ) 99 126 return false; // Abort this branch. 100 127 else 101 128 continue; // We're at the top level. Move on to the next one. 102 129 } 103 130 104 if ( $queued ) // Already gr obbed it and its deps131 if ( $queued ) // Already grabbed it and its dependencies. 105 132 continue; 106 133 107 134 if ( isset($handle_parts[1]) ) … … 114 141 } 115 142 116 143 /** 117 * Adds item144 * Register an item. 118 145 * 119 * Adds the item only if no item of that name already exists146 * Registers the item if no item of that name already exists. 120 147 * 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 148 * @access public 149 * @since Unknown 150 * 151 * @param string $handle Unique item name. 152 * @param string $src The item url. 153 * @param array $deps Optional. An array of item handle strings on which this item depends. 154 * @param string $ver Optional. Version (used for cache busting). 155 * @param mixed $args Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer. 156 * @return bool True on success, false on failure. 126 157 */ 127 function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {158 public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) { 128 159 if ( isset($this->registered[$handle]) ) 129 160 return false; 130 161 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args ); … … 132 163 } 133 164 134 165 /** 135 * Add s extra data166 * Add extra item data. 136 167 * 137 * Adds data only if script has already been added.168 * Adds data to a registered item. 138 169 * 139 * @param string $handle Script name 140 * @param string $key 141 * @param mixed $value 142 * @return bool success 170 * @access public 171 * @since Unknown 172 * 173 * @param string $handle Unique item name. 174 * @param string $key Data key. 175 * @param mixed $value Data value. 176 * @return bool True on success, false on failure. 143 177 */ 144 function add_data( $handle, $key, $value ) {178 public function add_data( $handle, $key, $value ) { 145 179 if ( !isset( $this->registered[$handle] ) ) 146 180 return false; 147 181 … … 149 183 } 150 184 151 185 /** 152 * Get extra data186 * Get extra item data. 153 187 * 154 * Gets data associated with a certain handle.188 * Gets data associated with a registered item. 155 189 * 156 * @since WP 3.3 190 * @access public 191 * @since 3.3.0 157 192 * 158 * @param string $handle Script name159 * @param string $key 160 * @return mixed 193 * @param string $handle Unique item name. 194 * @param string $key Data key. 195 * @return mixed (string) Extra item data, false otherwise. 161 196 */ 162 function get_data( $handle, $key ) {197 public function get_data( $handle, $key ) { 163 198 if ( !isset( $this->registered[$handle] ) ) 164 199 return false; 165 200 … … 169 204 return $this->registered[$handle]->extra[$key]; 170 205 } 171 206 172 function remove( $handles ) { 207 /** 208 * Unregister and item or items. 209 * 210 * @access public 211 * @since Unknown 212 * 213 * @param mixed $handles (string) Item handle or (array of strings) Item handles. 214 * @return void 215 */ 216 public function remove( $handles ) { 173 217 foreach ( (array) $handles as $handle ) 174 218 unset($this->registered[$handle]); 175 219 } 176 220 177 function enqueue( $handles ) { 221 /** 222 * Queue an item or items. 223 * 224 * Decodes handles and arguments, then queues handles and stores 225 * arguments in the class property $args. For example in extending 226 * classes, $args is appended to the item url as a query string. 227 * Note $args is NOT the $args property of items in the $registered array. 228 * 229 * @access public 230 * @since Unknown 231 * 232 * @param mixed $handles (string) Item handle and argument or (array of strings) Item handles and arguments 233 * @return void 234 */ 235 public function enqueue( $handles ) { 178 236 foreach ( (array) $handles as $handle ) { 179 237 $handle = explode('?', $handle); 180 238 if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) { … … 185 243 } 186 244 } 187 245 188 function dequeue( $handles ) { 246 /** 247 * Dequeue an item or items. 248 * 249 * Decodes handles and arguments, then dequeues handles 250 * and removes arguments from the class property $args. 251 * 252 * @access public 253 * @since Unknown 254 * 255 * @param mixed $handles (string) Item handle and argument or (array of strings) Item handles and arguments 256 * @return void 257 */ 258 public function dequeue( $handles ) { 189 259 foreach ( (array) $handles as $handle ) { 190 260 $handle = explode('?', $handle); 191 261 $key = array_search($handle[0], $this->queue); … … 196 266 } 197 267 } 198 268 199 200 function query( $handle, $list = 'registered' ) { 269 /** 270 * Query list for an item. 271 * 272 * @access public 273 * @since Unknown 274 * 275 * @param string $handle The item handle. 276 * @param string $list Property name of list array. 277 * @return bool Found, or object Item data. 278 */ 279 public function query( $handle, $list = 'registered' ) { 201 280 switch ( $list ) { 202 281 case 'registered' : 203 282 case 'scripts': // back compat … … 220 299 return false; 221 300 } 222 301 223 function set_group( $handle, $recursion, $group ) { 302 /** 303 * Set item group, unless already in a lower group. 304 * 305 * @access public 306 * @since Unknown 307 * 308 * @param string $handle The item handle. 309 * @param bool $recursion Internal flag that calling function was called recursively. 310 * @param mixed $group Group level. 311 * @return bool Not already in the group or a lower group 312 */ 313 public function set_group( $handle, $recursion, $group ) { 224 314 $group = (int) $group; 225 315 226 316 if ( $recursion ) … … 235 325 return true; 236 326 } 237 327 238 } 328 } // WP_Dependencies 239 329 330 /** 331 * Class _WP_Dependency 332 * 333 * Helper class to register a handle and associated data. 334 * 335 * @access private 336 * @since 2.6.0 337 */ 240 338 class _WP_Dependency { 241 339 var $handle; 242 340 var $src; … … 246 344 247 345 var $extra = array(); 248 346 347 /** 348 * Setup dependencies. 349 * 350 * @since 2.6.0 351 */ 249 352 function __construct() { 250 353 @list( $this->handle, $this->src, $this->deps, $this->ver, $this->args ) = func_get_args(); 251 354 if ( ! is_array($this->deps) ) 252 355 $this->deps = array(); 253 356 } 254 357 358 /** 359 * Add handle data. 360 * 361 * @access public 362 * @since 2.6.0 363 * 364 * @param string $name The data key to add. 365 * @param mixed $data The data value to add. 366 * @return bool False if not scalar, true otherwise. 367 */ 255 368 function add_data( $name, $data ) { 256 369 if ( !is_scalar($name) ) 257 370 return false; 258 371 $this->extra[$name] = $data; 259 372 return true; 260 373 } 261 } 374 375 } // _WP_Dependencies