PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.15.0
Import WP – CSV & XML Import Export for WordPress v2.15.0
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.15.0, at class/Common/Importer/ParsedData.php

224 lines 5.3 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 use ImportWP\Common\Importer\Exception\RecordUpdatedSkippedException;
6
7 class ParsedData
8 {
9 protected $id;
10 protected $method;
11 protected $data;
12
13 /**
14 * @var MapperInterface $mapper
15 */
16 protected $mapper;
17
18 public function __construct(MapperInterface $mapper)
19 {
20 $this->mapper = $mapper;
21 }
22
23 public function add($data, $group = 'default')
24 {
25 $group_id = 'default';
26 if (is_string($group)) {
27 $group_id = $group;
28 } elseif (is_array($group)) {
29 $group_id = isset($group['id']) ? $group['id'] : 'default';
30 }
31
32 if (!isset($this->data[$group_id])) {
33 $this->data[$group_id] = [];
34 }
35
36 $this->data[$group_id] = array_merge($this->data[$group_id], $data);
37 }
38
39 /**
40 * Update data with new values
41 *
42 * @param $data
43 * @param string $group
44 */
45 public function update($data, $group = 'default')
46 {
47
48 $group_id = 'default';
49 if (is_string($group)) {
50 $group_id = $group;
51 } elseif (is_array($group)) {
52 $group_id = isset($group['id']) ? $group['id'] : 'default';
53 }
54
55 foreach ($data as $key => $value) {
56 $this->data[$group_id][$key] = $value;
57 }
58 }
59
60 public function replace($data, $group = 'default')
61 {
62
63 $group_id = 'default';
64 if (is_string($group)) {
65 $group_id = $group;
66 } elseif (is_array($group)) {
67 $group_id = isset($group['id']) ? $group['id'] : 'default';
68 }
69
70 $this->data[$group_id] = $data;
71 }
72
73 /**
74 * Get data by group
75 *
76 * @param string $group Group id.
77 *
78 * @return array
79 */
80 public function getData($group = 'default')
81 {
82 return isset($this->data[$group]) ? $this->data[$group] : array();
83 }
84
85 public function getValue($field, $group = 'default')
86 {
87 // Escape early if no data is set
88 if (empty($this->data)) {
89 return false;
90 }
91
92 if ($group === '*') {
93 foreach ($this->data as $group => $group_data) {
94 if (isset($group_data[$field])) {
95 return $group_data[$field];
96 }
97 }
98 } else {
99 $data = $this->getData($group);
100 return isset($data[$field]) ? $data[$field] : false;
101 }
102
103 return false;
104 }
105
106 public function getId()
107 {
108 return $this->id;
109 }
110
111 public function map()
112 {
113 do_action('iwp/importer/mapper/init', $this);
114
115 $this->id = $this->mapper->exists($this);
116
117 $this->method = false === $this->id ? 'INSERT' : 'UPDATE';
118
119 // TODO: Should we pre process the data before permissions?
120
121 if ($this->mapper->permission()) {
122 $allowed_fields = $this->mapper->permission()->validate($this->getData('default'), $this->method, 'default');
123 $this->replace($allowed_fields);
124 }
125
126 do_action('iwp/importer/mapper/before', $this);
127
128 if (false === $this->id) {
129
130 do_action('iwp/importer/mapper/before_insert', $this);
131
132 // NOTE: has should be fetched before insert
133 $data_hash = $this->generate_hash();
134
135 $this->id = $this->mapper->insert($this);
136 $this->update_hash($data_hash);
137 } else {
138
139 do_action('iwp/importer/mapper/before_update', $this);
140
141 // NOTE: has should be fetched before update
142 $hash_check = $this->generate_hash();
143
144 if (!$this->hash_compare($hash_check)) {
145
146 $this->id = $this->mapper->update($this);
147 $this->update_hash($hash_check);
148 } else {
149
150 // we need to log this result
151 $this->mapper->add_version_tag();
152 $this->update_hash($hash_check);
153 throw new RecordUpdatedSkippedException("Data matches previous import");
154 return;
155 }
156 }
157
158 do_action('iwp/importer/mapper/after', $this);
159 }
160
161 public function getGroupKeys()
162 {
163 return array_keys($this->data);
164 }
165
166 public function getMethod()
167 {
168 return $this->method;
169 }
170
171 public function getLog()
172 {
173 return $this->mapper->getLog();
174 }
175
176 public function permission()
177 {
178 return $this->mapper->permission();
179 }
180
181 public function isInsert()
182 {
183 return $this->method === 'INSERT';
184 }
185
186 public function isUpdate()
187 {
188 return $this->method === 'UPDATE';
189 }
190
191 public function generate_hash()
192 {
193 return $this->array_md5($this->data);
194 }
195
196 function array_md5($array)
197 {
198 array_multisort($array);
199 return md5(json_encode(apply_filters('iwp/hash_compare', $array)));
200 }
201
202 public function hash_compare($hash_b)
203 {
204 $hash_a = $this->get_last_hash();
205 $hash_check_enabled = apply_filters('iwp/importer/mapper/hash_check_enabled', false);
206
207 if ($hash_check_enabled && !empty($hash_a) && $hash_a === $hash_b) {
208 return true;
209 }
210
211 return false;
212 }
213
214 public function get_last_hash()
215 {
216 return $this->mapper->get_custom_field($this->id, '_iwp_hash', true);
217 }
218
219 public function update_hash($hash)
220 {
221 $this->mapper->update_custom_field($this->id, '_iwp_hash', $hash);
222 }
223 }
224