| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Elementor\Plugin; |
| 6 |
use Exception; |
| 7 |
use Templately\Core\Importer\Utils\Utils; |
| 8 |
|
| 9 |
class ElementorContent extends BaseRunner { |
| 10 |
public function get_name(): string { |
| 11 |
return 'content'; |
| 12 |
} |
| 13 |
|
| 14 |
public function get_label(): string { |
| 15 |
return __( 'Elementor', 'templately' ); |
| 16 |
} |
| 17 |
|
| 18 |
public function should_run( $data, $imported_data = [] ): bool { |
| 19 |
return $this->manifest['platform'] === 'elementor' && ! empty( $this->manifest['content'] ); |
| 20 |
} |
| 21 |
|
| 22 |
public function should_log(): bool { |
| 23 |
return true; |
| 24 |
} |
| 25 |
|
| 26 |
public function get_action(): string { |
| 27 |
return 'eventLog'; |
| 28 |
} |
| 29 |
|
| 30 |
public function log_message(): string { |
| 31 |
return __( 'Importing Elementor Templates (Pages, Posts etc)', 'templately' ); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* @throws Exception |
| 36 |
*/ |
| 37 |
public function import( $data, $imported_data ): array { |
| 38 |
$results = []; |
| 39 |
$contents = $this->manifest['content']; |
| 40 |
$path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR; |
| 41 |
|
| 42 |
// $total = array_reduce( $contents, function ( $carry, $item ) { |
| 43 |
// return $carry + count( $item ); |
| 44 |
// }, 0 ); |
| 45 |
// $processed = 0; |
| 46 |
|
| 47 |
/** |
| 48 |
* Check if there is any active kit? |
| 49 |
* If not, create one. |
| 50 |
*/ |
| 51 |
|
| 52 |
$kits_manager = Plugin::$instance->kits_manager; |
| 53 |
$active_kit = $kits_manager->get_active_id(); |
| 54 |
|
| 55 |
$kit = $kits_manager->get_kit( $active_kit ); |
| 56 |
|
| 57 |
if ( ! $kit->get_id() ) { |
| 58 |
$kit = $kits_manager->create_default(); |
| 59 |
update_option( $kits_manager::OPTION_ACTIVE, $kit ); |
| 60 |
} |
| 61 |
|
| 62 |
$processed = 0; |
| 63 |
$total = array_reduce($contents, function($carry, $item) { |
| 64 |
return $carry + count($item); |
| 65 |
}, 0); |
| 66 |
|
| 67 |
foreach ( $contents as $post_type => $post ) { |
| 68 |
if ( ! post_type_exists( $post_type ) ) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
foreach ( $post as $id => $content_settings ) { |
| 73 |
$import = $this->import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings ); |
| 74 |
|
| 75 |
if ( ! $import ) { |
| 76 |
$results[ $post_type ]['failed'][ $id ] = $import; |
| 77 |
} else { |
| 78 |
Utils::import_page_settings( $import, $content_settings ); |
| 79 |
$results[ $post_type ]['succeed'][ $id ] = $import; |
| 80 |
} |
| 81 |
|
| 82 |
// Broadcast Log |
| 83 |
$processed += 1; |
| 84 |
$progress = floor( ( 100 * $processed ) / $total ); |
| 85 |
$this->log( $progress, null, 'eventLog' ); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return [ 'content' => $results ]; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* @throws Exception |
| 94 |
*/ |
| 95 |
private function import_post_type_content( $id, $post_type, $path, $imported_data, $content_settings ) { |
| 96 |
try { |
| 97 |
$template = $this->factory->create( $content_settings['doc_type'], [ |
| 98 |
'post_title' => $content_settings['title'], |
| 99 |
'post_status' => 'publish', |
| 100 |
'post_type' => $post_type, |
| 101 |
] ); |
| 102 |
|
| 103 |
$file = $path . $post_type . DIRECTORY_SEPARATOR . "{$id}.json"; |
| 104 |
$post_data = Utils::read_json_file( $file ); |
| 105 |
|
| 106 |
if ( ! empty( $content_settings['data'] ) ) { |
| 107 |
/** |
| 108 |
* TODO: |
| 109 |
* |
| 110 |
* We can check if there is any data for settings. |
| 111 |
* if yes: ignore content from insert. |
| 112 |
* |
| 113 |
* Process the content while finalizing. |
| 114 |
*/ |
| 115 |
// $this->json->prepare( $post_data['content'], $id, $content_settings['data'], $imported_data ); |
| 116 |
|
| 117 |
$post_data['content'] = []; |
| 118 |
} |
| 119 |
|
| 120 |
$post_data['import_settings'] = $content_settings; |
| 121 |
|
| 122 |
$template->import( $post_data ); |
| 123 |
|
| 124 |
return $template->get_main_id(); |
| 125 |
} catch ( Exception $e ) { |
| 126 |
return false; |
| 127 |
} |
| 128 |
} |
| 129 |
} |