# xspeed/1.0.1/includes/class-gzip.php

xSpeed Cache: AI-Powered Performance Hub with MCP, Caching &amp; CDN, version 1.0.1. 100 lines.

- Page: https://pluginprobe.com/plugins/xspeed/1.0.1/code/includes/class-gzip.php
- Raw: https://pluginprobe.com/plugins/xspeed/1.0.1/raw/includes/class-gzip.php
- Modified: 2026-05-27T11:31:36+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/xspeed/1.0.1/code/includes/class-gzip.php#L10-L20`.

```php
<?php
/**
 * GZIP toggle.
 *
 * Apache + LiteSpeed: writes/removes a marker block in the site root
 * .htaccess. Other servers (nginx, IIS): the toggle stores the user's
 * preference but server config must be edited manually — the UI surfaces
 * a copy-pasteable snippet via Gzip::manual_snippet().
 *
 * @package XSpeed
 */

namespace XSpeed;

defined( 'ABSPATH' ) || exit;

class Gzip {

	const MARKER = 'xSpeed GZIP';

	public static function apply( $enabled ) {
		if ( ! Server::supports_htaccess() ) {
			return false;
		}

		$htaccess = ABSPATH . '.htaccess';
		if ( ! file_exists( $htaccess ) ) {
			return false;
		}

		if ( ! function_exists( 'insert_with_markers' ) ) {
			require_once ABSPATH . 'wp-admin/includes/misc.php';
		}

		$rules = $enabled ? self::apache_rules() : array();
		return (bool) insert_with_markers( $htaccess, self::MARKER, $rules );
	}

	public static function apache_rules() {
		return array(
			'<IfModule mod_deflate.c>',
			'  AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript',
			'  AddOutputFilterByType DEFLATE application/javascript application/x-javascript application/json',
			'  AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml',
			'  AddOutputFilterByType DEFLATE image/svg+xml font/ttf font/otf application/font-woff application/font-woff2',
			'</IfModule>',
		);
	}

	/**
	 * nginx config snippet for users to paste into their server block.
	 */
	public static function nginx_snippet() {
		return implode(
			"\n",
			array(
				'gzip on;',
				'gzip_vary on;',
				'gzip_min_length 1024;',
				'gzip_proxied any;',
				'gzip_comp_level 6;',
				'gzip_types',
				'  text/plain text/css text/xml text/javascript',
				'  application/javascript application/json application/xml',
				'  application/xml+rss application/xhtml+xml',
				'  image/svg+xml font/ttf font/otf application/font-woff application/font-woff2;',
			)
		);
	}

	/**
	 * Returns true if the response we just received looks GZIP-encoded.
	 * Result is cached in a transient for 1 hour because the probe makes a
	 * full HTTP request to home_url() — too slow to run on every /status hit.
	 */
	public static function probe_active() {
		$cached = get_transient( 'xspeed_gzip_active' );
		if ( false !== $cached ) {
			return '1' === $cached;
		}

		$res = wp_remote_get(
			home_url( '/' ),
			array(
				'headers' => array( 'Accept-Encoding' => 'gzip' ),
				'timeout' => 3,
			)
		);

		$active = false;
		if ( ! is_wp_error( $res ) ) {
			$enc    = wp_remote_retrieve_header( $res, 'content-encoding' );
			$active = is_string( $enc ) && false !== stripos( $enc, 'gzip' );
		}

		set_transient( 'xspeed_gzip_active', $active ? '1' : '0', HOUR_IN_SECONDS );
		return $active;
	}
}

```
