From f20f62b27d4253b16ba744aa3cad8a69d08b29ae Mon Sep 17 00:00:00 2001 From: azaozz Date: Mon, 26 Jan 2009 12:59:10 +0000 Subject: [PATCH] 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 --- wp-admin/includes/template.php | 4 +- wp-admin/load-scripts.php | 27 ++++--- wp-admin/load-styles.php | 12 ++-- wp-includes/class.wp-scripts.php | 23 ++++-- wp-includes/class.wp-styles.php | 6 +- wp-includes/default-filters.php | 12 ++-- wp-includes/script-loader.php | 118 +++++++++++++++++++++---------- 7 files changed, 137 insertions(+), 65 deletions(-) diff --git a/wp-admin/includes/template.php b/wp-admin/includes/template.php index c9bcfb4420..7a9f2e2c3e 100644 --- a/wp-admin/includes/template.php +++ b/wp-admin/includes/template.php @@ -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() { ?> - + \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 "\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(); - + 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 "\n"; } @@ -639,17 +677,23 @@ function wp_print_admin_styles() { function script_concat_settings() { global $concatenate_scripts, $compress_scripts, $compress_css; - $concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true; - if ( $concatenate_scripts && -1 == get_option('concatenate_scripts') ) - $concatenate_scripts = false; + if ( ! isset($concatenate_scripts) ) { + $concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true; + if ( ! is_admin() || ( $concatenate_scripts && -1 == get_user_option('concatenate_scripts') ) ) + $concatenate_scripts = false; + } - $compress_scripts = defined('COMPRESS_SCRIPTS') ? COMPRESS_SCRIPTS : true; - if ( $compress_scripts && ! get_option('can_compress_scripts') ) - $compress_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; + } - $compress_css = defined('COMPRESS_CSS') ? COMPRESS_CSS : true; - if ( $compress_css && ! get_option('can_compress_scripts') ) - $compress_css = 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' );