| @@ -4,10 +4,10 @@ | ||
| 4 | 4 | |
| 5 | 5 | |
| 6 | 6 | use Exception; |
| 7 | 7 | use Templately\Core\Importer\Utils\AIContentHelper; |
| 8 | -use Templately\Core\Importer\Utils\AIContentResolver; | |
| 9 | 8 | use Templately\Core\Importer\Utils\Utils; |
| 9 | +use Templately\Core\Importer\Utils\AIUtils; | |
| 10 | 10 | use Templately\Utils\Helper; |
| 11 | 11 | |
| 12 | 12 | class Finalizer extends BaseRunner { |
| 13 | 13 | use AIContentHelper; |
| @@ -48,14 +48,12 @@ | ||
| 48 | 48 | |
| 49 | 49 | foreach ( $this->type_to_check as $type ) { |
| 50 | 50 | $contents = ! empty ( $this->manifest[ $type ] ) ? $this->manifest[ $type ] : []; |
| 51 | 51 | if ( $type == 'templates' ) { |
| 52 | - // $imported_data["templates"]["__attachments"][691] | |
| 53 | - $this->prepare( $data, $contents, $type, null, $imported_data ); | |
| 52 | + $this->prepare( $data, $contents, $type ); | |
| 54 | 53 | } else { |
| 55 | 54 | foreach ( $contents as $post_type => $templates ) { |
| 56 | - // $imported_data["content"]["__attachments"][$post_type][11] | |
| 57 | - $this->prepare( $data, $templates, $type, $post_type, $imported_data ); | |
| 55 | + $this->prepare( $data, $templates, $type, $post_type ); | |
| 58 | 56 | } |
| 59 | 57 | } |
| 60 | 58 | } |
| 61 | 59 | $this->options = &$data; |
| @@ -62,36 +60,14 @@ | ||
| 62 | 60 | |
| 63 | 61 | return ! empty( $data ) || $this->platform == 'gutenberg'; |
| 64 | 62 | } |
| 65 | 63 | |
| 66 | - private function prepare( &$data, $templates, $type, $sub_type = null, $imported_data = [] ) { | |
| 64 | + private function prepare( &$data, $templates, $type, $sub_type = null ) { | |
| 67 | 65 | if ( empty( $templates ) || ! is_array( $templates ) ) { |
| 68 | 66 | return; |
| 69 | 67 | } |
| 70 | 68 | 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 | - // Pick up docs the import runners flagged as carrying source-site links | |
| 85 | - // (Mega Menu / nav / button URLs) so the finalize loop rewrites them to the | |
| 86 | - // real imported permalinks — same opt-in mechanism as __attachments above. | |
| 87 | - // $imported_data["templates"]["__link_rewrites"][52] | |
| 88 | - // $imported_data["content"]["page"]["__link_rewrites"][35] | |
| 89 | - $needs_link_rewrite = $sub_type | |
| 90 | - ? isset( $imported_data[ $type ][ $sub_type ]['__link_rewrites'][ $id ] ) | |
| 91 | - : isset( $imported_data[ $type ]['__link_rewrites'][ $id ] ); | |
| 92 | - | |
| 93 | - if ( ! isset( $template['data'] ) && !$has_attachment && !$needs_link_rewrite && !isset($template['has_logo']) && !$this->is_ai_content($id) && !isset($template["page_settings"]["fluent_cart_store_settings"]) ) { | |
| 69 | + if ( ! isset( $template['data'] ) && !isset( $template['__attachments']) && !isset($template['has_logo']) && !$this->is_ai_content($id) ) { | |
| 94 | 70 | continue; |
| 95 | 71 | } |
| 96 | 72 | |
| 97 | 73 | // if ( ! isset( $template['data']['form'] ) && ! isset( $template['data']['nav_menus'] )) { |
| @@ -114,12 +90,8 @@ | ||
| 114 | 90 | |
| 115 | 91 | $this->json->imported_data = $this->imported_data; |
| 116 | 92 | $this->json->map_post_ids = Utils::map_old_new_post_ids( $this->imported_data ); |
| 117 | 93 | $this->json->map_term_ids = Utils::map_old_new_term_ids( $this->imported_data ); |
| 118 | - if ( $this->manifest['platform'] === 'elementor' ) { | |
| 119 | - $this->json->set_source_url( $this->manifest['site'] ?? '' ); | |
| 120 | - $this->json->set_source_pages( $this->build_source_pages_map() ); | |
| 121 | - } | |
| 122 | 94 | if ( ! empty( $imported_data['extra-content'] ) ) { |
| 123 | 95 | $this->extra_content = $imported_data['extra-content']; |
| 124 | 96 | } |
| 125 | 97 | |
| @@ -124,10 +96,8 @@ | ||
| 124 | 96 | } |
| 125 | 97 | |
| 126 | 98 | add_action('templately_import.finalize_gutenberg_attachment', [$this, 'post_log'], 10, 2); |
| 127 | 99 | |
| 128 | - $this->update_settings(); | |
| 129 | - | |
| 130 | 100 | $this->loop( $this->options, function($type, $contents ) { |
| 131 | 101 | $this->type = $type; |
| 132 | 102 | |
| 133 | 103 | if ( $type == 'templates' ) { |
| @@ -146,100 +116,8 @@ | ||
| 146 | 116 | |
| 147 | 117 | return []; |
| 148 | 118 | } |
| 149 | 119 | |
| 150 | - /** | |
| 151 | - * Build the ORIGINAL-slug => imported-permalink map handed to ElementorHelper so | |
| 152 | - * links can resolve to the page WordPress actually created (slug-change-proof). | |
| 153 | - * | |
| 154 | - * Keyed by the pack's original slug — which is what stored links still carry — | |
| 155 | - * with the value being get_permalink() of the imported post, so a conflict rename | |
| 156 | - * (about-us → about-us-2) is transparently followed. Pages win over other post | |
| 157 | - * types on a slug collision. WooCommerce system pages (shop, cart, checkout, | |
| 158 | - * my-account) are added as aliases since they live outside the manifest content. | |
| 159 | - * | |
| 160 | - * @return array | |
| 161 | - */ | |
| 162 | - private function build_source_pages_map(): array { | |
| 163 | - $map = []; | |
| 164 | - $map_post_ids = $this->json->map_post_ids; | |
| 165 | - $content = $this->manifest['content'] ?? []; | |
| 166 | - | |
| 167 | - if ( is_array( $content ) ) { | |
| 168 | - foreach ( $content as $post_type => $items ) { | |
| 169 | - if ( ! is_array( $items ) ) { | |
| 170 | - continue; | |
| 171 | - } | |
| 172 | - | |
| 173 | - foreach ( $items as $old_id => $entry ) { | |
| 174 | - $slug = isset( $entry['slug'] ) ? strtolower( trim( (string) $entry['slug'] ) ) : ''; | |
| 175 | - if ( '' === $slug ) { | |
| 176 | - continue; | |
| 177 | - } | |
| 178 | - | |
| 179 | - $new_id = $map_post_ids[ $old_id ] ?? null; | |
| 180 | - if ( empty( $new_id ) ) { | |
| 181 | - continue; | |
| 182 | - } | |
| 183 | - | |
| 184 | - $permalink = get_permalink( $new_id ); | |
| 185 | - if ( ! $permalink || is_wp_error( $permalink ) ) { | |
| 186 | - continue; | |
| 187 | - } | |
| 188 | - | |
| 189 | - // First match wins, but a real `page` always overrides another | |
| 190 | - // post type that happens to share the slug. | |
| 191 | - if ( ! isset( $map[ $slug ] ) || 'page' === $post_type ) { | |
| 192 | - $map[ $slug ] = $permalink; | |
| 193 | - } | |
| 194 | - } | |
| 195 | - } | |
| 196 | - } | |
| 197 | - | |
| 198 | - $this->add_woocommerce_page_aliases( $map ); | |
| 199 | - | |
| 200 | - return $map; | |
| 201 | - } | |
| 202 | - | |
| 203 | - /** | |
| 204 | - * Add WooCommerce system-page slugs to the resolution map. These pages are not in | |
| 205 | - * the manifest content, so menu links to them (often relative, e.g. /demo/shop, | |
| 206 | - * /demo/sign-in) would otherwise only get the plain domain swap. | |
| 207 | - * | |
| 208 | - * Heuristic: the demo's my-account page may be titled/slugged "sign-in", "login", | |
| 209 | - * etc.; those common aliases are mapped to this site's WooCommerce my-account page. | |
| 210 | - * Manifest pages already in the map are never overwritten. | |
| 211 | - * | |
| 212 | - * @param array $map slug => permalink (by reference). | |
| 213 | - * | |
| 214 | - * @return void | |
| 215 | - */ | |
| 216 | - private function add_woocommerce_page_aliases( array &$map ) { | |
| 217 | - if ( ! function_exists( 'wc_get_page_permalink' ) ) { | |
| 218 | - return; | |
| 219 | - } | |
| 220 | - | |
| 221 | - $aliases = [ | |
| 222 | - 'shop' => [ 'shop', 'store', 'products' ], | |
| 223 | - 'cart' => [ 'cart' ], | |
| 224 | - 'checkout' => [ 'checkout' ], | |
| 225 | - 'myaccount' => [ 'my-account', 'myaccount', 'account', 'sign-in', 'signin', 'login', 'log-in', 'dashboard' ], | |
| 226 | - ]; | |
| 227 | - | |
| 228 | - foreach ( $aliases as $wc_page => $slugs ) { | |
| 229 | - $permalink = wc_get_page_permalink( $wc_page ); | |
| 230 | - if ( ! $permalink ) { | |
| 231 | - continue; | |
| 232 | - } | |
| 233 | - | |
| 234 | - foreach ( $slugs as $slug ) { | |
| 235 | - if ( ! isset( $map[ $slug ] ) ) { | |
| 236 | - $map[ $slug ] = $permalink; | |
| 237 | - } | |
| 238 | - } | |
| 239 | - } | |
| 240 | - } | |
| 241 | - | |
| 242 | 120 | private function regenerate_assets() { |
| 243 | 121 | $upload_dir = wp_upload_dir(); |
| 244 | 122 | if ( is_dir( $upload_dir['basedir'] . '/eb-style/' ) ) { |
| 245 | 123 | array_map( 'unlink', glob( $upload_dir['basedir'] . '/eb-style/*.min.css' ) ); |
| @@ -248,9 +126,9 @@ | ||
| 248 | 126 | } |
| 249 | 127 | |
| 250 | 128 | private function finalize_imports( $templates, $type, $post_type = null ) { |
| 251 | 129 | // used for counting |
| 252 | - $processed = $this->get_loop_result([], 'finalized_imports', false); | |
| 130 | + $processed = $this->get_progress([], 'finalized_imports', false); | |
| 253 | 131 | $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR; |
| 254 | 132 | |
| 255 | 133 | if ( ! empty( $this->sub_type ) ) { |
| 256 | 134 | $path .= $this->sub_type . DIRECTORY_SEPARATOR; |
| @@ -272,26 +150,22 @@ | ||
| 272 | 150 | |
| 273 | 151 | $processed_pages = get_option("templately_ai_processed_pages", []); |
| 274 | 152 | $updated_ids = $processed_pages[$this->process_id] ?? []; |
| 275 | 153 | $ai_paths = $this->generateAiFilePaths($old_template_id); |
| 276 | - if($this->is_ai_content($old_template_id) && !file_exists($ai_paths['ai_file_path'])){ | |
| 277 | - // Source-agnostic seam: whichever AI-content source owns this | |
| 278 | - // process (classic per-page callback, chat-id pull, or a future | |
| 279 | - // source) stages the .ai.json; otherwise the shared SSE-wait runs. | |
| 280 | - AIContentResolver::ensure_page_ready([ | |
| 281 | - 'session_id' => $this->session_id, | |
| 282 | - 'process_id' => $this->process_id, | |
| 283 | - 'content_id' => $old_template_id, | |
| 284 | - 'ai_page_ids' => $this->ai_page_ids, | |
| 285 | - 'updated_ids' => $updated_ids, | |
| 286 | - 'sse_callback' => [$this, 'sse_message'], | |
| 287 | - 'progress_id' => 'ai_content_time', | |
| 288 | - 'additional_sse_data' => [ | |
| 289 | - 'name' => method_exists($this, 'get_name') ? $this->get_name() : '', | |
| 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() : '', | |
| 290 | 164 | 'post_type' => $post_type, |
| 291 | - 'id' => $old_template_id, | |
| 292 | - ], | |
| 293 | - ]); | |
| 165 | + 'id' => $old_template_id, | |
| 166 | + ] | |
| 167 | + ); | |
| 294 | 168 | } |
| 295 | 169 | // Check if this is AI content before processing |
| 296 | 170 | if ($this->isAiContent($old_template_id)) { |
| 297 | 171 | // Process AI content using AIContentHelper trait |
| @@ -305,9 +179,9 @@ | ||
| 305 | 179 | $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update(); |
| 306 | 180 | |
| 307 | 181 | $processed[] = $old_template_id; |
| 308 | 182 | // Add the template to the processed templates and update the session data |
| 309 | - $this->set_loop_result( $processed, 'finalized_imports'); | |
| 183 | + $this->update_progress( $processed, null, 'finalized_imports', false); | |
| 310 | 184 | // Broadcast Log |
| 311 | 185 | $progress = floor( ( 100 * count($processed) ) / $this->total_counts ); |
| 312 | 186 | if(empty($progress)){ |
| 313 | 187 | $xyz = 0; |
| @@ -324,37 +198,8 @@ | ||
| 324 | 198 | |
| 325 | 199 | public function post_log($id, $size_dimension = null){ |
| 326 | 200 | $this->log(-1, "Imported attachment: $id" . ( $size_dimension ? " - $size_dimension" : ''), 'eventLog'); |
| 327 | 201 | } |
| 328 | - | |
| 329 | - private function update_settings() { | |
| 330 | - $data = $this->data; | |
| 331 | - $map_post_ids = $this->json->map_post_ids; | |
| 332 | - $saved_options = get_option('fluent_cart_store_settings', []); | |
| 333 | - $mapped_keys = [ | |
| 334 | - 'checkout_page_id', | |
| 335 | - 'custom_payment_page_id', | |
| 336 | - 'registration_page_id', | |
| 337 | - 'login_page_id', | |
| 338 | - 'cart_page_id', | |
| 339 | - 'receipt_page_id', | |
| 340 | - 'shop_page_id', | |
| 341 | - 'customer_profile_page_id', | |
| 342 | - ]; | |
| 343 | - | |
| 344 | - foreach ($mapped_keys as $key) { | |
| 345 | - $old_id = $saved_options[$key] ?? null; | |
| 346 | - if (!empty($old_id) && isset($map_post_ids[$old_id])) { | |
| 347 | - $saved_options[$key] = $map_post_ids[$old_id]; | |
| 348 | - } | |
| 349 | - else if(isset($saved_options[$key])){ | |
| 350 | - unset($saved_options[$key]); | |
| 351 | - } | |
| 352 | - } | |
| 353 | - | |
| 354 | - update_option('fluent_cart_store_settings', $saved_options); | |
| 355 | - } | |
| 356 | - | |
| 357 | 202 | |
| 358 | 203 | |
| 359 | 204 | |
| 360 | 205 | |