| 1 |
import { getTemplate } from '@onboarding/api/DataApi' |
| 2 |
import { |
| 3 |
updateOption, |
| 4 |
createPage, |
| 5 |
trashPost, |
| 6 |
getPost, |
| 7 |
updateThemeVariation, |
| 8 |
} from '@onboarding/api/WPApi' |
| 9 |
|
| 10 |
export const createWordpressPages = async (pages, siteType, style) => { |
| 11 |
const pageIds = {} |
| 12 |
for (const page of pages) { |
| 13 |
const template = await getTemplate({ |
| 14 |
siteType: siteType.slug, |
| 15 |
layoutType: page.slug, |
| 16 |
baseLayout: page.slug === 'home' ? style?.homeBaseLayout : null, |
| 17 |
kit: page.slug !== 'home' ? style?.kit : null, |
| 18 |
}) |
| 19 |
let content = '' |
| 20 |
if (template?.data) { |
| 21 |
content = [template?.data?.code, template?.data?.code2] |
| 22 |
.filter(Boolean) |
| 23 |
.join('') |
| 24 |
} |
| 25 |
|
| 26 |
const name = page.slug |
| 27 |
// Get content |
| 28 |
const result = await createPage({ |
| 29 |
title: page.title, |
| 30 |
name, |
| 31 |
status: 'publish', |
| 32 |
content: content, |
| 33 |
template: 'no-title', |
| 34 |
meta: { made_with_extendify_launch: true }, |
| 35 |
}) |
| 36 |
pageIds[name] = { id: result.id, title: page.title } |
| 37 |
} |
| 38 |
// When we have home and blog, set reading setting |
| 39 |
if (pageIds?.home) { |
| 40 |
await updateOption('show_on_front', 'page') |
| 41 |
await updateOption('page_on_front', pageIds.home.id) |
| 42 |
if (pageIds?.blog) await updateOption('page_for_posts', pageIds.blog) |
| 43 |
} |
| 44 |
|
| 45 |
// If we've created pages, consider the onboarding in a completed state |
| 46 |
await updateOption( |
| 47 |
'extendify_onboarding_completed', |
| 48 |
new Date().toISOString(), |
| 49 |
) |
| 50 |
return pageIds |
| 51 |
} |
| 52 |
|
| 53 |
export const trashWordpressPages = (posts) => { |
| 54 |
posts.forEach(async (el) => { |
| 55 |
// Try and retrieve posts via slug. |
| 56 |
const post = await getPost(el.slug, el.type) |
| 57 |
|
| 58 |
// If post exists (but not trashed) then send it to trash. |
| 59 |
if (post?.length && post[0].status !== 'trash') { |
| 60 |
await trashPost(post[0].id, el.type) |
| 61 |
} |
| 62 |
}) |
| 63 |
} |
| 64 |
|
| 65 |
export const updateGlobalStyleVariant = (variation) => |
| 66 |
updateThemeVariation(window.extOnbData.globalStylesPostID, variation) |
| 67 |
|