class-filesystem.php

Filesystem boundary — directories, local-path resolution, minify path policy, atomic writes, verified PHP writes, and cache-path containment guards.

Source includes/Support/class-filesystem.php11 min readPart of Performance Optimisation

includes/Support/class-filesystem.php

Filesystem boundary — directories, local-path resolution, minify path policy, atomic writes, verified PHP writes, and cache-path containment guards.

Namespace: PerformanceOptimise\\Inc · Lines: 2423

Class Filesystem

Class Filesystem

Source: includes/Support/class-filesystem.php, line 55

final class Filesystem

Tags: @since 2.4.0

Properties

PropertyVisibilityTypeDefaultLine
$normalized_host_cacheprivate staticarrayarray()66
$minify_roots_cacheprivate staticarrayarray()81
$purge_fallback_memoprivate staticarrayarray()98
$home_url_cacheprivate staticarrayarray()2364

publicstatic clear_purge_fallback_memo()

public static function clear_purge_fallback_memo(=null $blog_id): void

Clear the purge-fallback gate memo (testing isolation, settings save).

ParameterTypeDefaultDescription
$blog_id=null—Optional blog ID to clear. Null clears all.

Return: void.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 114

publicstatic prepare_cache_dir()

public static function prepare_cache_dir($cache_dir): bool

Recursively creates cache directory if not exists.

ParameterTypeDefaultDescription
$cache_dirstring—Path to the cache directory.

Return: bool — True if created or exists, false otherwise.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 129

publicstatic init_filesystem()

public static function init_filesystem()

Initializes the WP_Filesystem API.

Return: mixed — WP_Filesystem_Base|false The filesystem object or false on failure.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 182

publicstatic get_local_path()

public static function get_local_path(string $url): string

Gets the local file path from a URL.

ParameterTypeDefaultDescription
$urlstring—The URL to process.

Return: string — The local file path.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 203

publicstatic get_minify_allowed_roots()

public static function get_minify_allowed_roots(): array

Gets the allow-listed filesystem roots for minify/combine file serving.

Return: string[] — Normalized absolute root paths.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 288

publicstatic reset_minify_roots_cache()

public static function reset_minify_roots_cache(): void

Reset the minify-roots memo (testing isolation).

Return: void.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 373

publicstatic is_minify_path_allowed()

public static function is_minify_path_allowed($path): bool

Whether a minify/combine source path is allowed to be read.

ParameterTypeDefaultDescription
$pathmixed—Candidate filesystem path.

Return: bool — True when the path resolves inside an allowed root.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 395

publicstatic validate_minify_path()

public static function validate_minify_path($path): string

Validates a minify/combine source path and returns its resolved form.

ParameterTypeDefaultDescription
$pathmixed—Candidate filesystem path.

Return: string — Resolved allowed path, or \’\’ when rejected.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 409

publicstatic get_js_css_minified_file()

public static function get_js_css_minified_file()

Gets the number of minified JS and CSS files.

Return: array — Associative array with counts for JS and CSS files.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 474

publicstatic normalize_cache_host()

public static function normalize_cache_host(string $raw_host): string

Normalize a raw host value into a safe cache-key domain.

ParameterTypeDefaultDescription
$raw_hoststring—Raw host value (e.g. $_SERVER[\’HTTP_HOST\’] or a home_url() host).

Return: string — Normalized lowercase host, or \’\’ when invalid.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 531

privatestatic memoize_normalized_host()

private static function memoize_normalized_host(string $raw_host, string $normalized): string

Store a normalized host in the per-value memo (bounded size).

ParameterTypeDefaultDescription
$raw_hoststring—Raw input key.
$normalizedstring—Normalized result.

Return: string — The normalized result (passthrough for `return` sites).

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 606

publicstatic reset_normalized_host_cache()

public static function reset_normalized_host_cache(): void

Reset the normalized-host memo (testing isolation).

Return: void.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 623

publicstatic sanitize_cache_url_path()

public static function sanitize_cache_url_path(?string $url_path, ?string=null $allowed_host): string

Sanitize a URL path for cache file mapping.

ParameterTypeDefaultDescription
$url_path?string—Raw URL path or URL.
$allowed_host?string=null—Optional canonical host (alias: $domain / $canonical_host at call-sites); same-host absolute URLs map to their path, others refuse.

Return: string — Sanitized relative path or empty string.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 660

publicstatic is_cache_path_contained()

public static function is_cache_path_contained(string $cache_root_dir, string $domain, string $path): bool

Whether an absolute path stays inside the cache tree.

ParameterTypeDefaultDescription
$cache_root_dirstring—Absolute cache root directory.
$domainstring—Canonical domain directory segment.
$pathstring—Absolute file or directory path to check.

Return: bool — True when contained.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 797

publicstatic is_realpath_contained()

public static function is_realpath_contained(string $cache_root_dir, string $domain, string $path): bool

