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