| 1 |
<?php |
| 2 |
/** |
| 3 |
* GZIP toggle. |
| 4 |
* |
| 5 |
* Apache + LiteSpeed: writes/removes a marker block in the site root |
| 6 |
* .htaccess. Other servers (nginx, IIS): the toggle stores the user's |
| 7 |
* preference but server config must be edited manually — the UI surfaces |
| 8 |
* a copy-pasteable snippet via Gzip::manual_snippet(). |
| 9 |
* |
| 10 |
* @package XSpeed |
| 11 |
*/ |
| 12 |
|
| 13 |
namespace XSpeed; |
| 14 |
|
| 15 |
defined( 'ABSPATH' ) || exit; |
| 16 |
|
| 17 |
class Gzip { |
| 18 |
|
| 19 |
const MARKER = 'xSpeed GZIP'; |
| 20 |
|
| 21 |
public static function apply( $enabled ) { |
| 22 |
if ( ! Server::supports_htaccess() ) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
$htaccess = ABSPATH . '.htaccess'; |
| 27 |
if ( ! file_exists( $htaccess ) ) { |
| 28 |
return false; |
| 29 |
} |
| 30 |
|
| 31 |
if ( ! function_exists( 'insert_with_markers' ) ) { |
| 32 |
require_once ABSPATH . 'wp-admin/includes/misc.php'; |
| 33 |
} |
| 34 |
|
| 35 |
$rules = $enabled ? self::apache_rules() : array(); |
| 36 |
return (bool) insert_with_markers( $htaccess, self::MARKER, $rules ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function apache_rules() { |
| 40 |
return array( |
| 41 |
'<IfModule mod_deflate.c>', |
| 42 |
' AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript', |
| 43 |
' AddOutputFilterByType DEFLATE application/javascript application/x-javascript application/json', |
| 44 |
' AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml', |
| 45 |
' AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf application/font-woff application/font-woff2', |
| 46 |
'</IfModule>', |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* nginx config snippet for users to paste into their server block. |
| 52 |
*/ |
| 53 |
public static function nginx_snippet() { |
| 54 |
return implode( |
| 55 |
"\n", |
| 56 |
array( |
| 57 |
'gzip on;', |
| 58 |
'gzip_vary on;', |
| 59 |
'gzip_min_length 1024;', |
| 60 |
'gzip_proxied any;', |
| 61 |
'gzip_comp_level 6;', |
| 62 |
'gzip_types', |
| 63 |
' text/plain text/css text/xml text/javascript', |
| 64 |
' application/javascript application/json application/xml', |
| 65 |
' application/xml+rss application/xhtml+xml', |
| 66 |
' image/svg+xml font/ttf font/otf application/font-woff application/font-woff2;', |
| 67 |
) |
| 68 |
); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns true if the response we just received looks GZIP-encoded. |
| 73 |
* Result is cached in a transient for 1 hour because the probe makes a |
| 74 |
* full HTTP request to home_url() — too slow to run on every /status hit. |
| 75 |
*/ |
| 76 |
public static function probe_active() { |
| 77 |
$cached = get_transient( 'xspeed_gzip_active' ); |
| 78 |
if ( false !== $cached ) { |
| 79 |
return '1' === $cached; |
| 80 |
} |
| 81 |
|
| 82 |
$res = wp_remote_get( |
| 83 |
home_url( '/' ), |
| 84 |
array( |
| 85 |
'headers' => array( 'Accept-Encoding' => 'gzip' ), |
| 86 |
'timeout' => 3, |
| 87 |
) |
| 88 |
); |
| 89 |
|
| 90 |
$active = false; |
| 91 |
if ( ! is_wp_error( $res ) ) { |
| 92 |
$enc = wp_remote_retrieve_header( $res, 'content-encoding' ); |
| 93 |
$active = is_string( $enc ) && false !== stripos( $enc, 'gzip' ); |
| 94 |
} |
| 95 |
|
| 96 |
set_transient( 'xspeed_gzip_active', $active ? '1' : '0', HOUR_IN_SECONDS ); |
| 97 |
return $active; |
| 98 |
} |
| 99 |
} |
| 100 |
|