class-bfcache.php

Bfcache for logged-in users — Instant Back/Forward.

Source includes/Cache/class-bfcache.php3 min readPart of Performance Optimisation

includes/Cache/class-bfcache.php

Bfcache for logged-in users — Instant Back/Forward.

Namespace: PerformanceOptimise\\Inc · Lines: 490

Class Bfcache

Bfcache handler.

Source: includes/Cache/class-bfcache.php, line 35

class Bfcache

Tags: @since 2.0.0

Constants

ConstantVisibilityValueLine
SESSION_KEYpublic\'bfcache_session_token\'43
FILTER_ENABLEDpublic\'wppo_bfcache_enabled\'51
SCRIPT_HANDLEpublic\'wppo-bfcache\'59

Properties

PropertyVisibilityTypeDefaultLine
$invalidation_scriptprivate staticstring\'\'71
$script_stagedprivate staticboolfalse82

publicstatic is_enabled()

public static function is_enabled(): bool

Whether bfcache handling is enabled.

Return: bool — True when bfcache is enabled.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 98

public static function get_cookie_name(): string

Get the cookie name for the bfcache session token.

Return: string — Cookie name.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 122

publicstatic generate_token()

public static function generate_token(): string

Generate a bfcache session token.

Return: string — Token (43 chars).

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 136

publicstatic get_user_token()

public static function get_user_token(?int=null $user_id): ?string$session_token=null):?string

Get the bfcache session token for a user/session.

ParameterTypeDefaultDescription
$user_id?int=null—User ID, defaults to current.
$session_tokenstring|null—Session token, defaults to current.

Return: string|null — Token or null.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 151

publicstatic attach_session_information()

public static function attach_session_information($session): array

Attach bfcache session information on login.

ParameterTypeDefaultDescription
$sessionmixed—Session array.

Return: array — Session array.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 192

private static function is_logged_in_cookie_secure(int $user_id): bool

Whether the logged_in cookie should be secure (copied from core logic).

ParameterTypeDefaultDescription
$user_idint—User ID.

Return: bool.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 214

public static function set_token_cookie(int $user_id, string $token, int $expire): void

Set the bfcache session token cookie.

ParameterTypeDefaultDescription
$user_idint—User ID.
$tokenstring—Token.
$expireint—Expiration timestamp.

Return: void.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 231

public static function on_set_logged_in_cookie(string $logged_in_cookie, int $expire, int $expiration, int $user_id, string $scheme, string $token): void

Hook: set_logged_in_cookie — mirror the session token into a readable cookie.

ParameterTypeDefaultDescription
$logged_in_cookiestring—Cookie value (unused).
$expireint—Expiration.
$expirationint—Expiration.
$user_idint—User ID.
$schemestring—Scheme.
$tokenstring—Session token.

Return: void.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 255

public static function on_clear_auth_cookie(): void

Hook: clear_auth_cookie — clear the bfcache session token cookie on logout.

Return: void.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 271

publicstatic filter_nocache_headers()

public static function filter_nocache_headers($headers): array

Filter nocache_headers to allow bfcache while preserving privacy.

ParameterTypeDefaultDescription
$headersmixed—Headers array.

Return: array — Headers.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 296

publicstatic enqueue_scripts()

public static function enqueue_scripts(): void

Enqueue/print the bfcache invalidation script for authenticated pages.

Return: void.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 348

publicstatic print_invalidation_script()

public static function print_invalidation_script(): void

Print the staged bfcache invalidation script.

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-bfcache.php, line 431

publicstatic reset_runtime_state()

public static function reset_runtime_state(): void

Reset staged bfcache state for the next request.

Return: void.

Tags: @since NEXT

Source: includes/Cache/class-bfcache.php, line 453

publicstatic reset_state_for_tests()

public static function reset_state_for_tests(): void

Reset staged bfcache state (tests / compatibility helper).

Return: void.

Tags: @since 2.2.0

Source: includes/Cache/class-bfcache.php, line 464

publicstatic init()

public static function init(): void

Register hooks.

Return: void.

Tags: @since 2.0.0

Source: includes/Cache/class-bfcache.php, line 474