'GZIP', 'icon' => 'Layers', 'description' => 'Compress responses to reduce transfer size.', ); } public function settings_schema(): array { return array( 'gzip_enabled' => array( 'type' => 'bool', 'default' => false, 'label' => 'Enable GZIP Compression', 'description' => 'On Apache / LiteSpeed we write the .htaccess rules automatically. On nginx the snippet below must be added to your server config.', ), ); } /** * 1.1.0: drain gzip_enabled from the legacy xspeed_options blob into * this module's option. Idempotent — a re-run with the legacy key * already gone is a no-op. */ public function migrations(): array { return array( '1.1.0' => static function ( array $opts ): array { $legacy = get_option( 'xspeed_options', array() ); if ( ! is_array( $legacy ) || ! array_key_exists( 'gzip_enabled', $legacy ) ) { return $opts; } $opts['gzip_enabled'] = (bool) $legacy['gzip_enabled']; unset( $legacy['gzip_enabled'] ); update_option( 'xspeed_options', $legacy ); return $opts; }, ); } public function cli_commands(): array { return array( array( 'name' => 'xspeed gzip', 'callback' => array( $this, 'cli_handler' ), 'shortdesc' => 'Show GZIP status (server type, active, mode).', 'synopsis' => array(), ), ); } /** * Conditional notice published to the module's panel via * Module::ui_notices(). When gzip is enabled and the server can't * auto-configure (nginx / unknown / IIS), we surface a copy-able * snippet so the user can wire it up. */ public function ui_notices(): array { $opts = Settings_Manager::get( self::SLUG ); if ( empty( $opts['gzip_enabled'] ) ) { return array(); } if ( 'auto' === Server::gzip_mode() && LegacyGzip::probe_active() ) { return array(); } if ( 'auto' === Server::gzip_mode() ) { return array(); } // Manual server (nginx, IIS, unknown) → render the snippet. $type = Server::type(); return array( array( 'tone' => 'warn', 'title' => sprintf( /* translators: %s: server software label (e.g. "nginx") */ __( 'GZIP requires server config on %s', 'xspeed' ), 'nginx' === $type ? 'nginx' : __( 'this server', 'xspeed' ) ), 'body' => __( 'xSpeed can only auto-configure GZIP on Apache and LiteSpeed (via .htaccess). Paste the snippet below into your server config and reload.', 'xspeed' ), 'snippet' => LegacyGzip::nginx_snippet(), ), ); } /** * Module booting: seed per-module option from legacy if needed, * then register the change hook that flips Apache / LiteSpeed * .htaccess rules whenever gzip_enabled is toggled. This handler * fires on writes to xspeed_module_gzip (not the legacy blob). */ public function boot(): void { $this->seed_from_legacy_if_needed(); add_action( 'update_option_xspeed_module_gzip', array( __CLASS__, 'on_settings_change' ), 10, 2 ); add_action( 'add_option_xspeed_module_gzip', array( __CLASS__, 'on_settings_added' ), 10, 2 ); } /** * Detect the gzip_enabled flip on write and apply / remove the * .htaccess rules. Idempotent — re-running with the same state is a * no-op inside LegacyGzip::apply(). */ public static function on_settings_change( $old, $new ): void { $old_gzip = is_array( $old ) ? ! empty( $old['gzip_enabled'] ) : false; $new_gzip = is_array( $new ) ? ! empty( $new['gzip_enabled'] ) : false; if ( $old_gzip !== $new_gzip ) { LegacyGzip::apply( $new_gzip ); } } /** * The very first write (option doesn't exist yet) hits add_option * instead of update_option. Treat it as old=false → new=current. */ public static function on_settings_added( $name, $value ): void { $enabled = is_array( $value ) ? ! empty( $value['gzip_enabled'] ) : false; if ( $enabled ) { LegacyGzip::apply( true ); } } public function activate(): void { $this->seed_from_legacy_if_needed(); } private function seed_from_legacy_if_needed(): void { if ( null !== get_option( 'xspeed_module_gzip', null ) ) { return; } $legacy = get_option( 'xspeed_options', array() ); if ( ! is_array( $legacy ) || ! array_key_exists( 'gzip_enabled', $legacy ) ) { return; } update_option( 'xspeed_module_gzip', array( '_version' => self::VERSION, 'gzip_enabled' => (bool) $legacy['gzip_enabled'], ) ); unset( $legacy['gzip_enabled'] ); update_option( 'xspeed_options', $legacy ); } public function cli_handler( array $args, array $assoc ): void { $opts = Settings_Manager::get( self::SLUG ); \WP_CLI::log( 'enabled ' . ( $opts['gzip_enabled'] ? 'true' : 'false' ) ); \WP_CLI::log( 'server ' . Server::type() ); \WP_CLI::log( 'mode ' . Server::gzip_mode() ); \WP_CLI::log( 'active ' . ( LegacyGzip::probe_active() ? 'true' : 'false' ) ); \WP_CLI::log( 'nginx_snippet ' . LegacyGzip::nginx_snippet() ); } }