class-pagespeed.php

Pagespeed Class

Source includes/Insight/class-pagespeed.php5 min readPart of Performance Optimisation

includes/Insight/class-pagespeed.php

Pagespeed Class

Namespace: PerformanceOptimise\\Inc · Lines: 988

Class Pagespeed

Class Pagespeed

Source: includes/Insight/class-pagespeed.php, line 32

class Pagespeed

Tags: @since 1.6.0

Constants

ConstantVisibilityValueLine
API_ENDPOINTpublic\'https://www.googleapis.com/pagespeedonline/v5/runPagespeed\'40
AS_HOOKpublic\'wppo_pagespeed_scan\'48
AS_GROUPpublic\'performance_optimisation\'56
TRANSIENT_TTLpublicDAY_IN_SECONDS64
TREND_OPTIONpublic\'wppo_web_vitals_trends\'72
TREND_LIMITpublic3080
TREND_MAX_KEYSpublic2091
TREND_LOCK_OPTIONpublic\'wppo_web_vitals_trends_lock\'98
TREND_LOCK_TTLpublic60105

publicstatic queue_scan()

public static function queue_scan(string $url, string=\'mobile\' $strategy): int

Queue a PageSpeed scan as an async background job.

ParameterTypeDefaultDescription
$urlstring—The URL to scan.
$strategystring=\'mobile\'—Either \’mobile\’ or \’desktop\’.

Return: int — Action Scheduler job ID.

Tags: @since 1.6.0

Source: includes/Insight/class-pagespeed.php, line 119

privatestatic find_pending_job_id()

private static function find_pending_job_id(array $args): int

Find the pending Action Scheduler job ID for the given args.

ParameterTypeDefaultDescription
$argsarray—Action arguments.

Return: int — Pending job ID, or 0 when none is found.

Tags: @since 2.2.0

Source: includes/Insight/class-pagespeed.php, line 167

publicstatic run_scan()

public static function run_scan(array $args): void

Execute the PageSpeed API call.

ParameterTypeDefaultDescription
$argsarray—url: string, strategy: string }.

Return: void.

Tags: @since 1.6.0

Source: includes/Insight/class-pagespeed.php, line 220

publicstatic get_results()

public static function get_results(string $url, string=\'mobile\' $strategy)

Retrieve cached PageSpeed results for a URL and strategy.

ParameterTypeDefaultDescription
$urlstring—The scanned URL.
$strategystring=\'mobile\'—Either \’mobile\’ or \’desktop\’.

Return: array|false — Prepared result array, or false if not ready.

Tags: @since 1.6.0

Source: includes/Insight/class-pagespeed.php, line 397

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).

ParameterTypeDefaultDescription
$query_urlstring—Fully built API URL.
$timeoutint—Request timeout in seconds.

Return: array|WP_Error — Response array or error object.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 417

privatestatic get_retry_delay()

private static function get_retry_delay(): int

Get the re-queue delay (seconds) after a transport failure.

Return: int — Delay clamped to 1-10 seconds.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 432

privatestatic store_failure()

private static function store_failure(string $url, string $strategy, string $message): void

Persist a scan failure for later surfacing in the admin UI.

ParameterTypeDefaultDescription
$urlstring—Scanned URL.
$strategystring—Strategy slug.
$messagestring—Failure message.

Return: void.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 452

publicstatic get_transient_key()

public static function get_transient_key(string $url, string $strategy): string

Build the transient key for a URL + strategy combination.

ParameterTypeDefaultDescription
$urlstring—The scanned URL.
$strategystring—Either \’mobile\’ or \’desktop\’.

Return: string — Transient key.

Tags: @since 1.6.0

Source: includes/Insight/class-pagespeed.php, line 471

privatestatic get_api_key()

private static function get_api_key(): string

Read the PageSpeed API key from plugin settings.

Return: string — API key, or empty string if not configured.

Tags: @since 1.6.0

Source: includes/Insight/class-pagespeed.php, line 484

publicstatic record_trend()

public static function record_trend(string $url, array $prepared, string=\'mobile\' $strategy): void

Record a PageSpeed result into the Web Vitals trend history.

ParameterTypeDefaultDescription
$urlstring—The scanned URL.
$preparedarray—The prepared PageSpeed result array.
$strategystring=\'mobile\'—Either \’mobile\’ or \’desktop\’.

Return: void.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 504

privatestatic acquire_trend_lock()

private static function acquire_trend_lock(): bool

Acquire the cross-request trend write lock.

Return: bool.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 554

privatestatic release_trend_lock()

private static function release_trend_lock(): void

Release the trend write lock.

Return: void.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 574

private static function prune_trends(array $hist): array

Enforce a global cap on the number of URL + strategy keys.

ParameterTypeDefaultDescription
$histarray—The full trends map.

Return: array — The trimmed trends map.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 589

public static function get_trends(): array

Retrieve the full Web Vitals trend history.

Return: array — Keyed by md5(url)_strategy, each value a list of snapshots.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 627

public static function migrate_trends_autoload(): void

Migrate the trends option to non-autoloading.

Return: void.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 642

privatestatic prepare_response()

private static function prepare_response(array $response): array

Extract and normalise the fields we need from the raw Lighthouse response.

ParameterTypeDefaultDescription
$responsearray—Decoded JSON response from the PageSpeed API.

Return: array — Prepared result array.

Tags: @since 1.6.0

Source: includes/Insight/class-pagespeed.php, line 673

privatestatic sanitize_audit_details()

private static function sanitize_audit_details($details): array

Whitelist and slice raw Lighthouse audit details.

ParameterTypeDefaultDescription
$detailsmixed—Raw details array from the API.

Return: array — Sanitized details.

Tags: @since 2.2.0

Source: includes/Insight/class-pagespeed.php, line 771

privatestatic sanitize_snippet_for_lcp()

private static function sanitize_snippet_for_lcp(string $snippet): string

Sanitize an LCP snippet while preserving the <img src> the extract_lcp_image_url() fallback regexes for.

ParameterTypeDefaultDescription
$snippetstring—Raw snippet from the API.

Return: string — Sanitized snippet.

Tags: @since 2.2.0

Source: includes/Insight/class-pagespeed.php, line 834

privatestatic extract_lcp_image_url()

private static function extract_lcp_image_url(array $diagnostics): ?string

Extract the LCP image URL from PageSpeed diagnostic audit data.

ParameterTypeDefaultDescription
$diagnosticsarray—The prepared diagnostics array.

Return: string|null — The image URL, or null if not found.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 859

publicstatic store_lcp_image_url()

public static function store_lcp_image_url(string $url, array $prepared, string=\'mobile\' $strategy): void

Store the LCP image URL per page so it can be used for auto-preloading.

ParameterTypeDefaultDescription
$urlstring—The scanned URL.
$preparedarray—The prepared PageSpeed result array.
$strategystring=\'mobile\'—The scan strategy (\’mobile\’ or \’desktop\’).

Return: void.

Tags: @since 2.0.0

Source: includes/Insight/class-pagespeed.php, line 910

privatestatic normalise_url_for_compare()

private static function normalise_url_for_compare(string $url): string

Normalise a URL for front-page comparison.

ParameterTypeDefaultDescription
$urlstring—Raw URL.

Return: string — Sanitised URL without empty query/fragment.

Tags: @since 2.2.0

Source: includes/Insight/class-pagespeed.php, line 956

Hooks

Hooks referenced in includes/Insight/class-pagespeed.php:

HookTypeLineNotes
wppo_pagespeed_request_timeoutfilter285@param ×1
wppo_pagespeed_retry_delayfilter439@param ×1