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

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