| @@ -254,9 +254,12 @@ | ||
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | $started = Scan::start( |
| 257 | 257 | (string) ( $request->get_param( 'url' ) ?? '' ), |
| 258 | - (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' ) | |
| 259 | 262 | ); |
| 260 | 263 | if ( is_wp_error( $started ) ) { |
| 261 | 264 | return $started; |
| 262 | 265 | } |
| @@ -293,8 +296,9 @@ | ||
| 293 | 296 | 'status' => 'running', |
| 294 | 297 | 'scan_id' => $polled['scan_id'], |
| 295 | 298 | 'step' => $polled['step'], |
| 296 | 299 | 'latest' => Scan::latest(), |
| 300 | + 'stored' => Scan::stored_state(), | |
| 297 | 301 | 'visibility' => Scan::visibility(), |
| 298 | 302 | 'private_supported' => Scan::private_supported(), |
| 299 | 303 | ) |
| 300 | 304 | ); |
| @@ -302,8 +306,9 @@ | ||
| 302 | 306 | return rest_ensure_response( |
| 303 | 307 | array( |
| 304 | 308 | 'status' => 'complete', |
| 305 | 309 | 'latest' => $polled, |
| 310 | + 'stored' => 'ok', | |
| 306 | 311 | 'visibility' => Scan::visibility(), |
| 307 | 312 | 'private_supported' => Scan::private_supported(), |
| 308 | 313 | ) |
| 309 | 314 | ); |
| @@ -312,8 +317,9 @@ | ||
| 312 | 317 | return rest_ensure_response( |
| 313 | 318 | array( |
| 314 | 319 | 'status' => 'idle', |
| 315 | 320 | 'latest' => Scan::latest(), |
| 321 | + 'stored' => Scan::stored_state(), | |
| 316 | 322 | 'visibility' => Scan::visibility(), |
| 317 | 323 | 'private_supported' => Scan::private_supported(), |
| 318 | 324 | ) |
| 319 | 325 | ); |
| @@ -811,8 +817,15 @@ | ||
| 811 | 817 | 'description' => 'Force a new scan instead of reusing a recent cached report.', |
| 812 | 818 | 'optional' => true, |
| 813 | 819 | ), |
| 814 | 820 | array( |
| 821 | + 'type' => 'assoc', | |
| 822 | + 'name' => 'strategy', | |
| 823 | + '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.', | |
| 824 | + 'optional' => true, | |
| 825 | + 'options' => array( 'both', 'desktop', 'mobile' ), | |
| 826 | + ), | |
| 827 | + array( | |
| 815 | 828 | 'type' => 'flag', |
| 816 | 829 | 'name' => 'wait', |
| 817 | 830 | 'description' => 'Poll until the scan finishes instead of returning immediately.', |
| 818 | 831 | 'optional' => true, |
| @@ -863,9 +876,10 @@ | ||
| 863 | 876 | } |
| 864 | 877 | |
| 865 | 878 | $started = Scan::start( |
| 866 | 879 | (string) ( $assoc_args['target'] ?? '' ), |
| 867 | - ! empty( $assoc_args['fresh'] ) | |
| 880 | + ! empty( $assoc_args['fresh'] ), | |
| 881 | + (string) ( $assoc_args['strategy'] ?? 'both' ) | |
| 868 | 882 | ); |
| 869 | 883 | if ( is_wp_error( $started ) ) { |
| 870 | 884 | \WP_CLI::error( $started->get_error_message() ); |
| 871 | 885 | return; |
| @@ -914,8 +928,12 @@ | ||
| 914 | 928 | return; |
| 915 | 929 | } |
| 916 | 930 | |
| 917 | 931 | $latest = Scan::latest(); |
| 932 | + if ( null === $latest && 'unreadable' === Scan::stored_state() ) { | |
| 933 | + \WP_CLI::log( 'The last scan report could not be read back from the database and was discarded. Run `wp xspeed scan run --wait` again.' ); | |
| 934 | + return; | |
| 935 | + } | |
| 918 | 936 | if ( null === $latest ) { |
| 919 | 937 | \WP_CLI::log( 'No scan yet. Run `wp xspeed scan run --wait`.' ); |
| 920 | 938 | return; |
| 921 | 939 | } |
| @@ -935,20 +953,30 @@ | ||
| 935 | 953 | ); |
| 936 | 954 | foreach ( (array) ( $r['dimensions'] ?? array() ) as $key => $d ) { |
| 937 | 955 | \WP_CLI::log( sprintf( ' %-9s %3s/100 (%s of %s pts)', $key, $d['score'] ?? '-', $d['earned'] ?? '-', $d['weight'] ?? '-' ) ); |
| 938 | 956 | } |
| 939 | - $lh = $r['measured']['lighthouse'] ?? null; | |
| 957 | + // Read the PER-DEVICE fields, never `measured.lighthouse`: that one is | |
| 958 | + // the graded run's score, which is desktop on a default scan, so | |
| 959 | + // printing it as "mobile" reported desktop under a mobile label. | |
| 960 | + $lhm = $r['measured']['lighthouse_mobile'] ?? null; | |
| 940 | 961 | $lhd = $r['measured']['lighthouse_desktop'] ?? null; |
| 941 | - if ( null !== $lh || null !== $lhd ) { | |
| 942 | - // Both strategies: the engine measures both and they diverge | |
| 943 | - // widely, so reporting only mobile states the harsher number as | |
| 944 | - // though it were the whole picture. Labelled, and never as "the | |
| 945 | - // score": different scale. | |
| 962 | + if ( null !== $lhm || null !== $lhd ) { | |
| 963 | + // Name only the devices actually measured. A desktop-only scan | |
| 964 | + // never ran mobile, and a dash there reads as "scored zero" | |
| 965 | + // rather than "not run". | |
| 966 | + $parts = array(); | |
| 967 | + if ( null !== $lhd ) { | |
| 968 | + $parts[] = 'desktop ' . $lhd . '/100'; | |
| 969 | + } | |
| 970 | + if ( null !== $lhm ) { | |
| 971 | + $parts[] = 'mobile ' . $lhm . '/100'; | |
| 972 | + } | |
| 973 | + $graded = (string) ( $r['device'] ?? '' ); | |
| 946 | 974 | \WP_CLI::log( |
| 947 | 975 | sprintf( |
| 948 | - 'Lighthouse: mobile %s, desktop %s - a different scale, one check inside the score above.', | |
| 949 | - null === $lh ? '-' : $lh . '/100', | |
| 950 | - null === $lhd ? '-' : $lhd . '/100' | |
| 976 | + 'Lighthouse: %s - a different scale, one check inside the score above.%s', | |
| 977 | + implode( ', ', $parts ), | |
| 978 | + '' !== $graded ? ' Graded on ' . $graded . '.' : '' | |
| 951 | 979 | ) |
| 952 | 980 | ); |
| 953 | 981 | } |
| 954 | 982 | \WP_CLI::log( 'Report: ' . ( $r['report_url'] ?? '' ) ); |