| 1 |
<?php |
| 2 |
|
| 3 |
namespace Elementor\App\Modules\ImportExport\Runners\Import; |
| 4 |
|
| 5 |
use Elementor\App\Modules\ImportExport\Utils as ImportExportUtils; |
| 6 |
use Elementor\Plugin; |
| 7 |
use Elementor\TemplateLibrary\Source_Local; |
| 8 |
use Elementor\Utils; |
| 9 |
|
| 10 |
class Templates extends Import_Runner_Base { |
| 11 |
private $import_session_id; |
| 12 |
|
| 13 |
public static function get_name() : string { |
| 14 |
return 'templates'; |
| 15 |
} |
| 16 |
|
| 17 |
public function should_import( array $data ) { |
| 18 |
return ( |
| 19 |
Utils::has_pro() && |
| 20 |
isset( $data['include'] ) && |
| 21 |
in_array( 'templates', $data['include'], true ) && |
| 22 |
! empty( $data['extracted_directory_path'] ) && |
| 23 |
! empty( $data['manifest']['templates'] ) |
| 24 |
); |
| 25 |
} |
| 26 |
|
| 27 |
public function import( array $data, array $imported_data ) { |
| 28 |
$this->import_session_id = $data['session_id']; |
| 29 |
|
| 30 |
$path = $data['extracted_directory_path'] . 'templates/'; |
| 31 |
$templates = $data['manifest']['templates']; |
| 32 |
|
| 33 |
$result['templates'] = [ |
| 34 |
'succeed' => [], |
| 35 |
'failed' => [], |
| 36 |
]; |
| 37 |
|
| 38 |
foreach ( $templates as $id => $template_settings ) { |
| 39 |
try { |
| 40 |
$template_data = ImportExportUtils::read_json_file( $path . $id ); |
| 41 |
$import = $this->import_template( $id, $template_settings, $template_data ); |
| 42 |
|
| 43 |
$result['templates']['succeed'][ $id ] = $import; |
| 44 |
} catch ( \Exception $error ) { |
| 45 |
$result['templates']['failed'][ $id ] = $error->getMessage(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
return $result; |
| 50 |
} |
| 51 |
|
| 52 |
private function import_template( $id, array $template_settings, array $template_data ) { |
| 53 |
$doc_type = $template_settings['doc_type']; |
| 54 |
|
| 55 |
$new_document = Plugin::$instance->documents->create( |
| 56 |
$doc_type, |
| 57 |
[ |
| 58 |
'post_title' => $template_settings['title'], |
| 59 |
'post_type' => Source_Local::CPT, |
| 60 |
'post_status' => 'publish', |
| 61 |
] |
| 62 |
); |
| 63 |
|
| 64 |
if ( is_wp_error( $new_document ) ) { |
| 65 |
throw new \Exception( $new_document->get_error_message() ); |
| 66 |
} |
| 67 |
|
| 68 |
$template_data['import_settings'] = $template_settings; |
| 69 |
$template_data['id'] = $id; |
| 70 |
|
| 71 |
$new_attachment_callback = function( $attachment_id ) { |
| 72 |
$this->set_session_post_meta( $attachment_id, $this->import_session_id ); |
| 73 |
}; |
| 74 |
|
| 75 |
add_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback ); |
| 76 |
|
| 77 |
$new_document->import( $template_data ); |
| 78 |
|
| 79 |
remove_filter( 'elementor/template_library/import_images/new_attachment', $new_attachment_callback ); |
| 80 |
|
| 81 |
$document_id = $new_document->get_main_id(); |
| 82 |
|
| 83 |
$this->set_session_post_meta( $document_id, $this->import_session_id ); |
| 84 |
|
| 85 |
return $document_id; |
| 86 |
} |
| 87 |
} |
| 88 |
|