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