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 / Mapper / AbstractMapper.php

AbstractMapper.php in Import WP – CSV & XML Import Export for WordPress 2.15.0, at class/Common/Importer/Mapper/AbstractMapper.php

320 lines 9.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\Importer\Mapper;
4
5 use ImportWP\Common\Importer\ParsedData;
6 use ImportWP\Common\Importer\PermissionInterface;
7 use ImportWP\Common\Importer\Template\Template;
8 use ImportWP\Common\Importer\Template\TemplateManager;
9 use ImportWP\Common\Importer\TemplateInterface;
10 use ImportWP\Common\Model\ImporterModel;
11 use ImportWP\Common\Util\Logger;
12
13 class AbstractMapper
14 {
15 protected $ID;
16 /**
17 * Importer Template
18 *
19 * @var TemplateInterface
20 */
21 protected $template;
22 /**
23 * Importer Data
24 *
25 * @var ImporterModel
26 */
27 protected $importer;
28 /**
29 * Importer Permissions
30 *
31 * @var PermissionInterface
32 */
33 protected $permission;
34
35 protected $unique_indentifier_data;
36
37 public function __construct(ImporterModel $importer, Template $template, PermissionInterface $permission = null)
38 {
39 $this->importer = $importer;
40 $this->template = $template;
41 $this->permission = $permission;
42 }
43
44 public function permission()
45 {
46 if (is_null($this->permission)) {
47 return false;
48 }
49
50 return $this->permission;
51 }
52
53 /**
54 *
55 * @param ParsedData $data
56 * @return array
57 */
58 public function exists_get_identifier($data)
59 {
60 $unique_fields = [];
61 $has_unique_field = false;
62 $meta_args = [];
63
64 if ($this->importer->has_custom_unique_identifier()) {
65
66 // custom unique identifier is stored in meta table
67 $has_unique_field = true;
68 $key = $this->importer->get_iwp_reference_meta_key();
69
70 $value = $data->getValue($key, 'iwp');
71 // 0 / "0" are valid identifiers — only treat missing/blank as empty.
72 if (!$this->has_identifier_value($value)) {
73 $value = '';
74 }
75
76 $meta_args[] = array(
77 'key' => $key,
78 'value' => $value
79 );
80
81 $this->set_unique_identifier_settings($key, $value);
82
83 Logger::debug('AbstractMapper::exists_get_identifier -type="custom" -field="' . $key . '" -value="' . $value . '"');
84 } elseif ($this->importer->has_field_unique_identifier()) {
85
86 // we have set a specific identifier
87 $unique_field = $this->importer->getSetting('unique_identifier');
88 if ($unique_field !== null && !empty($unique_field)) {
89 $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field;
90 }
91
92 Logger::debug('AbstractMapper::exists_get_identifier -type="field" -field="' . wp_json_encode($unique_fields) . '"');
93 } else {
94
95 // NOTE: fallback to allow templates to set this in pre 2.11.9
96 $unique_fields = TemplateManager::get_template_unique_fields($this->template);
97
98 // allow user to set unique field name, get from importer setting
99 $unique_field = $this->importer->getSetting('unique_field');
100 if ($unique_field !== null) {
101 $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field;
102 }
103
104 $unique_fields = $this->getUniqueIdentifiers($unique_fields);
105 $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
106
107 Logger::debug('AbstractMapper::exists_get_identifier -type="legacy" -fields="' . wp_json_encode($unique_fields) . '"');
108 }
109
110
111 return [$unique_fields, $meta_args, $has_unique_field];
112 }
113
114 public function set_unique_identifier_settings($field, $value)
115 {
116 $this->unique_indentifier_data = ['field' => $field, 'value' => $value];
117 }
118
119 public function get_unqiue_identifier_settings()
120 {
121 return $this->unique_indentifier_data;
122 }
123
124 /**
125 * Whether a unique identifier value is present.
126 * Note: 0 and "0" are valid identifiers (PHP empty()/falsy checks treat them as empty).
127 *
128 * @param mixed $value
129 * @return bool
130 */
131 public function has_identifier_value($value)
132 {
133 return !(null === $value || false === $value || '' === $value);
134 }
135
136 public function getUniqueIdentifiers($unique_fields = [])
137 {
138
139 // set via importer interface
140 $unique_identifier = $this->importer->getSetting('unique_identifier');
141 if (empty($unique_identifier)) {
142 return $unique_fields;
143 }
144
145 if (is_string($unique_identifier)) {
146
147 $unique_identifier = explode(',', $unique_identifier);
148 if (!$unique_identifier) {
149 return $unique_fields;
150 }
151 }
152
153 $parts = array_filter(array_map('trim', (array)$unique_identifier));
154 if (empty($parts)) {
155 return $unique_fields;
156 }
157
158 return $parts;
159 }
160
161 public function find_unique_field_in_data($data, $field)
162 {
163 // TODO: this should only be done once per update
164 $unique_value = $data->getValue($field, '*');
165 if (!$this->has_identifier_value($unique_value)) {
166 $cf = $data->getData('custom_fields');
167 if (!empty($cf)) {
168 $cf_index = intval($cf['custom_fields._index']);
169 if ($cf_index > 0) {
170 for ($i = 0; $i < $cf_index; $i++) {
171 $row = 'custom_fields.' . $i . '.';
172 $custom_field_key = apply_filters('iwp/custom_field_key', $cf[$row . 'key']);
173 if ($custom_field_key !== $field) {
174 continue;
175 }
176 $unique_value = $cf[$row . 'value'];
177 break;
178 }
179 }
180 }
181 }
182
183 return $unique_value;
184 }
185
186 public function is_session_tag_enabled()
187 {
188 $db_version = intval(get_option('iwp_db_version', 0));
189 $config_data = get_option('iwp_importer_config_' . $this->importer->getId(), []);
190
191 if ($db_version >= 7 && isset($config_data['features'], $config_data['features']['session_table']) && $config_data['features']['session_table']) {
192 return true;
193 }
194
195 return false;
196 }
197
198 /**
199 * Update or insert record in session table
200 *
201 * Session table keeps track of what records where modified during the import
202 *
203 * @return void
204 */
205 public function add_session_tag($type)
206 {
207 /**
208 * @var \WPDB $wpdb
209 */
210 global $wpdb;
211
212 $data = [
213 'importer_id' => $this->importer->getId(),
214 'item_id' => $this->ID,
215 'item_type' => $type,
216 ];
217 $data_validation = ['%d', '%d', '%s'];
218
219 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
220 if (is_multisite()) {
221 $data['site_id'] = $wpdb->siteid;
222 $data_validation[] = '%d';
223 }
224
225 $updated = $wpdb->update($wpdb->prefix . 'iwp_sessions', [
226 'session' => $this->importer->getStatusId()
227 ], $data, ['%s'], $data_validation);
228
229 if (!$updated) {
230
231 $wpdb->insert($wpdb->prefix . 'iwp_sessions', array_merge($data, [
232 'session' => $this->importer->getStatusId()
233 ]));
234 }
235 }
236
237 /**
238 * Remove record from session table
239 *
240 * @param int $id
241 * @param string $type
242 * @return void
243 */
244 public function remove_session_tag($id, $type)
245 {
246 /**
247 * @var \WPDB $wpdb
248 */
249 global $wpdb;
250
251 $data = [
252 'importer_id' => $this->importer->getId(),
253 'item_id' => $id,
254 'item_type' => $type,
255 ];
256 $data_validation = ['%d', '%d', '%s'];
257
258 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
259 if (is_multisite()) {
260 $data['site_id'] = $wpdb->siteid;
261 $data_validation[] = '%d';
262 }
263
264 $wpdb->delete($wpdb->prefix . 'iwp_sessions', $data, $data_validation);
265 }
266
267 public function get_ids_without_session_tag($type)
268 {
269 /**
270 * @var \WPDB $wpdb
271 */
272 global $wpdb;
273
274 $import_ids = apply_filters('iwp/mapper/session_importer_ids', [$this->importer->getId()], $this->importer->getId());
275 if(empty($import_ids)){
276 return false;
277 }
278
279 $query = "SELECT item_id FROM `{$wpdb->prefix}iwp_sessions` WHERE importer_id IN (" . implode(',', $import_ids) . ") AND item_type='{$type}' AND `session` != '{$this->importer->getStatusId()}'";
280
281 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
282 if (is_multisite()) {
283 $query .= " AND site_id='{$wpdb->siteid}'";
284 }
285
286 $item_ids = $wpdb->get_col($query);
287 return $item_ids;
288 }
289
290 public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false)
291 {
292 }
293
294 /**
295 * Clear all meta before adding custom field
296 */
297 public function clear_custom_field($id, $key)
298 {
299 }
300
301 public function get_custom_field($id, $key = '', $single = false)
302 {
303 return false;
304 }
305
306 public function add_reference_tag($data)
307 {
308 if (!$this->importer->has_custom_unique_identifier()) {
309 return;
310 }
311
312 $key = $this->importer->get_iwp_reference_meta_key();
313 $this->update_custom_field($this->ID, $key, $data->getValue($key, 'iwp'));
314 }
315
316 public function add_version_tag()
317 {
318 }
319 }
320