Make WordPress Core


Ignore:
Timestamp:
01/14/2009 02:18:51 PM (16 years ago)
Author:
azaozz
Message:

Split the script queue in head and footer part, concatenate and compress the default js and css, first run, see #8628

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class.wp-scripts.php

    r10135 r10357  
    1919class WP_Scripts extends WP_Dependencies {
    2020    var $base_url; // Full URL with trailing slash
     21    var $content_url;
    2122    var $default_version;
     23    var $in_footer = array();
     24    var $concat = '';
     25    var $concat_version = '';
     26    var $do_concat = false;
     27    var $print_html = '';
     28    var $print_code = '';
     29    var $default_dirs;
    2230
    2331    function __construct() {
     
    3139     *
    3240     * @param mixed handles (optional) Scripts to be printed.  (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
     41     * @param int group (optional) If scripts were queued in groups prints this group number.
    3342     * @return array Scripts that have been printed
    3443     */
    35     function print_scripts( $handles = false ) {
    36         return $this->do_items( $handles );
     44    function print_scripts( $handles = false, $group = false ) {
     45        return $this->do_items( $handles, $group );
    3746    }
    3847
    39     function print_scripts_l10n( $handle ) {
     48    function print_scripts_l10n( $handle, $echo = true ) {
    4049        if ( empty($this->registered[$handle]->extra['l10n']) || empty($this->registered[$handle]->extra['l10n'][0]) || !is_array($this->registered[$handle]->extra['l10n'][1]) )
    4150            return false;
     
    4352        $object_name = $this->registered[$handle]->extra['l10n'][0];
    4453
    45         echo "<script type='text/javascript'>\n";
    46         echo "/* <![CDATA[ */\n";
    47         echo "\t$object_name = {\n";
     54        $data = "var $object_name = {\n";
    4855        $eol = '';
    4956        foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
     
    5259                continue;
    5360            }
    54             echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
     61            $data .= "$eol\t$var: \"" . js_escape( $val ) . '"';
    5562            $eol = ",\n";
    5663        }
    57         echo "\n\t}\n";
    58         echo isset($after) ? "\t$after\n" : '';
    59         echo "/* ]]> */\n";
    60         echo "</script>\n";
     64        $data .= "\n};\n";
     65        $data .= isset($after) ? "$after\n" : '';
    6166
    62         return true;
     67        if ( $echo ) {
     68            echo "<script type='text/javascript'>\n";
     69            echo "/* <![CDATA[ */\n";
     70            echo $data;
     71            echo "/* ]]> */\n";
     72            echo "</script>\n";
     73            return true;
     74        } else {
     75            return $data;
     76        }
    6377    }
    6478
    65     function do_item( $handle ) {
     79    function do_item( $handle, $group = false ) {
    6680        if ( !parent::do_item($handle) )
    6781            return false;
     82
     83        if ( 0 === $group && $this->groups[$handle] > 0 ) {
     84            $this->in_footer[] = $handle;
     85            return false;
     86        }
     87
     88        if ( false === $group && in_array($handle, $this->in_footer, true) )
     89            $this->in_footer = array_diff( $this->in_footer, (array) $handle );
    6890
    6991        $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
     
    7294
    7395        $src = $this->registered[$handle]->src;
    74         if ( !preg_match('|^https?://|', $src) && !preg_match('|^' . preg_quote(WP_CONTENT_URL) . '|', $src) ) {
     96
     97        if ( $this->do_concat ) {
     98            $srce = apply_filters( 'script_loader_src', $src, $handle, $echo );
     99            if ( $this->in_default_dir($srce) ) {
     100                $this->print_code .= $this->print_scripts_l10n( $handle, false );
     101                $this->concat .= $handle . ',';
     102                $this->concat_version .= $ver;
     103                return true;
     104            }
     105        }
     106
     107        $this->print_scripts_l10n( $handle );
     108        if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
    75109            $src = $this->base_url . $src;
    76110        }
    77111
    78112        $src = add_query_arg('ver', $ver, $src);
    79         $src = clean_url(apply_filters( 'script_loader_src', $src, $handle ));
     113        $src = clean_url(apply_filters( 'script_loader_src', $src, $handle, $echo ));
    80114
    81         $this->print_scripts_l10n( $handle );
    82 
    83         echo "<script type='text/javascript' src='$src'></script>\n";
     115        if ( $this->do_concat )
     116            $this->print_html .= "<script type='text/javascript' src='$src'></script>\n";
     117        else
     118            echo "<script type='text/javascript' src='$src'></script>\n";
    84119
    85120        return true;
     
    102137    }
    103138
     139    function set_group( $handle, $recursion, $group = false ) {
     140        $grp = isset($this->registered[$handle]->extra['group']) ? (int) $this->registered[$handle]->extra['group'] : 0;
     141        if ( false !== $group && $grp > $group )
     142            $grp = $group;
     143
     144        parent::set_group( $handle, $recursion, $grp );
     145    }
     146
    104147    function all_deps( $handles, $recursion = false ) {
    105148        $r = parent::all_deps( $handles, $recursion );
     
    108151        return $r;
    109152    }
     153
     154    function do_head_items() {
     155        $this->do_items(false, 0);
     156        return $this->done;
     157    }
     158
     159    function do_footer_items() {
     160        if ( !empty($this->in_footer) ) {
     161            foreach( $this->in_footer as $key => $handle ) {
     162                if ( !in_array($handle, $this->done, true) && isset($this->registered[$handle]) ) {
     163                    $this->do_item($handle, false, $this->doecho);
     164                    $this->done[] = $handle;
     165                    unset( $this->in_footer[$key] );
     166                }
     167            }
     168        }
     169        return $this->done;
     170    }
     171
     172    function in_default_dir($src) {
     173        if ( ! $this->default_dirs )
     174            return true;
     175
     176        foreach ( (array) $this->default_dirs as $test ) {
     177            if ( 0 === strpos($src, $test) )
     178                return true;
     179        }
     180        return false;
     181    }
    110182}
Note: See TracChangeset for help on using the changeset viewer.