| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Core\Importer\Form; |
| 7 |
use Templately\Core\Importer\Runners\BaseRunner; |
| 8 |
use Templately\Core\Importer\Utils\Utils; |
| 9 |
|
| 10 |
class DownloadZip extends BaseRunner { |
| 11 |
|
| 12 |
public function get_name(): string { |
| 13 |
return 'downloadzip'; |
| 14 |
} |
| 15 |
|
| 16 |
public function get_label(): string { |
| 17 |
return __( 'Download Zip', 'templately' ); |
| 18 |
} |
| 19 |
|
| 20 |
public function should_log(): bool { |
| 21 |
return true; |
| 22 |
} |
| 23 |
|
| 24 |
public function get_action(): string { |
| 25 |
return 'eventLog'; |
| 26 |
} |
| 27 |
|
| 28 |
public function log_message(): string { |
| 29 |
return __( 'Updating customizer settings.', 'templately' ); |
| 30 |
} |
| 31 |
|
| 32 |
public function should_run( $data, $imported_data = [] ): bool { |
| 33 |
$params = $this->origin->get_request_params(); |
| 34 |
return ! empty( $params['title'] ) || !empty( $params['slogan'] ); |
| 35 |
} |
| 36 |
|
| 37 |
public function import( $data, $imported_data ): array { |
| 38 |
/** |
| 39 |
* Download the zip |
| 40 |
*/ |
| 41 |
$this->download_zip( $_id ); |
| 42 |
|
| 43 |
$progress['download_zip'] = true; |
| 44 |
$this->update_session_data( [ |
| 45 |
'progress' => $progress, |
| 46 |
] ); |
| 47 |
$this->sse_message( [ |
| 48 |
'type' => 'continue', |
| 49 |
'action' => 'continue', |
| 50 |
'info' => 'download_zip', |
| 51 |
'results' => __METHOD__ . '::' . __LINE__, |
| 52 |
] ); |
| 53 |
|
| 54 |
return [ 'customizer' => $customizer ]; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @throws Exception |
| 59 |
*/ |
| 60 |
private function download_zip( $id ) { |
| 61 |
$this->sse_log( 'download', __( 'Downloading Template Pack', 'templately' ), 1 ); |
| 62 |
$response = wp_remote_get( $this->get_api_url( "v2", "import/pack/$id" ), [ |
| 63 |
'timeout' => 30, |
| 64 |
'headers' => [ |
| 65 |
'Content-Type' => 'application/json', |
| 66 |
'Authorization' => 'Bearer ' . $this->api_key, |
| 67 |
'x-templately-ip' => Helper::get_ip(), |
| 68 |
'x-templately-url' => home_url('/'), |
| 69 |
'x-templately-version' => TEMPLATELY_VERSION, |
| 70 |
] |
| 71 |
]); |
| 72 |
|
| 73 |
$response_code = wp_remote_retrieve_response_code($response); |
| 74 |
$content_type = wp_remote_retrieve_header($response, 'content-type'); |
| 75 |
$this->download_key = wp_remote_retrieve_header($response, 'download-key'); |
| 76 |
|
| 77 |
if (is_wp_error($response)) { |
| 78 |
$this->throw_retryable(__('Template pack download failed', 'templately') . $response->get_error_message()); |
| 79 |
} else if ($response_code != 200) { |
| 80 |
if (strpos($content_type, 'application/json') !== false) { |
| 81 |
// Retrieve Data from Response Body. |
| 82 |
$response_body = json_decode(wp_remote_retrieve_body($response), true); |
| 83 |
|
| 84 |
// If the response body is JSON and it contains an error, throw an exception with the error message |
| 85 |
if (isset($response_body['status']) && $response_body['status'] === 'error') { |
| 86 |
$support_message = ''; |
| 87 |
if(strpos($response_body['message'], 'https://wpdeveloper.com/support') === false){ |
| 88 |
$support_message = sprintf(__(" Please try again or contact <a href='%s' target='_blank'>support</a>.", "templately"), 'https://wpdeveloper.com/support'); |
| 89 |
} |
| 90 |
$this->throw_non_retryable($response_body['message'] . $support_message); |
| 91 |
} |
| 92 |
} |
| 93 |
$this->throw_unknown(__('Template pack download failed with response code: ', 'templately') . $response_code); |
| 94 |
} |
| 95 |
|
| 96 |
$this->sse_log('download', __('Downloading Template Pack', 'templately'), 57); |
| 97 |
|
| 98 |
// $session_id = uniqid(); |
| 99 |
$this->dir_path = $this->tmp_dir . $this->session_id . DIRECTORY_SEPARATOR; |
| 100 |
$this->filePath = $this->tmp_dir . "{$this->session_id}.zip"; |
| 101 |
// $this->session_id = $session_id; |
| 102 |
|
| 103 |
$this->update_session_data([ |
| 104 |
'session_id' => $this->session_id, |
| 105 |
'dir_path' => $this->dir_path, |
| 106 |
'download_key' => $this->download_key, |
| 107 |
]); |
| 108 |
|
| 109 |
if (file_put_contents($this->filePath, $response['body'])) { // phpcs:ignore |
| 110 |
$this->sse_log('download', __('Downloading Template Pack', 'templately'), 100); |
| 111 |
|
| 112 |
$this->unzip(); |
| 113 |
} else { |
| 114 |
$this->throw_retryable(__('Downloading Failed. Please try again', 'templately')); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
} |