Detect when the server compresses output, see #8628

git-svn-id: http://svn.automattic.com/wordpress/trunk@10476 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
azaozz 2009-02-01 09:45:24 +00:00
parent fc8345977f
commit 75850602a5
8 changed files with 107 additions and 77 deletions

View File

@ -45,7 +45,7 @@ case 'ajax-tag-search' :
die('-1');
$s = $_GET['q']; // is this slashed already?
if ( isset($_GET['tax']) )
$taxonomy = sanitize_title($_GET['tax']);
else
@ -67,13 +67,43 @@ case 'ajax-tag-search' :
case 'wp-compression-test' :
if ( !current_user_can( 'manage_options' ) )
die('-1');
if ( isset($_GET['tested']) ) {
if ( 1 == $_GET['tested'] )
update_option('can_compress_scripts', 1);
elseif ( 0 == $_GET['tested'] )
update_option('can_compress_scripts', 0);
if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') ) {
update_option('can_compress_scripts', 0);
die('0');
}
if ( isset($_GET['test']) ) {
header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
header( 'Pragma: no-cache' );
header('Content-Type: application/x-javascript; charset=UTF-8');
$force_gzip = ( defined('ENFORCE_GZIP') && ENFORCE_GZIP );
$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."';
if ( 1 == $_GET['test'] ) {
echo $test_str;
die;
} elseif ( 2 == $_GET['test'] ) {
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {
header('Content-Encoding: deflate');
$out = gzdeflate( $test_str, 1 );
} elseif ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') && function_exists('gzencode') ) {
header('Content-Encoding: gzip');
$out = gzencode( $test_str, 1 );
} else {
die('-1');
}
echo $out;
die;
} elseif ( 'no' == $_GET['test'] ) {
update_option('can_compress_scripts', 0);
} elseif ( 'yes' == $_GET['test'] ) {
update_option('can_compress_scripts', 1);
}
}
die('0');
break;
default :
@ -514,7 +544,7 @@ case 'get-tagcloud' :
$taxonomy = sanitize_title($_POST['tax']);
else
die('0');
$tags = get_terms( $taxonomy, array( 'number' => 45, 'orderby' => 'count', 'order' => 'DESC' ) );
if ( empty( $tags ) )

View File

