| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\FullSiteImport; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Modules\FullSiteImport\Exception\SkippableErrorException; |
| 7 |
use Templately\Utils\Helper; |
| 8 |
use Templately\Modules\FullSiteImport\Runners\Attachments; |
| 9 |
use Templately\Modules\FullSiteImport\Runners\BaseRunner; |
| 10 |
use Templately\Modules\FullSiteImport\Runners\CustomCSS; |
| 11 |
use Templately\Modules\FullSiteImport\Runners\Customizer; |
| 12 |
use Templately\Modules\FullSiteImport\Runners\Dependencies; |
| 13 |
use Templately\Modules\FullSiteImport\Runners\ElementorContent; |
| 14 |
use Templately\Modules\FullSiteImport\Runners\ExtraContent; |
| 15 |
use Templately\Modules\FullSiteImport\Runners\Finalizer; |
| 16 |
use Templately\Modules\FullSiteImport\Runners\GutenbergContent; |
| 17 |
use Templately\Modules\FullSiteImport\Runners\Loop; |
| 18 |
use Templately\Modules\FullSiteImport\Runners\Templates; |
| 19 |
use Templately\Modules\FullSiteImport\Runners\WPContent; |
| 20 |
|
| 21 |
class Import { |
| 22 |
use Concerns\LogHelper; |
| 23 |
use Loop; |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private $manifest; |
| 30 |
|
| 31 |
private $runners; |
| 32 |
private $request_params; |
| 33 |
private $session_id; |
| 34 |
|
| 35 |
private $imported_data = []; |
| 36 |
|
| 37 |
/** |
| 38 |
* @throws Exception |
| 39 |
*/ |
| 40 |
public function __construct( $args ) { |
| 41 |
$this->request_params = $args; |
| 42 |
$this->manifest = $args['manifest']; |
| 43 |
$this->session_id = $args['session_id']; |
| 44 |
|
| 45 |
$this->register_runners(); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* @throws Exception |
| 50 |
*/ |
| 51 |
private function register_runners() { |
| 52 |
$this->runners = [ |
| 53 |
// TODO: Site Settings Import Runner |
| 54 |
new Dependencies( $this->request_params ), |
| 55 |
new Attachments( $this->request_params ), |
| 56 |
new Customizer( $this->request_params ), |
| 57 |
new CustomCSS( $this->request_params ), |
| 58 |
new ExtraContent( $this->request_params ), |
| 59 |
new Templates( $this->request_params ), |
| 60 |
new GutenbergContent( $this->request_params ), |
| 61 |
new ElementorContent( $this->request_params ), |
| 62 |
new WPContent( $this->request_params ), |
| 63 |
new Finalizer( $this->request_params ) |
| 64 |
]; |
| 65 |
|
| 66 |
$this->runners = apply_filters( 'templately_import_runners', $this->runners, $this->request_params ); |
| 67 |
} |
| 68 |
|
| 69 |
public function run( $callable = null ): array { |
| 70 |
$data = $this->request_params; |
| 71 |
|
| 72 |
/** |
| 73 |
* Pre-flight: the pack's builder must be LOADED before any runner touches |
| 74 |
* it. Runs here — not only inside the Dependencies runner — because that |
| 75 |
* runner is skipped whenever the request carries no `theme`/`plugins` |
| 76 |
* (`Dependencies::should_run()`), and a skipped dependency step used to |
| 77 |
* mean the import sailed on to the Templates runner and died with a raw |
| 78 |
* PHP fatal instead of a readable error: |
| 79 |
* |
| 80 |
* Uncaught Error: Class "Elementor\Core\Base\Document" not found |
| 81 |
* |
| 82 |
* SKIPPED while the Dependencies runner still has plugins to install. That |
| 83 |
* runner installs the builder and then `exit`s with a `continue` event |
| 84 |
* precisely because a plugin activated in this request is not loaded until |
| 85 |
* the next one — so on the first slice of a site that does not yet have the |
| 86 |
* builder, "not loaded" is the expected state and not a failure. Refusing |
| 87 |
* there made the dependency step unreachable and turned every |
| 88 |
* install-the-builder import into a hard stop that told the user to install |
| 89 |
* by hand the very plugin the importer was about to install for them. |
| 90 |
* |
| 91 |
* Cheap (two class_exists calls) and idempotent across resumed slices. |
| 92 |
*/ |
| 93 |
if ( ! Dependencies::installation_pending( $data ) ) { |
| 94 |
Dependencies::verify_platform_available( (string) ( $data['manifest']['platform'] ?? '' ) ); |
| 95 |
} |
| 96 |
|
| 97 |
// Get the cached imported_data |
| 98 |
// $this->imported_data = $data['imported_data'] ?? []; |
| 99 |
|
| 100 |
/** |
| 101 |
* @var BaseRunner $runner |
| 102 |
*/ |
| 103 |
$imported_data = $this->loop( $this->runners, function($id, $runner, $imported_data ) use($data) { |
| 104 |
$import = []; |
| 105 |
|
| 106 |
try{ |
| 107 |
if ( $runner->should_run( $data, $imported_data ) ) { |
| 108 |
if (!$this->is_key_processed($runner->get_name(), 'runner_start')) { |
| 109 |
$runner->log( 0 ); |
| 110 |
$this->mark_key_processed($runner->get_name(), 'runner_start'); |
| 111 |
} |
| 112 |
$import = $runner->import( $data, $imported_data ); |
| 113 |
$imported_data = array_merge( $imported_data, $import ); |
| 114 |
|
| 115 |
if( $runner->get_name() != 'finalize' ) { |
| 116 |
$runner->log( 100 ); |
| 117 |
} else { |
| 118 |
$runner->log( 100, $runner->log_message() ); |
| 119 |
} |
| 120 |
} |
| 121 |
}catch (Exception $e){ |
| 122 |
Helper::log( |
| 123 |
sprintf( |
| 124 |
'%s (runner: %s, %s:%d)' . "\n%s", |
| 125 |
$e->getMessage(), |
| 126 |
$runner->get_name(), |
| 127 |
$e->getFile(), |
| 128 |
$e->getLine(), |
| 129 |
$e->getTraceAsString() |
| 130 |
), |
| 131 |
'import_runner_error' |
| 132 |
); |
| 133 |
|
| 134 |
// Swallow ONLY skippable errors (item skipped, import continues). Every |
| 135 |
// other error propagates so the SSE layer can surface it (034 FR-002): |
| 136 |
// Retryable → Retry affordance, NonRetirable → stop, Unknown/other → surfaced. |
| 137 |
if ( ! ( $e instanceof SkippableErrorException ) ) { |
| 138 |
throw $e; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
return $imported_data; |
| 143 |
}, null, true); |
| 144 |
|
| 145 |
return $imported_data; |
| 146 |
} |
| 147 |
|
| 148 |
public function get_runners() { |
| 149 |
return $this->runners; |
| 150 |
} |
| 151 |
} |
| 152 |
|