df223fbc by Marty Penner

Refactored search and merge users 'sub'-functions and set old user status of ter…

…minated, merged_to -> new user id, and datetime_merged -> now
1 parent 0eca07ae
......@@ -967,6 +967,8 @@ class Actions {
}
if ($do_search == 'yes') {
$username = $_POST['search_user_' . $direction];
// Don't search for an empty string as it will return all results
if (empty($_POST['search_user_' . $direction])) {
die(json_encode(array(
......@@ -979,11 +981,17 @@ class Actions {
$html = '<div><ul class="user-list"><h4>Username</h4><h4>Member ID</h4><h4>Email</h4>';
$i = 0;
$query = "SELECT ID, user_login, user_email FROM $wpdb->users WHERE user_login LIKE '" . $wpdb->escape($_POST['search_user_' . $direction]) . "%'";
$user_list = $wpdb->get_results($query, ARRAY_A);
$query = "SELECT ID, user_login, user_email FROM $wpdb->users WHERE user_login LIKE '" . $wpdb->escape($username) . "%'";
$result = $wpdb->get_results($query, ARRAY_A);
if (! empty($result)) {
foreach ($result as $user) {
// Check if user has status of 'terminated'
$user_status = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key LIKE 'status'", $user['ID']));
if ($user_status == 'terminated') {
continue;
}
if (! empty($user_list)) {
foreach ($user_list as $user) {
// Get the member ID as well, but don't display it if it's the same as 'user_login'
$member_id = get_user_meta($user['ID'], 'member_id', TRUE);
if ($member_id == $user['user_login']) {
......@@ -1029,54 +1037,33 @@ HTML;
)));
}
$unchanged_html = '';
$html = '';
$remove_user_html = '';
$from_user = new User\Account($_POST['merge_user_from']);
$to_user = new User\Account($_POST['merge_user_to']);
$to_user_array = array();
$skipped_fields = array(
'ID',
'id',
'user_login',
'user_email'
'user_email',
'member_id'
);
// Get rid of the unnecessary data object in the current user object
unset($from_user->data);
foreach ($skipped_fields as $field_name) {
$unchanged_html .= '<li>' . $field_name . ': ' . $from_user->$field_name . '</li>';
}
$unchanged_html = '<h6>Unchanged:</h6><ul>' . $unchanged_html . '</ul>';
foreach ($from_user as $key => $val) {
if (in_array($key, $skipped_fields)) {
continue;
}
// Special case: only transfer member_id if it's not blank
if ($key == 'member_id' && empty($val)) {
continue;
}
// Place all allowed fields into the new user array
$to_user_array[$key] = $val;
}
// Make sure we're updating the right user ID
$to_user_array['ID'] = $to_user->ID;
$to_user_array['id'] = $to_user->id;
// Perform the update
_update_user($to_user_array);
// Delete the old user if the checkbox is set
if (isset($_POST['delete_old_user']) && $_POST['delete_old_user'] == 'yes') {
$username = $from_user->user_login;
if (@wp_delete_user($from_user->ID)) {
$remove_user_html = "<h6>Deleted user $username.</h6>";
} else {
$remove_user_html = '<h6>Could not delete user.</h6>';
}
}
// Make sure we're updating the right user ID and perform update
update_user_meta($to_user->ID, $key, $val);
// Build a set of nested html lists that display the fields (goes 3 levels deep; bloody ugly, but works)
foreach ($to_user_array as $key => $val) {
// Build a set of nested html lists that display the fields (goes 3 levels deep; bloody ugly, but works)
$html_l2 = '';
if (is_array($val)) {
// Rinse and repeat
......@@ -1098,18 +1085,12 @@ HTML;
}
}
if (! empty($remove_user_html)) {
$initial = $remove_user_html;
} else {
$initial = $unchanged_html;
}
$html = //'<h4><a href="/wp-admin/admin-ajax.php?ajax=yes&action=build_user_remove&uid=' .
//$from_user->ID .
//'" rel="' .
//$from_user->ID .
//'" class="remove-user">Remove old user</a></h4>' .
$initial .
'<h6>Changed:</h6><ul>' .
// Set a few meta values for the old user
update_user_meta($from_user->ID, 'status', 'terminated');
update_user_meta($from_user->ID, 'merged_to', $to_user->ID);
update_user_meta($from_user->ID, 'datetime_merged', time());
$html = '<h6>Changed:</h6><ul>' .
$html .
'</ul>';
......
......@@ -130,9 +130,6 @@ h4 {
<input type="hidden" value="from" name="which_user_search" />
<input type="hidden" value="no" name="do_search" />
<input type="hidden" value="no" name="do_merge" />
<input type="hidden" value="0" name="uid" />
<input type="checkbox" value="yes" name="delete_old_user" />
<label for="delete_old_user">Delete old user?</label>
<input type="button" value="Merge Users" name="btn_merge" />
</div>
......