| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\AddonAPI; |
| 4 |
|
| 5 |
use ImportWP\Common\AddonAPI\Exporter\ExporterData; |
| 6 |
use ImportWP\Common\AddonAPI\Exporter\ExporterSchema; |
| 7 |
|
| 8 |
class ExporterAddon extends Addon |
| 9 |
{ |
| 10 |
/** |
| 11 |
* @var ExporterData |
| 12 |
*/ |
| 13 |
private $_exporter_data; |
| 14 |
|
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
parent::__construct(); |
| 18 |
|
| 19 |
$allowed_types = $this->export_types(); |
| 20 |
foreach ($allowed_types as $allowed_type) { |
| 21 |
|
| 22 |
// pass template type to exporter |
| 23 |
add_filter('iwp/exporter/' . $allowed_type . '/fields', function ($fields, $template_args) use ($allowed_type) { |
| 24 |
return $this->exporter_modify_fields($fields, $template_args, $allowed_type); |
| 25 |
}, 10, 2); |
| 26 |
|
| 27 |
// get user custom code at this point before 'iwp/exporter_record/{type} is triggered. |
| 28 |
add_filter('iwp/exporter/' . $allowed_type . '/setup_data', function ($record, $template_args) use ($allowed_type) { |
| 29 |
return $this->exporter_load_data($record, $template_args, $allowed_type); |
| 30 |
}, 10, 2); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
public function exporter_modify_fields($fields, $template_args, $template_type) |
| 35 |
{ |
| 36 |
// capture schema setup |
| 37 |
$schema = new ExporterSchema($template_type, $template_args); |
| 38 |
$this->export_schema($schema); |
| 39 |
|
| 40 |
foreach ($schema->get_groups() as $group) { |
| 41 |
|
| 42 |
$fields['children'][$group->get_id()] = [ |
| 43 |
'key' => $group->get_id(), |
| 44 |
'label' => $group->get_name(), |
| 45 |
'loop' => false, |
| 46 |
'fields' => $group->get_fields(), |
| 47 |
'children' => [] |
| 48 |
]; |
| 49 |
} |
| 50 |
|
| 51 |
return $fields; |
| 52 |
} |
| 53 |
|
| 54 |
public function exporter_load_data($record, $template_args, $template_type) |
| 55 |
{ |
| 56 |
// setup exporter data |
| 57 |
if (is_null($this->_exporter_data)) { |
| 58 |
$this->_exporter_data = new ExporterData($template_type, $template_args); |
| 59 |
} |
| 60 |
|
| 61 |
$this->_exporter_data->load_record($record); |
| 62 |
$this->export_data($this->_exporter_data); |
| 63 |
|
| 64 |
foreach ($this->_exporter_data->get_groups() as $group_id => $group_data) { |
| 65 |
|
| 66 |
if (!isset($record[$group_id])) { |
| 67 |
$record[$group_id] = []; |
| 68 |
} |
| 69 |
|
| 70 |
$data = []; |
| 71 |
foreach ($group_data->get_fields() as $field) { |
| 72 |
|
| 73 |
foreach ($field->get_values() as $sub_field_id => $sub_field_value) { |
| 74 |
$data[$sub_field_id] = $sub_field_value; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
$record[$group_id] = $data; |
| 79 |
} |
| 80 |
|
| 81 |
return $record; |
| 82 |
} |
| 83 |
|
| 84 |
public function export_types() |
| 85 |
{ |
| 86 |
return []; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @param ExporterSchema $exporter |
| 91 |
* @return void |
| 92 |
*/ |
| 93 |
public function export_schema($exporter) |
| 94 |
{ |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* @param ExporterData $exporter |
| 99 |
* @return void |
| 100 |
*/ |
| 101 |
public function export_data($exporter) |
| 102 |
{ |
| 103 |
} |
| 104 |
} |
| 105 |
|