| 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 |
public function get_name(): string { |
| 12 |
return 'extra-content'; |
| 13 |
} |
| 14 |
|
| 15 |
public function get_label(): string { |
| 16 |
return __( 'Extra Contents', 'templately' ); |
| 17 |
} |
| 18 |
|
| 19 |
public function should_log(): bool { |
| 20 |
return true; |
| 21 |
} |
| 22 |
|
| 23 |
public function get_action(): string { |
| 24 |
return 'eventLog'; |
| 25 |
} |
| 26 |
|
| 27 |
public function log_message(): string { |
| 28 |
return __( 'Importing Forms and Others Extra Contents', 'templately' ); |
| 29 |
} |
| 30 |
|
| 31 |
public function should_run( $data, $imported_data = [] ): bool { |
| 32 |
return ! empty( $this->manifest['extra-content'] ) && !empty($data['import_demo_content']); |
| 33 |
} |
| 34 |
|
| 35 |
public function import( $data, $imported_data ): array { |
| 36 |
$extra_content = []; |
| 37 |
$contents = $this->manifest['extra-content']; |
| 38 |
|
| 39 |
if( ! empty( $contents ) ) { |
| 40 |
foreach( $contents as $type => $content ) { |
| 41 |
switch ( $type ) { |
| 42 |
case 'form': |
| 43 |
$import = $this->import_form( $content ); |
| 44 |
$extra_content['form'] = $import; |
| 45 |
break; |
| 46 |
case 'data': |
| 47 |
break; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// $imported_data = array_merge( $imported_data, [ 'extra-content' => $this->extra_content ] ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
return [ 'extra-content' => $extra_content ]; |
| 56 |
} |
| 57 |
|
| 58 |
private function import_form( $contents ): array { |
| 59 |
if( ! is_array( $contents ) || empty( $contents ) ) { |
| 60 |
return []; |
| 61 |
} |
| 62 |
|
| 63 |
$result = []; |
| 64 |
|
| 65 |
$root_path = $this->dir_path . 'content' . DIRECTORY_SEPARATOR; |
| 66 |
|
| 67 |
foreach( $contents as $json_id => $plugin_list ) { |
| 68 |
if( empty( $plugin_list ) ) { |
| 69 |
continue; |
| 70 |
} |
| 71 |
|
| 72 |
foreach ( $plugin_list as $plugin_name => $form_list ) { |
| 73 |
if( empty( $form_list ) ) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
foreach ( $form_list as $form ) { |
| 78 |
try { |
| 79 |
$file_path = $root_path . wp_unslash( $form['file_path'] ); |
| 80 |
$import = new Form( $plugin_name, $file_path, $form ); |
| 81 |
$data = $import->run(); |
| 82 |
|
| 83 |
$result[$json_id][ $plugin_name ][ $form['id'] ] = $data; |
| 84 |
} catch ( Exception $e ) { |
| 85 |
continue; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return $result; |
| 92 |
} |
| 93 |
} |