Skip to content

Commit 13d3219

Browse files
committed
Detect when the server compresses output, see WordPress#8628
git-svn-id: https://develop.svn.wordpress.org/trunk@10476 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8ec672d commit 13d3219

8 files changed

Lines changed: 106 additions & 76 deletions

File tree

wp-admin/admin-ajax.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
die('-1');
4646

4747
$s = $_GET['q']; // is this slashed already?
48-
48+
4949
if ( isset($_GET['tax']) )
5050
$taxonomy = sanitize_title($_GET['tax']);
5151
else
@@ -67,13 +67,43 @@
6767
case 'wp-compression-test' :
6868
if ( !current_user_can( 'manage_options' ) )
6969
die('-1');
70-
71-
if ( isset($_GET['tested']) ) {
72-
if ( 1 == $_GET['tested'] )
73-
update_option('can_compress_scripts', 1);
74-
elseif ( 0 == $_GET['tested'] )
70+
71+
if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ) {
72+
update_option('can_compress_scripts', 0);
73+
die('0');
74+
}
75+
76+
if ( isset($_GET['test']) ) {
77+
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
78+
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
79+
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
80+
header( 'Pragma: no-cache' );
81+
header('Content-Type: application/x-javascript; charset=UTF-8');
82+
$force_gzip = ( defined('ENFORCE_GZIP') && ENFORCE_GZIP );
83+
$test_str = '"wpCompressionTest Lorem ipsum dolor sit amet consectetuer mollis sapien urna ut a. Eu nonummy condimentum fringilla tempor pretium platea vel nibh netus Maecenas. Hac molestie amet justo quis pellentesque est ultrices interdum nibh Morbi. Cras mattis pretium Phasellus ante ipsum ipsum ut sociis Suspendisse Lorem. Ante et non molestie. Porta urna Vestibulum egestas id congue nibh eu risus gravida sit. Ac augue auctor Ut et non a elit massa id sodales. Elit eu Nulla at nibh adipiscing mattis lacus mauris at tempus. Netus nibh quis suscipit nec feugiat eget sed lorem et urna. Pellentesque lacus at ut massa consectetuer ligula ut auctor semper Pellentesque. Ut metus massa nibh quam Curabitur molestie nec mauris congue. Volutpat molestie elit justo facilisis neque ac risus Ut nascetur tristique. Vitae sit lorem tellus et quis Phasellus lacus tincidunt nunc Fusce. Pharetra wisi Suspendisse mus sagittis libero lacinia Integer consequat ac Phasellus. Et urna ac cursus tortor aliquam Aliquam amet tellus volutpat Vestibulum. Justo interdum condimentum In augue congue tellus sollicitudin Quisque quis nibh."';
84+
85+
if ( 1 == $_GET['test'] ) {
86+
echo $test_str;
87+
die;
88+
} elseif ( 2 == $_GET['test'] ) {
89+
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
90+
header('Content-Encoding: deflate');
91+
$out = gzdeflate( $test_str, 1 );
92+
} elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
93+
header('Content-Encoding: gzip');
94+
$out = gzencode( $test_str, 1 );
95+
} else {
96+
die('-1');
97+
}
98+
echo $out;
99+
die;
100+
} elseif ( 'no' == $_GET['test'] ) {
75101
update_option('can_compress_scripts', 0);
102+
} elseif ( 'yes' == $_GET['test'] ) {
103+
update_option('can_compress_scripts', 1);
104+
}
76105
}
106+
77107
die('0');
78108
break;
79109
default :
@@ -514,7 +544,7 @@ function _wp_ajax_delete_comment_response( $comment_id ) {
514544
$taxonomy = sanitize_title($_POST['tax']);
515545
else
516546
die('0');
517-
547+
518548
$tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
519549

520550
if ( empty( $tags ) )

wp-admin/includes/schema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ function populate_options() {
303303
add_option('dismissed_update_core', array());
304304

305305
// Delete unused options
306-
$unusedoptions = array ('blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory', 'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping', 'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers', 'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference', 'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char', 'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1', 'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5', 'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9', 'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat', 'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog', '_wpnonce', '_wp_http_referer', 'Update', 'action', 'rich_editing', 'autosave_interval', 'deactivated_plugins');
306+
$unusedoptions = array ('blodotgsping_url', 'bodyterminator', 'emailtestonly', 'phoneemail_separator', 'smilies_directory', 'subjectprefix', 'use_bbcode', 'use_blodotgsping', 'use_phoneemail', 'use_quicktags', 'use_weblogsping', 'weblogs_cache_file', 'use_preview', 'use_htmltrans', 'smilies_directory', 'fileupload_allowedusers', 'use_phoneemail', 'default_post_status', 'default_post_category', 'archive_mode', 'time_difference', 'links_minadminlevel', 'links_use_adminlevels', 'links_rating_type', 'links_rating_char', 'links_rating_ignore_zero', 'links_rating_single_image', 'links_rating_image0', 'links_rating_image1', 'links_rating_image2', 'links_rating_image3', 'links_rating_image4', 'links_rating_image5', 'links_rating_image6', 'links_rating_image7', 'links_rating_image8', 'links_rating_image9', 'weblogs_cacheminutes', 'comment_allowed_tags', 'search_engine_friendly_urls', 'default_geourl_lat', 'default_geourl_lon', 'use_default_geourl', 'weblogs_xml_url', 'new_users_can_blog', '_wpnonce', '_wp_http_referer', 'Update', 'action', 'rich_editing', 'autosave_interval', 'deactivated_plugins', 'can_compress_scripts');
307307
foreach ($unusedoptions as $option) :
308308
delete_option($option);
309309
endforeach;

wp-admin/includes/template.php

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1816,17 +1816,17 @@ function user_row( $user_object, $style = '', $role = '' ) {
18161816
$short_url = substr( $short_url, 0, 32 ).'...';
18171817
$numposts = get_usernumposts( $user_object->ID );
18181818
$checkbox = '';
1819-
// Check if the user for this row is editable
1819+
// Check if the user for this row is editable
18201820
if ( current_user_can( 'edit_user', $user_object->ID ) ) {
1821-
// Set up the user editing link
1821+
// Set up the user editing link
18221822
// TODO: make profile/user-edit determination a seperate function
18231823
if ($current_user->ID == $user_object->ID) {
18241824
$edit_link = 'profile.php';
18251825
} else {
18261826
$edit_link = clean_url( add_query_arg( 'wp_http_referer', urlencode( clean_url( stripslashes( $_SERVER['REQUEST_URI'] ) ) ), "user-edit.php?user_id=$user_object->ID" ) );
18271827
}
18281828
$edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
1829-
1829+
18301830
// Set up the hover actions for this user
18311831
$actions = array();
18321832
$actions['edit'] = '<a href="' . $edit_link . '">' . __('Edit') . '</a>';
@@ -1841,10 +1841,10 @@ function user_row( $user_object, $style = '', $role = '' ) {
18411841
$edit .= "<span class='$action'>$link$sep</span>";
18421842
}
18431843
$edit .= '</div>';
1844-
1844+
18451845
// Set up the checkbox (because the user is editable, otherwise its empty)
18461846
$checkbox = "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
1847-
1847+
18481848
} else {
18491849
$edit = '<strong>' . $user_object->user_login . '</strong>';
18501850
}
@@ -1922,7 +1922,7 @@ function _wp_get_comment_list( $status = '', $s = false, $start, $num, $post = 0
19221922
$post = (int) $post;
19231923
$count = wp_count_comments();
19241924
$index = '';
1925-
1925+
19261926
if ( 'moderated' == $status ) {
19271927
$approved = "comment_approved = '0'";
19281928
$total = $count->moderated;
@@ -2615,7 +2615,7 @@ function the_attachment_links( $id = false ) {
26152615
<?php
26162616
}
26172617

2618-
2618+
26192619
/**
26202620
* Print out <option> html elements for role selectors based on $wp_roles
26212621
*
@@ -2630,9 +2630,9 @@ function wp_dropdown_roles( $selected = false ) {
26302630
global $wp_roles;
26312631
$p = '';
26322632
$r = '';
2633-
2633+
26342634
$editable_roles = get_editable_roles();
2635-
2635+
26362636
foreach( $editable_roles as $role => $details ) {
26372637
$name = translate_with_context($details['name']);
26382638
if ( $selected == $role ) // Make default first in list
@@ -3402,28 +3402,60 @@ function screen_icon($name = '') {
34023402
* Outputs JavaScript that tests if compression from PHP works as expected
34033403
* and sets an option with the result. Has no effect when the current user
34043404
* is not an administrator. To run the test again the option 'can_compress_scripts'
3405-
* has to be deleted.
3405+
* has to be deleted.
34063406
*
34073407
* @since 2.8.0
34083408
*/
34093409
function compression_test() {
34103410
?>
3411-
<script type="text/javascript" src="load-scripts.php?test=1<?php echo ( defined('ENFORCE_GZIP') && ENFORCE_GZIP ) ? '&c=gzip' : ''; ?>"></script>
34123411
<script type="text/javascript">
34133412
/* <![CDATA[ */
3414-
(function() {
3415-
var x, test = typeof wpCompressionTest == 'undefined' ? 0 : 1;
3416-
if ( window.XMLHttpRequest ) {
3417-
x = new XMLHttpRequest();
3418-
} else {
3419-
try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
3420-
}
3421-
3422-
if (x) {
3423-
x.open('GET', 'admin-ajax.php?action=wp-compression-test&tested='+test+'&'+(new Date()).getTime(), true);
3424-
x.send('');
3413+
var testCompression = {
3414+
get : function(test) {
3415+
var x;
3416+
if ( window.XMLHttpRequest ) {
3417+
x = new XMLHttpRequest();
3418+
} else {
3419+
try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
3420+
}
3421+
3422+
if (x) {
3423+
x.onreadystatechange = function() {
3424+
var r, h;
3425+
if ( x.readyState == 4 ) {
3426+
r = x.responseText.substr(0, 18);
3427+
h = x.getResponseHeader('Content-Encoding');
3428+
testCompression.check(r, h, test);
3429+
}
3430+
}
3431+
3432+
x.open('GET', 'admin-ajax.php?action=wp-compression-test&test='+test+'&'+(new Date()).getTime(), true);
3433+
x.send('');
3434+
}
3435+
},
3436+
3437+
check : function(r, h, test) {
3438+
if ( ! r && ! test )
3439+
this.get(1);
3440+
3441+
if ( 1 == test ) {
3442+
if ( h && ( h.match(/deflate/i) || h.match(/gzip/i) ) )
3443+
this.get('no');
3444+
else
3445+
this.get(2);
3446+
3447+
return;
3448+
}
3449+
3450+
if ( 2 == test ) {
3451+
if ( '"wpCompressionTest' == r )
3452+
this.get('yes');
3453+
else
3454+
this.get('no');
3455+
}
34253456
}
3426-
})();
3457+
};
3458+
testCompression.check();
34273459
/* ]]> */
34283460
</script>
34293461
<?php

wp-admin/load-scripts.php

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,36 +85,7 @@ function get_file($path) {
8585
return @file_get_contents($path);
8686
}
8787

88-
// Discard any buffers
89-
while ( @ob_end_clean() );
90-
91-
if ( isset($_GET['test']) && 1 == $_GET['test'] ) {
92-
if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') )
93-
exit('');
94-
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') ) {
102-
header('Content-Encoding: gzip');
103-
$out = gzencode( $out, 3 );
104-
} else {
105-
exit('');
106-
}
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'] );
88+
$load = preg_replace( '/[^a-z0-9,_-]+/i', '', $_GET['load'] );
11889
$load = explode(',', $load);
11990

12091
if ( empty($load) )
@@ -142,7 +113,7 @@ function get_file($path) {
142113
header('Content-Type: application/x-javascript; charset=UTF-8');
143114
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
144115
header("Cache-Control: public, max-age=$expires_offset");
145-
116+
146117
if ( $compress && ! ini_get('zlib.output_compression') && 'ob_gzhandler' != ini_get('output_handler') ) {
147118
header('Vary: Accept-Encoding'); // Handle proxies
148119
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {

wp-admin/load-styles.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,7 @@ function get_file($path) {
8888
require(ABSPATH . '/wp-includes/script-loader.php');
8989
require(ABSPATH . '/wp-includes/version.php');
9090

91-
// Discard any buffers
92-
while ( @ob_end_clean() );
93-
94-
$load = preg_replace( '/[^a-z0-9,_-]*/i', '', $_GET['load'] );
91+
$load = preg_replace( '/[^a-z0-9,_-]+/i', '', $_GET['load'] );
9592
$load = explode(',', $load);
9693

9794
if ( empty($load) )
@@ -126,7 +123,7 @@ function get_file($path) {
126123
header('Content-Type: text/css');
127124
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
128125
header("Cache-Control: public, max-age=$expires_offset");
129-
126+
130127
if ( $compress && ! ini_get('zlib.output_compression') && 'ob_gzhandler' != ini_get('output_handler') ) {
131128
header('Vary: Accept-Encoding'); // Handle proxies
132129
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {

wp-includes/js/tinymce/wp-tinymce.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
$basepath = dirname(__FILE__);
44

5-
while ( @ob_end_clean() );
6-
75
function get_file($path) {
86

97
if ( function_exists('realpath') )
@@ -22,7 +20,7 @@ function get_file($path) {
2220
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
2321
header("Cache-Control: public, max-age=$expires_offset");
2422

25-
if ( isset($_GET['c']) && 1 == $_GET['c'] && ! ini_get('zlib.output_compression') && false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') ) {
23+
if ( isset($_GET['c']) && 1 == $_GET['c'] && false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') ) {
2624
header('Content-Encoding: gzip');
2725
echo get_file($basepath . '/wp-tinymce.js.gz');
2826
} else {

wp-includes/script-loader.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,10 +616,10 @@ function wp_print_head_scripts() {
616616
do_action('wp_print_scripts');
617617

618618
global $wp_scripts;
619-
619+
620620
if ( !is_a($wp_scripts, 'WP_Scripts') )
621621
return array(); // no need to run if nothing is queued
622-
622+
623623
return print_head_scripts();
624624
}
625625

@@ -634,7 +634,7 @@ function wp_print_footer_scripts() {
634634

635635
/**
636636
* Wrapper for do_action('wp_enqueue_scripts')
637-
*
637+
*
638638
* Allows plugins to queue scripts for the front end using wp_enqueue_script().
639639
* Runs first in wp_head() where all is_home(), is_page(), etc. functions are available.
640640
*
@@ -678,6 +678,8 @@ function print_admin_styles() {
678678
function script_concat_settings() {
679679
global $concatenate_scripts, $compress_scripts, $compress_css;
680680

681+
$compressed_output = ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') );
682+
681683
if ( ! isset($concatenate_scripts) ) {
682684
$concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true;
683685
if ( ! is_admin() || ( $concatenate_scripts && -1 == get_user_option('concatenate_scripts') ) )
@@ -686,13 +688,13 @@ function script_concat_settings() {
686688

687689
if ( ! isset($compress_scripts) ) {
688690
$compress_scripts = defined('COMPRESS_SCRIPTS') ? COMPRESS_SCRIPTS : true;
689-
if ( $compress_scripts && ! get_option('can_compress_scripts') )
691+
if ( $compress_scripts && ( ! get_option('can_compress_scripts') || $compressed_output ) )
690692
$compress_scripts = false;
691693
}
692694

693695
if ( ! isset($compress_css) ) {
694696
$compress_css = defined('COMPRESS_CSS') ? COMPRESS_CSS : true;
695-
if ( $compress_css && ! get_option('can_compress_scripts') )
697+
if ( $compress_css && ( ! get_option('can_compress_scripts') || $compressed_output ) )
696698
$compress_css = false;
697699
}
698700
}

wp-includes/version.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515
*
1616
* @global int $wp_db_version
1717
*/
18-
$wp_db_version = 9872;
18+
$wp_db_version = 9873;
1919

2020
?>

0 commit comments

Comments
 (0)