| @@ -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; |
| @@ -80,18 +80,9 @@ | ||
| 80 | 80 | $has_attachment = isset($imported_data[$type]['__attachments'][$id]); |
| 81 | 81 | } |
| 82 | 82 | } |
| 83 | 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"]) ) { | |
| 84 | + if ( ! isset( $template['data'] ) && !$has_attachment && !isset($template['has_logo']) && !$this->is_ai_content($id) && !isset($template["page_settings"]["fluent_cart_store_settings"]) ) { | |
| 94 | 85 | continue; |
| 95 | 86 | } |
| 96 | 87 | |
| 97 | 88 | // if ( ! isset( $template['data']['form'] ) && ! isset( $template['data']['nav_menus'] )) { |
| @@ -114,12 +105,8 @@ | ||
| 114 | 105 | |
| 115 | 106 | $this->json->imported_data = $this->imported_data; |
| 116 | 107 | $this->json->map_post_ids = Utils::map_old_new_post_ids( $this->imported_data ); |
| 117 | 108 | $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 | 109 | if ( ! empty( $imported_data['extra-content'] ) ) { |
| 123 | 110 | $this->extra_content = $imported_data['extra-content']; |
| 124 | 111 | } |
| 125 | 112 | |
| @@ -146,100 +133,8 @@ | ||
| 146 | 133 | |
| 147 | 134 | return []; |
| 148 | 135 | } |
| 149 | 136 | |
| 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 | 137 | private function regenerate_assets() { |
| 243 | 138 | $upload_dir = wp_upload_dir(); |
| 244 | 139 | if ( is_dir( $upload_dir['basedir'] . '/eb-style/' ) ) { |
| 245 | 140 | array_map( 'unlink', glob( $upload_dir['basedir'] . '/eb-style/*.min.css' ) ); |
| @@ -248,9 +143,9 @@ | ||
| 248 | 143 | } |
| 249 | 144 | |
| 250 | 145 | private function finalize_imports( $templates, $type, $post_type = null ) { |
| 251 | 146 | // used for counting |
| 252 | - $processed = $this->get_loop_result([], 'finalized_imports', false); | |
| 147 | + $processed = $this->get_progress([], 'finalized_imports', false); | |
| 253 | 148 | $path = $this->dir_path . $this->type . DIRECTORY_SEPARATOR; |
| 254 | 149 | |
| 255 | 150 | if ( ! empty( $this->sub_type ) ) { |
| 256 | 151 | $path .= $this->sub_type . DIRECTORY_SEPARATOR; |
| @@ -273,25 +168,22 @@ | ||
| 273 | 168 | $processed_pages = get_option("templately_ai_processed_pages", []); |
| 274 | 169 | $updated_ids = $processed_pages[$this->process_id] ?? []; |
| 275 | 170 | $ai_paths = $this->generateAiFilePaths($old_template_id); |
| 276 | 171 | 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() : '', | |
| 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() : '', | |
| 290 | 181 | 'post_type' => $post_type, |
| 291 | - 'id' => $old_template_id, | |
| 182 | + 'id' => $old_template_id, | |
| 292 | 183 | ], |
| 293 | - ]); | |
| 184 | + $old_template_id // Pass template ID for local site polling | |
| 185 | + ); | |
| 294 | 186 | } |
| 295 | 187 | // Check if this is AI content before processing |
| 296 | 188 | if ($this->isAiContent($old_template_id)) { |
| 297 | 189 | // Process AI content using AIContentHelper trait |
| @@ -305,9 +197,9 @@ | ||
| 305 | 197 | $this->json->prepare( $template_json, $template_settings, $this->extra_content['form'][ $old_template_id ] ?? [], $params )->update(); |
| 306 | 198 | |
| 307 | 199 | $processed[] = $old_template_id; |
| 308 | 200 | // Add the template to the processed templates and update the session data |
| 309 | - $this->set_loop_result( $processed, 'finalized_imports'); | |
| 201 | + $this->update_progress( $processed, null, 'finalized_imports', false); | |
| 310 | 202 | // Broadcast Log |
| 311 | 203 | $progress = floor( ( 100 * count($processed) ) / $this->total_counts ); |
| 312 | 204 | if(empty($progress)){ |
| 313 | 205 | $xyz = 0; |