Refactored search and merge users 'sub'-functions and set old user status of ter…
…minated, merged_to -> new user id, and datetime_merged -> now
Showing
2 changed files
with
27 additions
and
49 deletions
| ... | @@ -967,6 +967,8 @@ class Actions { | ... | @@ -967,6 +967,8 @@ class Actions { |
| 967 | } | 967 | } |
| 968 | 968 | ||
| 969 | if ($do_search == 'yes') { | 969 | if ($do_search == 'yes') { |
| 970 | $username = $_POST['search_user_' . $direction]; | ||
| 971 | |||
| 970 | // Don't search for an empty string as it will return all results | 972 | // Don't search for an empty string as it will return all results |
| 971 | if (empty($_POST['search_user_' . $direction])) { | 973 | if (empty($_POST['search_user_' . $direction])) { |
| 972 | die(json_encode(array( | 974 | die(json_encode(array( |
| ... | @@ -979,11 +981,17 @@ class Actions { | ... | @@ -979,11 +981,17 @@ class Actions { |
| 979 | $html = '<div><ul class="user-list"><h4>Username</h4><h4>Member ID</h4><h4>Email</h4>'; | 981 | $html = '<div><ul class="user-list"><h4>Username</h4><h4>Member ID</h4><h4>Email</h4>'; |
| 980 | $i = 0; | 982 | $i = 0; |
| 981 | 983 | ||
| 982 | $query = "SELECT ID, user_login, user_email FROM $wpdb->users WHERE user_login LIKE '" . $wpdb->escape($_POST['search_user_' . $direction]) . "%'"; | 984 | $query = "SELECT ID, user_login, user_email FROM $wpdb->users WHERE user_login LIKE '" . $wpdb->escape($username) . "%'"; |
| 983 | $user_list = $wpdb->get_results($query, ARRAY_A); | 985 | $result = $wpdb->get_results($query, ARRAY_A); |
| 986 | |||
| 987 | if (! empty($result)) { | ||
| 988 | foreach ($result as $user) { | ||
| 989 | // Check if user has status of 'terminated' | ||
| 990 | $user_status = $wpdb->get_var($wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key LIKE 'status'", $user['ID'])); | ||
| 991 | if ($user_status == 'terminated') { | ||
| 992 | continue; | ||
| 993 | } | ||
| 984 | 994 | ||
| 985 | if (! empty($user_list)) { | ||
| 986 | foreach ($user_list as $user) { | ||
| 987 | // Get the member ID as well, but don't display it if it's the same as 'user_login' | 995 | // Get the member ID as well, but don't display it if it's the same as 'user_login' |
| 988 | $member_id = get_user_meta($user['ID'], 'member_id', TRUE); | 996 | $member_id = get_user_meta($user['ID'], 'member_id', TRUE); |
| 989 | if ($member_id == $user['user_login']) { | 997 | if ($member_id == $user['user_login']) { |
| ... | @@ -1029,54 +1037,33 @@ HTML; | ... | @@ -1029,54 +1037,33 @@ HTML; |
| 1029 | ))); | 1037 | ))); |
| 1030 | } | 1038 | } |
| 1031 | 1039 | ||
| 1032 | $unchanged_html = ''; | ||
| 1033 | $html = ''; | 1040 | $html = ''; |
| 1034 | $remove_user_html = ''; | ||
| 1035 | $from_user = new User\Account($_POST['merge_user_from']); | 1041 | $from_user = new User\Account($_POST['merge_user_from']); |
| 1036 | $to_user = new User\Account($_POST['merge_user_to']); | 1042 | $to_user = new User\Account($_POST['merge_user_to']); |
| 1037 | $to_user_array = array(); | ||
| 1038 | $skipped_fields = array( | 1043 | $skipped_fields = array( |
| 1044 | 'ID', | ||
| 1045 | 'id', | ||
| 1039 | 'user_login', | 1046 | 'user_login', |
| 1040 | 'user_email' | 1047 | 'user_email', |
| 1048 | 'member_id' | ||
| 1041 | ); | 1049 | ); |
| 1050 | // Get rid of the unnecessary data object in the current user object | ||
| 1042 | unset($from_user->data); | 1051 | unset($from_user->data); |
| 1043 | 1052 | ||
| 1044 | foreach ($skipped_fields as $field_name) { | ||
| 1045 | $unchanged_html .= '<li>' . $field_name . ': ' . $from_user->$field_name . '</li>'; | ||
| 1046 | } | ||
| 1047 | $unchanged_html = '<h6>Unchanged:</h6><ul>' . $unchanged_html . '</ul>'; | ||
| 1048 | |||
| 1049 | foreach ($from_user as $key => $val) { | 1053 | foreach ($from_user as $key => $val) { |
| 1050 | if (in_array($key, $skipped_fields)) { | 1054 | if (in_array($key, $skipped_fields)) { |
| 1051 | continue; | 1055 | continue; |
| 1052 | } | 1056 | } |
| 1057 | |||
| 1053 | // Special case: only transfer member_id if it's not blank | 1058 | // Special case: only transfer member_id if it's not blank |
| 1054 | if ($key == 'member_id' && empty($val)) { | 1059 | if ($key == 'member_id' && empty($val)) { |
| 1055 | continue; | 1060 | continue; |
| 1056 | } | 1061 | } |
| 1057 | 1062 | ||
| 1058 | // Place all allowed fields into the new user array | 1063 | // Make sure we're updating the right user ID and perform update |
| 1059 | $to_user_array[$key] = $val; | 1064 | update_user_meta($to_user->ID, $key, $val); |
| 1060 | } | ||
| 1061 | // Make sure we're updating the right user ID | ||
| 1062 | $to_user_array['ID'] = $to_user->ID; | ||
| 1063 | $to_user_array['id'] = $to_user->id; | ||
| 1064 | |||
| 1065 | // Perform the update | ||
| 1066 | _update_user($to_user_array); | ||
| 1067 | |||
| 1068 | // Delete the old user if the checkbox is set | ||
| 1069 | if (isset($_POST['delete_old_user']) && $_POST['delete_old_user'] == 'yes') { | ||
| 1070 | $username = $from_user->user_login; | ||
| 1071 | if (@wp_delete_user($from_user->ID)) { | ||
| 1072 | $remove_user_html = "<h6>Deleted user $username.</h6>"; | ||
| 1073 | } else { | ||
| 1074 | $remove_user_html = '<h6>Could not delete user.</h6>'; | ||
| 1075 | } | ||
| 1076 | } | ||
| 1077 | 1065 | ||
| 1078 | // Build a set of nested html lists that display the fields (goes 3 levels deep; bloody ugly, but works) | 1066 | // Build a set of nested html lists that display the fields (goes 3 levels deep; bloody ugly, but works) |
| 1079 | foreach ($to_user_array as $key => $val) { | ||
| 1080 | $html_l2 = ''; | 1067 | $html_l2 = ''; |
| 1081 | if (is_array($val)) { | 1068 | if (is_array($val)) { |
| 1082 | // Rinse and repeat | 1069 | // Rinse and repeat |
| ... | @@ -1098,18 +1085,12 @@ HTML; | ... | @@ -1098,18 +1085,12 @@ HTML; |
| 1098 | } | 1085 | } |
| 1099 | } | 1086 | } |
| 1100 | 1087 | ||
| 1101 | if (! empty($remove_user_html)) { | 1088 | // Set a few meta values for the old user |
| 1102 | $initial = $remove_user_html; | 1089 | update_user_meta($from_user->ID, 'status', 'terminated'); |
| 1103 | } else { | 1090 | update_user_meta($from_user->ID, 'merged_to', $to_user->ID); |
| 1104 | $initial = $unchanged_html; | 1091 | update_user_meta($from_user->ID, 'datetime_merged', time()); |
| 1105 | } | 1092 | |
| 1106 | $html = //'<h4><a href="/wp-admin/admin-ajax.php?ajax=yes&action=build_user_remove&uid=' . | 1093 | $html = '<h6>Changed:</h6><ul>' . |
| 1107 | //$from_user->ID . | ||
| 1108 | //'" rel="' . | ||
| 1109 | //$from_user->ID . | ||
| 1110 | //'" class="remove-user">Remove old user</a></h4>' . | ||
| 1111 | $initial . | ||
| 1112 | '<h6>Changed:</h6><ul>' . | ||
| 1113 | $html . | 1094 | $html . |
| 1114 | '</ul>'; | 1095 | '</ul>'; |
| 1115 | 1096 | ... | ... |
| ... | @@ -130,9 +130,6 @@ h4 { | ... | @@ -130,9 +130,6 @@ h4 { |
| 130 | <input type="hidden" value="from" name="which_user_search" /> | 130 | <input type="hidden" value="from" name="which_user_search" /> |
| 131 | <input type="hidden" value="no" name="do_search" /> | 131 | <input type="hidden" value="no" name="do_search" /> |
| 132 | <input type="hidden" value="no" name="do_merge" /> | 132 | <input type="hidden" value="no" name="do_merge" /> |
| 133 | <input type="hidden" value="0" name="uid" /> | ||
| 134 | <input type="checkbox" value="yes" name="delete_old_user" /> | ||
| 135 | <label for="delete_old_user">Delete old user?</label> | ||
| 136 | <input type="button" value="Merge Users" name="btn_merge" /> | 133 | <input type="button" value="Merge Users" name="btn_merge" /> |
| 137 | </div> | 134 | </div> |
| 138 | 135 | ... | ... |
-
Please register or sign in to post a comment