Skip to content

Commit 22eddf3

Browse files
committed
Split the script queue in head and footer part, concatenate and compress the default js and css, first run, see WordPress#8628
git-svn-id: https://develop.svn.wordpress.org/trunk@10357 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 1677a33 commit 22eddf3

10 files changed

Lines changed: 727 additions & 59 deletions

wp-admin/admin-ajax.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,18 @@
6363
echo join( $results, "\n" );
6464
die;
6565
break;
66+
case 'wp-compression-test' :
67+
if ( !current_user_can( 'manage_options' ) )
68+
die('-1');
69+
70+
if ( isset($_GET['tested']) ) {
71+
if ( 1 == $_GET['tested'] )
72+
update_option('can_compress_scripts', 1);
73+
elseif ( 0 == $_GET['tested'] )
74+
update_option('can_compress_scripts', 0);
75+
}
76+
die('0');
77+
break;
6678
default :
6779
do_action( 'wp_ajax_' . $_GET['action'] );
6880
die('0');

wp-admin/admin-footer.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,14 @@
2222
<p id="footer-upgrade" class="alignright"><?php echo $upgrade; ?></p>
2323
<div class="clear"></div>
2424
</div>
25-
<?php do_action('admin_footer', ''); ?>
25+
<?php
26+
do_action('admin_footer', '');
27+
do_action('admin_print_footer_scripts');
28+
29+
if ( false === get_option('can_compress_scripts') )
30+
compression_test();
31+
?>
2632
<script type="text/javascript">if(typeof wpOnload=='function')wpOnload();</script>
2733
</body>
28-
</html>
34+
</html>
35+

wp-admin/includes/template.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3387,4 +3387,37 @@ function screen_icon($name = '') {
33873387
<?php
33883388
}
33893389

3390+
/**
3391+
* Test support for compressed JavaScript and CSS
3392+
*
3393+
* Outputs JavaScript that tests if compression from PHP works as expected
3394+
* and sets an option with the result. Has no effect when the current user
3395+
* is not an administrator. To run the test again the option 'can_compress_scripts'
3396+
* has to be deleted.
3397+
*
3398+
* @since 2.8.0
3399+
*/
3400+
function compression_test() {
3401+
?>
3402+
<script type="text/javascript" src="load-scripts.php?test=1"></script>
3403+
<script type="text/javascript">
3404+
/* <![CDATA[ */
3405+
(function() {
3406+
var x, test = typeof wpCompressionTest == 'undefined' ? 0 : 1;
3407+
if ( window.XMLHttpRequest ) {
3408+
x = new XMLHttpRequest();
3409+
} else {
3410+
try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
3411+
}
3412+
3413+
if (x) {
3414+
x.open('GET', 'admin-ajax.php?action=wp-compression-test&tested='+test+'&'+(new Date()).getTime(), true);
3415+
x.send('');
3416+
}
3417+
})();
3418+
/* ]]> */
3419+
</script>
3420+
<?php
3421+
}
3422+
33903423
?>

wp-admin/load-scripts.php

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
/** Set ABSPATH for execution */
4+
define( 'ABSPATH', dirname(dirname(__FILE__)) );
5+
define( 'WPINC', '/wp-includes' );
6+
7+
/**
8+
* @ignore
9+
*/
10+
function __() {}
11+
12+
/**
13+
* @ignore
14+
*/
15+
function _c() {}
16+
17+
/**
18+
* @ignore
19+
*/
20+
function add_filter() {}
21+
22+
/**
23+
* @ignore
24+
*/
25+
function attribute_escape() {}
26+
27+
/**
28+
* @ignore
29+
*/
30+
function apply_filters() {}
31+
32+
/**
33+
* @ignore
34+
*/
35+
function get_option() {}
36+
37+
/**
38+
* @ignore
39+
*/
40+
function is_lighttpd_before_150() {}
41+
42+
/**
43+
* @ignore
44+
*/
45+
function add_action() {}
46+
47+
/**
48+
* @ignore
49+
*/
50+
function do_action_ref_array() {}
51+
52+
/**
53+
* @ignore
54+
*/
55+
function get_bloginfo() {}
56+
57+
/**
58+
* @ignore
59+
*/
60+
function is_admin() {return true;}
61+
62+
/**
63+
* @ignore
64+
*/
65+
function site_url() {}
66+
67+
/**
68+
* @ignore
69+
*/
70+
function admin_url() {}
71+
72+
/**
73+
* @ignore
74+
*/
75+
function wp_guess_url() {}
76+
77+
function get_file($path) {
78+
79+
if ( function_exists('realpath') )
80+
$path = realpath($path);
81+
82+
if ( ! $path || ! @is_file($path) )
83+
return '';
84+
85+
return @file_get_contents($path);
86+
}
87+
88+
if ( isset($_GET['test']) && 1 == $_GET['test'] ) {
89+
if ( ini_get('zlib.output_compression') )
90+
exit('');
91+
92+
$out = 'var wpCompressionTest = 1;';
93+
94+
$compressed = false;
95+
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') ) {
96+
header('Content-Encoding: deflate');
97+
$out = gzdeflate( $out, 3 );
98+
$compressed = true;
99+
} elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
100+
header('Content-Encoding: gzip');
101+
$out = gzencode( $out, 3 );
102+
$compressed = true;
103+
}
104+
105+
if ( ! $compressed )
106+
exit('');
107+
108+
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
109+
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
110+
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
111+
header( 'Pragma: no-cache' );
112+
header( 'Content-Type: application/x-javascript; charset=UTF-8' );
113+
echo $out;
114+
exit;
115+
}
116+
117+
$load = preg_replace( '/[^a-z0-9,_-]*/i', '', $_GET['load'] );
118+
$load = explode(',', $load);
119+
120+
if ( empty($load) )
121+
exit;
122+
123+
require(ABSPATH . '/wp-includes/script-loader.php');
124+
require(ABSPATH . '/wp-includes/version.php');
125+
126+
// Discard any buffers
127+
while ( @ob_end_clean() );
128+
129+
$compress = ( isset($_GET['c']) && 1 == $_GET['c'] );
130+
$expires_offset = 31536000;
131+
$out = '';
132+
133+
$wp_scripts = new WP_Scripts();
134+
wp_default_scripts($wp_scripts);
135+
136+
foreach( $load as $handle ) {
137+
if ( !array_key_exists($handle, $wp_scripts->registered) )
138+
continue;
139+
140+
$path = ABSPATH . $wp_scripts->registered[$handle]->src;
141+
$out .= get_file($path) . "\n";
142+
}
143+
144+
header('Content-Type: application/x-javascript; charset=UTF-8');
145+
header('Vary: Accept-Encoding'); // Handle proxies
146+
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
147+
header("Cache-Control: public, max-age=$expires_offset");
148+
149+
if ( $compress && ! ini_get('zlib.output_compression') ) {
150+
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') ) {
151+
header('Content-Encoding: deflate');
152+
$out = gzdeflate( $out, 3 );
153+
} elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
154+
header('Content-Encoding: gzip');
155+
$out = gzencode( $out, 3 );
156+
}
157+
}
158+
159+
echo $out;
160+
exit;

