PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.5
Import WP – CSV & XML Import Export for WordPress v2.7.5
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Addon / AddonBase.php

AddonBase.php in Import WP – CSV & XML Import Export for WordPress 2.7.5, at class/Common/Addon/AddonBase.php

516 lines 14.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImportWP\Common\Addon;
4
5 use ImportWP\Common\Importer\ParsedData;
6 use ImportWP\Common\Importer\Template\Template;
7 use ImportWP\Common\Model\ImporterModel;
8 use ImportWP\Container;
9
10 class AddonBase implements AddonInterface
11 {
12 /**
13 * @var callable
14 */
15 protected $_run_conditions;
16
17 /**
18 * @var AddonBasePanel[]
19 */
20 protected $_sections = [];
21
22 protected $name;
23
24 protected $id;
25
26 protected $_meta = [];
27
28 /**
29 * @var string
30 */
31 protected $_process_mapper;
32
33 protected $_register_panels_callback;
34
35 /**
36 * @var AddonCustomFieldsApi[] $_custom_fields
37 */
38 protected $_custom_fields = [];
39
40 protected $_init_callback;
41
42 /**
43 * @var ImporterModel
44 */
45 protected $_importer_model;
46
47 /**
48 * @var Template
49 */
50 protected $_template;
51
52 protected $_migrations = [];
53
54 protected $_migrations_callback;
55
56 protected $_setup = false;
57
58 /**
59 * @var \ImportWP\ServiceProvider
60 */
61 protected $_service_provider;
62
63 public function __construct($name, $id, $callback)
64 {
65 $this->name = $name;
66 $this->id = $id;
67 $this->_init_callback = $callback;
68
69 add_action('iwp/register_events', function ($event_handler, $service_provider) {
70
71 $this->_service_provider = $service_provider;
72
73 // template
74 $event_handler->listen('template.fields', [$this, '_register_template_fields']);
75 $event_handler->listen('template.pre_process_groups', [$this, '_data_groups']);
76 $event_handler->listen('template.pre_process', [$this, '_pre_process']);
77 $event_handler->listen('template.process', [$this, '_process']);
78
79 // custom fields
80 $event_handler->listen('importer.custom_fields.init', [$this, '_custom_fields_init']);
81 $event_handler->listen('importer.custom_fields.get_fields', [$this, '_custom_fields_get_fields']);
82 $event_handler->listen('importer.custom_fields.process_field', [$this, '_custom_fields_process_field']);
83 }, 10, 2);
84
85 add_action('iwp/importer/shutdown', function () {
86
87 foreach ($this->_sections as $section) {
88 $section->clear();
89 }
90 });
91 }
92
93 public function get_service_provider($prop)
94 {
95 if (property_exists($this->_service_provider, $prop)) {
96 return $this->_service_provider->{$prop};
97 }
98
99 return false;
100 }
101
102 public function get_name()
103 {
104 return $this->name;
105 }
106
107 public function get_id()
108 {
109 return $this->id;
110 }
111
112 /**
113 * Register template fields
114 *
115 * @param array $fields
116 * @param Template $template
117 * @param ImporterModel $importer_model
118 * @return array
119 */
120 public function _register_template_fields($fields, $template, $importer_model)
121 {
122 $this->_try_init($importer_model, $template);
123
124 if (!$this->_can_run($template, $importer_model)) {
125 return $fields;
126 }
127
128 $this->_setup_panels($importer_model);
129
130 // TODO: Add field modifiers
131 if (!empty($this->_sections)) {
132 foreach ($this->_sections as $section_id => $section) {
133
134 $section->_register($importer_model);
135
136 $section_data = $section->data();
137
138 if (isset($section_data['settings']['maybe_run']) && is_callable($section_data['settings']['maybe_run']) && !call_user_func_array($section_data['settings']['maybe_run'], [$template, $importer_model])) {
139 continue;
140 }
141
142 $group_fields = $section->fields();
143 $group_fields = $this->_register_fields($group_fields, $template, $importer_model);
144
145
146 $fields = array_merge($fields, [
147 $template->register_group($section_data['name'], $section_id, (array)$group_fields, $section->settings())
148 ]);
149 }
150 }
151
152 return $fields;
153 }
154
155 public function _can_run($template, $importer_model)
156 {
157 if (!is_null($this->_run_conditions) && is_callable($this->_run_conditions)) {
158 return call_user_func_array($this->_run_conditions, [$template, $importer_model]);
159 }
160
161 return true;
162 }
163
164 /**
165 * @param AddonBaseField[]|AddonBaseGroup[] $group_fields
166 * @param Template $template
167 * @param ImporterModel $importer_model
168 *
169 * @return []
170 */
171 public function _register_fields($group_fields, $template, $importer_model)
172 {
173 // array_values, reindexes array from zero
174 $group_fields = array_values(array_filter($group_fields, function ($item) use ($template, $importer_model) {
175 /**
176 * @var AddonBaseField $item
177 */
178 return $item->_is_allowed($template, $importer_model);
179 }));
180
181 $group_fields = array_map(function ($item) use ($template, $importer_model) {
182
183 /**
184 * @var AddonBaseField $item
185 */
186
187 $field_data = $item->data();
188 switch ($field_data['type']) {
189 case 'group':
190 /**
191 * @var AddonBaseGroup $item
192 */
193 return $template->register_group($field_data['name'], $field_data['id'], $this->_register_fields($item->fields(), $template, $importer_model));
194 break;
195 case 'attachment':
196 return $template->register_attachment_fields($field_data['name'], $field_data['id'], $field_data['field_label'], $field_data['settings']);
197 break;
198 default:
199 return $template->register_field($field_data['name'], $field_data['id'], $field_data['settings']);
200 break;
201 }
202 }, $group_fields);
203
204 return $group_fields;
205 }
206
207 /**
208 * @param string[] $groups
209 * @param ParsedData $data
210 * @param Template $template
211 *
212 * @return void
213 */
214 public function _data_groups($groups, $data, $template)
215 {
216 $importer_model = $template->get_importer();
217 $this->_try_init($importer_model, $template);
218
219 if (empty($this->_sections)) {
220 return $groups;
221 }
222 return array_merge((array) $groups, array_keys($this->_sections));
223 }
224
225 /**
226 * @param ParsedData $data
227 * @param ImporterModel $importer_model
228 * @param Template $template
229 *
230 * @return ParsedData
231 */
232 public function _pre_process($data, $importer_model, $template)
233 {
234 $this->_try_init($importer_model, $template);
235
236 $this->_setup_panels($importer_model);
237
238 // check to see what fields are enabled
239 foreach ($this->_sections as $section_id => $section) {
240
241 $section->_register($importer_model);
242 $output = $section->_pre_process($data, $importer_model);
243 $data->replace($output, $section_id);
244 }
245
246 return $data;
247 }
248
249 public function update_meta($object_id, $key, $value, $is_unique = true)
250 {
251 switch ($this->_process_mapper) {
252 case 'user':
253 if (!$is_unique) {
254 add_user_meta($object_id, $key, $value);
255 } else {
256 delete_user_meta($object_id, $key);
257 update_user_meta($object_id, $key, $value);
258 }
259 break;
260 // TODO: Addon mappers should not be listed here replace with filter?
261 case 'woocommerce-product':
262 case 'post':
263 if (!$is_unique) {
264 add_post_meta($object_id, $key, $value);
265 } else {
266 delete_post_meta($object_id, $key);
267 update_post_meta($object_id, $key, $value);
268 }
269 break;
270 case 'term':
271 if (!$is_unique) {
272 add_term_meta($object_id, $key, $value);
273 } else {
274 delete_term_meta($object_id, $key);
275 update_term_meta($object_id, $key, $value);
276 }
277 break;
278 }
279 }
280
281 /**
282 * @param integer $id
283 * @param \ImportWP\Common\Importer\ParsedData $data
284 * @param \ImportWP\Common\Model\ImporterModel $importer_model
285 * @param \ImportWP\Common\Importer\Template\Template $template
286 * @return void
287 */
288 public function _process($id, $data, $importer_model, $template)
289 {
290 // setup
291 $this->_process_mapper = $template->get_mapper();
292 $this->clear_meta();
293
294 foreach ($this->_sections as $section) {
295
296 $section->_process($id, $data, $importer_model, $template);
297 }
298
299 // teardown
300 $this->_process_mapper = null;
301
302 return $id;
303 }
304
305
306
307 /**
308 * When should the addon run?
309 *
310 * @param callable $callback
311 * @return bool
312 */
313 public function enabled($callback)
314 {
315 $this->_run_conditions = $callback;
316 }
317
318 public function register_panel($section_name, $section_id, $callback, $settings = [])
319 {
320 $this->_sections[$section_id] = new AddonBasePanel($this, $callback, $section_id, [
321 'name' => $section_name,
322 'settings' => $settings
323 ]);
324 }
325
326 public function register_panels($callback)
327 {
328 $this->_register_panels_callback = $callback;
329 }
330
331 public function _setup_panels($importer_model)
332 {
333 if (!is_null($this->_register_panels_callback) && is_callable($this->_register_panels_callback)) {
334 call_user_func($this->_register_panels_callback, $importer_model);
335 }
336 }
337
338 public function store_meta($section_id, $id, $key, $value, $i = false)
339 {
340 if (!isset($this->_meta[$section_id])) {
341 $this->_meta[$section_id] = [];
342 }
343
344 if ($i === false) {
345
346 $this->_meta[$section_id][$key] = [
347 'id' => $id,
348 'key' => $key,
349 'value' => $value
350 ];
351 } else {
352
353 if (!isset($this->_meta[$section_id][$key])) {
354 $this->_meta[$section_id][$key] = [
355 'id' => $id,
356 'key' => $key,
357 'value' => []
358 ];
359 }
360
361 $this->_meta[$section_id][$key]['value'][$i] = $value;
362 }
363 }
364
365 public function get_meta($section_id)
366 {
367 return isset($this->_meta[$section_id]) ? $this->_meta[$section_id] : [];
368 }
369
370 public function clear_meta()
371 {
372 $this->_meta = [];
373 }
374
375 public function register_custom_fields($name, $callback)
376 {
377 $api = new AddonCustomFieldsApi($name, $callback);
378 $this->_custom_fields[] = $api;
379
380 if ($this->_setup) {
381 $api->_init($api);
382 }
383 }
384
385 /**
386 * @param mixed $result
387 * @param \ImportWP\Pro\Importer\Template\CustomFields $custom_fields
388 *
389 * @return void
390 */
391 public function _custom_fields_init($result, $custom_fields)
392 {
393 foreach ($this->_custom_fields as $api) {
394 $api->_init($custom_fields);
395 }
396
397 $this->_setup = true;
398 }
399
400 /**
401 * @param array $fields
402 * @param ImporterModel $importer_model
403 * @return array
404 */
405 public function _custom_fields_get_fields($fields, $importer_model)
406 {
407 $this->_try_init($importer_model);
408 foreach ($this->_custom_fields as $api) {
409
410 $api->_register_fields($importer_model);
411
412 $fields = array_merge($api->_get_fields(), $fields);
413 }
414
415 return $fields;
416 }
417
418 public function _custom_fields_process_field($result, $post_id, $key, $value, $custom_field_record, $prefix, $importer_model, $custom_field)
419 {
420 foreach ($this->_custom_fields as $api) {
421
422 $response = new AddonCustomFieldSaveResponse($importer_model, $custom_field);
423 $response->_set_records($custom_field_record, $prefix);
424
425 $api->_save($response, $post_id, $key, $value);
426 }
427
428 return $result;
429 }
430
431 /**
432 * @param \ImportWP\Common\Model\ImporterModel $importer_model
433 * @param \ImportWP\Common\Importer\Template\Template $template
434 *
435 * @return ParsedData
436 */
437 protected function _try_init($importer_model, $template = null)
438 {
439 $this->_importer_model = $importer_model;
440 $this->_template = $template;
441
442 if (!is_null($this->_init_callback) && is_callable($this->_init_callback)) {
443 call_user_func($this->_init_callback, $this);
444
445 // clear callback so its not triggered twice
446 $this->_init_callback = null;
447 }
448 }
449
450 public function importer_model()
451 {
452 return $this->_importer_model;
453 }
454
455 public function template()
456 {
457 return $this->_template;
458 }
459
460 public function register_migrations($callback)
461 {
462 $this->_migrations_callback = $callback;
463 // Migrate has to be called after the init callback
464 add_action('init', function () {
465 /**
466 * @var \ImportWP\Common\Importer\ImporterManager $importer_manager
467 */
468 $importer_manager = Container::getInstance()->get('importer_manager');
469 $this->_migrate(new \ImportWP\Common\Addon\AddonMigration(), $importer_manager);
470 });
471 }
472
473 /**
474 * @param \ImportWP\Common\Addon\AddonMigration $migration
475 * @param \ImportWP\Common\Importer\ImporterManager $importer_manager
476 */
477 protected function _migrate($migration, $importer_manager)
478 {
479 if (!is_null($this->_migrations_callback) && is_callable($this->_migrations_callback)) {
480 call_user_func($this->_migrations_callback, $migration);
481 $this->_migrations = $migration->data();
482
483 if (empty($this->_migrations)) {
484 return;
485 }
486
487 $option_key = 'migrate_' . $this->get_id();
488 $max_migrations = count($this->_migrations);
489
490 $importers = $importer_manager->get_importers();
491 if (!empty($importers)) {
492 foreach ($importers as $importer_id) {
493
494
495 $importer = $importer_manager->get_importer($importer_id);
496 $version = $importer->getSetting($option_key);
497 $version = intval($version);
498
499 if ($max_migrations > $version) {
500 for ($i = $version + 1; $i <= $max_migrations; $i++) {
501 call_user_func($this->_migrations[$i - 1], $importer);
502
503 $importer = $importer_manager->get_importer($importer_id);
504 $importer->setSetting($option_key, $i);
505 $importer->save();
506 }
507 }
508 }
509 }
510
511 // clear callback so its not triggered twice
512 $this->_migrations_callback = null;
513 }
514 }
515 }
516