Symlink-aware containment check for cache write targets.

ParameterTypeDefaultDescription
$cache_root_dirstring—Absolute cache root directory.
$domainstring—Canonical domain directory segment.
$pathstring—Absolute file or directory path to check.

Return: bool — True when contained.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 864

publicstatic resolve_realpath()

public static function resolve_realpath(string $lexical_path): ?string

Resolve a path via realpath(), keeping the lexical remainder.

ParameterTypeDefaultDescription
$lexical_pathstring—Normalized absolute path to resolve.

Return: string|null — Resolved absolute path, or null when unresolvable.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 977

publicstatic validate_cache_write_path()

public static function validate_cache_write_path(string $cache_root_dir, string $domain, string $path): bool

Single-call validator for absolute cache write targets.

ParameterTypeDefaultDescription
$cache_root_dirstring—Absolute cache root directory.
$domainstring—Canonical domain directory segment.
$pathstring—Absolute file path to validate.

Return: bool — True when the target may be written.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1034

publicstatic is_htaccess_path_allowed()

public static function is_htaccess_path_allowed(string $htaccess_file): bool

Whether an .htaccess target may be written by the plugin.

ParameterTypeDefaultDescription
$htaccess_filestring—Absolute .htaccess path candidate.

Return: bool — True when the target may be written.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1064

publicstatic sanitize_cache_path()

public static function sanitize_cache_path(string $cache_root_dir, string $domain, $url_path_or_url, string $filename): string

Build a contained absolute cache file path from its parts.

ParameterTypeDefaultDescription
$cache_root_dirstring—Absolute cache root directory.
$domainstring—Canonical domain directory segment.
$url_path_or_urlstring|null—Raw URL path or URL.
$filenamestring—File name (e.g. `index.html`).

Return: string — Contained absolute path, or \’\’ when refused.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1158

publicstatic atomic_tmp_path()

public static function atomic_tmp_path(string $final_path): string

Build a unique sibling tmp path for atomic writes.

ParameterTypeDefaultDescription
$final_pathstring—Final file path the tmp sits beside.

Return: string — Tmp sibling path (\’\’ when input is empty).

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1242

publicstatic atomic_file_put_contents()

public static function atomic_file_put_contents($fs, string $path, string $contents): bool

Atomically write contents via tmp-file + rename.

ParameterTypeDefaultDescription
$fsmixed—Filesystem object exposing put_contents()/move()/delete().
$pathstring—Final file path.
$contentsstring—File contents.

Return: bool — True on success.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1275

publicstatic verify_php_syntax()

