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

110 lines 2.2 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 InvalidArgumentException;
6
7 class Variable {
8 private array $data;
9
10 private function __construct( array $data ) {
11 $this->data = $data;
12 }
13
14 public static function create_new( array $data ): self {
15 $now = gmdate( 'Y-m-d H:i:s' );
16
17 $data['created_at'] = $now;
18 $data['updated_at'] = $now;
19
20 return self::from_array( $data );
21 }
22
23 public static function from_array( array $data ): self {
24 $required = [ 'id', 'type', 'label', 'value' ];
25
26 foreach ( $required as $key ) {
27 if ( ! array_key_exists( $key, $data ) ) {
28 throw new InvalidArgumentException(
29 sprintf(
30 "Missing required field '%s' in %s::from_array()",
31 esc_html( $key ),
32 self::class
33 )
34 );
35 }
36 }
37
38 return new self( $data );
39 }
40
41 public function soft_delete(): void {
42 $this->data['deleted_at'] = $this->now();
43 }
44
45 public function restore(): void {
46 unset( $this->data['deleted_at'] );
47 // TODO to be removed if client is no longer need this
48 unset( $this->data['deleted'] );
49
50 $this->data['updated_at'] = $this->now();
51 }
52
53 private function now() {
54 return gmdate( 'Y-m-d H:i:s' );
55 }
56
57 public function to_array(): array {
58 return array_diff_key( $this->data, array_flip( [ 'id' ] ) );
59 }
60
61 public function id(): string {
62 return $this->data['id'];
63 }
64
65 public function label(): string {
66 return $this->data['label'];
67 }
68
69 public function order(): int {
70 return $this->data['order'];
71 }
72
73 public function value() {
74 return $this->data['value'];
75 }
76
77 public function set_value( $value ) {
78 $this->data['value'] = $value;
79 }
80
81 public function type() {
82 return $this->data['type'];
83 }
84
85 public function has_order(): int {
86 return isset( $this->data['order'] );
87 }
88
89 public function is_deleted(): bool {
90 return isset( $this->data['deleted_at'] );
91 }
92
93 public function apply_changes( array $data ): void {
94 $allowed_fields = [ 'label', 'value', 'order' ];
95 $has_changes = false;
96
97 foreach ( $allowed_fields as $field ) {
98 if ( isset( $data[ $field ] ) ) {
99 $this->data[ $field ] = $data[ $field ];
100
101 $has_changes = true;
102 }
103 }
104
105 if ( $has_changes ) {
106 $this->data['updated_at'] = $this->now();
107 }
108 }
109 }
110