Script loader cleanup from mdawaffe. fixes #5003

git-svn-id: http://svn.automattic.com/wordpress/trunk@6133 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-09-19 00:51:21 +00:00
parent 1fa2f6df66
commit deb7701a85
1 changed files with 36 additions and 34 deletions

View File

@ -2,6 +2,7 @@
class WP_Scripts {
var $scripts = array();
var $queue = array();
var $to_print = array();
var $printed = array();
var $args = array();
@ -154,27 +155,15 @@ class WP_Scripts {
* @return array Scripts that have been printed
*/
function print_scripts( $handles = false ) {
// Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
$handles = false === $handles ? $this->queue : (array) $handles;
$handles = $this->all_deps( $handles );
$this->_print_scripts( $handles );
return $this->printed;
}
/**
* Internally used helper function for printing script tags
*
* @param array handles Hierarchical array of scripts to be printed
* @see WP_Scripts::all_deps()
*/
function _print_scripts( $handles ) {
global $wp_db_version;
foreach( array_keys($handles) as $handle ) {
if ( !$handles[$handle] )
return;
elseif ( is_array($handles[$handle]) )
$this->_print_scripts( $handles[$handle] );
// Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
$handles = false === $handles ? $this->queue : (array) $handles;
$this->all_deps( $handles );
$to_print = apply_filters( 'print_scripts_array', array_keys($this->to_print) );
foreach( $to_print as $handle ) {
if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) {
if ( $this->scripts[$handle]->src ) { // Else it defines a group.
$ver = $this->scripts[$handle]->ver ? $this->scripts[$handle]->ver : $wp_db_version;
@ -195,6 +184,9 @@ class WP_Scripts {
$this->printed[] = $handle;
}
}
$this->to_print = array();
return $this->printed;
}
function print_scripts_l10n( $handle ) {
@ -219,33 +211,43 @@ class WP_Scripts {
/**
* Determines dependencies of scripts
*
* Recursively builds hierarchical array of script dependencies. Does NOT catch infinite loops.
* Recursively builds array of scripts to print taking dependencies into account. Does NOT catch infinite loops.
*
* @param mixed handles Accepts (string) script name or (array of strings) script names
* @param bool recursion Used internally when function calls itself
* @return array Hierarchical array of dependencies
*/
function all_deps( $handles, $recursion = false ) {
if ( ! $handles = (array) $handles )
return array();
$return = array();
if ( !$handles = (array) $handles )
return false;
foreach ( $handles as $handle ) {
$handle = explode('?', $handle);
if ( isset($handle[1]) )
$this->args[$handle[0]] = $handle[1];
$handle = $handle[0];
if ( is_null($return[$handle]) ) // Prime the return array with $handles
$return[$handle] = true;
if ( $this->scripts[$handle]->deps ) {
if ( false !== $return[$handle] && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
$return[$handle] = false; // Script required deps which don't exist
if ( isset($this->to_print[$handle]) ) // Already grobbed it and its deps
continue;
$keep_going = true;
if ( !isset($this->scripts[$handle]) )
$keep_going = false; // Script doesn't exist
elseif ( $this->scripts[$handle]->deps && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
$keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?)
elseif ( $this->scripts[$handle]->deps && !$this->all_deps( $this->scripts[$handle]->deps, true ) )
$keep_going = false; // Script requires deps which don't exist
if ( !$keep_going ) { // Either script or its deps don't exist.
if ( $recursion )
return false; // Abort this branch.
else
$return[$handle] = $this->all_deps( $this->scripts[$handle]->deps, true ); // Build the hierarchy
}
if ( $recursion && false === $return[$handle] )
return false; // Cut the branch
continue; // We're at the top level. Move on to the next one.
}
$this->to_print[$handle] = true;
}
return $return;
return true;
}
/**