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

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