validator = $validator; } public function parse_variables( string $css ): array { $css = $this->normalize_dashes( $this->strip_comments( $css ) ); $items = []; $item_keys = []; $skipped = []; $root_body = ''; if ( preg_match_all( '/:root\s*\{([^{}]*)\}/is', $css, $matches ) ) { $root_body = implode( "\n", $matches[1] ); } if ( '' === trim( $root_body ) ) { if ( preg_match( '/--[a-zA-Z][a-zA-Z0-9_-]*\s*:/', $css ) ) { $root_body = $css; } else { return [ 'items' => [], 'skipped' => [], 'errors' => [], ]; } } if ( '' === trim( $root_body ) ) { return [ 'items' => [], 'skipped' => [], 'errors' => [], ]; } foreach ( $this->parse_declarations( $root_body ) as $declaration ) { $name = $declaration['property']; $value = $declaration['value']; if ( ! $this->validator->is_valid_variable_name( $name ) ) { $skipped[] = [ 'selector' => $name, 'reason' => __( 'Invalid CSS custom property name.', 'v4studio' ), 'css' => $name . ': ' . $value . ';', ]; continue; } if ( $this->is_border_style_variable_name( $name ) ) { $skipped[] = [ 'selector' => $name, 'reason' => __( 'Border style variables are not supported by Elementor V4 variables.', 'v4studio' ), 'css' => $name . ': ' . $value . ';', ]; continue; } $type = $this->validator->detect_variable_type( $value ); if ( $this->is_size_variable_name( $name ) ) { $type = 'size'; } $item = [ 'name' => $name, 'label' => ltrim( $name, '-' ), 'value' => $value, 'type' => $type, 'status' => 'valid', ]; if ( array_key_exists( $name, $item_keys ) ) { $items[ $item_keys[ $name ] ] = $item; continue; } $item_keys[ $name ] = count( $items ); $items[] = $item; } return [ 'items' => $items, 'skipped' => $skipped, 'errors' => [], ]; } private function is_size_variable_name( string $name ): bool { return 1 === preg_match( '/(?:^|--|-)(?:space|spacing|size|width|height|radius|shadow|gap|margin|padding|inset|top|right|bottom|left|columns?|rows?)(?:-|$)/i', $name ); } private function is_border_style_variable_name( string $name ): bool { return 1 === preg_match( '/(?:^|--|-)border-style(?:-|$)/i', $name ); } public function parse_classes( string $css ): array { $css = $this->normalize_dashes( $this->strip_comments( $css ) ); $items = []; $skipped = []; $media_blocks = $this->extract_media_blocks( $css ); if ( preg_match_all( '/@(container|supports)[^{]*\{/is', $css, $at_rules, PREG_OFFSET_CAPTURE ) ) { foreach ( $at_rules[0] as [ $at_rule, $at_offset ] ) { $skipped[] = [ 'selector' => trim( rtrim( $at_rule, '{' ) ), 'reason' => __( 'Container and supports queries are not supported by Elementor V4 classes, so this rule was skipped.', 'v4studio' ), 'css' => $this->balanced_block_from( $css, $at_offset ), ]; } } /* * Media blocks must be SPLICED OUT in reverse, because substr_replace() works on * byte offsets and removing an earlier block invalidates every later one. They must * be EMITTED in source order, which is a separate concern the reverse iteration used * to silently decide as well. * * Emitting them reversed made the serializer oscillate with period 2: parse produced * media items back-to-front, format_class_block() re-emitted them in that order, and * the next parse reversed them again. Two `@media` blocks were enough. A save that * changed nothing therefore alternated between two byte-different documents, and * because the fallback retirement stamp hashes those bytes, every other save * invalidated retirement and brought the static fallback back. It also inverted the * cascade for a query written twice: the first declaration won instead of the last. * * Keying by the original index and restoring ascending order fixes both, and leaves * the splice logic untouched. */ $media_items = []; foreach ( array_reverse( $media_blocks, true ) as $index => $media_block ) { $css = substr_replace( $css, '', $media_block['start'], $media_block['length'] ); $parsed_block = $this->parse_class_rules( $media_block['body'], $media_block['query'] ); $media_items[ $index ] = $parsed_block['items']; $skipped = array_merge( $skipped, $parsed_block['skipped'] ); } ksort( $media_items ); foreach ( $media_items as $block_items ) { $items = array_merge( $items, $block_items ); } $css = preg_replace( '/@(container|supports)[^{]*\{.*?\}\s*\}/is', '', $css ) ?? $css; $parsed_base = $this->parse_class_rules( $css ); $items = array_merge( $parsed_base['items'], $items ); $skipped = array_merge( $skipped, $parsed_base['skipped'] ); return [ 'items' => $items, 'skipped' => $skipped, 'errors' => [], ]; } /** * Parse a run of class rules. * * Returns `items` plus `skipped`. Selectors the validator rejects used to be dropped with * a bare `continue`, so a descendant, grouped or ID selector vanished from the import with * nothing reported — the caller could not distinguish "nothing to skip" from "silently * deleted". Validator::get_class_skip_reason() already produced the user-facing wording * for each rejection class and had no production caller; it does now. * * @return array{items: array, skipped: array} */ private function parse_class_rules( string $css, string $media_query = '' ): array { $items = []; $skipped = []; if ( preg_match_all( '/([^{}@]+)\{([^{}]*)\}/s', $css, $matches, PREG_SET_ORDER ) ) { foreach ( $matches as $match ) { $selector = trim( $match[1] ); $body = trim( $match[2] ); if ( ! $this->validator->is_valid_simple_class_selector( $selector ) ) { if ( '' !== $selector ) { $skipped[] = [ 'selector' => $selector, 'reason' => $this->validator->get_class_skip_reason( $selector ), 'css' => trim( $match[0] ), 'media_query' => trim( $media_query ), ]; } continue; } $declarations = $this->parse_declarations( $body ); $declaration_map = []; foreach ( $declarations as $declaration ) { $property = $declaration['property']; $value = $declaration['value']; if ( 'box-shadow' === $property && isset( $declaration_map[ $property ] ) ) { $declaration_map[ $property ] .= ', ' . $value; continue; } $declaration_map[ $property ] = $value; } preg_match( '/^\.([a-zA-Z_-][a-zA-Z0-9_-]*)(?::([a-zA-Z][a-zA-Z0-9-]*))?$/', $selector, $selector_parts ); $items[] = [ 'selector' => $selector, 'class_name' => (string) ( $selector_parts[1] ?? substr( $selector, 1 ) ), 'state' => sanitize_key( (string) ( $selector_parts[2] ?? '' ) ), 'breakpoint' => $this->breakpoint_from_media_query( $media_query ), 'media_query' => trim( $media_query ), 'declarations' => $declaration_map, 'status' => 'valid', ]; } } return [ 'items' => $items, 'skipped' => $skipped, ]; } /** * The text of the block whose opening brace is the first `{` at or after $offset, braces balanced, * from the at-rule keyword to its closing brace. Empty when the block never closes. */ private function balanced_block_from( string $css, int $offset ): string { $open = strpos( $css, '{', $offset ); if ( false === $open ) { return ''; } $depth = 0; $length = strlen( $css ); for ( $cursor = $open; $cursor < $length; $cursor++ ) { if ( '{' === $css[ $cursor ] ) { ++$depth; } elseif ( '}' === $css[ $cursor ] && 0 === --$depth ) { return trim( substr( $css, $offset, $cursor - $offset + 1 ) ); } } return ''; } private function extract_media_blocks( string $css ): array { $blocks = []; $offset = 0; while ( preg_match( '/@media\s*([^\{]+)\{/i', $css, $match, PREG_OFFSET_CAPTURE, $offset ) ) { $start = $match[0][1]; $open_brace = $start + strlen( $match[0][0] ) - 1; $depth = 1; $cursor = $open_brace + 1; $length = strlen( $css ); while ( $cursor < $length && $depth > 0 ) { if ( '{' === $css[ $cursor ] ) { ++$depth; } elseif ( '}' === $css[ $cursor ] ) { --$depth; } ++$cursor; } if ( 0 !== $depth ) { break; } $blocks[] = [ 'query' => trim( (string) $match[1][0] ), 'body' => substr( $css, $open_brace + 1, $cursor - $open_brace - 2 ), 'start' => $start, 'length' => $cursor - $start, ]; $offset = $cursor; } return $blocks; } private function breakpoint_from_media_query( string $media_query ): string { if ( '' === trim( $media_query ) ) { return 'desktop'; } if ( ! preg_match( '/(min|max)-width\s*:\s*(\d+(?:\.\d+)?)px/i', $media_query, $matches ) ) { return 'desktop'; } $direction = strtolower( $matches[1] ); $value = (float) $matches[2]; $breakpoints = $this->active_breakpoints_for_media_direction( $direction ); $closest = 'min' === $direction ? 'widescreen' : 'desktop'; $distance = PHP_FLOAT_MAX; foreach ( $breakpoints as $name => $breakpoint_value ) { $current_distance = abs( $value - $breakpoint_value ); if ( $current_distance < $distance ) { $closest = $name; $distance = $current_distance; } } return $closest; } /** * Return the active Elementor breakpoints that can represent a media query. * * Elementor styles use the site's active breakpoint values. Resolving against * the same values keeps custom Elementor breakpoints mapped to the correct * global-class variants instead of approximating them with fixed defaults. * * @param string $direction Media-query direction: min or max. * @return array */ private function active_breakpoints_for_media_direction( string $direction ): array { $fallbacks = [ 'mobile' => [ 'value' => 767.0, 'direction' => 'max' ], 'mobile_extra' => [ 'value' => 880.0, 'direction' => 'max' ], 'tablet' => [ 'value' => 1024.0, 'direction' => 'max' ], 'tablet_extra' => [ 'value' => 1200.0, 'direction' => 'max' ], 'laptop' => [ 'value' => 1366.0, 'direction' => 'max' ], 'widescreen' => [ 'value' => 2400.0, 'direction' => 'min' ], ]; $breakpoints = []; if ( class_exists( '\\Elementor\\Plugin' ) && isset( \Elementor\Plugin::$instance->breakpoints ) ) { $manager = \Elementor\Plugin::$instance->breakpoints; $active = is_object( $manager ) && method_exists( $manager, 'get_active_breakpoints' ) ? $manager->get_active_breakpoints() : []; if ( is_array( $active ) ) { foreach ( $active as $key => $breakpoint ) { $name = sanitize_key( (string) $key ); if ( is_object( $breakpoint ) && method_exists( $breakpoint, 'get_name' ) ) { $name = sanitize_key( (string) $breakpoint->get_name() ); } if ( '' === $name || 'desktop' === $name ) { continue; } $value = is_object( $breakpoint ) && method_exists( $breakpoint, 'get_value' ) ? (float) $breakpoint->get_value() : (float) ( $fallbacks[ $name ]['value'] ?? 0 ); $breakpoint_direction = is_object( $breakpoint ) && method_exists( $breakpoint, 'get_direction' ) ? strtolower( (string) $breakpoint->get_direction() ) : (string) ( $fallbacks[ $name ]['direction'] ?? ( 'widescreen' === $name ? 'min' : 'max' ) ); if ( $value > 0 && $direction === $breakpoint_direction ) { $breakpoints[ $name ] = $value; } } } } if ( empty( $breakpoints ) ) { foreach ( $fallbacks as $name => $breakpoint ) { if ( $direction === $breakpoint['direction'] ) { $breakpoints[ $name ] = $breakpoint['value']; } } } return $breakpoints; } private function strip_comments( string $css ): string { return preg_replace( '#/\*.*?\*/#s', '', $css ) ?? $css; } private function parse_declarations( string $body ): array { $declarations = []; $parts = $this->split_css_top_level( $body, ';' ); foreach ( $parts as $part ) { $part = trim( $part ); if ( '' === $part || ! str_contains( $part, ':' ) ) { continue; } [ $property, $value ] = array_map( 'trim', explode( ':', $part, 2 ) ); $property = $this->normalize_variable_property_name( $property ); /* * VENDOR PREFIXES. * * This pattern used to demand a letter first, so every `-webkit-*` declaration was * dropped here — silently, with no warning and no `invalid` entry, before the rest of * the pipeline ever saw it. `.text-stroke` is the case that exposed it: the author * writes `-webkit-text-stroke`, the parser discards it, so nothing reaches the atomic * mapper, Elementor stores no `stroke` prop, and the next sync or class editor save * rewrites the source without the declaration. The class rendered as plain * transparent text with no outline at all. * * Nothing downstream needed fixing: the mapper already maps `-webkit-text-stroke` to * Elementor's native `stroke` prop, and a prefixed property it does not know is * returned as unmapped and preserved in custom_css like any other. Only this gate was * wrong. * * The admin's own JavaScript has always accepted `(?:--|-)?`, so the class editor let * an operator author a declaration the server then threw away. This makes the two * agree. A single leading hyphen is a vendor prefix; two is a custom property; the * letter requirement after the prefix still rejects `-`, `---x` and `-1x`. */ if ( '' === $property || '' === $value || ! preg_match( '/^(--|-)?[a-zA-Z][a-zA-Z0-9_-]*$/', $property ) ) { continue; } $declarations[] = [ // A custom property is case-sensitive (--Brand-Gap and --brand-gap are two properties). 'property' => str_starts_with( $property, '--' ) ? $property : strtolower( $property ), 'value' => $this->sanitize_css_value( $value ), ]; } return $declarations; } private function sanitize_css_value( string $value ): string { $value = wp_strip_all_tags( $value ); $value = preg_replace( '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/', '', $value ) ?? $value; return trim( $value ); } private function normalize_dashes( string $css ): string { return str_replace( [ '—', '–' ], '--', $css ); } private function normalize_variable_property_name( string $property ): string { $property = trim( $property ); if ( str_starts_with( $property, '--' ) ) { return $property; } if ( preg_match( '/--([a-zA-Z_][a-zA-Z0-9_-]*)$/', $property, $matches ) ) { return '--' . $matches[1]; } return $property; } }