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

311 lines 9.2 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\Importer\Template\TemplateManager;
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 $unique_fields = TemplateManager::get_template_unique_fields($this->template);
45
46 $unique_fields = $this->getUniqueIdentifiers($unique_fields);
47 $unique_fields = apply_filters('iwp/template_unique_fields', $unique_fields, $this->template, $this->importer);
48
49 $unique_field_found = false;
50
51 $default_group = $data->getData('default');
52 $query_args = array();
53 $meta_args = array();
54 $search = array(); // store search values
55 $search_columns = array(); // store search columns
56
57 $has_unique_field = false;
58
59 foreach ($unique_fields as $field) {
60
61 // check all groups for a unique value
62 $unique_value = $data->getValue($field, '*');
63 if (!empty($unique_value)) {
64 $has_unique_field = true;
65
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 );
76 }
77 $unique_field_found = $field;
78 break;
79 }
80 }
81
82 if (!$has_unique_field) {
83 throw new MapperException("No Unique fields present.");
84 }
85
86 // create search
87 $query_args['search'] = implode(', ', $search);
88 $query_args['search_columns'] = $search_columns;
89 $query_args['meta_query'] = $meta_args;
90 $query = new \WP_User_Query($query_args);
91
92 if ($query->total_users > 1) {
93 $ids = [];
94 foreach ($query->results as $result) {
95 $ids[] = $result->ID;
96 }
97 throw new MapperException("Record is not unique: " . $unique_field_found . ", Matching Ids: (" . implode(', ', $ids) . ").");
98 }
99
100 if ($query->total_users == 1) {
101 $this->ID = $query->results[0]->ID;
102 return $this->ID;
103 }
104 return false;
105 }
106
107 public function insert(ParsedData $data)
108 {
109 $fields = $data->getData('default');
110
111 // ID Cant be inserted or updated, it is just used for reference
112 if (isset($fields['ID'])) {
113 unset($fields['ID']);
114 }
115
116 $core = [];
117 $meta = [];
118
119 foreach ($fields as $field_name => $field_value) {
120 if (in_array($field_name, $this->_user_fields)) {
121 $core[$field_name] = $field_value;
122 } else {
123 $meta[$field_name] = $field_value;
124 }
125 }
126
127 if (!isset($core['user_login']) || empty($core['user_login'])) {
128 throw new MapperException("No username present");
129 }
130
131 if (!isset($core['user_pass']) || empty($core['user_pass'])) {
132 throw new MapperException("No password present");
133 }
134
135 if (!empty($core['user_email']) && !is_email($core['user_email'])) {
136 throw new MapperException(strval($core['user_email']) . " is not a valid email address");
137 }
138
139 $this->ID = wp_insert_user($core);
140 if (is_wp_error($this->ID)) {
141 throw new MapperException($this->ID->get_error_message());
142 }
143
144 // TODO: Do we merge in the custom fields, or do we process that in post_process
145 $this->template->process($this->ID, $data, $this->importer);
146
147 // TODO: merge in custom fields from $data->getData('custom_fields');
148 $meta = array_merge($meta, $data->getData('custom_fields'));
149
150 // update user meta
151 if (!empty($meta)) {
152 foreach ($meta as $key => $value) {
153 if (!in_array($key, $this->_user_fields)) {
154 if (is_array($value)) {
155 $this->clear_custom_field($this->ID, $key);
156 foreach ($value as $v) {
157 $this->add_custom_field($this->ID, $key, $v);
158 }
159 } else {
160 $this->update_custom_field($this->ID, $key, $value);
161 }
162 }
163 }
164 }
165
166 $this->add_version_tag();
167 $this->template->post_process($this->ID, $data);
168
169 return $this->ID;
170 }
171
172 public function update(ParsedData $data)
173 {
174 $fields = $data->getData('default');
175
176 // ID Cant be inserted or updated, it is just used for reference
177 if (isset($fields['ID'])) {
178 unset($fields['ID']);
179 }
180
181 $core = [];
182 $meta = [];
183
184 foreach ($fields as $field_name => $field_value) {
185 if (in_array($field_name, $this->_user_fields)) {
186 $core[$field_name] = $field_value;
187 } else {
188 $meta[$field_name] = $field_value;
189 }
190 }
191
192 if (!empty($core)) {
193 $core['ID'] = $this->ID;
194 $result = wp_update_user($core);
195 if (is_wp_error($result)) {
196 throw new MapperException($result->get_error_message());
197 }
198 }
199
200 // TODO: Do we merge in the custom fields, or do we process that in post_process
201 $this->template->process($this->ID, $data, $this->importer);
202
203 $meta = array_merge($meta, $data->getData('custom_fields'));
204
205 // update user meta
206 if (!empty($meta)) {
207 foreach ($meta as $key => $value) {
208 if (!in_array($key, $this->_user_fields)) {
209 if (is_array($value)) {
210 $this->clear_custom_field($this->ID, $key);
211 foreach ($value as $v) {
212 $this->add_custom_field($this->ID, $key, $v);
213 }
214 } else {
215 $this->update_custom_field($this->ID, $key, $value);
216 }
217 }
218 }
219 }
220
221 $this->add_version_tag();
222 $this->template->post_process($this->ID, $data);
223
224 return $this->ID;
225 }
226
227 public function get_objects_for_removal()
228 {
229 if ($this->is_session_tag_enabled()) {
230 return $this->get_ids_without_session_tag('user');
231 } else {
232 $wp_user_query = new \WP_User_Query([
233 'fields' => 'ID',
234 'meta_query' => array(
235 array(
236 'key' => '_iwp_session_' . $this->importer->getId(),
237 'value' => $this->importer->getStatusId(),
238 'compare' => '!='
239 )
240 ),
241 'number' => -1
242 ]);
243
244 $results = $wp_user_query->get_results();
245 if (!empty($results)) {
246 return $results;
247 }
248 }
249
250 return false;
251 }
252
253 public function delete($id)
254 {
255 require_once(ABSPATH . 'wp-admin/includes/user.php');
256 wp_delete_user($id);
257
258 $this->remove_session_tag($id, 'user');
259 }
260
261 public function get_custom_field($id, $key, $single = true)
262 {
263 return get_user_meta($id, $key, $single);
264 }
265
266 /**
267 * Clear all post meta before adding custom field
268 */
269 public function clear_custom_field($user_id, $key)
270 {
271 delete_user_meta($user_id, $key);
272 }
273
274 /**
275 * Add custom field, allow for multiple records using the same key
276 *
277 * @param int $user_id
278 * @param string $key
279 * @param string $value
280 * @return void
281 */
282 public function add_custom_field($user_id, $key, $value)
283 {
284 // Stop double serialization
285 if (is_serialized($value)) {
286 $value = unserialize($value);
287 }
288
289 add_user_meta($user_id, $key, $value);
290 }
291
292 public function update_custom_field($user_id, $key, $value)
293 {
294 // Stop double serialization
295 if (is_serialized($value)) {
296 $value = unserialize($value);
297 }
298
299 update_user_meta($user_id, $key, $value);
300 }
301
302 public function add_version_tag()
303 {
304 if ($this->is_session_tag_enabled()) {
305 $this->add_session_tag('user');
306 } else {
307 update_user_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId());
308 }
309 }
310 }
311