| 1 |
import updateVariation from '@agent/workflows/theme/tools/update-variation'; |
| 2 |
import { appliedVibeOwnership } from '@shared/lib/applied-vibes'; |
| 3 |
import { |
| 4 |
applyPaletteGlobals, |
| 5 |
applyPaletteStyles, |
| 6 |
purgePaletteGlobals, |
| 7 |
} from '@shared/lib/palette-globals'; |
| 8 |
import { getServedPalettes, palettesBySlug } from '@shared/lib/palettes'; |
| 9 |
import { isObject } from '@shared/lib/utils'; |
| 10 |
import { |
| 11 |
preserveVibeSettings, |
| 12 |
preserveVibeStyles, |
| 13 |
} from '@shared/lib/vibe-globals'; |
| 14 |
import apiFetch from '@wordpress/api-fetch'; |
| 15 |
|
| 16 |
const { globalStylesPostID } = window.extSharedData; |
| 17 |
|
| 18 |
export default async ({ colorPalette, variation }) => { |
| 19 |
// The picker offers the theme's own variations when /api/palettes is down, and |
| 20 |
// a colourway leaves every palette leaf it does not declare standing. |
| 21 |
if (!colorPalette) { |
| 22 |
return variation |
| 23 |
? updateVariation({ variation, purge: purgePaletteGlobals }) |
| 24 |
: null; |
| 25 |
} |
| 26 |
|
| 27 |
const siteStyle = await getSiteStyle(); |
| 28 |
|
| 29 |
const [current, palettes, vibes] = await Promise.all([ |
| 30 |
apiFetch({ |
| 31 |
path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, |
| 32 |
}), |
| 33 |
getServedPalettes('agent', [siteStyle.colorPalette, colorPalette]).then( |
| 34 |
palettesBySlug, |
| 35 |
), |
| 36 |
appliedVibeOwnership(siteStyle.vibe), |
| 37 |
]); |
| 38 |
|
| 39 |
// Reporting a colour change nothing wrote is worse than a failed turn. |
| 40 |
if (!palettes[colorPalette]) { |
| 41 |
throw new Error(`No served palette named ${colorPalette}`); |
| 42 |
} |
| 43 |
|
| 44 |
const { settings, styles } = palettes[colorPalette]; |
| 45 |
|
| 46 |
// A second POST to global-styles drops the first. |
| 47 |
const [written] = await Promise.all([ |
| 48 |
apiFetch({ |
| 49 |
method: 'POST', |
| 50 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 51 |
data: { |
| 52 |
id: globalStylesPostID, |
| 53 |
settings: preserveVibeSettings({ |
| 54 |
mergedSettings: applyPaletteGlobals({ |
| 55 |
currentSettings: current.settings, |
| 56 |
paletteSettings: settings, |
| 57 |
palettes, |
| 58 |
}), |
| 59 |
currentSettings: current.settings, |
| 60 |
vibes, |
| 61 |
}), |
| 62 |
styles: preserveVibeStyles({ |
| 63 |
mergedStyles: applyPaletteStyles({ |
| 64 |
currentStyles: current.styles, |
| 65 |
paletteStyles: styles, |
| 66 |
palettes, |
| 67 |
}), |
| 68 |
currentStyles: current.styles, |
| 69 |
vibes, |
| 70 |
}), |
| 71 |
}, |
| 72 |
}), |
| 73 |
writeSiteStyle(siteStyle, colorPalette), |
| 74 |
]); |
| 75 |
|
| 76 |
return written; |
| 77 |
}; |
| 78 |
|
| 79 |
const getSiteStyle = async () => { |
| 80 |
const { data } = await apiFetch({ |
| 81 |
path: '/extendify/v1/launch/options?option=extendify_siteStyle', |
| 82 |
}); |
| 83 |
|
| 84 |
// PHP serialises an empty object as [], which would spread into the write. |
| 85 |
return isObject(data) ? data : {}; |
| 86 |
}; |
| 87 |
|
| 88 |
const writeSiteStyle = (siteStyle, colorPalette) => |
| 89 |
apiFetch({ |
| 90 |
path: '/extendify/v1/launch/options', |
| 91 |
method: 'POST', |
| 92 |
data: { |
| 93 |
option: 'extendify_siteStyle', |
| 94 |
value: { ...siteStyle, colorPalette }, |
| 95 |
}, |
| 96 |
}); |
| 97 |
|