| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\FullSiteImport\Runners; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Modules\ThemeBuilder\Factory\TemplateFactory; |
| 7 |
use Templately\Modules\FullSiteImport\FullSiteImport; |
| 8 |
use Templately\Modules\FullSiteImport\Import; |
| 9 |
use Templately\Modules\FullSiteImport\Concerns\LogHelper; |
| 10 |
use Templately\Modules\FullSiteImport\Utils\AIUtils; |
| 11 |
use Templately\Modules\FullSiteImport\Utils\ImportHelper; |
| 12 |
use Templately\Modules\FullSiteImport\Utils\Utils; |
| 13 |
use Templately\Utils\Helper; |
| 14 |
|
| 15 |
abstract class BaseRunner { |
| 16 |
use LogHelper; |
| 17 |
use Loop; |
| 18 |
|
| 19 |
const META_SESSION_KEY = '_templately_import_session_id'; |
| 20 |
/** |
| 21 |
* @var FullSiteImport |
| 22 |
*/ |
| 23 |
protected $dev_mode; |
| 24 |
protected $origin; |
| 25 |
protected $platform; |
| 26 |
protected $manifest; |
| 27 |
protected $prv_dir; |
| 28 |
protected $dir_path; |
| 29 |
protected $session_id; |
| 30 |
/** |
| 31 |
* @var array|mixed |
| 32 |
*/ |
| 33 |
public $ai_page_ids = []; |
| 34 |
/** |
| 35 |
* @var string|null |
| 36 |
*/ |
| 37 |
public $process_id = null; |
| 38 |
|
| 39 |
/** |
| 40 |
* @var ImportHelper |
| 41 |
*/ |
| 42 |
protected $json; |
| 43 |
|
| 44 |
/** |
| 45 |
* @var TemplateFactory |
| 46 |
*/ |
| 47 |
protected $factory; |
| 48 |
|
| 49 |
/** |
| 50 |
* @throws Exception |
| 51 |
*/ |
| 52 |
public function __construct( $request_params ) { |
| 53 |
$this->dev_mode = defined('TEMPLATELY_DEV') && TEMPLATELY_DEV; |
| 54 |
$this->origin = $request_params['origin']; |
| 55 |
$this->prv_dir = $request_params['prv_dir']; |
| 56 |
$this->dir_path = $request_params['dir_path']; |
| 57 |
$this->manifest = &$request_params['manifest']; |
| 58 |
$this->platform = $this->manifest['platform'] ?? ''; |
| 59 |
$this->session_id = $request_params['session_id']; |
| 60 |
|
| 61 |
|
| 62 |
$this->factory = new TemplateFactory( $this->platform ); |
| 63 |
$this->json = Utils::get_json_helper( $this->platform ); |
| 64 |
$this->json->session_id = $this->session_id; |
| 65 |
|
| 66 |
// Normalize once here so every runner sees the canonical |
| 67 |
// `type/sub_type => ['id',...]` shape. This value arrives from the import |
| 68 |
// FormData (a JSON string), session data, or the manifest, and consumers |
| 69 |
// index into it in ways that either throw or silently miss pages when the |
| 70 |
// shape differs. See AIUtils::normalize_ai_page_ids(). |
| 71 |
$this->ai_page_ids = AIUtils::normalize_ai_page_ids( $request_params['ai_page_ids'] ?? [] ); |
| 72 |
$this->process_id = $request_params['process_id'] ?? null; |
| 73 |
|
| 74 |
if ( empty( $this->platform ) ) { |
| 75 |
throw new Exception( __( 'Platform is not specified. Please try again after specifying the platform.', 'templately' ) ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
public function should_log(): bool { |
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
public function get_action(): string { |
| 84 |
return 'updateLog'; |
| 85 |
} |
| 86 |
|
| 87 |
abstract public function get_name(): string; |
| 88 |
|
| 89 |
abstract public function get_label(): string; |
| 90 |
|
| 91 |
abstract public function should_run( $data, $imported_data = [] ): bool; |
| 92 |
|
| 93 |
abstract public function import( $data, $imported_data ): array; |
| 94 |
|
| 95 |
abstract public function log_message() : string; |
| 96 |
|
| 97 |
public function log( $progress = 0, $message = null, $action = null ) { |
| 98 |
if( ! $this->should_log() ) { |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
if( $message == null ) { |
| 103 |
$message = $this->log_message(); |
| 104 |
} |
| 105 |
|
| 106 |
if( $action == null ) { |
| 107 |
$action = $this->get_action(); |
| 108 |
} |
| 109 |
|
| 110 |
$this->sse_log( $this->get_name(), $message, min( $progress, 100 ), $action ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* @throws Exception |
| 115 |
*/ |
| 116 |
protected function throw($message, $code = 0) { |
| 117 |
if ($this->dev_mode) { |
| 118 |
Helper::log($message, 'fsi_error'); |
| 119 |
} |
| 120 |
throw new Exception($message); |
| 121 |
} |
| 122 |
|
| 123 |
protected function is_ai_content($old_template_id ): bool { |
| 124 |
if(empty($this->process_id) || empty($this->ai_page_ids)){ |
| 125 |
return false; |
| 126 |
} |
| 127 |
|
| 128 |
// $this->ai_page_ids is already normalized in the constructor, so this is |
| 129 |
// a plain flatten + strict membership test. It used to array_reduce with |
| 130 |
// 'array_merge', which throws a TypeError if any group value is a scalar. |
| 131 |
return AIUtils::is_ai_content($old_template_id, $this->ai_page_ids); |
| 132 |
} |
| 133 |
} |