PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.1
Elementor Website Builder – more than just a page builder v4.2.1
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 4.0.7 All 451 releases
elementor / modules / variables / storage / entities / variable.php

variable.php in Elementor Website Builder – more than just a page builder 4.2.1, at modules/variables/storage/entities/variable.php

180 lines 4.0 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\Variables\Storage\Entities;
4
5 use Elementor\Modules\Variables\Adapters\Prop_Type_Adapter;
6 use Elementor\Modules\Variables\PropTypes\Size_Variable_Prop_Type;
7 use Elementor\Modules\Variables\Storage\Exceptions\Type_Mismatch;
8 use Elementor\Modules\Variables\Storage\Exceptions\InvalidVariable;
9 use InvalidArgumentException;
10
11 class Variable {
12 private array $data;
13
14 private function __construct( array $data ) {
15 $this->data = $data;
16 }
17
18 public static function create_new( array $data ): self {
19 $now = gmdate( 'Y-m-d H:i:s' );
20
21 $data['created_at'] = $now;
22 $data['updated_at'] = $now;
23
24 return self::from_array( $data );
25 }
26
27 public static function from_array( array $data ): self {
28 $required = [ 'id', 'type', 'label', 'value' ];
29
30 foreach ( $required as $key ) {
31 if ( ! array_key_exists( $key, $data ) ) {
32 throw new InvalidArgumentException(
33 sprintf(
34 "Missing required field '%s' in %s::from_array()",
35 esc_html( $key ),
36 self::class
37 )
38 );
39 }
40 }
41
42 return new self( $data );
43 }
44
45 public function soft_delete(): void {
46 $this->data['deleted_at'] = $this->now();
47 }
48
49 public function restore(): void {
50 unset( $this->data['deleted_at'] );
51 // TODO to be removed if client is no longer need this
52 unset( $this->data['deleted'] );
53
54 $this->data['updated_at'] = $this->now();
55 }
56
57 private function now() {
58 return gmdate( 'Y-m-d H:i:s' );
59 }
60
61 public function to_array(): array {
62 return array_diff_key( $this->data, array_flip( [ 'id' ] ) );
63 }
64
65 public function id(): string {
66 return $this->data['id'];
67 }
68
69 public function label(): string {
70 return $this->data['label'];
71 }
72
73 public function order(): int {
74 return $this->data['order'];
75 }
76
77 public function value() {
78 return $this->data['value'];
79 }
80
81 public function set_value( $value ) {
82 $this->data['value'] = $value;
83 }
84
85 public function sync_to_v3(): bool {
86 return ! empty( $this->data['sync_to_v3'] );
87 }
88
89 public function set_sync_to_v3( bool $sync_to_v3 ) {
90 $this->data['sync_to_v3'] = $sync_to_v3;
91 }
92
93 public function type() {
94 return $this->data['type'];
95 }
96
97 public function set_type( $type ) {
98 $this->data['type'] = $type;
99 }
100
101 public function has_order(): int {
102 return isset( $this->data['order'] );
103 }
104
105 public function is_deleted(): bool {
106 return isset( $this->data['deleted_at'] );
107 }
108
109 /**
110 * @throws Type_Mismatch If a type that is not allowed to be changed is passed.
111 */
112 private function maybe_apply_type( array $data ) {
113 if ( ! array_key_exists( 'type', $data ) ) {
114 return false;
115 }
116
117 $current_type = $this->type();
118 $target_type = $data['type'];
119
120 if ( $current_type === $target_type ) {
121 return false;
122 }
123
124 $custom_size_prop_type = Prop_Type_Adapter::GLOBAL_CUSTOM_SIZE_VARIABLE_KEY;
125 $size_prop_type = Size_Variable_Prop_Type::get_key();
126
127 $allowed_types = [ $custom_size_prop_type, $size_prop_type ];
128
129 $is_valid_transition =
130 in_array( $current_type, $allowed_types, true ) &&
131 in_array( $target_type, $allowed_types, true );
132
133 if ( ! $is_valid_transition ) {
134 throw new Type_Mismatch( 'Type change is forbidden' );
135 }
136
137 $this->set_type( $data['type'] );
138
139 return true;
140 }
141
142 /**
143 * @throws InvalidVariable If the variable is not valid.
144 */
145 public function apply_changes( array $data ): void {
146 $this->validate();
147
148 $allowed_fields = [ 'label', 'value', 'order', 'type', 'sync_to_v3' ];
149 $has_changes = $this->maybe_apply_type( $data );
150
151 foreach ( $allowed_fields as $field ) {
152 if ( isset( $data[ $field ] ) ) {
153 $this->data[ $field ] = $data[ $field ];
154
155 $has_changes = true;
156 }
157 }
158
159 if ( $has_changes ) {
160 $this->data['updated_at'] = $this->now();
161 }
162 }
163
164 /**
165 * @return bool True if the variable is valid, throws an exception otherwise.
166 * @throws InvalidVariable If the variable is not valid.
167 */
168 public function validate(): bool {
169 if ( strpos( $this->label(), ' ' ) !== false ) {
170 throw new InvalidVariable( 'Label cannot contain spaces' );
171 }
172
173 if ( strlen( $this->label() ) > 50 ) {
174 throw new InvalidVariable( 'Label cannot be longer than 50 characters' );
175 }
176
177 return true;
178 }
179 }
180