| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Core\Importer\Runners\Attachments; |
| 7 |
use Templately\Core\Importer\Runners\BaseRunner; |
| 8 |
use Templately\Core\Importer\Runners\Customizer; |
| 9 |
use Templately\Core\Importer\Runners\ElementorContent; |
| 10 |
use Templately\Core\Importer\Runners\ExtraContent; |
| 11 |
use Templately\Core\Importer\Runners\Finalizer; |
| 12 |
use Templately\Core\Importer\Runners\GutenbergContent; |
| 13 |
use Templately\Core\Importer\Runners\Templates; |
| 14 |
use Templately\Core\Importer\Runners\WPContent; |
| 15 |
|
| 16 |
class Import { |
| 17 |
use LogHelper; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var FullSiteImport |
| 21 |
*/ |
| 22 |
public $full_site_import; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var array |
| 26 |
*/ |
| 27 |
private $manifest; |
| 28 |
|
| 29 |
private $runners; |
| 30 |
|
| 31 |
private $imported_data = []; |
| 32 |
|
| 33 |
/** |
| 34 |
* @throws Exception |
| 35 |
*/ |
| 36 |
public function __construct( $full_site_import ) { |
| 37 |
$this->full_site_import = $full_site_import; |
| 38 |
|
| 39 |
$this->manifest = $full_site_import->manifest; |
| 40 |
|
| 41 |
$this->register_runners(); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* @throws Exception |
| 46 |
*/ |
| 47 |
private function register_runners() { |
| 48 |
$this->runners = [ |
| 49 |
// TODO: Site Settings Import Runner |
| 50 |
new Attachments( $this->full_site_import ), |
| 51 |
new Customizer( $this->full_site_import ), |
| 52 |
new ExtraContent( $this->full_site_import ), |
| 53 |
new Templates( $this->full_site_import ), |
| 54 |
new GutenbergContent( $this->full_site_import ), |
| 55 |
new ElementorContent( $this->full_site_import ), |
| 56 |
new WPContent( $this->full_site_import ), |
| 57 |
new Finalizer( $this->full_site_import ) |
| 58 |
]; |
| 59 |
} |
| 60 |
|
| 61 |
public function run( $callable = null ): array { |
| 62 |
$data = $this->full_site_import->get_request_params(); |
| 63 |
$progress = $this->full_site_import->get_progress(); |
| 64 |
|
| 65 |
// Get the cached imported_data |
| 66 |
$this->imported_data = $data['imported_data'] ?? []; |
| 67 |
|
| 68 |
/** |
| 69 |
* @var BaseRunner $runner |
| 70 |
*/ |
| 71 |
foreach ( $this->runners as $id => $runner ) { |
| 72 |
$import = null; |
| 73 |
// If the template has been processed, skip it |
| 74 |
if (in_array($id, $progress)) { |
| 75 |
continue; |
| 76 |
} |
| 77 |
try{ |
| 78 |
if ( $runner->should_run( $data, $this->imported_data ) ) { |
| 79 |
$import = $runner->import( $data, $this->imported_data ); |
| 80 |
$this->imported_data = array_merge_recursive( $this->imported_data, $import ); |
| 81 |
|
| 82 |
if( $runner->get_name() != 'finalize' ) { |
| 83 |
$runner->log( 100 ); |
| 84 |
} else { |
| 85 |
$runner->log( 100, $runner->log_message() ); |
| 86 |
} |
| 87 |
} |
| 88 |
}catch (Exception $e){ |
| 89 |
error_log($e->getMessage()); |
| 90 |
} |
| 91 |
|
| 92 |
// Update the progress |
| 93 |
$progress[] = $id; |
| 94 |
$this->full_site_import->update_progress($progress, $import); |
| 95 |
|
| 96 |
if(end($this->runners) !== $runner) { |
| 97 |
// $_runner = $this->runners[$id + 1]; |
| 98 |
// if($_runner){ |
| 99 |
// $_runner->log(); |
| 100 |
// } |
| 101 |
$this->sse_message( [ |
| 102 |
'type' => 'continue', |
| 103 |
'action' => 'continue', |
| 104 |
'results' => $runner->get_label(), |
| 105 |
] ); |
| 106 |
exit; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
return $this->imported_data; |
| 111 |
} |
| 112 |
|
| 113 |
public function get_runners() { |
| 114 |
return $this->runners; |
| 115 |
} |
| 116 |
} |