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 / variables-collection.php

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

170 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;
4
5 use Elementor\Core\Utils\Collection;
6 use Elementor\Modules\AtomicWidgets\Utils\Utils;
7 use Elementor\Modules\Variables\Storage\Entities\Variable;
8 use Elementor\Modules\Variables\Storage\Exceptions\DuplicatedLabel;
9 use Elementor\Modules\Variables\Storage\Exceptions\RecordNotFound;
10 use Elementor\Modules\Variables\Storage\Exceptions\VariablesLimitReached;
11
12 /**
13 * TODO: a tradeoff when you want to use collection base methods they are
14 * performing immutable process ( creating new instances )
15 * we will see if we need to extend collection as time goes on
16 */
17 class Variables_Collection extends Collection {
18
19 private int $watermark;
20
21 private int $version;
22
23 private function __construct( array $items = [], ?int $watermark = 0, ?int $version = null ) {
24 parent::__construct();
25
26 $this->items = $items;
27 $this->watermark = $watermark;
28 $this->version = $version ?? Constants::FORMAT_VERSION_V1;
29 }
30
31 public static function hydrate( array $record ): self {
32 $variables = [];
33
34 foreach ( $record['data'] ?? [] as $id => $item ) {
35 $data = array_merge( [ 'id' => $id ], $item );
36
37 $variables[ $id ] = Variable::from_array( $data );
38 }
39
40 $watermark = $record['watermark'];
41 $version = $record['version'] ?? null;
42
43 return new self( $variables, $watermark, $version );
44 }
45
46 public function serialize( bool $include_deleted_key = false ): array {
47 $data = [];
48
49 foreach ( $this->all() as $variable ) {
50 $var = $variable->to_array();
51
52 if ( $include_deleted_key && $variable->is_deleted() ) {
53 $var['deleted'] = true;
54 }
55
56 $data[ $variable->id() ] = $var;
57 }
58
59 return [
60 'data' => $data,
61 'watermark' => $this->watermark,
62 'version' => $this->version,
63 ];
64 }
65
66
67 public function set_version( $version ): void {
68 $this->version = $version;
69 }
70
71
72 public static function default(): self {
73 return new self(
74 [],
75 0,
76 Constants::FORMAT_VERSION_V1
77 );
78 }
79
80 public function watermark(): int {
81 return $this->watermark;
82 }
83
84 private function reset_watermark() {
85 $this->watermark = 0;
86 }
87
88 public function increment_watermark() {
89 if ( PHP_INT_MAX === $this->watermark ) {
90 $this->reset_watermark();
91 }
92
93 ++$this->watermark;
94 }
95
96 public function add_variable( Variable $variable ): void {
97 $this->items[ $variable->id() ] = $variable;
98 }
99
100 /**
101 * @throws RecordNotFound When a variable is not found.
102 */
103 public function find_or_fail( string $id ): Variable {
104 $variable = $this->get( $id );
105
106 if ( ! isset( $variable ) ) {
107 throw new RecordNotFound( 'Variable not found' );
108 }
109
110 return $variable;
111 }
112
113 /**
114 * @throws DuplicatedLabel If there is a duplicate label in the database.
115 */
116 public function assert_label_is_unique( string $label, ?string $ignore_id = null ): void {
117 foreach ( $this->all() as $variable ) {
118 if ( $variable->is_deleted() ) {
119 continue;
120 }
121
122 if ( null !== $ignore_id && $variable->id() === $ignore_id ) {
123 continue;
124 }
125
126 if ( strcasecmp( $variable->label(), $label ) === 0 ) {
127 throw new DuplicatedLabel( esc_html( "Variable label '$label' already exists." ) );
128 }
129 }
130 }
131
132 /**
133 * @throws VariablesLimitReached If variable limit reached.
134 */
135 public function assert_limit_not_reached(): void {
136 $active_count = 0;
137
138 foreach ( $this->all() as $variable ) {
139 if ( ! $variable->is_deleted() ) {
140 ++$active_count;
141 }
142 }
143
144 if ( Constants::TOTAL_VARIABLES_COUNT <= $active_count ) {
145 throw new VariablesLimitReached( 'Total variables count limit reached' );
146 }
147 }
148
149 public function next_id(): string {
150 return Utils::generate_id( 'e-gv-', array_keys( $this->all() ) );
151 }
152
153
154 public function get_next_order(): int {
155 $highest_order = 0;
156
157 foreach ( $this->all() as $variable ) {
158 if ( $variable->is_deleted() ) {
159 continue;
160 }
161
162 if ( $variable->has_order() && $variable->order() > $highest_order ) {
163 $highest_order = $variable->order();
164 }
165 }
166
167 return $highest_order + 1;
168 }
169 }
170