public static function verify_php_syntax(string $code, string=\'\' $tmp_file_for_lint): bool

Check that PHP code parses without a syntax error.

ParameterTypeDefaultDescription
$codestring—PHP source to check.
$tmp_file_for_lintstring=\'\'—Optional tmp file holding $code for `php -l`.

Return: bool — True when the code looks parseable.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1324

privatestatic php_brackets_balanced()

private static function php_brackets_balanced(array $tokens): bool

Check that structural brackets are balanced in a token stream.

ParameterTypeDefaultDescription
$tokensarray—Token stream from `PhpToken::tokenize()`.

Return: bool — True when every bracket type is balanced and ordered.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1416

publicstatic atomic_write_php_verified()

public static function atomic_write_php_verified($fs, string $path, string $contents, =null $expect): ?bool

Atomically write PHP source with syntax verification and rollback.

ParameterTypeDefaultDescription
$fsmixed—Filesystem object exposing exists()/get_contents()/put_contents()/move()/copy()/delete().
$pathstring—Final file path.
$contentsstring—New file contents.
$expect=null—Optional assertion receiving contents, returning bool.

Return: bool|null — True on verified success, false on verified failure, null when unsupported.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1474

privatestatic restore_php_backup()

private static function restore_php_backup($fs, string $path, string $original, int $chmod): bool

Restore a PHP file from its in-memory original or `.wppo-bak` backup.

ParameterTypeDefaultDescription
$fsmixed—Filesystem object.
$pathstring—Final file path.
$originalstring—In-memory original contents (\’\’ when none).
$chmodint—File mode for a direct-write restore.

Return: bool — True when a restore write/copy was issued.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1587

privatestatic delete_php_backup()

private static function delete_php_backup($fs, string $path): void

Best-effort deletion of the `.wppo-bak` backup beside a PHP file.

ParameterTypeDefaultDescription
$fsmixed—Filesystem object.
$pathstring—Final file path (backup is `$path.wppo-bak`).

Return: void.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1625

publicstatic is_purge_fallback_enabled()

public static function is_purge_fallback_enabled(): bool

Whether the post-purge last-good fallback is enabled.

Return: bool — True when purge-fallback retention/serving is active.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1650

publicstatic get_purge_fallback_path_for()

public static function get_purge_fallback_path_for(string $file_path): string

Map a derived asset path to its sibling last-good fallback path.

ParameterTypeDefaultDescription
$file_pathstring—Absolute derived-asset path.

Return: string — Sibling fallback path, or \’\’ when not applicable.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1716

publicstatic retain_purge_fallback_file()

public static function retain_purge_fallback_file($fs, callable $is_allowed, string $file_path): void

Retain a last-good fallback copy before a derived file is purged.

ParameterTypeDefaultDescription
$fsobject—Filesystem exposing exists()/size()/copy()/get_contents()/put_contents().
$is_allowedcallable—Containment validator: fn( string $path ): bool.
$file_pathstring—The derived file about to be deleted.

Return: void.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1800

publicstatic is_purge_fallback_payload_valid()

public static function is_purge_fallback_payload_valid($fs, string $fallback): bool

Whether a retained fallback file holds a servable payload.

ParameterTypeDefaultDescription
$fsobject—Filesystem exposing size()/get_contents().
$fallbackstring—Absolute fallback path.

Return: bool — True when the fallback exists with non-empty content.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1898

publicstatic get_staged_path_for()

public static function get_staged_path_for(string $file_path): string

Map a derived CSS/JS file to its sibling staged-rollout path.

ParameterTypeDefaultDescription
$file_pathstring—Absolute live derived-file path.

Return: string — Sibling staged path, or \’\’ when not applicable.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 1949

publicstatic promote_staged_file()

public static function promote_staged_file($fs, callable $is_allowed, string $live_path): bool

Promote a staged-rollout file over its live sibling (issue #1348).

ParameterTypeDefaultDescription
$fsobject—Filesystem exposing exists()/size()/get_contents()/move()/copy()/delete().
$is_allowedcallable—Containment validator: fn( string $path ): bool.
$live_pathstring—Absolute live derived-file path.

Return: bool — True when the staged file replaced the live file.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2029

publicstatic restore_fallback_file()

public static function restore_fallback_file($fs, callable $is_allowed, string $live_path): bool

Restore a derived file from its retained last-good fallback (issue #1348).

ParameterTypeDefaultDescription
$fsobject—Filesystem exposing copy()/delete() plus the fallback-validity surface.
$is_allowedcallable—Containment validator: fn( string $path ): bool.
$live_pathstring—Absolute live derived-file path.

Return: bool — True when the fallback payload replaced the live file.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2106

publicstatic describe_rollout_slot()

public static function describe_rollout_slot($fs, string $live_path): array

Describe the safe-rollout slot triple for a live file (issue #1348).

ParameterTypeDefaultDescription
$fsobject—Filesystem exposing exists()/size()/get_contents().
$live_pathstring—Absolute live derived-file path.

Return: array{live_bytes: — int, live_checksum: string, staged: bool, staged_bytes: int, staged_checksum: string, staged_changed: bool, fallback: bool} Slot description.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2158

publicstatic purge_fallback_should_log()

public static function purge_fallback_should_log(): bool

A single blog-prefixed transient (`wppo_purge_fallback_served`) gates all fallback-serve log rows (both Cache and used-CSS share it) so a post-purge miss storm writes one row per day instead of one per directory.

Return: bool — True when the caller should write its log row.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2259

publicstatic css_file_valid()

public static function css_file_valid(string $path): bool

Whether a generated CSS file is valid (exists, readable, non-empty).

ParameterTypeDefaultDescription
$pathstring—Absolute path to the CSS file.

Return: bool — True when the file is usable.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2290

publicstatic log_css_fallback()

public static function log_css_fallback(string $reason, array $handles, string $context): void

Log a guarded CSS fallback (combine or used-CSS) event with throttling.

ParameterTypeDefaultDescription
$reasonstring—Machine-readable reason code (empty_payload, write_failure, head_match_failure, …).
$handlesarray—Handles preserved by the fallback.
$contextstring—\’combine\’ or \’usedcss\’ — selects the log-key prefix and message.

Return: void.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2314

privatestatic home_url_for_local_path()

private static function home_url_for_local_path(): string

Resolve the untrailed home URL with a per-blog memo.

Return: string — Untrailed home URL, or \’\’ when unavailable.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2372

publicstatic reset_home_url_cache()

public static function reset_home_url_cache(): void

Reset the home-URL memo (testing isolation / switch_to_blog).

Return: void.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2404

privatestatic min_cache_dir_for_counts()

private static function min_cache_dir_for_counts(): string

Current site\’s blog-scoped minify cache directory.

Return: string — Normalized absolute path to the site-scoped min cache dir.

Tags: @since 2.4.0

Source: includes/Support/class-filesystem.php, line 2418

Hooks

Hooks referenced in includes/Support/class-filesystem.php:

HookTypeLineNotes
wppo_minify_allowed_rootsfilter333—
wppo_allow_php_lintfilter1367—
wppo_purge_fallback_enabledfilter1683@param ×1