global variable -> current variable value * * not * * class property -> copied literal value * * A literal that happens to equal the variable's current value is NOT equivalent: it stops * tracking the token, so changing the variable later updates every correctly-bound consumer * and silently skips the flattened one. Current-value equality is therefore never sufficient * evidence of a correct conversion, and this class exists so that rule is applied by one * implementation rather than restated in each caller. * * Three acceptable-or-not outcomes: * * - BOUND the value carries a native Elementor global-variable reference * (`global-color-variable`, `global-size-variable`, …) that renders here. * Preferred for colour and font; V4 writes sizes as RETAINED `var()` text * (see VAR_TEXT_TYPES). * - RETAINED the original `var(--token)` text survives, either inside a prop value that * accepts free-form CSS (a `custom`-unit size, for example) or in * Elementor-owned custom CSS. Elementor emits its global variables under their * own label names, so such a reference keeps resolving after V4's own :root * output is retired. * - LITERAL neither. The binding was lost. Never acceptable. * * @package V4STUDIO */ namespace V4STUDIO; if ( ! defined( 'ABSPATH' ) ) { exit; } class Variable_Binding { const BOUND = 'bound'; const RETAINED = 'retained'; const LITERAL = 'literal'; const ABSENT = 'absent'; /** * Memoized result of supported_native_variable_types(). Null means "not yet probed"; * an empty array is a real answer meaning "nothing native renders here". * * @var string[]|null */ private static ?array $supported_variable_types = null; /** * Size reference types V4 Studio writes as `var(--label)` text. * * Every size token is written as `var(--label)` text in a `custom`-unit size, on every site: one value, * rendered the same way everywhere and editable in Elementor's class editor. (A native size reference renders * only where a transformer is registered for it.) `global-custom-size-variable` is listed too, so it is written * the same way if Elementor renders that type as a class prop later. */ private const VAR_TEXT_TYPES = [ 'global-size-variable', 'global-custom-size-variable' ]; /** The state name used for a variant with no pseudo-state, mirroring a stored `state => null`. */ const STATE_BASE = 'base'; /** * The identity of one variant: breakpoint plus state. * * WHY THIS EXISTS AS ONE FUNCTION * A class does not have "a" border-color. It has one per (breakpoint, state), and each of * those has its OWN source declaration. `.btn-ghost { border-color: var(--color-transparent) }` * and `.btn-ghost:hover { border-color: transparent }` are two different declarations, and * the second is a literal because the author wrote a literal. * * Collapsing them is not a rounding error, it inverts the result: the hover literal gets * compared against the base `var()` and reported as a lost binding, and "repairing" it * writes a token the source never asked for. That is exactly what happened here — twice. * Every comparison must therefore go through a variant key, and there is one implementation * of that key so the audit, the repair tool and the tests cannot disagree about identity. */ public static function variant_key( $breakpoint, $state ): string { $breakpoint = is_string( $breakpoint ) ? trim( $breakpoint ) : ''; $state = is_string( $state ) ? trim( $state ) : ''; return ( '' !== $breakpoint ? sanitize_key( $breakpoint ) : 'desktop' ) . ':' . ( '' !== $state ? sanitize_key( $state ) : self::STATE_BASE ); } /** * The variant key of a STORED Elementor variant. Base state is stored as null. */ public static function stored_variant_key( array $variant ): string { $meta = (array) ( $variant['meta'] ?? [] ); return self::variant_key( $meta['breakpoint'] ?? null, $meta['state'] ?? null ); } /** * Human-readable breakpoint and state for a variant key, for reporting. * * @return array{breakpoint:string,state:string} */ public static function split_variant_key( string $key ): array { $parts = explode( ':', $key, 2 ); return [ 'breakpoint' => (string) ( $parts[0] ?? 'desktop' ), 'state' => (string) ( $parts[1] ?? self::STATE_BASE ), ]; } /** * Group parsed class rules into class => variant key => property => value, keeping only * declarations that actually reference a custom property. * * Input is `CSS_Parser::parse_classes()['items']` — deliberately the SAME parser the import * path uses. Auditing against a second, independently written parser would mean the audit * could pass while disagreeing with what the importer actually saw, which is the failure * mode this whole area already has once. * * Later rules win, matching the cascade, so a class restated further down the stylesheet * is judged on the declaration that actually applies. * * @param array $items * @return array>> */ public static function source_by_variant( array $items ): array { $out = []; foreach ( $items as $item ) { if ( ! is_array( $item ) || 'valid' !== ( $item['status'] ?? 'valid' ) ) { continue; } $label = (string) ( $item['class_name'] ?? '' ); if ( '' === $label ) { continue; } $key = self::variant_key( $item['breakpoint'] ?? null, $item['state'] ?? null ); foreach ( (array) ( $item['declarations'] ?? [] ) as $property => $value ) { if ( ! is_string( $property ) || ! is_scalar( $value ) ) { continue; } if ( ! self::declaration_uses_variable( (string) $value ) ) { continue; } $out[ $label ][ $key ][ trim( $property ) ] = trim( (string) $value ); } } return $out; } /** * Does a CSS declaration value reference a custom property at all? */ public static function declaration_uses_variable( string $value ): bool { return (bool) preg_match( '/var\(\s*--/', $value ); } /** * The custom-property tokens a declaration references, in order. * * @return string[] e.g. ['--shadow-xs'] */ public static function tokens_in( string $value ): array { if ( ! preg_match_all( '/var\(\s*(--[a-zA-Z0-9_-]+)/', $value, $matches ) ) { return []; } return array_values( array_unique( $matches[1] ) ); } /** * Does a stored prop value carry a native Elementor global-variable reference? * * Checked recursively: a variable reference can sit nested inside a composite value, * such as the colour of one shadow inside a box-shadow array. * * TWO QUESTIONS, CHOSEN BY THE CALLER. * * Auditing what is STORED asks whether the reference paints: a size reference counts where a * transformer renders it. * * Deciding what to WRITE passes `$for_write`, which also counts VAR_TEXT_TYPES as unbound. * Elementor's CSS converter produces native size references, and the adapter's binding guard * decides from this answer whether to keep them. With `$for_write` a size reference is not a * binding, so the guard writes it as `var()` text. * * @param mixed $value * @param bool $for_write Count only types V4 writes: see is_writable_variable_type(). */ public static function has_native_reference( $value, bool $for_write = false ): bool { if ( is_array( $value ) ) { $type = $value['$$type'] ?? null; /* * SUPPORTED, not merely well-formed. A `global-size-variable` reference stores and * validates but renders as the raw `e-gv-*` id where nothing registers its renderer, * so counting it as BOUND there would report a binding that paints nothing. See * supported_native_variable_types(). */ if ( is_string( $type ) && ( $for_write ? self::is_writable_variable_type( $type ) : self::is_supported_variable_type( $type ) ) ) { return true; } foreach ( $value as $child ) { if ( self::has_native_reference( $child, $for_write ) ) { return true; } } } return false; } /** * Elementor names its variable prop types `global-*-variable`. Matching the shape rather * than a fixed list means a new variable type added by Elementor counts immediately, * instead of being reported as a lost binding until someone updates a constant here. * * This answers "does this LOOK like a variable reference", which is not the same question * as "will Elementor render it" -- see is_supported_variable_type(). */ public static function is_variable_type( string $type ): bool { return (bool) preg_match( '/^global-[a-z-]*variable$/', $type ); } /** * Which native variable reference types can the INSTALLED Elementor actually render? * * SCHEMA-VALID IS NOT RENDERER-SUPPORTED. * * `Size_Style_Schema` adds `Size_Variable_Prop_Type` (`global-size-variable`) to every size * prop union, so a size reference passes validation and stores happily. But * `Style_Transformers::append_to()` registers the global-variable transformer for COLOUR and * FONT only. An unregistered key falls through `Transformers_Registry::get()` to the * registered fallback, `Plain_Transformer`, which returns the value unchanged -- so the * variable ID itself becomes the declaration value and Elementor emits * * .the-class{max-width:e-gv-1950668;} * * which no browser can use. Measured on Elementor 4.2.3: colour and font are the only two * variable types with a registered styles transformer, and neither `global-size-variable` * nor `global-custom-size-variable` is among them. * * Capability is PROBED rather than hardcoded to a version string, so a stored reference of a * type that a later Elementor (or Elementor Pro) renders audits as bound with no code change * here. What V4 writes is a separate question: see is_writable_variable_type(). It is * resolved through Elementor's own public accessor, once, and memoized: this is * consulted per property per declaration, and rebuilding the registry in that loop would be * pointless work. * * FAILS CLOSED. When the registry cannot be established -- Elementor absent, or too old to * expose the resolver -- this returns an empty list, so nothing is claimed as natively bound. * The declaration then keeps its `var()` text instead, which renders correctly on its own. * * @return string[] */ public static function supported_native_variable_types(): array { if ( null === self::$supported_variable_types ) { self::probe_native_variable_types(); } return self::$supported_variable_types; } /** * May V4 WRITE a native reference of this type? Use for write decisions. * * RENDERING IS NOT THE SAME QUESTION AS WRITING. A type must render here, and must not be * one of VAR_TEXT_TYPES: sizes are written as `var()` text on every site, so a site's classes * are the same whichever plugins are active. Fails closed exactly as supported_native_variable_types() does. */ public static function is_writable_variable_type( string $type ): bool { return self::is_supported_variable_type( $type ) && ! in_array( $type, self::VAR_TEXT_TYPES, true ); } /** Probe the styles registry once and memoize the answer. */ private static function probe_native_variable_types(): void { self::$supported_variable_types = []; $resolver = '\Elementor\Modules\AtomicWidgets\PropsResolver\Render_Props_Resolver'; if ( ! class_exists( $resolver ) || ! method_exists( $resolver, 'for_styles' ) ) { return; } try { $registry = $resolver::for_styles()->get_transformers_registry(); if ( ! is_object( $registry ) || ! method_exists( $registry, 'all' ) ) { return; } $transformers = (array) $registry->all(); } catch ( \Throwable $e ) { return; } foreach ( array_keys( $transformers ) as $key ) { $key = (string) $key; if ( self::is_variable_type( $key ) ) { self::$supported_variable_types[] = $key; } } } /** * Does this reference type both look like a variable reference AND render in this runtime? */ public static function is_supported_variable_type( string $type ): bool { return self::is_variable_type( $type ) && in_array( $type, self::supported_native_variable_types(), true ); } /** * Does the original `var()` text survive anywhere inside a stored prop value? * * @param mixed $value */ public static function retains_var_text( $value, array $tokens = [] ): bool { $encoded = wp_json_encode( $value ); if ( ! is_string( $encoded ) || false === strpos( $encoded, 'var(' ) ) { return false; } if ( empty( $tokens ) ) { return true; } foreach ( $tokens as $token ) { if ( false === strpos( $encoded, $token ) ) { return false; } } return true; } /** * Classify one converted declaration. * * @param mixed $prop_value The stored prop value, or null when no prop was stored. * @param string $source The original CSS declaration value. * @param string|null $custom_css Decoded Elementor-owned custom CSS for the variant. * @param string $property The CSS property name. * @param bool $for_write For a write decision: see has_native_reference(). */ public static function classify( $prop_value, string $source, ?string $custom_css = null, string $property = '', bool $for_write = false ): string { $tokens = self::tokens_in( $source ); if ( null !== $prop_value ) { if ( self::has_native_reference( $prop_value, $for_write ) ) { return self::BOUND; } if ( self::retains_var_text( $prop_value, $tokens ) ) { return self::RETAINED; } } if ( null !== $custom_css && '' !== $custom_css && self::custom_css_retains( $custom_css, $property, $tokens ) ) { return self::RETAINED; } return null === $prop_value ? self::ABSENT : self::LITERAL; } /** * Does custom CSS carry the declaration, still referencing every expected token? * * The property must match too. Finding `--shadow-xs` somewhere in an unrelated * declaration would otherwise vouch for a `box-shadow` that was never preserved. */ public static function custom_css_retains( string $custom_css, string $property, array $tokens ): bool { if ( '' === $property ) { return false; } $found = false; foreach ( explode( ';', $custom_css ) as $declaration ) { $colon = strpos( $declaration, ':' ); if ( false === $colon ) { continue; } if ( strtolower( trim( substr( $declaration, 0, $colon ) ) ) !== strtolower( $property ) ) { continue; } $value = substr( $declaration, $colon + 1 ); foreach ( $tokens as $token ) { if ( false === strpos( $value, $token ) ) { continue 2; } } $found = true; break; } return $found; } }