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 / global-classes / global-classes-labels.php

global-classes-labels.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/global-classes/global-classes-labels.php

156 lines 3.4 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\GlobalClasses;
4
5 use Elementor\Core\Kits\Documents\Kit;
6 use Elementor\Core\Kits\Concerns\Has_Kit_Dependency;
7 use Elementor\Modules\GlobalClasses\Concerns\Has_Preview_Context;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit;
11 }
12
13 class Global_Classes_Labels {
14 use Has_Kit_Dependency;
15 use Has_Preview_Context;
16
17 const META_KEY_FRONTEND = '_elementor_global_classes_labels';
18 const META_KEY_PREVIEW = '_elementor_global_classes_labels_preview';
19
20 const META_KEY = self::META_KEY_FRONTEND;
21
22 protected array $context_keys = [
23 'labels' => [
24 'frontend' => self::META_KEY_FRONTEND,
25 'preview' => self::META_KEY_PREVIEW,
26 ],
27 ];
28
29 private ?array $cache = null;
30
31 private function __construct() {
32 }
33
34 public static function make( Kit $kit ): self {
35 return ( new self() )->set_kit( $kit );
36 }
37
38 protected function on_preview_change(): void {
39 $this->cache = null;
40 }
41
42 public function get_labels(): array {
43 $stored = $this->read_stored();
44
45 if ( empty( $stored ) ) {
46 return [];
47 }
48
49 return $stored;
50 }
51
52 public function get_ordered_labels(): array {
53 $order = Global_Classes_Order::make( $this->get_kit() )->set_preview( $this->is_preview() )->get_order();
54 $map = $this->get_labels();
55
56 if ( $this->is_preview() ) {
57 $frontend_map = self::make( $this->get_kit() )->get_labels();
58 foreach ( $order as $id ) {
59 if ( ! isset( $map[ $id ] ) && isset( $frontend_map[ $id ] ) ) {
60 $map[ $id ] = $frontend_map[ $id ];
61 }
62 }
63 }
64
65 $result = [];
66
67 foreach ( $order as $id ) {
68 if ( isset( $map[ $id ] ) ) {
69 $result[ $id ] = $map[ $id ];
70 }
71 }
72
73 return $result;
74 }
75
76 public static function generate_unique_label( string $label, array $existing_labels ): string {
77 $prefix = 'DUP_';
78 $max_length = 50;
79
80 $has_prefix = str_starts_with( $label, $prefix );
81
82 if ( $has_prefix ) {
83 $base = substr( $label, strlen( $prefix ) );
84 $counter = 1;
85 $candidate = $prefix . $base . $counter;
86
87 while ( in_array( $candidate, $existing_labels, true ) ) {
88 $candidate = $prefix . $base . ( ++$counter );
89 }
90
91 if ( strlen( $candidate ) > $max_length ) {
92 $base = substr( $base, 0, $max_length - strlen( $prefix . $counter ) );
93 $candidate = $prefix . $base . $counter;
94 }
95
96 return $candidate;
97 }
98
99 $available_length = strlen( $label );
100 $candidate = $prefix . $label;
101
102 if ( strlen( $candidate ) > $max_length ) {
103 $available_length = $max_length - strlen( $prefix );
104 $candidate = $prefix . substr( $label, 0, $available_length );
105 }
106
107 $base = substr( $label, 0, $available_length );
108 $counter = 1;
109
110 while ( in_array( $candidate, $existing_labels, true ) ) {
111 $candidate = $prefix . $base . $counter;
112
113 if ( strlen( $candidate ) > $max_length ) {
114 $base = substr( $label, 0, $max_length - strlen( $prefix . $counter ) );
115 $candidate = $prefix . $base . $counter;
116 }
117
118 ++$counter;
119 }
120
121 return $candidate;
122 }
123
124 public function set_labels( array $id_to_label ): bool {
125 $kit = $this->get_kit();
126
127 if ( ! $kit ) {
128 return false;
129 }
130
131 $result = $kit->update_meta( $this->get_context_key( 'labels' ), $id_to_label );
132 $this->cache = $id_to_label;
133
134 return false !== $result;
135 }
136
137 private function read_stored(): array {
138 if ( null !== $this->cache ) {
139 return $this->cache;
140 }
141
142 $kit = $this->get_kit();
143
144 if ( ! $kit ) {
145 $this->cache = [];
146
147 return [];
148 }
149
150 $raw = $kit->get_meta( $this->get_context_key( 'labels' ) );
151 $this->cache = is_array( $raw ) ? $raw : [];
152
153 return $this->cache;
154 }
155 }
156