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