| @@ -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, |
| @@ -196,8 +192,20 @@ | ||
| 196 | 192 | |
| 197 | 193 | return is_array( $data ) ? $data : []; |
| 198 | 194 | } |
| 199 | 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 | + | |
| 200 | 208 | public function update_data( |
| 201 | 209 | array $data, |
| 202 | 210 | string $version = ELEMENTOR_VERSION |
| 203 | 211 | ): bool { |
| @@ -259,6 +267,55 @@ | ||
| 259 | 267 | public function delete(): bool { |
| 260 | 268 | $result = wp_delete_post( $this->post->ID, true ); |
| 261 | 269 | |
| 262 | 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 ); | |
| 263 | 320 | } |
| 264 | 321 | } |