| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\FullSiteImport\Utils; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use FluentForm\App\Services\Transfer\TransferService; |
| 7 |
use FluentForm\Framework\Request\File; |
| 8 |
use Templately\Utils\Helper; |
| 9 |
|
| 10 |
class Form { |
| 11 |
protected $plugin; |
| 12 |
protected $file; |
| 13 |
protected $settings = []; |
| 14 |
|
| 15 |
public function __construct( $plugin, $file, $settings = [] ) { |
| 16 |
$this->plugin = $plugin; |
| 17 |
$this->file = $file; |
| 18 |
$this->settings = $settings; |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @throws Exception |
| 23 |
*/ |
| 24 |
public function run() { |
| 25 |
if ( ! file_exists( $this->file ) ) { |
| 26 |
throw new Exception( __( 'Form JSON does not exists.', 'templately' ) ); |
| 27 |
} |
| 28 |
|
| 29 |
$data = $this->import(); |
| 30 |
|
| 31 |
if ( ! $data ) { |
| 32 |
throw new Exception( __( 'Cannot be imported.', 'templately' ) ); |
| 33 |
} |
| 34 |
|
| 35 |
return $data; |
| 36 |
} |
| 37 |
|
| 38 |
public function import() { |
| 39 |
try { |
| 40 |
$results = null; |
| 41 |
switch ( $this->plugin ) { |
| 42 |
// `finfo` (the PHP `fileinfo` extension) is required: FluentForm's |
| 43 |
// File::__construct() resolves the MIME type via `new finfo()`, which |
| 44 |
// fatals if the extension is missing. Guard it so we skip the form |
| 45 |
// import instead of crashing the whole site import. |
| 46 |
case 'fluent-forms' && is_plugin_active( 'fluentform/fluentform.php' ) && class_exists( File::class ) && class_exists( TransferService::class ) && class_exists( 'finfo' ): |
| 47 |
$fileObject = new File( $this->file, '' ); |
| 48 |
$importer = new TransferService(); |
| 49 |
$inserted = $importer->importForms( $fileObject ); |
| 50 |
$results = key( $inserted['inserted_forms'] ); |
| 51 |
break; |
| 52 |
default: |
| 53 |
break; |
| 54 |
} |
| 55 |
|
| 56 |
return $results; |
| 57 |
|
| 58 |
} catch ( \Throwable $e ) { |
| 59 |
// \Throwable, not \Exception: a fatal \Error raised inside the third-party |
| 60 |
// form importer (a missing class or constant) is NOT an Exception, so the |
| 61 |
// old catch could not contain it and one broken form aborted the entire |
| 62 |
// site import. |
| 63 |
// |
| 64 |
// This swallows the failure by design — a form that cannot be imported must |
| 65 |
// not stop the site import — but silence made every such failure invisible, |
| 66 |
// so it is logged before being dropped. |
| 67 |
Helper::log( |
| 68 |
sprintf( 'Form import failed (%s): %s', $this->plugin, $e->getMessage() ), |
| 69 |
'fsi', |
| 70 |
'error' |
| 71 |
); |
| 72 |
|
| 73 |
return null; |
| 74 |
} |
| 75 |
} |
| 76 |
} |