| 1 |
<?php |
| 2 |
/** |
| 3 |
* GZIP module. |
| 4 |
* |
| 5 |
* Owns the gzip_enabled setting. On flip → writes / removes Apache / |
| 6 |
* LiteSpeed `.htaccess` rules via the static XSpeed\Gzip helper. On |
| 7 |
* nginx (or any server xSpeed can't auto-configure), the panel shows |
| 8 |
* the manual snippet via ui_notices(). |
| 9 |
* |
| 10 |
* Tier: Free. See SETTINGS.md for the contract this module satisfies. |
| 11 |
* |
| 12 |
* @package XSpeed |
| 13 |
*/ |
| 14 |
|
| 15 |
declare(strict_types=1); |
| 16 |
|
| 17 |
namespace XSpeed\Modules\Gzip; |
| 18 |
|
| 19 |
use XSpeed\Gzip as LegacyGzip; |
| 20 |
use XSpeed\Module; |
| 21 |
use XSpeed\Server; |
| 22 |
use XSpeed\Settings_Manager; |
| 23 |
|
| 24 |
final class GzipModule extends Module { |
| 25 |
|
| 26 |
public const SLUG = 'gzip'; |
| 27 |
public const TIER = self::TIER_FREE; |
| 28 |
public const VERSION = '1.1.0'; |
| 29 |
|
| 30 |
public function ui_metadata(): array { |
| 31 |
return array( |
| 32 |
'label' => 'GZIP', |
| 33 |
'icon' => 'Layers', |
| 34 |
'description' => 'Compress responses to reduce transfer size.', |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
public function settings_schema(): array { |
| 39 |
return array( |
| 40 |
'gzip_enabled' => array( |
| 41 |
'type' => 'bool', |
| 42 |
'default' => false, |
| 43 |
'label' => 'Enable GZIP Compression', |
| 44 |
'description' => 'On Apache / LiteSpeed we write the .htaccess rules automatically. On nginx the snippet below must be added to your server config.', |
| 45 |
), |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* 1.1.0: drain gzip_enabled from the legacy xspeed_options blob into |
| 51 |
* this module's option. Idempotent — a re-run with the legacy key |
| 52 |
* already gone is a no-op. |
| 53 |
*/ |
| 54 |
public function migrations(): array { |
| 55 |
return array( |
| 56 |
'1.1.0' => static function ( array $opts ): array { |
| 57 |
$legacy = get_option( 'xspeed_options', array() ); |
| 58 |
if ( ! is_array( $legacy ) || ! array_key_exists( 'gzip_enabled', $legacy ) ) { |
| 59 |
return $opts; |
| 60 |
} |
| 61 |
$opts['gzip_enabled'] = (bool) $legacy['gzip_enabled']; |
| 62 |
unset( $legacy['gzip_enabled'] ); |
| 63 |
update_option( 'xspeed_options', $legacy ); |
| 64 |
return $opts; |
| 65 |
}, |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
public function cli_commands(): array { |
| 70 |
return array( |
| 71 |
array( |
| 72 |
'name' => 'xspeed gzip', |
| 73 |
'callback' => array( $this, 'cli_handler' ), |
| 74 |
'shortdesc' => 'Show GZIP status (server type, active, mode).', |
| 75 |
'synopsis' => array(), |
| 76 |
), |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Conditional notice published to the module's panel via |
| 82 |
* Module::ui_notices(). When gzip is enabled and the server can't |
| 83 |
* auto-configure (nginx / unknown / IIS), we surface a copy-able |
| 84 |
* snippet so the user can wire it up. |
| 85 |
*/ |
| 86 |
public function ui_notices(): array { |
| 87 |
$opts = Settings_Manager::get( self::SLUG ); |
| 88 |
if ( empty( $opts['gzip_enabled'] ) ) { |
| 89 |
return array(); |
| 90 |
} |
| 91 |
if ( 'auto' === Server::gzip_mode() && LegacyGzip::probe_active() ) { |
| 92 |
return array(); |
| 93 |
} |
| 94 |
if ( 'auto' === Server::gzip_mode() ) { |
| 95 |
return array(); |
| 96 |
} |
| 97 |
// Manual server (nginx, IIS, unknown) → render the snippet. |
| 98 |
$type = Server::type(); |
| 99 |
return array( |
| 100 |
array( |
| 101 |
'tone' => 'warn', |
| 102 |
'title' => sprintf( |
| 103 |
/* translators: %s: server software label (e.g. "nginx") */ |
| 104 |
__( 'GZIP requires server config on %s', 'xspeed' ), |
| 105 |
'nginx' === $type ? 'nginx' : __( 'this server', 'xspeed' ) |
| 106 |
), |
| 107 |
'body' => __( 'xSpeed can only auto-configure GZIP on Apache and LiteSpeed (via .htaccess). Paste the snippet below into your server config and reload.', 'xspeed' ), |
| 108 |
'snippet' => LegacyGzip::nginx_snippet(), |
| 109 |
), |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Module booting: seed per-module option from legacy if needed, |
| 115 |
* then register the change hook that flips Apache / LiteSpeed |
| 116 |
* .htaccess rules whenever gzip_enabled is toggled. This handler |
| 117 |
* fires on writes to xspeed_module_gzip (not the legacy blob). |
| 118 |
*/ |
| 119 |
public function boot(): void { |
| 120 |
$this->seed_from_legacy_if_needed(); |
| 121 |
add_action( 'update_option_xspeed_module_gzip', array( __CLASS__, 'on_settings_change' ), 10, 2 ); |
| 122 |
add_action( 'add_option_xspeed_module_gzip', array( __CLASS__, 'on_settings_added' ), 10, 2 ); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Detect the gzip_enabled flip on write and apply / remove the |
| 127 |
* .htaccess rules. Idempotent — re-running with the same state is a |
| 128 |
* no-op inside LegacyGzip::apply(). |
| 129 |
*/ |
| 130 |
public static function on_settings_change( $old, $new ): void { |
| 131 |
$old_gzip = is_array( $old ) ? ! empty( $old['gzip_enabled'] ) : false; |
| 132 |
$new_gzip = is_array( $new ) ? ! empty( $new['gzip_enabled'] ) : false; |
| 133 |
if ( $old_gzip !== $new_gzip ) { |
| 134 |
LegacyGzip::apply( $new_gzip ); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* The very first write (option doesn't exist yet) hits add_option |
| 140 |
* instead of update_option. Treat it as old=false → new=current. |
| 141 |
*/ |
| 142 |
public static function on_settings_added( $name, $value ): void { |
| 143 |
$enabled = is_array( $value ) ? ! empty( $value['gzip_enabled'] ) : false; |
| 144 |
if ( $enabled ) { |
| 145 |
LegacyGzip::apply( true ); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
public function activate(): void { |
| 150 |
$this->seed_from_legacy_if_needed(); |
| 151 |
} |
| 152 |
|
| 153 |
private function seed_from_legacy_if_needed(): void { |
| 154 |
if ( null !== get_option( 'xspeed_module_gzip', null ) ) { |
| 155 |
return; |
| 156 |
} |
| 157 |
$legacy = get_option( 'xspeed_options', array() ); |
| 158 |
if ( ! is_array( $legacy ) || ! array_key_exists( 'gzip_enabled', $legacy ) ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
update_option( |
| 162 |
'xspeed_module_gzip', |
| 163 |
array( |
| 164 |
'_version' => self::VERSION, |
| 165 |
'gzip_enabled' => (bool) $legacy['gzip_enabled'], |
| 166 |
) |
| 167 |
); |
| 168 |
unset( $legacy['gzip_enabled'] ); |
| 169 |
update_option( 'xspeed_options', $legacy ); |
| 170 |
} |
| 171 |
|
| 172 |
public function cli_handler( array $args, array $assoc ): void { |
| 173 |
$opts = Settings_Manager::get( self::SLUG ); |
| 174 |
\WP_CLI::log( 'enabled ' . ( $opts['gzip_enabled'] ? 'true' : 'false' ) ); |
| 175 |
\WP_CLI::log( 'server ' . Server::type() ); |
| 176 |
\WP_CLI::log( 'mode ' . Server::gzip_mode() ); |
| 177 |
\WP_CLI::log( 'active ' . ( LegacyGzip::probe_active() ? 'true' : 'false' ) ); |
| 178 |
\WP_CLI::log( 'nginx_snippet ' . LegacyGzip::nginx_snippet() ); |
| 179 |
} |
| 180 |
} |
| 181 |
|