PluginProbe
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN / 1.4.0
xSpeed Cache: AI-Powered Performance Hub with MCP, Caching & CDN v1.4.0
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 1.1.2 All 35 releases
← All changes | includes/modules/Score/ScoreModule.php +317 -72 1.2.4 → 1.4.0 View file →
@@ -27,8 +27,9 @@
27 27 defined( 'ABSPATH' ) || exit;
28 28
29 29 use XSpeed\Module;
30 30 use XSpeed\Modules\Mcp\Mcp_Hub;
31 +use XSpeed\Modules\Mcp\Mcp_Pairing;
31 32 use XSpeed\Scan;
32 33 use XSpeed\Score;
33 34 use XSpeed\Settings_Manager;
34 35
@@ -37,18 +38,33 @@
37 38 public const SLUG = 'score';
38 39 public const TIER = self::TIER_FREE;
39 40 public const VERSION = '1.1.0';
40 41
42 + /**
43 + * No On/Off state (#425).
44 + *
45 + * The default reports the `enabled` setting, which put an "Off" pill on a
46 + * panel whose Test button works regardless — the press is the consent and
47 + * flips the setting itself. A run-on-demand panel has no meaningful
48 + * on/off, exactly like Health; the setting stays as the internal gate for
49 + * non-press callers (optimize runs, GTmetrix polling), it just is not a
50 + * state this panel wears.
51 + */
52 + public function is_active(): ?bool {
53 + return null;
54 + }
55 +
41 56 public function ui_metadata(): array {
42 57 return array(
43 - 'label' => 'Speed Test',
58 + 'label' => __( 'Speed Test', 'xspeed' ),
44 59 'icon' => 'Gauge',
45 60 // Provider-neutral: the panel runs whichever provider the site has
46 61 // configured (PageSpeed Insights by default, no API key needed).
47 62 // The Hub-run test has its own copy and is gated behind
48 63 // hub_speed_test_enabled(), so this line must not promise it.
49 - 'description' => 'Run a PageSpeed Insights or GTmetrix audit from the dashboard and keep the history next to your TTFB benchmark.',
64 + 'description' => __( 'Run a PageSpeed Insights or GTmetrix test and keep a history of the scores.', 'xspeed' ),
50 65 'custom_panel' => 'ScorePanel',
66 + 'group' => 'insights',
51 67 );
52 68 }
53 69
54 70 public function settings_schema(): array {
@@ -55,12 +71,23 @@
55 71 return array(
56 72 'enabled' => array(
57 73 'type' => 'bool',
58 74 'default' => false,
59 - 'label' => 'Enable external scores',
60 - // Off by default and stated plainly: this is the only part
61 - // of the plugin that talks to a third party on your behalf.
62 - 'description' => 'Lets you run a PageSpeed Insights or GTmetrix audit from this dashboard. Nothing is sent anywhere until you press Test.',
75 + // Meaningful for the surfaces it still reaches (REST schema,
76 + // CLI settings, a wp-config override): it names what the
77 + // value permits, not a switch nobody sees.
78 + 'label' => __( 'Allow speed tests', 'xspeed' ),
79 + // Off by default, but pressing Test IS the consent: the first
80 + // run turns this on rather than refusing (#425). What it
81 + // still guards is everything that is NOT a Test press — an
82 + // optimize run measuring its own effect, for instance.
83 + // Switch it off (REST/CLI) and nothing contacts a provider.
84 + 'description' => __( 'Turns on the first time you run a speed test. Switch it off and no feature, not even an optimize run, contacts a test provider.', 'xspeed' ),
85 + // No dashboard control: the Test press manages it, and a
86 + // visible switch that gates a button elsewhere was the
87 + // confusion #425 removed. Hidden fields are skipped by the
88 + // panel renderer and by settings search.
89 + 'hidden' => true,
63 90 ),
64 91 'provider' => array(
65 92 'type' => 'enum',
66 93 'default' => 'psi',
@@ -68,20 +95,20 @@
68 95 'option_labels' => array(
69 96 'psi' => 'PageSpeed Insights',
70 97 'gtmetrix' => 'GTmetrix',
71 98 ),
72 - 'label' => 'Provider',
73 - 'description' => 'PageSpeed Insights works without an API key. GTmetrix requires one.',
74 - 'dependsOn' => array( 'field' => 'enabled' ),
99 + 'label' => __( 'Test provider', 'xspeed' ),
100 + 'description' => __( 'PageSpeed Insights works without an API key. GTmetrix needs one.', 'xspeed' ),
75 101 ),
76 102 'psi_api_key' => array(
77 103 'type' => 'secret',
78 104 'default' => '',
79 - 'label' => 'PageSpeed API key (optional)',
80 - 'description' => 'Only needed if you hit Google\'s anonymous rate limit. Free from cloud.google.com.',
105 + 'label' => __( 'PageSpeed API key (optional)', 'xspeed' ),
106 + 'description' => __( 'Only needed if Google starts refusing tests without a key. You can get a free key at cloud.google.com.', 'xspeed' ),
81 107 // Rendered as a trailing "Check the documentation" link —
82 108 // descriptions themselves are plain text (#111).
83 109 'doc_url' => 'https://xspeedcache.com/docs/pagespeed-insights-integration/',
110 + 'advanced' => true,
84 111 'dependsOn' => array(
85 112 'field' => 'provider',
86 113 'value' => 'psi',
87 114 ),
@@ -88,10 +115,10 @@
88 115 ),
89 116 'gtmetrix_api_key' => array(
90 117 'type' => 'secret',
91 118 'default' => '',
92 - 'label' => 'GTmetrix API key',
93 - 'description' => 'Required — GTmetrix has no anonymous mode. Found in your GTmetrix account settings.',
119 + 'label' => __( 'GTmetrix API key', 'xspeed' ),
120 + 'description' => __( 'GTmetrix does not run tests without a key. Find it in your GTmetrix account settings.', 'xspeed' ),
94 121 'dependsOn' => array(
95 122 'field' => 'provider',
96 123 'value' => 'gtmetrix',
97 124 ),
@@ -98,18 +125,17 @@
98 125 ),
99 126 'test_url' => array(
100 127 'type' => 'url',
101 128 'default' => '',
102 - 'label' => 'URL to test',
103 - 'description' => 'Leave empty to test your home page.',
104 - 'dependsOn' => array( 'field' => 'enabled' ),
129 + 'label' => __( 'URL to test', 'xspeed' ),
130 + 'description' => __( 'Leave empty to test your home page.', 'xspeed' ),
105 131 ),
106 132 'default_strategy' => array(
107 133 'type' => 'enum',
108 134 'default' => 'mobile',
109 135 'options' => array( 'mobile', 'desktop' ),
110 - 'label' => 'Strategy',
111 - 'description' => 'PageSpeed Insights only. Mobile is what Google ranks on.',
136 + 'label' => __( 'Device', 'xspeed' ),
137 + 'description' => __( 'Test as a phone or a desktop visitor. Google ranks sites on the mobile result.', 'xspeed' ),
112 138 'dependsOn' => array(
113 139 'field' => 'provider',
114 140 'value' => 'psi',
115 141 ),
@@ -228,9 +254,12 @@
228 254 }
229 255
230 256 $started = Scan::start(
231 257 (string) ( $request->get_param( 'url' ) ?? '' ),
232 - (bool) $request->get_param( 'fresh' )
258 + (bool) $request->get_param( 'fresh' ),
259 + // `both` (default), `desktop` or `mobile` — which Lighthouse runs
260 + // the engine spends and therefore which device is the headline.
261 + (string) ( $request->get_param( 'strategy' ) ?? 'both' )
233 262 );
234 263 if ( is_wp_error( $started ) ) {
235 264 return $started;
236 265 }
@@ -409,18 +438,10 @@
409 438 * POST, never GET: this spends someone else's rate limit and takes up
410 439 * to a minute. A GET would be prefetched by a browser.
411 440 */
412 441 public function rest_run( \WP_REST_Request $request ) {
413 - $opts = Settings_Manager::get( self::SLUG );
442 + $opts = $this->consent_by_running( Settings_Manager::get( self::SLUG ) );
414 443
415 - if ( empty( $opts['enabled'] ) ) {
416 - return new \WP_Error(
417 - 'xspeed_score_disabled',
418 - __( 'External scores are turned off. Enable them first — this is the only feature that contacts a third party.', 'xspeed' ),
419 - array( 'status' => 409 )
420 - );
421 - }
422 -
423 444 $url = $this->resolve_url( (string) $request->get_param( 'url' ), $opts );
424 445 if ( '' === $url ) {
425 446 return new \WP_Error(
426 447 'xspeed_score_no_url',
@@ -436,12 +457,200 @@
436 457 return is_wp_error( $started ) ? $started : rest_ensure_response( $started );
437 458 }
438 459
439 460 $strategy = (string) ( $request->get_param( 'strategy' ) ?: $opts['default_strategy'] );
440 - return rest_ensure_response( Score::run_psi( $url, $strategy, (string) $opts['psi_api_key'] ) );
461 + $api_key = (string) $opts['psi_api_key'];
462 +
463 + // No key of their own → run it through the Hub when this site is
464 + // connected. The Hub holds a real Google key, so this is the path
465 + // that does NOT die on the shared anonymous quota (#426). When the
466 + // Hub can't take it, fall through to the anonymous direct call —
467 + // worse odds, but exactly what the plugin did before.
468 + if ( '' === trim( $api_key ) ) {
469 + $via_hub = $this->start_psi_via_hub( $url, $strategy );
470 + if ( null !== $via_hub ) {
471 + return $via_hub;
472 + }
473 + }
474 +
475 + return rest_ensure_response( Score::run_psi( $url, $strategy, $api_key ) );
441 476 }
442 477
443 478 /**
479 + * Record the Test press as the opt-in (#425).
480 + *
481 + * The five-step funnel — find the toggle, enable it, come back, press
482 + * Test — existed to make the outbound call opt-in. The press already is
483 + * the opt-in: it is an explicit, authenticated request to contact a
484 + * provider right now. So a run no longer refuses when the toggle is off;
485 + * it turns the toggle on and proceeds, and the toggle keeps its real job
486 + * of gating everything that is NOT a Test press (optimize runs measuring
487 + * their own effect, GTmetrix polling).
488 + *
489 + * @param array<string,mixed> $opts Current module settings.
490 + * @return array<string,mixed> Settings with `enabled` true.
491 + */
492 + private function consent_by_running( array $opts ): array {
493 + if ( empty( $opts['enabled'] ) ) {
494 + Settings_Manager::update( self::SLUG, array( 'enabled' => true ) );
495 + $opts['enabled'] = true;
496 + }
497 + return $opts;
498 + }
499 +
500 + /**
501 + * Start a keyless PSI audit through the Hub, or null when the Hub cannot
502 + * take it and the caller should fall back to the direct anonymous call.
503 + *
504 + * Null — fall back — only for "the Hub was never an option here": not
505 + * connected, PSI not configured on it, or unreachable. A real refusal
506 + * (rate-limited, a run already active) is surfaced, because retrying it
507 + * anonymously would spend the shared quota to report a worse error.
508 + *
509 + * The Hub audits the site's HOME page, so a custom test URL also skips
510 + * this path rather than silently testing a different page than asked.
511 + *
512 + * @return \WP_REST_Response|\WP_Error|null
513 + */
514 + private function start_psi_via_hub( string $url, string $strategy ) {
515 + if ( untrailingslashit( $url ) !== untrailingslashit( (string) home_url( '/' ) ) ) {
516 + return null;
517 + }
518 +
519 + $result = Mcp_Hub::psi_test( $strategy );
520 +
521 + if ( is_wp_error( $result ) ) {
522 + if ( in_array( $result->get_error_code(), array( 'not_connected', 'psi_not_configured', 'hub_unreachable' ), true ) ) {
523 + return null;
524 + }
525 + // A 401/403 means the pairing is dead (revoked, detached, stale
526 + // token) — for THIS feature that is the same as not connected,
527 + // not an error the Test button should wear.
528 + $data = $result->get_error_data();
529 + if ( is_array( $data ) && in_array( (int) ( $data['status'] ?? 0 ), array( 401, 403 ), true ) ) {
530 + return null;
531 + }
532 + return $this->hub_result( $result );
533 + }
534 +
535 + $run_id = isset( $result['run']['id'] ) ? (string) $result['run']['id'] : '';
536 +
537 + // No run id means nothing can ever be polled — writing a marker here
538 + // would orphan it (may_poll() rejects an empty test_id before the
539 + // staleness check, so it would never expire either). A 202 without an
540 + // id is a malformed Hub response; say so rather than pretend a test
541 + // is pending.
542 + if ( '' === $run_id ) {
543 + return new \WP_Error(
544 + 'hub_error',
545 + __( 'xSpeed Hub accepted the test but returned no run id. Please try again.', 'xspeed' ),
546 + array( 'status' => 502 )
547 + );
548 + }
549 +
550 + // The Hub answers 202 before the audit runs; the result arrives via
551 + // the same pending/poll machinery GTmetrix already uses.
552 + update_option(
553 + Score::PENDING_OPTION,
554 + array(
555 + 'test_id' => $run_id,
556 + 'url' => $url,
557 + 'started' => time(),
558 + 'provider' => 'hub-psi',
559 + ),
560 + false
561 + );
562 +
563 + return rest_ensure_response(
564 + array(
565 + 'ok' => true,
566 + 'provider' => 'psi',
567 + 'source' => 'hub',
568 + 'state' => 'queued',
569 + 'test_id' => $run_id,
570 + 'url' => $url,
571 + 'strategy' => $strategy,
572 + 'pending' => true,
573 + )
574 + );
575 + }
576 +
577 + /**
578 + * Poll an in-flight Hub-run PSI audit.
579 + *
580 + * psi_runs() has already copied any finished run into the local history,
581 + * so resolving here is: find our run, see whether it is still going, and
582 + * drop the marker the moment it is not.
583 + *
584 + * @param array<string,mixed> $pending The stored pending marker.
585 + * @return array<string,mixed>|\WP_Error
586 + */
587 + private function poll_hub_psi( array $pending ) {
588 + $result = Mcp_Hub::psi_runs();
589 + if ( is_wp_error( $result ) ) {
590 + return $result;
591 + }
592 +
593 + $mine = null;
594 + foreach ( (array) ( $result['runs'] ?? array() ) as $run ) {
595 + if ( is_array( $run ) && (string) ( $run['id'] ?? '' ) === (string) $pending['test_id'] ) {
596 + $mine = $run;
597 + break;
598 + }
599 + }
600 +
601 + $state = is_array( $mine ) ? (string) ( $mine['status'] ?? '' ) : '';
602 +
603 + if ( 'queued' === $state || 'running' === $state ) {
604 + return array(
605 + 'ok' => true,
606 + 'provider' => 'psi',
607 + 'source' => 'hub',
608 + 'state' => $state,
609 + 'test_id' => (string) $pending['test_id'],
610 + 'pending' => true,
611 + );
612 + }
613 +
614 + // Terminal — done, error, or the Hub no longer lists it at all.
615 + delete_option( Score::PENDING_OPTION );
616 +
617 + if ( 'error' === $state ) {
618 + $row = array(
619 + 'ok' => false,
620 + 'provider' => 'psi',
621 + 'source' => 'hub',
622 + 'state' => 'error',
623 + 'pending' => false,
624 + 'error' => (string) ( $mine['error'] ?? __( 'The audit did not produce a result.', 'xspeed' ) ),
625 + );
626 + Score::record(
627 + array(
628 + 'ok' => false,
629 + 'provider' => 'psi',
630 + 'ts' => time(),
631 + 'url' => (string) ( $pending['url'] ?? '' ),
632 + 'strategy' => 'mobile',
633 + 'score' => null,
634 + 'metrics' => array(),
635 + 'issues' => array(),
636 + 'error' => $row['error'],
637 + 'source' => 'hub',
638 + )
639 + );
640 + return $row;
641 + }
642 +
643 + return array(
644 + 'ok' => true,
645 + 'provider' => 'psi',
646 + 'source' => 'hub',
647 + 'state' => 'completed',
648 + 'pending' => false,
649 + );
650 + }
651 +
652 + /**
444 653 * Poll an in-flight GTmetrix test.
445 654 *
446 655 * GET because it is a read of state we already started — the browser
447 656 * calls it every few seconds while a test is queued.
@@ -449,12 +658,13 @@
449 658 public function rest_status() {
450 659 $opts = Settings_Manager::get( self::SLUG );
451 660 $pending = get_option( Score::PENDING_OPTION, array() );
452 661
453 - // Same opt-in gate as rest_run(). Without it, `status` — which is
454 - // also the CLI's DEFAULT action — polled GTmetrix with the feature
455 - // switched off and no API key, which falsified readme.txt's promise
456 - // that nothing is sent while it is off.
662 + // Same opt-in gate as the rest of the module. Without it, `status` —
663 + // which is also the CLI's DEFAULT action — polled GTmetrix with the
664 + // feature switched off and no API key, which falsified readme.txt's
665 + // promise that nothing is sent while it is off. (A Hub-run test polls
666 + // only the Hub the site is deliberately connected to.)
457 667 if ( ! $this->may_poll( $opts, $pending ) ) {
458 668 return rest_ensure_response(
459 669 array(
460 670 'pending' => false,
@@ -463,19 +673,11 @@
463 673 )
464 674 );
465 675 }
466 676
467 - if ( ! is_array( $pending ) || empty( $pending['test_id'] ) ) {
468 - return rest_ensure_response(
469 - array(
470 - 'pending' => false,
471 - 'state' => 'idle',
472 - 'latest' => Score::latest(),
473 - )
474 - );
475 - }
476 -
477 - $polled = Score::poll_gtmetrix( (string) $opts['gtmetrix_api_key'] );
677 + $polled = 'hub-psi' === ( $pending['provider'] ?? '' )
678 + ? $this->poll_hub_psi( $pending )
679 + : Score::poll_gtmetrix( (string) $opts['gtmetrix_api_key'] );
478 680 if ( is_wp_error( $polled ) ) {
479 681 return $polled;
480 682 }
481 683
@@ -497,33 +699,35 @@
497 699 );
498 700 }
499 701
500 702 /**
501 - * May we contact GTmetrix to poll the in-flight test?
703 + * May we contact anyone to poll the in-flight test?
502 704 *
503 - * Three conditions, all necessary: the feature is on, an API key exists
504 - * (there is no anonymous GTmetrix), and the pending marker is real and
505 - * not stale. A marker with no expiry turned one failed start into a
506 - * permanent poll loop against a third party.
705 + * The pending marker must be real and not stale — a marker with no expiry
706 + * turned one failed start into a permanent poll loop against a third
707 + * party. Beyond that, who we may poll depends on who ran the test: a
708 + * GTmetrix test needs the feature on and an API key (there is no
709 + * anonymous GTmetrix); a Hub-run test needs only the Hub connection the
710 + * site already has — the Hub is not a third party the toggle guards.
507 711 *
508 712 * @param array<string,mixed> $opts Module settings.
509 713 * @param mixed $pending The stored pending marker.
510 714 */
511 715 private function may_poll( array $opts, $pending ): bool {
512 - if ( empty( $opts['enabled'] ) || '' === trim( (string) $opts['gtmetrix_api_key'] ) ) {
513 - return false;
514 - }
515 716 if ( ! is_array( $pending ) || empty( $pending['test_id'] ) ) {
516 717 return false;
517 718 }
518 - // A GTmetrix test that hasn't resolved within the window is not going
519 - // to; drop the marker rather than poll it forever.
719 + // A test that hasn't resolved within the window is not going to;
720 + // drop the marker rather than poll it forever.
520 721 $started = isset( $pending['started'] ) ? (int) $pending['started'] : 0;
521 722 if ( $started > 0 && ( time() - $started ) > Score::PENDING_MAX_AGE ) {
522 723 delete_option( Score::PENDING_OPTION );
523 724 return false;
524 725 }
525 - return true;
726 + if ( 'hub-psi' === ( $pending['provider'] ?? '' ) ) {
727 + return '' !== Mcp_Pairing::site_token();
728 + }
729 + return ! empty( $opts['enabled'] ) && '' !== trim( (string) $opts['gtmetrix_api_key'] );
526 730 }
527 731
528 732 /**
529 733 * Fall back to the home page when no URL is configured — testing "my
@@ -610,8 +814,15 @@
610 814 'description' => 'Force a new scan instead of reusing a recent cached report.',
611 815 'optional' => true,
612 816 ),
613 817 array(
818 + 'type' => 'assoc',
819 + 'name' => 'strategy',
820 + 'description' => 'Which Lighthouse runs to spend: both (default; desktop graded as the headline, mobile graded alongside), desktop, or mobile. One device costs half the engine quota and becomes the headline.',
821 + 'optional' => true,
822 + 'options' => array( 'both', 'desktop', 'mobile' ),
823 + ),
824 + array(
614 825 'type' => 'flag',
615 826 'name' => 'wait',
616 827 'description' => 'Poll until the scan finishes instead of returning immediately.',
617 828 'optional' => true,
@@ -662,9 +873,10 @@
662 873 }
663 874
664 875 $started = Scan::start(
665 876 (string) ( $assoc_args['target'] ?? '' ),
666 - ! empty( $assoc_args['fresh'] )
877 + ! empty( $assoc_args['fresh'] ),
878 + (string) ( $assoc_args['strategy'] ?? 'both' )
667 879 );
668 880 if ( is_wp_error( $started ) ) {
669 881 \WP_CLI::error( $started->get_error_message() );
670 882 return;
@@ -734,20 +946,30 @@
734 946 );
735 947 foreach ( (array) ( $r['dimensions'] ?? array() ) as $key => $d ) {
736 948 \WP_CLI::log( sprintf( ' %-9s %3s/100 (%s of %s pts)', $key, $d['score'] ?? '-', $d['earned'] ?? '-', $d['weight'] ?? '-' ) );
737 949 }
738 - $lh = $r['measured']['lighthouse'] ?? null;
950 + // Read the PER-DEVICE fields, never `measured.lighthouse`: that one is
951 + // the graded run's score, which is desktop on a default scan, so
952 + // printing it as "mobile" reported desktop under a mobile label.
953 + $lhm = $r['measured']['lighthouse_mobile'] ?? null;
739 954 $lhd = $r['measured']['lighthouse_desktop'] ?? null;
740 - if ( null !== $lh || null !== $lhd ) {
741 - // Both strategies: the engine measures both and they diverge
742 - // widely, so reporting only mobile states the harsher number as
743 - // though it were the whole picture. Labelled, and never as "the
744 - // score": different scale.
955 + if ( null !== $lhm || null !== $lhd ) {
956 + // Name only the devices actually measured. A desktop-only scan
957 + // never ran mobile, and a dash there reads as "scored zero"
958 + // rather than "not run".
959 + $parts = array();
960 + if ( null !== $lhd ) {
961 + $parts[] = 'desktop ' . $lhd . '/100';
962 + }
963 + if ( null !== $lhm ) {
964 + $parts[] = 'mobile ' . $lhm . '/100';
965 + }
966 + $graded = (string) ( $r['device'] ?? '' );
745 967 \WP_CLI::log(
746 968 sprintf(
747 - 'Lighthouse: mobile %s, desktop %s - a different scale, one check inside the score above.',
748 - null === $lh ? '-' : $lh . '/100',
749 - null === $lhd ? '-' : $lhd . '/100'
969 + 'Lighthouse: %s - a different scale, one check inside the score above.%s',
970 + implode( ', ', $parts ),
971 + '' !== $graded ? ' Graded on ' . $graded . '.' : ''
750 972 )
751 973 );
752 974 }
753 975 \WP_CLI::log( 'Report: ' . ( $r['report_url'] ?? '' ) );
@@ -804,12 +1026,11 @@
804 1026 return;
805 1027 }
806 1028
807 1029 if ( 'run' === $action ) {
808 - if ( empty( $opts['enabled'] ) ) {
809 - \WP_CLI::error( 'External scores are turned off. Enable the score module first — this is the only feature that contacts a third party.' );
810 - return;
811 - }
1030 + // Running the command IS the opt-in — same consent rule as the
1031 + // dashboard's Test button (#425).
1032 + $opts = $this->consent_by_running( $opts );
812 1033
813 1034 $url = $this->resolve_url( isset( $assoc['target'] ) ? (string) $assoc['target'] : '', $opts );
814 1035 $provider = isset( $assoc['provider'] ) ? (string) $assoc['provider'] : (string) $opts['provider'];
815 1036
@@ -823,10 +1044,25 @@
823 1044 return;
824 1045 }
825 1046
826 1047 $strategy = isset( $assoc['strategy'] ) ? (string) $assoc['strategy'] : (string) $opts['default_strategy'];
827 - $run = Score::run_psi( $url, $strategy, (string) $opts['psi_api_key'] );
1048 + $api_key = (string) $opts['psi_api_key'];
828 1049
1050 + // No key → prefer the Hub, same ladder as rest_run() (#426).
1051 + if ( '' === trim( $api_key ) ) {
1052 + $via_hub = $this->start_psi_via_hub( $url, $strategy );
1053 + if ( $via_hub instanceof \WP_Error ) {
1054 + \WP_CLI::error( $via_hub->get_error_message() );
1055 + return;
1056 + }
1057 + if ( null !== $via_hub ) {
1058 + \WP_CLI::success( 'PageSpeed audit started via xSpeed Hub. Poll with: wp xspeed score status' );
1059 + return;
1060 + }
1061 + }
1062 +
1063 + $run = Score::run_psi( $url, $strategy, $api_key );
1064 +
829 1065 if ( empty( $run['ok'] ) ) {
830 1066 \WP_CLI::error( (string) $run['error'] );
831 1067 return;
832 1068 }
@@ -844,15 +1080,24 @@
844 1080
845 1081 // status
846 1082 $pending = get_option( Score::PENDING_OPTION, array() );
847 1083 if ( $this->may_poll( $opts, $pending ) ) {
848 - $polled = Score::poll_gtmetrix( (string) $opts['gtmetrix_api_key'] );
1084 + $polled = 'hub-psi' === ( $pending['provider'] ?? '' )
1085 + ? $this->poll_hub_psi( $pending )
1086 + : Score::poll_gtmetrix( (string) $opts['gtmetrix_api_key'] );
849 1087 if ( is_wp_error( $polled ) ) {
850 1088 \WP_CLI::error( $polled->get_error_message() );
851 1089 return;
852 1090 }
853 1091 if ( ! empty( $polled['pending'] ) ) {
854 - \WP_CLI::log( sprintf( 'GTmetrix test %s is %s.', (string) $pending['test_id'], (string) ( $polled['state'] ?? 'running' ) ) );
1092 + \WP_CLI::log(
1093 + sprintf(
1094 + '%s test %s is %s.',
1095 + 'hub-psi' === ( $pending['provider'] ?? '' ) ? 'PageSpeed (Hub)' : 'GTmetrix',
1096 + (string) $pending['test_id'],
1097 + (string) ( $polled['state'] ?? 'running' )
1098 + )
1099 + );
855 1100 return;
856 1101 }
857 1102 }
858 1103