PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / atomic-widgets / styles / local-style.php

local-style.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/atomic-widgets/styles/local-style.php

127 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\Styles;
4
5 if ( ! defined( 'ABSPATH' ) ) {
6 exit;
7 }
8
9 /**
10 * Represents a single stored local style (the first entry in an element's
11 * `styles` map) and knows how to render its variants into the MCP round-trip
12 * CSS string: top-level declarations for desktop, `&:state { ... }` for
13 * pseudo variants, and `@media(--<breakpoint>) { ... }` for non-desktop
14 * breakpoints — merged into one media block per breakpoint.
15 */
16 class Local_Style {
17
18 const DESKTOP_BREAKPOINT = 'desktop';
19
20 private string $id;
21
22 /**
23 * @var Local_Style_Variant[]
24 */
25 private array $variants;
26
27 /**
28 * @param string $id
29 * @param Local_Style_Variant[] $variants
30 */
31 private function __construct( string $id, array $variants ) {
32 $this->id = $id;
33 $this->variants = $variants;
34 }
35
36 public static function from_styles_map( array $styles ): ?self {
37 $entry = self::pick_first_entry( $styles );
38
39 if ( null === $entry ) {
40 return null;
41 }
42
43 $raw_variants = is_array( $entry['variants'] ?? null ) ? $entry['variants'] : [];
44
45 if ( empty( $raw_variants ) ) {
46 return null;
47 }
48
49 $variants = array_map(
50 [ Local_Style_Variant::class, 'from_array' ],
51 $raw_variants
52 );
53
54 return new self( (string) ( $entry['id'] ?? '' ), $variants );
55 }
56
57 public function id(): string {
58 return $this->id;
59 }
60
61 public function to_css(): string {
62 $grouped = $this->group_variants_by_breakpoint();
63
64 $desktop_variants = $grouped[ self::DESKTOP_BREAKPOINT ] ?? [];
65 unset( $grouped[ self::DESKTOP_BREAKPOINT ] );
66
67 $sections = [];
68
69 $desktop_css = $this->concat_fragments( $desktop_variants );
70 if ( '' !== $desktop_css ) {
71 $sections[] = $desktop_css;
72 }
73
74 foreach ( $grouped as $breakpoint => $variants ) {
75 $inner = $this->concat_fragments( $variants );
76
77 if ( '' === $inner ) {
78 continue;
79 }
80
81 $sections[] = sprintf( '@media(--%s) { %s }', $breakpoint, $inner );
82 }
83
84 return implode( "\n", $sections );
85 }
86
87 /**
88 * @return array<string, Local_Style_Variant[]>
89 */
90 private function group_variants_by_breakpoint(): array {
91 $grouped = [];
92
93 foreach ( $this->variants as $variant ) {
94 $grouped[ $variant->breakpoint() ][] = $variant;
95 }
96
97 return $grouped;
98 }
99
100 /**
101 * @param Local_Style_Variant[] $variants
102 */
103 private function concat_fragments( array $variants ): string {
104 $fragments = [];
105
106 foreach ( $variants as $variant ) {
107 $fragment = $variant->to_css_fragment();
108
109 if ( '' !== $fragment ) {
110 $fragments[] = $fragment;
111 }
112 }
113
114 return implode( ' ', $fragments );
115 }
116
117 private static function pick_first_entry( array $styles ): ?array {
118 if ( empty( $styles ) ) {
119 return null;
120 }
121
122 $first = reset( $styles );
123
124 return is_array( $first ) ? $first : null;
125 }
126 }
127