| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Builder\Factory\TemplateFactory; |
| 7 |
use Templately\Core\Importer\FullSiteImport; |
| 8 |
use Templately\Core\Importer\Import; |
| 9 |
use Templately\Core\Importer\LogHelper; |
| 10 |
use Templately\Core\Importer\Utils\ImportHelper; |
| 11 |
use Templately\Core\Importer\Utils\Utils; |
| 12 |
|
| 13 |
abstract class BaseRunner { |
| 14 |
use LogHelper; |
| 15 |
use Loop; |
| 16 |
|
| 17 |
const META_SESSION_KEY = '_templately_import_session_id'; |
| 18 |
/** |
| 19 |
* @var FullSiteImport |
| 20 |
*/ |
| 21 |
protected $dev_mode; |
| 22 |
protected $origin; |
| 23 |
protected $platform; |
| 24 |
protected $manifest; |
| 25 |
protected $prv_dir; |
| 26 |
protected $dir_path; |
| 27 |
protected $session_id; |
| 28 |
/** |
| 29 |
* @var array|mixed |
| 30 |
*/ |
| 31 |
public $ai_page_ids = []; |
| 32 |
/** |
| 33 |
* @var string|null |
| 34 |
*/ |
| 35 |
public $process_id = null; |
| 36 |
|
| 37 |
/** |
| 38 |
* @var ImportHelper |
| 39 |
*/ |
| 40 |
protected $json; |
| 41 |
|
| 42 |
/** |
| 43 |
* @var TemplateFactory |
| 44 |
*/ |
| 45 |
protected $factory; |
| 46 |
|
| 47 |
/** |
| 48 |
* @throws Exception |
| 49 |
*/ |
| 50 |
public function __construct( $request_params ) { |
| 51 |
$this->dev_mode = defined('TEMPLATELY_DEV') && TEMPLATELY_DEV; |
| 52 |
$this->origin = $request_params['origin']; |
| 53 |
$this->prv_dir = $request_params['prv_dir']; |
| 54 |
$this->dir_path = $request_params['dir_path']; |
| 55 |
$this->manifest = &$request_params['manifest']; |
| 56 |
$this->platform = $this->manifest['platform'] ?? ''; |
| 57 |
$this->session_id = $request_params['session_id']; |
| 58 |
|
| 59 |
|
| 60 |
$this->factory = new TemplateFactory( $this->platform ); |
| 61 |
$this->json = Utils::get_json_helper( $this->platform ); |
| 62 |
$this->json->session_id = $this->session_id; |
| 63 |
|
| 64 |
$this->ai_page_ids = $request_params['ai_page_ids'] ?? []; |
| 65 |
$this->process_id = $request_params['process_id'] ?? null; |
| 66 |
|
| 67 |
if ( empty( $this->platform ) ) { |
| 68 |
throw new Exception( __( 'Platform is not specified. Please try again after specifying the platform.', 'templately' ) ); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
public function should_log(): bool { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
public function get_action(): string { |
| 77 |
return 'updateLog'; |
| 78 |
} |
| 79 |
|
| 80 |
abstract public function get_name(): string; |
| 81 |
|
| 82 |
abstract public function get_label(): string; |
| 83 |
|
| 84 |
abstract public function should_run( $data, $imported_data = [] ): bool; |
| 85 |
|
| 86 |
abstract public function import( $data, $imported_data ): array; |
| 87 |
|
| 88 |
abstract public function log_message() : string; |
| 89 |
|
| 90 |
public function log( $progress = 0, $message = null, $action = null ) { |
| 91 |
if( ! $this->should_log() ) { |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
if( $message == null ) { |
| 96 |
$message = $this->log_message(); |
| 97 |
} |
| 98 |
|
| 99 |
if( $action == null ) { |
| 100 |
$action = $this->get_action(); |
| 101 |
} |
| 102 |
|
| 103 |
$this->sse_log( $this->get_name(), $message, min( $progress, 100 ), $action ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @throws Exception |
| 108 |
*/ |
| 109 |
protected function throw($message, $code = 0) { |
| 110 |
if ($this->dev_mode) { |
| 111 |
error_log(print_r($message, 1)); |
| 112 |
} |
| 113 |
throw new Exception($message); |
| 114 |
} |
| 115 |
|
| 116 |
protected function is_ai_content($old_template_id ): bool { |
| 117 |
if(empty($this->process_id) || empty($this->ai_page_ids) || !is_array($this->ai_page_ids)){ |
| 118 |
return false; |
| 119 |
} |
| 120 |
// Get array of AI page IDs, filtering out any non-numeric values |
| 121 |
$ai_page_ids = array_reduce($this->ai_page_ids, 'array_merge', array()); |
| 122 |
// $ai_page_ids = array_filter( |
| 123 |
// array_map('intval', explode(',', $this->ai_page_ids ?? '')) |
| 124 |
// ); |
| 125 |
|
| 126 |
$is_ai_content = in_array($old_template_id, $ai_page_ids); |
| 127 |
|
| 128 |
return $is_ai_content; |
| 129 |
} |
| 130 |
} |