PluginProbe
AI Builder – Generate pages, blocks, images & translate with AI / 2.1.2
AI Builder – Generate pages, blocks, images & translate with AI v2.1.2
2.7.10 2.7.9 2.7.8 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 2.3.10 All 122 releases
ai-builder / assets / js / multi-page-apply.js

multi-page-apply.js in AI Builder – Generate pages, blocks, images & translate with AI 2.1.2, at assets/js/multi-page-apply.js

168 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 document.addEventListener('DOMContentLoaded', async () => {
2 try {
3 const params = new URLSearchParams(window.location.search);
4 const genId = params.get('aibui_gen_id');
5 if (!genId) return;
6
7 // Wait for Gutenberg editor readiness
8 await (async function waitForEditorReady() {
9 const start = Date.now();
10 const timeoutMs = 15000;
11 while (Date.now() - start < timeoutMs) {
12 if (window.wp && wp.blocks && wp.data && wp.data.select('core/block-editor')) {
13 const dispatcher = wp.data.dispatch('core/block-editor');
14 if (dispatcher && typeof dispatcher.insertBlocks === 'function') break;
15 }
16 await new Promise(r => setTimeout(r, 200));
17 }
18 })();
19 if (!window.wp || !wp.blocks || !wp.data) return;
20
21 // Try closing the pattern chooser/welcome modal if visible
22 try {
23 if (wp.data.dispatch('core/preferences')) {
24 // Disable welcome guide preference
25 wp.data.dispatch('core/preferences').set('core/edit-post', 'welcomeGuide', false);
26 }
27 // Close any open dialog with a close button
28 setTimeout(() => {
29 const dialog = document.querySelector('[role="dialog"]');
30 const closeBtn = dialog && dialog.querySelector('button[aria-label="Close"], [aria-label="Close dialog"], [aria-label="Close"]');
31 if (closeBtn) closeBtn.click();
32 // Fallback: send Escape
33 if (!closeBtn && dialog) {
34 const evt = new KeyboardEvent('keydown', { key: 'Escape', keyCode: 27, which: 27, bubbles: true });
35 document.dispatchEvent(evt);
36 }
37 }, 200);
38 } catch (e) { }
39
40 // Fetch generation by ID
41 const res = await fetch(aiBuilderVars.ajaxurl, {
42 method: 'POST',
43 headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
44 body: `action=aibui_get_generation&nonce=${aiBuilderVars.nonce}&id=${encodeURIComponent(genId)}`,
45 });
46 const data = await res.json();
47 if (!data.success) return;
48
49 const gen = data.data;
50 const blocksJson = Array.isArray(gen.blocksJson) ? gen.blocksJson : [];
51
52 // Helper to build blocks recursively (same shape as chat-widget)
53 function buildBlock(block) {
54 const { blockName, attrs = {}, innerBlocks = [] } = block;
55 return wp.blocks.createBlock(
56 blockName,
57 attrs,
58 innerBlocks.map(buildBlock)
59 );
60 }
61
62 // Insert blocks
63 if (blocksJson.length > 0) {
64 const newBlocks = blocksJson.map(buildBlock);
65 wp.data.dispatch('core/block-editor').resetBlocks([]);
66 wp.data.dispatch('core/block-editor').insertBlocks(newBlocks);
67 }
68
69 // Apply title
70 if (gen.title) {
71 wp.data.dispatch('core/editor').editPost({ title: gen.title });
72 }
73
74 // Function to inject admin CSS for hiding title
75 function injectAICreatedAdminCSS() {
76 if (document.getElementById('aibui-hide-ai-title-admin')) {
77 return;
78 }
79
80 const style = document.createElement('style');
81 style.id = 'aibui-hide-ai-title-admin';
82 style.type = 'text/css';
83 style.textContent = `
84 .editor-post-title,
85 .editor-post-title__input,
86 .edit-post-visual-editor__post-title-wrapper,
87 .editor-post-title__block,
88 .wp-block[data-type="core/post-title"],
89 .block-editor-block-list__block[data-type="core/post-title"],
90 .wp-block-post-title.editor-post-title__block {
91 display: none !important;
92 visibility: hidden !important;
93 height: 0 !important;
94 margin: 0 !important;
95 padding: 0 !important;
96 overflow: hidden !important;
97 opacity: 0 !important;
98 }
99
100 .edit-post-visual-editor__post-title-wrapper {
101 display: none !important;
102 visibility: hidden !important;
103 height: 0 !important;
104 margin: 0 !important;
105 padding: 0 !important;
106 overflow: hidden !important;
107 }
108 `;
109 document.head.appendChild(style);
110 }
111
112 // Save CSS/meta through existing chat-widget helpers if available
113 async function saveMetaAndCss() {
114 try {
115 if (gen.cssContent && typeof saveCSSInPostMeta === 'function' && typeof loadCSSFromPostMeta === 'function') {
116 await saveCSSInPostMeta(gen.cssContent, 'page');
117 await loadCSSFromPostMeta();
118 }
119 if (gen.metaDesc && typeof setMetaDescriptionField === 'function') {
120 await setMetaDescriptionField(gen.metaDesc);
121 }
122 } catch (e) {
123 // Silent fail
124 }
125 }
126 await saveMetaAndCss();
127
128 // Auto-save the post (leave as draft by default)
129 try {
130 await wp.data.dispatch('core/editor').savePost();
131 } catch (e) { }
132
133 // Mark generation as applied (attach pageId if available)
134 const postId = (wp.data.select('core/editor').getCurrentPostId && wp.data.select('core/editor').getCurrentPostId()) || 0;
135 try {
136 await fetch(aiBuilderVars.ajaxurl, {
137 method: 'POST',
138 headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
139 body: `action=aibui_mark_generation_applied&nonce=${aiBuilderVars.nonce}&id=${encodeURIComponent(genId)}&page_id=${encodeURIComponent(postId)}`,
140 });
141 } catch (e) { }
142
143 // Mark page as created via AI
144 if (postId) {
145 try {
146 await fetch(aiBuilderVars.ajaxurl, {
147 method: 'POST',
148 headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
149 body: `action=aibui_mark_ai_created&nonce=${aiBuilderVars.nonce}&post_id=${encodeURIComponent(postId)}`,
150 });
151 // Injecter le CSS admin immédiatement
152 injectAICreatedAdminCSS();
153 } catch (e) {
154 console.error('Error marking page as AI-created:', e);
155 }
156 }
157
158 // Clean URL param to avoid reapplying on refresh
159 const url = new URL(window.location.href);
160 url.searchParams.delete('aibui_gen_id');
161 window.history.replaceState({}, '', url.toString());
162 } catch (e) {
163 // Silent
164 }
165 });
166
167
168