← All changes
|
includes/modules/BrowserCache/BrowserCacheModule.php
+78
-17
1.0.2
→
1.3.2
View file →
| @@ -13,9 +13,12 @@ | ||
| 13 | 13 | declare(strict_types=1); |
| 14 | 14 | |
| 15 | 15 | namespace XSpeed\Modules\BrowserCache; |
| 16 | 16 | |
| 17 | +defined( 'ABSPATH' ) || exit; | |
| 18 | + | |
| 17 | 19 | use XSpeed\Browser_Cache; |
| 20 | +use XSpeed\Deep_Link; | |
| 18 | 21 | use XSpeed\Module; |
| 19 | 22 | use XSpeed\Server; |
| 20 | 23 | |
| 21 | 24 | final class BrowserCacheModule extends Module { |
| @@ -25,11 +28,11 @@ | ||
| 25 | 28 | public const VERSION = '1.0.0'; |
| 26 | 29 | |
| 27 | 30 | public function ui_metadata(): array { |
| 28 | 31 | return array( |
| 29 | - 'label' => 'Browser Cache', | |
| 32 | + 'label' => __( 'Browser Cache', 'xspeed' ), | |
| 30 | 33 | 'icon' => 'Clock', |
| 31 | - 'description' => 'Tell browsers (and intermediate CDNs) how long to cache static assets and HTML.', | |
| 34 | + 'description' => __( 'Tell browsers (and intermediate CDNs) how long to cache static assets and HTML.', 'xspeed' ), | |
| 32 | 35 | ); |
| 33 | 36 | } |
| 34 | 37 | |
| 35 | 38 | public function settings_schema(): array { |
| @@ -36,10 +39,10 @@ | ||
| 36 | 39 | return array( |
| 37 | 40 | 'enabled' => array( |
| 38 | 41 | 'type' => 'bool', |
| 39 | 42 | '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.', | |
| 43 | + 'label' => __( 'Enable browser cache headers', 'xspeed' ), | |
| 44 | + '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.', 'xspeed' ), | |
| 42 | 45 | ), |
| 43 | 46 | 'asset_ttl' => array( |
| 44 | 47 | 'type' => 'int', |
| 45 | 48 | 'default' => Browser_Cache::DEFAULT_ASSET_TTL, |
| @@ -44,10 +47,12 @@ | ||
| 44 | 47 | 'type' => 'int', |
| 45 | 48 | 'default' => Browser_Cache::DEFAULT_ASSET_TTL, |
| 46 | 49 | 'min' => 0, |
| 47 | 50 | '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).', | |
| 51 | + 'label' => __( 'Static asset TTL (seconds)', 'xspeed' ), | |
| 52 | + 'unit' => 'seconds', | |
| 53 | + 'description' => __( 'Cache lifetime for CSS, JS, fonts, images. Defaults to 1 year + immutable (the industry-standard "fingerprinted assets never change" pattern).', 'xspeed' ), | |
| 54 | + 'dependsOn' => array( 'field' => 'enabled' ), | |
| 50 | 55 | ), |
| 51 | 56 | 'html_ttl' => array( |
| 52 | 57 | 'type' => 'int', |
| 53 | 58 | 'default' => Browser_Cache::DEFAULT_HTML_TTL, |
| @@ -52,10 +57,12 @@ | ||
| 52 | 57 | 'type' => 'int', |
| 53 | 58 | 'default' => Browser_Cache::DEFAULT_HTML_TTL, |
| 54 | 59 | 'min' => 0, |
| 55 | 60 | '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.', | |
| 61 | + 'label' => __( 'HTML TTL (seconds)', 'xspeed' ), | |
| 62 | + 'unit' => 'seconds', | |
| 63 | + 'description' => __( 'Cache lifetime for the HTML document itself. Keep short (default 1h) so post edits roll out same-day.', 'xspeed' ), | |
| 64 | + 'dependsOn' => array( 'field' => 'enabled' ), | |
| 58 | 65 | ), |
| 59 | 66 | ); |
| 60 | 67 | } |
| 61 | 68 | |
| @@ -68,8 +75,12 @@ | ||
| 68 | 75 | if ( ! is_array( $new ) ) { |
| 69 | 76 | return; |
| 70 | 77 | } |
| 71 | 78 | Browser_Cache::apply( ! empty( $new['enabled'] ), $new ); |
| 79 | + // Settings just changed — TTL values likely differ, so the | |
| 80 | + // currently-cached "probe says headers active" answer is stale. | |
| 81 | + // Clear the transient so the next dashboard load re-probes. | |
| 82 | + delete_transient( 'xspeed_browser_cache_probe' ); | |
| 72 | 83 | } |
| 73 | 84 | |
| 74 | 85 | public function on_settings_added( $name, $value ): void { |
| 75 | 86 | if ( ! is_array( $value ) ) { |
| @@ -75,21 +86,56 @@ | ||
| 75 | 86 | if ( ! is_array( $value ) ) { |
| 76 | 87 | return; |
| 77 | 88 | } |
| 78 | 89 | Browser_Cache::apply( ! empty( $value['enabled'] ), $value ); |
| 90 | + delete_transient( 'xspeed_browser_cache_probe' ); | |
| 79 | 91 | } |
| 80 | 92 | |
| 81 | 93 | 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() ), | |
| 94 | + if ( ! class_exists( '\\XSpeed\\Server' ) || Server::supports_htaccess() ) { | |
| 95 | + return array(); | |
| 96 | + } | |
| 97 | + $opts = $this->get_settings(); | |
| 98 | + if ( empty( $opts['enabled'] ) ) { | |
| 99 | + return array(); | |
| 100 | + } | |
| 101 | + | |
| 102 | + // Probe the live response for caching headers on a known static | |
| 103 | + // asset. Suppress the notice unless the probe PROVES nothing is | |
| 104 | + // being sent: | |
| 105 | + // | |
| 106 | + // true → headers present, from our snippet or from the host's own | |
| 107 | + // vhost — either way the feature's job is done (issue #329) | |
| 108 | + // null → the loopback never completed, so we know nothing; a | |
| 109 | + // broken probe is not evidence of a broken server and must | |
| 110 | + // not raise a warning the operator cannot act on (issue #18) | |
| 111 | + // false → proven absent, fall through and show the notice | |
| 112 | + if ( false !== Browser_Cache::probe_headers_present() ) { | |
| 113 | + return array(); | |
| 114 | + } | |
| 115 | + | |
| 116 | + // nginx hosts can't auto-write Cache-Control / Expires headers, and the | |
| 117 | + // live probe just confirmed they are NOT being served — so the feature | |
| 118 | + // reads "enabled" in the dashboard while doing nothing. That is a warning, | |
| 119 | + // not a passive info note (a token that a user missed on this exact | |
| 120 | + // account, issue #117): escalate the tone and say plainly that the config | |
| 121 | + // is configured-but-not-live until the snippet is pasted + nginx reloaded. | |
| 122 | + // The directives go into the unified server-block snippet on the Cache | |
| 123 | + // panel — point users there instead of duplicating the snippet here. | |
| 124 | + return array( | |
| 125 | + array( | |
| 126 | + 'tone' => 'warn', | |
| 127 | + 'title' => __( 'Browser cache is enabled but not active on the server', 'xspeed' ), | |
| 128 | + 'body' => __( 'Browser-cache headers need to live in your nginx config, and the live response shows they are not being sent yet — so this is on in settings but doing nothing. Your settings are included in the unified server-block snippet on the Cache panel: paste it once into your nginx vhost (or container nginx config) and reload nginx.', 'xspeed' ), | |
| 129 | + // Lands on the snippet itself rather than on the Cache panel, | |
| 130 | + // where it is one collapsed section among several (issue #49). | |
| 131 | + 'action' => Deep_Link::action( | |
| 132 | + __( 'Go to the snippet', 'xspeed' ), | |
| 133 | + 'cache', | |
| 134 | + 'nginx_snippet' | |
| 88 | 135 | ), |
| 89 | - ); | |
| 90 | - } | |
| 91 | - return array(); | |
| 136 | + ), | |
| 137 | + ); | |
| 92 | 138 | } |
| 93 | 139 | |
| 94 | 140 | public function deactivate(): void { |
| 95 | 141 | Browser_Cache::apply( false ); |
| @@ -100,8 +146,9 @@ | ||
| 100 | 146 | array( |
| 101 | 147 | 'name' => 'xspeed browser-cache', |
| 102 | 148 | 'callback' => array( $this, 'cli_handler' ), |
| 103 | 149 | 'shortdesc' => 'Print the Apache or nginx browser-cache snippet.', |
| 150 | + 'ai_hint' => 'Get the server config snippet that sets browser cache-control headers for static assets. Use when PageSpeed reports "serve static assets with an efficient cache policy", or when the user needs the rules to paste into Apache/nginx.', | |
| 104 | 151 | 'synopsis' => array( |
| 105 | 152 | array( |
| 106 | 153 | 'type' => 'positional', |
| 107 | 154 | 'name' => 'flavor', |
| @@ -122,6 +169,20 @@ | ||
| 122 | 169 | } |
| 123 | 170 | foreach ( Browser_Cache::apache_rules( $opts ) as $line ) { |
| 124 | 171 | \WP_CLI::log( $line ); |
| 125 | 172 | } |
| 173 | + } | |
| 174 | + | |
| 175 | + /** | |
| 176 | + * Cache-Control directives for the unified nginx server-block | |
| 177 | + * snippet. nginx sends no Expires — `expires off;` pins the block so | |
| 178 | + * only the explicit `add_header` speaks (#259). Null when the module is disabled — no | |
| 179 | + * directives to install. | |
| 180 | + */ | |
| 181 | + public function nginx_directives(): ?string { | |
| 182 | + $opts = $this->get_settings(); | |
| 183 | + if ( empty( $opts['enabled'] ) ) { | |
| 184 | + return null; | |
| 185 | + } | |
| 186 | + return Browser_Cache::nginx_snippet( $opts ); | |
| 126 | 187 | } |
| 127 | 188 | } |