← All changes
|
includes/integrations/class-google-pagespeed-client.php
+26
-1
2.2.0
→
2.12.0
View file →
| @@ -211,13 +211,32 @@ | ||
| 211 | 211 | set_transient('thinkrank_psi_failure_' . $hash, $e->getMessage(), self::FAILURE_TTL); |
| 212 | 212 | throw $e; |
| 213 | 213 | } |
| 214 | 214 | |
| 215 | + // Lighthouse answers 200 with a populated lighthouseResult even when the | |
| 216 | + // audit itself failed (NO_FCP, ERRORED_DOCUMENT_REQUEST, …); the score | |
| 217 | + // then comes back null. Coercing that to 0 stored a failed run as a | |
| 218 | + // genuine "this site scores 0" measurement, which every consumer — | |
| 219 | + // the SEO score's mobile factor most visibly — has no way to tell from | |
| 220 | + // a real result. Treat it as the failure it is so the caller's existing | |
| 221 | + // failure handling applies. | |
| 222 | + $runtime_error = $result['lighthouseResult']['runtimeError']['code'] ?? ''; | |
| 223 | + $raw_score = $result['lighthouseResult']['categories']['performance']['score'] ?? null; | |
| 224 | + | |
| 225 | + if (('' !== $runtime_error && 'NO_ERROR' !== $runtime_error) || null === $raw_score) { | |
| 226 | + $message = $result['lighthouseResult']['runtimeError']['message'] | |
| 227 | + ?? __('PageSpeed Insights returned no performance score for this URL.', 'thinkrank'); | |
| 228 | + | |
| 229 | + set_transient('thinkrank_psi_failure_' . $hash, $message, self::FAILURE_TTL); | |
| 230 | + | |
| 231 | + throw new \Exception($message); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped | |
| 232 | + } | |
| 233 | + | |
| 215 | 234 | $snapshot = [ |
| 216 | 235 | 'core_web_vitals' => $this->parse_core_web_vitals($result), |
| 217 | 236 | 'opportunities' => $this->parse_opportunities($result), |
| 218 | 237 | 'diagnostics' => $this->parse_diagnostics($result), |
| 219 | - 'performance_score' => (float) (($result['lighthouseResult']['categories']['performance']['score'] ?? 0) * 100), | |
| 238 | + 'performance_score' => (float) ($raw_score * 100), | |
| 220 | 239 | 'loading_experience' => $result['loadingExperience'] ?? [], |
| 221 | 240 | 'fetched_at' => time(), |
| 222 | 241 | ]; |
| 223 | 242 | |
| @@ -249,10 +268,16 @@ | ||
| 249 | 268 | 'test_url' => $test_url, |
| 250 | 269 | 'performance_score' => $performance_score |
| 251 | 270 | ]; |
| 252 | 271 | } catch (\Exception $e) { |
| 272 | + // `message` as well as `error`: the success branch above reports | |
| 273 | + // its outcome as `message`, and every consumer reads that key, so | |
| 274 | + // a failure that only set `error` was reported to the admin as an | |
| 275 | + // error with no reason at all (#852). `error` is kept for any | |
| 276 | + // caller that already reads it. | |
| 253 | 277 | return [ |
| 254 | 278 | 'success' => false, |
| 279 | + 'message' => $e->getMessage(), | |
| 255 | 280 | 'error' => $e->getMessage() |
| 256 | 281 | ]; |
| 257 | 282 | } |
| 258 | 283 | } |