/** * The two import pages. Import from other plugins (Funnelforms → Import from other plugins): forms * of other form plugins found on this website, or an export file, become drafts. Import * Funnelforms (Funnelforms → Import Funnelforms): export files of Funnelforms 5 and 3 become * drafts. Each imported form is listed with what was not taken over, in plain words, and a link to * open it in the editor. */ import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import type { ImportedEntries, ImportedForm } from '../api'; import { IMPORT_MAX_BYTES, type ConnectApi, type ImportItem, type ImportResult, type ImportSource } from '../connect'; import { problemTexts } from './catalog'; import { __, sprintf } from './i18n'; import { Spinner } from './ui'; export interface ImportConfig { /** The editor, to open a form: its address plus &form=. */ builderUrl?: string | null; /** The entries, to show those of a form: its address plus &form=; null without the right to see them. */ entriesUrl?: string | null; /** Size limit of an export file in bytes. */ maxBytes?: number; /** The other import page: Import from other plugins … */ importUrl?: string; /** … and Import Funnelforms. */ importOwn?: string; } /** Names of the form plugins; brand names, so they are not translated. */ const PLUGINS: Record = { wpforms: 'WPForms', gravityforms: 'Gravity Forms', 'ninja-forms': 'Ninja Forms', 'fluent-forms': 'Fluent Forms', 'contact-form-7': 'Contact Form 7', formidable: 'Formidable Forms', elementor: 'Elementor Pro', }; /** A line of the result: an item and the name of the plugin it came from. */ interface Row { item: ImportItem; plugin: string; } function message( e: unknown ): string { return e instanceof Error ? e.message : String( e ); } function megabytes( bytes: number ): string { return String( Math.round( bytes / ( 1024 * 1024 ) ) ); } const key = ( source: string, id: string ): string => source + '|' + id; function Icon( { path }: { path: string } ) { return ( ); } export function ImportScreen( { api, config = {} }: { api: ConnectApi; config?: ImportConfig } ) { const [ sources, setSources ] = useState( null ); const [ loadError, setLoadError ] = useState( null ); const [ selected, setSelected ] = useState>( new Set() ); const [ again, setAgain ] = useState( false ); const [ busy, setBusy ] = useState( false ); const [ rows, setRows ] = useState( null ); const [ errors, setErrors ] = useState( [] ); const [ dragging, setDragging ] = useState( false ); const picker = useRef( null ); const resultHead = useRef( null ); const maxBytes = config.maxBytes ?? IMPORT_MAX_BYTES; const load = useCallback( async (): Promise => { setLoadError( null ); try { setSources( await api.importSources() ); } catch ( e ) { setLoadError( message( e ) ); } }, [ api ] ); useEffect( () => { void load(); }, [ load ] ); useEffect( () => { if ( rows || errors.length ) { resultHead.current?.focus(); } }, [ rows, errors ] ); const base = config.builderUrl ?? ''; const editUrl = ( id: number ): string => base + ( base.includes( '?' ) ? '&' : '?' ) + 'form=' + id; const chosen = useMemo( () => [ ...selected ], [ selected ] ); const finish = ( found: Row[], failed: string[] ): void => { setRows( found ); setErrors( failed ); setSelected( new Set() ); setBusy( false ); void load(); }; const importSelected = async ( list: string[] = chosen, repeat = again ): Promise => { setBusy( true ); const found: Row[] = []; const failed: string[] = []; const bySource = new Map(); for ( const entry of list ) { const [ source, ...rest ] = entry.split( '|' ); bySource.set( source ?? '', [ ...( bySource.get( source ?? '' ) ?? [] ), rest.join( '|' ) ] ); } for ( const [ source, ids ] of bySource ) { try { const result: ImportResult = await api.importSite( source, ids, repeat ); found.push( ...result.forms.map( ( item ) => ( { item, plugin: PLUGINS[ source ] ?? source } ) ) ); } catch ( e ) { failed.push( message( e ) ); } } finish( found, failed ); }; const importFiles = async ( files: File[] ): Promise => { setBusy( true ); const found: Row[] = []; const failed: string[] = []; for ( const file of files ) { if ( file.size > maxBytes ) { failed.push( sprintf( __( '%1$s is larger than %2$s MB.', 'funnelforms-pro' ), file.name, megabytes( maxBytes ) ) ); continue; } try { const result = await api.importFile( file, file.name ); found.push( ...result.forms.map( ( item ) => ( { item, plugin: PLUGINS[ result.source ] ?? result.source } ) ) ); } catch ( e ) { failed.push( sprintf( __( '%1$s: %2$s', 'funnelforms-pro' ), file.name, message( e ) ) ); } } finish( found, failed ); }; const toggle = ( id: string ): void => setSelected( ( set ) => { const next = new Set( set ); if ( next.has( id ) ) { next.delete( id ); } else { next.add( id ); } return next; } ); const selectable = ( source: ImportSource ): string[] => source.forms.filter( ( f ) => again || null === f.importedAs ).map( ( f ) => key( source.source, f.id ) ); const toggleAll = ( source: ImportSource ): void => { const all = selectable( source ); const every = all.length > 0 && all.every( ( id ) => selected.has( id ) ); setSelected( ( set ) => { const next = new Set( set ); all.forEach( ( id ) => ( every ? next.delete( id ) : next.add( id ) ) ); return next; } ); }; const anyImported = ( sources ?? [] ).some( ( s ) => s.forms.some( ( f ) => null !== f.importedAs ) ); const created = ( rows ?? [] ).filter( ( r ) => ! r.item.skipped ); const skipped = ( rows ?? [] ).filter( ( r ) => r.item.skipped ); return (
{ rows || errors.length ? (

{ __( 'Result', 'funnelforms-pro' ) }

{ 0 === created.length ? __( 'No new form was created.', 'funnelforms-pro' ) : 1 === created.length ? __( '1 form was imported as a draft. Nothing is live yet: open it, check it and publish it.', 'funnelforms-pro' ) : sprintf( __( '%d forms were imported as drafts. Nothing is live yet: open each one, check it and publish it.', 'funnelforms-pro' ), created.length ) }

{ errors.map( ( text, i ) => (

{ text }

) ) }
    { created.map( ( { item, plugin } ) => { const todo = problemTexts( item.problems ?? [] ); const notes = item.messages ?? []; return (
  • { item.title } { plugin }
    { __( 'Draft created', 'funnelforms-pro' ) } { null !== item.id ? ( { __( 'Open in the editor', 'funnelforms-pro' ) } { ' – ' + ( item.title ?? '' ) } ) : null }
    { 0 === notes.length && 0 === todo.length ?

    { __( 'Everything was taken over.', 'funnelforms-pro' ) }

    : null } { notes.length > 0 ? (

    { __( 'Not taken over, or changed', 'funnelforms-pro' ) }

      { notes.map( ( n ) => (
    • { n }
    • ) ) }
    ) : null } { todo.length > 0 ? (

    { __( 'To do before you can publish', 'funnelforms-pro' ) }

      { todo.map( ( n ) => (
    • { n }
    • ) ) }
    ) : null }
  • ); } ) } { skipped.map( ( { item, plugin } ) => (
  • { sprintf( __( 'Form %s', 'funnelforms-pro' ), item.sourceId ) } { plugin }
    { __( 'Skipped', 'funnelforms-pro' ) } { 'already_imported' === item.skipped && null !== item.id ? ( <> { __( 'Open the form', 'funnelforms-pro' ) } ) : null }

    { 'already_imported' === item.skipped ? __( 'This form was imported before and the draft still exists.', 'funnelforms-pro' ) : null } { 'not_found' === item.skipped ? __( 'This form is no longer there.', 'funnelforms-pro' ) : null } { 'document_too_large' === item.skipped ? __( 'This form is too large to import.', 'funnelforms-pro' ) : null }

  • ) ) }
) : null }

{ __( 'Forms on this website', 'funnelforms-pro' ) }

{ __( 'Form plugins whose forms Funnelforms found here. They are copied; the other plugin and its entries stay as they are.', 'funnelforms-pro' ) }

{ loadError ? (

{ loadError }

) : null } { ! loadError && ! sources ? : null } { sources && 0 === sources.length ? (

{ __( 'No forms of other plugins found', 'funnelforms-pro' ) }

{ __( 'Funnelforms looked for WPForms, Gravity Forms, Ninja Forms, Fluent Forms, Contact Form 7, Formidable Forms and Elementor Pro. Upload an export file instead.', 'funnelforms-pro' ) }

) : null } { sources?.map( ( source ) => { const all = selectable( source ); const every = all.length > 0 && all.every( ( id ) => selected.has( id ) ); return (
{ source.label } { 1 === source.forms.length ? __( '1 form', 'funnelforms-pro' ) : sprintf( __( '%d forms', 'funnelforms-pro' ), source.forms.length ) }
    { source.forms.map( ( form ) => { const id = key( source.source, form.id ); const done = null !== form.importedAs; return (
  • { done && null !== form.importedAs ? ( { __( 'Imported', 'funnelforms-pro' ) } { __( 'Open the form', 'funnelforms-pro' ) } { ' – ' + form.title } ) : null }
  • ); } ) }
); } ) } { sources && sources.length > 0 ? (
{ anyImported ? ( ) : ( ) }
) : null }

{ __( 'Upload an export file', 'funnelforms-pro' ) }

{ __( 'Export the forms in the other plugin and upload the file here.', 'funnelforms-pro' ) }

{ config.importOwn ? (

{ __( 'A file exported from Funnelforms?', 'funnelforms-pro' ) }{ ' ' } { __( 'Import Funnelforms', 'funnelforms-pro' ) }

) : null }
{ e.preventDefault(); setDragging( true ); } } onDragLeave={ () => setDragging( false ) } onDrop={ ( e ) => { e.preventDefault(); setDragging( false ); const files = [ ...e.dataTransfer.files ]; if ( files.length && ! busy ) { void importFiles( files ); } } } >

{ __( 'Drop the file here', 'funnelforms-pro' ) }

{ __( 'or', 'funnelforms-pro' ) }

{ const files = [ ...( e.target.files ?? [] ) ]; e.target.value = ''; if ( files.length ) { void importFiles( files ); } } } />

