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