wp-admin/load-styles.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
/** Set ABSPATH for execution */
4+
define( 'ABSPATH', dirname(dirname(__FILE__)) );
5+
define( 'WPINC', '/wp-includes' );
6+
7+
/**
8+
* @ignore
9+
*/
10+
function __() {}
11+
12+
/**
13+
* @ignore
14+
*/
15+
function _c() {}
16+
17+
/**
18+
* @ignore
19+
*/
20+
function add_filter() {}
21+
22+
/**
23+
* @ignore
24+
*/
25+
function attribute_escape() {}
26+
27+
/**
28+
* @ignore
29+
*/
30+
function apply_filters() {}
31+
32+
/**
33+
* @ignore
34+
*/
35+
function get_option() {}
36+
37+
/**
38+
* @ignore
39+
*/
40+
function is_lighttpd_before_150() {}
41+
42+
/**
43+
* @ignore
44+
*/
45+
function add_action() {}
46+
47+
/**
48+
* @ignore
49+
*/
50+
function do_action_ref_array() {}
51+
52+
/**
53+
* @ignore
54+
*/
55+
function get_bloginfo() {}
56+
57+
/**
58+
* @ignore
59+
*/
60+
function is_admin() {return true;}
61+
62+
/**
63+
* @ignore
64+
*/
65+
function site_url() {}
66+
67+
/**
68+
* @ignore
69+
*/
70+
function admin_url() {}
71+
72+
/**
73+
* @ignore
74+
*/
75+
function wp_guess_url() {}
76+
77+
function get_file($path) {
78+
79+
if ( function_exists('realpath') )
80+
$path = realpath($path);
81+
82+
if ( ! $path || ! @is_file($path) )
83+
return '';
84+
85+
return @file_get_contents($path);
86+
}
87+
88+
require(ABSPATH . '/wp-includes/script-loader.php');
89+
require(ABSPATH . '/wp-includes/version.php');
90+
91+
// Discard any buffers
92+
while ( @ob_end_clean() );
93+
94+
$load = preg_replace( '/[^a-z0-9,_-]*/i', '', $_GET['load'] );
95+
$load = explode(',', $load);
96+
97+
if ( empty($load) )
98+
exit;
99+
100+
$compress = ( isset($_GET['c']) && 1 == $_GET['c'] );
101+
$rtl = ( isset($_GET['rtl']) && 1 == $_GET['rtl'] );
102+
$expires_offset = 31536000;
103+
$out = '';
104+
105+
$wp_styles = new WP_Styles();
106+
wp_default_styles($wp_styles);
107+
108+
foreach( $load as $handle ) {
109+
if ( !array_key_exists($handle, $wp_styles->registered) )
110+
continue;
111+
112+
$style = $wp_styles->registered[$handle];
113+
$path = ABSPATH . $style->src;
114+
115+
$content = get_file($path) . "\n";
116+
117+
if ( $rtl && isset($style->extra['rtl']) && $style->extra['rtl'] ) {
118+
$rtl_path = is_bool($style->extra['rtl']) ? str_replace( '.css', '-rtl.css', $path ) : ABSPATH . $style->extra['rtl'];
119+
$content .= get_file($rtl_path) . "\n";
120+
}
121+
122+
$out .= str_replace( '../images/', 'images/', $content );
123+
}
124+
125+
header('Content-Type: text/css');
126+
header('Vary: Accept-Encoding'); // Handle proxies
127+
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
128+
header("Cache-Control: public, max-age=$expires_offset");
129+
130+
if ( $compress && ! ini_get('zlib.output_compression') ) {
131+
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') ) {
132+
header('Content-Encoding: deflate');
133+
$out = gzdeflate( $out, 3 );
134+
} elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
135+
header('Content-Encoding: gzip');
136+
$out = gzencode( $out, 3 );
137+
}
138+
}
139+
140+
echo $out;
141+
exit;

0 commit comments

Comments
 (0)