| @@ -1,10 +1,9 @@ | ||
| 1 | -import { updatePage } from '@auto-launch/functions/pages'; | |
| 1 | +import { sectionSlug, updatePage } from '@auto-launch/functions/pages'; | |
| 2 | 2 | import { alreadyActive } from '@auto-launch/functions/plugins'; |
| 3 | 3 | import { getOption, getPageById } from '@auto-launch/functions/wp'; |
| 4 | 4 | import { AI_HOST } from '@constants'; |
| 5 | 5 | import { reqDataBasics } from '@shared/lib/data'; |
| 6 | -import { pageNames } from '@shared/lib/pages'; | |
| 7 | 6 | import { getBlockContent, parse } from '@wordpress/blocks'; |
| 8 | 7 | |
| 9 | 8 | const { homeUrl } = window.extSharedData; |
| 10 | 9 | const buttonRegex = /href="(#extendify-[\w-]+)"/gi; |
| @@ -9,9 +8,13 @@ | ||
| 9 | 8 | const { homeUrl } = window.extSharedData; |
| 10 | 9 | const buttonRegex = /href="(#extendify-[\w-]+)"/gi; |
| 11 | 10 | const pagesWithButtons = (p) => p?.content?.raw?.match(buttonRegex); |
| 12 | 11 | |
| 13 | -export const updateButtonLinks = async (wpPages, pluginPages) => { | |
| 12 | +export const updateButtonLinks = async ( | |
| 13 | + wpPages, | |
| 14 | + pluginPages, | |
| 15 | + headerCode = '', | |
| 16 | +) => { | |
| 14 | 17 | // Fetch active plugins after installing plugins |
| 15 | 18 | const contactPageSlug = wpPages.find(({ originalSlug }) => |
| 16 | 19 | originalSlug?.startsWith('contact'), |
| 17 | 20 | )?.slug; |
| @@ -31,8 +34,12 @@ | ||
| 31 | 34 | // TODO: Filter out patterns from pages that have identical buttons? |
| 32 | 35 | ); |
| 33 | 36 | }); |
| 34 | 37 | |
| 38 | + if (headerCode?.match(buttonRegex)) { | |
| 39 | + patternsToProcess.push(headerCode); | |
| 40 | + } | |
| 41 | + | |
| 35 | 42 | // Collect the page slugs to share with the server |
| 36 | 43 | const availablePages = wpPages |
| 37 | 44 | .concat(pluginPages) |
| 38 | 45 | .filter(({ slug }) => !slug.startsWith('home')) |
| @@ -94,18 +101,26 @@ | ||
| 94 | 101 | ) |
| 95 | 102 | .filter((r) => r.status === 'fulfilled') |
| 96 | 103 | .map((r) => r.value); |
| 97 | 104 | |
| 98 | - return ( | |
| 99 | - wpPages | |
| 100 | - // Add the new pages into the wpPages array | |
| 101 | - .map((p) => newPages.find(({ id }) => id === p.id) || p) | |
| 102 | - // Also include the originalSlug from wpPages | |
| 103 | - .map((p) => { | |
| 104 | - const { originalSlug } = wpPages.find(({ id }) => id === p.id) || {}; | |
| 105 | - return { ...p, originalSlug }; | |
| 105 | + const updatedHeaderCode = linkKeys | |
| 106 | + ? headerCode.replace(new RegExp(linkKeys, 'g'), (match) => { | |
| 107 | + const link = suggestedLinks[match.replace(/"/g, '')]; | |
| 108 | + if (link === '/') return `"${homeUrl}/${contactPageSlug ?? ''}"`; | |
| 109 | + return `"${homeUrl}/${link.replace(/^\//, '')}"`; | |
| 106 | 110 | }) |
| 107 | - ); | |
| 111 | + : headerCode.replace(new RegExp(buttonRegex, 'g'), 'href="#"'); | |
| 112 | + | |
| 113 | + const updatedPages = wpPages | |
| 114 | + // Add the new pages into the wpPages array | |
| 115 | + .map((p) => newPages.find(({ id }) => id === p.id) || p) | |
| 116 | + // Also include the originalSlug from wpPages | |
| 117 | + .map((p) => { | |
| 118 | + const { originalSlug } = wpPages.find(({ id }) => id === p.id) || {}; | |
| 119 | + return { ...p, originalSlug }; | |
| 120 | + }); | |
| 121 | + | |
| 122 | + return { wpPages: updatedPages, headerCode: updatedHeaderCode }; | |
| 108 | 123 | }; |
| 109 | 124 | |
| 110 | 125 | export const updateSinglePageLinksToSections = async ( |
| 111 | 126 | wpPages, |
| @@ -110,11 +125,12 @@ | ||
| 110 | 125 | export const updateSinglePageLinksToSections = async ( |
| 111 | 126 | wpPages, |
| 112 | 127 | pages, |
| 113 | 128 | options, |
| 129 | + headerCode = '', | |
| 114 | 130 | ) => { |
| 115 | 131 | let homePageContent = wpPages?.[0]?.content?.raw; |
| 116 | - if (!homePageContent) return wpPages; | |
| 132 | + if (!homePageContent) return { wpPages, headerCode }; | |
| 117 | 133 | const { objective, activePlugins, landingPageCTALink } = options; |
| 118 | 134 | |
| 119 | 135 | /** |
| 120 | 136 | * Special case handling for landing page sites. |
| @@ -135,24 +151,22 @@ | ||
| 135 | 151 | `href="${ctaHref}"`, |
| 136 | 152 | ), |
| 137 | 153 | }); |
| 138 | 154 | |
| 139 | - return wpPages; | |
| 155 | + return { | |
| 156 | + wpPages, | |
| 157 | + headerCode: headerCode.replaceAll( | |
| 158 | + /href="(#extendify-[\w|-]+)"/gi, | |
| 159 | + `href="${ctaHref}"`, | |
| 160 | + ), | |
| 161 | + }; | |
| 140 | 162 | } |
| 141 | 163 | |
| 142 | 164 | // get all the patterns that we have in the home page |
| 143 | - const patternTypes = pages?.[0]?.patterns | |
| 144 | - ?.map((pattern) => pattern?.patternTypes?.[0]) | |
| 145 | - ?.filter((patternType) => patternType !== 'hero-header') | |
| 146 | - ?.map((patternType) => { | |
| 147 | - const { slug } = | |
| 148 | - Object.values(pageNames).find(({ alias }) => | |
| 149 | - alias.includes(patternType), | |
| 150 | - ) || {}; | |
| 151 | - return slug; | |
| 152 | - }) | |
| 153 | - ?.filter(Boolean) | |
| 154 | - ?.flat(); | |
| 165 | + const sectionSlugs = pages?.[0]?.patterns | |
| 166 | + ?.filter((pattern) => pattern?.patternTypes?.[0] !== 'hero-header') | |
| 167 | + ?.map(sectionSlug) | |
| 168 | + ?.filter(Boolean); | |
| 155 | 169 | |
| 156 | 170 | const createdPages = |
| 157 | 171 | pages |
| 158 | 172 | ?.filter((page) => page.slug !== 'home') |
| @@ -175,9 +189,9 @@ | ||
| 175 | 189 | if (alreadyActive(activePlugins, 'the-events-calendar')) { |
| 176 | 190 | pluginPages.push('events'); |
| 177 | 191 | } |
| 178 | 192 | |
| 179 | - const allAvailablePages = (patternTypes ?? []).concat(pluginPages); | |
| 193 | + const allAvailablePages = (sectionSlugs ?? []).concat(pluginPages); | |
| 180 | 194 | if (!allAvailablePages.length) { |
| 181 | 195 | wpPages[0] = updatePage({ |
| 182 | 196 | id: wpPages[0].id, |
| 183 | 197 | content: homePageContent.replaceAll( |
| @@ -184,19 +198,28 @@ | ||
| 184 | 198 | /href="(#extendify-[\w|-]+)"/gi, |
| 185 | 199 | 'href="#"', |
| 186 | 200 | ), |
| 187 | 201 | }); |
| 188 | - return wpPages; | |
| 202 | + return { | |
| 203 | + wpPages, | |
| 204 | + headerCode: headerCode.replaceAll( | |
| 205 | + /href="(#extendify-[\w|-]+)"/gi, | |
| 206 | + 'href="#"', | |
| 207 | + ), | |
| 208 | + }; | |
| 189 | 209 | } |
| 190 | 210 | |
| 191 | 211 | // get the suggested links from the AI and send both the patterns and the plugin pages. |
| 192 | 212 | const { suggestedLinks } = |
| 193 | 213 | (await getLinkSuggestions({ |
| 194 | - pageContent: homePageContent, | |
| 214 | + pageContent: headerCode | |
| 215 | + ? `${homePageContent}\n${headerCode}` | |
| 216 | + : homePageContent, | |
| 195 | 217 | availablePages: allAvailablePages, |
| 196 | 218 | })) || {}; |
| 197 | 219 | |
| 198 | 220 | // replace the links |
| 221 | + let updatedHeaderCode = headerCode; | |
| 199 | 222 | homePageContent = Object.keys(suggestedLinks ?? {}).reduce((content, key) => { |
| 200 | 223 | const slug = suggestedLinks[key]; |
| 201 | 224 | |
| 202 | 225 | if (!slug) return content; |
| @@ -204,8 +227,9 @@ | ||
| 204 | 227 | const newLink = pluginPages.concat(createdPages).includes(slug) |
| 205 | 228 | ? `"${homeUrl}/${slug}"` |
| 206 | 229 | : `"${homeUrl}/#${slug}"`; |
| 207 | 230 | |
| 231 | + updatedHeaderCode = updatedHeaderCode.replaceAll(`"${key}"`, newLink); | |
| 208 | 232 | return content.replaceAll(`"${key}"`, newLink); |
| 209 | 233 | }, homePageContent); |
| 210 | 234 | |
| 211 | 235 | // Update the first page by replacing the buttons urls with the new slug |
| @@ -213,9 +237,9 @@ | ||
| 213 | 237 | id: wpPages[0].id, |
| 214 | 238 | content: homePageContent, |
| 215 | 239 | }); |
| 216 | 240 | |
| 217 | - return wpPages; | |
| 241 | + return { wpPages, headerCode: updatedHeaderCode }; | |
| 218 | 242 | }; |
| 219 | 243 | |
| 220 | 244 | const getLinkSuggestions = async ({ pageContent, availablePages }) => { |
| 221 | 245 | try { |