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 -43 2.7.12 → 2.15.0 View file →
@@ -4,8 +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\Util\Logger;
8 9
9 10 class UserMapper extends AbstractMapper implements MapperInterface
10 11 {
11 12 /**
@@ -39,48 +40,51 @@
39 40 }
40 41
41 42 public function exists(ParsedData $data)
42 43 {
43 - $unique_fields = ['user_email', 'user_login'];
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 + }
44 51
45 - $unique_fields = $this->getUniqueIdentifiers($unique_fields);
46 - $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
47 -
48 52 $unique_field_found = false;
49 53
50 - $default_group = $data->getData('default');
51 54 $query_args = array();
52 - $meta_args = array();
53 55 $search = array(); // store search values
54 56 $search_columns = array(); // store search columns
55 57
56 - $has_unique_field = false;
58 + if (!$has_unique_field) {
59 + foreach ($unique_fields as $field) {
57 60
58 - foreach ($unique_fields as $field) {
61 + // check all groups for a unique value
62 + $unique_value = $this->find_unique_field_in_data($data, $field);
59 63
60 - // check all groups for a unique value
61 - $unique_value = $data->getValue($field, '*');
62 - if (!empty($unique_value)) {
63 - $has_unique_field = true;
64 + if ($this->has_identifier_value($unique_value)) {
65 + $has_unique_field = true;
64 66
65 - if (in_array($field, $this->_user_fields)) {
66 - $search_columns[] = $field;
67 - $search[] = $default_group[$field];
68 - } else {
69 - $meta_args[] = array(
70 - 'key' => $field,
71 - 'value' => $default_group[$field],
72 - 'compare' => '=',
73 - 'type' => 'CHAR'
74 - );
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;
75 81 }
76 - $unique_field_found = $field;
77 - break;
78 82 }
79 83 }
80 84
81 85 if (!$has_unique_field) {
82 - 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'));
83 87 }
84 88
85 89 // create search
86 90 $query_args['search'] = implode(', ', $search);
@@ -85,16 +89,63 @@
85 89 // create search
86 90 $query_args['search'] = implode(', ', $search);
87 91 $query_args['search_columns'] = $search_columns;
88 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));
89 95 $query = new \WP_User_Query($query_args);
90 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 +
91 142 if ($query->total_users > 1) {
92 143 $ids = [];
93 144 foreach ($query->results as $result) {
94 145 $ids[] = $result->ID;
95 146 }
96 - 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)));
97 148 }
98 149
99 150 if ($query->total_users == 1) {
100 151 $this->ID = $query->results[0]->ID;
@@ -123,19 +174,21 @@
123 174 }
124 175 }
125 176
126 177 if (!isset($core['user_login']) || empty($core['user_login'])) {
127 - throw new MapperException("No username present");
178 + throw new MapperException(__("No username present", 'jc-importer'));
128 179 }
129 180
130 181 if (!isset($core['user_pass']) || empty($core['user_pass'])) {
131 - throw new MapperException("No password present");
182 + throw new MapperException(__("No password present", 'jc-importer'));
132 183 }
133 184
134 185 if (!empty($core['user_email']) && !is_email($core['user_email'])) {
135 - 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'])));
136 187 }
137 188
189 + Logger::debug('UserMapper::insert -wp_insert_user=' . wp_json_encode($core));
190 +
138 191 $this->ID = wp_insert_user($core);
139 192 if (is_wp_error($this->ID)) {
140 193 throw new MapperException($this->ID->get_error_message());
141 194 }
@@ -144,8 +197,9 @@
144 197 $this->template->process($this->ID, $data, $this->importer);
145 198
146 199 // TODO: merge in custom fields from $data->getData('custom_fields');
147 200 $meta = array_merge($meta, $data->getData('custom_fields'));
201 + Logger::debug('UserMapper::insert -meta=' . wp_json_encode($meta));
148 202
149 203 // update user meta
150 204 if (!empty($meta)) {
151 205 foreach ($meta as $key => $value) {
@@ -150,9 +204,8 @@
150 204 if (!empty($meta)) {
151 205 foreach ($meta as $key => $value) {
152 206 if (!in_array($key, $this->_user_fields)) {
153 207 if (is_array($value)) {
154 - $this->clear_custom_field($this->ID, $key);
155 208 foreach ($value as $v) {
156 209 $this->add_custom_field($this->ID, $key, $v);
157 210 }
158 211 } else {
@@ -162,8 +215,9 @@
162 215 }
163 216 }
164 217
165 218 $this->add_version_tag();
219 + $this->add_reference_tag($data);
166 220 $this->template->post_process($this->ID, $data);
167 221
168 222 return $this->ID;
169 223 }
@@ -187,8 +241,10 @@
187 241 $meta[$field_name] = $field_value;
188 242 }
189 243 }
190 244
245 + Logger::debug('UserMapper::update -wp_update_user=' . wp_json_encode($core));
246 +
191 247 if (!empty($core)) {
192 248 $core['ID'] = $this->ID;
193 249 $result = wp_update_user($core);
194 250 if (is_wp_error($result)) {
@@ -199,15 +255,16 @@
199 255 // TODO: Do we merge in the custom fields, or do we process that in post_process
200 256 $this->template->process($this->ID, $data, $this->importer);
201 257
202 258 $meta = array_merge($meta, $data->getData('custom_fields'));
259 + Logger::debug('UserMapper::update -meta=' . wp_json_encode($meta));
203 260
204 261 // update user meta
205 262 if (!empty($meta)) {
206 263 foreach ($meta as $key => $value) {
207 264 if (!in_array($key, $this->_user_fields)) {
265 + $this->clear_custom_field($this->ID, $key);
208 266 if (is_array($value)) {
209 - $this->clear_custom_field($this->ID, $key);
210 267 foreach ($value as $v) {
211 268 $this->add_custom_field($this->ID, $key, $v);
212 269 }
213 270 } else {
@@ -217,8 +274,9 @@
217 274 }
218 275 }
219 276
220 277 $this->add_version_tag();
278 + $this->add_reference_tag($data);
221 279 $this->template->post_process($this->ID, $data);
222 280
223 281 return $this->ID;
224 282 }
@@ -227,17 +285,30 @@
227 285 {
228 286 if ($this->is_session_tag_enabled()) {
229 287 return $this->get_ids_without_session_tag('user');
230 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 +
231 308 $wp_user_query = new \WP_User_Query([
232 309 'fields' => 'ID',
233 - 'meta_query' => array(
234 - array(
235 - 'key' => '_iwp_session_' . $this->importer->getId(),
236 - 'value' => $this->importer->getStatusId(),
237 - 'compare' => '!='
238 - )
239 - ),
310 + 'meta_query' => $meta_query,
240 311 'number' => -1
241 312 ]);
242 313
243 314 $results = $wp_user_query->get_results();
@@ -250,15 +321,19 @@
250 321 }
251 322
252 323 public function delete($id)
253 324 {
254 - require_once(ABSPATH . 'wp-admin/includes/user.php');
255 - 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 + }
256 331
257 332 $this->remove_session_tag($id, 'user');
258 333 }
259 334
260 - public function get_custom_field($id, $key, $single = true)
335 + public function get_custom_field($id, $key = '', $single = false)
261 336 {
262 337 return get_user_meta($id, $key, $single);
263 338 }
264 339
@@ -287,9 +362,9 @@
287 362
288 363 add_user_meta($user_id, $key, $value);
289 364 }
290 365
291 - public function update_custom_field($user_id, $key, $value)
366 + public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false)
292 367 {
293 368 // Stop double serialization
294 369 if (is_serialized($value)) {
295 370 $value = unserialize($value);
@@ -294,9 +369,9 @@
294 369 if (is_serialized($value)) {
295 370 $value = unserialize($value);
296 371 }
297 372
298 - update_user_meta($user_id, $key, $value);
373 + update_user_meta($id, $key, $value);
299 374 }
300 375
301 376 public function add_version_tag()
302 377 {