current_fallback_context(); if ( ! isset( $decisions[ $context ] ) ) { /* * A gate that cannot be consulted must not retire anything, and must not take the * page down either. Both failure modes resolve to "keep serving the fallback". */ try { $decisions[ $context ] = $this->service->fallback_retirement_gate()->decide( $context ); } catch ( \Throwable $error ) { /* * A swallowed exception must never be able to masquerade as a clean retirement. * The decision and its reason are recorded (record_fallback_gate_error()), and * verification output shows them. */ $reason = __( 'The fallback retirement gate could not be evaluated, so the fallback stylesheet stays active.', 'v4studio' ); self::record_fallback_gate_error( $context, $reason ); $decisions[ $context ] = [ 'retire' => false, 'reason' => $reason, ]; } } return $decisions[ $context ]; } /** * Gate evaluation failures for this request, keyed by context. * * @var array */ private static array $fallback_gate_errors = []; protected static function record_fallback_gate_error( string $context, string $reason ): void { self::$fallback_gate_errors[ $context ] = $reason; } /** * Any gate evaluation failures seen this request. * * Surfaced by verification output so a gate that threw cannot be mistaken for a gate that * decided. Empty is the normal state. * * @return array */ public static function fallback_gate_errors(): array { return self::$fallback_gate_errors; } /** * Handle for the request-time recovery variables, and the marker printed inside them. * * Both exist so this block is IDENTIFIABLE. The previous emission went into Elementor's * generated kit stylesheet carrying nothing that named V4, which is how a verification of * "no V4 variables on the frontend" was written and believed while 133 V4 declarations * were being served. A grep for either of these now answers that question truthfully. */ public const FALLBACK_VARIABLES_HANDLE = 'v4studio-fallback-variables'; public const FALLBACK_VARIABLES_MARKER = 'v4studio-fallback-variables'; public const FALLBACK_VARIABLES_LAYER = 'v4studio-fallback'; /** * Design variables for the fallback classes, emitted at REQUEST TIME inside a cascade layer. * * This replaces the old elementor/css-file/post/parse injection entirely. That path wrote * into Elementor's CACHED kit stylesheet, which made its correctness depend on when the file * happened to be generated: a file written while the gate was green kept its missing V4 block * after the stamp went stale, leaving the fallback classes active with their var() references * resolving to nothing. Nothing regenerates that file merely because a stamp changed, and the * transition that matters most -- someone editing a V4 class in Elementor's own UI -- runs no * V4 code at all, so there is no hook on which to invalidate. * * Emitting per request removes the cache from the correctness argument: the decision that * applies is the one taken for THIS request. * * WHY A CASCADE LAYER. The old injection had to run at priority 5 so that Elementor's own * :root landed later and won the equal-specificity tie -- an ordering argument, and ordering * is exactly what a cached file cannot guarantee. An unlayered declaration wins over a layered * one REGARDLESS OF SOURCE ORDER, so wrapping these in a layer makes "Elementor always wins * where Elementor has a value" true by construction rather than by arrangement. Verified in a * browser both ways round: a token declared in both resolves to Elementor's in either order, * and a token declared only here still applies. * * WHAT THIS DOES NOT DO. The layer fills declarations Elementor is MISSING. It cannot repair * one Elementor still has: an Elementor declaration holding an invalid value, or a var() * reference to something undefined, keeps winning and stays broken, because it is unlayered * and a layered declaration never overrides it. Recovery here is "the token exists", not "the * token is good". Verification remains fail-closed for that reason. * * No !important is used, and none is needed: layering decides this, and adding !important * would invert the guarantee above by beating Elementor's normal declarations outright. */ public function enqueue_fallback_variables_css(): void { $decision = $this->fallback_decision(); if ( ! empty( $decision['retire'] ) ) { return; } /* * Not in the outer editor chrome. The chrome renders Elementor's own interface, not the * document; the design is rendered in the preview iframe, which is enqueued separately * and does receive these. Emitting into the chrome would put a :root block on the editor * UI for no rendering benefit. */ if ( Fallback_Retirement_Gate::CONTEXT_EDITOR_CHROME === $this->current_fallback_context() ) { return; } // Both wp_enqueue_scripts and elementor/preview/enqueue_styles fire inside the preview // iframe, and wp_add_inline_style() appends rather than replaces. static $variables_enqueued = false; if ( $variables_enqueued ) { return; } $variables_enqueued = true; $css = trim( $this->service->get_fallback_variables_css() ); if ( '' === $css ) { return; } $css = '/* ' . self::FALLBACK_VARIABLES_MARKER . ' */' . " " . '@layer ' . self::FALLBACK_VARIABLES_LAYER . ' {' . " " . $css . " " . '}'; wp_register_style( self::FALLBACK_VARIABLES_HANDLE, false, [], V4STUDIO_VERSION ); wp_enqueue_style( self::FALLBACK_VARIABLES_HANDLE ); // Escaped where it is output: generated CSS has no markup, so this removes only a stray tag. wp_add_inline_style( self::FALLBACK_VARIABLES_HANDLE, wp_strip_all_tags( $css ) ); } /** * Is the backup stylesheet retired for visitors right now? Read only. Used by Fallback_Reverify. */ public function fallback_retired(): bool { return ! empty( $this->service->fallback_retirement_gate()->decide( Fallback_Retirement_Gate::CONTEXT_FRONTEND )['retire'] ); } /** * The design-system service, for Fallback_Reverify, which re-verifies after Elementor changes the design * system outside V4 Studio. That write lives there, never here: this class runs on renders. */ public function design_system_service(): Elementor_Design_System_Service { return $this->service; } /** Keep utility composition slots local to each element, including nested effects. */ public function enqueue_composition_properties(): void { static $enqueued = false; if ( $enqueued ) { return; } $enqueued = true; $slots = [ 'filter-blur' => '0px', 'filter-brightness' => '1', 'filter-contrast' => '1', 'filter-saturate' => '1', 'filter-grayscale' => '0', 'border-style' => 'solid' ]; $css = ''; foreach ( $slots as $name => $initial ) { $css .= '@property --v4-' . $name . '{syntax:"*";inherits:false;initial-value:' . $initial . ';}'; } wp_register_style( 'v4studio-composition-properties', false, [], V4STUDIO_VERSION ); wp_enqueue_style( 'v4studio-composition-properties' ); wp_add_inline_style( 'v4studio-composition-properties', wp_strip_all_tags( $css ) ); } /** * Classes the class fallback leaves out because Elementor already renders them, so the fallback * covers only what Elementor is missing and never overrides a class Elementor holds. Null (Elementor * unreadable, or an injected attestation) keeps the whole fallback. * * Leaving out what Elementor holds matters: with the whole fallback served, one class Elementor stored empty put * every page on the full 115 KB stylesheet, whose copies of every other class then sat over Elementor's own. * * @return array|null */ protected function fallback_class_exclusions(): ?array { return $this->service->fallback_retirement_gate()->held_class_labels(); } public function enqueue_custom_css(): void { /* * Retirement gate. The implementation below is untouched and still reachable — * this only decides whether to run it. V4STUDIO_FORCE_FALLBACK_CSS restores the old * behaviour everywhere. */ $decision = $this->fallback_decision(); if ( ! empty( $decision['retire'] ) ) { return; } // Inside the Elementor preview iframe both wp_enqueue_scripts and // elementor/preview/enqueue_styles fire, and wp_add_inline_style() appends rather // than replaces — without this guard the whole fallback stylesheet is emitted twice. static $custom_css_enqueued = false; if ( $custom_css_enqueued ) { return; } $custom_css_enqueued = true; // This used to also enqueue one remote font stylesheet per font-typed // variable in fallback_payload. Retired. Elementor already enqueues every family in // its own global variables and kit typography, and — when local hosting is on — // serves them from wp-content/uploads/, so V4's remote fetch both duplicated // families and defeated that local hosting. The payload is also a frozen snapshot, // so it drifted: it still named Abel long after the live variable became Orbitron. $css = trim( $this->service->get_fallback_classes_css( $this->fallback_class_exclusions() ) ); if ( '' === trim( $css ) ) { return; } wp_register_style( 'v4studio-custom-css', false, [], V4STUDIO_VERSION ); wp_enqueue_style( 'v4studio-custom-css' ); wp_add_inline_style( 'v4studio-custom-css', wp_strip_all_tags( $css ) ); // Wired here, where the fallback is enqueued; an override of this method must wire it too. add_action( 'wp_print_styles', [ $this, 'place_fallback_before_local_styles' ], 0 ); } /** * Print the class fallback where Elementor's own global classes sit: after its atomic base and * global-class stylesheets, before its element (local) stylesheets. * * The fallback's rules are `.x` and `.elementor .x`, the specificity of Elementor's base styles * (`.elementor .e-grid-base`) and of an element's local style (`.elementor .e-1a2b3c4-local`) * alike. So only print order decides, and it has to sit between them: after the base styles, so a * class still beats Elementor's defaults the way a global class does, and before the local styles, * so a setting made on one element still beats the class. Printed after everything (its place on * wp_enqueue_scripts), a fallback grid-columns-2 overrode an element's own 2fr 3fr columns and a * fallback width-fit an element's own max-content. * * WordPress prints a style's dependencies first, so the fallback depends on every queued base and * global stylesheet and every queued local stylesheet depends on the fallback. Elementor's * stylesheets are recognised by handle AND by their file under uploads/elementor/css/, so another * plugin's handle that happens to start with "base-" or "local-" is never reordered. */ public function place_fallback_before_local_styles(): void { $styles = wp_styles(); $handle = 'v4studio-custom-css'; if ( ! in_array( $handle, (array) $styles->queue, true ) || ! isset( $styles->registered[ $handle ] ) ) { return; } foreach ( (array) $styles->queue as $queued ) { $kind = $this->elementor_atomic_style_kind( (string) $queued ); if ( '' === $kind ) { continue; } if ( 'local' === $kind ) { $styles->registered[ $queued ]->deps = array_values( array_unique( array_merge( (array) $styles->registered[ $queued ]->deps, [ $handle ] ) ) ); } else { $styles->registered[ $handle ]->deps = array_values( array_unique( array_merge( (array) $styles->registered[ $handle ]->deps, [ $queued ] ) ) ); } } } /** 'base', 'global' or 'local' for one of Elementor's atomic stylesheets; '' for any other handle. */ private function elementor_atomic_style_kind( string $handle ): string { if ( 1 !== preg_match( '/^(base|global|local)-[a-z0-9_-]+$/', $handle, $match ) ) { return ''; } $src = (string) ( wp_styles()->registered[ $handle ]->src ?? '' ); return false !== strpos( $src, '/elementor/css/' . $handle . '.css' ) ? $match[1] : ''; } /** * Keep the Elementor editor preview aligned with the frontend design system. * * Elementor's atomic base stylesheet can be printed after the editor's * generated global-class stylesheet. Reusing the fallback payload here gives * the preview the same rules without adding anything to the live frontend. * * CLASSES ONLY. This deliberately does not emit the variables, and that is the whole * point of the method as it now stands. * * The variables reach the preview the same way they reach the frontend: through * enqueue_fallback_variables_css(), which is registered on wp_enqueue_scripts and on * elementor/preview/enqueue_styles, and emits them inside a cascade layer. * * Emitting them from THIS method instead appended a second, UNLAYERED :root block much * later in the document — measured in a running editor at sheet position 290 against the * kit's 171 — so V4's copy won a later-wins tie the frontend never has. That is invisible * while both copies agree and produces a wrong colour the moment they drift: the saved * design system held --color-primary #0f4283 while the kit stylesheet had been regenerated * to #3a5ae9, so the editor rendered navy buttons against a blue frontend, from one design * system. * * The cascade layer is what makes that class of bug unreachable now rather than merely * avoided: a layered declaration cannot win a later-wins tie against Elementor's unlayered * one, in any order. Adding an UNLAYERED copy back here would still re-create it. */ public function enqueue_editor_custom_css(): void { // Same gate as the primary enqueue: this is the later synchronisation copy of the same // stylesheet, so retiring one without the other would leave the fallback half-applied. $decision = $this->fallback_decision(); if ( ! empty( $decision['retire'] ) ) { return; } static $editor_css_synchronized = false; if ( $editor_css_synchronized ) { return; } $editor_css_synchronized = true; $handle = 'v4studio-custom-css'; $dependencies = []; if ( wp_style_is( 'base-desktop-css', 'registered' ) || wp_style_is( 'base-desktop-css', 'enqueued' ) ) { $dependencies[] = 'base-desktop-css'; } $styles = wp_styles(); if ( isset( $styles->registered[ $handle ] ) ) { $registered = $styles->registered[ $handle ]; $registered->deps = array_values( array_unique( array_merge( (array) $registered->deps, $dependencies ) ) ); if ( ! wp_style_is( $handle, 'enqueued' ) ) { wp_enqueue_style( $handle ); } return; } $css = trim( $this->service->get_fallback_classes_css( $this->fallback_class_exclusions() ) ); if ( '' === $css ) { return; } wp_register_style( $handle, false, $dependencies, V4STUDIO_VERSION ); wp_enqueue_style( $handle ); wp_add_inline_style( $handle, wp_strip_all_tags( $css ) ); } public static function default_settings(): array { return [ /* * Brand Data; see Brand_Data for the catalogue and read API. */ 'dynamic_data' => [], /* * Whether the Brand Data seed has already run. It must be declared here, because * known_settings() drops any key this schema does not name -- including on the very * write that sets it. Without the declaration the seed would run again on the next * visit and re-fill fields the user had deliberately emptied. */ 'dynamic_data_seeded' => false, 'defaults_version' => '', 'defaults_imported' => '', /* A failed defaults import: attempts, when the next automatic attempt is due, what failed. Empty once an attempt succeeds. */ 'defaults_retry' => [], 'variables_css' => '', 'classes_css' => '', 'last_variables' => [], 'last_classes' => [], 'last_import' => [], /* * Font fallback-stack repair: what V4 owns, and what it last reported. * * THESE MUST BE DECLARED HERE OR THEY DO NOT PERSIST. update_settings() writes * array_intersect_key( $settings, default_settings() ) plus the keys already stored * that are not defaults, so a brand-new key that is neither is silently DROPPED on * every write. The repair ran, wrote its declarations into Elementor, and returned a * manifest -- and the manifest vanished at save time, every time. * * That is not a cosmetic loss. The manifest is the ownership record: without it the * repair cannot tell its own declaration from a user's, cannot update one when the * token changes, and cannot honour a tombstone. It would re-plan the same writes * forever and would eventually overwrite something a user had deliberately changed. */ 'font_stack_manifest' => [], 'font_stack_report' => [], /* * Brand Data from a JSON import is staged here, checked by the Brand Data sanitiser and * applied in the same request (apply_staged_brand_data()). Nothing else is staged: * imported token values are applied straight away through the Tokens screen's checks. * The admin page keeps only Brand Data here (tidy_staged_import()). */ 'staged_import' => [], 'aliases_initialized' => false, 'alias_overrides' => [], 'fallback_payload' => [ 'variables' => [], 'classes' => [], ], 'sync_ignore' => [ 'variables' => [], 'classes' => [], ], ]; } public static function get_settings(): array { $settings = get_option( static::OPTION_KEY, null ); $settings = wp_parse_args( is_array( $settings ) ? $settings : [], static::default_settings() ); return static::known_settings( $settings ); } /** * Write the settings this request changed, and nothing else. * * Every caller reads the settings, changes some keys and passes the whole array back. A long request (an * Elementor import takes seconds) would then write back, unchanged, keys a short request saved meanwhile. So * only the keys that differ from what this request read are written, onto a fresh read of the option: a key * another request changed in between keeps that change. (What this request read is WordPress's cached copy of * the option, which is also refreshed by this request's own earlier writes.) */ public static function update_settings( array $settings ): void { $read = get_option( static::OPTION_KEY, null ); $was = static::known_settings( wp_parse_args( is_array( $read ) ? $read : [], static::default_settings() ) ); $settings = static::known_settings( wp_parse_args( $settings, static::default_settings() ) ); $changed = []; foreach ( $settings as $key => $value ) { if ( ! array_key_exists( $key, $was ) || $was[ $key ] !== $value ) { $changed[ $key ] = $value; } } if ( function_exists( 'wp_cache_delete' ) ) { wp_cache_delete( static::OPTION_KEY, 'options' ); } $existing = get_option( static::OPTION_KEY, null ); $existing = is_array( $existing ) ? $existing : ( is_array( $read ) ? $read : [] ); $preserved = array_diff_key( $existing, static::default_settings() ); $current = static::known_settings( wp_parse_args( $existing, static::default_settings() ) ); update_option( static::OPTION_KEY, array_merge( $preserved, array_merge( $current, $changed ) ), false ); } private static function known_settings( array $settings ): array { return array_intersect_key( $settings, static::default_settings() ); } }