| 1 |
import { __, sprintf } from '@wordpress/i18n' |
| 2 |
import { Axios as api } from './axios' |
| 3 |
|
| 4 |
export const parseThemeJson = (themeJson) => |
| 5 |
api.post('onboarding/parse-theme-json', { themeJson }) |
| 6 |
|
| 7 |
export const updateOption = (option, value) => |
| 8 |
api.post('onboarding/options', { option, value }) |
| 9 |
|
| 10 |
export const getOption = async (option) => { |
| 11 |
const { data } = await api.get('onboarding/options', { |
| 12 |
params: { option }, |
| 13 |
}) |
| 14 |
return data |
| 15 |
} |
| 16 |
|
| 17 |
export const createPage = (pageData) => |
| 18 |
api.post(`${window.extOnbData.wpRoot}wp/v2/pages`, pageData) |
| 19 |
|
| 20 |
export const trashPost = (postId, postType) => |
| 21 |
api.delete(`${window.extOnbData.wpRoot}wp/v2/${postType}s/${postId}`) |
| 22 |
|
| 23 |
export const getPost = (postSlug, type = 'post') => |
| 24 |
api.get(`${window.extOnbData.wpRoot}wp/v2/${type}s?slug=${postSlug}`) |
| 25 |
|
| 26 |
export const installPlugin = async (plugin) => { |
| 27 |
// Fail silently if no slug is provided |
| 28 |
if (!plugin?.wordpressSlug) return |
| 29 |
|
| 30 |
try { |
| 31 |
// Install plugin and try to activate it. |
| 32 |
const response = await api.post( |
| 33 |
`${window.extOnbData.wpRoot}wp/v2/plugins`, |
| 34 |
{ |
| 35 |
slug: plugin.wordpressSlug, |
| 36 |
status: 'active', |
| 37 |
}, |
| 38 |
) |
| 39 |
if (!response.ok) return response |
| 40 |
} catch (e) { |
| 41 |
// Fail gracefully for now |
| 42 |
} |
| 43 |
|
| 44 |
try { |
| 45 |
// Try and activate it if the above fails |
| 46 |
return await activatePlugin(plugin) |
| 47 |
} catch (e) { |
| 48 |
// Fail gracefully for now |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
export const activatePlugin = async (plugin) => { |
| 53 |
const endpoint = `${window.extOnbData.wpRoot}wp/v2/plugins` |
| 54 |
const response = await api.get(`${endpoint}?search=${plugin.wordpressSlug}`) |
| 55 |
const pluginSlug = response?.[0]?.plugin |
| 56 |
if (!pluginSlug) { |
| 57 |
throw new Error('Plugin not found') |
| 58 |
} |
| 59 |
// Attempt to activate the plugin with the slug we found |
| 60 |
return await api.post(`${endpoint}/${pluginSlug}`, { status: 'active' }) |
| 61 |
} |
| 62 |
|
| 63 |
export const updateTemplatePart = (part, content) => |
| 64 |
api.post(`${window.extOnbData.wpRoot}wp/v2/template-parts/${part}`, { |
| 65 |
slug: `${part}`, |
| 66 |
theme: 'extendable', |
| 67 |
type: 'wp_template_part', |
| 68 |
status: 'publish', |
| 69 |
description: sprintf( |
| 70 |
__('Added by %s', 'extendify'), |
| 71 |
'Extendify Launch', |
| 72 |
), |
| 73 |
content, |
| 74 |
}) |
| 75 |
|
| 76 |
export const getHeadersAndFooters = async () => { |
| 77 |
let patterns = await getTemplateParts() |
| 78 |
patterns = patterns?.filter((p) => p.theme === 'extendable') |
| 79 |
const headers = patterns?.filter((p) => p?.slug?.includes('header')) |
| 80 |
const footers = patterns?.filter((p) => p?.slug?.includes('footer')) |
| 81 |
return { headers, footers } |
| 82 |
} |
| 83 |
|
| 84 |
const getTemplateParts = () => |
| 85 |
api.get(window.extOnbData.wpRoot + 'wp/v2/template-parts') |
| 86 |
|
| 87 |
export const getThemeVariations = async () => { |
| 88 |
const variations = await api.get( |
| 89 |
window.extOnbData.wpRoot + |
| 90 |
'wp/v2/global-styles/themes/extendable/variations', |
| 91 |
) |
| 92 |
if (!Array.isArray(variations)) { |
| 93 |
throw new Error('Could not get theme variations') |
| 94 |
} |
| 95 |
return { data: variations } |
| 96 |
} |
| 97 |
|
| 98 |
export const updateThemeVariation = (id, variation) => |
| 99 |
api.post(`${window.extOnbData.wpRoot}wp/v2/global-styles/${id}`, { |
| 100 |
id, |
| 101 |
settings: variation.settings, |
| 102 |
styles: variation.styles, |
| 103 |
}) |
| 104 |
|
| 105 |
export const addLaunchPagesToNav = ( |
| 106 |
pages, |
| 107 |
pageIds, |
| 108 |
rawCode, |
| 109 |
replace = null, |
| 110 |
) => { |
| 111 |
if (!replace) |
| 112 |
replace = |
| 113 |
'<!-- wp:page-list {"isNavigationChild":true,"showSubmenuIcon":true,"openSubmenusOnClick":false} /-->' |
| 114 |
const pageListItems = pages |
| 115 |
.map( |
| 116 |
(page) => |
| 117 |
`<!-- wp:navigation-link {"label":"${ |
| 118 |
page.title |
| 119 |
}","type":"page","id":${pageIds[page.slug].id},"url":"/${ |
| 120 |
page.slug |
| 121 |
}","kind":"post-type","isTopLevelLink":true} /-->`, |
| 122 |
) |
| 123 |
.join('') |
| 124 |
return rawCode.replace(replace, pageListItems) |
| 125 |
} |
| 126 |
|