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
← All changes | class/Common/Importer/Mapper/AbstractMapper.php +163 -5 2.8.3 → 2.15.0 View file →
@@ -1,13 +1,15 @@
1 1 <?php
2 2
3 3 namespace ImportWP\Common\Importer\Mapper;
4 4
5 -use ImportWP\Common\Importer\Exception\MapperException;
5 +use ImportWP\Common\Importer\ParsedData;
6 6 use ImportWP\Common\Importer\PermissionInterface;
7 7 use ImportWP\Common\Importer\Template\Template;
8 +use ImportWP\Common\Importer\Template\TemplateManager;
8 9 use ImportWP\Common\Importer\TemplateInterface;
9 10 use ImportWP\Common\Model\ImporterModel;
11 +use ImportWP\Common\Util\Logger;
10 12
11 13 class AbstractMapper
12 14 {
13 15 protected $ID;
@@ -29,8 +31,10 @@
29 31 * @var PermissionInterface
30 32 */
31 33 protected $permission;
32 34
35 + protected $unique_indentifier_data;
36 +
33 37 public function __construct(ImporterModel $importer, Template $template, PermissionInterface $permission = null)
34 38 {
35 39 $this->importer = $importer;
36 40 $this->template = $template;
@@ -45,8 +49,91 @@
45 49
46 50 return $this->permission;
47 51 }
48 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 +
49 136 public function getUniqueIdentifiers($unique_fields = [])
50 137 {
51 138
52 139 // set via importer interface
@@ -54,9 +141,17 @@
54 141 if (empty($unique_identifier)) {
55 142 return $unique_fields;
56 143 }
57 144
58 - $parts = array_filter(array_map('trim', explode(',', $unique_identifier)));
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));
59 154 if (empty($parts)) {
60 155 return $unique_fields;
61 156 }
62 157
@@ -62,12 +157,37 @@
62 157
63 158 return $parts;
64 159 }
65 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 +
66 186 public function is_session_tag_enabled()
67 187 {
68 - $db_version = intval(get_site_option('iwp_db_version', 0));
69 - $config_data = get_site_option('iwp_importer_config_' . $this->importer->getId(), []);
188 + $db_version = intval(get_option('iwp_db_version', 0));
189 + $config_data = get_option('iwp_importer_config_' . $this->importer->getId(), []);
70 190
71 191 if ($db_version >= 7 && isset($config_data['features'], $config_data['features']['session_table']) && $config_data['features']['session_table']) {
72 192 return true;
73 193 }
@@ -95,8 +215,9 @@
95 215 'item_type' => $type,
96 216 ];
97 217 $data_validation = ['%d', '%d', '%s'];
98 218
219 + // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
99 220 if (is_multisite()) {
100 221 $data['site_id'] = $wpdb->siteid;
101 222 $data_validation[] = '%d';
102 223 }
@@ -133,8 +254,9 @@
133 254 'item_type' => $type,
134 255 ];
135 256 $data_validation = ['%d', '%d', '%s'];
136 257
258 + // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
137 259 if (is_multisite()) {
138 260 $data['site_id'] = $wpdb->siteid;
139 261 $data_validation[] = '%d';
140 262 }
@@ -148,10 +270,16 @@
148 270 * @var \WPDB $wpdb
149 271 */
150 272 global $wpdb;
151 273
152 - $query = "SELECT item_id FROM `{$wpdb->prefix}iwp_sessions` WHERE importer_id={$this->importer->getId()} AND item_type='{$type}' AND `session` != '{$this->importer->getStatusId()}'";
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()}'";
153 280
281 + // TODO: is this needed as blog_id is more relevant since there is a sessions table per site?
154 282 if (is_multisite()) {
155 283 $query .= " AND site_id='{$wpdb->siteid}'";
156 284 }
157 285
@@ -156,6 +284,36 @@
156 284 }
157 285
158 286 $item_ids = $wpdb->get_col($query);
159 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 + {
160 318 }
161 319 }