PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.1.0
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.1.0
1.3.3 1.3.2 1.3.1 1.3.0 1.2.4 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 All 29 releases
xspeed / includes / modules / Minify / MinifyModule.php

MinifyModule.php in xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN 1.1.0, at includes/modules/Minify/MinifyModule.php

251 lines 8.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Minify module.
4 *
5 * Owns the minify_html / minify_css / minify_js settings. Engine work
6 * still happens in XSpeed\Minifier (filters style_loader_src and
7 * script_loader_src), but the Module is now the storage and editing
8 * authority. Settings live in xspeed_module_minify; the legacy
9 * xspeed_options blob is drained on first boot and on POST.
10 *
11 * Tier: Free. See SETTINGS.md for the contract this module satisfies.
12 *
13 * @package XSpeed
14 */
15
16 declare(strict_types=1);
17
18 namespace XSpeed\Modules\Minify;
19
20 defined( 'ABSPATH' ) || exit;
21
22 use XSpeed\Minifier as LegacyMinifier;
23 use XSpeed\Module;
24 use XSpeed\Settings_Manager;
25
26 final class MinifyModule extends Module {
27
28 public const SLUG = 'minify';
29 public const TIER = self::TIER_FREE;
30 public const VERSION = '1.2.0';
31
32 public function ui_metadata(): array {
33 return array(
34 'label' => 'CSS & JavaScript',
35 'tab_label' => 'Minify', // its own tab on the CSS & JavaScript page
36 'icon' => 'Wand2',
37 'description' => 'Strip whitespace and rewrite enqueued CSS / JS.',
38 // Host page: Minify (this module) / Critical CSS (Pro) / Unused
39 // CSS (Pro) as tabs — the three CSS/JS optimizations live on one
40 // page instead of three sidebar rows (FBS-83633).
41 'custom_panel' => 'CssJsPanel',
42 );
43 }
44
45 public function settings_schema(): array {
46 return array(
47 'minify_html' => array(
48 'type' => 'bool',
49 'default' => false,
50 'label' => 'Minify HTML',
51 'description' => 'Strip whitespace and comments from HTML output. Safe on most themes.',
52 ),
53 'minify_css' => array(
54 'type' => 'bool',
55 'default' => false,
56 'label' => 'Minify CSS',
57 'description' => 'Compress and rewrite enqueued local stylesheets. External CSS is left untouched.',
58 ),
59 'minify_js' => array(
60 'type' => 'bool',
61 'default' => false,
62 'label' => 'Minify JavaScript',
63 'description' => 'Compress enqueued local scripts. Disable if you hit script-loading conflicts on the frontend.',
64 ),
65 'defer_js' => array(
66 'type' => 'bool',
67 'default' => false,
68 'label' => 'Defer JavaScript',
69 'description' => 'Add defer="defer" to enqueued script tags so they execute after HTML parsing. jQuery + its hard dependencies are skipped automatically.',
70 ),
71 'delay_js' => array(
72 'type' => 'bool',
73 'default' => false,
74 'label' => 'Delay JavaScript Until Interaction',
75 'description' => 'Postpone script loading until the visitor scrolls, moves the mouse, taps, or presses a key. Drastically improves first paint on script-heavy pages; can break above-the-fold scripted UI — test before leaving on.',
76 ),
77 'async_css' => array(
78 'type' => 'bool',
79 'default' => false,
80 'label' => 'Load CSS Asynchronously',
81 'description' => 'Rewrite stylesheet link tags to load non-blocking via the print-then-all pattern. Pairs well with critical-CSS workflows; can cause a flash of unstyled content if the theme has no critical CSS.',
82 ),
83 'remove_query_strings' => array(
84 'type' => 'bool',
85 'default' => false,
86 'label' => 'Remove Asset Query Strings',
87 'description' => 'Strip ?ver=X.Y from enqueued CSS / JS URLs. Some CDN caches and proxies cache better when query strings are absent.',
88 ),
89 'defer_js_excluded' => array(
90 'type' => 'list',
91 'default' => array( 'jquery-core', 'jquery-migrate' ),
92 'item_type' => 'string',
93 'label' => 'Defer / Delay Exclusions',
94 'description' => 'Script handles OR URL substrings that skip defer + delay. Defaults exclude jQuery (most themes depend on it being available synchronously). One per line.',
95 // Only relevant once defer OR delay is on — the exclusion list
96 // governs both. Uses the `any` (OR) dependency form. (FBS-82227)
97 'dependsOn' => array(
98 'any' => array(
99 array( 'field' => 'defer_js' ),
100 array( 'field' => 'delay_js' ),
101 ),
102 ),
103 ),
104 'combine_css' => array(
105 'type' => 'bool',
106 'default' => false,
107 'label' => 'Combine CSS Files',
108 'description' => 'Concatenate enqueued local stylesheets into a single file (with @import and url(…) paths resolved). External CSS is left alone. Pairs poorly with HTTP/2 push — only enable on HTTP/1.1 hosts.',
109 ),
110 'combine_js' => array(
111 'type' => 'bool',
112 'default' => false,
113 'label' => 'Combine JavaScript Files',
114 'description' => 'Concatenate enqueued local scripts into a single file. External scripts + scripts marked async / deferred are left alone. Disable if you hit dependency-order issues; the combiner respects WordPress enqueue order but inline scripts attached via wp_add_inline_script can shift behavior.',
115 ),
116 );
117 }
118
119 /**
120 * 1.1.0: drain minify_html / minify_css / minify_js from the legacy
121 * `xspeed_options` blob into this module's per-module option, then
122 * delete the keys from the legacy blob so duplicate sources can't
123 * re-appear. Idempotent — re-running is a no-op once the keys are
124 * gone from xspeed_options.
125 */
126 public function migrations(): array {
127 return array(
128 '1.1.0' => static function ( array $opts ): array {
129 $legacy = get_option( 'xspeed_options', array() );
130 if ( ! is_array( $legacy ) ) {
131 return $opts;
132 }
133 $dirty = false;
134 foreach ( array( 'minify_html', 'minify_css', 'minify_js' ) as $key ) {
135 if ( array_key_exists( $key, $legacy ) ) {
136 $opts[ $key ] = (bool) $legacy[ $key ];
137 unset( $legacy[ $key ] );
138 $dirty = true;
139 }
140 }
141 if ( $dirty ) {
142 update_option( 'xspeed_options', $legacy );
143 }
144 return $opts;
145 },
146 );
147 }
148
149 /**
150 * Conflict declarations. Detected automatically by Conflict_Registry,
151 * but listing them here keeps the module self-documenting.
152 */
153 public function conflicts(): array {
154 return array(
155 array(
156 'plugin' => 'autoptimize/autoptimize.php',
157 'feature' => 'minify.html',
158 'strategy' => \XSpeed\Conflict_Registry::STRATEGY_REFUSE,
159 'reason' => 'Autoptimize is active and handles minification.',
160 ),
161 array(
162 'plugin' => 'wp-rocket/wp-rocket.php',
163 'feature' => 'minify.html',
164 'strategy' => \XSpeed\Conflict_Registry::STRATEGY_REFUSE,
165 'reason' => 'WP Rocket already handles minification.',
166 ),
167 );
168 }
169
170 public function cli_commands(): array {
171 return array(
172 array(
173 'name' => 'xspeed minify',
174 'callback' => array( $this, 'cli_handler' ),
175 'shortdesc' => 'Inspect or purge xSpeed minify cache.',
176 'synopsis' => array(
177 array(
178 'type' => 'positional',
179 'name' => 'action',
180 'options' => array( 'status', 'purge' ),
181 'optional' => false,
182 ),
183 ),
184 ),
185 );
186 }
187
188 /**
189 * On boot:
190 * 1. Seed our per-module option from the legacy blob if neither
191 * our option nor the migration has run yet (covers the
192 * already-installed-before-this-module-shipped path).
193 * 2. Instantiate the v1 Minifier engine; it now reads from
194 * Settings_Manager::get('minify') via its updated read path.
195 */
196 public function boot(): void {
197 $this->seed_from_legacy_if_needed();
198 new LegacyMinifier();
199 }
200
201 public function activate(): void {
202 // Plugin activation hits all modules. Same seed logic — safe to
203 // run more than once.
204 $this->seed_from_legacy_if_needed();
205 }
206
207 private function seed_from_legacy_if_needed(): void {
208 $existing = get_option( 'xspeed_module_minify', null );
209 if ( null !== $existing ) {
210 return;
211 }
212 $legacy = get_option( 'xspeed_options', array() );
213 if ( ! is_array( $legacy ) ) {
214 return;
215 }
216 $seed = array( '_version' => self::VERSION );
217 $dirty = false;
218 foreach ( array( 'minify_html', 'minify_css', 'minify_js' ) as $key ) {
219 if ( array_key_exists( $key, $legacy ) ) {
220 $seed[ $key ] = (bool) $legacy[ $key ];
221 unset( $legacy[ $key ] );
222 $dirty = true;
223 }
224 }
225 if ( $dirty ) {
226 update_option( 'xspeed_module_minify', $seed );
227 update_option( 'xspeed_options', $legacy );
228 }
229 }
230
231 public function cli_handler( array $args, array $assoc ): void {
232 $action = $args[0] ?? 'status';
233
234 if ( 'status' === $action ) {
235 $opts = Settings_Manager::get( self::SLUG );
236 \WP_CLI::log( sprintf( 'minify_html: %s', $opts['minify_html'] ? 'on' : 'off' ) );
237 \WP_CLI::log( sprintf( 'minify_css : %s', $opts['minify_css'] ? 'on' : 'off' ) );
238 \WP_CLI::log( sprintf( 'minify_js : %s', $opts['minify_js'] ? 'on' : 'off' ) );
239 return;
240 }
241
242 if ( 'purge' === $action ) {
243 LegacyMinifier::purge_minified();
244 \WP_CLI::success( 'Minify cache purged.' );
245 return;
246 }
247
248 \WP_CLI::error( "Unknown action: $action" );
249 }
250 }
251