| [ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
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 * 31 * Process the items passed to it or the queue. Processes all dependencies. 32 * 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 35 */ 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. 38 $handles = false === $handles ? $this->queue : (array) $handles; 39 $this->all_deps( $handles ); 40 41 foreach( $this->to_do as $key => $handle ) { 42 if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) { 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 * 66 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops. 67 * 68 * 69 * @param mixed $handles Accepts (string) dep name or (array of strings) dep names 70 * @param bool $recursion Used internally when function calls itself 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)) ) 93 $keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?) 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 101 continue; // We're at the top level. Move on to the next one. 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 * 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 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 * 137 * Adds data only if script has already been added. 138 * 139 * @param string $handle Script name 140 * @param string $key 141 * @param mixed $value 142 * @return bool success 143 */ 144 function add_data( $handle, $key, $value ) { 145 if ( !isset( $this->registered[$handle] ) ) 146 return false; 147 148 return $this->registered[$handle]->add_data( $key, $value ); 149 } 150 151 /** 152 * Get extra data 153 * 154 * Gets data associated with a certain handle. 155 * 156 * @since WP 3.3 157 * 158 * @param string $handle Script name 159 * @param string $key 160 * @return mixed 161 */ 162 function get_data( $handle, $key ) { 163 if ( !isset( $this->registered[$handle] ) ) 164 return false; 165 166 if ( !isset( $this->registered[$handle]->extra[$key] ) ) 167 return false; 168 169 return $this->registered[$handle]->extra[$key]; 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 199 function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do 200 switch ( $list ) : 201 case 'registered': 202 case 'scripts': // back compat 203 if ( isset($this->registered[$handle]) ) 204 return $this->registered[$handle]; 205 break; 206 case 'to_print': // back compat 207 case 'printed': // back compat 208 if ( 'to_print' == $list ) 209 $list = 'to_do'; 210 else 211 $list = 'printed'; 212 default: 213 if ( in_array($handle, $this->$list) ) 214 return true; 215 break; 216 endswitch; 217 return false; 218 } 219 220 function set_group( $handle, $recursion, $group ) { 221 $group = (int) $group; 222 223 if ( $recursion ) 224 $group = min($this->group, $group); 225 else 226 $this->group = $group; 227 228 if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group ) 229 return false; 230 231 $this->groups[$handle] = $group; 232 return true; 233 } 234 235 } 236 237 class _WP_Dependency { 238 var $handle; 239 var $src; 240 var $deps = array(); 241 var $ver = false; 242 var $args = null; 243 244 var $extra = array(); 245 246 function __construct() { 247 @list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args(); 248 if ( !is_array($this->deps) ) 249 $this->deps = array(); 250 } 251 252 function add_data( $name, $data ) { 253 if ( !is_scalar($name) ) 254 return false; 255 $this->extra[$name] = $data; 256 return true; 257 } 258 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Fri May 25 03:56:23 2012 | Hosted by follow the white rabbit. |