| 1 |
<?php |
| 2 |
/** |
| 3 |
* Browser cache headers module — writes Cache-Control + Expires |
| 4 |
* directives to .htaccess (Apache/LiteSpeed) or surfaces an nginx |
| 5 |
* snippet for manual paste on other servers. |
| 6 |
* |
| 7 |
* Tier: Free per FEATURES.md (LiteSpeed parity — Browser Cache |
| 8 |
* settings are all in LS free). |
| 9 |
* |
| 10 |
* @package XSpeed |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace XSpeed\Modules\BrowserCache; |
| 16 |
|
| 17 |
use XSpeed\Browser_Cache; |
| 18 |
use XSpeed\Module; |
| 19 |
use XSpeed\Server; |
| 20 |
|
| 21 |
final class BrowserCacheModule extends Module { |
| 22 |
|
| 23 |
public const SLUG = 'browser-cache'; |
| 24 |
public const TIER = self::TIER_FREE; |
| 25 |
public const VERSION = '1.0.0'; |
| 26 |
|
| 27 |
public function ui_metadata(): array { |
| 28 |
return array( |
| 29 |
'label' => 'Browser Cache', |
| 30 |
'icon' => 'Clock', |
| 31 |
'description' => 'Tell browsers (and intermediate CDNs) how long to cache static assets and HTML.', |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
public function settings_schema(): array { |
| 36 |
return array( |
| 37 |
'enabled' => array( |
| 38 |
'type' => 'bool', |
| 39 |
'default' => false, |
| 40 |
'label' => 'Enable browser cache headers', |
| 41 |
'description' => 'On Apache/LiteSpeed this writes Cache-Control + Expires rules into .htaccess. On nginx it just stores the settings — you paste the snippet into your server block manually.', |
| 42 |
), |
| 43 |
'asset_ttl' => array( |
| 44 |
'type' => 'int', |
| 45 |
'default' => Browser_Cache::DEFAULT_ASSET_TTL, |
| 46 |
'min' => 0, |
| 47 |
'max' => 31536000, |
| 48 |
'label' => 'Static asset TTL (seconds)', |
| 49 |
'description' => 'Cache lifetime for CSS, JS, fonts, images. Defaults to 1 year + immutable (the industry-standard "fingerprinted assets never change" pattern).', |
| 50 |
), |
| 51 |
'html_ttl' => array( |
| 52 |
'type' => 'int', |
| 53 |
'default' => Browser_Cache::DEFAULT_HTML_TTL, |
| 54 |
'min' => 0, |
| 55 |
'max' => 31536000, |
| 56 |
'label' => 'HTML TTL (seconds)', |
| 57 |
'description' => 'Cache lifetime for the HTML document itself. Keep short (default 1h) so post edits roll out same-day.', |
| 58 |
), |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
public function boot(): void { |
| 63 |
add_action( 'update_option_xspeed_module_browser-cache', array( $this, 'on_settings_change' ), 10, 2 ); |
| 64 |
add_action( 'add_option_xspeed_module_browser-cache', array( $this, 'on_settings_added' ), 10, 2 ); |
| 65 |
} |
| 66 |
|
| 67 |
public function on_settings_change( $old, $new ): void { |
| 68 |
if ( ! is_array( $new ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
Browser_Cache::apply( ! empty( $new['enabled'] ), $new ); |
| 72 |
} |
| 73 |
|
| 74 |
public function on_settings_added( $name, $value ): void { |
| 75 |
if ( ! is_array( $value ) ) { |
| 76 |
return; |
| 77 |
} |
| 78 |
Browser_Cache::apply( ! empty( $value['enabled'] ), $value ); |
| 79 |
} |
| 80 |
|
| 81 |
public function ui_notices(): array { |
| 82 |
if ( class_exists( '\\XSpeed\\Server' ) && ! Server::supports_htaccess() ) { |
| 83 |
return array( |
| 84 |
array( |
| 85 |
'tone' => 'info', |
| 86 |
'title' => 'Manual nginx config required', |
| 87 |
'body' => "Your server doesn't support .htaccess. After saving, copy the nginx snippet below into your server block.\n\n" . Browser_Cache::nginx_snippet( $this->get_settings() ), |
| 88 |
), |
| 89 |
); |
| 90 |
} |
| 91 |
return array(); |
| 92 |
} |
| 93 |
|
| 94 |
public function deactivate(): void { |
| 95 |
Browser_Cache::apply( false ); |
| 96 |
} |
| 97 |
|
| 98 |
public function cli_commands(): array { |
| 99 |
return array( |
| 100 |
array( |
| 101 |
'name' => 'xspeed browser-cache', |
| 102 |
'callback' => array( $this, 'cli_handler' ), |
| 103 |
'shortdesc' => 'Print the Apache or nginx browser-cache snippet.', |
| 104 |
'synopsis' => array( |
| 105 |
array( |
| 106 |
'type' => 'positional', |
| 107 |
'name' => 'flavor', |
| 108 |
'options' => array( 'apache', 'nginx' ), |
| 109 |
'optional' => true, |
| 110 |
), |
| 111 |
), |
| 112 |
), |
| 113 |
); |
| 114 |
} |
| 115 |
|
| 116 |
public function cli_handler( array $args, array $assoc ): void { |
| 117 |
$flavor = $args[0] ?? 'apache'; |
| 118 |
$opts = $this->get_settings(); |
| 119 |
if ( 'nginx' === $flavor ) { |
| 120 |
\WP_CLI::log( Browser_Cache::nginx_snippet( $opts ) ); |
| 121 |
return; |
| 122 |
} |
| 123 |
foreach ( Browser_Cache::apache_rules( $opts ) as $line ) { |
| 124 |
\WP_CLI::log( $line ); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|