@ -303,7 +303,7 @@ function populate_options() {
add_option('dismissed_update_core', array());
// Delete unused options
$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');
$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');
foreach ($unusedoptions as $option) :
delete_option($option);
endforeach;

View File

@ -1816,9 +1816,9 @@ function user_row( $user_object, $style = '', $role = '' ) {
$short_url = substr( $short_url, 0, 32 ).'...';
$numposts = get_usernumposts( $user_object->ID );
$checkbox = '';
// Check if the user for this row is editable
// Check if the user for this row is editable
if ( current_user_can( 'edit_user', $user_object->ID ) ) {
// Set up the user editing link
// Set up the user editing link
// TODO: make profile/user-edit determination a seperate function
if ($current_user->ID == $user_object->ID) {
$edit_link = 'profile.php';
@ -1826,7 +1826,7 @@ function user_row( $user_object, $style = '', $role = '' ) {
$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" ) );
}
$edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
// Set up the hover actions for this user
$actions = array();
$actions['edit'] = '<a href="' . $edit_link . '">' . __('Edit') . '</a>';
@ -1841,10 +1841,10 @@ function user_row( $user_object, $style = '', $role = '' ) {
$edit .= "<span class='$action'>$link$sep</span>";
}
$edit .= '</div>';
// Set up the checkbox (because the user is editable, otherwise its empty)
$checkbox = "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
} else {
$edit = '<strong>' . $user_object->user_login . '</strong>';
}
@ -1922,7 +1922,7 @@ function _wp_get_comment_list( $status = '', $s = false, $start, $num, $post = 0
$post = (int) $post;
$count = wp_count_comments();
$index = '';
if ( 'moderated' == $status ) {
$approved = "comment_approved = '0'";
$total = $count->moderated;
@ -2615,7 +2615,7 @@ function the_attachment_links( $id = false ) {
<?php
}
/**
* Print out <option> html elements for role selectors based on $wp_roles
*
@ -2630,9 +2630,9 @@ function wp_dropdown_roles( $selected = false ) {
global $wp_roles;
$p = '';
$r = '';
$editable_roles = get_editable_roles();
foreach( $editable_roles as $role => $details ) {
$name = translate_with_context($details['name']);
if ( $selected == $role ) // Make default first in list
@ -3402,28 +3402,60 @@ function screen_icon($name = '') {
* 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
* is not an administrator. To run the test again the option 'can_compress_scripts'
* has to be deleted.
* has to be deleted.
*
* @since 2.8.0
*/
function compression_test() {
?>
<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() {
var x, test = typeof wpCompressionTest == 'undefined' ? 0 : 1;
if ( window.XMLHttpRequest ) {
x = new XMLHttpRequest();
} else {
try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
var testCompression = {
get : function(test) {
var x;
if ( window.XMLHttpRequest ) {
x = new XMLHttpRequest();
} else {
try{x=new ActiveXObject('Msxml2.XMLHTTP');}catch(e){try{x=new ActiveXObject('Microsoft.XMLHTTP');}catch(e){};}
}
if (x) {
x.onreadystatechange = function() {
var r, h;
if ( x.readyState == 4 ) {
r = x.responseText.substr(0, 18);
h = x.getResponseHeader('Content-Encoding');
testCompression.check(r, h, test);
}
}
x.open('GET', 'admin-ajax.php?action=wp-compression-test&test='+test+'&'+(new Date()).getTime(), true);
x.send('');
}
},
check : function(r, h, test) {
if ( ! r && ! test )
this.get(1);
if ( 1 == test ) {
if ( h && ( h.match(/deflate/i) || h.match(/gzip/i) ) )
this.get('no');
else
this.get(2);
return;
}
if ( 2 == test ) {
if ( '"wpCompressionTest' == r )
this.get('yes');
else
this.get('no');
}
}
if (x) {
x.open('GET', 'admin-ajax.php?action=wp-compression-test&tested='+test+'&'+(new Date()).getTime(), true);
x.send('');
}
})();
};
testCompression.check();
/* ]]> */
</script>
<?php

View File

@ -85,36 +85,7 @@ function get_file($path) {
return @file_get_contents($path);
}
// Discard any buffers
while ( @ob_end_clean() );
if ( isset($_GET['test']) && 1 == $_GET['test'] ) {
if ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') )
exit('');
$out = 'var wpCompressionTest = 1;';
$force_gzip = ( isset($_GET['c']) && 'gzip' == $_GET['c'] );
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' );
header( 'Cache-Control: no-cache, must-revalidate, max-age=0' );
header( 'Pragma: no-cache' );
header( 'Content-Type: application/x-javascript; charset=UTF-8' );
echo $out;
exit;
}
$load = preg_replace( '/[^a-z0-9,_-]*/i', '', $_GET['load'] );
$load = preg_replace( '/[^a-z0-9,_-]+/i', '', $_GET['load'] );
$load = explode(',', $load);
if ( empty($load) )
@ -142,7 +113,7 @@ foreach( $load as $handle ) {
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') && 'ob_gzhandler' != ini_get('output_handler') ) {
header('Vary: Accept-Encoding'); // Handle proxies
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {

View File

@ -88,10 +88,7 @@ function get_file($path) {
require(ABSPATH . '/wp-includes/script-loader.php');
require(ABSPATH . '/wp-includes/version.php');
// Discard any buffers
while ( @ob_end_clean() );
$load = preg_replace( '/[^a-z0-9,_-]*/i', '', $_GET['load'] );
$load = preg_replace( '/[^a-z0-9,_-]+/i', '', $_GET['load'] );
$load = explode(',', $load);
if ( empty($load) )
@ -126,7 +123,7 @@ foreach( $load as $handle ) {
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') && 'ob_gzhandler' != ini_get('output_handler') ) {
header('Vary: Accept-Encoding'); // Handle proxies
if ( false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'deflate') && function_exists('gzdeflate') && ! $force_gzip ) {

View File

@ -2,8 +2,6 @@
$basepath = dirname(__FILE__);
while ( @ob_end_clean() );
function get_file($path) {
if ( function_exists('realpath') )
@ -22,7 +20,7 @@ header('Vary: Accept-Encoding'); // Handle proxies
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
header("Cache-Control: public, max-age=$expires_offset");
if ( isset($_GET['c']) && 1 == $_GET['c'] && ! ini_get('zlib.output_compression') && false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') ) {
if ( isset($_GET['c']) && 1 == $_GET['c'] && false !== strpos( strtolower($_SERVER['HTTP_ACCEPT_ENCODING']), 'gzip') ) {
header('Content-Encoding: gzip');
echo get_file($basepath . '/wp-tinymce.js.gz');
} else {

View File

@ -616,10 +616,10 @@ function wp_print_head_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();
}
@ -634,7 +634,7 @@ function wp_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.
*
@ -678,6 +678,8 @@ function print_admin_styles() {
function script_concat_settings() {
global $concatenate_scripts, $compress_scripts, $compress_css;
$compressed_output = ( ini_get('zlib.output_compression') || 'ob_gzhandler' == ini_get('output_handler') );
if ( ! isset($concatenate_scripts) ) {
$concatenate_scripts = defined('CONCATENATE_SCRIPTS') ? CONCATENATE_SCRIPTS : true;
if ( ! is_admin() || ( $concatenate_scripts && -1 == get_user_option('concatenate_scripts') ) )
@ -686,13 +688,13 @@ function script_concat_settings() {
if ( ! isset($compress_scripts) ) {
$compress_scripts = defined('COMPRESS_SCRIPTS') ? COMPRESS_SCRIPTS : true;
if ( $compress_scripts && ! get_option('can_compress_scripts') )
if ( $compress_scripts && ( ! get_option('can_compress_scripts') || $compressed_output ) )
$compress_scripts = false;
}
if ( ! isset($compress_css) ) {
$compress_css = defined('COMPRESS_CSS') ? COMPRESS_CSS : true;
if ( $compress_css && ! get_option('can_compress_scripts') )
if ( $compress_css && ( ! get_option('can_compress_scripts') || $compressed_output ) )
$compress_css = false;
}
}

View File

@ -15,6 +15,6 @@ $wp_version = '2.8-bleeding-edge';
*
* @global int $wp_db_version
*/
$wp_db_version = 9872;
$wp_db_version = 9873;
?>