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/UserMapper.php +118 -44 2.8.3 → 2.15.0 View file →
@@ -4,9 +4,9 @@
4 4
5 5 use ImportWP\Common\Importer\Exception\MapperException;
6 6 use ImportWP\Common\Importer\MapperInterface;
7 7 use ImportWP\Common\Importer\ParsedData;
8 -use ImportWP\Common\Importer\Template\TemplateManager;
8 +use ImportWP\Common\Util\Logger;
9 9
10 10 class UserMapper extends AbstractMapper implements MapperInterface
11 11 {
12 12 /**
@@ -40,48 +40,51 @@
40 40 }
41 41
42 42 public function exists(ParsedData $data)
43 43 {
44 - $unique_fields = TemplateManager::get_template_unique_fields($this->template);
44 + list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data);
45 + if (!empty($meta_args)) {
46 + foreach ($meta_args as &$meta_arg_v) {
47 + $meta_arg_v['compare'] = '=';
48 + $meta_arg_v['type'] = 'CHAR';
49 + }
50 + }
45 51
46 - $unique_fields = $this->getUniqueIdentifiers($unique_fields);
47 - $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
48 -
49 52 $unique_field_found = false;
50 53
51 - $default_group = $data->getData('default');
52 54 $query_args = array();
53 - $meta_args = array();
54 55 $search = array(); // store search values
55 56 $search_columns = array(); // store search columns
56 57
57 - $has_unique_field = false;
58 + if (!$has_unique_field) {
59 + foreach ($unique_fields as $field) {
58 60
59 - foreach ($unique_fields as $field) {
61 + // check all groups for a unique value
62 + $unique_value = $this->find_unique_field_in_data($data, $field);
60 63
61 - // check all groups for a unique value
62 - $unique_value = $data->getValue($field, '*');
63 - if (!empty($unique_value)) {
64 - $has_unique_field = true;
64 + if ($this->has_identifier_value($unique_value)) {
65 + $has_unique_field = true;
65 66
66 - if (in_array($field, $this->_user_fields)) {
67 - $search_columns[] = $field;
68 - $search[] = $default_group[$field];
69 - } else {
70 - $meta_args[] = array(
71 - 'key' => $field,
72 - 'value' => $default_group[$field],
73 - 'compare' => '=',
74 - 'type' => 'CHAR'
75 - );
67 + if (in_array($field, ['ID', 'user_login', 'user_nicename', 'user_email', 'user_url'])) {
68 + $search_columns[] = $field;
69 + $search[] = $unique_value;
70 + } else {
71 + $meta_args[] = array(
72 + 'key' => $field,
73 + 'value' => $unique_value,
74 + 'compare' => '=',
75 + 'type' => 'CHAR'
76 + );
77 + }
78 + $unique_field_found = $field;
79 + $this->set_unique_identifier_settings($unique_field_found, $unique_value);
80 + break;
76 81 }
77 - $unique_field_found = $field;
78 - break;
79 82 }
80 83 }
81 84
82 85 if (!$has_unique_field) {
83 - throw new MapperException("No Unique fields present.");
86 + throw new MapperException(__('No unique identifier value present. Check that Permissions has a unique identifier set and this row has a non-empty value. File column references are stored as _iwp_ref_uid.', 'jc-importer'));
84 87 }
85 88
86 89 // create search
87 90 $query_args['search'] = implode(', ', $search);
@@ -86,16 +89,63 @@
86 89 // create search
87 90 $query_args['search'] = implode(', ', $search);
88 91 $query_args['search_columns'] = $search_columns;
89 92 $query_args['meta_query'] = $meta_args;
93 + $query_args = apply_filters('iwp/importer/mapper/user_exists_query', $query_args);
94 + Logger::debug("UserMapper::exists -query=" . wp_json_encode($query_args));
90 95 $query = new \WP_User_Query($query_args);
91 96
97 + // if we are on a multisite instance and user has not been found.
98 + if (is_multisite() && $query->get_total() == 0) {
99 +
100 + // get list of sites except this one
101 + $sites = get_sites([
102 + 'site__not_in' => get_current_blog_id(),
103 + 'fields' => 'ids'
104 + ]);
105 +
106 + foreach ($sites as $site) {
107 +
108 + // do user search on each site
109 + switch_to_blog($site);
110 + $query = new \WP_User_Query($query_args);
111 + restore_current_blog();
112 +
113 + $total = $query->get_total();
114 + if ($total > 1) {
115 +
116 + $ids = [];
117 + foreach ($query->results as $result) {
118 + $ids[] = $result->ID;
119 + }
120 + throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $ids)));
121 + } elseif ($total == 1) {
122 +
123 + // if we have 1 result then add the user to our site.
124 +
125 + $role = $query->results[0]->roles[0];
126 + if ($this->importer->isEnabledField('user.role')) {
127 + $tmp_role = $data->getValue('role');
128 + if (!empty($tmp_role)) {
129 + $role = $tmp_role;
130 + }
131 + }
132 +
133 + $result = add_user_to_blog(get_current_blog_id(), $query->results[0]->ID, $role);
134 + if (is_wp_error($result)) {
135 + throw new MapperException($result->get_error_message());
136 + }
137 + break;
138 + }
139 + }
140 + }
141 +
92 142 if ($query->total_users > 1) {
93 143 $ids = [];
94 144 foreach ($query->results as $result) {
95 145 $ids[] = $result->ID;
96 146 }
97 - throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $ids) . ").");
147 + throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $ids)));
98 148 }
99 149
100 150 if ($query->total_users == 1) {
101 151 $this->ID = $query->results[0]->ID;
@@ -124,19 +174,21 @@
124 174 }
125 175 }
126 176
127 177 if (!isset($core['user_login']) || empty($core['user_login'])) {
128 - throw new MapperException("No username present");
178 + throw new MapperException(__("No username present", 'jc-importer'));
129 179 }
130 180
131 181 if (!isset($core['user_pass']) || empty($core['user_pass'])) {
132 - throw new MapperException("No password present");
182 + throw new MapperException(__("No password present", 'jc-importer'));
133 183 }
134 184
135 185 if (!empty($core['user_email']) && !is_email($core['user_email'])) {
136 - throw new MapperException(strval($core['user_email']) . " is not a valid email address");
186 + throw new MapperException(sprintf(__("%s is not a valid email address", 'jc-importer'), strval($core['user_email'])));
137 187 }
138 188
189 + Logger::debug('UserMapper::insert -wp_insert_user=' . wp_json_encode($core));
190 +
139 191 $this->ID = wp_insert_user($core);
140 192 if (is_wp_error($this->ID)) {
141 193 throw new MapperException($this->ID->get_error_message());
142 194 }
@@ -145,8 +197,9 @@
145 197 $this->template->process($this->ID, $data, $this->importer);
146 198
147 199 // TODO: merge in custom fields from $data->getData('custom_fields');
148 200 $meta = array_merge($meta, $data->getData('custom_fields'));
201 + Logger::debug('UserMapper::insert -meta=' . wp_json_encode($meta));
149 202
150 203 // update user meta
151 204 if (!empty($meta)) {
152 205 foreach ($meta as $key => $value) {
@@ -151,9 +204,8 @@
151 204 if (!empty($meta)) {
152 205 foreach ($meta as $key => $value) {
153 206 if (!in_array($key, $this->_user_fields)) {
154 207 if (is_array($value)) {
155 - $this->clear_custom_field($this->ID, $key);
156 208 foreach ($value as $v) {
157 209 $this->add_custom_field($this->ID, $key, $v);
158 210 }
159 211 } else {
@@ -163,8 +215,9 @@
163 215 }
164 216 }
165 217
166 218 $this->add_version_tag();
219 + $this->add_reference_tag($data);
167 220 $this->template->post_process($this->ID, $data);
168 221
169 222 return $this->ID;
170 223 }
@@ -188,8 +241,10 @@
188 241 $meta[$field_name] = $field_value;
189 242 }
190 243 }
191 244
245 + Logger::debug('UserMapper::update -wp_update_user=' . wp_json_encode($core));
246 +
192 247 if (!empty($core)) {
193 248 $core['ID'] = $this->ID;
194 249 $result = wp_update_user($core);
195 250 if (is_wp_error($result)) {
@@ -200,15 +255,16 @@
200 255 // TODO: Do we merge in the custom fields, or do we process that in post_process
201 256 $this->template->process($this->ID, $data, $this->importer);
202 257
203 258 $meta = array_merge($meta, $data->getData('custom_fields'));
259 + Logger::debug('UserMapper::update -meta=' . wp_json_encode($meta));
204 260
205 261 // update user meta
206 262 if (!empty($meta)) {
207 263 foreach ($meta as $key => $value) {
208 264 if (!in_array($key, $this->_user_fields)) {
265 + $this->clear_custom_field($this->ID, $key);
209 266 if (is_array($value)) {
210 - $this->clear_custom_field($this->ID, $key);
211 267 foreach ($value as $v) {
212 268 $this->add_custom_field($this->ID, $key, $v);
213 269 }
214 270 } else {
@@ -218,8 +274,9 @@
218 274 }
219 275 }
220 276
221 277 $this->add_version_tag();
278 + $this->add_reference_tag($data);
222 279 $this->template->post_process($this->ID, $data);
223 280
224 281 return $this->ID;
225 282 }
@@ -228,17 +285,30 @@
228 285 {
229 286 if ($this->is_session_tag_enabled()) {
230 287 return $this->get_ids_without_session_tag('user');
231 288 } else {
289 +
290 + $import_ids = apply_filters('iwp/mapper/session_importer_ids', [$this->importer->getId()], $this->importer->getId());
291 + if (empty($import_ids)) {
292 + return false;
293 + }
294 +
295 + $meta_query = [];
296 + foreach ($import_ids as $import_id) {
297 + $meta_query[] = [
298 + 'key' => '_iwp_session_' . $import_id,
299 + 'value' => $this->importer->getStatusId(),
300 + 'compare' => '!='
301 + ];
302 + }
303 +
304 + if (count($meta_query) > 1) {
305 + $meta_query['relation'] = 'AND';
306 + }
307 +
232 308 $wp_user_query = new \WP_User_Query([
233 309 'fields' => 'ID',
234 - 'meta_query' => array(
235 - array(
236 - 'key' => '_iwp_session_' . $this->importer->getId(),
237 - 'value' => $this->importer->getStatusId(),
238 - 'compare' => '!='
239 - )
240 - ),
310 + 'meta_query' => $meta_query,
241 311 'number' => -1
242 312 ]);
243 313
244 314 $results = $wp_user_query->get_results();
@@ -251,15 +321,19 @@
251 321 }
252 322
253 323 public function delete($id)
254 324 {
255 - require_once(ABSPATH . 'wp-admin/includes/user.php');
256 - wp_delete_user($id);
325 + if (is_multisite()) {
326 + remove_user_from_blog($id);
327 + } else {
328 + require_once(ABSPATH . 'wp-admin/includes/user.php');
329 + wp_delete_user($id);
330 + }
257 331
258 332 $this->remove_session_tag($id, 'user');
259 333 }
260 334
261 - public function get_custom_field($id, $key, $single = true)
335 + public function get_custom_field($id, $key = '', $single = false)
262 336 {
263 337 return get_user_meta($id, $key, $single);
264 338 }
265 339
@@ -288,9 +362,9 @@
288 362
289 363 add_user_meta($user_id, $key, $value);
290 364 }
291 365
292 - public function update_custom_field($user_id, $key, $value)
366 + public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false)
293 367 {
294 368 // Stop double serialization
295 369 if (is_serialized($value)) {
296 370 $value = unserialize($value);
@@ -295,9 +369,9 @@
295 369 if (is_serialized($value)) {
296 370 $value = unserialize($value);
297 371 }
298 372
299 - update_user_meta($user_id, $key, $value);
373 + update_user_meta($id, $key, $value);
300 374 }
301 375
302 376 public function add_version_tag()
303 377 {