PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.4.6
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.4.6
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.6, at includes/Core/Importer/Runners/Finalizer.php

209 lines 6.4 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 $this->prepare( $data, $contents, $type );
53 } else {
54 foreach ( $contents as $post_type => $templates ) {
55 $this->prepare( $data, $templates, $type, $post_type );
56 }
57 }
58 }
59 $this->options = &$data;
60
61 return ! empty( $data ) || $this->platform == 'gutenberg';
62 }
63
64 private function prepare( &$data, $templates, $type, $sub_type = null ) {
65 if ( empty( $templates ) || ! is_array( $templates ) ) {
66 return;
67 }
68 foreach ( $templates as $id => $template ) {
69 if ( ! isset( $template['data'] ) && !isset( $template['__attachments']) && !isset($template['has_logo']) && !$this->is_ai_content($id) && !isset($template["page_settings"]["fluent_cart_store_settings"]) ) {
70 continue;
71 }
72
73 // if ( ! isset( $template['data']['form'] ) && ! isset( $template['data']['nav_menus'] )) {
74 // continue;
75 // }
76
77 $this->total_counts += 1;
78
79 if ( $sub_type ) {
80 $data[ $type ][ $sub_type ][ $id ] = $template;
81 } else {
82 $data[ $type ][ $id ] = $template;
83 }
84 }
85 }
86
87 public function import( $data, $imported_data ): array {
88 $this->data = &$data;
89 $this->imported_data = &$imported_data;
90
91 $this->json->imported_data = $this->imported_data;
92 $this->json->map_post_ids = Utils::map_old_new_post_ids( $this->imported_data );
93 $this->json->map_term_ids = Utils::map_old_new_term_ids( $this->imported_data );
94 if ( ! empty( $imported_data['extra-content'] ) ) {
95 $this->extra_content = $imported_data['extra-content'];
96 }
97
98 add_action('templately_import.finalize_gutenberg_attachment', [$this, 'post_log'], 10, 2);
99
100 $this->loop( $this->options, function($type, $contents ) {
101 $this->type = $type;
102
103 if ( $type == 'templates' ) {
104 $this->finalize_imports( $contents, $type );
105 } else {
106 $this->loop( $contents, function($post_type, $templates ) use($type) {
107 $this->sub_type = $post_type;
108 $this->finalize_imports( $templates, $type, $post_type );
109 }, $type);
110 }
111 });
112
113 if ( $this->platform == 'gutenberg' ) {
114 $this->regenerate_assets();
115 }
116
117 return [];
118 }
119
120 private function regenerate_assets() {
121 $upload_dir = wp_upload_dir();
122 if ( is_dir( $upload_dir['basedir'] . '/eb-style/' ) ) {
123 array_map( 'unlink', glob( $upload_dir['basedir'] . '/eb-style/*.min.css' ) );
124 rmdir( $upload_dir['basedir'] . '/eb-style/' );
125 }
126 }
127
128 private function finalize_imports( $templates, $type, $post_type = null ) {
129 // used for counting
130 $processed = $this->get_progress([], 'finalized_imports', false);
131 $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR;
132
133 if ( ! empty( $this->sub_type ) ) {
134 $path .= $this->sub_type . DIRECTORY_SEPARATOR;
135 }
136
137 $this->loop( $templates, function($old_template_id, $template_settings ) use($type, $post_type, $path, $processed) {
138 try {
139
140 if($post_type && isset($this->imported_data[$type]['__attachments'][$post_type][$old_template_id])){
141 $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$post_type][$old_template_id];
142 }
143 else if(empty($post_type) && isset($this->imported_data[$type]['__attachments'][$old_template_id])){
144 $template_settings['__attachments'] = $this->imported_data[$type]['__attachments'][$old_template_id];
145 }
146
147 // Read the original template file for non-AI content
148 $original_file = $path . "{$old_template_id}.json";
149 $template_json = Utils::read_json_file($original_file);
150
151 $processed_pages = get_option("templately_ai_processed_pages", []);
152 $updated_ids = $processed_pages[$this->process_id] ?? [];
153 $ai_paths = $this->generateAiFilePaths($old_template_id);
154 if(!empty($this->process_id) && is_numeric($this->process_id) && $this->is_ai_content($old_template_id) && !file_exists($ai_paths['ai_file_path'])){
155 // Use the static timeout-aware wait handler from AIUtils
156 AIUtils::handle_sse_wait_with_timeout(
157 $this->session_id,
158 'ai_content_time',
159 $updated_ids,
160 $this->ai_page_ids,
161 [$this, 'sse_message'],
162 [
163 'name' => method_exists($this, 'get_name') ? $this->get_name() : '',
164 'post_type' => $post_type,
165 'id' => $old_template_id,
166 ],
167 $old_template_id // Pass template ID for local site polling
168 );
169 }
170 // Check if this is AI content before processing
171 if ($this->isAiContent($old_template_id)) {
172 // Process AI content using AIContentHelper trait
173 $ai_result = $this->processAiContent($old_template_id);
174 if($ai_result['is_ai'] && !empty($ai_result['template_json'])){
175 $template_json = $ai_result['template_json'];
176 }
177 }
178
179 $params = $this->origin->get_request_params();
180 $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update();
181
182 $processed[] = $old_template_id;
183 // Add the template to the processed templates and update the session data
184 $this->update_progress( $processed, null, 'finalized_imports', false);
185 // Broadcast Log
186 $progress = floor( ( 100 * count($processed) ) / $this->total_counts );
187 if(empty($progress)){
188 $xyz = 0;
189 }
190 $this->log( $progress );
191
192 } catch ( Exception $e ) {
193
194 }
195
196
197 }, "$type-$post_type");
198 }
199
200 public function post_log($id, $size_dimension = null){
201 $this->log(-1, "Imported attachment: $id" . ( $size_dimension ? " - $size_dimension" : ''), 'eventLog');
202 }
203
204
205
206
207
208 }
209