includes/Insight/class-pagespeed.php
Pagespeed Class
Class Pagespeed
Class Pagespeed
class PagespeedConstants
| Constant | Visibility | Value | Line |
|---|---|---|---|
API_ENDPOINT | public | \'https://www.googleapis.com/pagespeedonline/v5/runPagespeed\' | 40 |
AS_HOOK | public | \'wppo_pagespeed_scan\' | 48 |
AS_GROUP | public | \'performance_optimisation\' | 56 |
TRANSIENT_TTL | public | DAY_IN_SECONDS | 64 |
TREND_OPTION | public | \'wppo_web_vitals_trends\' | 72 |
TREND_LIMIT | public | 30 | 80 |
TREND_MAX_KEYS | public | 20 | 91 |
TREND_LOCK_OPTION | public | \'wppo_web_vitals_trends_lock\' | 98 |
TREND_LOCK_TTL | public | 60 | 105 |
publicstatic queue_scan()
public static function queue_scan(string $url, string=\'mobile\' $strategy): intQueue a PageSpeed scan as an async background job. Parameter Type Default Description $urlstring— The URL to scan. $strategystring=\'mobile\'— Either \’mobile\’ or \’desktop\’.
Return: int — Action Scheduler job ID.
privatestatic find_pending_job_id()
private static function find_pending_job_id(array $args): intFind the pending Action Scheduler job ID for the given args. Parameter Type Default Description $argsarray— Action arguments.
Return: int — Pending job ID, or 0 when none is found.
publicstatic run_scan()
public static function run_scan(array $args): voidExecute the PageSpeed API call. Parameter Type Default Description $argsarray— url: string, strategy: string }.
Return: void.
publicstatic get_results()
public static function get_results(string $url, string=\'mobile\' $strategy)Retrieve cached PageSpeed results for a URL and strategy. Parameter Type Default Description $urlstring— The scanned URL. $strategystring=\'mobile\'— Either \’mobile\’ or \’desktop\’.
Return: array|false — Prepared result array, or false if not ready.
privatestatic request_pagespeed_api()
private static function request_pagespeed_api(string $query_url, int $timeout)Perform the PageSpeed API request (single attempt, no blocking retry). Parameter Type Default Description $query_urlstring— Fully built API URL. $timeoutint— Request timeout in seconds.
Return: array|WP_Error — Response array or error object.
privatestatic get_retry_delay()
private static function get_retry_delay(): intGet the re-queue delay (seconds) after a transport failure.
Return: int — Delay clamped to 1-10 seconds.
privatestatic store_failure()
private static function store_failure(string $url, string $strategy, string $message): voidPersist a scan failure for later surfacing in the admin UI. Parameter Type Default Description $urlstring— Scanned URL. $strategystring— Strategy slug. $messagestring— Failure message.
Return: void.
publicstatic get_transient_key()
public static function get_transient_key(string $url, string $strategy): stringBuild the transient key for a URL + strategy combination. Parameter Type Default Description $urlstring— The scanned URL. $strategystring— Either \’mobile\’ or \’desktop\’.
Return: string — Transient key.
privatestatic get_api_key()
private static function get_api_key(): stringRead the PageSpeed API key from plugin settings.
Return: string — API key, or empty string if not configured.
publicstatic record_trend()
public static function record_trend(string $url, array $prepared, string=\'mobile\' $strategy): voidRecord a PageSpeed result into the Web Vitals trend history. Parameter Type Default Description $urlstring— The scanned URL. $preparedarray— The prepared PageSpeed result array. $strategystring=\'mobile\'— Either \’mobile\’ or \’desktop\’.
Return: void.
privatestatic acquire_trend_lock()
private static function acquire_trend_lock(): boolAcquire the cross-request trend write lock.
Return: bool.
privatestatic release_trend_lock()
private static function release_trend_lock(): voidRelease the trend write lock.
Return: void.
privatestatic prune_trends()
private static function prune_trends(array $hist): arrayEnforce a global cap on the number of URL + strategy keys. Parameter Type Default Description $histarray— The full trends map.
Return: array — The trimmed trends map.
publicstatic get_trends()
public static function get_trends(): arrayRetrieve the full Web Vitals trend history.
Return: array — Keyed by md5(url)_strategy, each value a list of snapshots.
publicstatic migrate_trends_autoload()
public static function migrate_trends_autoload(): voidMigrate the trends option to non-autoloading.
Return: void.
privatestatic prepare_response()
private static function prepare_response(array $response): arrayExtract and normalise the fields we need from the raw Lighthouse response. Parameter Type Default Description $responsearray— Decoded JSON response from the PageSpeed API.
Return: array — Prepared result array.
privatestatic sanitize_audit_details()
private static function sanitize_audit_details($details): arrayWhitelist and slice raw Lighthouse audit details. Parameter Type Default Description $detailsmixed— Raw details array from the API.
Return: array — Sanitized details.
privatestatic sanitize_snippet_for_lcp()
private static function sanitize_snippet_for_lcp(string $snippet): stringSanitize an LCP snippet while preserving the <img src> the extract_lcp_image_url() fallback regexes for. Parameter Type Default Description $snippetstring— Raw snippet from the API.
Return: string — Sanitized snippet.
privatestatic extract_lcp_image_url()
private static function extract_lcp_image_url(array $diagnostics): ?stringExtract the LCP image URL from PageSpeed diagnostic audit data. Parameter Type Default Description $diagnosticsarray— The prepared diagnostics array.
Return: string|null — The image URL, or null if not found.
publicstatic store_lcp_image_url()
public static function store_lcp_image_url(string $url, array $prepared, string=\'mobile\' $strategy): voidStore the LCP image URL per page so it can be used for auto-preloading. Parameter Type Default Description $urlstring— The scanned URL. $preparedarray— The prepared PageSpeed result array. $strategystring=\'mobile\'— The scan strategy (\’mobile\’ or \’desktop\’).
Return: void.
privatestatic normalise_url_for_compare()
private static function normalise_url_for_compare(string $url): stringNormalise a URL for front-page comparison. Parameter Type Default Description $urlstring— Raw URL.
Return: string — Sanitised URL without empty query/fragment.
Hooks
Hooks referenced in includes/Insight/class-pagespeed.php: Hook Type Line Notes wppo_pagespeed_request_timeoutfilter 285 @param ×1 wppo_pagespeed_retry_delayfilter 439 @param ×1