raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * BackPress Scripts enqueue.
mp-wp_genesis 4 *
mp-wp_genesis 5 * These classes were refactored from the WordPress WP_Scripts and WordPress
mp-wp_genesis 6 * script enqueue API.
mp-wp_genesis 7 *
mp-wp_genesis 8 * @package BackPress
mp-wp_genesis 9 * @since r74
mp-wp_genesis 10 */
mp-wp_genesis 11
mp-wp_genesis 12 /**
mp-wp_genesis 13 * BackPress enqueued dependiences class.
mp-wp_genesis 14 *
mp-wp_genesis 15 * @package BackPress
mp-wp_genesis 16 * @uses _WP_Dependency
mp-wp_genesis 17 * @since r74
mp-wp_genesis 18 */
mp-wp_genesis 19 class WP_Dependencies {
mp-wp_genesis 20 var $registered = array();
mp-wp_genesis 21 var $queue = array();
mp-wp_genesis 22 var $to_do = array();
mp-wp_genesis 23 var $done = array();
mp-wp_genesis 24 var $args = array();
mp-wp_genesis 25
mp-wp_genesis 26 function WP_Dependencies() {
mp-wp_genesis 27 $args = func_get_args();
mp-wp_genesis 28 call_user_func_array( array(&$this, '__construct'), $args );
mp-wp_genesis 29 }
mp-wp_genesis 30
mp-wp_genesis 31 function __construct() {}
mp-wp_genesis 32
mp-wp_genesis 33 /**
mp-wp_genesis 34 * Do the dependencies
mp-wp_genesis 35 *
mp-wp_genesis 36 * Process the items passed to it or the queue. Processes all dependencies.
mp-wp_genesis 37 *
mp-wp_genesis 38 * @param mixed handles (optional) items to be processed. (void) processes queue, (string) process that item, (array of strings) process those items
mp-wp_genesis 39 * @return array Items that have been processed
mp-wp_genesis 40 */
mp-wp_genesis 41 function do_items( $handles = false ) {
mp-wp_genesis 42 // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
mp-wp_genesis 43 $handles = false === $handles ? $this->queue : (array) $handles;
mp-wp_genesis 44 $this->all_deps( $handles );
mp-wp_genesis 45
mp-wp_genesis 46 foreach( $this->to_do as $handle ) {
mp-wp_genesis 47 if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) {
mp-wp_genesis 48 if ( $this->registered[$handle]->src ) { // Else it defines a group.
mp-wp_genesis 49 $this->do_item( $handle );
mp-wp_genesis 50 }
mp-wp_genesis 51 $this->done[] = $handle;
mp-wp_genesis 52 }
mp-wp_genesis 53 }
mp-wp_genesis 54
mp-wp_genesis 55 $this->to_do = array();
mp-wp_genesis 56 return $this->done;
mp-wp_genesis 57 }
mp-wp_genesis 58
mp-wp_genesis 59 function do_item( $handle ) {
mp-wp_genesis 60 return isset($this->registered[$handle]);
mp-wp_genesis 61 }
mp-wp_genesis 62
mp-wp_genesis 63 /**
mp-wp_genesis 64 * Determines dependencies
mp-wp_genesis 65 *
mp-wp_genesis 66 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops.
mp-wp_genesis 67 *
mp-wp_genesis 68
mp-wp_genesis 69 * @param mixed handles Accepts (string) dep name or (array of strings) dep names
mp-wp_genesis 70 * @param bool recursion Used internally when function calls itself
mp-wp_genesis 71 */
mp-wp_genesis 72 function all_deps( $handles, $recursion = false ) {
mp-wp_genesis 73 if ( !$handles = (array) $handles )
mp-wp_genesis 74 return false;
mp-wp_genesis 75
mp-wp_genesis 76 foreach ( $handles as $handle ) {
mp-wp_genesis 77 $handle = explode('?', $handle);
mp-wp_genesis 78 if ( isset($handle[1]) )
mp-wp_genesis 79 $this->args[$handle[0]] = $handle[1];
mp-wp_genesis 80 $handle = $handle[0];
mp-wp_genesis 81
mp-wp_genesis 82 if ( isset($this->to_do[$handle]) ) // Already grobbed it and its deps
mp-wp_genesis 83 continue;
mp-wp_genesis 84
mp-wp_genesis 85 $keep_going = true;
mp-wp_genesis 86 if ( !isset($this->registered[$handle]) )
mp-wp_genesis 87 $keep_going = false; // Script doesn't exist
mp-wp_genesis 88 elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
mp-wp_genesis 89 $keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?)
mp-wp_genesis 90 elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true ) )
mp-wp_genesis 91 $keep_going = false; // Script requires deps which don't exist
mp-wp_genesis 92
mp-wp_genesis 93 if ( !$keep_going ) { // Either script or its deps don't exist.
mp-wp_genesis 94 if ( $recursion )
mp-wp_genesis 95 return false; // Abort this branch.
mp-wp_genesis 96 else
mp-wp_genesis 97 continue; // We're at the top level. Move on to the next one.
mp-wp_genesis 98 }
mp-wp_genesis 99
mp-wp_genesis 100 $this->to_do[$handle] = true;
mp-wp_genesis 101 }
mp-wp_genesis 102
mp-wp_genesis 103 if ( !$recursion ) // at the end
mp-wp_genesis 104 $this->to_do = array_keys( $this->to_do );
mp-wp_genesis 105 return true;
mp-wp_genesis 106 }
mp-wp_genesis 107
mp-wp_genesis 108 /**
mp-wp_genesis 109 * Adds item
mp-wp_genesis 110 *
mp-wp_genesis 111 * Adds the item only if no item of that name already exists
mp-wp_genesis 112 *
mp-wp_genesis 113 * @param string handle Script name
mp-wp_genesis 114 * @param string src Script url
mp-wp_genesis 115 * @param array deps (optional) Array of script names on which this script depends
mp-wp_genesis 116 * @param string ver (optional) Script version (used for cache busting)
mp-wp_genesis 117 * @return array Hierarchical array of dependencies
mp-wp_genesis 118 */
mp-wp_genesis 119 function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
mp-wp_genesis 120 if ( isset($this->registered[$handle]) )
mp-wp_genesis 121 return false;
mp-wp_genesis 122 $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
mp-wp_genesis 123 return true;
mp-wp_genesis 124 }
mp-wp_genesis 125
mp-wp_genesis 126 /**
mp-wp_genesis 127 * Adds extra data
mp-wp_genesis 128 *
mp-wp_genesis 129 * Adds data only if script has already been added
mp-wp_genesis 130 *
mp-wp_genesis 131 * @param string handle Script name
mp-wp_genesis 132 * @param string data_name Name of object in which to store extra data
mp-wp_genesis 133 * @param array data Array of extra data
mp-wp_genesis 134 * @return bool success
mp-wp_genesis 135 */
mp-wp_genesis 136 function add_data( $handle, $data_name, $data ) {
mp-wp_genesis 137 if ( !isset($this->registered[$handle]) )
mp-wp_genesis 138 return false;
mp-wp_genesis 139 return $this->registered[$handle]->add_data( $data_name, $data );
mp-wp_genesis 140 }
mp-wp_genesis 141
mp-wp_genesis 142 function remove( $handles ) {
mp-wp_genesis 143 foreach ( (array) $handles as $handle )
mp-wp_genesis 144 unset($this->registered[$handle]);
mp-wp_genesis 145 }
mp-wp_genesis 146
mp-wp_genesis 147 function enqueue( $handles ) {
mp-wp_genesis 148 foreach ( (array) $handles as $handle ) {
mp-wp_genesis 149 $handle = explode('?', $handle);
mp-wp_genesis 150 if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
mp-wp_genesis 151 $this->queue[] = $handle[0];
mp-wp_genesis 152 if ( isset($handle[1]) )
mp-wp_genesis 153 $this->args[$handle[0]] = $handle[1];
mp-wp_genesis 154 }
mp-wp_genesis 155 }
mp-wp_genesis 156 }
mp-wp_genesis 157
mp-wp_genesis 158 function dequeue( $handles ) {
mp-wp_genesis 159 foreach ( (array) $handles as $handle )
mp-wp_genesis 160 unset( $this->queue[$handle] );
mp-wp_genesis 161 }
mp-wp_genesis 162
mp-wp_genesis 163 function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do
mp-wp_genesis 164 switch ( $list ) :
mp-wp_genesis 165 case 'registered':
mp-wp_genesis 166 case 'scripts': // back compat
mp-wp_genesis 167 if ( isset($this->registered[$handle]) )
mp-wp_genesis 168 return $this->registered[$handle];
mp-wp_genesis 169 break;
mp-wp_genesis 170 case 'to_print': // back compat
mp-wp_genesis 171 case 'printed': // back compat
mp-wp_genesis 172 if ( 'to_print' == $list )
mp-wp_genesis 173 $list = 'to_do';
mp-wp_genesis 174 else
mp-wp_genesis 175 $list = 'printed';
mp-wp_genesis 176 default:
mp-wp_genesis 177 if ( in_array($handle, $this->$list) )
mp-wp_genesis 178 return true;
mp-wp_genesis 179 break;
mp-wp_genesis 180 endswitch;
mp-wp_genesis 181 return false;
mp-wp_genesis 182 }
mp-wp_genesis 183
mp-wp_genesis 184 }
mp-wp_genesis 185
mp-wp_genesis 186 class _WP_Dependency {
mp-wp_genesis 187 var $handle;
mp-wp_genesis 188 var $src;
mp-wp_genesis 189 var $deps = array();
mp-wp_genesis 190 var $ver = false;
mp-wp_genesis 191 var $args = null;
mp-wp_genesis 192
mp-wp_genesis 193 var $extra = array();
mp-wp_genesis 194
mp-wp_genesis 195 function _WP_Dependency() {
mp-wp_genesis 196 @list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
mp-wp_genesis 197 if ( !is_array($this->deps) )
mp-wp_genesis 198 $this->deps = array();
mp-wp_genesis 199 if ( !$this->ver )
mp-wp_genesis 200 $this->ver = false;
mp-wp_genesis 201 }
mp-wp_genesis 202
mp-wp_genesis 203 function add_data( $name, $data ) {
mp-wp_genesis 204 if ( !is_scalar($name) )
mp-wp_genesis 205 return false;
mp-wp_genesis 206 $this->extra[$name] = $data;
mp-wp_genesis 207 return true;
mp-wp_genesis 208 }
mp-wp_genesis 209 }