/** * The one place that knows *how* the admin app's settings tree is loaded and * saved, so nothing else has to care which host it's running in. * * The same React app mounts in two places (see * `Aureon\Customizer\Admin_Panel::enqueue_bundle()`), and each has its own * natural mechanism: * * - `page` — the Appearance → Aureon theme options page. There is no * `wp.customize` on a plain wp-admin screen, so the tree round-trips over * `GET`/`POST /aureon/v1/settings` (see `Aureon\Core\REST`), and Save is * the app's own button. * - `customizer` — mounted inside `customize.php`. Every path is a real * registered `WP_Customize_Setting` (see `Aureon\Customizer\Customizer`), * so the tree reads straight out of `wp.customize()` synchronously and Save * defers to core's own Publish (see customize-bridge.ts). * * Both land on the same `aureon_settings` option through the same * `Settings` sanitization, so a value saved from either host is * indistinguishable to the rest of the theme — which is exactly what lets * both surfaces stay live at once without one shadowing the other. */ import apiFetch from '@wordpress/api-fetch'; import { getPath, setPathValue } from './tree-utils'; import { getValue as getCustomizeValue, setValue as setCustomizeValue, publish as publishCustomizeSave, } from './customize-bridge'; import type { SettingsSchema, WorkingSettings } from './types'; /** Which surface this bundle is mounted in. Defaults to the standalone page. */ export const HOST: 'page' | 'customizer' = window.aureonAdmin?.host ?? 'page'; /** * True inside `customize.php`. Also the flag the app uses to decide what to * *render*, not just how to persist: the four design-token groups have native * `WP_Customize_Section`s of their own there (so they stay out of this app's * internal nav), and the Customizer supplies its own preview pane (so the * app's own `LivePreview` iframe would be a second, redundant one). */ export const isCustomizer = 'customizer' === HOST; const SETTINGS_PATH = window.aureonAdmin?.settingsPath ?? '/aureon/v1/settings'; export interface LoadedSettings { schema: SettingsSchema; defaults: Partial< WorkingSettings >; settings: WorkingSettings; } /** * Re-assemble the full `WorkingSettings` tree from `wp.customize()` — used * both for the initial load and to refresh state after a save, since * `wp.customize()`'s own values are the authoritative post-sanitize source * either way (see customize-bridge.ts). */ export const readWorkingTreeFromCustomize = (): WorkingSettings => { const ids = window.aureonAdmin?.settingIds ?? {}; const tree: Record< string, unknown > = {}; Object.keys( ids ).forEach( ( path ) => { setPathValue( tree, path, getCustomizeValue( path ) ); } ); return tree as WorkingSettings; }; /** * Load the tree, its defaults and the control schema. * * The Customizer host resolves synchronously (defaults/schema ride in the * config blob, the tree comes from `wp.customize()`) but still returns a * promise, so the caller has one shape to handle. */ export function loadSettings(): Promise< LoadedSettings > { if ( isCustomizer ) { return Promise.resolve( { schema: window.aureonAdmin?.schema ?? {}, defaults: window.aureonAdmin?.defaults ?? {}, settings: readWorkingTreeFromCustomize(), } ); } return apiFetch< LoadedSettings >( { path: SETTINGS_PATH } ); } /** * Re-read just the control schema, over REST on *both* hosts. * * Deliberately not `loadSettings()`: the schema describes state the theme * reads from elsewhere in WordPress (which WooCommerce pages use the block * cart/checkout, what shipping methods exist, the store currency …), none of * which lives in the settings tree, so a plugin action can invalidate it * without touching a single setting — see the `refresh` flag on a panel * action's response in `Admin_Panel::panel_sections()`'s docblock. * * The Customizer host gets its schema from the config blob, which was * serialized when `customize.php` rendered and is precisely what goes stale * here, so that host has to ask the server too. Only the schema is adopted by * the callers — re-adopting `settings` would clobber whatever the user has * edited but not yet published. */ export function loadSchema(): Promise< SettingsSchema > { return apiFetch< LoadedSettings >( { path: SETTINGS_PATH } ).then( ( res ) => res.schema ?? {} ); } /** * Persist the working tree, resolving with the authoritative post-sanitize * state the caller should adopt as both its new baseline and its displayed * value. * * On the Customizer host only the paths that actually changed are pushed into * their settings — each `.set()` marks the Customizer dirty and live-updates * its preview immediately, same as editing a native control would — and * `publish()` then triggers the real save. * * @param working The edited tree. * @param saved The last-saved baseline, to diff against (Customizer host * only; the REST host POSTs the tree wholesale). */ export function saveSettings( working: WorkingSettings, saved: WorkingSettings | null ): Promise< WorkingSettings > { if ( ! isCustomizer ) { return apiFetch< { settings: WorkingSettings } >( { path: SETTINGS_PATH, method: 'POST', data: working, } ).then( ( res ) => res.settings ); } const ids = window.aureonAdmin?.settingIds ?? {}; Object.keys( ids ).forEach( ( path ) => { const next = getPath( working, path ); const prev = getPath( saved, path ); if ( JSON.stringify( next ) !== JSON.stringify( prev ) ) { setCustomizeValue( path, next ); } } ); return publishCustomizeSave().then( () => readWorkingTreeFromCustomize() ); }