PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.0-beta1
Elementor Website Builder – more than just a page builder v4.1.0-beta1
4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 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 All 456 releases
← All changes | modules/global-classes/global-class-post.php +29 -140 4.2.0-dev2 → 4.1.0-beta1 View file →
@@ -3,11 +3,8 @@
3 3 namespace Elementor\Modules\GlobalClasses;
4 4
5 5 use Elementor\Modules\AtomicWidgets\PropTypeMigrations\Migrations_Orchestrator;
6 6 use Elementor\Modules\GlobalClasses\Concerns\Has_Preview_Context;
7 -use Elementor\Plugin;
8 -use Elementor\Core\Kits\Documents\Kit;
9 -use Elementor\Modules\GlobalClasses\Utils\Global_Class_Data_Normalizer;
10 7 use WP_Post;
11 8
12 9 if ( ! defined( 'ABSPATH' ) ) {
13 10 exit; // Exit if accessed directly.
@@ -19,9 +16,8 @@
19 16 const META_KEY_VERSION = '_elementor_version';
20 17 const META_KEY_ID = '_elementor_global_class_id';
21 18 const META_KEY_DATA = '_elementor_global_class_data';
22 19 const META_KEY_DATA_PREVIEW = '_elementor_global_class_data_preview';
23 - const META_KEY_EDITED = '_elementor_global_class_edited';
24 20
25 21 protected array $context_keys = [
26 22 'data' => [
27 23 'frontend' => self::META_KEY_DATA,
@@ -35,9 +31,9 @@
35 31 $this->post = $post;
36 32 }
37 33
38 34 public static function from_post( WP_Post $post, bool $is_preview = false ): self {
39 - return ( new static( $post ) )->set_preview( $is_preview );
35 + return ( new self( $post ) )->set_preview( $is_preview );
40 36 }
41 37
42 38 public static function from_post_id( int $post_id, bool $is_preview = false ): ?self {
43 39 $post = get_post( $post_id );
@@ -45,25 +41,25 @@
45 41 if ( ! $post || Global_Class_Post_Type::CPT !== $post->post_type ) {
46 42 return null;
47 43 }
48 44
49 - return ( new static( $post ) )->set_preview( $is_preview );
45 + return ( new self( $post ) )->set_preview( $is_preview );
50 46 }
51 47
52 - public static function find_by_class_id( string $class_id, bool $is_preview = false, ?Kit $kit = null ): ?self {
53 - $kit = $kit ?? Plugin::$instance->kits_manager->get_active_kit();
48 + public static function find_by_class_id( string $class_id, bool $is_preview = false ): ?self {
49 + $posts = get_posts( [
50 + 'post_type' => Global_Class_Post_Type::CPT,
51 + 'post_status' => 'publish',
52 + 'posts_per_page' => 1,
53 + 'meta_key' => self::META_KEY_ID,
54 + 'meta_value' => $class_id,
55 + ] );
54 56
55 - if ( ! $kit ) {
57 + if ( empty( $posts ) ) {
56 58 return null;
57 59 }
58 60
59 - $post_id = Global_Classes_Post_IDs::make( $kit )->get_post_id( $class_id );
60 -
61 - if ( ! $post_id ) {
62 - return null;
63 - }
64 -
65 - return self::from_post_id( $post_id, $is_preview );
61 + return ( new self( $posts[0] ) )->set_preview( $is_preview );
66 62 }
67 63
68 64 public function get_post_id(): int {
69 65 return $this->post->ID;
@@ -82,8 +78,12 @@
82 78 public function get_label(): string {
83 79 return $this->post->post_title;
84 80 }
85 81
82 + public function get_order(): int {
83 + return (int) $this->post->menu_order;
84 + }
85 +
86 86 public function get_data( bool $skip_migration = false ): array {
87 87 $data = $this->get_context_data();
88 88 $meta_key = $this->get_context_key( 'data' );
89 89
@@ -120,59 +120,22 @@
120 120
121 121 public function to_array( bool $skip_migration = false ): array {
122 122 $data = $this->get_data( $skip_migration );
123 123
124 - $class_id = $this->get_class_id();
124 + $result = [
125 + 'id' => $this->get_class_id(),
126 + 'label' => $this->get_label(),
127 + 'type' => $data['type'] ?? 'class',
128 + 'variants' => $data['variants'] ?? [],
129 + ];
125 130
126 - return Global_Class_Data_Normalizer::normalize_style(
127 - $class_id,
128 - array_merge( $data, [ 'label' => $this->get_label() ] )
129 - );
130 - }
131 -
132 - public function was_edited(): bool {
133 - $last_edited = $this->get_last_edited_timestamp();
134 - $creation = $this->get_creation_timestamp();
135 -
136 - return $last_edited > $creation;
137 - }
138 -
139 - public function has_edit_timestamp(): bool {
140 - return $this->get_last_edited_timestamp() > 0;
141 - }
142 -
143 - private function get_creation_timestamp(): int {
144 - $dt = get_post_datetime( $this->post, 'date', 'gmt' );
145 -
146 - if ( $dt ) {
147 - return (int) $dt->format( 'U' );
131 + if ( array_key_exists( 'sync_to_v3', $data ) ) {
132 + $result['sync_to_v3'] = (bool) $data['sync_to_v3'];
148 133 }
149 134
150 - return (int) strtotime( (string) $this->post->post_date );
135 + return $result;
151 136 }
152 137
153 - private function get_last_edited_timestamp(): int {
154 - $raw = get_post_meta( $this->post->ID, self::META_KEY_EDITED, true );
155 -
156 - if ( self::is_timestamp( $raw ) ) {
157 - return (int) $raw;
158 - }
159 -
160 - return 0;
161 - }
162 -
163 - private static function is_timestamp( $value ): bool {
164 - if ( ! is_numeric( $value ) ) {
165 - return false;
166 - }
167 -
168 - return (int) $value > 0;
169 - }
170 -
171 - protected function get_current_timestamp(): int {
172 - return (int) time();
173 - }
174 -
175 138 public function update_label( string $label ): bool {
176 139 $result = wp_update_post( [
177 140 'ID' => $this->post->ID,
178 141 'post_title' => $label,
@@ -177,12 +140,8 @@
177 140 'ID' => $this->post->ID,
178 141 'post_title' => $label,
179 142 ] );
180 143
181 - if ( ! is_wp_error( $result ) && ! $this->is_preview() ) {
182 - update_post_meta( $this->post->ID, self::META_KEY_EDITED, $this->get_current_timestamp() );
183 - }
184 -
185 144 return ! is_wp_error( $result );
186 145 }
187 146
188 147 private function get_context_data(): array {
@@ -196,20 +155,8 @@
196 155
197 156 return is_array( $data ) ? $data : [];
198 157 }
199 158
200 - private function get_preview_data(): array {
201 - $data = get_post_meta( $this->post->ID, self::META_KEY_DATA_PREVIEW, true );
202 -
203 - return is_array( $data ) ? $data : [];
204 - }
205 -
206 - private function get_version(): string {
207 - $version = get_post_meta( $this->post->ID, self::META_KEY_VERSION, true );
208 -
209 - return is_string( $version ) ? $version : '';
210 - }
211 -
212 159 public function update_data(
213 160 array $data,
214 161 string $version = ELEMENTOR_VERSION
215 162 ): bool {
@@ -222,12 +169,8 @@
222 169 }
223 170
224 171 update_post_meta( $this->post->ID, self::META_KEY_VERSION, $version );
225 172
226 - if ( ! $this->is_preview() ) {
227 - update_post_meta( $this->post->ID, self::META_KEY_EDITED, $this->get_current_timestamp() );
228 - }
229 -
230 173 return false !== $result;
231 174 }
232 175
233 176 public static function create(
@@ -233,9 +176,9 @@
233 176 public static function create(
234 177 string $class_id,
235 178 string $label,
236 179 array $data,
237 - ?Kit $kit = null,
180 + int $order = 0,
238 181 string $version = ELEMENTOR_VERSION
239 182 ): ?self {
240 183 $post_id = wp_insert_post( [
241 184 'post_type' => Global_Class_Post_Type::CPT,
@@ -240,8 +183,9 @@
240 183 $post_id = wp_insert_post( [
241 184 'post_type' => Global_Class_Post_Type::CPT,
242 185 'post_title' => $label,
243 186 'post_status' => 'publish',
187 + 'menu_order' => $order,
244 188 ] );
245 189
246 190 if ( is_wp_error( $post_id ) || ! $post_id ) {
247 191 return null;
@@ -246,27 +190,13 @@
246 190 if ( is_wp_error( $post_id ) || ! $post_id ) {
247 191 return null;
248 192 }
249 193
250 - $normalized_data = Global_Class_Data_Normalizer::normalize_style_fields( $data );
251 -
252 194 update_post_meta( $post_id, self::META_KEY_ID, $class_id );
253 - update_post_meta( $post_id, self::META_KEY_DATA, $normalized_data );
195 + update_post_meta( $post_id, self::META_KEY_DATA, $data );
254 196 update_post_meta( $post_id, self::META_KEY_VERSION, $version );
255 197
256 - $kit = $kit ?? Plugin::$instance->kits_manager->get_active_kit();
257 -
258 - if ( $kit ) {
259 - Global_Classes_Post_IDs::make( $kit )->set( $class_id, (int) $post_id );
260 - }
261 -
262 - $instance = self::from_post_id( $post_id );
263 -
264 - if ( $instance ) {
265 - update_post_meta( $post_id, self::META_KEY_EDITED, $instance->get_creation_timestamp() );
266 - }
267 -
268 - return $instance;
198 + return self::from_post_id( $post_id );
269 199 }
270 200
271 201 public function delete(): bool {
272 202 $result = wp_delete_post( $this->post->ID, true );
@@ -271,47 +201,6 @@
271 201 public function delete(): bool {
272 202 $result = wp_delete_post( $this->post->ID, true );
273 203
274 204 return false !== $result;
275 - }
276 -
277 - public static function clone_to_other_kit( string $style_id, Kit $source_kit, Kit $target_kit ): ?Global_Class_Post {
278 - $source_post = self::find_by_class_id( $style_id, false, $source_kit );
279 -
280 - if ( ! $source_post ) {
281 - return null;
282 - }
283 -
284 - $new_post_id = wp_insert_post( [
285 - 'post_type' => Global_Class_Post_Type::CPT,
286 - 'post_title' => $source_post->get_label(),
287 - 'post_status' => 'publish',
288 - ] );
289 -
290 - if ( is_wp_error( $new_post_id ) || ! $new_post_id ) {
291 - return null;
292 - }
293 -
294 - update_post_meta( $new_post_id, self::META_KEY_ID, $style_id );
295 - update_post_meta( $new_post_id, self::META_KEY_VERSION, $source_post->get_version() );
296 -
297 - $frontend_data = $source_post->get_frontend_data();
298 - $preview_data = $source_post->get_preview_data();
299 - $last_edited_timestamp = get_post_meta( $source_post->get_post_id(), self::META_KEY_EDITED, true );
300 -
301 - if ( ! empty( $frontend_data ) ) {
302 - update_post_meta( $new_post_id, self::META_KEY_DATA, $frontend_data );
303 - }
304 -
305 - if ( ! empty( $preview_data ) ) {
306 - update_post_meta( $new_post_id, self::META_KEY_DATA_PREVIEW, $preview_data );
307 - }
308 -
309 - if ( $last_edited_timestamp ) {
310 - update_post_meta( $new_post_id, self::META_KEY_EDITED, $last_edited_timestamp );
311 - }
312 -
313 - Global_Classes_Post_IDs::make( $target_kit )->set( $style_id, (int) $new_post_id );
314 -
315 - return self::from_post_id( $new_post_id );
316 205 }
317 206 }