wp/wp-includes/class.wp-dependencies.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    94 		 */
    94 		 */
    95 		$handles = false === $handles ? $this->queue : (array) $handles;
    95 		$handles = false === $handles ? $this->queue : (array) $handles;
    96 		$this->all_deps( $handles );
    96 		$this->all_deps( $handles );
    97 
    97 
    98 		foreach ( $this->to_do as $key => $handle ) {
    98 		foreach ( $this->to_do as $key => $handle ) {
    99 			if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
    99 			if ( ! in_array( $handle, $this->done, true ) && isset( $this->registered[ $handle ] ) ) {
   100 				/*
   100 				/*
   101 				 * Attempt to process the item. If successful,
   101 				 * Attempt to process the item. If successful,
   102 				 * add the handle to the done array.
   102 				 * add the handle to the done array.
   103 				 *
   103 				 *
   104 				 * Unset the item from the to_do array.
   104 				 * Unset the item from the to_do array.
   105 				 */
   105 				 */
   106 				if ( $this->do_item( $handle, $group ) )
   106 				if ( $this->do_item( $handle, $group ) ) {
   107 					$this->done[] = $handle;
   107 					$this->done[] = $handle;
   108 
   108 				}
   109 				unset( $this->to_do[$key] );
   109 
       
   110 				unset( $this->to_do[ $key ] );
   110 			}
   111 			}
   111 		}
   112 		}
   112 
   113 
   113 		return $this->done;
   114 		return $this->done;
   114 	}
   115 	}
   120 	 *
   121 	 *
   121 	 * @param string $handle Name of the item. Should be unique.
   122 	 * @param string $handle Name of the item. Should be unique.
   122 	 * @return bool True on success, false if not set.
   123 	 * @return bool True on success, false if not set.
   123 	 */
   124 	 */
   124 	public function do_item( $handle ) {
   125 	public function do_item( $handle ) {
   125 		return isset($this->registered[$handle]);
   126 		return isset( $this->registered[ $handle ] );
   126 	}
   127 	}
   127 
   128 
   128 	/**
   129 	/**
   129 	 * Determines dependencies.
   130 	 * Determines dependencies.
   130 	 *
   131 	 *
   139 	 * @param bool      $recursion Internal flag that function is calling itself.
   140 	 * @param bool      $recursion Internal flag that function is calling itself.
   140 	 * @param int|false $group     Group level: (int) level, (false) no groups.
   141 	 * @param int|false $group     Group level: (int) level, (false) no groups.
   141 	 * @return bool True on success, false on failure.
   142 	 * @return bool True on success, false on failure.
   142 	 */
   143 	 */
   143 	public function all_deps( $handles, $recursion = false, $group = false ) {
   144 	public function all_deps( $handles, $recursion = false, $group = false ) {
   144 		if ( !$handles = (array) $handles )
   145 		if ( ! $handles = (array) $handles ) {
   145 			return false;
   146 			return false;
       
   147 		}
   146 
   148 
   147 		foreach ( $handles as $handle ) {
   149 		foreach ( $handles as $handle ) {
   148 			$handle_parts = explode('?', $handle);
   150 			$handle_parts = explode( '?', $handle );
   149 			$handle = $handle_parts[0];
   151 			$handle       = $handle_parts[0];
   150 			$queued = in_array($handle, $this->to_do, true);
   152 			$queued       = in_array( $handle, $this->to_do, true );
   151 
   153 
   152 			if ( in_array($handle, $this->done, true) ) // Already done
   154 			if ( in_array( $handle, $this->done, true ) ) { // Already done
   153 				continue;
   155 				continue;
       
   156 			}
   154 
   157 
   155 			$moved     = $this->set_group( $handle, $recursion, $group );
   158 			$moved     = $this->set_group( $handle, $recursion, $group );
   156 			$new_group = $this->groups[ $handle ];
   159 			$new_group = $this->groups[ $handle ];
   157 
   160 
   158 			if ( $queued && !$moved ) // already queued and in the right group
   161 			if ( $queued && ! $moved ) { // already queued and in the right group
   159 				continue;
   162 				continue;
       
   163 			}
   160 
   164 
   161 			$keep_going = true;
   165 			$keep_going = true;
   162 			if ( !isset($this->registered[$handle]) )
   166 			if ( ! isset( $this->registered[ $handle ] ) ) {
   163 				$keep_going = false; // Item doesn't exist.
   167 				$keep_going = false; // Item doesn't exist.
   164 			elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
   168 			} elseif ( $this->registered[ $handle ]->deps && array_diff( $this->registered[ $handle ]->deps, array_keys( $this->registered ) ) ) {
   165 				$keep_going = false; // Item requires dependencies that don't exist.
   169 				$keep_going = false; // Item requires dependencies that don't exist.
   166 			elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $new_group ) )
   170 			} elseif ( $this->registered[ $handle ]->deps && ! $this->all_deps( $this->registered[ $handle ]->deps, true, $new_group ) ) {
   167 				$keep_going = false; // Item requires dependencies that don't exist.
   171 				$keep_going = false; // Item requires dependencies that don't exist.
       
   172 			}
   168 
   173 
   169 			if ( ! $keep_going ) { // Either item or its dependencies don't exist.
   174 			if ( ! $keep_going ) { // Either item or its dependencies don't exist.
   170 				if ( $recursion )
   175 				if ( $recursion ) {
   171 					return false; // Abort this branch.
   176 					return false; // Abort this branch.
   172 				else
   177 				} else {
   173 					continue; // We're at the top level. Move on to the next one.
   178 					continue; // We're at the top level. Move on to the next one.
   174 			}
   179 				}
   175 
   180 			}
   176 			if ( $queued ) // Already grabbed it and its dependencies.
   181 
       
   182 			if ( $queued ) { // Already grabbed it and its dependencies.
   177 				continue;
   183 				continue;
   178 
   184 			}
   179 			if ( isset($handle_parts[1]) )
   185 
   180 				$this->args[$handle] = $handle_parts[1];
   186 			if ( isset( $handle_parts[1] ) ) {
       
   187 				$this->args[ $handle ] = $handle_parts[1];
       
   188 			}
   181 
   189 
   182 			$this->to_do[] = $handle;
   190 			$this->to_do[] = $handle;
   183 		}
   191 		}
   184 
   192 
   185 		return true;
   193 		return true;
   192 	 *
   200 	 *
   193 	 * @since 2.1.0
   201 	 * @since 2.1.0
   194 	 * @since 2.6.0 Moved from `WP_Scripts`.
   202 	 * @since 2.6.0 Moved from `WP_Scripts`.
   195 	 *
   203 	 *
   196 	 * @param string           $handle Name of the item. Should be unique.
   204 	 * @param string           $handle Name of the item. Should be unique.
   197 	 * @param string           $src    Full URL of the item, or path of the item relative to the WordPress root directory.
   205 	 * @param string|bool      $src    Full URL of the item, or path of the item relative to the WordPress root directory.
   198 	 * @param array            $deps   Optional. An array of registered item handles this item depends on. Default empty array.
   206 	 *                                 If source is set to false, item is an alias of other items it depends on.
       
   207 	 * @param string[]         $deps   Optional. An array of registered item handles this item depends on. Default empty array.
   199 	 * @param string|bool|null $ver    Optional. String specifying item version number, if it has one, which is added to the URL
   208 	 * @param string|bool|null $ver    Optional. String specifying item version number, if it has one, which is added to the URL
   200 	 *                                 as a query string for cache busting purposes. If version is set to false, a version
   209 	 *                                 as a query string for cache busting purposes. If version is set to false, a version
   201 	 *                                 number is automatically added equal to current installed WordPress version.
   210 	 *                                 number is automatically added equal to current installed WordPress version.
   202 	 *                                 If set to null, no version is added.
   211 	 *                                 If set to null, no version is added.
   203 	 * @param mixed            $args   Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer.
   212 	 * @param mixed            $args   Optional. Custom property of the item. NOT the class property $args. Examples: $media, $in_footer.
   204 	 * @return bool Whether the item has been registered. True on success, false on failure.
   213 	 * @return bool Whether the item has been registered. True on success, false on failure.
   205 	 */
   214 	 */
   206 	public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
   215 	public function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
   207 		if ( isset($this->registered[$handle]) )
   216 		if ( isset( $this->registered[ $handle ] ) ) {
   208 			return false;
   217 			return false;
   209 		$this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
   218 		}
       
   219 		$this->registered[ $handle ] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
   210 		return true;
   220 		return true;
   211 	}
   221 	}
   212 
   222 
   213 	/**
   223 	/**
   214 	 * Add extra item data.
   224 	 * Add extra item data.
   221 	 * @param string $key    The data key.
   231 	 * @param string $key    The data key.
   222 	 * @param mixed  $value  The data value.
   232 	 * @param mixed  $value  The data value.
   223 	 * @return bool True on success, false on failure.
   233 	 * @return bool True on success, false on failure.
   224 	 */
   234 	 */
   225 	public function add_data( $handle, $key, $value ) {
   235 	public function add_data( $handle, $key, $value ) {
   226 		if ( !isset( $this->registered[$handle] ) )
   236 		if ( ! isset( $this->registered[ $handle ] ) ) {
   227 			return false;
   237 			return false;
   228 
   238 		}
   229 		return $this->registered[$handle]->add_data( $key, $value );
   239 
       
   240 		return $this->registered[ $handle ]->add_data( $key, $value );
   230 	}
   241 	}
   231 
   242 
   232 	/**
   243 	/**
   233 	 * Get extra item data.
   244 	 * Get extra item data.
   234 	 *
   245 	 *
   239 	 * @param string $handle Name of the item. Should be unique.
   250 	 * @param string $handle Name of the item. Should be unique.
   240 	 * @param string $key    The data key.
   251 	 * @param string $key    The data key.
   241 	 * @return mixed Extra item data (string), false otherwise.
   252 	 * @return mixed Extra item data (string), false otherwise.
   242 	 */
   253 	 */
   243 	public function get_data( $handle, $key ) {
   254 	public function get_data( $handle, $key ) {
   244 		if ( !isset( $this->registered[$handle] ) )
   255 		if ( ! isset( $this->registered[ $handle ] ) ) {
   245 			return false;
   256 			return false;
   246 
   257 		}
   247 		if ( !isset( $this->registered[$handle]->extra[$key] ) )
   258 
   248 			return false;
   259 		if ( ! isset( $this->registered[ $handle ]->extra[ $key ] ) ) {
   249 
   260 			return false;
   250 		return $this->registered[$handle]->extra[$key];
   261 		}
       
   262 
       
   263 		return $this->registered[ $handle ]->extra[ $key ];
   251 	}
   264 	}
   252 
   265 
   253 	/**
   266 	/**
   254 	 * Un-register an item or items.
   267 	 * Un-register an item or items.
   255 	 *
   268 	 *
   258 	 *
   271 	 *
   259 	 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
   272 	 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
   260 	 * @return void
   273 	 * @return void
   261 	 */
   274 	 */
   262 	public function remove( $handles ) {
   275 	public function remove( $handles ) {
   263 		foreach ( (array) $handles as $handle )
   276 		foreach ( (array) $handles as $handle ) {
   264 			unset($this->registered[$handle]);
   277 			unset( $this->registered[ $handle ] );
       
   278 		}
   265 	}
   279 	}
   266 
   280 
   267 	/**
   281 	/**
   268 	 * Queue an item or items.
   282 	 * Queue an item or items.
   269 	 *
   283 	 *
   277 	 *
   291 	 *
   278 	 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
   292 	 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
   279 	 */
   293 	 */
   280 	public function enqueue( $handles ) {
   294 	public function enqueue( $handles ) {
   281 		foreach ( (array) $handles as $handle ) {
   295 		foreach ( (array) $handles as $handle ) {
   282 			$handle = explode('?', $handle);
   296 			$handle = explode( '?', $handle );
   283 			if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
   297 			if ( ! in_array( $handle[0], $this->queue ) && isset( $this->registered[ $handle[0] ] ) ) {
   284 				$this->queue[] = $handle[0];
   298 				$this->queue[] = $handle[0];
   285 				if ( isset($handle[1]) )
   299 				if ( isset( $handle[1] ) ) {
   286 					$this->args[$handle[0]] = $handle[1];
   300 					$this->args[ $handle[0] ] = $handle[1];
       
   301 				}
   287 			}
   302 			}
   288 		}
   303 		}
   289 	}
   304 	}
   290 
   305 
   291 	/**
   306 	/**
   299 	 *
   314 	 *
   300 	 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
   315 	 * @param mixed $handles Item handle and argument (string) or item handles and arguments (array of strings).
   301 	 */
   316 	 */
   302 	public function dequeue( $handles ) {
   317 	public function dequeue( $handles ) {
   303 		foreach ( (array) $handles as $handle ) {
   318 		foreach ( (array) $handles as $handle ) {
   304 			$handle = explode('?', $handle);
   319 			$handle = explode( '?', $handle );
   305 			$key = array_search($handle[0], $this->queue);
   320 			$key    = array_search( $handle[0], $this->queue );
   306 			if ( false !== $key ) {
   321 			if ( false !== $key ) {
   307 				unset($this->queue[$key]);
   322 				unset( $this->queue[ $key ] );
   308 				unset($this->args[$handle[0]]);
   323 				unset( $this->args[ $handle[0] ] );
   309 			}
   324 			}
   310 		}
   325 		}
   311 	}
   326 	}
   312 
   327 
   313 	/**
   328 	/**
   314 	 * Recursively search the passed dependency tree for $handle
   329 	 * Recursively search the passed dependency tree for $handle
   315 	 *
   330 	 *
   316 	 * @since 4.0.0
   331 	 * @since 4.0.0
   317 	 *
   332 	 *
   318 	 * @param array  $queue  An array of queued _WP_Dependency handle objects.
   333 	 * @param string[] $queue  An array of queued _WP_Dependency handles.
   319 	 * @param string $handle Name of the item. Should be unique.
   334 	 * @param string   $handle Name of the item. Should be unique.
   320 	 * @return bool Whether the handle is found after recursively searching the dependency tree.
   335 	 * @return bool Whether the handle is found after recursively searching the dependency tree.
   321 	 */
   336 	 */
   322 	protected function recurse_deps( $queue, $handle ) {
   337 	protected function recurse_deps( $queue, $handle ) {
   323 		foreach ( $queue as $queued ) {
   338 		foreach ( $queue as $queued ) {
   324 			if ( ! isset( $this->registered[ $queued ] ) ) {
   339 			if ( ! isset( $this->registered[ $queued ] ) ) {
   345 	 * @param string $list   Property name of list array.
   360 	 * @param string $list   Property name of list array.
   346 	 * @return bool|_WP_Dependency Found, or object Item data.
   361 	 * @return bool|_WP_Dependency Found, or object Item data.
   347 	 */
   362 	 */
   348 	public function query( $handle, $list = 'registered' ) {
   363 	public function query( $handle, $list = 'registered' ) {
   349 		switch ( $list ) {
   364 		switch ( $list ) {
   350 			case 'registered' :
   365 			case 'registered':
   351 			case 'scripts': // back compat
   366 			case 'scripts': // back compat
   352 				if ( isset( $this->registered[ $handle ] ) )
   367 				if ( isset( $this->registered[ $handle ] ) ) {
   353 					return $this->registered[ $handle ];
   368 					return $this->registered[ $handle ];
       
   369 				}
   354 				return false;
   370 				return false;
   355 
   371 
   356 			case 'enqueued' :
   372 			case 'enqueued':
   357 			case 'queue' :
   373 			case 'queue':
   358 				if ( in_array( $handle, $this->queue ) ) {
   374 				if ( in_array( $handle, $this->queue ) ) {
   359 					return true;
   375 					return true;
   360 				}
   376 				}
   361 				return $this->recurse_deps( $this->queue, $handle );
   377 				return $this->recurse_deps( $this->queue, $handle );
   362 
   378 
   363 			case 'to_do' :
   379 			case 'to_do':
   364 			case 'to_print': // back compat
   380 			case 'to_print': // back compat
   365 				return in_array( $handle, $this->to_do );
   381 				return in_array( $handle, $this->to_do );
   366 
   382 
   367 			case 'done' :
   383 			case 'done':
   368 			case 'printed': // back compat
   384 			case 'printed': // back compat
   369 				return in_array( $handle, $this->done );
   385 				return in_array( $handle, $this->done );
   370 		}
   386 		}
   371 		return false;
   387 		return false;
   372 	}
   388 	}