| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Core\Importer\Runners; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Templately\Core\Importer\Form; |
| 7 |
use Templately\Core\Importer\Runners\BaseRunner; |
| 8 |
|
| 9 |
class ExtraContent extends BaseRunner { |
| 10 |
|
| 11 |
protected static $imported_form = []; |
| 12 |
|
| 13 |
public function get_name(): string { |
| 14 |
return 'extra-content'; |
| 15 |
} |
| 16 |
|
| 17 |
public function get_label(): string { |
| 18 |
return __( 'Extra Contents', 'templately' ); |
| 19 |
} |
| 20 |
|
| 21 |
public function should_log(): bool { |
| 22 |
return true; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_action(): string { |
| 26 |
return 'eventLog'; |
| 27 |
} |
| 28 |
|
| 29 |
public function log_message(): string { |
| 30 |
return __( 'Importing Forms and Others Extra Contents', 'templately' ); |
| 31 |
} |
| 32 |
|
| 33 |
public function should_run( $data, $imported_data = [] ): bool { |
| 34 |
return ! empty( $this->manifest['extra-content'] ) && !empty($data['import_demo_content']); |
| 35 |
} |
| 36 |
|
| 37 |
public function import( $data, $imported_data ): array { |
| 38 |
$contents = $this->manifest['extra-content'] ?? []; |
| 39 |
|
| 40 |
$extra_content = $this->loop( $contents, function($type, $content ) { |
| 41 |
$extra_content = []; |
| 42 |
switch ( $type ) { |
| 43 |
case 'form': |
| 44 |
$import = $this->import_form( $content ); |
| 45 |
$extra_content['form'] = $import; |
| 46 |
break; |
| 47 |
case 'data': |
| 48 |
break; |
| 49 |
} |
| 50 |
return $extra_content; |
| 51 |
}); |
| 52 |
|
| 53 |
return [ 'extra-content' => $extra_content ]; |
| 54 |
} |
| 55 |
|
| 56 |
private function import_form( $contents ): array { |
| 57 |
if( ! is_array( $contents ) || empty( $contents ) ) { |
| 58 |
return []; |
| 59 |
} |
| 60 |
|
| 61 |
$result = []; |
| 62 |
|
| 63 |
$root_path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR; |
| 64 |
|
| 65 |
foreach( $contents as $json_id => $plugin_list ) { |
| 66 |
if( empty( $plugin_list ) ) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
foreach ( $plugin_list as $plugin_name => $form_list ) { |
| 71 |
if( empty( $form_list ) ) { |
| 72 |
continue; |
| 73 |
} |
| 74 |
|
| 75 |
foreach ( $form_list as $form ) { |
| 76 |
try { |
| 77 |
$form_id = isset( $form['identifier'] ) ? $form['settings'][$form['identifier']] : $form['settings']['form_list']; |
| 78 |
if(isset(self::$imported_form[ $plugin_name ][ $form_id ])){ |
| 79 |
$result[$json_id][ $plugin_name ][ $form['id'] ] = self::$imported_form[ $plugin_name ][ $form_id ]; |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
$file_path = $root_path . wp_unslash( $form['file_path'] ); |
| 84 |
$import = new Form( $plugin_name, $file_path, $form ); |
| 85 |
$data = $import->run(); |
| 86 |
|
| 87 |
self::$imported_form[ $plugin_name ][ $form_id ] = (string) $data; |
| 88 |
$result[$json_id][ $plugin_name ][ $form['id'] ] = (string) $data; |
| 89 |
} catch ( Exception $e ) { |
| 90 |
continue; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
return $result; |
| 97 |
} |
| 98 |
} |