import {useState, useEffect} from '@wordpress/element'; import {__, _x} from '@wordpress/i18n'; import {speak} from '@wordpress/a11y'; import {Spinner} from '@wordpress/components'; import PositioningField from '../../../design/form-fields/PositioningField'; import SpacingField from '../../../design/form-fields/SpacingField'; import TextField from '../../../design/form-fields/TextField'; import {buildShortcode} from '../../../design/utils/layoutOverrides'; import Toggle from '../../../design/components/Toggle'; import Button from '../../../design/components/Button'; import ModalHeader from './ModalHeader'; import ModalFooter from './ModalFooter'; const ShortcodeBuilder = ( {context, closeModal, setModalTitle, setIsFullscreen, isFullscreen} ) => { const {entityType, entityId, navigableIds = []} = context; const [editingEntityId, setEditingEntityId] = useState( entityId ); const [entityData, setEntityData] = useState( null ); const [isLoading, setIsLoading] = useState( false ); // Singular form ('ad', 'group', 'placement') const typeSingular = entityType === 'ads' ? 'ad' : (entityType === 'groups' ? 'group' : 'placement'); const [alignment, setAlignment] = useState( 'none' ); const [isInline, setIsInline] = useState( false ); const [margin, setMargin] = useState( {} ); const [className, setClassName] = useState( '' ); const [size, setSize] = useState( {width: '', height: ''} ); const [copied, setCopied] = useState( false ); useEffect( () => { const fetchData = async () => { setIsLoading( true ); try { const data = await wp.apiFetch( {path: `adpresso/v1/${entityType}/${editingEntityId}`} ); setEntityData( data ); setModalTitle( __( 'Shortcode: ', 'adpresso' ) + data.title ); } catch ( error ) { console.error( 'Error fetching entity data:', error ); } finally { setIsLoading( false ); } }; fetchData(); }, [editingEntityId, entityType, setModalTitle] ); //---------------------------------------------------------------------- // Mock the field object from our PHP structure. //---------------------------------------------------------------------- const positioningFieldMock = { id: 'shortcode_alignment', label: __( 'Positioning & Layout', 'adpresso' ), positioning_options: { 'default': { title: __( 'Theme Default', 'adpresso' ), description: __( 'Inherits alignment from your active website theme.', 'adpresso' ), options: { 'none': {title: __( 'None', 'adpresso' )} } }, 'float': { title: _x( 'Float', 'Layout options "Text Flow" heading', 'adpresso' ), description: __( 'The ad is embedded into the text, allowing content to wrap around it.', 'adpresso' ), options: { 'left_float': {title: __( 'Left', 'adpresso' )}, 'right_float': {title: __( 'Right', 'adpresso' )} } }, 'block': { title: _x( 'Block', 'Layout options "Text Flow" heading', 'adpresso' ), description: __( 'The ad stands alone. The following content starts below.', 'adpresso' ), options: { 'left_nofloat': {title: __( 'Left', 'adpresso' )}, 'center_nofloat': {title: __( 'Center', 'adpresso' )}, 'right_nofloat': {title: __( 'Right', 'adpresso' )} } } } }; const spacingFieldMock = { id: 'shortcode_margin', options: { top: {label: __( 'Top', 'adpresso' )}, right: {label: __( 'Right', 'adpresso' )}, bottom: {label: __( 'Bottom', 'adpresso' )}, left: {label: __( 'Left', 'adpresso' )}, }, }; // Placements bring their own layout, only ads and groups get layout overrides. const hasLayout = entityType !== 'placements'; const shortcode = buildShortcode( typeSingular, editingEntityId, { position: hasLayout ? alignment : 'none', margin: hasLayout ? margin : {}, width: hasLayout ? size.width : '', height: hasLayout ? size.height : '', className, inline: isInline, } ); const handleCopy = () => { navigator.clipboard.writeText( shortcode ); setCopied( true ); speak( __( 'Shortcode copied to clipboard.', 'adpresso' ) ); setTimeout( () => setCopied( false ), 2000 ); }; const handleNavigation = ( direction ) => { const currentIndex = navigableIds.indexOf( editingEntityId ); if ( currentIndex === - 1 ) { return; } let newEntityId = null; if ( direction === 'prev' && currentIndex > 0 ) { newEntityId = navigableIds[currentIndex - 1]; } else if ( direction === 'next' && currentIndex < navigableIds.length - 1 ) { newEntityId = navigableIds[currentIndex + 1]; } if ( newEntityId ) { setEditingEntityId( newEntityId ); } }; const currentIndex = navigableIds.indexOf( editingEntityId ); const isPrevDisabled = currentIndex <= 0; const isNextDisabled = currentIndex >= navigableIds.length - 1; if ( isLoading || !entityData ) { return (
); } return (
{ }} // Read-only in shortcode mode onNavigate={handleNavigation} onClose={closeModal} isPrevDisabled={isPrevDisabled} isNextDisabled={isNextDisabled} onNavigateBack={null} previousEntity={null} onRefresh={() => { }} setIsFullscreen={setIsFullscreen} isFullscreen={isFullscreen} showFullscreen={false} />

{__( 'Shortcode', 'adpresso' )}

{shortcode}

{__( 'Copy this shortcode and paste it anywhere in your content.', 'adpresso' )}

{hasLayout && (

{__( 'Positioning & Layout', 'adpresso' )}

{__( 'Margin (px)', 'adpresso' )}

setMargin( ( prev ) => ( {...prev, [direction]: value} ) )} />

{__( 'Reserve space (px)', 'adpresso' )}

setSize( ( prev ) => ( {...prev, width: value} ) )} /> setSize( ( prev ) => ( {...prev, height: value} ) )} />
)}

{__( 'CSS class', 'adpresso' )}

setClassName( value )} />

{__( 'Inline', 'adpresso' )}

); }; export default ShortcodeBuilder;