PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.1.2
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.1.2
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.2, at includes/modules/Minify/MinifyModule.php

259 lines 9.1 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 'delay_js_targets' => array(
105 'type' => 'list',
106 'default' => array(),
107 'item_type' => 'string',
108 'label' => 'Delay Only These Scripts',
109 'description' => 'Script handles OR URL substrings. When non-empty, ONLY matching scripts are delayed — everything else loads normally. Leave empty to delay all scripts (minus the exclusions above). Ideal for postponing one heavy third-party embed without touching the rest of the page. One per line.',
110 'dependsOn' => array( 'field' => 'delay_js' ),
111 ),
112 'combine_css' => array(
113 'type' => 'bool',
114 'default' => false,
115 'label' => 'Combine CSS Files',
116 '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.',
117 ),
118 'combine_js' => array(
119 'type' => 'bool',
120 'default' => false,
121 'label' => 'Combine JavaScript Files',
122 '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.',
123 ),
124 );
125 }
126
127 /**
128 * 1.1.0: drain minify_html / minify_css / minify_js from the legacy
129 * `xspeed_options` blob into this module's per-module option, then
130 * delete the keys from the legacy blob so duplicate sources can't
131 * re-appear. Idempotent — re-running is a no-op once the keys are
132 * gone from xspeed_options.
133 */
134 public function migrations(): array {
135 return array(
136 '1.1.0' => static function ( array $opts ): array {
137 $legacy = get_option( 'xspeed_options', array() );
138 if ( ! is_array( $legacy ) ) {
139 return $opts;
140 }
141 $dirty = false;
142 foreach ( array( 'minify_html', 'minify_css', 'minify_js' ) as $key ) {
143 if ( array_key_exists( $key, $legacy ) ) {
144 $opts[ $key ] = (bool) $legacy[ $key ];
145 unset( $legacy[ $key ] );
146 $dirty = true;
147 }
148 }
149 if ( $dirty ) {
150 update_option( 'xspeed_options', $legacy );
151 }
152 return $opts;
153 },
154 );
155 }
156
157 /**
158 * Conflict declarations. Detected automatically by Conflict_Registry,
159 * but listing them here keeps the module self-documenting.
160 */
161 public function conflicts(): array {
162 return array(
163 array(
164 'plugin' => 'autoptimize/autoptimize.php',
165 'feature' => 'minify.html',
166 'strategy' => \XSpeed\Conflict_Registry::STRATEGY_REFUSE,
167 'reason' => 'Autoptimize is active and handles minification.',
168 ),
169 array(
170 'plugin' => 'wp-rocket/wp-rocket.php',
171 'feature' => 'minify.html',
172 'strategy' => \XSpeed\Conflict_Registry::STRATEGY_REFUSE,
173 'reason' => 'WP Rocket already handles minification.',
174 ),
175 );
176 }
177
178 public function cli_commands(): array {
179 return array(
180 array(
181 'name' => 'xspeed minify',
182 'callback' => array( $this, 'cli_handler' ),
183 'shortdesc' => 'Inspect or purge xSpeed minify cache.',
184 'synopsis' => array(
185 array(
186 'type' => 'positional',
187 'name' => 'action',
188 'options' => array( 'status', 'purge' ),
189 'optional' => false,
190 ),
191 ),
192 ),
193 );
194 }
195
196 /**
197 * On boot:
198 * 1. Seed our per-module option from the legacy blob if neither
199 * our option nor the migration has run yet (covers the
200 * already-installed-before-this-module-shipped path).
201 * 2. Instantiate the v1 Minifier engine; it now reads from
202 * Settings_Manager::get('minify') via its updated read path.
203 */
204 public function boot(): void {
205 $this->seed_from_legacy_if_needed();
206 new LegacyMinifier();
207 }
208
209 public function activate(): void {
210 // Plugin activation hits all modules. Same seed logic — safe to
211 // run more than once.
212 $this->seed_from_legacy_if_needed();
213 }
214
215 private function seed_from_legacy_if_needed(): void {
216 $existing = get_option( 'xspeed_module_minify', null );
217 if ( null !== $existing ) {
218 return;
219 }
220 $legacy = get_option( 'xspeed_options', array() );
221 if ( ! is_array( $legacy ) ) {
222 return;
223 }
224 $seed = array( '_version' => self::VERSION );
225 $dirty = false;
226 foreach ( array( 'minify_html', 'minify_css', 'minify_js' ) as $key ) {
227 if ( array_key_exists( $key, $legacy ) ) {
228 $seed[ $key ] = (bool) $legacy[ $key ];
229 unset( $legacy[ $key ] );
230 $dirty = true;
231 }
232 }
233 if ( $dirty ) {
234 update_option( 'xspeed_module_minify', $seed );
235 update_option( 'xspeed_options', $legacy );
236 }
237 }
238
239 public function cli_handler( array $args, array $assoc ): void {
240 $action = $args[0] ?? 'status';
241
242 if ( 'status' === $action ) {
243 $opts = Settings_Manager::get( self::SLUG );
244 \WP_CLI::log( sprintf( 'minify_html: %s', $opts['minify_html'] ? 'on' : 'off' ) );
245 \WP_CLI::log( sprintf( 'minify_css : %s', $opts['minify_css'] ? 'on' : 'off' ) );
246 \WP_CLI::log( sprintf( 'minify_js : %s', $opts['minify_js'] ? 'on' : 'off' ) );
247 return;
248 }
249
250 if ( 'purge' === $action ) {
251 LegacyMinifier::purge_minified();
252 \WP_CLI::success( 'Minify cache purged.' );
253 return;
254 }
255
256 \WP_CLI::error( "Unknown action: $action" );
257 }
258 }
259