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