| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Utils\Base; |
| 7 |
use Templately\Utils\Helper; |
| 8 |
use Templately\Utils\Installer; |
| 9 |
use Templately\Utils\Options; |
| 10 |
|
| 11 |
class FullSiteImport extends Base { |
| 12 |
use LogHelper; |
| 13 |
|
| 14 |
const SESSION_OPTION_KEY = 'templately_import_session'; |
| 15 |
public $manifest; |
| 16 |
protected $export; |
| 17 |
|
| 18 |
private $version = '1.0.0'; |
| 19 |
|
| 20 |
public $session_id; |
| 21 |
public $download_key; |
| 22 |
public $dir_path; |
| 23 |
protected $filePath; |
| 24 |
protected $tmp_dir = null; |
| 25 |
protected $dev_mode = false; |
| 26 |
protected $api_key = ''; |
| 27 |
public $request_params = []; |
| 28 |
protected $documents_data = []; |
| 29 |
protected $dependency_data = []; |
| 30 |
private $is_import_status_handled = false; |
| 31 |
|
| 32 |
public function __construct() { |
| 33 |
$this->dev_mode = defined( 'TEMPLATELY_DEV' ) && TEMPLATELY_DEV; |
| 34 |
$this->api_key = Options::get_instance()->get( 'api_key' ); |
| 35 |
|
| 36 |
add_action( 'wp_ajax_templately_import_settings', [ $this, 'import_settings' ] ); |
| 37 |
add_action( 'wp_ajax_templately_pack_import_status', [ $this, 'import_status' ] ); |
| 38 |
add_action( 'wp_ajax_templately_pack_import', [ $this, 'import' ] ); |
| 39 |
add_action( 'wp_ajax_templately_pack_import_info', [ $this, 'import_info' ] ); |
| 40 |
add_action( 'admin_init', [ $this, 'admin_init' ] ); |
| 41 |
|
| 42 |
if(isset($_GET['action']) && $_GET['action'] == 'templately_pack_import') { |
| 43 |
add_filter('wp_redirect', '__return_false', 999); |
| 44 |
} |
| 45 |
|
| 46 |
if ( $this->dev_mode ) { |
| 47 |
add_filter( 'http_request_host_is_external', '__return_true' ); |
| 48 |
add_filter( 'http_request_args', function ( $args ) { |
| 49 |
$args['sslverify'] = false; |
| 50 |
|
| 51 |
return $args; |
| 52 |
} ); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function admin_init(){ |
| 57 |
if(get_option('templately_flush_rewrite_rules', false)){ |
| 58 |
flush_rewrite_rules(); |
| 59 |
delete_option('templately_flush_rewrite_rules'); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function import_settings() { |
| 64 |
if( ! current_user_can( 'edit_posts' ) ) { |
| 65 |
// @todo show error in ui. Currently getting stack. |
| 66 |
wp_send_json_error( __( 'Unauthorized action.', 'templately' ) ); |
| 67 |
} |
| 68 |
|
| 69 |
$data = wp_unslash( $_POST ); |
| 70 |
|
| 71 |
update_option( self::SESSION_OPTION_KEY, $data ); |
| 72 |
|
| 73 |
delete_transient('templately_fsi_log'); |
| 74 |
|
| 75 |
wp_send_json_success( [ |
| 76 |
'is_lightspeed' => !Helper::should_flush(), |
| 77 |
] ); |
| 78 |
} |
| 79 |
|
| 80 |
private function update_session_data( $data ): bool { |
| 81 |
$old_data = get_option( self::SESSION_OPTION_KEY, [] ); |
| 82 |
|
| 83 |
return update_option( self::SESSION_OPTION_KEY, wp_parse_args( $data, $old_data ) ); |
| 84 |
} |
| 85 |
|
| 86 |
public function get_session_data(): array { |
| 87 |
$data = get_option( self::SESSION_OPTION_KEY, [] ); |
| 88 |
|
| 89 |
$options = []; |
| 90 |
|
| 91 |
if ( is_array( $data ) && ! empty( $data ) ) { |
| 92 |
foreach ( $data as $key => $value ) { |
| 93 |
$json = json_decode( $value, true ); |
| 94 |
$options[ $key ] = $json !== null ? $json : $value; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return $options; |
| 99 |
} |
| 100 |
|
| 101 |
public function initialize_props() { |
| 102 |
$data = $this->get_session_data(); |
| 103 |
if(isset($data['session_id'])){ |
| 104 |
$this->session_id = $data['session_id']; |
| 105 |
} |
| 106 |
if(isset($data['dir_path'])){ |
| 107 |
$this->dir_path = $data['dir_path']; |
| 108 |
} |
| 109 |
if(isset($data['download_key'])){ |
| 110 |
$this->download_key = $data['download_key']; |
| 111 |
} |
| 112 |
if(isset($data['dependency_data'])){ |
| 113 |
$this->dependency_data = $data['dependency_data']; |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
private function clear_session_data(): bool { |
| 119 |
return delete_site_option( self::SESSION_OPTION_KEY ); |
| 120 |
} |
| 121 |
|
| 122 |
public function import() { |
| 123 |
if ( ! $this->dev_mode && ! wp_doing_ajax() ) { |
| 124 |
exit; |
| 125 |
} |
| 126 |
|
| 127 |
delete_transient( 'templately_fsi_log' ); |
| 128 |
|
| 129 |
if(Helper::should_flush()){ |
| 130 |
header( "Cache-Control: no-store, no-cache" ); |
| 131 |
header( 'Content-Type: text/event-stream, charset=UTF-8' ); |
| 132 |
header( "Connection: Keep-Alive" ); |
| 133 |
} |
| 134 |
|
| 135 |
if ( $GLOBALS['is_nginx'] ) { |
| 136 |
header( 'X-Accel-Buffering: no' ); |
| 137 |
header( 'Content-Encoding: none' ); |
| 138 |
} |
| 139 |
|
| 140 |
register_shutdown_function( [ $this, 'register_shutdown' ] ); |
| 141 |
|
| 142 |
// Ignore user aborts and allow the script |
| 143 |
// to run forever |
| 144 |
ignore_user_abort(true); |
| 145 |
|
| 146 |
// Time to run the import! |
| 147 |
set_time_limit( 0 ); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
// // Don't run cron until the request finishes, if possible. |
| 152 |
// if ( PHP_VERSION_ID >= 70016 && function_exists( 'fastcgi_finish_request' ) ) { |
| 153 |
// fastcgi_finish_request(); |
| 154 |
// } elseif ( function_exists( 'litespeed_finish_request' ) ) { |
| 155 |
// litespeed_finish_request(); |
| 156 |
// } |
| 157 |
if(Helper::should_flush()){ |
| 158 |
flush(); |
| 159 |
wp_ob_end_flush_all(); |
| 160 |
} |
| 161 |
|
| 162 |
try { |
| 163 |
// TODO: Need to check if user is connected or not |
| 164 |
|
| 165 |
$this->request_params = $this->get_session_data(); |
| 166 |
|
| 167 |
$_id = isset( $this->request_params['id'] ) ? (int) $this->request_params['id'] : null; |
| 168 |
|
| 169 |
if ( $_id === null ) { |
| 170 |
$this->throw( __( 'Invalid Pack ID.', 'templately' ) ); |
| 171 |
} |
| 172 |
|
| 173 |
if(!isset($_GET['part'])){ |
| 174 |
$this->throw( __( 'Invalid request.', 'templately' ) ); |
| 175 |
} |
| 176 |
|
| 177 |
switch ($_GET['part']) { |
| 178 |
case 'download': |
| 179 |
/** |
| 180 |
* Check Writing Permission |
| 181 |
*/ |
| 182 |
$this->check_writing_permission(); |
| 183 |
|
| 184 |
/** |
| 185 |
* Download the zip |
| 186 |
*/ |
| 187 |
$this->download_zip( $_id ); |
| 188 |
|
| 189 |
/** |
| 190 |
* Reading Manifest File |
| 191 |
*/ |
| 192 |
$this->read_manifest(); |
| 193 |
|
| 194 |
/** |
| 195 |
* Version Check |
| 196 |
*/ |
| 197 |
if ( ! empty( $this->manifest['version'] ) && version_compare( $this->manifest['version'], $this->version, '>' ) ) { |
| 198 |
/** |
| 199 |
* FIXME: The message should be re-written (by content/support team). |
| 200 |
*/ |
| 201 |
$this->throw( __( 'Please update the templately plugin.', 'templately' ) ); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Checking & Installing Plugin Dependencies |
| 206 |
*/ |
| 207 |
$this->install_dependencies(); |
| 208 |
|
| 209 |
$this->sse_message( [ |
| 210 |
'type' => 'complete', |
| 211 |
'action' => 'downloadComplete', |
| 212 |
'results' => '', |
| 213 |
] ); |
| 214 |
break; |
| 215 |
case 'import': |
| 216 |
/** |
| 217 |
* Should Revert Old Data |
| 218 |
*/ |
| 219 |
$this->revert(); |
| 220 |
|
| 221 |
|
| 222 |
$this->initialize_props(); |
| 223 |
|
| 224 |
/** |
| 225 |
* Reading Manifest File |
| 226 |
*/ |
| 227 |
$this->read_manifest(); |
| 228 |
|
| 229 |
/** |
| 230 |
* Platform Based Templates Import |
| 231 |
*/ |
| 232 |
$this->start_content_import(); |
| 233 |
break; |
| 234 |
default: |
| 235 |
$this->throw( __( 'Invalid request.', 'templately' ) ); |
| 236 |
break; |
| 237 |
} |
| 238 |
|
| 239 |
} catch ( Exception $e ) { |
| 240 |
$this->handle_import_status('failed', $e->getMessage()); |
| 241 |
$this->sse_message( [ |
| 242 |
'action' => 'error', |
| 243 |
'status' => 'error', |
| 244 |
'type' => "error", |
| 245 |
'title' => __("Oops!", "templately"), |
| 246 |
'message' => $e->getMessage() |
| 247 |
] ); |
| 248 |
} |
| 249 |
|
| 250 |
if($_GET['part'] === 'import'){ |
| 251 |
// TODO: cleanup |
| 252 |
$this->clear_session_data(); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
public function import_status(){ |
| 257 |
$log = get_transient( 'templately_fsi_log' ); |
| 258 |
if(!empty($log) && is_array($log)){ |
| 259 |
$lastLogIndex = isset($_GET['lastLogIndex']) ? (int) $_GET['lastLogIndex'] : 0; |
| 260 |
$log = array_slice($log, $lastLogIndex); |
| 261 |
|
| 262 |
// delete log after complete |
| 263 |
$data = end($log); |
| 264 |
if(isset($data, $data['action']) && ($data['action'] === 'complete' || $data['action'] === 'downloadComplete' || $data['action'] === 'error')){ |
| 265 |
delete_transient( 'templately_fsi_log' ); |
| 266 |
} |
| 267 |
} |
| 268 |
wp_send_json( ['log' => $log] ); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* @throws Exception |
| 273 |
*/ |
| 274 |
private function throw( $message, $code = 0 ) { |
| 275 |
if ( $this->dev_mode ) { |
| 276 |
error_log( print_r( $message, 1 ) ); |
| 277 |
} |
| 278 |
throw new Exception( $message ); |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* @throws Exception |
| 283 |
*/ |
| 284 |
private function check_writing_permission() { |
| 285 |
$upload_dir = wp_upload_dir(); |
| 286 |
|
| 287 |
if ( ! is_writable( $upload_dir['basedir'] ) ) { |
| 288 |
$this->throw( __( 'Upload directory is not writable.', 'templately' ) ); |
| 289 |
} |
| 290 |
|
| 291 |
$this->tmp_dir = trailingslashit( $upload_dir['basedir'] ) . 'templately' . DIRECTORY_SEPARATOR . 'tmp' . DIRECTORY_SEPARATOR; |
| 292 |
|
| 293 |
if ( ! is_dir( $this->tmp_dir ) ) { |
| 294 |
wp_mkdir_p( $this->tmp_dir ); |
| 295 |
} |
| 296 |
|
| 297 |
$this->sse_log( 'writing_permission_check', __( 'Permission Passed', 'templately' ), 100 ); |
| 298 |
} |
| 299 |
|
| 300 |
private function get_api_url( $version, $end_point ): string { |
| 301 |
return $this->dev_mode ? "https://app.templately.dev/api/$version/" . $end_point : "https://app.templately.com/api/$version/" . $end_point; |
| 302 |
} |
| 303 |
|
| 304 |
private function info_get_api_url( $id ): string { |
| 305 |
return $this->dev_mode ? 'https://app.templately.dev/api/v1/import/info/pack/' . $id : 'https://app.templately.com/api/v1/import/info/pack/' . $id; |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* @throws Exception |
| 310 |
*/ |
| 311 |
private function download_zip( $id ) { |
| 312 |
// $this->sse_log( 'download', __( 'Downloading Template Pack', 'templately' ), 1 ); |
| 313 |
$response = wp_remote_get( $this->get_api_url( "v2", "import/pack/$id" ), [ |
| 314 |
'timeout' => 30, |
| 315 |
'headers' => [ |
| 316 |
'Content-Type' => 'application/json', |
| 317 |
'Authorization' => 'Bearer ' . $this->api_key, |
| 318 |
'x-templately-ip' => Helper::get_ip(), |
| 319 |
'x-templately-url' => home_url( '/' ), |
| 320 |
'x-templately-version' => TEMPLATELY_VERSION, |
| 321 |
] |
| 322 |
] ); |
| 323 |
|
| 324 |
$response_code = wp_remote_retrieve_response_code( $response ); |
| 325 |
$content_type = wp_remote_retrieve_header( $response, 'content-type' ); |
| 326 |
$this->download_key = wp_remote_retrieve_header( $response, 'download-key' ); |
| 327 |
|
| 328 |
if ( is_wp_error( $response ) ) { |
| 329 |
$this->throw( __( 'Template pack download failed', 'templately' ) . $response->get_error_message() ); |
| 330 |
} |
| 331 |
else if ($response_code != 200) { |
| 332 |
if (strpos($content_type, 'application/json') !== false) { |
| 333 |
// Retrieve Data from Response Body. |
| 334 |
$response_body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 335 |
|
| 336 |
// If the response body is JSON and it contains an error, throw an exception with the error message |
| 337 |
if (isset($response_body['status']) && $response_body['status'] === 'error') { |
| 338 |
$this->throw( $response_body['message'] ); |
| 339 |
} |
| 340 |
} |
| 341 |
$this->throw( __( 'Template pack download failed with response code: ', 'templately' ) . $response_code ); |
| 342 |
} |
| 343 |
|
| 344 |
$this->sse_log( 'download', __( 'Template is getting ready', 'templately' ), 57 ); |
| 345 |
|
| 346 |
$session_id = uniqid(); |
| 347 |
$this->dir_path = $this->tmp_dir . $session_id . DIRECTORY_SEPARATOR; |
| 348 |
$this->filePath = $this->tmp_dir . "{$session_id}.zip"; |
| 349 |
$this->session_id = $session_id; |
| 350 |
|
| 351 |
$this->update_session_data( [ |
| 352 |
'session_id' => $this->session_id, |
| 353 |
'dir_path' => $this->dir_path, |
| 354 |
'download_key' => $this->download_key, |
| 355 |
] ); |
| 356 |
|
| 357 |
if ( file_put_contents( $this->filePath, $response['body'] ) ) { // phpcs:ignore |
| 358 |
$this->sse_log( 'download', __( 'Template is getting ready', 'templately' ), 100 ); |
| 359 |
|
| 360 |
$this->unzip(); |
| 361 |
} else { |
| 362 |
$this->throw( __( 'Downloading Failed. Please try again', 'templately' ) ); |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* @throws Exception |
| 368 |
*/ |
| 369 |
protected function unzip() { |
| 370 |
if ( ! WP_Filesystem() ) { |
| 371 |
$this->throw( __( 'WP_Filesystem cannot be initialized', 'templately' ) ); |
| 372 |
} |
| 373 |
|
| 374 |
$unzip = unzip_file( $this->filePath, $this->dir_path ); |
| 375 |
if ( is_wp_error( $unzip ) ) { |
| 376 |
$unzip = $this->unzip_file( $this->filePath, $this->dir_path ); |
| 377 |
} |
| 378 |
|
| 379 |
if ( is_wp_error( $unzip ) ) { |
| 380 |
$error = $unzip->get_error_message(); |
| 381 |
if(empty($error)){ |
| 382 |
// Generic error message |
| 383 |
Helper::log( $unzip ); |
| 384 |
$error_message = sprintf(__("It seems we're experiencing technical difficulties. Please try again or contact <a href='%s' target='_blank'>support</a>.", "templately"), 'https://wpdeveloper.com/support'); |
| 385 |
$this->throw( $error_message ); |
| 386 |
} |
| 387 |
else{ |
| 388 |
$this->throw( $unzip->get_error_message() ); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
if ( $unzip ) { |
| 393 |
unlink( $this->filePath ); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Unzip a specified ZIP file to a location on the Filesystem. |
| 399 |
* |
| 400 |
* @param string $file Full path and filename of ZIP archive. |
| 401 |
* @param string $to Full path on the filesystem to extract archive to. |
| 402 |
* @return true|WP_Error True on success, WP_Error on failure. |
| 403 |
*/ |
| 404 |
function unzip_file($file, $to) { |
| 405 |
try { |
| 406 |
$zip = new \ZipArchive; |
| 407 |
|
| 408 |
$res = $zip->open($file); |
| 409 |
if ($res === TRUE) { |
| 410 |
$zip->extractTo($to); |
| 411 |
$zip->close(); |
| 412 |
|
| 413 |
return true; |
| 414 |
} |
| 415 |
} catch (\Throwable $th) { |
| 416 |
return new \WP_Error('exception_caught', $th->getMessage()); |
| 417 |
} |
| 418 |
|
| 419 |
if (isset($zip)) { |
| 420 |
return new \WP_Error('zip_error_' . $zip->status, $zip->getStatusString()); |
| 421 |
} else { |
| 422 |
return new \WP_Error('unknown_error', ''); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
/** |
| 427 |
* @throws Exception |
| 428 |
*/ |
| 429 |
private function read_manifest() { |
| 430 |
$manifest_content = file_get_contents( $this->dir_path . DIRECTORY_SEPARATOR . 'manifest.json' ); |
| 431 |
if ( empty( $manifest_content ) ) { |
| 432 |
$this->throw( __( 'Cannot be imported, as the manifest file is corrupted', 'templately' ) ); |
| 433 |
} |
| 434 |
|
| 435 |
$this->manifest = json_decode( $manifest_content, true ); |
| 436 |
$this->removeLog( 'temp' ); |
| 437 |
|
| 438 |
// TODO: Read & Broadcast the LOG for waiting list |
| 439 |
// $this->sse_log( 'plugin', 'Installing required plugins', '--', 'updateLog', 'processing' ); |
| 440 |
// // $this->sse_log( 'extra-content', 'Import Extra Contents (i.e: Forms)', '--', 'updateLog', 'processing' ); |
| 441 |
// $this->sse_log( 'templates', 'Import Templates (i.e: Header, Footer etc)', '--', 'updateLog', 'processing' ); |
| 442 |
// // $this->sse_log( 'content', 'Import Pages, Posts etc', '--', 'updateLog', 'processing' ); |
| 443 |
// $this->sse_log( 'wp-content', 'Importing Pages, Posts, Navigation, etc', '--', 'updateLog', 'processing' ); |
| 444 |
// $this->sse_log( 'finalize', 'Finalizing Your Imports', '--', 'updateLog', 'processing' ); |
| 445 |
} |
| 446 |
|
| 447 |
private function skipped_plugin(): bool { |
| 448 |
return empty( $this->request_params['plugins'] ) || ! is_array( $this->request_params['plugins'] ); |
| 449 |
} |
| 450 |
|
| 451 |
private function install_dependencies() { |
| 452 |
$allowed_themes = ['hello-elementor', 'twentytwentyfour']; |
| 453 |
|
| 454 |
if ( ! empty( $this->request_params['theme'] ) && is_array( $this->request_params['theme'] ) ) { |
| 455 |
// activate theme |
| 456 |
$theme = $this->request_params['theme']; |
| 457 |
|
| 458 |
$this->before_install_hook(); |
| 459 |
|
| 460 |
if (isset($theme['stylesheet']) && in_array($theme['stylesheet'], $allowed_themes)) { |
| 461 |
// do_action('before_theme_activation', $theme); // Trigger action before theme activation |
| 462 |
$this->sse_log( 'theme', 'Installing and activating theme: ' . $theme['name'], 0 ); |
| 463 |
$plugin_status = Installer::get_instance()->install_and_activate_theme( $theme['stylesheet'] ); |
| 464 |
|
| 465 |
if (!$plugin_status['success']) { |
| 466 |
$this->sse_message( [ |
| 467 |
'action' => 'updateLog', |
| 468 |
'status' => 'error', |
| 469 |
'message' => "Failed to activate theme: " . $theme['name'], |
| 470 |
'type' => "theme", |
| 471 |
'progress' => 0 |
| 472 |
] ); |
| 473 |
$this->dependency_data['theme'] = [ |
| 474 |
'success' => false, |
| 475 |
'name' => $theme['name'], |
| 476 |
'slug' => $theme['stylesheet'], |
| 477 |
'message' => $plugin_status['message'] ?? '' |
| 478 |
]; |
| 479 |
} else { |
| 480 |
// do_action('after_theme_activation', $theme); // Trigger action after theme activation |
| 481 |
|
| 482 |
$this->sse_message( [ |
| 483 |
'action' => 'updateLog', |
| 484 |
'status' => 'complete', |
| 485 |
'message' => "Activated theme: " . $theme['name'], |
| 486 |
'type' => "theme", |
| 487 |
'progress' => 100 |
| 488 |
] ); |
| 489 |
$this->dependency_data['theme'] = [ |
| 490 |
'success' => true, |
| 491 |
'name' => $theme['name'], |
| 492 |
'slug' => $theme['stylesheet'], |
| 493 |
'message' => $plugin_status['message'] ?? '' |
| 494 |
]; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
$this->after_install_hook(); |
| 499 |
} |
| 500 |
else { |
| 501 |
$this->removeLog( 'theme' ); |
| 502 |
} |
| 503 |
if ( ! empty( $this->request_params['plugins'] ) && is_array( $this->request_params['plugins'] ) ) { |
| 504 |
// $this->sse_log( 'plugin', 'Installing Plugins', 1 ); |
| 505 |
$total_plugin = count( $this->request_params['plugins'] ); |
| 506 |
|
| 507 |
$total_plugin_installed = $total_plugin; |
| 508 |
$_installed_plugins = 0; |
| 509 |
|
| 510 |
$this->before_install_hook(); |
| 511 |
|
| 512 |
foreach ( $this->request_params['plugins'] as $dependency ) { |
| 513 |
$this->sse_log( 'plugin', 'Installing required plugins: ' . $dependency['name'], floor( ( 100 * $_installed_plugins / $total_plugin ) ) ); |
| 514 |
|
| 515 |
$dependency['slug'] = $dependency['plugin_original_slug']; |
| 516 |
$plugin_status = Installer::get_instance()->install( $dependency ); |
| 517 |
|
| 518 |
if ( ! $plugin_status['success'] ) { |
| 519 |
$this->sse_message( [ |
| 520 |
'position' => 'plugin', |
| 521 |
'action' => 'updateLog', |
| 522 |
'status' => 'error', |
| 523 |
'message' => 'Installation Failed: ' . $dependency['name'] . ' (' . ( $plugin_status['message'] ?? '' ) . ')', |
| 524 |
'type' => "plugin_{$dependency['plugin_original_slug']}", |
| 525 |
'progress' => 0 |
| 526 |
] ); |
| 527 |
|
| 528 |
if(isset($dependency['mustHave']) && $dependency['mustHave']){ |
| 529 |
$this->removeLog( 'plugin' ); |
| 530 |
$this->throw( 'Installation Failed: ' . $dependency['name'] . ' (' . ( $plugin_status['message'] ?? '' ) . ')' ); |
| 531 |
} |
| 532 |
|
| 533 |
$this->dependency_data['plugins']['failed'][] = [ |
| 534 |
'name' => $dependency['name'], |
| 535 |
'slug' => $dependency['slug'], |
| 536 |
'link' => $dependency['link'], |
| 537 |
'message' => $plugin_status['message'] ?? '' |
| 538 |
]; |
| 539 |
} else { |
| 540 |
$_installed_plugins++; |
| 541 |
$total_plugin_installed--; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
$this->after_install_hook(); |
| 546 |
|
| 547 |
$this->sse_message( [ |
| 548 |
'action' => 'updateLog', |
| 549 |
'status' => 'complete', |
| 550 |
'message' => "Installed required plugins ($_installed_plugins/$total_plugin)", |
| 551 |
'type' => "plugin", |
| 552 |
'progress' => 100 |
| 553 |
] ); |
| 554 |
$this->dependency_data['plugins']['total'] = $total_plugin; |
| 555 |
$this->dependency_data['plugins']['succeed'] = $_installed_plugins; |
| 556 |
} else { |
| 557 |
$this->removeLog( 'plugin' ); |
| 558 |
} |
| 559 |
|
| 560 |
|
| 561 |
$this->update_session_data( [ |
| 562 |
'dependency_data' => json_encode($this->dependency_data), |
| 563 |
] ); |
| 564 |
// $this->sse_log( 'plugin', 'Skipped Installing Plugins', '--', 'updateLog', 'skipped' ); |
| 565 |
} |
| 566 |
|
| 567 |
private function before_install_hook() { |
| 568 |
// remove_all_actions( 'wp_loaded' ); |
| 569 |
// remove_all_actions( 'after_setup_theme' ); |
| 570 |
// remove_all_actions( 'plugins_loaded' ); |
| 571 |
// remove_all_actions( 'init' ); |
| 572 |
|
| 573 |
// making sure so that no redirection happens during plugin installation and hooks triggered bellow. |
| 574 |
add_filter('wp_redirect', '__return_false', 999); |
| 575 |
} |
| 576 |
|
| 577 |
private function after_install_hook() { |
| 578 |
// do_action( 'wp_loaded' ); |
| 579 |
// do_action( 'after_setup_theme' ); |
| 580 |
// do_action( 'plugins_loaded' ); |
| 581 |
// do_action( 'init' ); |
| 582 |
} |
| 583 |
|
| 584 |
/** |
| 585 |
* @throws Exception |
| 586 |
*/ |
| 587 |
private function start_content_import() { |
| 588 |
add_filter('upload_mimes', array($this, 'allow_svg_upload')); |
| 589 |
add_filter('elementor/files/allow_unfiltered_upload', '__return_true'); |
| 590 |
|
| 591 |
$import = new Import( $this ); |
| 592 |
$imported_data = $import->run(); |
| 593 |
|
| 594 |
$this->handle_import_status('success'); |
| 595 |
|
| 596 |
update_option('templately_flush_rewrite_rules', true, false); |
| 597 |
|
| 598 |
$this->sse_message( [ |
| 599 |
'type' => 'complete', |
| 600 |
'action' => 'complete', |
| 601 |
'results' => $this->normalize_imported_data( $imported_data ) |
| 602 |
] ); |
| 603 |
} |
| 604 |
|
| 605 |
private function normalize_imported_data( $data ) { |
| 606 |
$templates = ! empty( $data['templates']['succeed'] ) ? count( $data['templates']['succeed'] ) : 0; |
| 607 |
$template_types = ! empty( $data['templates']['template_types'] ) ? $data['templates']['template_types'] : []; |
| 608 |
|
| 609 |
$post_types = []; |
| 610 |
$content_templates = []; |
| 611 |
if ( ! empty( $data['content'] ) && is_array( $data['content'] ) ) { |
| 612 |
foreach ( $data['content'] as $type => $type_data ) { |
| 613 |
$content_templates[ $type ] = ! empty( $type_data['succeed'] ) ? count( $type_data['succeed'] ) : 0; |
| 614 |
$post_types[] = $this->get_post_type_label_by_slug($type); |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
$contents = []; |
| 619 |
if ( ! empty( $data['wp-content'] ) && is_array( $data['wp-content'] ) ) { |
| 620 |
foreach ( $data['wp-content'] as $type => $type_data ) { |
| 621 |
$contents[ $type ] = ! empty( $type_data['succeed'] ) ? count( $type_data['succeed'] ) : 0; |
| 622 |
if(!in_array($type, ['wp_navigation', 'nav_menu_item'])){ |
| 623 |
$post_types[] = $this->get_post_type_label_by_slug($type); |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
Helper::log( $data ); |
| 629 |
|
| 630 |
return [ |
| 631 |
'templates' => $templates, |
| 632 |
'contents' => $content_templates, |
| 633 |
'wp-content' => $contents, |
| 634 |
'post_types' => $post_types, |
| 635 |
'template_types' => $template_types, |
| 636 |
'dependency_data' => $this->dependency_data, |
| 637 |
]; |
| 638 |
} |
| 639 |
|
| 640 |
public function get_request_params() { |
| 641 |
return $this->request_params; |
| 642 |
} |
| 643 |
|
| 644 |
private function revert() { |
| 645 |
// $request = $this->get_request_params(); |
| 646 |
// if ( isset( $request['revert'] ) && $request['revert'] ) { |
| 647 |
// // TODO: Implement the Revert Process. |
| 648 |
// } |
| 649 |
} |
| 650 |
|
| 651 |
public function redirect_for_archives( $link, $post_id ) { |
| 652 |
$archive_settings = get_option( 'templately_post_archive' ); |
| 653 |
if ( ! empty( $archive_settings ) && intval( $archive_settings['post_id'] ) === intval( $post_id ) ) { |
| 654 |
$link = str_replace( $post_id, $archive_settings['archive_id'], $link ); |
| 655 |
} |
| 656 |
|
| 657 |
return $link; |
| 658 |
} |
| 659 |
|
| 660 |
public function allow_svg_upload($mimes) { |
| 661 |
// Allow SVG |
| 662 |
$mimes['svg'] = 'image/svg+xml'; |
| 663 |
return $mimes; |
| 664 |
} |
| 665 |
|
| 666 |
public function register_shutdown(){ |
| 667 |
$status = connection_status(); |
| 668 |
$last_error = error_get_last(); |
| 669 |
if ($status != CONNECTION_NORMAL && $last_error && $last_error['type'] === E_ERROR) { |
| 670 |
if ((defined('WP_DEBUG') && WP_DEBUG || defined('TEMPLATELY_EVENT_LOG') && TEMPLATELY_EVENT_LOG) && !empty($last_error['message'])) { |
| 671 |
$full_message = $last_error['message']; |
| 672 |
|
| 673 |
// Split the message at the first newline to remove the stack trace |
| 674 |
$message_parts = preg_split('/\n/', $full_message); |
| 675 |
|
| 676 |
// The error message is the first part |
| 677 |
$error_message = $message_parts[0]; |
| 678 |
} else { |
| 679 |
// Generic error message |
| 680 |
$error_message = sprintf(__("It seems we're experiencing technical difficulties. Please try again or contact <a href='%s' target='_blank'>support</a>.", "templately"), 'https://wpdeveloper.com/support'); |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
$this->handle_import_status('failed', $error_message); |
| 685 |
// Handle the error, e.g. log it or display a message to the user |
| 686 |
$this->sse_message( [ |
| 687 |
'action' => 'error', |
| 688 |
'status' => 'error', |
| 689 |
'type' => "error", |
| 690 |
'title' => __("Oops!", "templately"), |
| 691 |
'message' => $error_message, |
| 692 |
// 'position' => 'plugin', |
| 693 |
// 'progress' => '--', |
| 694 |
] ); |
| 695 |
} |
| 696 |
|
| 697 |
$this->debug_log( "Shutdown:....." ); |
| 698 |
$this->debug_log( "connection_status: " . $this->getConnectionStatusText() ); |
| 699 |
$this->debug_log( $last_error ); |
| 700 |
} |
| 701 |
|
| 702 |
public function handle_import_status($status, $description = '') { |
| 703 |
if ($this->is_import_status_handled) { |
| 704 |
Helper::log("Import status already handled: $status"); |
| 705 |
return null; |
| 706 |
} |
| 707 |
$this->is_import_status_handled = $status; |
| 708 |
|
| 709 |
$download_key = $this->download_key; |
| 710 |
|
| 711 |
$headers = [ |
| 712 |
'Content-Type' => 'application/json', |
| 713 |
'Authorization' => 'Bearer ' . $this->api_key, |
| 714 |
'download_key' => $download_key, |
| 715 |
'download-key' => $download_key, |
| 716 |
'x-templately-ip' => Helper::get_ip(), |
| 717 |
'x-templately-url' => home_url( '/' ), |
| 718 |
]; |
| 719 |
|
| 720 |
$args = [ |
| 721 |
'headers' => $headers, |
| 722 |
]; |
| 723 |
|
| 724 |
|
| 725 |
if ($status === 'success') { |
| 726 |
$url = $this->get_api_url( "v1", 'import/success'); |
| 727 |
$args['body'] = json_encode(['type' => 'pack']); |
| 728 |
$response = wp_remote_post($url, $args); |
| 729 |
} elseif ($status === 'failed') { |
| 730 |
$url = $this->get_api_url( "v1", 'import/failed'); |
| 731 |
$args['body'] = json_encode(['type' => 'pack', 'description' => $description ?: "Something Went wrong....."]); |
| 732 |
$response = wp_remote_post($url, $args); |
| 733 |
} |
| 734 |
|
| 735 |
Helper::log($response); |
| 736 |
|
| 737 |
if (is_wp_error($response)) { |
| 738 |
// Handle error |
| 739 |
Helper::log($response->get_error_message()); |
| 740 |
} else { |
| 741 |
// Handle success |
| 742 |
$body = wp_remote_retrieve_body($response); |
| 743 |
// Do something with $body |
| 744 |
return $body; |
| 745 |
} |
| 746 |
|
| 747 |
return null; |
| 748 |
} |
| 749 |
|
| 750 |
protected function getConnectionStatusText() { |
| 751 |
$status = connection_status(); |
| 752 |
switch ($status) { |
| 753 |
case CONNECTION_NORMAL: |
| 754 |
return "Normal"; |
| 755 |
case CONNECTION_ABORTED: |
| 756 |
return "Aborted"; |
| 757 |
case CONNECTION_TIMEOUT: |
| 758 |
return "Timeout"; |
| 759 |
default: |
| 760 |
return "Unknown"; |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
protected function get_post_type_label_by_slug($slug) { |
| 765 |
$post_type_obj = get_post_type_object($slug); |
| 766 |
if($post_type_obj) { |
| 767 |
return $post_type_obj->label; |
| 768 |
} |
| 769 |
return null; |
| 770 |
} |
| 771 |
|
| 772 |
public function import_info(){ |
| 773 |
|
| 774 |
$platform = isset($_GET['platform']) ? $_GET['platform'] : 'elementor'; |
| 775 |
$id = isset($_GET['id']) ? intval($_GET['id']) : 0; |
| 776 |
|
| 777 |
$response = wp_remote_get( $this->info_get_api_url($id), [ |
| 778 |
'timeout' => 30, |
| 779 |
'headers' => [ |
| 780 |
'Authorization' => 'Bearer ' . $this->api_key, |
| 781 |
'x-templately-ip' => Helper::get_ip(), |
| 782 |
'x-templately-url' => home_url( '/' ), |
| 783 |
'x-templately-version' => TEMPLATELY_VERSION, |
| 784 |
] |
| 785 |
] ); |
| 786 |
|
| 787 |
if( is_wp_error( $response ) ){ |
| 788 |
wp_send_json_error( $response->get_error_message() ); |
| 789 |
return; |
| 790 |
} |
| 791 |
// If the response code is not 200, return the error message |
| 792 |
if( wp_remote_retrieve_response_code( $response ) != 200 ){ |
| 793 |
wp_send_json_error( json_decode(wp_remote_retrieve_body( $response )) ); |
| 794 |
return; |
| 795 |
} |
| 796 |
// If the response body is JSON and it contains an error, return the error message |
| 797 |
// Retrieve Data from Response Body. |
| 798 |
$body = wp_remote_retrieve_body( $response ); |
| 799 |
$data = json_decode($body, true); |
| 800 |
|
| 801 |
if (isset($data['error'])) { |
| 802 |
wp_send_json_error($data['error']); |
| 803 |
return; |
| 804 |
} |
| 805 |
|
| 806 |
if(isset($data['data']['manifest'])){ |
| 807 |
$data['data']['manifest'] = json_decode($data['data']['manifest'], true); |
| 808 |
} |
| 809 |
if(isset($data['data']['settings'])){ |
| 810 |
$data['data']['settings'] = json_decode($data['data']['settings'], true); |
| 811 |
} |
| 812 |
|
| 813 |
// Return the response body |
| 814 |
wp_send_json($data); |
| 815 |
} |
| 816 |
|
| 817 |
} |