PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.14.24
Import WP – CSV & XML Import Export for WordPress v2.14.24
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 / UserMapper.php

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

385 lines 12.1 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\Exception\MapperException;
6 use ImportWP\Common\Importer\MapperInterface;
7 use ImportWP\Common\Importer\ParsedData;
8 use ImportWP\Common\Util\Logger;
9
10 class UserMapper extends AbstractMapper implements MapperInterface
11 {
12 /**
13 * Reserved Field Names for user table
14 * @var array
15 */
16 protected $_user_fields = array(
17 'ID',
18 'user_pass',
19 'user_login',
20 'user_nicename',
21 'user_url',
22 'user_email',
23 'display_name',
24 'nickname',
25 'first_name',
26 'last_name',
27 'description',
28 'rich_editing',
29 'user_registered',
30 'role',
31 );
32 protected $_user_required = array('user_login');
33
34 public function setup()
35 {
36 }
37
38 public function teardown()
39 {
40 }
41
42 public function exists(ParsedData $data)
43 {
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 }
51
52 $unique_field_found = false;
53
54 $query_args = array();
55 $search = array(); // store search values
56 $search_columns = array(); // store search columns
57
58 if (!$has_unique_field) {
59 foreach ($unique_fields as $field) {
60
61 // check all groups for a unique value
62 $unique_value = $this->find_unique_field_in_data($data, $field);
63
64 if (!empty($unique_value)) {
65 $has_unique_field = true;
66
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;
81 }
82 }
83 }
84
85 if (!$has_unique_field) {
86 throw new MapperException(__("No Unique fields present.", 'jc-importer'));
87 }
88
89 // create search
90 $query_args['search'] = implode(', ', $search);
91 $query_args['search_columns'] = $search_columns;
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));
95 $query = new \WP_User_Query($query_args);
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
142 if ($query->total_users > 1) {
143 $ids = [];
144 foreach ($query->results as $result) {
145 $ids[] = $result->ID;
146 }
147 throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $ids)));
148 }
149
150 if ($query->total_users == 1) {
151 $this->ID = $query->results[0]->ID;
152 return $this->ID;
153 }
154 return false;
155 }
156
157 public function insert(ParsedData $data)
158 {
159 $fields = $data->getData('default');
160
161 // ID Cant be inserted or updated, it is just used for reference
162 if (isset($fields['ID'])) {
163 unset($fields['ID']);
164 }
165
166 $core = [];
167 $meta = [];
168
169 foreach ($fields as $field_name => $field_value) {
170 if (in_array($field_name, $this->_user_fields)) {
171 $core[$field_name] = $field_value;
172 } else {
173 $meta[$field_name] = $field_value;
174 }
175 }
176
177 if (!isset($core['user_login']) || empty($core['user_login'])) {
178 throw new MapperException(__("No username present", 'jc-importer'));
179 }
180
181 if (!isset($core['user_pass']) || empty($core['user_pass'])) {
182 throw new MapperException(__("No password present", 'jc-importer'));
183 }
184
185 if (!empty($core['user_email']) && !is_email($core['user_email'])) {
186 throw new MapperException(sprintf(__("%s is not a valid email address", 'jc-importer'), strval($core['user_email'])));
187 }
188
189 Logger::debug('UserMapper::insert -wp_insert_user=' . wp_json_encode($core));
190
191 $this->ID = wp_insert_user($core);
192 if (is_wp_error($this->ID)) {
193 throw new MapperException($this->ID->get_error_message());
194 }
195
196 // TODO: Do we merge in the custom fields, or do we process that in post_process
197 $this->template->process($this->ID, $data, $this->importer);
198
199 // TODO: merge in custom fields from $data->getData('custom_fields');
200 $meta = array_merge($meta, $data->getData('custom_fields'));
201 Logger::debug('UserMapper::insert -meta=' . wp_json_encode($meta));
202
203 // update user meta
204 if (!empty($meta)) {
205 foreach ($meta as $key => $value) {
206 if (!in_array($key, $this->_user_fields)) {
207 if (is_array($value)) {
208 foreach ($value as $v) {
209 $this->add_custom_field($this->ID, $key, $v);
210 }
211 } else {
212 $this->update_custom_field($this->ID, $key, $value);
213 }
214 }
215 }
216 }
217
218 $this->add_version_tag();
219 $this->add_reference_tag($data);
220 $this->template->post_process($this->ID, $data);
221
222 return $this->ID;
223 }
224
225 public function update(ParsedData $data)
226 {
227 $fields = $data->getData('default');
228
229 // ID Cant be inserted or updated, it is just used for reference
230 if (isset($fields['ID'])) {
231 unset($fields['ID']);
232 }
233
234 $core = [];
235 $meta = [];
236
237 foreach ($fields as $field_name => $field_value) {
238 if (in_array($field_name, $this->_user_fields)) {
239 $core[$field_name] = $field_value;
240 } else {
241 $meta[$field_name] = $field_value;
242 }
243 }
244
245 Logger::debug('UserMapper::update -wp_update_user=' . wp_json_encode($core));
246
247 if (!empty($core)) {
248 $core['ID'] = $this->ID;
249 $result = wp_update_user($core);
250 if (is_wp_error($result)) {
251 throw new MapperException($result->get_error_message());
252 }
253 }
254
255 // TODO: Do we merge in the custom fields, or do we process that in post_process
256 $this->template->process($this->ID, $data, $this->importer);
257
258 $meta = array_merge($meta, $data->getData('custom_fields'));
259 Logger::debug('UserMapper::update -meta=' . wp_json_encode($meta));
260
261 // update user meta
262 if (!empty($meta)) {
263 foreach ($meta as $key => $value) {
264 if (!in_array($key, $this->_user_fields)) {
265 $this->clear_custom_field($this->ID, $key);
266 if (is_array($value)) {
267 foreach ($value as $v) {
268 $this->add_custom_field($this->ID, $key, $v);
269 }
270 } else {
271 $this->update_custom_field($this->ID, $key, $value);
272 }
273 }
274 }
275 }
276
277 $this->add_version_tag();
278 $this->add_reference_tag($data);
279 $this->template->post_process($this->ID, $data);
280
281 return $this->ID;
282 }
283
284 public function get_objects_for_removal()
285 {
286 if ($this->is_session_tag_enabled()) {
287 return $this->get_ids_without_session_tag('user');
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
308 $wp_user_query = new \WP_User_Query([
309 'fields' => 'ID',
310 'meta_query' => $meta_query,
311 'number' => -1
312 ]);
313
314 $results = $wp_user_query->get_results();
315 if (!empty($results)) {
316 return $results;
317 }
318 }
319
320 return false;
321 }
322
323 public function delete($id)
324 {
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 }
331
332 $this->remove_session_tag($id, 'user');
333 }
334
335 public function get_custom_field($id, $key = '', $single = false)
336 {
337 return get_user_meta($id, $key, $single);
338 }
339
340 /**
341 * Clear all post meta before adding custom field
342 */
343 public function clear_custom_field($user_id, $key)
344 {
345 delete_user_meta($user_id, $key);
346 }
347
348 /**
349 * Add custom field, allow for multiple records using the same key
350 *
351 * @param int $user_id
352 * @param string $key
353 * @param string $value
354 * @return void
355 */
356 public function add_custom_field($user_id, $key, $value)
357 {
358 // Stop double serialization
359 if (is_serialized($value)) {
360 $value = unserialize($value);
361 }
362
363 add_user_meta($user_id, $key, $value);
364 }
365
366 public function update_custom_field($id, $key, $value, $unique = false, $skip_permissions = false)
367 {
368 // Stop double serialization
369 if (is_serialized($value)) {
370 $value = unserialize($value);
371 }
372
373 update_user_meta($id, $key, $value);
374 }
375
376 public function add_version_tag()
377 {
378 if ($this->is_session_tag_enabled()) {
379 $this->add_session_tag('user');
380 } else {
381 update_user_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId());
382 }
383 }
384 }
385