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

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

- Page: https://pluginprobe.com/plugins/xspeed/1.1.1/code/includes/class-gzip.php
- Raw: https://pluginprobe.com/plugins/xspeed/1.1.1/raw/includes/class-gzip.php
- Modified: 2026-06-25T12:53:32+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.1.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';
		}

		// Build the block from the GZIP base (only when GZIP is on) plus any
		// rules add-ons append via the filter. An add-on (e.g. the Pro Brotli
		// module) may want its own directives even when GZIP itself is off —
		// so we run the filter regardless and write whatever it returns,
		// emptying the marker block only when there is genuinely nothing.
		$rules = self::apache_rules( (bool) $enabled );
		return (bool) insert_with_markers( $htaccess, self::MARKER, $rules );
	}

	/**
	 * Compression rules written inside the marker block.
	 *
	 * @param bool $gzip_enabled Whether to include the GZIP (mod_deflate)
	 *                           base rules. Add-on filter contributions are
	 *                           applied either way, so Brotli (or another
	 *                           encoder) can be served even with GZIP off.
	 * @return string[]
	 */
	public static function apache_rules( $gzip_enabled = true ) {
		$rules = $gzip_enabled ? 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>',
		) : array();

		/**
		 * Filter the Apache compression rules written to .htaccess.
		 *
		 * The core engine emits GZIP (mod_deflate) when GZIP is enabled.
		 * Add-ons (xspeed-pro Brotli module) append their own
		 * <IfModule mod_brotli.c> block so Brotli is served where supported,
		 * GZIP otherwise. The base array is empty when GZIP is off, so an
		 * add-on can be the sole contributor. Listeners MUST return the full
		 * rules array (append, don't replace).
		 *
		 * @param string[] $rules        Lines written inside the xSpeed GZIP marker block.
		 * @param bool     $gzip_enabled Whether GZIP's own base rules are included.
		 */
		return (array) apply_filters( 'xspeed_compression_apache_rules', $rules, $gzip_enabled );
	}

	/**
	 * nginx config snippet for users to paste into their server block.
	 */
	public static function nginx_snippet() {
		$snippet = 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;',
			)
		);

		/**
		 * Filter the nginx compression snippet shown for manual config.
		 *
		 * Core emits the GZIP directives. Add-ons (xspeed-pro Brotli
		 * module) append an ngx_brotli block so users can paste Brotli +
		 * GZIP fallback in one go. Return the full snippet string.
		 *
		 * @param string $snippet The nginx directives block.
		 */
		return (string) apply_filters( 'xspeed_compression_nginx_snippet', $snippet );
	}

	/**
	 * 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;
	}
}

```
