PluginProbe
Import WP – CSV & XML Import Export for WordPress / trunk
Import WP – CSV & XML Import Export for WordPress vtrunk
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 trunk 0.1.6 All 142 releases
jc-importer / class / Common / Addon / AddonBase.php

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

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