| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
|
| 3 |
const globalStylesPostID = window.extSharedData?.globalStylesPostID; |
| 4 |
const themeSlug = window.extAgentData?.context?.themeSlug; |
| 5 |
|
| 6 |
export default async ({ selectedVibe }) => { |
| 7 |
if ( |
| 8 |
!selectedVibe || |
| 9 |
typeof selectedVibe !== 'string' || |
| 10 |
selectedVibe.trim() === '' |
| 11 |
) { |
| 12 |
return; |
| 13 |
} |
| 14 |
|
| 15 |
try { |
| 16 |
// Fetch variation |
| 17 |
const { styles: variationStyles } = await apiFetch({ |
| 18 |
path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, |
| 19 |
}); |
| 20 |
// Fetch theme styles |
| 21 |
const { styles: themeStyles } = await apiFetch({ |
| 22 |
path: `/wp/v2/global-styles/themes/${themeSlug}?context=edit`, |
| 23 |
}); |
| 24 |
|
| 25 |
const updatedBlocks = {}; |
| 26 |
for (const [blockName, blockObj] of Object.entries(themeStyles.blocks)) { |
| 27 |
if (!blockObj?.variations) { |
| 28 |
updatedBlocks[blockName] = blockObj; |
| 29 |
continue; |
| 30 |
} |
| 31 |
|
| 32 |
const { variations, ...rest } = blockObj; |
| 33 |
updatedBlocks[blockName] = { |
| 34 |
...rest, |
| 35 |
variations: processBlockVariations(variations, selectedVibe), |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
// Apply the update |
| 40 |
await Promise.all([ |
| 41 |
apiFetch({ |
| 42 |
path: `wp/v2/global-styles/${globalStylesPostID}`, |
| 43 |
method: 'POST', |
| 44 |
data: { |
| 45 |
styles: { ...variationStyles, blocks: updatedBlocks }, |
| 46 |
}, |
| 47 |
}), |
| 48 |
updateSiteStyleOption(selectedVibe), |
| 49 |
]); |
| 50 |
} catch (error) { |
| 51 |
const errorMessage = |
| 52 |
error?.response?.data?.message || error?.message || 'Unknown error'; |
| 53 |
throw new Error(`Vibe update failed: ${errorMessage}`); |
| 54 |
} |
| 55 |
}; |
| 56 |
|
| 57 |
const updateSiteStyleOption = async (selectedVibe) => { |
| 58 |
const { data: currentSiteStyle } = await apiFetch({ |
| 59 |
path: '/extendify/v1/launch/options?option=extendify_siteStyle', |
| 60 |
}); |
| 61 |
|
| 62 |
const existingSiteStyle = |
| 63 |
currentSiteStyle && |
| 64 |
typeof currentSiteStyle === 'object' && |
| 65 |
!Array.isArray(currentSiteStyle) |
| 66 |
? currentSiteStyle |
| 67 |
: {}; |
| 68 |
|
| 69 |
const updatedSiteStyle = { ...existingSiteStyle, vibe: selectedVibe }; |
| 70 |
|
| 71 |
return await apiFetch({ |
| 72 |
path: '/extendify/v1/launch/options', |
| 73 |
method: 'POST', |
| 74 |
data: { option: 'extendify_siteStyle', value: updatedSiteStyle }, |
| 75 |
}); |
| 76 |
}; |
| 77 |
|
| 78 |
const processBlockVariations = (variations, targetVibe) => { |
| 79 |
const updatedVariations = {}; |
| 80 |
|
| 81 |
for (const [styleName, styleProperties] of Object.entries(variations)) { |
| 82 |
if (!styleName.includes('--natural-1--')) { |
| 83 |
updatedVariations[styleName] = styleProperties; |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
const sourceStyleName = styleName.replace( |
| 88 |
'--natural-1--', |
| 89 |
`--${targetVibe}--`, |
| 90 |
); |
| 91 |
const sourceStyle = variations[sourceStyleName]; |
| 92 |
|
| 93 |
updatedVariations[styleName] = sourceStyle || styleProperties; |
| 94 |
} |
| 95 |
|
| 96 |
return updatedVariations; |
| 97 |
}; |
| 98 |
|