PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0
Elementor Website Builder – more than just a page builder v4.1.0
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.1.0, at modules/variables/storage/entities/variable.php

172 lines 3.9 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 type() {
86 return $this->data['type'];
87 }
88
89 public function set_type( $type ) {
90 $this->data['type'] = $type;
91 }
92
93 public function has_order(): int {
94 return isset( $this->data['order'] );
95 }
96
97 public function is_deleted(): bool {
98 return isset( $this->data['deleted_at'] );
99 }
100
101 /**
102 * @throws Type_Mismatch If a type that is not allowed to be changed is passed.
103 */
104 private function maybe_apply_type( array $data ) {
105 if ( ! array_key_exists( 'type', $data ) ) {
106 return false;
107 }
108
109 $current_type = $this->type();
110 $target_type = $data['type'];
111
112 if ( $current_type === $target_type ) {
113 return false;
114 }
115
116 $custom_size_prop_type = Prop_Type_Adapter::GLOBAL_CUSTOM_SIZE_VARIABLE_KEY;
117 $size_prop_type = Size_Variable_Prop_Type::get_key();
118
119 $allowed_types = [ $custom_size_prop_type, $size_prop_type ];
120
121 $is_valid_transition =
122 in_array( $current_type, $allowed_types, true ) &&
123 in_array( $target_type, $allowed_types, true );
124
125 if ( ! $is_valid_transition ) {
126 throw new Type_Mismatch( 'Type change is forbidden' );
127 }
128
129 $this->set_type( $data['type'] );
130
131 return true;
132 }
133
134 /**
135 * @throws InvalidVariable If the variable is not valid.
136 */
137 public function apply_changes( array $data ): void {
138 $this->validate();
139
140 $allowed_fields = [ 'label', 'value', 'order', 'type', 'sync_to_v3' ];
141 $has_changes = $this->maybe_apply_type( $data );
142
143 foreach ( $allowed_fields as $field ) {
144 if ( isset( $data[ $field ] ) ) {
145 $this->data[ $field ] = $data[ $field ];
146
147 $has_changes = true;
148 }
149 }
150
151 if ( $has_changes ) {
152 $this->data['updated_at'] = $this->now();
153 }
154 }
155
156 /**
157 * @return bool True if the variable is valid, throws an exception otherwise.
158 * @throws InvalidVariable If the variable is not valid.
159 */
160 public function validate(): bool {
161 if ( strpos( $this->label(), ' ' ) !== false ) {
162 throw new InvalidVariable( 'Label cannot contain spaces' );
163 }
164
165 if ( strlen( $this->label() ) > 50 ) {
166 throw new InvalidVariable( 'Label cannot be longer than 50 characters' );
167 }
168
169 return true;
170 }
171 }
172