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

git-svn-id: http://svn.automattic.com/wordpress/trunk@10442 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2009-01-26 12:59:10 +00:00
parent ccfc848be2
commit f20f62b27d
7 changed files with 137 additions and 65 deletions

View File

@ -3395,7 +3395,7 @@ function screen_icon($name = '') {
}
/**
* Test support for compressed JavaScript and CSS
* Test support for compressing JavaScript from PHP
*
* Outputs JavaScript that tests if compression from PHP works as expected
* and sets an option with the result. Has no effect when the current user
@ -3406,7 +3406,7 @@ function screen_icon($name = '') {
*/
function compression_test() {
?>
<script type="text/javascript" src="load-scripts.php?test=1"></script>
<script type="text/javascript" src="load-scripts.php?test=1<?php echo ( defined('ENFORCE_GZIP') && ENFORCE_GZIP ) ? '&c=gzip' : ''; ?>"></script>
<script type="text/javascript">
/* <![CDATA[ */
(function() {

View File

@ -92,13 +92,18 @@ if ( isset($_GET['test']) && 1 == $_GET['test'] ) {
if ( ini_get('zlib.output_compression') )
exit('');
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
header('Content-Encoding: gzip');
$out = gzencode( 'var wpCompressionTest = 1;', 3 );
}
$out = 'var wpCompressionTest = 1;';
$force_gzip = ( isset($_GET['c']) && 'gzip' == $_GET['c'] );
if ( ! isset($out) )
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
header('Content-Encoding: deflate');
$out = gzdeflate( $out, 3 );
} elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
header('Content-Encoding: gzip');
$out = gzencode( $out, 3 );
} else {
exit('');
}
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
@ -118,7 +123,8 @@ if ( empty($load) )
require(ABSPATH . '/wp-includes/script-loader.php');
require(ABSPATH . '/wp-includes/version.php');
$compress = ( isset($_GET['c']) && 1 == $_GET['c'] );
$compress = ( isset($_GET['c']) && $_GET['c'] );
$force_gzip = ( $compress && 'gzip' == $_GET['c'] );
$expires_offset = 31536000;
$out = '';
@ -137,9 +143,12 @@ header('Content-Type: application/x-javascript; charset=UTF-8');
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
header("Cache-Control: public, max-age=$expires_offset");
if ( $compress && ! ini_get('zlib.output_compression') && function_exists('gzencode') ) {
if ( $compress && ! ini_get('zlib.output_compression') ) {
header('Vary: Accept-Encoding'); // Handle proxies
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') ) {
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
header('Content-Encoding: deflate');
$out = gzdeflate( $out, 3 );
} elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
header('Content-Encoding: gzip');
$out = gzencode( $out, 3 );
}

View File

@ -97,8 +97,9 @@ $load = explode(',', $load);
if ( empty($load) )
exit;
$compress = ( isset($_GET['c']) && 1 == $_GET['c'] );
$rtl = ( isset($_GET['rtl']) && 1 == $_GET['rtl'] );
$compress = ( isset($_GET['c']) && $_GET['c'] );
$force_gzip = ( $compress && 'gzip' == $_GET['c'] );
$rtl = ( isset($_GET['dir']) && 'rtl' == $_GET['dir'] );
$expires_offset = 31536000;
$out = '';
@ -126,9 +127,12 @@ header('Content-Type: text/css');
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
header("Cache-Control: public, max-age=$expires_offset");
if ( $compress && ! ini_get('zlib.output_compression') && function_exists('gzencode') ) {
if ( $compress && ! ini_get('zlib.output_compression') ) {
header('Vary: Accept-Encoding'); // Handle proxies
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') ) {
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
header('Content-Encoding: deflate');
$out = gzdeflate( $out, 3 );
} elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
header('Content-Encoding: gzip');
$out = gzencode( $out, 3 );
}

View File

@ -26,7 +26,8 @@ class WP_Scripts extends WP_Dependencies {
var $do_concat = false;
var $print_html = '';
var $print_code = '';
var $src = '';
var $ext_handles = '';
var $ext_version = '';
var $default_dirs;
function __construct() {
@ -99,9 +100,12 @@ class WP_Scripts extends WP_Dependencies {
$srce = apply_filters( 'script_loader_src', $src, $handle );
if ( $this->in_default_dir($srce) ) {
$this->print_code .= $this->print_scripts_l10n( $handle, false );
$this->concat .= $handle . ',';
$this->concat_version .= $ver;
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
return true;
} else {
$this->ext_handles .= "$handle,";
$this->ext_version .= "$handle$ver";
}
}
@ -110,7 +114,6 @@ class WP_Scripts extends WP_Dependencies {
$src = $this->base_url . $src;
}
$this->src .= "$src,";
$src = add_query_arg('ver', $ver, $src);
$src = clean_url(apply_filters( 'script_loader_src', $src, $handle ));
@ -146,7 +149,7 @@ class WP_Scripts extends WP_Dependencies {
parent::set_group( $handle, $recursion, $grp );
}
function all_deps( $handles, $recursion = false ) {
function all_deps( $handles, $recursion = false, $group = false ) {
$r = parent::all_deps( $handles, $recursion );
if ( !$recursion )
$this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
@ -181,4 +184,14 @@ class WP_Scripts extends WP_Dependencies {
}
return false;
}
function reset() {
$this->do_concat = false;
$this->print_code = '';
$this->concat = '';
$this->concat_version = '';
$this->print_html = '';
$this->ext_version = '';
$this->ext_handles = '';
}
}

View File

@ -41,8 +41,8 @@ class WP_Styles extends WP_Dependencies {
if ( $this->do_concat ) {
if ( $this->in_default_dir($this->registered[$handle]->src) && !isset($this->registered[$handle]->extra['conditional']) && !isset($this->registered[$handle]->extra['alt']) ) {
$this->concat .= $handle . ',';
$this->concat_version .= $ver;
$this->concat .= "$handle,";
$this->concat_version .= "$handle$ver";
return true;
}
}
@ -88,7 +88,7 @@ class WP_Styles extends WP_Dependencies {
return true;
}
function all_deps( $handles, $recursion = false ) {
function all_deps( $handles, $recursion = false, $group = false ) {
$r = parent::all_deps( $handles, $recursion );
if ( !$recursion )
$this->to_do = apply_filters( 'print_styles_array', $this->to_do );

View File

@ -164,15 +164,17 @@ add_filter( 'editable_slug', 'urldecode' );
add_filter('atom_service_url','atom_service_url_filter');
// Actions
add_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_head', 'feed_links_extra', 3);
add_action('wp_head', 'rsd_link');
add_action('wp_head', 'wlwmanifest_link');
add_action('wp_head', 'locale_stylesheet');
add_action('publish_future_post', 'check_and_publish_future_post', 10, 1);
add_action('wp_head', 'noindex', 1);
add_action('wp_head', 'wp_print_styles', 9);
add_action('wp_head', 'wp_print_scripts');
add_action('wp_head', 'wp_print_styles', 8);
add_action('wp_head', 'wp_print_head_scripts', 9);
add_action('wp_head', 'wp_generator');
add_action('wp_footer', 'wp_print_footer_scripts');
if(!defined('DOING_CRON'))
add_action('init', 'wp_cron');
add_action('do_feed_rdf', 'do_feed_rdf', 10, 1);
@ -182,9 +184,9 @@ add_action('do_feed_atom', 'do_feed_atom', 10, 1);
add_action('do_pings', 'do_all_pings', 10, 1);
add_action('do_robots', 'do_robots');
add_action('sanitize_comment_cookies', 'sanitize_comment_cookies');
add_action('admin_print_scripts', 'wp_print_head_scripts', 20);
add_action('admin_print_footer_scripts', 'wp_print_footer_scripts', 20);
add_action('admin_print_styles', 'wp_print_admin_styles', 20);
add_action('admin_print_scripts', 'print_head_scripts', 20);
add_action('admin_print_footer_scripts', 'print_footer_scripts', 20);
add_action('admin_print_styles', 'print_admin_styles', 20);
add_action('init', 'smilies_init', 5);
add_action( 'plugins_loaded', 'wp_maybe_load_widgets', 0 );
add_action( 'shutdown', 'wp_ob_end_flush_all', 1);

View File

@ -526,57 +526,53 @@ function wp_style_loader_src( $src, $handle ) {
}
/**
* Print the script queue in the HTML head.
* Prints the script queue in the HTML head on admin pages.
*
* Postpones the scripts that were queued for the footer.
* wp_print_footer_scripts() has to be called in the footer to print these scripts.
* print_footer_scripts() is called in the footer to print these scripts.
*
* @since unknown
* @since 2.8
* @see wp_print_scripts()
*/
function wp_print_head_scripts() {
do_action( 'wp_print_scripts' );
function print_head_scripts() {
if ( ! did_action('wp_print_scripts') )
do_action('wp_print_scripts');
global $wp_scripts, $concatenate_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
$wp_scripts = new WP_Scripts();
if ( ! isset($concatenate_scripts) )
script_concat_settings();
$wp_scripts->do_concat = $concatenate_scripts;
$wp_scripts->do_head_items();
if ( apply_filters('print_head_scripts', true) )
_pring_scripts();
$wp_scripts->do_concat = false;
$wp_scripts->print_code = $wp_scripts->concat = $wp_scripts->concat_version = $wp_scripts->print_html = $wp_scripts->src = '';
$wp_scripts->reset();
return $wp_scripts->done;
}
/**
* Print the scripts that were queued for the footer.
* Prints the scripts that were queued for the footer on admin pages.
*
* @since unknown
* @since 2.8
*/
function wp_print_footer_scripts() {
function print_footer_scripts() {
global $wp_scripts, $concatenate_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
return array(); // No need to run if not instantiated.
if ( ! isset($concatenate_scripts) )
script_concat_settings();
$wp_scripts->do_concat = $concatenate_scripts;
$wp_scripts->do_footer_items();
if ( apply_filters('print_footer_scripts', true) )
_pring_scripts();
$wp_scripts->do_concat = false;
$wp_scripts->concat = $wp_scripts->concat_version = $wp_scripts->print_code = $wp_scripts->print_html = $wp_scripts->src = '';
$wp_scripts->reset();
return $wp_scripts->done;
}
@ -584,6 +580,8 @@ function _pring_scripts() {
global $wp_scripts, $compress_scripts;
$zip = $compress_scripts ? 1 : 0;
if ( $zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP )
$zip = 'gzip';
if ( !empty($wp_scripts->concat) ) {
@ -595,35 +593,75 @@ function _pring_scripts() {
echo "</script>\n";
}
$ver = md5("$wp_scripts->concat" . "$wp_scripts->concat_version");
$src = $wp_scripts->base_url . "/wp-admin/load-scripts.php?c={$zip}&load=" . rtrim($wp_scripts->concat, ',') . "&ver=$ver";
$ver = md5("$wp_scripts->concat_version");
$src = $wp_scripts->base_url . "/wp-admin/load-scripts.php?c={$zip}&load=" . trim($wp_scripts->concat, ', ') . "&ver=$ver";
echo "<script type='text/javascript' src='$src'></script>\n";
}
if ( !empty($wp_scripts->print_html) )
echo $wp_scripts->print_html;
}
function wp_print_admin_styles() {
/**
* Prints the script queue in the HTML head on the front end.
*
* Postpones the scripts that were queued for the footer.
* wp_print_footer_scripts() is called in the footer to print these scripts.
*
* @since 2.8
*/
function wp_print_head_scripts() {
if ( ! did_action('wp_print_scripts') )
do_action('wp_print_scripts');
global $wp_scripts;
if ( !is_a($wp_scripts, 'WP_Scripts') )
return array(); // no need to run if nothing is queued
return print_head_scripts();
}
/**
* Prints the scripts that were queued for the footer on the front end.
*
* @since 2.8
*/
function wp_print_footer_scripts() {
return print_footer_scripts();
}
/**
* Wrapper for do_action('wp_enqueue_scripts')
*
* Allows plugins to queue scripts for the front end using wp_enqueue_script().
* Runs first in wp_head() where all is_home(), is_page(), etc. functions are available.
*
* @since 2.8
*/
function wp_enqueue_scripts() {
do_action('wp_enqueue_scripts');
}
function print_admin_styles() {
global $wp_styles, $concatenate_scripts, $compress_css;
if ( !is_a($wp_styles, 'WP_Styles') )
$wp_styles = new WP_Styles();
if ( ! isset($concatenate_scripts) )
script_concat_settings();
$wp_styles->do_concat = $concatenate_scripts;
$zip = $compress_css ? 1 : 0;
if ( $zip && defined('ENFORCE_GZIP') && ENFORCE_GZIP )
$zip = 'gzip';
$wp_styles->do_items(false);
if ( apply_filters('print_admin_styles', true) ) {
if ( !empty($wp_styles->concat) ) {
$ver = md5("$wp_styles->concat" . "$wp_styles->concat_version");
$rtl = 'rtl' === $wp_styles->text_direction ? 1 : 0;
$href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&rtl={$rtl}&load=" . rtrim($wp_styles->concat, ',') . "&ver=$ver";
$dir = $wp_styles->text_direction;
$ver = md5("$wp_styles->concat_version{$dir}");
$href = $wp_styles->base_url . "/wp-admin/load-styles.php?c={$zip}&dir={$dir}&load=" . trim($wp_styles->concat, ', ') . "&ver=$ver";
echo "<link rel='stylesheet' href='$href' type='text/css' media='all' />\n";
}
@ -639,17 +677,23 @@ function wp_print_admin_styles() {
function script_concat_settings() {
global $concatenate_scripts, $compress_scripts, $compress_css;
if ( ! isset($concatenate_scripts) ) {
$concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true;
if ( $concatenate_scripts && -1 == get_option('concatenate_scripts') )
if ( ! is_admin() || ( $concatenate_scripts && -1 == get_user_option('concatenate_scripts') ) )
$concatenate_scripts = false;
}
if ( ! isset($compress_scripts) ) {
$compress_scripts = defined('COMPRESS_SCRIPTS') ? COMPRESS_SCRIPTS : true;
if ( $compress_scripts && ! get_option('can_compress_scripts') )
$compress_scripts = false;
}
if ( ! isset($compress_css) ) {
$compress_css = defined('COMPRESS_CSS') ? COMPRESS_CSS : true;
if ( $compress_css && ! get_option('can_compress_scripts') )
$compress_css = false;
}
}
add_action( 'wp_default_scripts', 'wp_default_scripts' );