/** * Redesigned "Site Identity" control. * * Replaces core's own Logo / Site Title / Tagline / Site Icon controls (see * `Aureon\Customizer\Customizer::override_site_identity()`) with the same * card language every other Aureon surface uses. Reads and writes core's own * `custom_logo`/`blogname`/`blogdescription`/`site_icon` settings directly, * by their literal `wp.customize` id — these live outside the Aureon * settings tree entirely (a theme mod and three options, not part of * `aureon_settings`), same as `StaticFrontPageSection`. */ import { useEffect, useState } from '@wordpress/element'; import type { ReactNode } from 'react'; import { __ } from '@wordpress/i18n'; import apiFetch from '@wordpress/api-fetch'; import { TextControl } from '@wordpress/components'; import { Image as ImageIcon, Plus, Info, Trash2, RefreshCw, X, } from 'lucide-react'; import { getRawValue, setRawValue, subscribeRawValue, } from '../customize-bridge'; interface MediaAttachment { id: number; url: string; sizes?: { thumbnail?: { url?: string }; medium?: { url?: string } }; } interface MediaFrame { open: () => void; on: ( event: string, callback: () => void ) => void; state: () => { get: ( key: string ) => { first: () => { toJSON: () => MediaAttachment }; }; }; } interface PickedImage { id: number; url: string; } // `wp-api-fetch` (already a dependency here — see other apiFetch calls below) // enqueues this alongside itself, the same root+nonce its own default // middleware reads. uploadFile() below needs the raw values since progress // events require XMLHttpRequest, which apiFetch doesn't run on. declare global { interface Window { wpApiSettings?: { root: string; nonce: string }; } } /** * Opens the native Media Library (enqueued via `wp_enqueue_media()`, see * `Admin_Panel::enqueue_bundle()`) and resolves once an image is chosen. * Resolves `undefined` if `wp.media` isn't available — shouldn't happen * here, but keeps callers from needing their own guard. Same shape as * `MediaImageField`'s own picker, duplicated rather than shared since that * component's Replace/Remove-row UI doesn't fit either field here. * * @param title Frame title (and the picker window's heading). */ function pickImage( title: string ): Promise< PickedImage | undefined > { return new Promise( ( resolve ) => { const media = window.wp?.media as unknown as | ( ( options: Record< string, unknown > ) => MediaFrame ) | undefined; if ( ! media ) { resolve( undefined ); return; } const frame = media( { title, button: { text: __( 'Use this image', 'aureon' ) }, library: { type: [ 'image' ] }, multiple: false, } ); frame.on( 'select', () => { const selected = frame.state().get( 'selection' ).first().toJSON(); const url = selected.sizes?.medium?.url ?? selected.sizes?.thumbnail?.url ?? selected.url; resolve( { id: selected.id, url: url ?? '' } ); } ); frame.open(); } ); } /** * Uploads a dropped file straight to the Media Library over core's own * REST endpoint — the dropzone's other path onto the same two settings * `pickImage()` writes, for a file dragged in rather than chosen from the * library. Built on `XMLHttpRequest` rather than `apiFetch`: `fetch` (what * `apiFetch` runs on) exposes no upload-progress event, and the dropzone * shows one while this runs. `wpApiSettings` (root + nonce) is the same * global `@wordpress/api-fetch`'s own default nonce middleware reads — * enqueued here already, since other calls in this file already use apiFetch. * * @param file Dropped file. * @param onProgress Called with the upload's percent complete (0–100) as it * reports progress; only fires when the browser can * compute a total (almost always, for a single file). */ function uploadFile( file: File, onProgress: ( percent: number ) => void ): Promise< PickedImage | undefined > { return new Promise( ( resolve ) => { const settings = window.wpApiSettings; if ( ! settings ) { resolve( undefined ); return; } const body = new FormData(); body.append( 'file', file ); const xhr = new XMLHttpRequest(); xhr.open( 'POST', `${ settings.root }wp/v2/media` ); xhr.setRequestHeader( 'X-WP-Nonce', settings.nonce ); xhr.upload.addEventListener( 'progress', ( event ) => { if ( event.lengthComputable ) { const percent = Math.round( ( event.loaded / event.total ) * 100 ); onProgress( percent ); } } ); xhr.addEventListener( 'load', () => { if ( xhr.status >= 200 && xhr.status < 300 ) { try { const media = JSON.parse( xhr.responseText ); resolve( { id: media.id, url: media.source_url } ); return; } catch ( error ) { // Falls through to resolve( undefined ) below. } } resolve( undefined ); } ); xhr.addEventListener( 'error', () => resolve( undefined ) ); xhr.send( body ); } ); } /** * Fetches one attachment's URL for preview, since a `wp.customize` setting * only ever holds the attachment id, not its URL. * * @param id Attachment id, or `0` for none. */ function useAttachmentUrl( id: number ): string { const [ url, setUrl ] = useState( '' ); useEffect( () => { if ( id <= 0 ) { setUrl( '' ); return; } apiFetch< { source_url?: string } >( { path: `/wp/v2/media/${ id }?_fields=source_url`, } ) .then( ( media ) => setUrl( media.source_url ?? '' ) ) .catch( () => setUrl( '' ) ); }, [ id ] ); return url; } /** The gray tile + brand "+" badge shown in an empty dropzone. */ export function DropzoneGraphic() { return ( ); } /** * One dropzone: click opens the Media Library, and a dragged-in file uploads * straight through {@see uploadFile}, showing a progress bar in place of the * empty-state graphic while it runs. Both paths land on the same * `onPicked( id, url )`. * * @param props Dropzone props. * @param props.onClick Opens the Media Library (a click anywhere on the * dropzone, not just its text label) — ignored while * an upload from a drop is in progress. * @param props.onPicked Called with the chosen/uploaded attachment's id and URL. * @param props.children The empty-state graphic and copy. */ export function Dropzone( { onClick, onPicked, children, }: { onClick: () => void; onPicked: ( id: number, url: string ) => void; children: ReactNode; } ) { const [ isDragOver, setIsDragOver ] = useState( false ); const [ progress, setProgress ] = useState< number | null >( null ); return ( ); } export default function SiteIdentitySection() { const [ logoId, setLogoIdState ] = useState< number >( () => Number( getRawValue( 'custom_logo' ) ) || 0 ); const [ iconId, setIconIdState ] = useState< number >( () => Number( getRawValue( 'site_icon' ) ) || 0 ); const [ title, setTitleState ] = useState< string >( () => ( getRawValue( 'blogname' ) as string ) || '' ); const [ tagline, setTaglineState ] = useState< string >( () => ( getRawValue( 'blogdescription' ) as string ) || '' ); // Another surface changing these same settings (core's own changeset // sync makes it possible) should still be reflected here. useEffect( () => { subscribeRawValue( 'custom_logo', ( value ) => setLogoIdState( Number( value ) || 0 ) ); subscribeRawValue( 'site_icon', ( value ) => setIconIdState( Number( value ) || 0 ) ); subscribeRawValue( 'blogname', ( value ) => setTitleState( String( value ?? '' ) ) ); subscribeRawValue( 'blogdescription', ( value ) => setTaglineState( String( value ?? '' ) ) ); }, [] ); const logoUrl = useAttachmentUrl( logoId ); const iconUrl = useAttachmentUrl( iconId ); // A freshly-uploaded (dropped) attachment already carries its own URL — // showing it immediately avoids waiting on useAttachmentUrl's own fetch // for an id it hasn't seen yet. const [ droppedLogoUrl, setDroppedLogoUrl ] = useState( '' ); const [ droppedIconUrl, setDroppedIconUrl ] = useState( '' ); const setLogo = ( id: number, url = '' ) => { setLogoIdState( id ); setDroppedLogoUrl( url ); setRawValue( 'custom_logo', id ); }; const setIcon = ( id: number, url = '' ) => { setIconIdState( id ); setDroppedIconUrl( url ); setRawValue( 'site_icon', id ); }; const setTitle = ( value: string ) => { setTitleState( value ); setRawValue( 'blogname', value ); }; const setTagline = ( value: string ) => { setTaglineState( value ); setRawValue( 'blogdescription', value ); }; const pickLogo = () => pickImage( __( 'Select logo', 'aureon' ) ).then( ( picked ) => picked && setLogo( picked.id, picked.url ) ); const pickIcon = () => pickImage( __( 'Select site icon', 'aureon' ) ).then( ( picked ) => picked && setIcon( picked.id, picked.url ) ); const resolvedLogoUrl = droppedLogoUrl || logoUrl; const resolvedIconUrl = droppedIconUrl || iconUrl; return (
{ __( 'Upload your site logo. It will be displayed in the header.', 'aureon' ) }
{ resolvedLogoUrl ? ( <> > ) : ({ __( 'The name of your website.', 'aureon' ) }
{ __( 'A short description about your site (optional).', 'aureon' ) }
{ __( 'This icon will be shown in browser tabs, bookmark bars, and within the WordPress mobile apps.', 'aureon' ) }
{ resolvedIconUrl ? ( <>{ __( 'The Site Icon should be square and at least', 'aureon' ) }{ ' ' } 512 × 512{ ' ' } { __( 'pixels for the best results.', 'aureon' ) }