PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.7.7
Import WP – CSV & XML Import Export for WordPress v2.7.7
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 / Importer / ParsedData.php

ParsedData.php in Import WP – CSV & XML Import Export for WordPress 2.7.7, at class/Common/Importer/ParsedData.php

167 lines 3.8 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\Importer;
4
5 class ParsedData
6 {
7 private $id;
8 private $method;
9 private $data;
10
11 /**
12 * @var MapperInterface $mapper
13 */
14 private $mapper;
15
16 public function __construct(MapperInterface $mapper)
17 {
18 $this->mapper = $mapper;
19 }
20
21 public function add($data, $group = 'default')
22 {
23 $group_id = 'default';
24 if (is_string($group)) {
25 $group_id = $group;
26 } elseif (is_array($group)) {
27 $group_id = isset($group['id']) ? $group['id'] : 'default';
28 }
29
30 if (!isset($this->data[$group_id])) {
31 $this->data[$group_id] = [];
32 }
33
34 $this->data[$group_id] = array_merge($this->data[$group_id], $data);
35 }
36
37 /**
38 * Update data with new values
39 *
40 * @param $data
41 * @param string $group
42 */
43 public function update($data, $group = 'default')
44 {
45
46 $group_id = 'default';
47 if (is_string($group)) {
48 $group_id = $group;
49 } elseif (is_array($group)) {
50 $group_id = isset($group['id']) ? $group['id'] : 'default';
51 }
52
53 foreach ($data as $key => $value) {
54 $this->data[$group_id][$key] = $value;
55 }
56 }
57
58 public function replace($data, $group = 'default')
59 {
60
61 $group_id = 'default';
62 if (is_string($group)) {
63 $group_id = $group;
64 } elseif (is_array($group)) {
65 $group_id = isset($group['id']) ? $group['id'] : 'default';
66 }
67
68 $this->data[$group_id] = $data;
69 }
70
71 /**
72 * Get data by group
73 *
74 * @param string $group Group id.
75 *
76 * @return array
77 */
78 public function getData($group = 'default')
79 {
80 return isset($this->data[$group]) ? $this->data[$group] : array();
81 }
82
83 public function getValue($field, $group = 'default')
84 {
85 // Escape early if no data is set
86 if (empty($this->data)) {
87 return false;
88 }
89
90 if ($group === '*') {
91 foreach ($this->data as $group => $group_data) {
92 if (isset($group_data[$field])) {
93 return $group_data[$field];
94 }
95 }
96 } else {
97 $data = $this->getData($group);
98 return isset($data[$field]) ? $data[$field] : false;
99 }
100
101 return false;
102 }
103
104 public function getId()
105 {
106 return $this->id;
107 }
108
109 public function map()
110 {
111 do_action('iwp/importer/mapper/init', $this);
112
113 $this->id = $this->mapper->exists($this);
114
115 $this->method = false === $this->id ? 'INSERT' : 'UPDATE';
116
117 // TODO: Should we pre process the data before permissions?
118
119 if ($this->mapper->permission()) {
120 $allowed_fields = $this->mapper->permission()->validate($this->getData('default'), $this->method, 'default');
121 $this->replace($allowed_fields);
122 }
123
124 do_action('iwp/importer/mapper/before', $this);
125
126 if (false === $this->id) {
127 do_action('iwp/importer/mapper/before_insert', $this);
128 $this->id = $this->mapper->insert($this);
129 } else {
130 do_action('iwp/importer/mapper/before_update', $this);
131 $this->id = $this->mapper->update($this);
132 }
133
134 do_action('iwp/importer/mapper/after', $this);
135 }
136
137 public function getGroupKeys()
138 {
139 return array_keys($this->data);
140 }
141
142 public function getMethod()
143 {
144 return $this->method;
145 }
146
147 public function getLog()
148 {
149 return $this->mapper->getLog();
150 }
151
152 public function permission()
153 {
154 return $this->mapper->permission();
155 }
156
157 public function isInsert()
158 {
159 return $this->method === 'INSERT';
160 }
161
162 public function isUpdate()
163 {
164 return $this->method === 'UPDATE';
165 }
166 }
167