| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\AddonAPI; |
| 4 |
|
| 5 |
use ImportWP\Common\AddonAPI\Importer\ImporterData; |
| 6 |
use ImportWP\Common\AddonAPI\Importer\ImporterRecordData; |
| 7 |
use ImportWP\Common\AddonAPI\Importer\Template\Template; |
| 8 |
|
| 9 |
class ImporterAddon extends Addon |
| 10 |
{ |
| 11 |
/** |
| 12 |
* @var \ImportWP\Common\Importer\Template\Template |
| 13 |
*/ |
| 14 |
private $_template; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var \ImportWP\Common\Model\ImporterModel |
| 18 |
*/ |
| 19 |
private $_template_importer_model; |
| 20 |
|
| 21 |
/** |
| 22 |
* @var \ImportWP\Common\Importer\Importer |
| 23 |
*/ |
| 24 |
private $_importer; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var \ImportWP\EventHandler |
| 28 |
*/ |
| 29 |
private $event_handler; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var Template |
| 33 |
*/ |
| 34 |
private $_addon_template; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var \ImportWP\Common\AddonAPI\Importer\ImporterRecordData |
| 38 |
*/ |
| 39 |
private $_record_data; |
| 40 |
|
| 41 |
public function __construct() |
| 42 |
{ |
| 43 |
parent::__construct(); |
| 44 |
|
| 45 |
// capture for template registration |
| 46 |
add_action('iwp/register_events', function ($event_handler) { |
| 47 |
|
| 48 |
$this->event_handler = $event_handler; |
| 49 |
|
| 50 |
$this->event_handler->listen('template.fields', function ($fields, $template, $importer_model) { |
| 51 |
|
| 52 |
/** |
| 53 |
* @var array $fields |
| 54 |
* @var \ImportWP\Common\Importer\Template\Template $template |
| 55 |
* @var \ImportWP\Common\Model\ImporterModel $importer_model |
| 56 |
*/ |
| 57 |
|
| 58 |
$this->_template = $template; |
| 59 |
$this->_template_importer_model = $importer_model; |
| 60 |
|
| 61 |
if ($this->init()) { |
| 62 |
$fields = $this->merge_fields($fields); |
| 63 |
} |
| 64 |
|
| 65 |
return $fields; |
| 66 |
}); |
| 67 |
|
| 68 |
// Register custom fields for display |
| 69 |
$this->event_handler->listen('importer.custom_fields.get_fields', function ($fields, $importer_model) { |
| 70 |
|
| 71 |
$this->_template_importer_model = $importer_model; |
| 72 |
if (!$this->init()) { |
| 73 |
return $fields; |
| 74 |
} |
| 75 |
|
| 76 |
$custom_fields = $this->_addon_template->get_custom_fields(); |
| 77 |
foreach ($custom_fields as $custom_fields) { |
| 78 |
$fields = array_merge($custom_fields->get_dropdown_fields(), $fields); |
| 79 |
} |
| 80 |
|
| 81 |
return $fields; |
| 82 |
}); |
| 83 |
|
| 84 |
$this->event_handler->listen('importer_manager.import', function ($importer_model) { |
| 85 |
|
| 86 |
/** |
| 87 |
* @var \ImportWP\Common\Model\ImporterModel $importer_model |
| 88 |
*/ |
| 89 |
$this->_template_importer_model = $importer_model; |
| 90 |
|
| 91 |
/** |
| 92 |
* @var \ImportWP\Common\Importer\ImporterManager $importer_manager |
| 93 |
*/ |
| 94 |
$importer_manager = \ImportWP\Container::getInstance()->get('importer_manager'); |
| 95 |
|
| 96 |
// NOTE: currently this is a string |
| 97 |
$this->_template = $importer_manager->get_template($importer_model->getTemplate()); |
| 98 |
|
| 99 |
$this->init(); |
| 100 |
|
| 101 |
return $importer_model; |
| 102 |
}); |
| 103 |
}, 10); |
| 104 |
|
| 105 |
// capture for import registration |
| 106 |
} |
| 107 |
|
| 108 |
private function merge_fields($field_data) |
| 109 |
{ |
| 110 |
// register panels |
| 111 |
$panels = $this->_addon_template->get_panels(); |
| 112 |
if (!empty($panels)) { |
| 113 |
foreach ($panels as $panel) { |
| 114 |
|
| 115 |
$field_data = array_merge($field_data, [ |
| 116 |
$this->_template->register_group( |
| 117 |
$panel->get_name(), |
| 118 |
$panel->get_id(), |
| 119 |
$panel->get_template_data($this->_template), |
| 120 |
$panel->get_args() |
| 121 |
) |
| 122 |
]); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return $field_data; |
| 127 |
} |
| 128 |
|
| 129 |
final protected function init() |
| 130 |
{ |
| 131 |
// should we continue |
| 132 |
if (!$this->can_run()) { |
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
$this->install_hooks(); |
| 137 |
|
| 138 |
$this->_addon_template = new Template(); |
| 139 |
$this->register($this->_addon_template); |
| 140 |
|
| 141 |
// register permission fields for ui |
| 142 |
add_filter('iwp/template/permission_fields', function ($permission_fields) { |
| 143 |
|
| 144 |
$panels = $this->_addon_template->get_panels(); |
| 145 |
foreach ($panels as $panel) { |
| 146 |
|
| 147 |
$section = $panel->get_name(); |
| 148 |
if (!isset($permission_fields[$section])) { |
| 149 |
$permission_fields[$section] = []; |
| 150 |
} |
| 151 |
|
| 152 |
foreach ($panel->get_fields() as $field) { |
| 153 |
$permission_fields[$section][$panel->get_id() . '.' . $field->get_id()] = $field->get_name(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return $permission_fields; |
| 158 |
}); |
| 159 |
|
| 160 |
$this->event_handler->listen('template.pre_process_groups', function ($groups, $data, $template) { |
| 161 |
|
| 162 |
$new_groups = $this->_addon_template->get_panel_ids(); |
| 163 |
if (!empty($new_groups)) { |
| 164 |
return array_merge((array) $groups, $new_groups); |
| 165 |
} |
| 166 |
|
| 167 |
return $groups; |
| 168 |
}); |
| 169 |
|
| 170 |
/** |
| 171 |
* Change custom field key to acf field key |
| 172 |
*/ |
| 173 |
add_filter('iwp/custom_field_key', function ($key) { |
| 174 |
|
| 175 |
foreach ($this->_addon_template->get_custom_fields() as $custom_fields) { |
| 176 |
|
| 177 |
if (strpos($key, $custom_fields->get_prefix() . "::") === 0) { |
| 178 |
|
| 179 |
$field_key = substr($key, strrpos($key, '::') + strlen('::')); |
| 180 |
if ($field = $custom_fields->get_field($field_key)) { |
| 181 |
return $field['id']; |
| 182 |
} |
| 183 |
|
| 184 |
break; |
| 185 |
} |
| 186 |
} |
| 187 |
return $key; |
| 188 |
}, 10); |
| 189 |
|
| 190 |
return true; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* @param Template $template_data |
| 195 |
* @return Template |
| 196 |
*/ |
| 197 |
public function register($template_data) |
| 198 |
{ |
| 199 |
return $template_data; |
| 200 |
} |
| 201 |
|
| 202 |
protected function install_hooks() |
| 203 |
{ |
| 204 |
// skip row |
| 205 |
add_filter('iwp/importer/skip_record', function ($result, $data, $importer) { |
| 206 |
|
| 207 |
// this is the earliest point we have access to the importer |
| 208 |
$this->_importer = $importer; |
| 209 |
|
| 210 |
return $this->filter_row($result, $data); |
| 211 |
}, 10, 3); |
| 212 |
|
| 213 |
// before import |
| 214 |
add_action('iwp/importer/init', function () { |
| 215 |
$this->before_import(); |
| 216 |
}); |
| 217 |
|
| 218 |
// after import |
| 219 |
$this->event_handler->listen('importer_manager.import_shutdown', function ($importer_model) { |
| 220 |
$state = \ImportWP\Common\Importer\State\ImporterState::get_state($importer_model->getId()); |
| 221 |
if ($state['status'] != 'complete') { |
| 222 |
return $importer_model; |
| 223 |
} |
| 224 |
|
| 225 |
$this->after_import(); |
| 226 |
|
| 227 |
return $importer_model; |
| 228 |
}); |
| 229 |
|
| 230 |
// before row |
| 231 |
add_action('iwp/importer/before_row', function ($data) { |
| 232 |
|
| 233 |
/** |
| 234 |
* @var \ImportWP\Common\Importer\ParsedData $data |
| 235 |
*/ |
| 236 |
$record_data = new ImporterRecordData($data, $this->_addon_template, $this->_importer); |
| 237 |
|
| 238 |
$this->before_row($record_data); |
| 239 |
}); |
| 240 |
|
| 241 |
// after row |
| 242 |
add_action('iwp/importer/after_row', [$this, 'after_row']); |
| 243 |
|
| 244 |
// save |
| 245 |
$this->event_handler->listen('template.process', function ($id, $data, $importer_model, $template) { |
| 246 |
|
| 247 |
/** |
| 248 |
* @var integer $id |
| 249 |
* @var \ImportWP\Common\Importer\ParsedData $data |
| 250 |
* @var \ImportWP\Common\Model\ImporterModel $importer_model |
| 251 |
* @var \ImportWP\Common\Importer\Template\Template $template |
| 252 |
*/ |
| 253 |
|
| 254 |
$addon_data = new ImporterData($id, $data, $this->_addon_template, $this->_importer, $template); |
| 255 |
$this->save($addon_data); |
| 256 |
|
| 257 |
add_filter('iwp/custom_fields/log_message', function ($message) use ($addon_data) { |
| 258 |
|
| 259 |
$logs = $addon_data->get_logs(); |
| 260 |
foreach ($logs as $group_id => $field_ids) { |
| 261 |
$message .= ', ' . $group_id . ' (' . implode(', ', $field_ids) . ')'; |
| 262 |
} |
| 263 |
|
| 264 |
return $message; |
| 265 |
}); |
| 266 |
|
| 267 |
return $id; |
| 268 |
}); |
| 269 |
} |
| 270 |
|
| 271 |
public function before_import() |
| 272 |
{ |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* @param bool $result |
| 277 |
* @param \ImportWP\Common\Importer\ParsedData $data |
| 278 |
* @param \ImportWP\Common\Importer\Importer $importer |
| 279 |
* @return bool |
| 280 |
*/ |
| 281 |
public function filter_row($result, $data) |
| 282 |
{ |
| 283 |
return $result; |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* @param \ImportWP\Common\AddonAPI\Importer\ImporterRecordData $record |
| 288 |
* @return void |
| 289 |
*/ |
| 290 |
public function before_row($record) |
| 291 |
{ |
| 292 |
} |
| 293 |
|
| 294 |
/** |
| 295 |
* @param ImporterData $data |
| 296 |
* @return void |
| 297 |
*/ |
| 298 |
public function save($data) |
| 299 |
{ |
| 300 |
} |
| 301 |
|
| 302 |
public function after_row() |
| 303 |
{ |
| 304 |
} |
| 305 |
|
| 306 |
public function after_import() |
| 307 |
{ |
| 308 |
} |
| 309 |
|
| 310 |
protected function get_importer() |
| 311 |
{ |
| 312 |
if (!is_null($this->_template_importer_model)) { |
| 313 |
return $this->_template_importer_model; |
| 314 |
} |
| 315 |
|
| 316 |
if (!is_null(iwp()->importer)) { |
| 317 |
return iwp()->importer; |
| 318 |
} |
| 319 |
|
| 320 |
return false; |
| 321 |
} |
| 322 |
|
| 323 |
protected function get_importer_id() |
| 324 |
{ |
| 325 |
$importer = $this->get_importer(); |
| 326 |
if (!$importer) { |
| 327 |
return false; |
| 328 |
} |
| 329 |
|
| 330 |
return $importer->getId(); |
| 331 |
} |
| 332 |
|
| 333 |
protected function get_template_id() |
| 334 |
{ |
| 335 |
$importer = $this->get_importer(); |
| 336 |
if (!$importer) { |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
return $importer->getTemplate(); |
| 341 |
} |
| 342 |
|
| 343 |
protected function get_parser_id() |
| 344 |
{ |
| 345 |
$importer = $this->get_importer(); |
| 346 |
if (!$importer) { |
| 347 |
return false; |
| 348 |
} |
| 349 |
|
| 350 |
return $importer->getParser(); |
| 351 |
} |
| 352 |
|
| 353 |
protected function get_mapper_id() |
| 354 |
{ |
| 355 |
if (is_null($this->_template)) { |
| 356 |
return false; |
| 357 |
} |
| 358 |
|
| 359 |
return $this->_template->get_mapper(); |
| 360 |
} |
| 361 |
|
| 362 |
protected function get_parser() |
| 363 |
{ |
| 364 |
if (is_null($this->_importer)) { |
| 365 |
return false; |
| 366 |
} |
| 367 |
|
| 368 |
return $this->_importer->getParser(); |
| 369 |
} |
| 370 |
|
| 371 |
protected function get_mapper() |
| 372 |
{ |
| 373 |
if (is_null($this->_importer)) { |
| 374 |
return false; |
| 375 |
} |
| 376 |
|
| 377 |
return $this->_importer->getMapper(); |
| 378 |
} |
| 379 |
|
| 380 |
protected function can_run() |
| 381 |
{ |
| 382 |
return true; |
| 383 |
} |
| 384 |
} |
| 385 |
|