PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.4.2
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.4.2
1.4.2 1.4.1 1.4.0 1.3.7 1.3.6 1.3.5 1.3.4 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 All 36 releases
← All changes | includes/class-admin.php +206 -39 1.1.0 → 1.4.2 View file →
@@ -14,8 +14,15 @@
14 14 const PAGE_SLUG = 'xspeed';
15 15
16 16 const THEME_COOKIE = 'xspeed_theme';
17 17
18 + /**
19 + * The dashboard's sidebar offer reads a module's `purchase_needed` before
20 + * its `status_label`. An add-on checks for this constant before it labels
21 + * a setup step, which an older dashboard would read as "still for sale".
22 + */
23 + const OFFER_READS_PURCHASE_NEEDED = true;
24 +
18 25 public function __construct() {
19 26 add_action( 'admin_menu', array( $this, 'register_menu' ) );
20 27 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
21 28 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_menu_styles' ) );
@@ -195,8 +202,16 @@
195 202 // user inside the right module. WordPress's add_submenu_page
196 203 // strips the hash, so we inject directly into $submenu where
197 204 // it survives intact (the same trick Yoast / WooCommerce use
198 205 // for their per-area shortcuts).
206 + //
207 + // Writing to $submenu skips the capability check add_submenu_page()
208 + // does, and core keeps a top-level item the user can't open while its
209 + // submenu has entries. Without this guard an Editor saw an empty
210 + // "xSpeed Cache" flyout that led to a permissions error. (#720)
211 + if ( ! current_user_can( 'manage_options' ) ) {
212 + return;
213 + }
199 214 global $submenu;
200 215 $deep_links = self::deep_link_items();
201 216 foreach ( $deep_links as $hash => $label ) {
202 217 $submenu[ self::PAGE_SLUG ][] = array(
@@ -207,47 +222,41 @@
207 222 }
208 223 }
209 224
210 225 /**
211 - * Section deep-links rendered under the xSpeed menu. Mirrors the
212 - * React sidebar's group manifest (see src/components/sidebarGroups.ts)
213 - * so the WP admin rail reads as the same map as the in-app sidebar.
214 - * Each entry points at the first module slug in that group; the
215 - * React app's hash router lands the user on that module, which is
216 - * the first row of that group's sidebar section — no manual scrolling.
226 + * Section deep-links rendered under the xSpeed menu.
217 227 *
218 - * If you add a group to React's SIDEBAR_GROUPS, add it here too. The
219 - * two arrays are intentionally co-located in PR review (same change
220 - * touches both) rather than DRY'd through a generated config file —
221 - * this is the only PHP↔TS coupling and it's tiny.
228 + * This is deliberately a SHORT LIST, not a mirror of React's
229 + * SIDEBAR_GROUPS. It used to mirror all eight groups 1:1, which made the
230 + * WP admin rail a second, competing copy of the in-app sidebar — the same
231 + * map rendered twice, one of them permanently expanded and pushing every
232 + * other plugin's menu down the page.
222 233 *
223 - * @return array<string,string> map of first-module-hash → group label.
234 + * What stays are the entry points a user navigates to from OUTSIDE the
235 + * app: the dashboard itself, the two areas people arrive at with a
236 + * specific errand (AI & agents to connect an assistant, Settings), and the
237 + * wizard. Everything else — Cache, Optimization, Network, Health &
238 + * insights, Tools — is one click away in the app's own sidebar, which is
239 + * where in-app navigation belongs.
240 + *
241 + * So adding a group to React's SIDEBAR_GROUPS no longer means adding it
242 + * here. The two lists are intentionally different lengths now.
243 + *
244 + * Setup Wizard is NOT in this array — it's a real submenu page registered
245 + * by Onboarding::register_menu() on `admin_menu` at priority 20, so it
246 + * lands after these entries.
247 + *
248 + * @return array<string,string> map of route hash → menu label.
224 249 */
225 250 private static function deep_link_items() {
226 - // Mirrors the dashboard sidebar groups (SIDEBAR_GROUPS in
227 - // sidebarGroups.ts) 1:1, in the same order. Each key is the first
228 - // module slug of that group (the anchor the submenu deep-links to).
229 - // Keep these two lists in sync — they're the only PHP↔TS coupling.
230 - // Anchors must be slugs that are always present in the /modules
231 - // payload so the hash resolves on Free too: 'ai-provider' (AI group;
232 - // the first AI slug — injected as a locked placeholder on Free via
233 - // the upsell manifest, so it resolves even though the real module is
234 - // Pro) and 'multisite' (Pro Add-ons; resolves to the real module on a
235 - // licensed multisite). NOTE: must match a slug React actually
236 - // renders — 'ai-privacy' was removed from the groups, so anchoring
237 - // AI there opened nothing (FBS-82743 #2). (FBS-82096)
251 + // Keys are route hashes (`/<group-id>`); the submenu loop prepends '#'.
252 + // React (nav.ts parseRoute) resolves `#/<group-id>` to that landing;
253 + // legacy `#slug` links still redirect, so old bookmarks keep working —
254 + // including bookmarks to the groups no longer listed here.
238 255 return array(
239 - 'cache' => __( 'Cache', 'xspeed' ),
240 - 'minify' => __( 'Optimization', 'xspeed' ),
241 - 'cdn' => __( 'Network', 'xspeed' ),
242 - 'health' => __( 'Health', 'xspeed' ),
243 - 'database' => __( 'Tools', 'xspeed' ),
244 - // AI & Agents is an accordion group; anchor to its first row. On
245 - // Free 'ai-provider' resolves via its locked manifest placeholder.
246 - // License / Branding / Multisite live under Settings (a Free
247 - // container, always present, so the anchor resolves on Free too).
248 - 'ai-provider' => __( 'AI & Agents', 'xspeed' ),
249 - 'settings' => __( 'Settings', 'xspeed' ),
256 + '/overview' => __( 'Overview', 'xspeed' ),
257 + '/ai-agents' => __( 'AI & agents', 'xspeed' ),
258 + '/settings' => __( 'Settings', 'xspeed' ),
250 259 );
251 260 }
252 261
253 262 /**
@@ -370,13 +379,25 @@
370 379 );
371 380 }
372 381 }
373 382
383 + // Redesign v2 design tokens + self-hosted fonts. Hand-written (not
384 + // Vite-bundled) so the @font-face url('./fonts/…') resolve relative to
385 + // assets/. admin.css depends on it so the CSS vars are defined first.
386 + $theme_css = XSPEED_DIR . 'assets/theme.css';
387 + if ( file_exists( $theme_css ) ) {
388 + wp_enqueue_style(
389 + 'xspeed-theme',
390 + XSPEED_URL . 'assets/theme.css',
391 + array(),
392 + XSPEED_VERSION . '.' . filemtime( $theme_css )
393 + );
394 + }
374 395 if ( file_exists( $asset_css ) ) {
375 396 wp_enqueue_style(
376 397 'xspeed-admin',
377 398 XSPEED_URL . 'assets/admin.css',
378 - array(),
399 + array( 'xspeed-theme' ),
379 400 XSPEED_VERSION . '.' . filemtime( $asset_css )
380 401 );
381 402 }
382 403
@@ -390,10 +411,23 @@
390 411 * @since 1.5.0
391 412 */
392 413 do_action( 'xspeed_admin_enqueue', $hook );
393 414
394 - wp_localize_script(
395 - 'xspeed-admin',
415 + // NOT wp_localize_script(). WP_Scripts::localize() casts every scalar
416 + // to a string (wp-includes/class-wp-scripts.php: `(string) $value`),
417 + // so an int arrives in JS as "21600" and a bool as "1" or "".
418 + //
419 + // That silently broke the Pro prewarm scheduler: it guards with
420 + // `typeof gmtOffset === 'number'`, which a string fails, so the site's
421 + // UTC offset was treated as 0 and every one-off warm was scheduled
422 + // against UTC instead of site time — hours late on any non-UTC site,
423 + // under a label that confidently read "Site time (UTC)".
424 + //
425 + // wp_add_inline_script() with wp_json_encode() preserves types, so
426 + // numbers stay numbers and booleans stay booleans. Worth doing beyond
427 + // the one field: every future numeric or boolean config value would
428 + // hit the same trap. (#105)
429 + self::print_config(
396 430 'XSpeedConfig',
397 431 array(
398 432 'restUrl' => esc_url_raw( rest_url( Rest_Api::NAMESPACE_V1 ) ),
399 433 'nonce' => wp_create_nonce( 'wp_rest' ),
@@ -415,8 +449,14 @@
415 449 // Pro), so Pro filters this via `xspeed_pro_state`. Default:
416 450 // not_installed when Pro is absent; 'active' when Pro is present
417 451 // (Pro downgrades to 'unlicensed' when its license isn't valid).
418 452 'proState' => self::pro_state(),
453 + // The xCloud purge plugin is here: the site already has
454 + // Cloudflare Enterprise from its host, so the dashboard does
455 + // not offer the add-on.
456 + 'xcloudPurgePlugin' => Managed_Edge_Purge::installed(),
457 + // Setup Wizard URL, surfaced in the sidebar profile popover.
458 + 'wizardUrl' => admin_url( 'admin.php?page=' . Onboarding::PAGE_SLUG ),
419 459 'bootstrap' => self::bootstrap_payload(),
420 460 )
421 461 );
422 462 }
@@ -421,8 +461,36 @@
421 461 );
422 462 }
423 463
424 464 /**
465 + * Emit a JS global for the admin bundle with types intact.
466 + *
467 + * The type-preserving replacement for wp_localize_script(), which
468 + * stringifies every scalar. Attached to the `xspeed-admin` handle as a
469 + * `before` script so it is defined by the time the bundle executes —
470 + * exactly the ordering guarantee localize gave us.
471 + *
472 + * Shared by the dashboard and the onboarding wizard so neither can drift
473 + * back to the stringifying path.
474 + *
475 + * @param string $var_name JS global to define.
476 + * @param array $data Payload; encoded with wp_json_encode().
477 + */
478 + public static function print_config( string $var_name, array $data ): void {
479 + $json = wp_json_encode( $data );
480 + if ( false === $json ) {
481 + // Never emit a broken assignment — the bundle reads this global
482 + // on mount and a syntax error here blanks the whole screen.
483 + $json = '{}';
484 + }
485 + wp_add_inline_script(
486 + 'xspeed-admin',
487 + 'var ' . $var_name . ' = ' . $json . ';',
488 + 'before'
489 + );
490 + }
491 +
492 + /**
425 493 * Pre-rendered settings + status payload, baked into the page so the
426 494 * React app can mount with real values instead of showing a loading state
427 495 * while it waits for /settings and /status REST calls.
428 496 */
@@ -482,8 +550,13 @@
482 550 // means the request-terminating nginx isn't user-editable on this
483 551 // host — the banner uses this to switch to honest messaging
484 552 // instead of dangling a snippet the user can't apply.
485 553 'behind_proxy' => Server::is_behind_proxy(),
554 + // Mirrors /status (see the note there). On first paint the
555 + // probe is whatever was last cached, so this is usually
556 + // `unknown` until the Health tab runs a live probe — the
557 + // dashboard needs the key present either way.
558 + 'rules' => Cache::rules_state( $probe ),
486 559 );
487 560 }
488 561
489 562 return array(
@@ -502,9 +575,18 @@
502 575 // /status block so the dashboard callout renders on first paint
503 576 // without waiting for a status re-fetch.
504 577 'mobile_separate' => array(
505 578 'enabled' => (bool) ( $opts['cache_enabled'] ? ( Settings_Manager::get( 'cache' )['mobile_separate'] ?? false ) : false ),
506 - 'blocking' => $rewrite_capable && 'mobile_separate' === Cache::static_rewrite_block_reason(),
579 + // Gated to servers that HAVE a static fast path — on IIS or
580 + // an undetected server block_reason still falls through to
581 + // mobile_separate, and reporting that as "blocking" would
582 + // nag about a rewrite that does not exist there (#108).
583 + // LiteSpeed joined the capable set with the Static Fast
584 + // Path opt-in (#509); its own refusal (litespeed_dropin)
585 + // outranks mobile_separate, so this stays false there
586 + // until the opt-in is on.
587 + 'blocking' => ( $rewrite_capable || Server::LITESPEED === $server_type )
588 + && 'mobile_separate' === Cache::static_rewrite_block_reason(),
507 589 'needs_review' => Cache::mobile_separate_needs_review(),
508 590 ),
509 591 // One consolidated nginx server-block snippet aggregating
510 592 // every enabled module's directives (Cache static-rewrite,
@@ -511,17 +593,41 @@
511 593 // BrowserCache headers, GZIP, …). Null on non-nginx hosts
512 594 // or when no module contributes directives. Replaces the
513 595 // per-module "paste this snippet" notices.
514 596 'nginx_server_block' => Cache::full_nginx_server_block(),
597 + // Mirrors the /status block so the enable-time disclosure is
598 + // correct on FIRST PAINT. Without it the top-bar switch can be
599 + // clicked before a status fetch lands, and the one moment the
600 + // warning exists for — a leftover drop-in about to be
601 + // replaced — is exactly when it would be missing.
602 + 'dropin' => Page_Cache_Detector::dropin_disclosure(),
515 603 ),
516 604 // Registered Modules (Free + Pro). The React app discovers them
517 605 // here and renders one sidebar item + one panel per module that
518 606 // declares a settings schema. Hidden modules are filtered.
519 607 'modules' => self::modules_payload(),
608 + // xSpeed Hub connection snapshot so the Account panel + header chip
609 + // render the correct connected/not-connected state on FIRST PAINT —
610 + // no fetch, no "not connected → connected" flash. Null when the MCP
611 + // module is unavailable (the panel then falls back to /mcp/hub).
612 + 'hub' => self::hub_payload(),
520 613 );
521 614 }
522 615
523 616 /**
617 + * xSpeed Hub connection snapshot for the dashboard bootstrap. Guarded so the
618 + * dashboard never hard-depends on the MCP module. Same shape as GET /mcp/hub.
619 + *
620 + * @return array<string,mixed>|null
621 + */
622 + private static function hub_payload() {
623 + if ( ! class_exists( '\XSpeed\Modules\Mcp\Mcp_Hub' ) ) {
624 + return null;
625 + }
626 + return \XSpeed\Modules\Mcp\Mcp_Hub::public_status();
627 + }
628 +
629 + /**
524 630 * Serialize every available Module for the React dashboard. Each entry
525 631 * carries enough to render: identity (slug + tier), UI metadata (label,
526 632 * icon, optional description), current settings, and the typed schema
527 633 * the panel uses to render controls.
@@ -545,22 +651,83 @@
545 651 // panel — i.e., truly nothing to render in the dashboard.
546 652 if ( empty( $schema ) && empty( $custom_panel ) ) {
547 653 continue;
548 654 }
655 + $settings = Settings_Manager::get_public( $slug );
656 + // Something else on the site has taken over this module's job. It
657 + // stands down whatever its own switch says, so it is reported off,
658 + // with the reason in place of its usual one.
659 + $blocked = $module->blocked_by();
660 + $active = $module->is_active();
661 +
549 662 $entry = array(
550 663 'slug' => $slug,
551 664 'tier' => $module->tier(),
552 665 'version' => $module->version(),
666 + // Promoted from inside `settings` so the payload is
667 + // self-describing. Consumers kept tripping on this — the Hub
668 + // rendered every module "Inactive" until it learned to look
669 + // inside the settings bag. `settings.enabled` is kept below
670 + // for back-compat; this is the same value, not a second
671 + // source of truth. Modules with no `enabled` key (status
672 + // panels like Health) report null rather than a misleading
673 + // false. (#146)
674 + 'enabled' => array_key_exists( 'enabled', $settings )
675 + ? (bool) $settings['enabled']
676 + : null,
677 + // Whether the module is actually DOING something, which is not
678 + // the same question as `enabled` above. "On" has several shapes
679 + // -- page caching lives in the global option, Minify and Lazy
680 + // are on when any flag is set, MCP when it is connected -- so
681 + // each module answers for itself via is_active(). Consumers
682 + // that want "what is switched on?" (the sidebar's "N on" badge)
683 + // must read THIS, not `enabled`, which only ever described the
684 + // modules that happen to store that one key. null means the
685 + // module has no meaningful on/off and should be excluded from
686 + // any count rather than treated as off. (#363)
687 + 'active' => ( null !== $blocked && true === $active ) ? false : $active,
688 + // One sentence explaining the line above, computed next to it
689 + // so the two cannot disagree. The UI shows it behind an (i)
690 + // beside the status pill: "On" is a bare assertion otherwise,
691 + // and least obvious exactly where it matters -- Media
692 + // Optimization reads On while its two most prominent switches
693 + // are off, because three other flags are on. (#363)
694 + 'active_reason' => null !== $blocked ? $blocked : $module->active_reason(),
695 + // A purchase or licence the module still waits on. Shown in the
696 + // page header in place of the On/Off pill. Not while another
697 + // module has taken over: that reason is the one to show.
698 + 'status_label' => null !== $blocked ? null : $module->status_label(),
553 699 'label' => $meta['label'] ?? ucfirst( $slug ),
554 700 'icon' => $meta['icon'] ?? 'Square',
555 701 'description' => $meta['description'] ?? '',
702 + // The dashboard group the module belongs to (cache, performance,
703 + // network, insights, tools, ai-agents, settings), declared by the
704 + // module itself. The Hub kept its own copy of this map, so every
705 + // new module landed in its "Other" bucket until the Hub shipped.
706 + // Must agree with src/components/sidebarGroups.ts; a unit test
707 + // holds the two together.
708 + 'group' => $meta['group'] ?? null,
556 709 // Short label for the module's own tab when it hosts a tabbed
557 710 // page (FBS-83633). Only set on host modules.
558 711 'tab_label' => $meta['tab_label'] ?? null,
559 - 'settings' => Settings_Manager::get( $slug ),
712 + // Public view: real values except secret fields, which are masked.
713 + // The dashboard bundle localizes this into page HTML, so a raw
714 + // credential here would be readable from view-source. (#115)
715 + 'settings' => $settings,
716 + // Where each value actually came from: a wp-config.php constant,
717 + // the option row, or the schema default. The panel renders a
718 + // constant-sourced field read-only and names the constant, so it
719 + // can never present an editable box over a value the site is not
720 + // using. Every module gets this, not just the ones that declare
721 + // constants today. (#398)
722 + 'setting_origins' => Settings_Manager::origins( $slug ),
560 723 'schema' => $schema,
561 724 'notices' => $module->ui_notices(),
562 725 'custom_panel' => $meta['custom_panel'] ?? null,
726 + // Why this module may not be switched on, when something else
727 + // on the site has taken over from it. Null for almost every
728 + // module almost always — Free never blocks anything itself.
729 + 'blocked_by' => $blocked,
563 730 );
564 731
565 732 /**
566 733 * Last-mile descriptor filter. Lets Pro (or third-party