← All changes
|
modules/global-classes/global-class-post.php
+80
-20
4.1.0-dev3
→
4.3.0-beta3
View file →
| @@ -3,8 +3,10 @@ | ||
| 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; | |
| 7 | 9 | use Elementor\Modules\GlobalClasses\Utils\Global_Class_Data_Normalizer; |
| 8 | 10 | use WP_Post; |
| 9 | 11 | |
| 10 | 12 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -46,22 +48,22 @@ | ||
| 46 | 48 | |
| 47 | 49 | return ( new static( $post ) )->set_preview( $is_preview ); |
| 48 | 50 | } |
| 49 | 51 | |
| 50 | - public static function find_by_class_id( string $class_id, bool $is_preview = false ): ?self { | |
| 51 | - $posts = get_posts( [ | |
| 52 | - 'post_type' => Global_Class_Post_Type::CPT, | |
| 53 | - 'post_status' => 'publish', | |
| 54 | - 'posts_per_page' => 1, | |
| 55 | - 'meta_key' => self::META_KEY_ID, | |
| 56 | - 'meta_value' => $class_id, | |
| 57 | - ] ); | |
| 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(); | |
| 58 | 54 | |
| 59 | - if ( empty( $posts ) ) { | |
| 55 | + if ( ! $kit ) { | |
| 60 | 56 | return null; |
| 61 | 57 | } |
| 62 | 58 | |
| 63 | - 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 ); | |
| 64 | 66 | } |
| 65 | 67 | |
| 66 | 68 | public function get_post_id(): int { |
| 67 | 69 | return $this->post->ID; |
| @@ -80,12 +82,8 @@ | ||
| 80 | 82 | public function get_label(): string { |
| 81 | 83 | return $this->post->post_title; |
| 82 | 84 | } |
| 83 | 85 | |
| 84 | - public function get_order(): int { | |
| 85 | - return (int) $this->post->menu_order; | |
| 86 | - } | |
| 87 | - | |
| 88 | 86 | public function get_data( bool $skip_migration = false ): array { |
| 89 | 87 | $data = $this->get_context_data(); |
| 90 | 88 | $meta_key = $this->get_context_key( 'data' ); |
| 91 | 89 | |
| @@ -102,12 +100,8 @@ | ||
| 102 | 100 | return $data; |
| 103 | 101 | } |
| 104 | 102 | |
| 105 | 103 | private function migrate_data( array &$data, string $meta_key ): void { |
| 106 | - if ( ! Migrations_Orchestrator::is_active() ) { | |
| 107 | - return; | |
| 108 | - } | |
| 109 | - | |
| 110 | 104 | $post_id = $this->post->ID; |
| 111 | 105 | |
| 112 | 106 | Migrations_Orchestrator::make()->migrate( |
| 113 | 107 | $data, |
| @@ -198,8 +192,20 @@ | ||
| 198 | 192 | |
| 199 | 193 | return is_array( $data ) ? $data : []; |
| 200 | 194 | } |
| 201 | 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 | + | |
| 202 | 208 | public function update_data( |
| 203 | 209 | array $data, |
| 204 | 210 | string $version = ELEMENTOR_VERSION |
| 205 | 211 | ): bool { |
| @@ -223,9 +229,9 @@ | ||
| 223 | 229 | public static function create( |
| 224 | 230 | string $class_id, |
| 225 | 231 | string $label, |
| 226 | 232 | array $data, |
| 227 | - int $order = 0, | |
| 233 | + ?Kit $kit = null, | |
| 228 | 234 | string $version = ELEMENTOR_VERSION |
| 229 | 235 | ): ?self { |
| 230 | 236 | $post_id = wp_insert_post( [ |
| 231 | 237 | 'post_type' => Global_Class_Post_Type::CPT, |
| @@ -230,9 +236,8 @@ | ||
| 230 | 236 | $post_id = wp_insert_post( [ |
| 231 | 237 | 'post_type' => Global_Class_Post_Type::CPT, |
| 232 | 238 | 'post_title' => $label, |
| 233 | 239 | 'post_status' => 'publish', |
| 234 | - 'menu_order' => $order, | |
| 235 | 240 | ] ); |
| 236 | 241 | |
| 237 | 242 | if ( is_wp_error( $post_id ) || ! $post_id ) { |
| 238 | 243 | return null; |
| @@ -243,8 +248,14 @@ | ||
| 243 | 248 | update_post_meta( $post_id, self::META_KEY_ID, $class_id ); |
| 244 | 249 | update_post_meta( $post_id, self::META_KEY_DATA, $normalized_data ); |
| 245 | 250 | update_post_meta( $post_id, self::META_KEY_VERSION, $version ); |
| 246 | 251 | |
| 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 | + | |
| 247 | 258 | $instance = self::from_post_id( $post_id ); |
| 248 | 259 | |
| 249 | 260 | if ( $instance ) { |
| 250 | 261 | update_post_meta( $post_id, self::META_KEY_EDITED, $instance->get_creation_timestamp() ); |
| @@ -256,6 +267,55 @@ | ||
| 256 | 267 | public function delete(): bool { |
| 257 | 268 | $result = wp_delete_post( $this->post->ID, true ); |
| 258 | 269 | |
| 259 | 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 ); | |
| 260 | 320 | } |
| 261 | 321 | } |