WordPress.org

Make WordPress Core

Changeset 10442


Ignore:
Timestamp:
01/26/2009 12:59:10 PM (9 years ago)
Author:
azaozz
Message:

Improvements to the script loader: allows plugins to queue scripts for the front end head and footer, adds hooks for server side caching of compressed scripts, adds support for ENFORCE_GZIP constant (deflate is used by default since it's faster), see #8628, fixes #8884

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/template.php

    r10424 r10442  
    33963396
    33973397/**
    3398  * Test support for compressed JavaScript and CSS
     3398 * Test support for compressing JavaScript from PHP
    33993399 *
    34003400 * Outputs JavaScript that tests if compression from PHP works as expected
     
    34073407function compression_test() {
    34083408?>
    3409     <script type="text/javascript" src="load-scripts.php?test=1"></script>
     3409    <script type="text/javascript" src="load-scripts.php?test=1<?php echo ( defined('ENFORCE_GZIP') && ENFORCE_GZIP ) ? '&c=gzip' : ''; ?>"></script>
    34103410    <script type="text/javascript">
    34113411    /* <![CDATA[ */
  • trunk/wp-admin/load-scripts.php

    r10372 r10442  
    9393        exit('');
    9494
    95     if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
     95    $out = 'var wpCompressionTest = 1;';
     96    $force_gzip = ( isset($_GET['c']) && 'gzip' == $_GET['c'] );
     97
     98    if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
     99        header('Content-Encoding: deflate');
     100        $out = gzdeflate( $out, 3 );
     101    } elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
    96102        header('Content-Encoding: gzip');
    97         $out = gzencode( 'var wpCompressionTest = 1;', 3 );
     103        $out = gzencode( $out, 3 );
     104    } else {
     105        exit('');
    98106    }
    99    
    100     if ( ! isset($out) )
    101         exit('');
    102    
     107
    103108    header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
    104109    header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
     
    119124require(ABSPATH . '/wp-includes/version.php');
    120125
    121 $compress = ( isset($_GET['c']) && 1 == $_GET['c'] );
     126$compress = ( isset($_GET['c']) && $_GET['c'] );
     127$force_gzip = ( $compress && 'gzip' == $_GET['c'] );
    122128$expires_offset = 31536000;
    123129$out = '';
     
    138144header("Cache-Control: public, max-age=$expires_offset");
    139145   
    140 if ( $compress && ! ini_get('zlib.output_compression') && function_exists('gzencode') ) {
     146if ( $compress && ! ini_get('zlib.output_compression') ) {
    141147    header('Vary: Accept-Encoding'); // Handle proxies
    142     if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') ) {
     148    if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
     149        header('Content-Encoding: deflate');
     150        $out = gzdeflate( $out, 3 );
     151    } elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
    143152        header('Content-Encoding: gzip');
    144153        $out = gzencode( $out, 3 );
  • trunk/wp-admin/load-styles.php

    r10372 r10442  
    9898    exit;
    9999
    100 $compress = ( isset($_GET['c']) && 1 == $_GET['c'] );
    101 $rtl = ( isset($_GET['rtl']) && 1 == $_GET['rtl'] );
     100$compress = ( isset($_GET['c']) && $_GET['c'] );
     101$force_gzip = ( $compress && 'gzip' == $_GET['c'] );
     102$rtl = ( isset($_GET['dir']) && 'rtl' == $_GET['dir'] );
    102103$expires_offset = 31536000;
    103104$out = '';
     
    127128header("Cache-Control: public, max-age=$expires_offset");
    128129   
    129 if ( $compress && ! ini_get('zlib.output_compression') && function_exists('gzencode') ) {
     130if ( $compress && ! ini_get('zlib.output_compression') ) {
    130131    header('Vary: Accept-Encoding'); // Handle proxies
    131     if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') ) {
     132    if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
     133        header('Content-Encoding: deflate');
     134        $out = gzdeflate( $out, 3 );
     135    } elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
    132136        header('Content-Encoding: gzip');
    133137        $out = gzencode( $out, 3 );
  • trunk/wp-includes/class.wp-scripts.php

    r10367 r10442  
    2727    var $print_html = '';
    2828    var $print_code = '';
    29     var $src = '';
     29    var $ext_handles = '';
     30    var $ext_version = '';
    3031    var $default_dirs;
    3132
     
    100101            if ( $this->in_default_dir($srce) ) {
    101102                $this->print_code .= $this->print_scripts_l10n( $handle, false );
    102                 $this->concat .= $handle . ',';
    103                 $this->concat_version .= $ver;
     103                $this->concat .= "$handle,";
     104                $this->concat_version .= "$handle$ver";
    104105                return true;
     106            } else {
     107                $this->ext_handles .= "$handle,";
     108                $this->ext_version .= "$handle$ver";
    105109            }
    106110        }
     
    111115        }
    112116
    113         $this->src .= "$src,";
    114117        $src = add_query_arg('ver', $ver, $src);
    115118        $src = clean_url(apply_filters( 'script_loader_src', $src, $handle ));
     
    147150    }
    148151
    149     function all_deps( $handles, $recursion = false ) {
     152    function all_deps( $handles, $recursion = false, $group = false ) {
    150153        $r = parent::all_deps( $handles, $recursion );
    151154        if ( !$recursion )
     
    182185        return false;
    183186    }
     187
     188    function reset() {
     189        $this->do_concat = false;
     190        $this->print_code = '';
     191        $this->concat = '';
     192        $this->concat_version = '';
     193        $this->print_html = '';
     194        $this->ext_version = '';
     195        $this->ext_handles = '';
     196    }
    184197}
  • trunk/wp-includes/class.wp-styles.php

    r10357 r10442  
    4242        if ( $this->do_concat ) {
    4343            if ( $this->in_default_dir($this->registered[$handle]->src) && !isset($this->registered[$handle]->extra['conditional']) && !isset($this->registered[$handle]->extra['alt']) ) {
    44                 $this->concat .= $handle . ',';
    45                 $this->concat_version .= $ver;
     44                $this->concat .= "$handle,";
     45                $this->concat_version .= "$handle$ver";
    4646                return true;
    4747            }
     
    8989    }
    9090
    91     function all_deps( $handles, $recursion = false ) {
     91    function all_deps( $handles, $recursion = false, $group = false ) {
    9292        $r = parent::all_deps( $handles, $recursion );
    9393        if ( !$recursion )
  • trunk/wp-includes/default-filters.php

    r10377 r10442  
    165165
    166166// Actions
     167add_action('wp_head', 'wp_enqueue_scripts', 1);
    167168add_action('wp_head', 'feed_links_extra', 3);
    168169add_action('wp_head', 'rsd_link');
     
    171172add_action('publish_future_post', 'check_and_publish_future_post', 10, 1);
    172173add_action('wp_head', 'noindex', 1);
    173 add_action('wp_head', 'wp_print_styles', 9);
    174 add_action('wp_head', 'wp_print_scripts');
     174add_action('wp_head', 'wp_print_styles', 8);
     175add_action('wp_head', 'wp_print_head_scripts', 9);
    175176add_action('wp_head', 'wp_generator');
     177add_action('wp_footer', 'wp_print_footer_scripts');
    176178if(!defined('DOING_CRON'))
    177179    add_action('init', 'wp_cron');
     
    183185add_action('do_robots', 'do_robots');
    184186add_action('sanitize_comment_cookies', 'sanitize_comment_cookies');
    185 add_action('admin_print_scripts', 'wp_print_head_scripts', 20);
    186 add_action('admin_print_footer_scripts', 'wp_print_footer_scripts', 20);
    187 add_action('admin_print_styles', 'wp_print_admin_styles', 20);
     187add_action('admin_print_scripts', 'print_head_scripts', 20);
     188add_action('admin_print_footer_scripts', 'print_footer_scripts', 20);
     189add_action('admin_print_styles', 'print_admin_styles', 20);
    188190add_action('init', 'smilies_init', 5);
    189191add_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 );
  • trunk/wp-includes/script-loader.php

    r10441 r10442  
    527527
    528528/**
    529  * Print the script queue in the HTML head.
     529 * Prints the script queue in the HTML head on admin pages.
    530530 *
    531531 * Postpones the scripts that were queued for the footer.
    532  * wp_print_footer_scripts() has to be called in the footer to print these scripts.
    533  *
    534  * @since unknown
     532 * print_footer_scripts() is called in the footer to print these scripts.
     533 *
     534 * @since 2.8
    535535 * @see wp_print_scripts()
    536536 */
    537 function wp_print_head_scripts() {
    538     do_action( 'wp_print_scripts' );
     537function print_head_scripts() {
     538    if ( ! did_action('wp_print_scripts') )
     539        do_action('wp_print_scripts');
     540
    539541    global $wp_scripts, $concatenate_scripts;
    540542
     
    542544        $wp_scripts = new WP_Scripts();
    543545
    544     if ( ! isset($concatenate_scripts) )
    545         script_concat_settings();
    546 
     546    script_concat_settings();
    547547    $wp_scripts->do_concat = $concatenate_scripts;
    548548    $wp_scripts->do_head_items();
     
    551551        _pring_scripts();
    552552
    553     $wp_scripts->do_concat = false;
    554     $wp_scripts->print_code = $wp_scripts->concat = $wp_scripts->concat_version = $wp_scripts->print_html = $wp_scripts->src = '';
     553    $wp_scripts->reset();
    555554    return $wp_scripts->done;
    556555}
    557556
    558557/**
    559  * Print the scripts that were queued for the footer.
    560  *
    561  * @since unknown
    562  */
    563 function wp_print_footer_scripts() {
     558 * Prints the scripts that were queued for the footer on admin pages.
     559 *
     560 * @since 2.8
     561 */
     562function print_footer_scripts() {
    564563    global $wp_scripts, $concatenate_scripts;
    565564
     
    567566        return array(); // No need to run if not instantiated.
    568567
    569     if ( ! isset($concatenate_scripts) )
    570         script_concat_settings();
    571 
     568    script_concat_settings();
    572569    $wp_scripts->do_concat = $concatenate_scripts;
    573570    $wp_scripts->do_footer_items();
     
    576573        _pring_scripts();
    577574
    578     $wp_scripts->do_concat = false;
    579     $wp_scripts->concat = $wp_scripts->concat_version = $wp_scripts->print_code = $wp_scripts->print_html = $wp_scripts->src = '';
     575    $wp_scripts->reset();
    580576    return $wp_scripts->done;
    581577}
     
    585581
    586582    $zip = $compress_scripts ? 1 : 0;
     583    if ( $zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP )
     584        $zip = 'gzip';
    587585
    588586    if ( !empty($wp_scripts->concat) ) {
     
    596594        }
    597595
    598         $ver = md5("$wp_scripts->concat" . "$wp_scripts->concat_version");
    599         $src = $wp_scripts->base_url . "/wp-admin/load-scripts.php?c={$zip}&load=" . rtrim($wp_scripts->concat, ',') . "&ver=$ver";
     596        $ver = md5("$wp_scripts->concat_version");
     597        $src = $wp_scripts->base_url . "/wp-admin/load-scripts.php?c={$zip}&load=" . trim($wp_scripts->concat, ', ') . "&ver=$ver";
    600598        echo "<script type='text/javascript' src='$src'></script>\n";
    601599    }
     
    603601    if ( !empty($wp_scripts->print_html) )
    604602        echo $wp_scripts->print_html;
    605 
    606 }
    607 
    608 function wp_print_admin_styles() {
     603}
     604
     605/**
     606 * Prints the script queue in the HTML head on the front end.
     607 *
     608 * Postpones the scripts that were queued for the footer.
     609 * wp_print_footer_scripts() is called in the footer to print these scripts.
     610 *
     611 * @since 2.8
     612 */
     613function wp_print_head_scripts() {
     614    if ( ! did_action('wp_print_scripts') )
     615        do_action('wp_print_scripts');
     616
     617    global $wp_scripts;
     618   
     619    if ( !is_a($wp_scripts, 'WP_Scripts') )
     620        return array(); // no need to run if nothing is queued
     621   
     622    return print_head_scripts();
     623}
     624
     625/**
     626 * Prints the scripts that were queued for the footer on the front end.
     627 *
     628 * @since 2.8
     629 */
     630function wp_print_footer_scripts() {
     631    return print_footer_scripts();
     632}
     633
     634/**
     635 * Wrapper for do_action('wp_enqueue_scripts')
     636 * 
     637 * Allows plugins to queue scripts for the front end using wp_enqueue_script().
     638 * Runs first in wp_head() where all is_home(), is_page(), etc. functions are available.
     639 *
     640 * @since 2.8
     641 */
     642function wp_enqueue_scripts() {
     643    do_action('wp_enqueue_scripts');
     644}
     645
     646function print_admin_styles() {
    609647    global $wp_styles, $concatenate_scripts, $compress_css;
    610648
     
    612650        $wp_styles = new WP_Styles();
    613651
    614     if ( ! isset($concatenate_scripts) )
    615         script_concat_settings();
    616 
     652    script_concat_settings();
    617653    $wp_styles->do_concat = $concatenate_scripts;
    618654    $zip = $compress_css ? 1 : 0;
     655    if ( $zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP )
     656        $zip = 'gzip';
    619657
    620658    $wp_styles->do_items(false);
     
    622660    if ( apply_filters('print_admin_styles', true) ) {
    623661        if ( !empty($wp_styles->concat) ) {
    624             $ver = md5("$wp_styles->concat" . "$wp_styles->concat_version");
    625             $rtl = 'rtl' === $wp_styles->text_direction ? 1 : 0;
    626             $href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&rtl={$rtl}&load=" . rtrim($wp_styles->concat, ',') . "&ver=$ver";
     662            $dir = $wp_styles->text_direction;
     663            $ver = md5("$wp_styles->concat_version{$dir}");
     664            $href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&dir={$dir}&load=" . trim($wp_styles->concat, ', ') . "&ver=$ver";
    627665            echo "<link rel='stylesheet' href='$href' type='text/css' media='all' />\n";
    628666        }
     
    640678    global $concatenate_scripts, $compress_scripts, $compress_css;
    641679
    642     $concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true;
    643     if ( $concatenate_scripts && -1 == get_option('concatenate_scripts') )
    644         $concatenate_scripts = false;
    645 
    646     $compress_scripts = defined('COMPRESS_SCRIPTS') ? COMPRESS_SCRIPTS : true;
    647     if ( $compress_scripts && ! get_option('can_compress_scripts') )
    648         $compress_scripts = false;
    649 
    650     $compress_css = defined('COMPRESS_CSS') ? COMPRESS_CSS : true;
    651     if ( $compress_css && ! get_option('can_compress_scripts') )
    652         $compress_css = false;
     680    if ( ! isset($concatenate_scripts) ) {
     681        $concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true;
     682        if ( ! is_admin() || ( $concatenate_scripts && -1 == get_user_option('concatenate_scripts') ) )
     683            $concatenate_scripts = false;
     684    }
     685
     686    if ( ! isset($compress_scripts) ) {
     687        $compress_scripts = defined('COMPRESS_SCRIPTS') ? COMPRESS_SCRIPTS : true;
     688        if ( $compress_scripts && ! get_option('can_compress_scripts') )
     689            $compress_scripts = false;
     690    }
     691
     692    if ( ! isset($compress_css) ) {
     693        $compress_css = defined('COMPRESS_CSS') ? COMPRESS_CSS : true;
     694        if ( $compress_css && ! get_option('can_compress_scripts') )
     695            $compress_css = false;
     696    }
    653697}
    654698
Note: See TracChangeset for help on using the changeset viewer.