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