, counts: array} */ public static function classify( array $bundled_items, array $stored_items, array $history, array $state ): array { $bundled = Class_History::group_by_class( $bundled_items ); $stored = Class_History::group_by_class( $stored_items ); $present = array_flip( array_map( 'strval', (array) ( $state['present'] ?? [] ) ) ); $drift = isset( $state['drift'] ) && is_array( $state['drift'] ) ? array_flip( array_map( 'strval', $state['drift'] ) ) : null; $drafts = isset( $state['drafts'] ) && is_array( $state['drafts'] ) ? array_flip( array_map( 'strval', $state['drafts'] ) ) : null; $variables = array_flip( array_map( 'strval', (array) ( $state['variables'] ?? [] ) ) ); $shipped = array_flip( array_map( 'strval', (array) ( $state['shipped_variables'] ?? [] ) ) ); $only = isset( $state['only'] ) && is_array( $state['only'] ) ? array_flip( array_map( 'strval', $state['only'] ) ) : null; $rows = []; foreach ( $bundled as $label => $b_items ) { if ( ! isset( $stored[ $label ] ) || ( null !== $only && ! isset( $only[ $label ] ) ) ) { continue; // a class the site lacks is Add Missing Defaults' job } $s_hash = Class_History::definition_hash( $stored[ $label ] ); $b_hash = Class_History::definition_hash( $b_items ); if ( $s_hash === $b_hash ) { continue; } $row = [ 'label' => (string) $label, 'group' => self::GROUP_UPDATE, 'reason' => '', 'ticked' => false, 'released' => Class_History::releases_for( $history, (string) $label, $s_hash ), 'changes' => self::describe_changes( $stored[ $label ], $b_items ), 'needs' => [], 'from' => $s_hash, 'to' => $b_hash, ]; $needs = array_values( array_filter( self::referenced_variables( $b_items ), static fn( string $name ): bool => isset( $shipped[ $name ] ) && ! isset( $variables[ $name ] ) ) ); if ( ! isset( $present[ $label ] ) ) { $row['group'] = self::GROUP_SKIPPED; $row['reason'] = 'not_in_elementor'; } elseif ( null === $drift ) { $row['group'] = self::GROUP_SKIPPED; $row['reason'] = 'unreadable'; } elseif ( isset( $drift[ $label ] ) ) { $row['group'] = self::GROUP_CHANGED; $row['reason'] = 'changed_in_elementor'; } elseif ( null === $drafts ) { $row['group'] = self::GROUP_SKIPPED; $row['reason'] = 'unreadable'; } elseif ( isset( $drafts[ $label ] ) ) { $row['group'] = self::GROUP_SKIPPED; $row['reason'] = 'draft'; } elseif ( ! empty( $needs ) ) { $row['group'] = self::GROUP_SKIPPED; $row['reason'] = 'needs_tokens'; $row['needs'] = $needs; } else { $row['ticked'] = ! empty( $row['released'] ); $row['reason'] = empty( $row['released'] ) ? 'unknown_version' : ''; } $rows[] = $row; } usort( $rows, static fn( array $a, array $b ): int => strcmp( $a['label'], $b['label'] ) ); $counts = [ self::GROUP_UPDATE => 0, self::GROUP_CHANGED => 0, self::GROUP_SKIPPED => 0 ]; foreach ( $rows as $row ) { ++$counts[ $row['group'] ]; } return [ 'rows' => $rows, 'counts' => $counts ]; } /** * How many shipped classes this site has whose stored definition differs from the bundled one, without reading * Elementor. The Classes card shows the line only when this is above zero. */ public static function changed_count( array $bundled_items, array $stored_items ): int { $stored = Class_History::group_by_class( $stored_items ); $count = 0; foreach ( Class_History::group_by_class( $bundled_items ) as $label => $b_items ) { if ( isset( $stored[ $label ] ) && Class_History::definition_hash( $stored[ $label ] ) !== Class_History::definition_hash( $b_items ) ) { ++$count; } } return $count; } /** * What changes from S to B, variant by variant, in CSS terms. * * @return array */ public static function describe_changes( array $from_items, array $to_items ): array { $from = self::by_variant( $from_items ); $to = self::by_variant( $to_items ); $changes = []; foreach ( $to as $key => $variant ) { if ( ! isset( $from[ $key ] ) ) { $changes[] = [ 'kind' => 'add_variant', 'media' => $variant['media'], 'state' => $variant['state'], 'declarations' => $variant['declarations'] ]; continue; } $before = $from[ $key ]['declarations']; foreach ( $variant['declarations'] as $property => $value ) { if ( ! array_key_exists( $property, $before ) ) { $changes[] = [ 'kind' => 'add', 'media' => $variant['media'], 'state' => $variant['state'], 'property' => $property, 'to' => $value ]; } elseif ( $before[ $property ] !== $value ) { $changes[] = [ 'kind' => 'change', 'media' => $variant['media'], 'state' => $variant['state'], 'property' => $property, 'from' => $before[ $property ], 'to' => $value ]; } } foreach ( $before as $property => $value ) { if ( ! array_key_exists( $property, $variant['declarations'] ) ) { $changes[] = [ 'kind' => 'remove', 'media' => $variant['media'], 'state' => $variant['state'], 'property' => $property, 'from' => $value ]; } } } foreach ( $from as $key => $variant ) { if ( ! isset( $to[ $key ] ) ) { $changes[] = [ 'kind' => 'remove_variant', 'media' => $variant['media'], 'state' => $variant['state'], 'declarations' => $variant['declarations'] ]; } } return $changes; } /** * The items to send to the importer to turn S into B for one class: every variant of B, plus a `remove_variant` * item for each variant S has and B does not (the importer leaves a variant it is not sent alone). */ public static function import_items( array $from_items, array $to_items ): array { $to = self::by_variant( $to_items ); $items = array_values( $to_items ); foreach ( self::by_variant( $from_items ) as $key => $variant ) { if ( ! isset( $to[ $key ] ) ) { $item = $variant['item']; $item['declarations'] = []; $item['remove_variant'] = true; $items[] = $item; } } return $items; } /** * The `--name` tokens a class's declarations reference through var(). * * @return string[] */ public static function referenced_variables( array $items ): array { $names = []; foreach ( $items as $item ) { foreach ( (array) ( $item['declarations'] ?? [] ) as $value ) { if ( preg_match_all( '/var\(\s*(--[A-Za-z0-9_-]+)/', (string) $value, $matches ) ) { foreach ( $matches[1] as $name ) { $names[ $name ] = true; } } } } return array_keys( $names ); } /** * Replace whole classes in the stored source, in place. * * For each class named: a rule whose variant the new definition has gets the new declarations in its own place; * a rule whose variant it lacks (or a second rule for the same variant) is removed; a new top-level variant goes * right after the class's last top-level rule; a new @media variant is appended in its own @media block (save_css() * puts @media blocks in order). Every other rule, comment and line is left byte for byte. * * @param array> $classes Class name => its new parsed items. */ public static function replace_in_source( string $css, array $classes ): string { if ( empty( $classes ) ) { return $css; } $wanted = []; $used = []; $last_top = []; $edits = []; foreach ( $classes as $label => $items ) { $wanted[ $label ] = self::by_variant( $items ); } foreach ( Elementor_Design_System_Service::class_rule_spans( $css ) as $rule ) { if ( ! preg_match( '/^\.([a-zA-Z_-][a-zA-Z0-9_-]*)(?::([a-zA-Z][a-zA-Z0-9-]*))?$/', $rule['selector'], $parts ) || ! isset( $wanted[ $parts[1] ] ) ) { continue; } $label = $parts[1]; $key = Class_History::variant_key( [ 'media_query' => $rule['media'], 'state' => $parts[2] ?? '' ] ); if ( isset( $wanted[ $label ][ $key ] ) && empty( $used[ $label ][ $key ] ) ) { $used[ $label ][ $key ] = true; // New top-level variants go after the last top-level rule that stays. if ( '' === $rule['media'] ) { $last_top[ $label ] = $rule; } $body = self::format_body( $wanted[ $label ][ $key ]['declarations'], $rule['indent'] . ' ' ) . "\n" . $rule['indent']; $edits[] = [ $rule['body_start'], $rule['body_start'] + strlen( $rule['body'] ), $body ]; } else { $edits[] = [ $rule['start'], $rule['end'], '' ]; } } $appended = []; foreach ( $wanted as $label => $variants ) { $top_blocks = []; foreach ( $variants as $key => $variant ) { if ( ! empty( $used[ $label ][ $key ] ) ) { continue; } $selector = '.' . $label . ( '' === $variant['state'] ? '' : ':' . $variant['state'] ); if ( '' === $variant['media'] && isset( $last_top[ $label ] ) ) { $indent = $last_top[ $label ]['indent']; $top_blocks[] = $indent . $selector . " {" . self::format_body( $variant['declarations'], $indent . ' ' ) . "\n" . $indent . '}'; } elseif ( '' === $variant['media'] ) { $appended[] = $selector . " {" . self::format_body( $variant['declarations'], ' ' ) . "\n}"; } else { $appended[] = '@media ' . $variant['media'] . " {\n " . $selector . " {" . self::format_body( $variant['declarations'], ' ' ) . "\n }\n}"; } } if ( ! empty( $top_blocks ) ) { $end = $last_top[ $label ]['end']; $edits[] = [ $end, $end, "\n\n" . implode( "\n\n", $top_blocks ) ]; } } usort( $edits, static fn( $a, $b ) => [ $b[0], $b[1] ] <=> [ $a[0], $a[1] ] ); foreach ( $edits as [ $start, $end, $text ] ) { // A removed rule takes its line break with it, and the blank line after it when one is left before it. if ( '' === $text && $end > $start && "\n" === ( $css[ $end ] ?? '' ) ) { ++$end; if ( "\n" === ( $css[ $end ] ?? '' ) && ( 0 === $start || "\n\n" === substr( $css, max( 0, $start - 2 ), min( 2, $start ) ) ) ) { ++$end; } } // The last rule of a block or of the file also takes the blank line before it. if ( '' === $text && $start >= 2 && "\n\n" === substr( $css, $start - 2, 2 ) && preg_match( '/^[ \t]*(?:\}|$)/', substr( $css, $end ) ) ) { --$start; } $css = substr_replace( $css, $text, $start, $end - $start ); } // A media block whose rules are all gone. $css = (string) preg_replace( '/^[ \t]*@media[^{]*\{\s*\}[ \t]*(?:\n(?:[ \t]*\n)?|$)/m', '', $css ); if ( ! empty( $appended ) ) { $css = rtrim( $css ) . "\n\n" . implode( "\n\n", $appended ) . "\n"; } return $css; } /** * A class's definition as CSS text, for the undo record and its JSON download. * * @param array $items */ public static function to_css( array $items ): string { $blocks = []; foreach ( $items as $item ) { $selector = (string) ( $item['selector'] ?? '' ); $media = trim( (string) ( $item['media_query'] ?? '' ) ); if ( '' === $selector ) { continue; } $blocks[] = '' === $media ? $selector . " {" . self::format_body( (array) ( $item['declarations'] ?? [] ), ' ' ) . "\n}" : '@media ' . $media . " {\n " . $selector . " {" . self::format_body( (array) ( $item['declarations'] ?? [] ), ' ' ) . "\n }\n}"; } return implode( "\n\n", $blocks ); } /** * The undo record for a run, before anything is written. * * @param array $before Class name => stored items before the update. * @param array $after Class name => definition hash written. */ public static function undo_record( array $before, array $after, int $user, int $time ): array { $classes = []; foreach ( $before as $label => $items ) { $classes[ (string) $label ] = [ 'css' => self::to_css( $items ), 'after' => (string) ( $after[ $label ] ?? '' ), ]; } return [ 'schema' => self::UNDO_SCHEMA, 'time' => $time, 'user' => $user, 'classes' => $classes ]; } /** The stored undo record, or null when there is none or it is malformed. */ public static function undo_from( $stored ): ?array { if ( ! is_array( $stored ) || self::UNDO_SCHEMA !== ( $stored['schema'] ?? null ) || empty( $stored['classes'] ) || ! is_array( $stored['classes'] ) ) { return null; } foreach ( $stored['classes'] as $label => $entry ) { if ( ! is_string( $label ) || ! is_array( $entry ) || ! is_string( $entry['css'] ?? null ) || ! is_string( $entry['after'] ?? null ) ) { return null; } } return $stored; } /** * Variants keyed by Class_History::variant_key(), later rules of the same variant merged over earlier ones (what * a repeated selector means in CSS, and what the importer does). * * @return array,item:array}> */ private static function by_variant( array $items ): array { $out = []; foreach ( $items as $item ) { if ( ! is_array( $item ) ) { continue; } $key = Class_History::variant_key( $item ); $declarations = []; foreach ( (array) ( $item['declarations'] ?? [] ) as $property => $value ) { $declarations[ strtolower( trim( (string) $property ) ) ] = Class_History::normalise_value( (string) $value ); } if ( isset( $out[ $key ] ) ) { $out[ $key ]['declarations'] = array_merge( $out[ $key ]['declarations'], $declarations ); continue; } $out[ $key ] = [ 'media' => trim( (string) ( $item['media_query'] ?? '' ) ), 'state' => strtolower( trim( (string) ( $item['state'] ?? '' ) ) ), 'declarations' => $declarations, 'item' => $item, ]; } return $out; } private static function format_body( array $declarations, string $indent ): string { $lines = []; foreach ( $declarations as $property => $value ) { $lines[] = $indent . trim( (string) $property ) . ': ' . trim( (string) $value ) . ';'; } return "\n" . implode( "\n", $lines ); } }