PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.14.23
Import WP – CSV & XML Import Export for WordPress v2.14.23
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.14.23, at class/Common/Importer/Mapper/AbstractMapper.php

307 lines 8.9 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 if (!$value) {
72 $value = '';
73 }
74
75 $meta_args[] = array(
76 'key' => $key,
77 'value' => $value
78 );
79
80 $this->set_unique_identifier_settings($key, $value);
81
82 Logger::debug('AbstractMapper::exists_get_identifier -type="custom" -field="' . $key . '" -value="' . $value . '"');
83 } elseif ($this->importer->has_field_unique_identifier()) {
84
85 // we have set a specific identifier
86 $unique_field = $this->importer->getSetting('unique_identifier');
87 if ($unique_field !== null && !empty($unique_field)) {
88 $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field;
89 }
90
91 Logger::debug('AbstractMapper::exists_get_identifier -type="field" -field="' . wp_json_encode($unique_fields) . '"');
92 } else {
93
94 // NOTE: fallback to allow templates to set this in pre 2.11.9
95 $unique_fields = TemplateManager::get_template_unique_fields($this->template);
96
97 // allow user to set unique field name, get from importer setting
98 $unique_field = $this->importer->getSetting('unique_field');
99 if ($unique_field !== null) {
100 $unique_fields = is_string($unique_field) ? [$unique_field] : $unique_field;
101 }
102
103 $unique_fields = $this->getUniqueIdentifiers($unique_fields);
104 $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
105
106 Logger::debug('AbstractMapper::exists_get_identifier -type="legacy" -fields="' . wp_json_encode($unique_fields) . '"');
107 }
108
109
110 return [$unique_fields, $meta_args, $has_unique_field];
111 }
112
113 public function set_unique_identifier_settings($field, $value)
114 {
115 $this->unique_indentifier_data = ['field' => $field, 'value' => $value];
116 }
117
118 public function get_unqiue_identifier_settings()
119 {
120 return $this->unique_indentifier_data;
121 }
122
123 public function getUniqueIdentifiers($unique_fields = [])
124 {
125
126 // set via importer interface
127 $unique_identifier = $this->importer->getSetting('unique_identifier');
128 if (empty($unique_identifier)) {
129 return $unique_fields;
130 }
131
132 if (is_string($unique_identifier)) {
133
134 $unique_identifier = explode(',', $unique_identifier);
135 if (!$unique_identifier) {
136 return $unique_fields;
137 }
138 }
139
140 $parts = array_filter(array_map('trim', (array)$unique_identifier));
141 if (empty($parts)) {
142 return $unique_fields;
143 }
144
145 return $parts;
146 }
147
148 public function find_unique_field_in_data($data, $field)
149 {
150 // TODO: this should only be done once per update
151 $unique_value = $data->getValue($field, '*');
152 if (empty($unique_value)) {
153 $cf = $data->getData('custom_fields');
154 if (!empty($cf)) {
155 $cf_index = intval($cf['custom_fields._index']);
156 if ($cf_index > 0) {
157 for ($i = 0; $i < $cf_index; $i++) {
158 $row = 'custom_fields.' . $i . '.';
159 $custom_field_key = apply_filters('iwp/custom_field_key', $cf[$row . 'key']);
160 if ($custom_field_key !== $field) {
161 continue;
162 }
163 $unique_value = $cf[$row . 'value'];
164 break;
165 }
166 }
167 }
168 }
169
170 return $unique_value;
171 }
172
173 public function is_session_tag_enabled()
174 {
175 $db_version = intval(get_option('iwp_db_version', 0));
176 $config_data = get_option('iwp_importer_config_' . $this->importer->getId(), []);
177
178 if ($db_version >= 7 && isset($config_data['features'], $config_data['features']['session_table']) && $config_data['features']['session_table']) {
179 return true;
180 }
181
182 return false;
183 }
184
185 /**
186 * Update or insert record in session table
187 *
188 * Session table keeps track of what records where modified during the import
189 *
190 * @return void
191 */
192 public function add_session_tag($type)
193 {
194 /**
195 * @var \WPDB $wpdb
196 */
197 global $wpdb;
198
199 $data = [
200 'importer_id' => $this->importer->getId(),
201 'item_id' => $this->ID,
202 'item_type' => $type,
203 ];
204 $data_validation = ['%d', '%d', '%s'];
205
206 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
207 if (is_multisite()) {
208 $data['site_id'] = $wpdb->siteid;
209 $data_validation[] = '%d';
210 }
211
212 $updated = $wpdb->update($wpdb->prefix . 'iwp_sessions', [
213 'session' => $this->importer->getStatusId()
214 ], $data, ['%s'], $data_validation);
215
216 if (!$updated) {
217
218 $wpdb->insert($wpdb->prefix . 'iwp_sessions', array_merge($data, [
219 'session' => $this->importer->getStatusId()
220 ]));
221 }
222 }
223
224 /**
225 * Remove record from session table
226 *
227 * @param int $id
228 * @param string $type
229 * @return void
230 */
231 public function remove_session_tag($id, $type)
232 {
233 /**
234 * @var \WPDB $wpdb
235 */
236 global $wpdb;
237
238 $data = [
239 'importer_id' => $this->importer->getId(),
240 'item_id' => $id,
241 'item_type' => $type,
242 ];
243 $data_validation = ['%d', '%d', '%s'];
244
245 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
246 if (is_multisite()) {
247 $data['site_id'] = $wpdb->siteid;
248 $data_validation[] = '%d';
249 }
250
251 $wpdb->delete($wpdb->prefix . 'iwp_sessions', $data, $data_validation);
252 }
253
254 public function get_ids_without_session_tag($type)
255 {
256 /**
257 * @var \WPDB $wpdb
258 */
259 global $wpdb;
260
261 $import_ids = apply_filters('iwp/mapper/session_importer_ids', [$this->importer->getId()], $this->importer->getId());
262 if(empty($import_ids)){
263 return false;
264 }
265
266 $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()}'";
267
268 // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
269 if (is_multisite()) {
270 $query .= " AND site_id='{$wpdb->siteid}'";
271 }
272
273 $item_ids = $wpdb->get_col($query);
274 return $item_ids;
275 }
276
277 public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false)
278 {
279 }
280
281 /**
282 * Clear all meta before adding custom field
283 */
284 public function clear_custom_field($id, $key)
285 {
286 }
287
288 public function get_custom_field($id, $key = '', $single = false)
289 {
290 return false;
291 }
292
293 public function add_reference_tag($data)
294 {
295 if (!$this->importer->has_custom_unique_identifier()) {
296 return;
297 }
298
299 $key = $this->importer->get_iwp_reference_meta_key();
300 $this->update_custom_field($this->ID, $key, $data->getValue($key, 'iwp'));
301 }
302
303 public function add_version_tag()
304 {
305 }
306 }
307