| @@ -196,8 +196,20 @@ | ||
| 196 | 196 | |
| 197 | 197 | return is_array( $data ) ? $data : []; |
| 198 | 198 | } |
| 199 | 199 | |
| 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 | + | |
| 200 | 212 | public function update_data( |
| 201 | 213 | array $data, |
| 202 | 214 | string $version = ELEMENTOR_VERSION |
| 203 | 215 | ): bool { |
| @@ -259,6 +271,47 @@ | ||
| 259 | 271 | public function delete(): bool { |
| 260 | 272 | $result = wp_delete_post( $this->post->ID, true ); |
| 261 | 273 | |
| 262 | 274 | 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 ); | |
| 263 | 316 | } |
| 264 | 317 | } |