| @@ -3,25 +3,21 @@ | ||
| 3 | 3 | namespace Templately\Core\Importer\Runners; |
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | use Exception; |
| 7 | -use Templately\Core\Importer\Utils\AIContentHelper; | |
| 8 | 7 | use Templately\Core\Importer\Utils\Utils; |
| 9 | -use Templately\Core\Importer\Utils\AIUtils; | |
| 10 | -use Templately\Utils\Helper; | |
| 11 | 8 | |
| 12 | 9 | class Finalizer extends BaseRunner { |
| 13 | - use AIContentHelper; | |
| 14 | 10 | private $options = []; |
| 15 | 11 | private $type_to_check = [ 'templates', 'content' ]; |
| 16 | 12 | private $imported_data; |
| 17 | 13 | |
| 18 | - public $type = ''; | |
| 19 | - private $data = []; | |
| 20 | - public $sub_type = ''; | |
| 14 | + private $type = ''; | |
| 15 | + private $sub_type = ''; | |
| 21 | 16 | private $extra_content; |
| 22 | 17 | |
| 23 | 18 | private $total_counts = 0; |
| 19 | + private $processed_items = 0; | |
| 24 | 20 | |
| 25 | 21 | /** |
| 26 | 22 | * @var array|mixed |
| 27 | 23 | */ |
| @@ -42,20 +38,18 @@ | ||
| 42 | 38 | public function log_message(): string { |
| 43 | 39 | return __( 'Finalizing Your Imports', 'templately' ); |
| 44 | 40 | } |
| 45 | 41 | |
| 46 | - public function should_run( $param_data, $imported_data = [] ): bool { | |
| 42 | + public function should_run( $data, $imported_data = [] ): bool { | |
| 47 | 43 | $data = []; |
| 48 | 44 | |
| 49 | 45 | foreach ( $this->type_to_check as $type ) { |
| 50 | 46 | $contents = ! empty ( $this->manifest[ $type ] ) ? $this->manifest[ $type ] : []; |
| 51 | 47 | if ( $type == 'templates' ) { |
| 52 | - // $imported_data["templates"]["__attachments"][691] | |
| 53 | - $this->prepare( $data, $contents, $type, null, $imported_data ); | |
| 48 | + $this->prepare( $data, $contents, $type ); | |
| 54 | 49 | } else { |
| 55 | 50 | foreach ( $contents as $post_type => $templates ) { |
| 56 | - // $imported_data["content"]["__attachments"][$post_type][11] | |
| 57 | - $this->prepare( $data, $templates, $type, $post_type, $imported_data ); | |
| 51 | + $this->prepare( $data, $templates, $type, $post_type ); | |
| 58 | 52 | } |
| 59 | 53 | } |
| 60 | 54 | } |
| 61 | 55 | $this->options = &$data; |
| @@ -62,27 +56,14 @@ | ||
| 62 | 56 | |
| 63 | 57 | return ! empty( $data ) || $this->platform == 'gutenberg'; |
| 64 | 58 | } |
| 65 | 59 | |
| 66 | - private function prepare( &$data, $templates, $type, $sub_type = null, $imported_data = [] ) { | |
| 60 | + private function prepare( &$data, $templates, $type, $sub_type = null ) { | |
| 67 | 61 | if ( empty( $templates ) || ! is_array( $templates ) ) { |
| 68 | 62 | return; |
| 69 | 63 | } |
| 70 | 64 | 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"]) ) { | |
| 65 | + if ( ! isset( $template['data'] ) && !isset( $template['__attachments']) && !isset($template['has_logo']) ) { | |
| 85 | 66 | continue; |
| 86 | 67 | } |
| 87 | 68 | |
| 88 | 69 | // if ( ! isset( $template['data']['form'] ) && ! isset( $template['data']['nav_menus'] )) { |
| @@ -99,9 +80,8 @@ | ||
| 99 | 80 | } |
| 100 | 81 | } |
| 101 | 82 | |
| 102 | 83 | public function import( $data, $imported_data ): array { |
| 103 | - $this->data = &$data; | |
| 104 | 84 | $this->imported_data = &$imported_data; |
| 105 | 85 | |
| 106 | 86 | $this->json->imported_data = $this->imported_data; |
| 107 | 87 | $this->json->map_post_ids = Utils::map_old_new_post_ids( $this->imported_data ); |
| @@ -109,24 +89,22 @@ | ||
| 109 | 89 | if ( ! empty( $imported_data['extra-content'] ) ) { |
| 110 | 90 | $this->extra_content = $imported_data['extra-content']; |
| 111 | 91 | } |
| 112 | 92 | |
| 93 | + $this->log( 0 ); | |
| 113 | 94 | add_action('templately_import.finalize_gutenberg_attachment', [$this, 'post_log'], 10, 2); |
| 114 | 95 | |
| 115 | - $this->update_settings(); | |
| 116 | - | |
| 117 | - $this->loop( $this->options, function($type, $contents ) { | |
| 96 | + foreach ( $this->options as $type => $contents ) { | |
| 118 | 97 | $this->type = $type; |
| 119 | - | |
| 120 | 98 | if ( $type == 'templates' ) { |
| 121 | - $this->finalize_imports( $contents, $type ); | |
| 99 | + $this->finalize_imports( $contents ); | |
| 122 | 100 | } else { |
| 123 | - $this->loop( $contents, function($post_type, $templates ) use($type) { | |
| 101 | + foreach ( $contents as $post_type => $templates ) { | |
| 124 | 102 | $this->sub_type = $post_type; |
| 125 | - $this->finalize_imports( $templates, $type, $post_type ); | |
| 126 | - }, $type); | |
| 103 | + $this->finalize_imports( $templates ); | |
| 104 | + } | |
| 127 | 105 | } |
| 128 | - }); | |
| 106 | + } | |
| 129 | 107 | |
| 130 | 108 | if ( $this->platform == 'gutenberg' ) { |
| 131 | 109 | $this->regenerate_assets(); |
| 132 | 110 | } |
| @@ -141,113 +119,30 @@ | ||
| 141 | 119 | rmdir( $upload_dir['basedir'] . '/eb-style/' ); |
| 142 | 120 | } |
| 143 | 121 | } |
| 144 | 122 | |
| 145 | - private function finalize_imports( $templates, $type, $post_type = null ) { | |
| 146 | - // used for counting | |
| 147 | - $processed = $this->get_loop_result([], '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) { | |
| 123 | + private function finalize_imports( $templates ) { | |
| 124 | + foreach ( $templates as $old_template_id => $template_settings ) { | |
| 155 | 125 | 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]; | |
| 126 | + $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR; | |
| 127 | + if ( ! empty( $this->sub_type ) ) { | |
| 128 | + $path .= $this->sub_type . DIRECTORY_SEPARATOR; | |
| 159 | 129 | } |
| 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 | - | |
| 130 | + $path .= "{$old_template_id}.json"; | |
| 131 | + $template_json = Utils::read_json_file( $path ); | |
| 196 | 132 | $params = $this->origin->get_request_params(); |
| 197 | 133 | $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update(); |
| 198 | 134 | |
| 199 | - $processed[] = $old_template_id; | |
| 200 | - // Add the template to the processed templates and update the session data | |
| 201 | - $this->set_loop_result( $processed, 'finalized_imports'); | |
| 202 | 135 | // Broadcast Log |
| 203 | - $progress = floor( ( 100 * count($processed) ) / $this->total_counts ); | |
| 204 | - if(empty($progress)){ | |
| 205 | - $xyz = 0; | |
| 206 | - } | |
| 136 | + $this->processed_items += 1; | |
| 137 | + $progress = floor( ( 100 * $this->processed_items ) / $this->total_counts ); | |
| 207 | 138 | $this->log( $progress ); |
| 208 | - | |
| 209 | 139 | } catch ( Exception $e ) { |
| 210 | - | |
| 140 | + continue; | |
| 211 | 141 | } |
| 212 | - | |
| 213 | - | |
| 214 | - }, "$type-$post_type"); | |
| 142 | + } | |
| 215 | 143 | } |
| 216 | 144 | |
| 217 | 145 | public function post_log($id, $size_dimension = null){ |
| 218 | 146 | $this->log(-1, "Imported attachment: $id" . ( $size_dimension ? " - $size_dimension" : ''), 'eventLog'); |
| 219 | 147 | } |
| 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 | -} | |
| 148 | +} | |