Make WordPress Core

Changeset 6133


Ignore:
Timestamp:
09/19/2007 12:51:21 AM (17 years ago)
Author:
ryan
Message:

Script loader cleanup from mdawaffe. fixes #5003

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/script-loader.php

    r5939 r6133  
    33    var $scripts = array();
    44    var $queue = array();
     5    var $to_print = array();
    56    var $printed = array();
    67    var $args = array();
     
    155156     */
    156157    function print_scripts( $handles = false ) {
     158        global $wp_db_version;
     159
    157160        // Print the queue if nothing is passed.  If a string is passed, print that script.  If an array is passed, print those scripts.
    158161        $handles = false === $handles ? $this->queue : (array) $handles;
    159         $handles = $this->all_deps( $handles );
    160         $this->_print_scripts( $handles );
    161         return $this->printed;
    162     }
    163 
    164     /**
    165      * Internally used helper function for printing script tags
    166      *
    167      * @param array handles Hierarchical array of scripts to be printed
    168      * @see WP_Scripts::all_deps()
    169      */
    170     function _print_scripts( $handles ) {
    171         global $wp_db_version;
    172 
    173         foreach( array_keys($handles) as $handle ) {
    174             if ( !$handles[$handle] )
    175                 return;
    176             elseif ( is_array($handles[$handle]) )
    177                 $this->_print_scripts( $handles[$handle] );
     162        $this->all_deps( $handles );
     163
     164        $to_print = apply_filters( 'print_scripts_array', array_keys($this->to_print) );
     165
     166        foreach( $to_print as $handle ) {
    178167            if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) {
    179168                if ( $this->scripts[$handle]->src ) { // Else it defines a group.
     
    196185            }
    197186        }
     187
     188        $this->to_print = array();
     189        return $this->printed;
    198190    }
    199191
     
    220212     * Determines dependencies of scripts
    221213     *
    222      * Recursively builds hierarchical array of script dependencies.  Does NOT catch infinite loops.
     214     * Recursively builds array of scripts to print taking dependencies into account.  Does NOT catch infinite loops.
    223215     *
    224216     * @param mixed handles Accepts (string) script name or (array of strings) script names
    225217     * @param bool recursion Used internally when function calls itself
    226      * @return array Hierarchical array of dependencies
    227218     */
    228219    function all_deps( $handles, $recursion = false ) {
    229         if ( ! $handles = (array) $handles )
    230             return array();
    231         $return = array();
     220        if ( !$handles = (array) $handles )
     221            return false;
     222
    232223        foreach ( $handles as $handle ) {
    233224            $handle = explode('?', $handle);
     
    235226                $this->args[$handle[0]] = $handle[1];
    236227            $handle = $handle[0];
    237             if ( is_null($return[$handle]) ) // Prime the return array with $handles
    238                 $return[$handle] = true;
    239             if ( $this->scripts[$handle]->deps ) {
    240                 if ( false !== $return[$handle] && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
    241                     $return[$handle] = false; // Script required deps which don't exist
     228
     229            if ( isset($this->to_print[$handle]) ) // Already grobbed it and its deps
     230                continue;
     231
     232            $keep_going = true;
     233            if ( !isset($this->scripts[$handle]) )
     234                $keep_going = false; // Script doesn't exist
     235            elseif ( $this->scripts[$handle]->deps && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
     236                $keep_going = false; // Script requires deps which don't exist (not a necessary check.  efficiency?)
     237            elseif ( $this->scripts[$handle]->deps && !$this->all_deps( $this->scripts[$handle]->deps, true ) )
     238                $keep_going = false; // Script requires deps which don't exist
     239
     240            if ( !$keep_going ) { // Either script or its deps don't exist.
     241                if ( $recursion )
     242                    return false; // Abort this branch.
    242243                else
    243                     $return[$handle] = $this->all_deps( $this->scripts[$handle]->deps, true ); // Build the hierarchy
    244             }
    245             if ( $recursion && false === $return[$handle] )
    246                 return false; // Cut the branch
     244                    continue; // We're at the top level.  Move on to the next one.
     245            }                   
     246
     247            $this->to_print[$handle] = true;
    247248        }
    248         return $return;
     249
     250        return true;
    249251    }
    250252
Note: See TracChangeset for help on using the changeset viewer.