Added ability to merge users from admin page (still bugs when deleting user)
Showing
2 changed files
with
369 additions
and
1 deletions
| ... | @@ -160,6 +160,10 @@ function _get_signups() { | ... | @@ -160,6 +160,10 @@ function _get_signups() { |
| 160 | return $users; | 160 | return $users; |
| 161 | } | 161 | } |
| 162 | 162 | ||
| 163 | function merge_users() { | ||
| 164 | require_once(__DIR__ . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'merge_users.php'); | ||
| 165 | } | ||
| 166 | |||
| 163 | class ProfileValidation extends Common\Validation { | 167 | class ProfileValidation extends Common\Validation { |
| 164 | public function prefix($val) { | 168 | public function prefix($val) { |
| 165 | update_user_meta($_POST['uid'], __FUNCTION__, $val); | 169 | update_user_meta($_POST['uid'], __FUNCTION__, $val); |
| ... | @@ -943,10 +947,169 @@ class Actions { | ... | @@ -943,10 +947,169 @@ class Actions { |
| 943 | 947 | ||
| 944 | } | 948 | } |
| 945 | 949 | ||
| 950 | public static function wp_ajax_admin_search_merge_users() { | ||
| 951 | global $wpdb; | ||
| 952 | |||
| 953 | $direction = $_POST['which_user_search']; | ||
| 954 | $do_search = $_POST['do_search']; | ||
| 955 | $do_merge = $_POST['do_merge']; | ||
| 956 | |||
| 957 | // Make sure we have sane 'defaults' | ||
| 958 | if ($direction != 'from' && $direction != 'to') { | ||
| 959 | $direction = 'from'; | ||
| 960 | } | ||
| 961 | if ($do_search != 'yes' && $do_search != 'no') { | ||
| 962 | $do_search = 'yes'; | ||
| 963 | } | ||
| 964 | if ($do_merge != 'yes' && $do_merge != 'no') { | ||
| 965 | $do_merge = 'yes'; | ||
| 966 | } | ||
| 967 | |||
| 968 | if ($do_search == 'yes') { | ||
| 969 | // Don't search for an empty string as it will return all results | ||
| 970 | if (empty($_POST['search_user_' . $direction])) { | ||
| 971 | die(json_encode(array( | ||
| 972 | 'success' => 'false', | ||
| 973 | 'msg' => 'You must enter a value to search for.' | ||
| 974 | ))); | ||
| 975 | } | ||
| 976 | |||
| 977 | // Initialize | ||
| 978 | $html = '<div><ul class="user-list"><h4>Username</h4><h4>Member ID</h4><h4>Email</h4>'; | ||
| 979 | $i = 0; | ||
| 980 | |||
| 981 | $query = "SELECT ID, user_login, user_email FROM $wpdb->users WHERE user_login LIKE '" . $wpdb->escape($_POST['search_user_' . $direction]) . "%'"; | ||
| 982 | $user_list = $wpdb->get_results($query, ARRAY_A); | ||
| 983 | |||
| 984 | if (! empty($user_list)) { | ||
| 985 | foreach ($user_list as $user) { | ||
| 986 | // Get the member ID as well, but don't display it if it's the same as 'user_login' | ||
| 987 | $member_id = get_user_meta($user['ID'], 'member_id', TRUE); | ||
| 988 | if ($member_id == $user['user_login']) { | ||
| 989 | $member_id = '""'; | ||
| 990 | } | ||
| 991 | |||
| 992 | $html .= <<<HTML | ||
| 993 | |||
| 994 | <li> | ||
| 995 | <input type="radio" name="merge_user_{$direction}" value="{$user['user_login']}" /> | ||
| 996 | <label for="merge_user_{$direction}">{$user['user_login']}</label> | ||
| 997 | <label for="merge_user_{$direction}">{$member_id}</label> | ||
| 998 | <label for="merge_user_{$direction}">{$user['user_email']}</label> | ||
| 999 | </li> | ||
| 1000 | HTML; | ||
| 1001 | |||
| 1002 | $i++; | ||
| 1003 | } | ||
| 1004 | } else { | ||
| 1005 | die(json_encode(array( | ||
| 1006 | 'success' => 'false', | ||
| 1007 | 'msg' => 'No users found matching that criteria.' | ||
| 1008 | ))); | ||
| 1009 | } | ||
| 1010 | $html .= '</ul></div>'; | ||
| 1011 | |||
| 1012 | $return = array( | ||
| 1013 | 'success' => 'true', | ||
| 1014 | 'direction' => $direction, | ||
| 1015 | 'html' => $html | ||
| 1016 | ); | ||
| 1017 | } else if ($do_merge == 'yes') { | ||
| 1018 | if (empty($_POST['merge_user_from'])) { | ||
| 1019 | die(json_encode(array( | ||
| 1020 | 'success' => 'false', | ||
| 1021 | 'msg' => 'You must select a user to merge from.' | ||
| 1022 | ))); | ||
| 1023 | } | ||
| 1024 | if (empty($_POST['merge_user_to'])) { | ||
| 1025 | die(json_encode(array( | ||
| 1026 | 'success' => 'false', | ||
| 1027 | 'msg' => 'You must select a user to merge to.' | ||
| 1028 | ))); | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | $unchanged_html = ''; | ||
| 1032 | $from_user = new User\Account($_POST['merge_user_from']); | ||
| 1033 | $to_user = new User\Account($_POST['merge_user_to']); | ||
| 1034 | $to_user_array = array(); | ||
| 1035 | $skipped_fields = array( | ||
| 1036 | 'user_login', | ||
| 1037 | 'user_email' | ||
| 1038 | ); | ||
| 1039 | unset($from_user->data); | ||
| 1040 | |||
| 1041 | foreach ($skipped_fields as $field_name) { | ||
| 1042 | $unchanged_html .= '<li>' . $field_name . ': ' . $from_user->$field_name . '</li>'; | ||
| 1043 | } | ||
| 1044 | $unchanged_html = '<h6>Unchanged:</h6><ul>' . $unchanged_html . '</ul>'; | ||
| 1045 | |||
| 1046 | foreach ($from_user as $key => $val) { | ||
| 1047 | if (in_array($key, $skipped_fields)) { | ||
| 1048 | continue; | ||
| 1049 | } | ||
| 1050 | // Special case: only transfer member_id if it's not blank | ||
| 1051 | if ($key == 'member_id' && empty($val)) { | ||
| 1052 | continue; | ||
| 1053 | } | ||
| 1054 | |||
| 1055 | // Place all allowed fields into the new user array | ||
| 1056 | $to_user_array[$key] = $val; | ||
| 1057 | } | ||
| 1058 | // Make sure we're updating the right user ID | ||
| 1059 | $to_user_array['ID'] = $to_user->ID; | ||
| 1060 | $to_user_array['id'] = $to_user->id; | ||
| 1061 | |||
| 1062 | // Perform the update | ||
| 1063 | _update_user($to_user_array); | ||
| 1064 | |||
| 1065 | // Delete the old user if the checkbox is set | ||
| 1066 | if (isset($_POST['delete_old_user']) && $_POST['delete_old_user'] == 'yes') { | ||
| 1067 | @wp_delete_user($from_user->ID); | ||
| 1068 | } | ||
| 1069 | |||
| 1070 | // Build a set of nested html lists that display the fields (goes 3 levels deep; bloody ugly, but works) | ||
| 1071 | $html = ''; | ||
| 1072 | foreach ($to_user_array as $key => $val) { | ||
| 1073 | $html_l2 = ''; | ||
| 1074 | if (is_array($val)) { | ||
| 1075 | // Rinse and repeat | ||
| 1076 | foreach ($val as $subk => $subv) { | ||
| 1077 | $html_l3 = ''; | ||
| 1078 | if (is_array($subv)) { | ||
| 1079 | // And yet again... | ||
| 1080 | foreach ($subv as $k3 => $v3) { | ||
| 1081 | $html_l3 .= "<li>$k3 = $v3</li>"; | ||
| 1082 | } | ||
| 1083 | $html_l2 .= "<li><ul>$subk: [$html_l3]</ul></li>"; | ||
| 1084 | } else { | ||
| 1085 | $html_l2 .= "<li>$subk = $subv</li>"; | ||
| 1086 | } | ||
| 1087 | } | ||
| 1088 | $html .= "<li><ul>$key: [$html_l2]</ul></li>"; | ||
| 1089 | } else { | ||
| 1090 | $html .= "<li>$key: $val</li>"; | ||
| 1091 | } | ||
| 1092 | } | ||
| 1093 | $html = $unchanged_html . '<h6>Changed:</h6><ul>' . $html . '</ul>'; | ||
| 1094 | |||
| 1095 | $return = array( | ||
| 1096 | 'success' => 'true', | ||
| 1097 | 'fields' => $html | ||
| 1098 | ); | ||
| 1099 | } else { | ||
| 1100 | $return = array( | ||
| 1101 | 'success' => 'false', | ||
| 1102 | 'msg' => 'Incorrect action. Please contact the webmaster.' | ||
| 1103 | ); | ||
| 1104 | } | ||
| 1105 | die(json_encode($return)); | ||
| 1106 | } | ||
| 1107 | |||
| 946 | public static function admin_menu() { | 1108 | public static function admin_menu() { |
| 947 | add_menu_page('CBV Users','CBV Users',CAPABILITY,'cbv_users',__NAMESPACE__ . '\display_users',null,3 ); | 1109 | add_menu_page('CBV Users','CBV Users',CAPABILITY,'cbv_users',__NAMESPACE__ . '\display_users',null,3 ); |
| 948 | add_submenu_page('cbv_users','New User', 'New User',CAPABILITY,'cbv_users_create',__NAMESPACE__ . '\create_user'); | 1110 | add_submenu_page('cbv_users','New User', 'New User',CAPABILITY,'cbv_users_create',__NAMESPACE__ . '\create_user'); |
| 949 | add_submenu_page('cbv_users','New User', 'Awaiting Validation',CAPABILITY,'cbv_users_signups',__NAMESPACE__ . '\signups'); | 1111 | add_submenu_page('cbv_users','New User', 'Awaiting Validation',CAPABILITY,'cbv_users_signups',__NAMESPACE__ . '\signups'); |
| 1112 | add_submenu_page('cbv_users', 'Merge Users', 'Merge Users', CAPABILITY, 'cbv_users_merge', __NAMESPACE__ . '\merge_users'); | ||
| 950 | } | 1113 | } |
| 951 | 1114 | ||
| 952 | public static function admin_init() { | 1115 | public static function admin_init() { |
| ... | @@ -960,4 +1123,4 @@ class Vars { | ... | @@ -960,4 +1123,4 @@ class Vars { |
| 960 | public static $validation; | 1123 | public static $validation; |
| 961 | } | 1124 | } |
| 962 | 1125 | ||
| 963 | ?> | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
| 1126 | ?> | ... | ... |
com/UserManager/views/merge_users.php
0 → 100644
| 1 | <?php | ||
| 2 | namespace Tz\WordPress\Tools\UserManager; | ||
| 3 | |||
| 4 | use Tz, Tz\Common; | ||
| 5 | use Tz\WordPress\CBV; | ||
| 6 | use Tz\WordPress\CBV\User; | ||
| 7 | use Tz\WordPress\UAM; | ||
| 8 | use Tz\WordPress\Tools; | ||
| 9 | use Tz\WordPress\Tools\Auth; | ||
| 10 | use Exception, StdClass; | ||
| 11 | use WP_User; | ||
| 12 | |||
| 13 | // Make sure some defaults are set because we use these values as is in the input boxes | ||
| 14 | if (! isset($_POST['search_user_from'])) { | ||
| 15 | $_POST['search_user_from'] = ''; | ||
| 16 | } | ||
| 17 | if (! isset($_POST['search_user_to'])) { | ||
| 18 | $_POST['search_user_to'] = ''; | ||
| 19 | } | ||
| 20 | |||
| 21 | ?> | ||
| 22 | <style> | ||
| 23 | |||
| 24 | #post-body { | ||
| 25 | -moz-border-radius-bottomleft: 6px; | ||
| 26 | -moz-border-radius-bottomright: 6px; | ||
| 27 | -moz-border-radius-topright: 6px; | ||
| 28 | background: none repeat scroll 0 0 #FFFFFF; | ||
| 29 | border-width: 1px 1px 1px; | ||
| 30 | padding: 10px; | ||
| 31 | } | ||
| 32 | |||
| 33 | h4 { | ||
| 34 | float: left; | ||
| 35 | padding-right: 1em; | ||
| 36 | } | ||
| 37 | |||
| 38 | .merge-users { | ||
| 39 | float: left; | ||
| 40 | padding-right: 2em; | ||
| 41 | } | ||
| 42 | |||
| 43 | .merge-users label { | ||
| 44 | float: left; | ||
| 45 | font-weight: bold; | ||
| 46 | padding-right: 1em; | ||
| 47 | } | ||
| 48 | |||
| 49 | .merge-users input { | ||
| 50 | float: left; | ||
| 51 | } | ||
| 52 | |||
| 53 | .clear { | ||
| 54 | float: left; | ||
| 55 | clear: both; | ||
| 56 | } | ||
| 57 | |||
| 58 | .user-list, .user-list li { | ||
| 59 | float: left; | ||
| 60 | clear: both; | ||
| 61 | } | ||
| 62 | |||
| 63 | .changed { | ||
| 64 | float: left; | ||
| 65 | width: 250px; | ||
| 66 | margin-top: 5px; | ||
| 67 | border: 1px solid green; | ||
| 68 | clear: both; | ||
| 69 | display: none; | ||
| 70 | } | ||
| 71 | |||
| 72 | .changed h6 { | ||
| 73 | padding: 0px; | ||
| 74 | margin: 0px 0px 10px 0px; | ||
| 75 | font-size: 11px; | ||
| 76 | text-transform: uppercase; | ||
| 77 | } | ||
| 78 | |||
| 79 | .changed ul { | ||
| 80 | margin: 0; | ||
| 81 | padding: 0; | ||
| 82 | } | ||
| 83 | |||
| 84 | .changed ul li { | ||
| 85 | margin: 0px 0px 3px 0px; | ||
| 86 | padding: 0px; | ||
| 87 | font-size: 11px; | ||
| 88 | background: none; | ||
| 89 | } | ||
| 90 | |||
| 91 | </style> | ||
| 92 | |||
| 93 | <div class="wrap"> | ||
| 94 | |||
| 95 | <div id="icon-users" class="icon32"><br /></div> | ||
| 96 | <h2>Merging CBV Users...</h2> | ||
| 97 | |||
| 98 | <div class="validation-errors" style="display:none; margin-top:10px;"> | ||
| 99 | <div class="error-wrap"> | ||
| 100 | <h6>OOPS...</h6> | ||
| 101 | <ul></ul> | ||
| 102 | </div> | ||
| 103 | </div> | ||
| 104 | |||
| 105 | <div id="post-body" style="padding:0px 10px 15px 10px;"> | ||
| 106 | <form method="post" id="admin-search-merge-form"> | ||
| 107 | |||
| 108 | <div class="merge-users"> | ||
| 109 | <label for="search_user_from">Merge FROM User:</label> | ||
| 110 | <input class="clear" type="text" name="search_user_from" value="<?php echo $_POST['search_user_from'] ?>" /> | ||
| 111 | <input type="button" name="btn_user_from" value="Search" /> | ||
| 112 | <div id="user-list-from"></div> | ||
| 113 | </div> | ||
| 114 | |||
| 115 | <div class="merge-users"> | ||
| 116 | <label for="search_user_to">Merge TO User:</label> | ||
| 117 | <input class="clear" type="text" name="search_user_to" value="<?php echo $_POST['search_user_to'] ?>" /> | ||
| 118 | <input type="button" name="btn_user_to" value="Search" /> | ||
| 119 | <div id="user-list-to"></div> | ||
| 120 | </div> | ||
| 121 | |||
| 122 | <div id="field-list" class="changed"> | ||
| 123 | <h6>Success!</h6> | ||
| 124 | <ul></ul> | ||
| 125 | </div> | ||
| 126 | |||
| 127 | <div style="clear:both;"></div> | ||
| 128 | |||
| 129 | <div style="margin-top:10px;padding-top:5px; border-top:1px solid #e8e8e8;"> | ||
| 130 | <input type="hidden" value="from" name="which_user_search" /> | ||
| 131 | <input type="hidden" value="no" name="do_search" /> | ||
| 132 | <input type="hidden" value="no" name="do_merge" /> | ||
| 133 | <input type="checkbox" value="yes" name="delete_old_user" /> | ||
| 134 | <label for="delete_old_user">Delete old user?</label> | ||
| 135 | <input type="button" value="Merge Users" name="btn_merge" /> | ||
| 136 | </div> | ||
| 137 | |||
| 138 | </form> | ||
| 139 | |||
| 140 | </div> | ||
| 141 | |||
| 142 | </div> | ||
| 143 | |||
| 144 | <script src="<?php echo Tools\url('../UserManager.js', __FILE__);?>" type="text/javascript"></script> | ||
| 145 | <script type="text/javascript"> | ||
| 146 | var $ = jQuery; | ||
| 147 | var $which_user_search = $('[name=which_user_search]'); | ||
| 148 | var $do_search = $('[name=do_search]'); | ||
| 149 | var $do_merge = $('[name=do_merge]'); | ||
| 150 | var $form = $('#admin-search-merge-form'); | ||
| 151 | var $error_container = $('.validation-errors'); | ||
| 152 | var $field_list = $('#field-list'); | ||
| 153 | |||
| 154 | $('[name=btn_user_from]').click(function() { | ||
| 155 | $which_user_search.val("from"); | ||
| 156 | $do_search.val("yes"); | ||
| 157 | $('[name=merge_user_from]').attr("checked", false); | ||
| 158 | $form.submit(); | ||
| 159 | }); | ||
| 160 | |||
| 161 | $('[name=btn_user_to]').click(function() { | ||
| 162 | $which_user_search.val("to"); | ||
| 163 | $do_search.val("yes"); | ||
| 164 | $('[name=merge_user_to]').attr("checked", false); | ||
| 165 | $form.submit(); | ||
| 166 | }); | ||
| 167 | |||
| 168 | $('[name=btn_merge]').click(function() { | ||
| 169 | $do_search.val("no"); | ||
| 170 | $do_merge.val("yes"); | ||
| 171 | $form.submit(); | ||
| 172 | }); | ||
| 173 | |||
| 174 | $(function() { | ||
| 175 | |||
| 176 | var options = { | ||
| 177 | url: ajaxurl, | ||
| 178 | dataType: 'json', | ||
| 179 | type: 'post', | ||
| 180 | data: ({ajax:"yes", action: 'admin_search_merge_users'}), | ||
| 181 | success: function(data) { | ||
| 182 | if (data.success == "false") { | ||
| 183 | $('h6', $error_container).html("OOPS..."); | ||
| 184 | $('ul', $error_container).html(data.msg); | ||
| 185 | $field_list.hide(); | ||
| 186 | $error_container.show(); | ||
| 187 | } else { | ||
| 188 | $error_container.hide(); | ||
| 189 | if (data.fields === undefined) { | ||
| 190 | $('#user-list-' + data.direction).html(data.html); | ||
| 191 | $field_list.hide(); | ||
| 192 | } else { | ||
| 193 | $('#user-list-from').html(""); | ||
| 194 | $('#user-list-to').html(""); | ||
| 195 | $('ul', $field_list).html(data.fields); | ||
| 196 | $field_list.show(); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | }; | ||
| 201 | $form.ajaxForm(options); | ||
| 202 | |||
| 203 | }); | ||
| 204 | |||
| 205 | </script> |
-
Please register or sign in to post a comment