{ sprintf( __( 'Up to %s MB per file.', 'funnelforms-pro' ), megabytes( maxBytes ) ) }

{ __( 'Where do I get the file?', 'funnelforms-pro' ) }
  • { __( 'WPForms: Tools → Export, JSON', 'funnelforms-pro' ) }
  • { __( 'Gravity Forms: Forms → Import/Export, JSON', 'funnelforms-pro' ) }
  • { __( 'Ninja Forms: Import/Export, .nff file', 'funnelforms-pro' ) }
  • { __( 'Fluent Forms: Tools → Export Forms, JSON', 'funnelforms-pro' ) }
  • { __( 'Contact Form 7: WordPress export (XML) of the forms', 'funnelforms-pro' ) }
  • { __( 'Formidable Forms: Import/Export, XML', 'funnelforms-pro' ) }
  • { __( 'Elementor Pro: template export, JSON', 'funnelforms-pro' ) }
); } /** A file that could not be imported, or a form of it that could not be converted. */ interface OwnFailure { file: string; text: string; } /** * Import Funnelforms: export files of Funnelforms (a form file of 5.0, .json; an export of * Funnelforms 4, the online version, .json; an export of Funnelforms 3, .zip) through * admin/forms/import. Each form becomes a draft. */ /** Whether a file is a leads export of Funnelforms 4 (format "funnelforms4-responses"). */ async function isLeadsFile( file: File ): Promise { try { return ( await file.slice( 0, 512 ).text() ).includes( '"funnelforms4-responses"' ); } catch { return false; } } /** The result of a leads file for one form. */ function LeadsRow( { form, entries, entriesUrl }: { form: ImportedForm; entries: ImportedEntries; entriesUrl?: string | null } ) { const notes = [ entries.skipped ? sprintf( __( 'Imported before and skipped: %d', 'funnelforms-pro' ), entries.skipped ) : '', entries.failed ? sprintf( __( 'Could not be saved: %d', 'funnelforms-pro' ), entries.failed ) : '', entries.files ? sprintf( __( 'Files copied: %d', 'funnelforms-pro' ), entries.files ) : '', entries.filesLeft ? sprintf( __( 'Files still in Funnelforms 4: %d. Import the same file again to copy them.', 'funnelforms-pro' ), entries.filesLeft ) : '', ].filter( Boolean ); return (
  • { form.title } { __( 'Leads of Funnelforms 4', 'funnelforms-pro' ) }
    { sprintf( __( 'Leads imported: %d', 'funnelforms-pro' ), entries.imported ) } { entriesUrl && null !== form.id ? ( { ' – ' + ( form.title ?? '' ) } ) : null }
    { notes.length > 0 ? (
      { notes.map( ( n ) => (
    • { n }
    • ) ) }
    ) : null }
  • ); } export function FunnelformsImport( { api, config = {} }: { api: { importFile( file: Blob, name: string ): Promise }; config?: ImportConfig } ) { const [ busy, setBusy ] = useState( false ); const [ forms, setForms ] = useState( null ); const [ failures, setFailures ] = useState( [] ); const [ dragging, setDragging ] = useState( false ); const picker = useRef( null ); const resultHead = useRef( null ); const maxBytes = config.maxBytes ?? IMPORT_MAX_BYTES; const base = config.builderUrl ?? ''; const editUrl = ( id: number ): string => base + ( base.includes( '?' ) ? '&' : '?' ) + 'form=' + id; const newForms = ( forms ?? [] ).filter( ( f ) => ! f.entries ); useEffect( () => { if ( forms || failures.length ) { resultHead.current?.focus(); } }, [ forms, failures ] ); const importFiles = async ( picked: File[] ): Promise => { setBusy( true ); const done: ImportedForm[] = []; const failed: OwnFailure[] = []; // Leads of Funnelforms 4 go into forms imported before: their files come after the forms files. const leads = await Promise.all( picked.map( ( file ) => isLeadsFile( file ) ) ); const files = [ ...picked.filter( ( _, i ) => ! leads[ i ] ), ...picked.filter( ( _, i ) => leads[ i ] ) ]; for ( const file of files ) { if ( file.size > maxBytes ) { failed.push( { file: file.name, text: sprintf( __( '%1$s is larger than %2$s MB.', 'funnelforms-pro' ), file.name, megabytes( maxBytes ) ) } ); continue; } try { for ( const form of await api.importFile( file, file.name ) ) { if ( form.errors?.includes( 'ff4_form_missing' ) ) { failed.push( { file: file.name, text: sprintf( /* translators: title of the form in Funnelforms 4, or the file name */ __( '%s: the form of these leads is not imported yet. Import the forms file of Funnelforms 4 first, then this file again.', 'funnelforms-pro' ), form.title || file.name ) } ); } else if ( null === form.id ) { failed.push( { file: file.name, text: sprintf( /* translators: 1: file name, 2: reason */ __( '%1$s: a form could not be converted (%2$s).', 'funnelforms-pro' ), file.name, ( form.errors ?? [] ).join( ', ' ) || '?' ) } ); } else { done.push( form ); } } } catch ( e ) { failed.push( { file: file.name, text: sprintf( __( '%1$s: %2$s', 'funnelforms-pro' ), file.name, message( e ) ) } ); } } setForms( done ); setFailures( failed ); setBusy( false ); }; return (
    { forms || failures.length ? (

    { __( 'Result', 'funnelforms-pro' ) }

    { ! newForms.length ? __( 'No new form was created.', 'funnelforms-pro' ) : 1 === newForms.length ? __( '1 form was imported as a draft. Nothing is live yet: open it, check it and publish it.', 'funnelforms-pro' ) : sprintf( __( '%d forms were imported as drafts. Nothing is live yet: open each one, check it and publish it.', 'funnelforms-pro' ), newForms.length ) } { newForms.length < ( forms?.length ?? 0 ) ? ' ' + __( 'The leads were imported. Imported leads send no e-mails or webhooks.', 'funnelforms-pro' ) : null }

    { failures.map( ( f, i ) => (

    { f.text }

    ) ) }
      { ( forms ?? [] ).map( ( form ) => { if ( form.entries ) { return ; } const todo = problemTexts( form.problems ?? [] ); const notes = form.messages ?? form.notes ?? []; return (
    • { form.title } { form.legacyId ? __( 'Funnelforms 3', 'funnelforms-pro' ) : 'ff4' === form.source ? __( 'Funnelforms 4', 'funnelforms-pro' ) : 'Funnelforms' }
      { __( 'Draft created', 'funnelforms-pro' ) } { null !== form.id ? ( { __( 'Open in the editor', 'funnelforms-pro' ) } { ' – ' + ( form.title ?? '' ) } ) : null }
      { 0 === notes.length && 0 === todo.length ?

      { __( 'Everything was taken over.', 'funnelforms-pro' ) }

      : null } { notes.length > 0 ? (

      { __( 'Not taken over, or changed', 'funnelforms-pro' ) }

        { notes.map( ( n ) => (
      • { n }
      • ) ) }
      ) : null } { todo.length > 0 ? (

      { __( 'To do before you can publish', 'funnelforms-pro' ) }

        { todo.map( ( n ) => (
      • { n }
      • ) ) }
      ) : null }
    • ); } ) }
    ) : null }

    { __( 'Upload a Funnelforms file', 'funnelforms-pro' ) }

    { __( 'Several files at once are fine. Forms of other plugins have their own import.', 'funnelforms-pro' ) }

    { e.preventDefault(); setDragging( true ); } } onDragLeave={ () => setDragging( false ) } onDrop={ ( e ) => { e.preventDefault(); setDragging( false ); const files = [ ...e.dataTransfer.files ]; if ( files.length && ! busy ) { void importFiles( files ); } } } >

    { __( 'Drop the file here', 'funnelforms-pro' ) }

    { __( 'or', 'funnelforms-pro' ) }

    { const files = [ ...( e.target.files ?? [] ) ]; e.target.value = ''; if ( files.length ) { void importFiles( files ); } } } />

    { sprintf( /* translators: %s: largest file size in MB */ __( '.json or .zip, up to %s MB per file.', 'funnelforms-pro' ), megabytes( maxBytes ) ) }

    { __( 'Which files?', 'funnelforms-pro' ) }

    • { __( 'Funnelforms 5', 'funnelforms-pro' ) } { __( 'A form file (.json): under Forms, the export button of a form downloads it.', 'funnelforms-pro' ) }
    • { __( 'Funnelforms 4', 'funnelforms-pro' ) } { __( 'The export of the online version (.json), with logic, contact form, thank-you page, e-mails, webhooks and design. Entries stay in Funnelforms 4.', 'funnelforms-pro' ) }
    • { __( 'Funnelforms 3', 'funnelforms-pro' ) } { __( 'The export of the forms (.zip) from the import and export of Funnelforms 3, with questions and contact forms. Calculations and connections (CRM, SMTP) are not part of it.', 'funnelforms-pro' ) }
    { config.importUrl ? (

    { __( 'Forms of Contact Form 7, WPForms, Gravity Forms or another form plugin?', 'funnelforms-pro' ) }{ ' ' } { __( 'Import from other plugins', 'funnelforms-pro' ) }

    ) : null }
    ); }