| @@ -73,14 +73,30 @@ | ||
| 73 | 73 | } |
| 74 | 74 | |
| 75 | 75 | async function ensurePremiumAccess() { |
| 76 | 76 | try { |
| 77 | + // Create AbortController for timeout | |
| 78 | + const controller = new AbortController(); | |
| 79 | + const timeoutId = setTimeout(() => controller.abort(), 20000); // 10 second timeout | |
| 80 | + | |
| 77 | 81 | const tokenResponse = await fetch(ajaxurl, { |
| 78 | 82 | method: "POST", |
| 79 | 83 | headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 80 | 84 | body: "action=aibui_get_token&nonce=" + aiBuilderVars.nonce, |
| 85 | + signal: controller.signal, | |
| 81 | 86 | }); |
| 82 | 87 | |
| 88 | + if (!tokenResponse.ok) { | |
| 89 | + if (tokenResponse.status === 500) { | |
| 90 | + disableMultiPageAccess("Server error: The request took too long. Please refresh the page and try again."); | |
| 91 | + } else { | |
| 92 | + disableMultiPageAccess("Unable to verify authentication. Please refresh the page."); | |
| 93 | + } | |
| 94 | + return false; | |
| 95 | + } | |
| 96 | + | |
| 97 | + clearTimeout(timeoutId); | |
| 98 | + | |
| 83 | 99 | const tokenData = await tokenResponse.json(); |
| 84 | 100 | if (!tokenData.success || !tokenData.data || !tokenData.data.token) { |
| 85 | 101 | disableMultiPageAccess("Please sign in to access the Multi Page Generator."); |
| 86 | 102 | return false; |
| @@ -111,10 +127,19 @@ | ||
| 111 | 127 | |
| 112 | 128 | isPremiumUser = true; |
| 113 | 129 | return true; |
| 114 | 130 | } catch (error) { |
| 131 | + if (typeof timeoutId !== 'undefined' && timeoutId) { | |
| 132 | + clearTimeout(timeoutId); | |
| 133 | + } | |
| 115 | 134 | console.error("Error verifying premium access:", error); |
| 116 | - disableMultiPageAccess("Unable to verify your subscription at this time. Please try again later."); | |
| 135 | + | |
| 136 | + // Handle timeout/abort errors | |
| 137 | + if (error.name === 'AbortError' || error.message.includes('timeout')) { | |
| 138 | + disableMultiPageAccess("Request timeout: The server took too long to respond. Please check your connection and try again."); | |
| 139 | + } else { | |
| 140 | + disableMultiPageAccess("Unable to verify your subscription at this time. Please try again later."); | |
| 141 | + } | |
| 117 | 142 | return false; |
| 118 | 143 | } |
| 119 | 144 | } |
| 120 | 145 | |
| @@ -184,9 +209,8 @@ | ||
| 184 | 209 | return; |
| 185 | 210 | } |
| 186 | 211 | |
| 187 | 212 | totalCostElement.textContent = `${totalCost} credits`; |
| 188 | - console.log(`Updating cost: ${pageCount} pages × 75 = ${totalCost} credits`); | |
| 189 | 213 | } |
| 190 | 214 | |
| 191 | 215 | function generatePagePrompts() { |
| 192 | 216 | if (!isPremiumUser) { |
| @@ -407,16 +431,32 @@ | ||
| 407 | 431 | // Démarrer la simulation de progression |
| 408 | 432 | const fillElement = document.getElementById(`fill-${pageNumber}`); |
| 409 | 433 | const progressSimulator = simulateProgress(fillElement); |
| 410 | 434 | |
| 435 | + let timeoutId; | |
| 411 | 436 | try { |
| 412 | - // Get JWT token | |
| 437 | + // Get JWT token with timeout | |
| 438 | + const controller = new AbortController(); | |
| 439 | + timeoutId = setTimeout(() => controller.abort(), 20000); // 10 second timeout | |
| 440 | + | |
| 413 | 441 | const tokenResponse = await fetch(ajaxurl, { |
| 414 | 442 | method: "POST", |
| 415 | 443 | headers: { "Content-Type": "application/x-www-form-urlencoded" }, |
| 416 | 444 | body: "action=aibui_get_token&nonce=" + aiBuilderVars.nonce, |
| 445 | + signal: controller.signal, | |
| 417 | 446 | }); |
| 418 | 447 | |
| 448 | + clearTimeout(timeoutId); | |
| 449 | + | |
| 450 | + if (!tokenResponse.ok) { | |
| 451 | + if (tokenResponse.status === 500) { | |
| 452 | + progressSimulator.complete(); | |
| 453 | + throw new Error("Server error: The request took too long or encountered an error. Please try again."); | |
| 454 | + } | |
| 455 | + progressSimulator.complete(); | |
| 456 | + throw new Error(`HTTP error! status: ${tokenResponse.status}`); | |
| 457 | + } | |
| 458 | + | |
| 419 | 459 | const tokenData = await tokenResponse.json(); |
| 420 | 460 | |
| 421 | 461 | if (!tokenData.success || !tokenData.data.token) { |
| 422 | 462 | progressSimulator.complete(); |
| @@ -434,8 +474,12 @@ | ||
| 434 | 474 | }, |
| 435 | 475 | body: JSON.stringify({ |
| 436 | 476 | userPrompt: prompt, |
| 437 | 477 | pageContent: "", // Kept for compatibility |
| 478 | + wooCommerceInstalled: !!(typeof aiBuilderVars !== 'undefined' && aiBuilderVars.wooCommerceInstalled && aiBuilderVars.wooCommerceInstalled !== '0'), | |
| 479 | + activeThemeName: (typeof aiBuilderVars !== 'undefined' && aiBuilderVars.activeThemeName) ? aiBuilderVars.activeThemeName : '', | |
| 480 | + sitePlugins: (typeof aiBuilderVars !== 'undefined' && Array.isArray(aiBuilderVars.sitePlugins)) ? aiBuilderVars.sitePlugins.slice(0, 15) : [], | |
| 481 | + wordpressVersion: (typeof aiBuilderVars !== 'undefined' && aiBuilderVars.wordpressVersion) ? String(aiBuilderVars.wordpressVersion) : '', | |
| 438 | 482 | }), |
| 439 | 483 | }); |
| 440 | 484 | |
| 441 | 485 | const data = await response.json(); |
| @@ -459,8 +503,9 @@ | ||
| 459 | 503 | id: `${Date.now()}_${Math.random().toString(36).slice(2, 10)}`, |
| 460 | 504 | title: data.postTitle || `Generated Page ${pageNumber}`, |
| 461 | 505 | metaDesc: data.postMetaDesc || '', |
| 462 | 506 | cssContent: data.cssContent || '', |
| 507 | + jsContent: data.jsContent || '', | |
| 463 | 508 | blocksJson: Array.isArray(data.pageContent) ? data.pageContent : [], |
| 464 | 509 | }; |
| 465 | 510 | const saveRes = await fetch(ajaxurl, { |
| 466 | 511 | method: "POST", |
| @@ -478,10 +523,19 @@ | ||
| 478 | 523 | pageTitle: generationPayload.title, |
| 479 | 524 | }; |
| 480 | 525 | |
| 481 | 526 | } catch (error) { |
| 527 | + if (timeoutId) { | |
| 528 | + clearTimeout(timeoutId); | |
| 529 | + } | |
| 482 | 530 | console.error("Error generating page:", error); |
| 483 | 531 | progressSimulator.complete(); |
| 532 | + | |
| 533 | + // Handle timeout/abort errors | |
| 534 | + if (error.name === 'AbortError' || error.message.includes('timeout')) { | |
| 535 | + return { success: false, error: "Request timeout: The server took too long to respond. Please check your connection and try again." }; | |
| 536 | + } | |
| 537 | + | |
| 484 | 538 | return { success: false, error: error.message }; |
| 485 | 539 | } |
| 486 | 540 | } |
| 487 | 541 | |