PluginProbe
Elementor Website Builder – more than just a page builder / 4.1.5
Elementor Website Builder – more than just a page builder v4.1.5
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 / global-classes / global-class-post.php

global-class-post.php in Elementor Website Builder – more than just a page builder 4.1.5, at modules/global-classes/global-class-post.php

265 lines 6.5 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\GlobalClasses;
4
5 use Elementor\Modules\AtomicWidgets\PropTypeMigrations\Migrations_Orchestrator;
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 use WP_Post;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit; // Exit if accessed directly.
14 }
15
16 class Global_Class_Post {
17 use Has_Preview_Context;
18
19 const META_KEY_VERSION = '_elementor_version';
20 const META_KEY_ID = '_elementor_global_class_id';
21 const META_KEY_DATA = '_elementor_global_class_data';
22 const META_KEY_DATA_PREVIEW = '_elementor_global_class_data_preview';
23 const META_KEY_EDITED = '_elementor_global_class_edited';
24
25 protected array $context_keys = [
26 'data' => [
27 'frontend' => self::META_KEY_DATA,
28 'preview' => self::META_KEY_DATA_PREVIEW,
29 ],
30 ];
31
32 private WP_Post $post;
33
34 private function __construct( WP_Post $post ) {
35 $this->post = $post;
36 }
37
38 public static function from_post( WP_Post $post, bool $is_preview = false ): self {
39 return ( new static( $post ) )->set_preview( $is_preview );
40 }
41
42 public static function from_post_id( int $post_id, bool $is_preview = false ): ?self {
43 $post = get_post( $post_id );
44
45 if ( ! $post || Global_Class_Post_Type::CPT !== $post->post_type ) {
46 return null;
47 }
48
49 return ( new static( $post ) )->set_preview( $is_preview );
50 }
51
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();
54
55 if ( ! $kit ) {
56 return null;
57 }
58
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 );
66 }
67
68 public function get_post_id(): int {
69 return $this->post->ID;
70 }
71
72 public function get_class_id(): string {
73 $class_id = get_post_meta( $this->post->ID, self::META_KEY_ID, true );
74
75 if ( ! $class_id ) {
76 return '';
77 }
78
79 return $class_id;
80 }
81
82 public function get_label(): string {
83 return $this->post->post_title;
84 }
85
86 public function get_data( bool $skip_migration = false ): array {
87 $data = $this->get_context_data();
88 $meta_key = $this->get_context_key( 'data' );
89
90 // Empty preview (draft) - use frontend (published) data
91 if ( empty( $data ) && $this->is_preview() ) {
92 $data = $this->get_frontend_data();
93 $meta_key = self::META_KEY_DATA;
94 }
95
96 if ( ! empty( $data ) && ( ! $skip_migration ) ) {
97 $this->migrate_data( $data, $meta_key );
98 }
99
100 return $data;
101 }
102
103 private function migrate_data( array &$data, string $meta_key ): void {
104 if ( ! Migrations_Orchestrator::is_active() ) {
105 return;
106 }
107
108 $post_id = $this->post->ID;
109
110 Migrations_Orchestrator::make()->migrate(
111 $data,
112 $post_id,
113 $meta_key,
114 function ( $migrated ) use ( $post_id, $meta_key ) {
115 update_post_meta( $post_id, $meta_key, $migrated );
116 clean_post_cache( $post_id );
117 }
118 );
119 }
120
121 public function to_array( bool $skip_migration = false ): array {
122 $data = $this->get_data( $skip_migration );
123
124 $class_id = $this->get_class_id();
125
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' );
148 }
149
150 return (int) strtotime( (string) $this->post->post_date );
151 }
152
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 public function update_label( string $label ): bool {
176 $result = wp_update_post( [
177 'ID' => $this->post->ID,
178 'post_title' => $label,
179 ] );
180
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 return ! is_wp_error( $result );
186 }
187
188 private function get_context_data(): array {
189 $data = get_post_meta( $this->post->ID, $this->get_context_key( 'data' ), true );
190
191 return is_array( $data ) ? $data : [];
192 }
193
194 private function get_frontend_data(): array {
195 $data = get_post_meta( $this->post->ID, self::META_KEY_DATA, true );
196
197 return is_array( $data ) ? $data : [];
198 }
199
200 public function update_data(
201 array $data,
202 string $version = ELEMENTOR_VERSION
203 ): bool {
204 $meta_key = $this->get_context_key( 'data' );
205
206 $result = update_post_meta( $this->post->ID, $meta_key, $data );
207
208 if ( ! $this->is_preview() ) {
209 delete_post_meta( $this->post->ID, self::META_KEY_DATA_PREVIEW );
210 }
211
212 update_post_meta( $this->post->ID, self::META_KEY_VERSION, $version );
213
214 if ( ! $this->is_preview() ) {
215 update_post_meta( $this->post->ID, self::META_KEY_EDITED, $this->get_current_timestamp() );
216 }
217
218 return false !== $result;
219 }
220
221 public static function create(
222 string $class_id,
223 string $label,
224 array $data,
225 ?Kit $kit = null,
226 string $version = ELEMENTOR_VERSION
227 ): ?self {
228 $post_id = wp_insert_post( [
229 'post_type' => Global_Class_Post_Type::CPT,
230 'post_title' => $label,
231 'post_status' => 'publish',
232 ] );
233
234 if ( is_wp_error( $post_id ) || ! $post_id ) {
235 return null;
236 }
237
238 $normalized_data = Global_Class_Data_Normalizer::normalize_style_fields( $data );
239
240 update_post_meta( $post_id, self::META_KEY_ID, $class_id );
241 update_post_meta( $post_id, self::META_KEY_DATA, $normalized_data );
242 update_post_meta( $post_id, self::META_KEY_VERSION, $version );
243
244 $kit = $kit ?? Plugin::$instance->kits_manager->get_active_kit();
245
246 if ( $kit ) {
247 Global_Classes_Post_IDs::make( $kit )->set( $class_id, (int) $post_id );
248 }
249
250 $instance = self::from_post_id( $post_id );
251
252 if ( $instance ) {
253 update_post_meta( $post_id, self::META_KEY_EDITED, $instance->get_creation_timestamp() );
254 }
255
256 return $instance;
257 }
258
259 public function delete(): bool {
260 $result = wp_delete_post( $this->post->ID, true );
261
262 return false !== $result;
263 }
264 }
265