PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.0.7
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.0.7
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 1.2.0 All 28 releases
xspeed / includes / modules / Minify / MinifyModule.php

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

246 lines 8.3 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' => 'Minify',
35 'icon' => 'Wand2',
36 'description' => 'Strip whitespace and rewrite enqueued CSS / JS.',
37 );
38 }
39
40 public function settings_schema(): array {
41 return array(
42 'minify_html' => array(
43 'type' => 'bool',
44 'default' => false,
45 'label' => 'Minify HTML',
46 'description' => 'Strip whitespace and comments from HTML output. Safe on most themes.',
47 ),
48 'minify_css' => array(
49 'type' => 'bool',
50 'default' => false,
51 'label' => 'Minify CSS',
52 'description' => 'Compress and rewrite enqueued local stylesheets. External CSS is left untouched.',
53 ),
54 'minify_js' => array(
55 'type' => 'bool',
56 'default' => false,
57 'label' => 'Minify JavaScript',
58 'description' => 'Compress enqueued local scripts. Disable if you hit script-loading conflicts on the frontend.',
59 ),
60 'defer_js' => array(
61 'type' => 'bool',
62 'default' => false,
63 'label' => 'Defer JavaScript',
64 'description' => 'Add defer="defer" to enqueued script tags so they execute after HTML parsing. jQuery + its hard dependencies are skipped automatically.',
65 ),
66 'delay_js' => array(
67 'type' => 'bool',
68 'default' => false,
69 'label' => 'Delay JavaScript Until Interaction',
70 '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.',
71 ),
72 'async_css' => array(
73 'type' => 'bool',
74 'default' => false,
75 'label' => 'Load CSS Asynchronously',
76 '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.',
77 ),
78 'remove_query_strings' => array(
79 'type' => 'bool',
80 'default' => false,
81 'label' => 'Remove Asset Query Strings',
82 'description' => 'Strip ?ver=X.Y from enqueued CSS / JS URLs. Some CDN caches and proxies cache better when query strings are absent.',
83 ),
84 'defer_js_excluded' => array(
85 'type' => 'list',
86 'default' => array( 'jquery-core', 'jquery-migrate' ),
87 'item_type' => 'string',
88 'label' => 'Defer / Delay Exclusions',
89 'description' => 'Script handles OR URL substrings that skip defer + delay. Defaults exclude jQuery (most themes depend on it being available synchronously). One per line.',
90 // Only relevant once defer OR delay is on — the exclusion list
91 // governs both. Uses the `any` (OR) dependency form. (FBS-82227)
92 'dependsOn' => array(
93 'any' => array(
94 array( 'field' => 'defer_js' ),
95 array( 'field' => 'delay_js' ),
96 ),
97 ),
98 ),
99 'combine_css' => array(
100 'type' => 'bool',
101 'default' => false,
102 'label' => 'Combine CSS Files',
103 '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.',
104 ),
105 'combine_js' => array(
106 'type' => 'bool',
107 'default' => false,
108 'label' => 'Combine JavaScript Files',
109 '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.',
110 ),
111 );
112 }
113
114 /**
115 * 1.1.0: drain minify_html / minify_css / minify_js from the legacy
116 * `xspeed_options` blob into this module's per-module option, then
117 * delete the keys from the legacy blob so duplicate sources can't
118 * re-appear. Idempotent — re-running is a no-op once the keys are
119 * gone from xspeed_options.
120 */
121 public function migrations(): array {
122 return array(
123 '1.1.0' => static function ( array $opts ): array {
124 $legacy = get_option( 'xspeed_options', array() );
125 if ( ! is_array( $legacy ) ) {
126 return $opts;
127 }
128 $dirty = false;
129 foreach ( array( 'minify_html', 'minify_css', 'minify_js' ) as $key ) {
130 if ( array_key_exists( $key, $legacy ) ) {
131 $opts[ $key ] = (bool) $legacy[ $key ];
132 unset( $legacy[ $key ] );
133 $dirty = true;
134 }
135 }
136 if ( $dirty ) {
137 update_option( 'xspeed_options', $legacy );
138 }
139 return $opts;
140 },
141 );
142 }
143
144 /**
145 * Conflict declarations. Detected automatically by Conflict_Registry,
146 * but listing them here keeps the module self-documenting.
147 */
148 public function conflicts(): array {
149 return array(
150 array(
151 'plugin' => 'autoptimize/autoptimize.php',
152 'feature' => 'minify.html',
153 'strategy' => \XSpeed\Conflict_Registry::STRATEGY_REFUSE,
154 'reason' => 'Autoptimize is active and handles minification.',
155 ),
156 array(
157 'plugin' => 'wp-rocket/wp-rocket.php',
158 'feature' => 'minify.html',
159 'strategy' => \XSpeed\Conflict_Registry::STRATEGY_REFUSE,
160 'reason' => 'WP Rocket already handles minification.',
161 ),
162 );
163 }
164
165 public function cli_commands(): array {
166 return array(
167 array(
168 'name' => 'xspeed minify',
169 'callback' => array( $this, 'cli_handler' ),
170 'shortdesc' => 'Inspect or purge xSpeed minify cache.',
171 'synopsis' => array(
172 array(
173 'type' => 'positional',
174 'name' => 'action',
175 'options' => array( 'status', 'purge' ),
176 'optional' => false,
177 ),
178 ),
179 ),
180 );
181 }
182
183 /**
184 * On boot:
185 * 1. Seed our per-module option from the legacy blob if neither
186 * our option nor the migration has run yet (covers the
187 * already-installed-before-this-module-shipped path).
188 * 2. Instantiate the v1 Minifier engine; it now reads from
189 * Settings_Manager::get('minify') via its updated read path.
190 */
191 public function boot(): void {
192 $this->seed_from_legacy_if_needed();
193 new LegacyMinifier();
194 }
195
196 public function activate(): void {
197 // Plugin activation hits all modules. Same seed logic — safe to
198 // run more than once.
199 $this->seed_from_legacy_if_needed();
200 }
201
202 private function seed_from_legacy_if_needed(): void {
203 $existing = get_option( 'xspeed_module_minify', null );
204 if ( null !== $existing ) {
205 return;
206 }
207 $legacy = get_option( 'xspeed_options', array() );
208 if ( ! is_array( $legacy ) ) {
209 return;
210 }
211 $seed = array( '_version' => self::VERSION );
212 $dirty = false;
213 foreach ( array( 'minify_html', 'minify_css', 'minify_js' ) as $key ) {
214 if ( array_key_exists( $key, $legacy ) ) {
215 $seed[ $key ] = (bool) $legacy[ $key ];
216 unset( $legacy[ $key ] );
217 $dirty = true;
218 }
219 }
220 if ( $dirty ) {
221 update_option( 'xspeed_module_minify', $seed );
222 update_option( 'xspeed_options', $legacy );
223 }
224 }
225
226 public function cli_handler( array $args, array $assoc ): void {
227 $action = $args[0] ?? 'status';
228
229 if ( 'status' === $action ) {
230 $opts = Settings_Manager::get( self::SLUG );
231 \WP_CLI::log( sprintf( 'minify_html: %s', $opts['minify_html'] ? 'on' : 'off' ) );
232 \WP_CLI::log( sprintf( 'minify_css : %s', $opts['minify_css'] ? 'on' : 'off' ) );
233 \WP_CLI::log( sprintf( 'minify_js : %s', $opts['minify_js'] ? 'on' : 'off' ) );
234 return;
235 }
236
237 if ( 'purge' === $action ) {
238 LegacyMinifier::purge_minified();
239 \WP_CLI::success( 'Minify cache purged.' );
240 return;
241 }
242
243 \WP_CLI::error( "Unknown action: $action" );
244 }
245 }
246