/** * Forms from other plugins (Contact Form 7, WPForms and the like), under * the Forms list. Each active plugin lists its forms; Connect picks the * email field, the name fields, the box people tick to subscribe and the * public tags. Elementor Pro shows how to add Mailpuff in Elementor. */ import { useEffect, useMemo, useState } from '@wordpress/element'; import { __, sprintf } from '@wordpress/i18n'; import { useUi } from '../../runtime'; import { useConnectPluginForm, useFormChoices, usePluginForms, } from '../../hooks/use-forms'; import { errorText, number } from '../../lib/format'; /** The value meaning "sending the form is the consent". */ export const SUBMIT = 'submit'; /** * The fields a picker offers: the kinds asked for first, then the rest. * * @param {Array} fields The form's fields. * @param {string[]} kinds Field types to put first. * @return {Array} Fields. */ export function ordered( fields = [], kinds = [] ) { const first = fields.filter( ( f ) => kinds.includes( f.type ) ); const rest = fields.filter( ( f ) => ! kinds.includes( f.type ) ); return [ ...first, ...rest ]; } /** * Whether a link can be connected: an email field and a consent. * * @param {Object} link The link. * @return {boolean} Ready. */ export function ready( link ) { return !! link?.email && !! link?.consent; } /** * @param {Object} props * @param {Object} props.form The form, with its fields and link. * @param {Function} props.onClose Called to close. * @return {Element} The dialog. */ function ConnectDialog( { form, onClose } ) { const { ui } = useUi(); const connect = useConnectPluginForm(); const { data: choices, isLoading: loadingTags } = useFormChoices(); const [ link, setLink ] = useState( form.link ); useEffect( () => setLink( form.link ), [ form ] ); const set = ( key ) => ( e ) => setLink( ( l ) => ( { ...l, [ key ]: e.target.value } ) ); const save = ( active ) => connect.mutate( { plugin: form.plugin, id: form.id, ...link, active }, { onSuccess: () => { ui.toast.success( active ? __( 'The form is connected.', 'mailpuff' ) : __( 'The form is disconnected.', 'mailpuff' ) ); onClose(); }, onError: ( err ) => ui.toast.error( errorText( err, __( 'Could not save.', 'mailpuff' ) ) ), } ); // The consent picker offers only boxes; the others offer every field. const select = ( label, key, kinds, none, extra = null, tip = '' ) => ( { ( a11y ) => ( { ( 'consent' === key ? form.fields.filter( ( f ) => kinds.includes( f.type ) ) : ordered( form.fields, kinds ) ).map( ( f ) => ( ) ) } { extra } ) } ); const tags = choices?.tags || []; const picked = link.tags || []; return ( ! next && onClose() } title={ sprintf( /* translators: %s: a form's name. */ __( 'Connect ā€œ%sā€', 'mailpuff' ), form.title ) } description={ __( 'Pick where the email address is and what shows they agreed. Everyone confirms by email first, and nobody else is added.', 'mailpuff' ) } footer={ <> { form.connected && ( save( false ) } loading={ connect.isPending } > { __( 'Disconnect', 'mailpuff' ) } ) } { __( 'Cancel', 'mailpuff' ) } save( true ) } loading={ connect.isPending } disabled={ ! ready( link ) } > { form.connected ? __( 'Save', 'mailpuff' ) : __( 'Connect', 'mailpuff' ) } } >
{ select( __( 'Email address', 'mailpuff' ), 'email', [ 'email' ], __( 'Pick a field', 'mailpuff' ) ) }
{ select( __( 'First name', 'mailpuff' ), 'first_name', [ 'first_name', 'name' ], __( 'None', 'mailpuff' ), null, __( 'A whole name field works too: its first word becomes the first name.', 'mailpuff' ) ) } { select( __( 'Last name', 'mailpuff' ), 'last_name', [ 'last_name' ], __( 'None', 'mailpuff' ) ) }
{ select( __( 'They agree when', 'mailpuff' ), 'consent', [ 'consent', 'checkbox' ], __( 'Pick the box they tick', 'mailpuff' ), ) } { SUBMIT === link.consent && (

{ __( 'Only for a form that clearly asks for an email to send news to. A contact or quote form needs a box to tick.', 'mailpuff' ) }

) } { () => loadingTags || tags.length ? (
{ tags.map( ( t ) => ( setLink( ( l ) => ( { ...l, tags: picked.includes( t.id ) ? picked.filter( ( id ) => id !== t.id ) : [ ...picked, t.id ], } ) ) } /> ) ) }
) : (

{ __( 'No public tags yet. Make a tag public on Subscribers to offer it here.', 'mailpuff' ) }

) }
); } /** * @return {Element|null} The section, or nothing without a form plugin. */ export default function PluginForms() { const { ui } = useUi(); const { data } = usePluginForms(); const [ open, setOpen ] = useState( null ); const plugins = data?.plugins || []; const columns = useMemo( () => [ { key: 'title', header: __( 'Form', 'mailpuff' ), render: ( f ) => ( { f.title } ), }, { key: 'status', header: __( 'Status', 'mailpuff' ), render: ( f ) => ( { f.connected ? __( 'Connected', 'mailpuff' ) : __( 'Not connected', 'mailpuff' ) } ), }, { key: 'signups', header: __( 'Signups, 30 days', 'mailpuff' ), align: 'right', render: ( f ) => number( f.signups?.last_30_days ), }, { key: 'total', header: __( 'In all', 'mailpuff' ), align: 'right', render: ( f ) => number( f.signups?.total ), }, ], // eslint-disable-next-line react-hooks/exhaustive-deps [ ui.Status ] ); if ( ! plugins.length ) { return null; } return (

{ __( 'Forms from other plugins', 'mailpuff' ) }

{ __( 'Connect a form so the people who tick its box are signed up. They confirm by email first.', 'mailpuff' ) }

{ plugins.map( ( p ) => (

{ p.label }

{ p.listable ? ( `${ p.id }:${ f.id }` } onRowClick={ setOpen } emptyTitle={ __( 'No forms yet.', 'mailpuff' ) } /> ) : (

{ p.note }{ ' ' } { p.signups?.total > 0 && sprintf( /* translators: %s: a number of people. */ __( '%s signed up so far.', 'mailpuff' ), number( p.signups.total ) ) }

) }
) ) } { open && ( setOpen( null ) } /> ) }
); }