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

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