PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.6.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.6.0
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Core / Importer / Runners / DownloadZip.php

DownloadZip.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.6.0, at includes/Core/Importer/Runners/DownloadZip.php

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