web/wp-includes/class.wp-dependencies.php
branchwordpress
changeset 109 03b0d1493584
child 132 4d4862461b8d
equal deleted inserted replaced
-1:000000000000 109:03b0d1493584
       
     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 	function WP_Dependencies() {
       
    29 		$args = func_get_args();
       
    30 		call_user_func_array( array(&$this, '__construct'), $args );
       
    31 	}
       
    32 
       
    33 	function __construct() {}
       
    34 
       
    35 	/**
       
    36 	 * Do the dependencies
       
    37 	 *
       
    38 	 * Process the items passed to it or the queue.  Processes all dependencies.
       
    39 	 *
       
    40 	 * @param mixed handles (optional) items to be processed.  (void) processes queue, (string) process that item, (array of strings) process those items
       
    41 	 * @return array Items that have been processed
       
    42 	 */
       
    43 	function do_items( $handles = false, $group = false ) {
       
    44 		// Print the queue if nothing is passed.  If a string is passed, print that script.  If an array is passed, print those scripts.
       
    45 		$handles = false === $handles ? $this->queue : (array) $handles;
       
    46 		$this->all_deps( $handles );
       
    47 
       
    48 		foreach( $this->to_do as $key => $handle ) {
       
    49 			if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) {
       
    50 
       
    51 				if ( ! $this->registered[$handle]->src ) { // Defines a group.
       
    52 					$this->done[] = $handle;
       
    53 					continue;
       
    54 				}
       
    55 
       
    56 				if ( $this->do_item( $handle, $group ) )
       
    57 					$this->done[] = $handle;
       
    58 
       
    59 				unset( $this->to_do[$key] );
       
    60 			}
       
    61 		}
       
    62 
       
    63 		return $this->done;
       
    64 	}
       
    65 
       
    66 	function do_item( $handle ) {
       
    67 		return isset($this->registered[$handle]);
       
    68 	}
       
    69 
       
    70 	/**
       
    71 	 * Determines dependencies
       
    72 	 *
       
    73 	 * Recursively builds array of items to process taking dependencies into account.  Does NOT catch infinite loops.
       
    74 	 *
       
    75 
       
    76 	 * @param mixed handles Accepts (string) dep name or (array of strings) dep names
       
    77 	 * @param bool recursion Used internally when function calls itself
       
    78 	 */
       
    79 	function all_deps( $handles, $recursion = false, $group = false ) {
       
    80 		if ( !$handles = (array) $handles )
       
    81 			return false;
       
    82 
       
    83 		foreach ( $handles as $handle ) {
       
    84 			$handle_parts = explode('?', $handle);
       
    85 			$handle = $handle_parts[0];
       
    86 			$queued = in_array($handle, $this->to_do, true);
       
    87 
       
    88 			if ( in_array($handle, $this->done, true) ) // Already done
       
    89 				continue;
       
    90 
       
    91 			$moved = $this->set_group( $handle, $recursion, $group );
       
    92 
       
    93 			if ( $queued && !$moved ) // already queued and in the right group
       
    94 				continue;
       
    95 
       
    96 			$keep_going = true;
       
    97 			if ( !isset($this->registered[$handle]) )
       
    98 				$keep_going = false; // Script doesn't exist
       
    99 			elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
       
   100 				$keep_going = false; // Script requires deps which don't exist (not a necessary check.  efficiency?)
       
   101 			elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) )
       
   102 				$keep_going = false; // Script requires deps which don't exist
       
   103 
       
   104 			if ( !$keep_going ) { // Either script or its deps don't exist.
       
   105 				if ( $recursion )
       
   106 					return false; // Abort this branch.
       
   107 				else
       
   108 					continue; // We're at the top level.  Move on to the next one.
       
   109 			}
       
   110 
       
   111 			if ( $queued ) // Already grobbed it and its deps
       
   112 				continue;
       
   113 
       
   114 			if ( isset($handle_parts[1]) )
       
   115 				$this->args[$handle] = $handle_parts[1];
       
   116 
       
   117 			$this->to_do[] = $handle;
       
   118 		}
       
   119 
       
   120 		return true;
       
   121 	}
       
   122 
       
   123 	/**
       
   124 	 * Adds item
       
   125 	 *
       
   126 	 * Adds the item only if no item of that name already exists
       
   127 	 *
       
   128 	 * @param string handle Script name
       
   129 	 * @param string src Script url
       
   130 	 * @param array deps (optional) Array of script names on which this script depends
       
   131 	 * @param string ver (optional) Script version (used for cache busting)
       
   132 	 * @return array Hierarchical array of dependencies
       
   133 	 */
       
   134 	function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
       
   135 		if ( isset($this->registered[$handle]) )
       
   136 			return false;
       
   137 		$this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
       
   138 		return true;
       
   139 	}
       
   140 
       
   141 	/**
       
   142 	 * Adds extra data
       
   143 	 *
       
   144 	 * Adds data only if script has already been added
       
   145 	 *
       
   146 	 * @param string handle Script name
       
   147 	 * @param string data_name Name of object in which to store extra data
       
   148 	 * @param array data Array of extra data
       
   149 	 * @return bool success
       
   150 	 */
       
   151 	function add_data( $handle, $data_name, $data ) {
       
   152 		if ( !isset($this->registered[$handle]) )
       
   153 			return false;
       
   154 		return $this->registered[$handle]->add_data( $data_name, $data );
       
   155 	}
       
   156 
       
   157 	function remove( $handles ) {
       
   158 		foreach ( (array) $handles as $handle )
       
   159 			unset($this->registered[$handle]);
       
   160 	}
       
   161 
       
   162 	function enqueue( $handles ) {
       
   163 		foreach ( (array) $handles as $handle ) {
       
   164 			$handle = explode('?', $handle);
       
   165 			if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
       
   166 				$this->queue[] = $handle[0];
       
   167 				if ( isset($handle[1]) )
       
   168 					$this->args[$handle[0]] = $handle[1];
       
   169 			}
       
   170 		}
       
   171 	}
       
   172 
       
   173 	function dequeue( $handles ) {
       
   174 		foreach ( (array) $handles as $handle )
       
   175 			unset( $this->queue[$handle] );
       
   176 	}
       
   177 
       
   178 	function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do
       
   179 		switch ( $list ) :
       
   180 		case 'registered':
       
   181 		case 'scripts': // back compat
       
   182 			if ( isset($this->registered[$handle]) )
       
   183 				return $this->registered[$handle];
       
   184 			break;
       
   185 		case 'to_print': // back compat
       
   186 		case 'printed': // back compat
       
   187 			if ( 'to_print' == $list )
       
   188 				$list = 'to_do';
       
   189 			else
       
   190 				$list = 'printed';
       
   191 		default:
       
   192 			if ( in_array($handle, $this->$list) )
       
   193 				return true;
       
   194 			break;
       
   195 		endswitch;
       
   196 		return false;
       
   197 	}
       
   198 
       
   199 	function set_group( $handle, $recursion, $group ) {
       
   200 		$group = (int) $group;
       
   201 
       
   202 		if ( $recursion )
       
   203 			$group = min($this->group, $group);
       
   204 		else
       
   205 			$this->group = $group;
       
   206 
       
   207 		if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group )
       
   208 			return false;
       
   209 
       
   210 		$this->groups[$handle] = $group;
       
   211 		return true;
       
   212 	}
       
   213 
       
   214 }
       
   215 
       
   216 class _WP_Dependency {
       
   217 	var $handle;
       
   218 	var $src;
       
   219 	var $deps = array();
       
   220 	var $ver = false;
       
   221 	var $args = null;
       
   222 
       
   223 	var $extra = array();
       
   224 
       
   225 	function _WP_Dependency() {
       
   226 		@list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
       
   227 		if ( !is_array($this->deps) )
       
   228 			$this->deps = array();
       
   229 		if ( !$this->ver )
       
   230 			$this->ver = false;
       
   231 	}
       
   232 
       
   233 	function add_data( $name, $data ) {
       
   234 		if ( !is_scalar($name) )
       
   235 			return false;
       
   236 		$this->extra[$name] = $data;
       
   237 		return true;
       
   238 	}
       
   239 }