class-used-css.php

Used_CSS class for removing unused CSS rules per page.

Source includes/CSS/class-used-css.php24 min readPart of Performance Optimisation

includes/CSS/class-used-css.php

Used_CSS class for removing unused CSS rules per page.

Namespace: PerformanceOptimise\\Inc · Lines: 4961

Class Used_CSS

Class Used_CSS

Source: includes/CSS/class-used-css.php, line 25

class Used_CSS

Tags: @since 1.9.0

Constants

ConstantVisibilityValueLine
CACHE_ROOT_DIRprivate\'/cache/wppo\'33
USED_CSS_FILENAMEprivate\'used-css.css\'41
LAST_FULL_REGEN_OPTIONpublic\'wppo_used_css_last_full_regen\'54
FULL_REGEN_COOLDOWN_SECONDSprivate1800065
TARGETED_REGEN_OPTIONpublic\'wppo_used_css_last_targeted_regen\'77
TARGETED_REGEN_COOLDOWN_SECONDSprivate360087
DELIVERY_MODESpublicarray( \'file\', \'delay\', \'async\', \'remove\' )101
STALE_THRESHOLD_SECONDSprivate86400109
DEFAULT_USED_CSS_QUEUE_CAPprivate50121
MAX_USED_CSS_QUEUE_CAPprivate500129
VIEWPORT_VARIANTSpublicarray( \'mobile\', \'desktop\' )141

Properties

PropertyVisibilityTypeDefaultLine
$optionsprivatearray—149
$safelistprivatearrayarray()157
$built_in_safelistprivatearrayarray( \'html\', \'body\', \':root\', \'*\', \'#wpcontent\', \'#wpwrap\', \'#wpadminbar\', \'.ab-\', \'.wp-admin-bar-\', \'.woocommerce-\', \'.wc-\', \'.single-product\', \'.cart-\', \'.current-menu-item\', \'.menu-item-\', \'.page-id-\', \'.postid-\', \'.attachmentid-\', \'.active\', \'.open\', \'.hidden\', \'.visible\', \'.show\', \'.hide\', \'.fade\', \'.collapsed\', \'.selected\', \'.current\', \'.focus\', \'.hover\', \'.visited\', \'.js-\', \'.is-\', \'.has-\', \'.wp-\', \'.admin-bar-\', \'.dashicons-\', \'.customize-\', // Builder / JS-state selectors (issue #966): builders inject // dynamic classes at runtime that the static DOM walk never sees. // Prefix entries ending in \'-\' or \'*\' match via prefix in // is_selector_used(); attribute entries (e.g. [data-elementor-type]) // match by attribute-name substring so compound selectors stay kept. \'.elementor-\', \'.e-con*\', \'.et_*\', \'.et_pb_*\', \'.et-pb-\', \'.bricks-\', \'.brx-\', \'.vc_*\', \'.wpb_*\', \'.oxygen-\', \'.oxy-\', \'.no-js\', \'.js-enabled\', \'[data-elementor-type]\', // Popup/modal selectors (issue #1023): popups render outside the // static DOM walk (hidden containers, JS portals), so purge keeps // Elementor + generic popup selectors by default. \'.elementor-popup-\', \'.e-popup-\', \'.dialog-\', \'.popup-\', \'.modal-\', \'.mfp-\', \'.swal2-\', \'[data-elementor-type=\"popup\"]\', )165
$cache_root_dirprivatestring—242
$cache_root_urlprivatestring—250
$domainprivatestring—263
$host_mismatchprivateboolfalse275
$traversal_probe_loggedprivate staticboolfalse287
$safelist_indexprivate?arraynull298
$selector_split_cacheprivatearrayarray()306
$source_checksum_memoprivate?stringnull317

public __construct()

public function __construct(array=array() $options)

Constructor.

ParameterTypeDefaultDescription
$optionsarray=array()—Plugin options.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 325

public is_host_mismatched()

public function is_host_mismatched(): bool

Whether the request Host header mismatched the canonical home host.

Return: bool — True when the request host differs from the canonical host.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 361

publicstatic get_safelist_presets()

public static function get_safelist_presets(): array

Safelist presets shipped safe-by-default (Elementor + popup selectors).

Return: string[].

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 378

private init_safelist()

private function init_safelist(): void

Initialize safelist from settings and built-in list.

Return: void.

Tags: @since 1.9.0 · @since 2.0.0 Added wppo_used_css_safelist filter (has_filter-guarded).

Source: includes/CSS/class-used-css.php, line 404

public extract_selectors()

public function extract_selectors(string $html): array

Extract used selectors from HTML content.

ParameterTypeDefaultDescription
$htmlstring—The HTML content.

Return: array{tags: — array, classes: array, ids: array, attrs: array}.

Tags: @since 1.9.0 · @since 2.0.0 Added WP_HTML_Processor path with Tag Processor fallback.

Source: includes/CSS/class-used-css.php, line 454

privatestatic extract_selectors_with_processor()

private static function extract_selectors_with_processor(string $html): ?array

Extract used selectors via the WP 6.9+ HTML processor.

ParameterTypeDefaultDescription
$htmlstring—The HTML content.

Return: array{tags: — array, classes: array, ids: array, attrs: array}|null Selector data, or null on failure.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 494

privatestatic collect_selectors_from_tag()

private static function collect_selectors_from_tag(\\WP_HTML_Tag_Processor $tags, array& $used): void

Collect used selectors from the current tag of a HTML processor.

ParameterTypeDefaultDescription
$tags\\WP_HTML_Tag_Processor—Processor positioned on the current tag.
$usedarray&—Selector accumulator (passed by reference).

Return: void.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 533

privatestatic strip_css_comments()

private static function strip_css_comments(string $css): string

Strip CSS comments while preserving quoted segments.

ParameterTypeDefaultDescription
$cssstring—Raw CSS content.

Return: string — CSS without comments.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 589

public parse_css()

public function parse_css(string $css): array

Parse CSS content into structured rules.

ParameterTypeDefaultDescription
$cssstring—Raw CSS content.

Return: array — Parsed rules.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 636

privatestatic find_at_rule_prelude_end()

private static function find_at_rule_prelude_end(string $css, int $offset, int $length): array

Find the end of an at-rule prelude, skipping quoted segments.

ParameterTypeDefaultDescription
$cssstring—Full CSS content.
$offsetint—At-rule start offset (the \’@\’).
$lengthint—Length of $css.

Return: array — Indices 0 (semicolon offset or false) and 1 (brace offset or false).

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 796

privatestatic find_rule_end()

private static function find_rule_end(string $css, int $offset, int $length): int

Find the end offset (one past \’}\’) of a regular rule, skipping quoted segments and backslash escapes.

ParameterTypeDefaultDescription
$cssstring—Full CSS content.
$offsetint—Rule start offset.
$lengthint—Length of $css.

Return: int — Offset one past the closing brace (or $length).

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 835

private parse_css_block_rules()

private function parse_css_block_rules(string $css): array

Parse CSS rules inside a block (e.g., @media) by delegating to parse_css().

ParameterTypeDefaultDescription
$cssstring—Block content.

Return: array — Parsed child rules.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 868

private split_selectors()

private function split_selectors(string $selector_list): array

Split a comma-separated selector list into individual selectors.

ParameterTypeDefaultDescription
$selector_liststring—Comma-separated selectors.

Return: array — Individual selectors.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 879

private get_safelist_index()

private function get_safelist_index(): array

Build the precomputed safelist index (once per instance).

Return: array{exact: — array<string, bool>, attrs: string[], prefixes: string[]}.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 921

private get_cached_simple_selectors()

private function get_cached_simple_selectors(string $selector): array

Cached split of a selector into simple parts.

ParameterTypeDefaultDescription
$selectorstring—Selector string.

Return: string[] — Simple selector parts.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 968

public is_selector_used()

public function is_selector_used(string $selector, array $used): bool

Check if a CSS selector matches any used element in the HTML.

ParameterTypeDefaultDescription
$selectorstring—A single CSS selector.
$usedarray—Used selectors from extract_selectors().

Return: bool — True if the selector is used.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 994

private extract_simple_selectors()

private function extract_simple_selectors(string $selector): array

Extract simple selector parts from a compound selector.

ParameterTypeDefaultDescription
$selectorstring—A CSS selector.

Return: array — Simple selector parts.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1066

private matches_simple_selector()

private function matches_simple_selector(string $simple, array $used): bool

Check if a simple selector matches used elements.

ParameterTypeDefaultDescription
$simplestring—A simple CSS selector (e.g., \”.class\”, \”#id\”, \”tag\”).
$usedarray—Used selectors.

Return: bool — True if matched.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1121

public purge_css()

public function purge_css(array $parsed_css, array $used): string

Purge unused CSS rules from parsed CSS.

ParameterTypeDefaultDescription
$parsed_cssarray—Parsed CSS from parse_css().
$usedarray—Used selectors from extract_selectors().

Return: string — Purged CSS content.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1214

private is_rule_used()

private function is_rule_used(array $rule, array $used): bool

Check if a single CSS rule is used.

ParameterTypeDefaultDescription
$rulearray—A parsed rule.
$usedarray—Used selectors.

Return: bool — True if the rule is used.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1257

public generate_used_css()

public function generate_used_css(string $html, array $css_assets): string

Generate used CSS for a given HTML content and CSS assets.

ParameterTypeDefaultDescription
$htmlstring—The page HTML.
$css_assetsarray—Array of CSS content strings (keyed by handle).

Return: string — Purged CSS content, or the unprocessed combined CSS when purging is unavailable.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1285

publicstatic sanitize_used_css_output()

public static function sanitize_used_css_output(string $css): string

Sanitize + bound derived CSS before it is served or stored (issue #1347).

ParameterTypeDefaultDescription
$cssstring—Derived CSS content.

Return: string — Safe CSS, or \’\’ when refused.

Tags: @since 2.3.0

Source: includes/CSS/class-used-css.php, line 1338

public get_used_css_path()

public function get_used_css_path(string=\'\' $url): string

Get the used-CSS cache file path for a URL.

ParameterTypeDefaultDescription
$urlstring=\'\'—The page URL.

Return: string — The filesystem path, or \’\’ when refused.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1370

public get_used_css_url()

public function get_used_css_url(string=\'\' $url): string

Get the used-CSS cache file URL for a URL.

ParameterTypeDefaultDescription
$urlstring=\'\'—The page URL.

Return: string — The public URL, or \’\’ when refused.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1411

private get_url_path()

private function get_url_path(string=\'\' $url): string

Get the normalized URL path for cache storage.

ParameterTypeDefaultDescription
$urlstring=\'\'—The page URL.

Return: string — Normalized path, or \’\’ when refused/empty.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1452

private is_raw_path_non_blank()

private function is_raw_path_non_blank(string $url): bool

Whether the raw path component behind a sanitized-\’\’ result is non-blank.

ParameterTypeDefaultDescription
$urlstring—The original URL (\’\’ = current REQUEST_URI).

Return: bool — True when the raw path component is non-blank.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 1484

private is_path_contained()

private function is_path_contained(string $path): bool

Whether an absolute path stays inside the used-CSS cache tree.

ParameterTypeDefaultDescription
$pathstring—Absolute file or directory path.

Return: bool — True when contained.

Tags: @since 2.0.0 · @since 2.2.0 Added realpath symlink containment via Util::validate_cache_write_path().

Source: includes/CSS/class-used-css.php, line 1513

private log_traversal_probe()

private function log_traversal_probe(string $raw_input): void

Log a blocked used-CSS path traversal probe (once per request).

ParameterTypeDefaultDescription
$raw_inputstring—The hostile input that was rejected.

Return: void.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 1536

public save_used_css()

public function save_used_css(string $css, string=\'\' $url): bool

Save used-CSS content for a URL.

ParameterTypeDefaultDescription
$cssstring—The purged CSS content.
$urlstring=\'\'—The page URL.

Return: bool — True on success.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 1577

public stage_used_css()

public function stage_used_css(string $css, string=\'\' $url): array

Stage used-CSS output for safe-rollout preview (issue #1348).

ParameterTypeDefaultDescription
$cssstring—Raw used-CSS content.
$urlstring=\'\'—The page URL.

Return: array{staged: — bool, reason: string, bytes: int, checksum: string, live_bytes: int, live_checksum: string, changed: bool} Preview metadata.

Tags: @since 2.3.0

Source: includes/CSS/class-used-css.php, line 1672

public promote_staged_used_css()

public function promote_staged_used_css(string=\'\' $url): bool

Promote staged used-CSS over the live file (issue #1348).

ParameterTypeDefaultDescription
$urlstring=\'\'—The page URL.

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

Tags: @since 2.3.0

Source: includes/CSS/class-used-css.php, line 1756

public rollback_used_css_to_fallback()

public function rollback_used_css_to_fallback(string=\'\' $url, string=\'manual\' $reason): bool

Roll back used-CSS to the retained last-good fallback (issue #1348).

ParameterTypeDefaultDescription
$urlstring=\'\'—The page URL.
$reasonstring=\'manual\'—Machine-readable reason recorded in the activity log.

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

Tags: @since 2.3.0

Source: includes/CSS/class-used-css.php, line 1817

public get_used_css_rollout_status()

public function get_used_css_rollout_status(string=\'\' $url): array

Describe the used-CSS safe-rollout slot for a URL (issue #1348).

ParameterTypeDefaultDescription
$urlstring=\'\'—The page URL.

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

Source: includes/CSS/class-used-css.php, line 1874

public verify_used_css_health()

public function verify_used_css_health(string=\'\' $url): array

Verify used-CSS health for a URL, auto-restoring last-good (issue #1348).

ParameterTypeDefaultDescription
$urlstring=\'\'—The page URL.

Return: array{status: — string, reason: string, bytes: int} One of healthy|restored|degraded.

Tags: @since 2.3.0

Source: includes/CSS/class-used-css.php, line 1905

public compute_css_checksum()

public function compute_css_checksum(string $css): string

Stable content hash of CSS source (issue #1038).

ParameterTypeDefaultDescription
$cssstring—CSS content.

Return: string — SHA-256 checksum, or \’\’ for empty input.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 1950

private get_checksum_path()

private function get_checksum_path(string $used_css_path): string

Sidecar path holding the source checksum for a used-CSS file.

ParameterTypeDefaultDescription
$used_css_pathstring—Used-CSS file path.

Return: string — Checksum sidecar path, or \’\’ when refused.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 1965

public compute_local_source_checksum()

public function compute_local_source_checksum(): string

Combined checksum of the locally-available queued stylesheets.

Return: string — Combined checksum, or \’\’ when unavailable.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 1991

public reset_source_checksum_memo()

public function reset_source_checksum_memo(): void

Reset the memoized local-source checksum.

Return: void.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 2058

private is_checksum_stale()

private function is_checksum_stale(string $used_css_path): bool

Whether the cached used-CSS is stale by content checksum.

ParameterTypeDefaultDescription
$used_css_pathstring—Used-CSS file path.

Return: bool — True when the source changed since generation.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 2074

private persist_source_checksum()

private function persist_source_checksum(string $used_css_path): void

Persist the source checksum sidecar after a successful generation.

ParameterTypeDefaultDescription
$used_css_pathstring—Used-CSS file path.

Return: void.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 2106

publicstatic get_purge_fallback_path()

public static function get_purge_fallback_path(string $file_path): string

Map a used-CSS path to its sibling last-good fallback path.

ParameterTypeDefaultDescription
$file_pathstring—Absolute used-CSS file path.

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

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2142

private retain_purge_fallback()

private function retain_purge_fallback(string $file_path): void

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

ParameterTypeDefaultDescription
$file_pathstring—The used-CSS file about to be deleted.

Return: void.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2175

public delete_used_css()

public function delete_used_css(=null $url): bool

Delete used-CSS file(s) for a URL or all URLs.

ParameterTypeDefaultDescription
$url=null—Optional URL to delete specific page used-CSS.

Return: bool — True on success.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 2209

private delete_variant_files_for_url()

private function delete_variant_files_for_url(string $url): void

Delete the viewport-variant sidecars for a URL (issue #1220).

ParameterTypeDefaultDescription
$urlstring—Page URL.

Return: void.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2270

publicstatic delete_all_used_css()

public static function delete_all_used_css(): bool

Delete all used-CSS files across all domains.

Return: bool — True on success.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 2314

publicstatic purge_coupled()

public static function purge_coupled(=null $url_path): array

Purge page cache and used CSS together via a single shared action.

ParameterTypeDefaultDescription
$url_path=null—Optional URL path for a single-page purge; null purges all.

Return: array{page_cache: — bool, used_css: bool} Per-store results.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 2404

private get_full_regen_cooldown()

private function get_full_regen_cooldown(): int

Effective full-regeneration cooldown in seconds (issue #1107).

Return: int — Cooldown seconds (>= 0).

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2440

private is_full_regen_cooled_down()

private function is_full_regen_cooled_down(): bool

Whether a non-forced full regeneration is inside the cooldown window (issue #1107).

Return: bool — True when the last full regen is newer than the cooldown.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2476

private mark_full_regen()

private function mark_full_regen(): void

Record a completed full-regeneration scan (issue #1107).

Return: void.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2502

publicstatic get_used_css_delivery_mode()

public static function get_used_css_delivery_mode(?array=null $file_opts): string

Effective used-CSS delivery mode (issue #1220).

ParameterTypeDefaultDescription
$file_opts?array=null—Optional file_optimisation settings (defaults to plugin settings).

Return: string — One of self::DELIVERY_MODES.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2522

publicstatic get_last_full_regen_time()

public static function get_last_full_regen_time(): int

Timestamp of the last full used-CSS regeneration (issue #1220).

Return: int — Unix timestamp, or 0 when never recorded.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2547

publicstatic get_staleness_info()

public static function get_staleness_info(): array

Staleness signal for the admin UI (issue #1220).

Return: array{last_regen:int,last_regen_human:string,is_stale:bool,cooldown_remaining:int,delivery_mode:string}.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2570

private get_targeted_regen_cooldown()

private function get_targeted_regen_cooldown(): int

Effective targeted-regeneration cooldown in seconds (issue #1220).

Return: int — Cooldown seconds (>= 0).

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2620

privatestatic get_effective_targeted_cooldown()

private static function get_effective_targeted_cooldown(): int

Static variant of the targeted-regen cooldown (issue #1220).

Return: int — Cooldown seconds (>= 0).

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2634

private is_targeted_regen_cooled_down()

private function is_targeted_regen_cooled_down(): bool

Whether a targeted regeneration is inside the cooldown window (issue #1220).

Return: bool — True when the last targeted regen is newer than the cooldown.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2667

private mark_targeted_regen()

private function mark_targeted_regen(): void

Record a targeted-regeneration pass (issue #1220).

Return: void.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2691

publicstatic request_targeted_regen()

public static function request_targeted_regen(string=\'\' $reason, int=20 $cap): int

Cooldown-gated targeted requeue after builder/theme updates (issue #1220).

ParameterTypeDefaultDescription
$reasonstring=\'\'—Short reason for logging (e.g. \’builder-update\’).
$capint=20—Maximum posts to requeue in this pass.

Return: int — Number of jobs queued.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2717

private is_variant_fresh_for_post()

private function is_variant_fresh_for_post(int $post_id, string $modified_gmt, ?string=null $permalink): bool

Whether the stored used-CSS variant is fresh for a post (issue #1107).

ParameterTypeDefaultDescription
$post_idint—Post ID.
$modified_gmtstring—Post modification time (GMT, Y-m-d H:i:s).
$permalink?string=null—Optional pre-resolved permalink (scan path passes its batch map so the URL is resolved once per post, not twice).

Return: bool — True when regeneration can be skipped for this post.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2861

privatestatic is_used_css_job_live()

private static function is_used_css_job_live(array $args): bool

Whether a used-CSS generation job is pending or running.

ParameterTypeDefaultDescription
$argsarray—Action arguments.

Return: bool — True when a matching job is pending or running.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 2921

publicstatic requeue_for_post()

public static function requeue_for_post(int $post_id, ?array=null $scheduled_hints, ?self=null $instance, ?bool&=null $already_scheduled): bool

Queue used-CSS regeneration for a single post (builder-drift requeue).

ParameterTypeDefaultDescription
$post_idint—Post ID to requeue.
$scheduled_hints?array=null—Optional hoisted pending-job map (post ID => true); null falls back to per-post lookup.
$instance?self=null—Optional hoisted instance (shares parsed safelist/memo across loop calls).
$already_scheduled?bool&=null—Optional out flag: set to true when the job was already pending (hint-hit, pre-check hit, or lost unique-race) rather than newly scheduled; false when a new job was inserted. Untouched (stays false) on skip/failure. Lets bulk callers count only genuinely new jobs (issue #1310).

Return: bool — True when a job was queued or already scheduled; false when skipped as fresh or on failure.

Tags: @since 2.0.0 · @since 2.2.0 Optional $scheduled_hints for batched targeted regen. · @since 2.2.0 Optional $instance to avoid per-post re-construction. · @since 2.2.0 Optional $already_scheduled out flag distinguishing already-pending from newly-scheduled.

Source: includes/CSS/class-used-css.php, line 2971

publicstatic order_post_ids_by_rum_priority()

public static function order_post_ids_by_rum_priority(array $post_ids, array=array() $permalinks, ?array=null $priority, ?array=null $trends): array

Order post IDs worst-p75 LCP first for used-CSS queue prioritization.

ParameterTypeDefaultDescription
$post_idsarray—Post IDs in FIFO order.
$permalinksarray=array()——
$priority?array=null—Optional hoisted RUM path-LCP priority map (regenerate_all fetches once per run; null fetches per call).
$trends?array=null—Optional hoisted PageSpeed trends (null fetches per call).

Return: int[] — Ordered post IDs (same entries).

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 3063

publicstatic get_used_css_queue_cap()

public static function get_used_css_queue_cap(): int

Read the configured per-run used-CSS queue cap (issue #1164).

Return: int — Per-run cap, or PHP_INT_MAX when uncapped.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3160

publicstatic is_viewport_variants_enabled()

public static function is_viewport_variants_enabled(): bool

Whether viewport-split used-CSS variants are enabled (issue #1164).

Return: bool — True when split variants should be emitted/served.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3191

publicstatic get_variant_filename()

public static function get_variant_filename(string $variant): string

Get the viewport-split variant filename for a base filename.

ParameterTypeDefaultDescription
$variantstring—Variant slug (\’mobile\’|\’desktop\’).

Return: string — Variant filename, or \’\’ when refused.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3215

public get_used_css_variant_path()

public function get_used_css_variant_path(string=\'\' $url, string=\'mobile\' $variant): string

Get the used-CSS variant file path for a URL (issue #1164).

ParameterTypeDefaultDescription
$urlstring=\'\'—Page URL.
$variantstring=\'mobile\'—Variant slug (\’mobile\’|\’desktop\’).

Return: string — Filesystem path, or \’\’ when refused/disabled.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3233

public resolve_used_css_path()

public function resolve_used_css_path(string=\'\' $url, string=\'mobile\' $variant): string

Resolve the usable used-CSS path with fail-open fallback (issue #1164).

ParameterTypeDefaultDescription
$urlstring=\'\'—Page URL.
$variantstring=\'mobile\'—Variant slug (\’mobile\’|\’desktop\’).

Return: string — Usable filesystem path, or \’\’ when none.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3263

publicstatic is_safe_to_strip_handle()

public static function is_safe_to_strip_handle(string $handle): bool

Whether a stylesheet handle is safe to strip when a block-library stylesheet is present (issue #1164).

ParameterTypeDefaultDescription
$handlestring—Stylesheet handle.

Return: bool — True when stripping is safe; false for block-library.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3308

publicstatic passes_elementor_smoke()

public static function passes_elementor_smoke(string $html, string $purged_css): bool

Elementor smoke check for used-CSS output (issue #1164).

ParameterTypeDefaultDescription
$htmlstring—Page HTML.
$purged_cssstring—Purged CSS candidate.

Return: bool — True when it is safe to serve the purged CSS.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3337

public regenerate_all()

public function regenerate_all(bool=false $force): int

Queue background used-CSS regeneration for all published posts.

ParameterTypeDefaultDescription
$forcebool=false—Bypass the cooldown (explicit operator paths: builder purge after a wipe, manual triggers). Per-post freshness still applies.

Return: int — Number of jobs queued.

Tags: @since 1.9.0 · @since 2.2.0 Added $force parameter, cooldown, and per-post freshness skip. · @since 2.2.0 Capped per-run queue with RUM-worst-first ordering (issue #1164).

Source: includes/CSS/class-used-css.php, line 3383

publicstatic get_excluded_post_types()

public static function get_excluded_post_types(): array

Builder-template post types excluded from used-CSS generation (issue #1274).

Return: string[] — Excluded post type slugs.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3693

publicstatic is_excluded_post()

public static function is_excluded_post(int $post_id): bool

Whether a post ID belongs to an excluded builder-template type (issue #1274).

ParameterTypeDefaultDescription
$post_idint—Post ID.

Return: bool — True when the post should be skipped.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 3726

publicstatic process_background()

public static function process_background(int $post_id, =null $sig): void

Process a single page for used-CSS generation (Action Scheduler callback).

ParameterTypeDefaultDescription
$post_idint—The post ID.
$sig=null—Optional HMAC signature over the payload.

Return: void.

Tags: @since 1.9.0 · @since 2.3.0 Accepts and verifies the optional HMAC signature. · @since 2.3.0 Documents the provenance-only residual and logs unsigned jobs throttled at WP_DEBUG level.

Source: includes/CSS/class-used-css.php, line 3779

privatestatic fetch_and_generate_used_css()

private static function fetch_and_generate_used_css(int $post_id, string $permalink, int $timeout): string

Fetch a permalink and generate its used CSS (issue #1348).

ParameterTypeDefaultDescription
$post_idint—The post ID (log context only).
$permalinkstring—Same-site-validated permalink to fetch.
$timeoutint—Fetch timeout in seconds.

Return: string — Purged CSS, or \’\’ on any failure.

Tags: @since 2.3.0

Source: includes/CSS/class-used-css.php, line 3880

public generate_preview_for_post()

public function generate_preview_for_post(int $post_id): array

Dry-run used-CSS generation for a post with staged preview (issue #1348).

ParameterTypeDefaultDescription
$post_idint—The post ID.

Return: array{staged: — bool, reason: string, bytes: int, checksum: string, live_bytes: int, live_checksum: string, changed: bool, post_id: int} Preview metadata.

Tags: @since 2.3.0

Source: includes/CSS/class-used-css.php, line 3937

privatestatic extract_css_assets_from_html()

private static function extract_css_assets_from_html(string $html): array

Extract CSS assets from HTML content by finding <link rel=\”stylesheet\”> tags.

ParameterTypeDefaultDescription
$htmlstring—The HTML content.

Return: array — Array of CSS content strings keyed by md5 hash of URL.

Tags: @since 1.9.0 · @since 2.0.0 Added WP_HTML_Processor path with Tag Processor fallback.

Source: includes/CSS/class-used-css.php, line 4006

privatestatic extract_css_assets_with_processor()

private static function extract_css_assets_with_processor(string $html): ?array

Extract stylesheet URLs via the WP 6.9+ HTML processor.

ParameterTypeDefaultDescription
$htmlstring—The HTML content.

Return: array<string,string>|null — CSS contents keyed by URL hash, or null on failure.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4038

privatestatic collect_css_asset_from_tag()

private static function collect_css_asset_from_tag(\\WP_HTML_Tag_Processor $tags, array& $assets): void

Collect the stylesheet content linked by the current tag.

ParameterTypeDefaultDescription
$tags\\WP_HTML_Tag_Processor—Processor positioned on the current tag.
$assetsarray&—Asset accumulator (passed by reference).

Return: void.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4075

privatestatic fetch_css_content_static()

private static function fetch_css_content_static(string $url)

Fetch CSS content from a URL or local path (static version).

ParameterTypeDefaultDescription
$urlstring—The CSS URL.

Return: string|false — CSS content or false on failure.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 4109

public get_all_css_assets()

public function get_all_css_assets(): array

Get all CSS assets (content) from enqueued styles.

Return: array — Array of CSS content strings keyed by handle.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 4132

private fetch_css_content()

private function fetch_css_content(string $url)

Fetch CSS content from a URL or local path (delegates to static helper).

ParameterTypeDefaultDescription
$urlstring—The CSS URL.

Return: string|false — CSS content or false on failure.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 4166

private function strip_stylesheet_links(string $buffer, array $quoted_srcs): ?array

Strip stylesheet <link> tags for the given quoted srcs (order-agnostic, bounded).

ParameterTypeDefaultDescription
$bufferstring—The HTML buffer.
$quoted_srcsarray—preg_quote()d src URLs (delimiter \’/\’).

Return: array|null — Array [ string $stripped, int $strip_count ] or null on PCRE error.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4191

private resolve_effective_delivery_mode()

private function resolve_effective_delivery_mode(string $buffer_after_strip, string $mode, array $handles): string

Smoke-gate the remove delivery mode (issue #1220).

ParameterTypeDefaultDescription
$buffer_after_stripstring—Buffer with original links stripped.
$modestring—Requested delivery mode.
$handlesarray—Stripped stylesheet handles.

Return: string — Effective delivery mode.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 4247

private build_delayed_css_loader()

private function build_delayed_css_loader(): string

Inline loader that swaps interaction-delayed stylesheets (issue #1220).

Return: string — Inline script tag.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 4302

private get_handle_url_with_ver()

private function get_handle_url_with_ver(string $handle): string

Registered stylesheet URL with its version query (issue #1220).

ParameterTypeDefaultDescription
$handlestring—Style handle.

Return: string — Full URL with ver query, or \’\’ when unresolvable.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 4322

private get_handle_media()

private function get_handle_media(string $handle): string

Registered stylesheet media (issue #1220).

ParameterTypeDefaultDescription
$handlestring—Style handle.

Return: string — Media attribute value.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 4353

private has_strict_csp()

private function has_strict_csp(string=\'\' $buffer): bool

Whether the response carries a strict CSP blocking inline handlers (issue #1220).

ParameterTypeDefaultDescription
$bufferstring=\'\'—Optional HTML buffer to scan for a meta CSP tag.

Return: bool — True when a strict CSP is detected.

Tags: @since 2.2.0

Source: includes/CSS/class-used-css.php, line 4384

private inject_used_css()

private function inject_used_css(string $buffer, string $used_css_url, array $handles): string

Inject used-CSS into the buffer: remove original <link> stylesheets and insert the used-CSS file with a <noscript> fallback.

ParameterTypeDefaultDescription
$bufferstring—The HTML buffer.
$used_css_urlstring—The URL of the used-CSS file (with version).
$handlesarray—Array of style handles to remove and include in fallback.

Return: string — Modified HTML buffer.

Tags: @since 1.9.0 · @since 2.2.0 Added file/delay/async/remove delivery modes.

Source: includes/CSS/class-used-css.php, line 4428

public is_regression_guard_enabled()

public function is_regression_guard_enabled(): bool

Whether the visual regression guard is enabled (issue #966).

Return: bool.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4657

public get_regression_threshold()

public function get_regression_threshold(): int

Retained-% threshold below which trimming is deemed a mismatch.

Return: int.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4673

public is_regression_guard_tripped()

public function is_regression_guard_tripped(string $combined_css, string $purged_css): bool

Whether purged output trips the visual regression guard.

ParameterTypeDefaultDescription
$combined_cssstring—Full combined stylesheet.
$purged_cssstring—Purged output.

Return: bool — True when the guard trips (serve the full stylesheet).

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4699

private is_safe_fallback_enabled()

private function is_safe_fallback_enabled(): bool

Whether the safe CSS combine fallback is enabled.

Return: bool.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4724

private log_used_css_fallback()

private function log_used_css_fallback(string $reason, array $handles): void

Log a used-CSS fallback with throttling.

ParameterTypeDefaultDescription
$reasonstring—Reason code.
$handlesarray—Handles involved.

Return: void.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4740

private is_used_css_valid()

private function is_used_css_valid(string $path): bool

Whether a used-CSS file is valid.

ParameterTypeDefaultDescription
$pathstring—Absolute path.

Return: bool.

Tags: @since 2.0.0

Source: includes/CSS/class-used-css.php, line 4751

public process_buffer()

public function process_buffer(string $buffer): string

Process the HTML buffer to apply used-CSS.

ParameterTypeDefaultDescription
$bufferstring—The HTML buffer.

Return: string — Modified HTML buffer.

Tags: @since 1.9.0

Source: includes/CSS/class-used-css.php, line 4766

Hooks

Hooks referenced in includes/CSS/class-used-css.php:

HookTypeLineNotes
wppo_used_css_safelistfilter433@param ×1
wppo_used_css_regen_cooldownfilter2459@param ×1
wppo_used_css_targeted_cooldownfilter2650@param ×1
wppo_css_preview_fetch_timeoutfilter3973—
wppo_used_css_strict_cspfilter4536@param ×1
wppo_unused_css_regression_thresholdfilter4678—