PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.9
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.9
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 / Finalizer.php

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

254 lines 7.7 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
6 use Exception;
7 use Templately\Core\Importer\Utils\AIContentHelper;
8 use Templately\Core\Importer\Utils\Utils;
9 use Templately\Core\Importer\Utils\AIUtils;
10 use Templately\Utils\Helper;
11
12 class Finalizer extends BaseRunner {
13 use AIContentHelper;
14 private $options = [];
15 private $type_to_check = [ 'templates', 'content' ];
16 private $imported_data;
17
18 public $type = '';
19 private $data = [];
20 public $sub_type = '';
21 private $extra_content;
22
23 private $total_counts = 0;
24
25 /**
26 * @var array|mixed
27 */
28 protected $map_post_ids = [];
29 /**
30 * @var array|mixed
31 */
32 protected $map_term_ids = [];
33
34 public function get_name(): string {
35 return 'finalize';
36 }
37
38 public function get_label(): string {
39 return __( 'Finalizing Your Imports', 'templately' );
40 }
41
42 public function log_message(): string {
43 return __( 'Finalizing Your Imports', 'templately' );
44 }
45
46 public function should_run( $param_data, $imported_data = [] ): bool {
47 $data = [];
48
49 foreach ( $this->type_to_check as $type ) {
50 $contents = ! empty ( $this->manifest[ $type ] ) ? $this->manifest[ $type ] : [];
51 if ( $type == 'templates' ) {
52 // $imported_data["templates"]["__attachments"][691]
53 $this->prepare( $data, $contents, $type, null, $imported_data );
54 } else {
55 foreach ( $contents as $post_type => $templates ) {
56 // $imported_data["content"]["__attachments"][$post_type][11]
57 $this->prepare( $data, $templates, $type, $post_type, $imported_data );
58 }
59 }
60 }
61 $this->options = &$data;
62
63 return ! empty( $data ) || $this->platform == 'gutenberg';
64 }
65
66 private function prepare( &$data, $templates, $type, $sub_type = null, $imported_data = [] ) {
67 if ( empty( $templates ) || ! is_array( $templates ) ) {
68 return;
69 }
70 foreach ( $templates as $id => $template ) {
71 // Check for __attachments in both template and imported_data
72 $has_attachment = !empty($template['__attachments']);
73
74 if (!$has_attachment && !empty($imported_data)) {
75 if ($sub_type) {
76 // $imported_data["content"]["__attachments"]["page"][11]
77 $has_attachment = isset($imported_data[$type]['__attachments'][$sub_type][$id]);
78 } else {
79 // $imported_data["templates"]["__attachments"][691]
80 $has_attachment = isset($imported_data[$type]['__attachments'][$id]);
81 }
82 }
83
84 if ( ! isset( $template['data'] ) && !$has_attachment && !isset($template['has_logo']) && !$this->is_ai_content($id) && !isset($template["page_settings"]["fluent_cart_store_settings"]) ) {
85 continue;
86 }
87
88 // if ( ! isset( $template['data']['form'] ) && ! isset( $template['data']['nav_menus'] )) {
89 // continue;
90 // }
91
92 $this->total_counts += 1;
93
94 if ( $sub_type ) {
95 $data[ $type ][ $sub_type ][ $id ] = $template;
96 } else {
97 $data[ $type ][ $id ] = $template;
98 }
99 }
100 }
101
102 public function import( $data, $imported_data ): array {
103 $this->data = &$data;
104 $this->imported_data = &$imported_data;
105
106 $this->json->imported_data = $this->imported_data;
107 $this->json->map_post_ids = Utils::map_old_new_post_ids( $this->imported_data );
108 $this->json->map_term_ids = Utils::map_old_new_term_ids( $this->imported_data );
109 if ( ! empty( $imported_data['extra-content'] ) ) {
110 $this->extra_content = $imported_data['extra-content'];
111 }
112
113 add_action('templately_import.finalize_gutenberg_attachment', [$this, 'post_log'], 10, 2);
114
115 $this->update_settings();
116
117 $this->loop( $this->options, function($type, $contents ) {
118 $this->type = $type;
119
120 if ( $type == 'templates' ) {
121 $this->finalize_imports( $contents, $type );
122 } else {
123 $this->loop( $contents, function($post_type, $templates ) use($type) {
124 $this->sub_type = $post_type;
125 $this->finalize_imports( $templates, $type, $post_type );
126 }, $type);
127 }
128 });
129
130 if ( $this->platform == 'gutenberg' ) {
131 $this->regenerate_assets();
132 }
133
134 return [];
135 }
136
137 private function regenerate_assets() {
138 $upload_dir = wp_upload_dir();
139 if ( is_dir( $upload_dir['basedir'] . '/eb-style/' ) ) {
140 array_map( 'unlink', glob( $upload_dir['basedir'] . '/eb-style/*.min.css' ) );
141 rmdir( $upload_dir['basedir'] . '/eb-style/' );
142 }
143 }
144
145 private function finalize_imports( $templates, $type, $post_type = null ) {
146 // used for counting
147 $processed = $this->get_progress([], 'finalized_imports', false);
148 $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR;
149
150 if ( ! empty( $this->sub_type ) ) {
151 $path .= $this->sub_type . DIRECTORY_SEPARATOR;
152 }
153
154 $this->loop( $templates, function($old_template_id, $template_settings ) use($type, $post_type, $path, $processed) {
155 try {
156
157 if($post_type && isset($this->imported_data[$type]['__attachments'][$post_type][$old_template_id])){
158 $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$post_type][$old_template_id];
159 }
160 else if(empty($post_type) && isset($this->imported_data[$type]['__attachments'][$old_template_id])){
161 $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$old_template_id];
162 }
163
164 // Read the original template file for non-AI content
165 $original_file = $path . "{$old_template_id}.json";
166 $template_json = Utils::read_json_file($original_file);
167
168 $processed_pages = get_option("templately_ai_processed_pages", []);
169 $updated_ids = $processed_pages[$this->process_id] ?? [];
170 $ai_paths = $this->generateAiFilePaths($old_template_id);
171 if($this->is_ai_content($old_template_id) && !file_exists($ai_paths['ai_file_path'])){
172 // Use the static timeout-aware wait handler from AIUtils
173 AIUtils::handle_sse_wait_with_timeout(
174 $this->session_id,
175 'ai_content_time',
176 $updated_ids,
177 $this->ai_page_ids,
178 [$this, 'sse_message'],
179 [
180 'name' => method_exists($this, 'get_name') ? $this->get_name() : '',
181 'post_type' => $post_type,
182 'id' => $old_template_id,
183 ],
184 $old_template_id // Pass template ID for local site polling
185 );
186 }
187 // Check if this is AI content before processing
188 if ($this->isAiContent($old_template_id)) {
189 // Process AI content using AIContentHelper trait
190 $ai_result = $this->processAiContent($old_template_id);
191 if($ai_result['is_ai'] && !empty($ai_result['template_json'])){
192 $template_json = $ai_result['template_json'];
193 }
194 }
195
196 $params = $this->origin->get_request_params();
197 $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update();
198
199 $processed[] = $old_template_id;
200 // Add the template to the processed templates and update the session data
201 $this->update_progress( $processed, null, 'finalized_imports', false);
202 // Broadcast Log
203 $progress = floor( ( 100 * count($processed) ) / $this->total_counts );
204 if(empty($progress)){
205 $xyz = 0;
206 }
207 $this->log( $progress );
208
209 } catch ( Exception $e ) {
210
211 }
212
213
214 }, "$type-$post_type");
215 }
216
217 public function post_log($id, $size_dimension = null){
218 $this->log(-1, "Imported attachment: $id" . ( $size_dimension ? " - $size_dimension" : ''), 'eventLog');
219 }
220
221 private function update_settings() {
222 $data = $this->data;
223 $map_post_ids = $this->json->map_post_ids;
224 $saved_options = get_option('fluent_cart_store_settings', []);
225 $mapped_keys = [
226 'checkout_page_id',
227 'custom_payment_page_id',
228 'registration_page_id',
229 'login_page_id',
230 'cart_page_id',
231 'receipt_page_id',
232 'shop_page_id',
233 'customer_profile_page_id',
234 ];
235
236 foreach ($mapped_keys as $key) {
237 $old_id = $saved_options[$key] ?? null;
238 if (!empty($old_id) && isset($map_post_ids[$old_id])) {
239 $saved_options[$key] = $map_post_ids[$old_id];
240 }
241 else if(isset($saved_options[$key])){
242 unset($saved_options[$key]);
243 }
244 }
245
246 update_option('fluent_cart_store_settings', $saved_options);
247 }
248
249
250
251
252
253 }
254