web/wp-includes/class.wp-dependencies.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    23 	var $done = array();
    23 	var $done = array();
    24 	var $args = array();
    24 	var $args = array();
    25 	var $groups = array();
    25 	var $groups = array();
    26 	var $group = 0;
    26 	var $group = 0;
    27 
    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 	/**
    28 	/**
    36 	 * Do the dependencies
    29 	 * Do the dependencies
    37 	 *
    30 	 *
    38 	 * Process the items passed to it or the queue.  Processes all dependencies.
    31 	 * Process the items passed to it or the queue. Processes all dependencies.
    39 	 *
    32 	 *
    40 	 * @param mixed handles (optional) items to be processed.  (void) processes queue, (string) process that item, (array of strings) process those items
    33 	 * @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
    34 	 * @return array Items that have been processed
    42 	 */
    35 	 */
    43 	function do_items( $handles = false, $group = false ) {
    36 	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.
    37 		// 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;
    38 		$handles = false === $handles ? $this->queue : (array) $handles;
    46 		$this->all_deps( $handles );
    39 		$this->all_deps( $handles );
    47 
    40 
    48 		foreach( $this->to_do as $key => $handle ) {
    41 		foreach( $this->to_do as $key => $handle ) {
    49 			if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) {
    42 			if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
    50 
    43 
    51 				if ( ! $this->registered[$handle]->src ) { // Defines a group.
    44 				if ( ! $this->registered[$handle]->src ) { // Defines a group.
    52 					$this->done[] = $handle;
    45 					$this->done[] = $handle;
    53 					continue;
    46 					continue;
    54 				}
    47 				}
    68 	}
    61 	}
    69 
    62 
    70 	/**
    63 	/**
    71 	 * Determines dependencies
    64 	 * Determines dependencies
    72 	 *
    65 	 *
    73 	 * Recursively builds array of items to process taking dependencies into account.  Does NOT catch infinite loops.
    66 	 * Recursively builds array of items to process taking dependencies into account. Does NOT catch infinite loops.
    74 	 *
    67 	 *
    75 
    68 	 *
    76 	 * @param mixed handles Accepts (string) dep name or (array of strings) dep names
    69 	 * @param mixed $handles Accepts (string) dep name or (array of strings) dep names
    77 	 * @param bool recursion Used internally when function calls itself
    70 	 * @param bool $recursion Used internally when function calls itself
    78 	 */
    71 	 */
    79 	function all_deps( $handles, $recursion = false, $group = false ) {
    72 	function all_deps( $handles, $recursion = false, $group = false ) {
    80 		if ( !$handles = (array) $handles )
    73 		if ( !$handles = (array) $handles )
    81 			return false;
    74 			return false;
    82 
    75 
    95 
    88 
    96 			$keep_going = true;
    89 			$keep_going = true;
    97 			if ( !isset($this->registered[$handle]) )
    90 			if ( !isset($this->registered[$handle]) )
    98 				$keep_going = false; // Script doesn't exist
    91 				$keep_going = false; // Script doesn't exist
    99 			elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
    92 			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?)
    93 				$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 ) )
    94 			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
    95 				$keep_going = false; // Script requires deps which don't exist
   103 
    96 
   104 			if ( !$keep_going ) { // Either script or its deps don't exist.
    97 			if ( !$keep_going ) { // Either script or its deps don't exist.
   105 				if ( $recursion )
    98 				if ( $recursion )
   106 					return false; // Abort this branch.
    99 					return false; // Abort this branch.
   107 				else
   100 				else
   108 					continue; // We're at the top level.  Move on to the next one.
   101 					continue; // We're at the top level. Move on to the next one.
   109 			}
   102 			}
   110 
   103 
   111 			if ( $queued ) // Already grobbed it and its deps
   104 			if ( $queued ) // Already grobbed it and its deps
   112 				continue;
   105 				continue;
   113 
   106 
   123 	/**
   116 	/**
   124 	 * Adds item
   117 	 * Adds item
   125 	 *
   118 	 *
   126 	 * Adds the item only if no item of that name already exists
   119 	 * Adds the item only if no item of that name already exists
   127 	 *
   120 	 *
   128 	 * @param string handle Script name
   121 	 * @param string $handle Script name
   129 	 * @param string src Script url
   122 	 * @param string $src Script url
   130 	 * @param array deps (optional) Array of script names on which this script depends
   123 	 * @param array $deps (optional) Array of script names on which this script depends
   131 	 * @param string ver (optional) Script version (used for cache busting)
   124 	 * @param string $ver (optional) Script version (used for cache busting)
   132 	 * @return array Hierarchical array of dependencies
   125 	 * @return array Hierarchical array of dependencies
   133 	 */
   126 	 */
   134 	function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
   127 	function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
   135 		if ( isset($this->registered[$handle]) )
   128 		if ( isset($this->registered[$handle]) )
   136 			return false;
   129 			return false;
   139 	}
   132 	}
   140 
   133 
   141 	/**
   134 	/**
   142 	 * Adds extra data
   135 	 * Adds extra data
   143 	 *
   136 	 *
   144 	 * Adds data only if script has already been added
   137 	 * Adds data only if script has already been added.
   145 	 *
   138 	 *
   146 	 * @param string handle Script name
   139 	 * @param string $handle Script name
   147 	 * @param string data_name Name of object in which to store extra data
   140 	 * @param string $key
   148 	 * @param array data Array of extra data
   141 	 * @param mixed $value
   149 	 * @return bool success
   142 	 * @return bool success
   150 	 */
   143 	 */
   151 	function add_data( $handle, $data_name, $data ) {
   144 	function add_data( $handle, $key, $value ) {
   152 		if ( !isset($this->registered[$handle]) )
   145 		if ( !isset( $this->registered[$handle] ) )
   153 			return false;
   146 			return false;
   154 		return $this->registered[$handle]->add_data( $data_name, $data );
   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];
   155 	}
   170 	}
   156 
   171 
   157 	function remove( $handles ) {
   172 	function remove( $handles ) {
   158 		foreach ( (array) $handles as $handle )
   173 		foreach ( (array) $handles as $handle )
   159 			unset($this->registered[$handle]);
   174 			unset($this->registered[$handle]);
   226 	var $ver = false;
   241 	var $ver = false;
   227 	var $args = null;
   242 	var $args = null;
   228 
   243 
   229 	var $extra = array();
   244 	var $extra = array();
   230 
   245 
   231 	function _WP_Dependency() {
   246 	function __construct() {
   232 		@list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
   247 		@list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
   233 		if ( !is_array($this->deps) )
   248 		if ( !is_array($this->deps) )
   234 			$this->deps = array();
   249 			$this->deps = array();
   235 		if ( !$this->ver )
       
   236 			$this->ver = false;
       
   237 	}
   250 	}
   238 
   251 
   239 	function add_data( $name, $data ) {
   252 	function add_data( $name, $data ) {
   240 		if ( !is_scalar($name) )
   253 		if ( !is_scalar($name) )
   241 			return false;
   254 			return false;