| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Core\Importer\Runners\BaseRunner; |
| 7 |
use Templately\Core\Importer\Runners\Customizer; |
| 8 |
use Templately\Core\Importer\Runners\ElementorContent; |
| 9 |
use Templately\Core\Importer\Runners\ExtraContent; |
| 10 |
use Templately\Core\Importer\Runners\Finalizer; |
| 11 |
use Templately\Core\Importer\Runners\GutenbergContent; |
| 12 |
use Templately\Core\Importer\Runners\Templates; |
| 13 |
use Templately\Core\Importer\Runners\WPContent; |
| 14 |
|
| 15 |
class Import { |
| 16 |
use LogHelper; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var FullSiteImport |
| 20 |
*/ |
| 21 |
public $full_site_import; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var array |
| 25 |
*/ |
| 26 |
private $manifest; |
| 27 |
|
| 28 |
private $runners; |
| 29 |
|
| 30 |
private $imported_data = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* @throws Exception |
| 34 |
*/ |
| 35 |
public function __construct( $full_site_import ) { |
| 36 |
$this->full_site_import = $full_site_import; |
| 37 |
|
| 38 |
$this->manifest = $full_site_import->manifest; |
| 39 |
|
| 40 |
$this->register_runners(); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* @throws Exception |
| 45 |
*/ |
| 46 |
private function register_runners() { |
| 47 |
$this->runners = [ |
| 48 |
// TODO: Site Settings Import Runner |
| 49 |
new Customizer( $this->full_site_import ), |
| 50 |
new ExtraContent( $this->full_site_import ), |
| 51 |
new Templates( $this->full_site_import ), |
| 52 |
new GutenbergContent( $this->full_site_import ), |
| 53 |
new ElementorContent( $this->full_site_import ), |
| 54 |
new WPContent( $this->full_site_import ), |
| 55 |
new Finalizer( $this->full_site_import ) |
| 56 |
]; |
| 57 |
} |
| 58 |
|
| 59 |
public function run( $callable = null ): array { |
| 60 |
$data = $this->full_site_import->get_request_params(); |
| 61 |
|
| 62 |
/** |
| 63 |
* @var BaseRunner $runner |
| 64 |
*/ |
| 65 |
foreach ( $this->runners as $runner ) { |
| 66 |
try{ |
| 67 |
if ( $runner->should_run( $data, $this->imported_data ) ) { |
| 68 |
if( $runner->get_name() != 'finalize' ) { |
| 69 |
$runner->log(); |
| 70 |
} |
| 71 |
|
| 72 |
$import = $runner->import( $data, $this->imported_data ); |
| 73 |
$this->imported_data = array_merge_recursive( $this->imported_data, $import ); |
| 74 |
|
| 75 |
if( $runner->get_name() != 'finalize' ) { |
| 76 |
$runner->log( 100 ); |
| 77 |
} else { |
| 78 |
$runner->log( 100, $runner->log_message() ); |
| 79 |
} |
| 80 |
} |
| 81 |
}catch (Exception $e){ |
| 82 |
error_log($e->getMessage()); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $this->imported_data; |
| 87 |
} |
| 88 |
|
| 89 |
public function get_runners() { |
| 90 |
return $this->runners; |
| 91 |
} |
| 92 |
} |