| 1 |
import { appliedVibeOwnership } from '@shared/lib/applied-vibes'; |
| 2 |
import { deepMerge } from '@shared/lib/utils'; |
| 3 |
import { |
| 4 |
preserveVibeSettings, |
| 5 |
preserveVibeStyles, |
| 6 |
} from '@shared/lib/vibe-globals'; |
| 7 |
import apiFetch from '@wordpress/api-fetch'; |
| 8 |
|
| 9 |
const { globalStylesPostID } = window.extSharedData; |
| 10 |
|
| 11 |
// The vibe layer keeps its owned leaves even when the variation declares them. |
| 12 |
export default async ({ variation, purge = (document) => document }) => { |
| 13 |
const [current, vibes] = await Promise.all([ |
| 14 |
apiFetch({ |
| 15 |
path: `/wp/v2/global-styles/${globalStylesPostID}?context=edit`, |
| 16 |
}), |
| 17 |
getCurrentVibes(), |
| 18 |
]); |
| 19 |
|
| 20 |
const merged = deepMerge( |
| 21 |
purge({ settings: current.settings, styles: current.styles }), |
| 22 |
variation, |
| 23 |
); |
| 24 |
|
| 25 |
return apiFetch({ |
| 26 |
method: 'POST', |
| 27 |
path: `/wp/v2/global-styles/${globalStylesPostID}`, |
| 28 |
data: { |
| 29 |
id: globalStylesPostID, |
| 30 |
settings: preserveVibeSettings({ |
| 31 |
mergedSettings: merged.settings, |
| 32 |
currentSettings: current.settings, |
| 33 |
vibes, |
| 34 |
}), |
| 35 |
styles: preserveVibeStyles({ |
| 36 |
mergedStyles: merged.styles, |
| 37 |
currentStyles: current.styles, |
| 38 |
vibes, |
| 39 |
}), |
| 40 |
}, |
| 41 |
}); |
| 42 |
}; |
| 43 |
|
| 44 |
const getCurrentVibes = async () => { |
| 45 |
try { |
| 46 |
const { data } = await apiFetch({ |
| 47 |
path: '/extendify/v1/launch/options?option=extendify_siteStyle', |
| 48 |
}); |
| 49 |
return await appliedVibeOwnership(data?.vibe); |
| 50 |
} catch { |
| 51 |
return {}; |
| 52 |
} |
| 53 |
}; |
| 54 |
|