$thisOption = '';
$requestedView = 'contacts.html';
$result = '';
+ $resultData = array();
$templateData = array(
'thisOption' => '',
case 'clean':
$requestedView = 'clean.html';
-
- $option2 = isset( $_REQUEST['option2'] ) ? filter_var( $_REQUEST['option2'], FILTER_SANITIZE_STRING ): '';
-
- $result = '';
- $archiveCount = 0;
+ $option2 = isset( $_REQUEST['option2'] ) ? filter_var( $_REQUEST['option2'], FILTER_SANITIZE_STRING ): '';
+ $result = '';
+ $archiveCount = 0;
// $result = '<pre>$option2: ' . print_r( $option2, true ) . '</pre>';
switch ( $option2 ) {
-
- case 'clear':
- $sqlTotal = "
- SELECT count(id)
- FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
- WHERE access = " . $this->config['access_numb']['Archived'];
- $totalArchived = $this->wpdb->get_var( $sqlTotal );
- $result .= '<p>Total Archived Members: ' . $totalArchived . '</p>';
- $sql = "
- SELECT id,name
- FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
- WHERE access = " . $this->config['access_numb']['Archived'];
- $sql .= " ORDER BY id LIMIT 50 OFFSET 0 ";
- $members = $this->wpdb->get_results(
- $sql,
- ARRAY_A
- );
- // $result .= '<pre>$members: ' . print_r( $members, true ) . '</pre>';
- foreach ( $members as $member ) {
- // $result .= '<pre>$member: ' . print_r( $member, true ) . '</pre>';
- // Delete all contacts and users belonging to the archived members.
- $member_contact = $this->wpdb->get_row(
+ case 'usernames':
+ $wp_count = 0;
+ $contact_count = 0;
+ $result .= '<h2>Updating Usernames</h2>';
+ // grab all contacts with the bad usernames (missing @ and .'s)
+ $sql = "
+ SELECT id,username,email
+ FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts
+ WHERE LOWER(username) = REPLACE(REPLACE(LOWER(email), '@', ''), '.', '')
+ AND email != username";
+ $contacts = $this->wpdb->get_results( $sql, ARRAY_A );
+ if ( $contacts ) {
+ foreach ( $contacts as $key => $contact ) {
+ $contact_count++;
+ // Update the contact username
+ $new_username = $contact['email'];
+ $this->wpdb->query(
$this->wpdb->prepare(
- "SELECT id,email
- FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts
- WHERE ref_dest = %d",
- $member['id']
- ),
- ARRAY_A
+ "UPDATE " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts
+ SET username = %s
+ WHERE id = %d",
+ $new_username,
+ $contact['id']
+ )
);
- // $result .= '<pre>$member_contact: ' . print_r( $member_contact, true ) . '</pre>';
- if ( $member_contact['email'] ) {
- $wpUser = get_user_by( 'email', $member_contact['email'] );
+ // Fetch the wordpress user
+ $wpUser = get_user_by( 'login', $contact['username'] );
+ if ( $wpUser ) {
+ $wp_count++;
+ $contacts[$key]['wp_ID'] = $wpUser->ID;
+ $contacts[$key]['wp_userlogin'] = $wpUser->user_login;
+ $contacts[$key]['wp_email'] = $wpUser->user_email;
+ } else {
+ $wpUser = get_user_by( 'email', $contact['email'] );
if ( $wpUser ) {
- // $result .= '<pre>$wpUser: ' . print_r( $wpUser, true ) . '</pre>';
- // Check for other roles assigned to this user and remove our roles
- $userHasOtherRole = false;
- foreach ($wpUser->roles as $r) {
- if (!in_array($r, $this->config['contact_role_wordpress'])) {
- // Apparently not, so we need to keep the Wordpress user
- $userHasOtherRole = true;
-
- //Otherwise, this is one of our roles so we should remove it just in case the Wordpress user isn't deleted
- } else {
- // Remove this role from our user
- $wpUser->remove_role($r);
- }
- }
- // If the user doesn't have a role other than our members contact roles
- if (!$userHasOtherRole) {
-
- // Delete the wordpress user
- wp_delete_user($wpUser->ID);
- $wpUserDeleted = true;
+ $wp_count++;
+ $contacts[$key]['wp_ID'] = $wpUser->ID;
+ $contacts[$key]['wp_userlogin'] = $wpUser->user_login;
+ $contacts[$key]['wp_email'] = $wpUser->user_email;
+ }
+ }
+ if ( $wpUser ) {
+ $this->wpdb->query(
+ $this->wpdb->prepare(
+ "UPDATE " . $this->wpdb->prefix . "users
+ SET user_login = %s
+ WHERE ID = %d",
+ $new_username,
+ $wpUser->ID
+ )
+ );
+ }
+ }
+ }
+ $resultData = $contacts;
+ $result .= '<p>Found ' . $contact_count . ' Contacts</p>';
+ $result .= '<p>Found ' . $wp_count . ' Wordpress matches</p>';
+ // $result .= '<pre>$contacts: ' . print_r( $contacts, true ) . '</pre>';
+ break;
- // Otherwise we need to drop the user meta data we added to the WP user.
+ case 'clear':
+ $sqlTotal = "
+ SELECT count(id)
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+ WHERE access = " . $this->config['access_numb']['Archived'];
+ $totalArchived = $this->wpdb->get_var( $sqlTotal );
+ $result .= '<p>Total Archived Members: ' . $totalArchived . '</p>';
+ $sql = "
+ SELECT id,name
+ FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+ WHERE access = " . $this->config['access_numb']['Archived'];
+ $sql .= " ORDER BY id LIMIT 50 OFFSET 0 ";
+ $members = $this->wpdb->get_results(
+ $sql,
+ ARRAY_A
+ );
+ // $result .= '<pre>$members: ' . print_r( $members, true ) . '</pre>';
+ foreach ( $members as $member ) {
+ // $result .= '<pre>$member: ' . print_r( $member, true ) . '</pre>';
+ // Delete all contacts and users belonging to the archived members.
+ $member_contact = $this->wpdb->get_row(
+ $this->wpdb->prepare(
+ "SELECT id,email
+ FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts
+ WHERE ref_dest = %d",
+ $member['id']
+ ),
+ ARRAY_A
+ );
+ // $result .= '<pre>$member_contact: ' . print_r( $member_contact, true ) . '</pre>';
+ if ( $member_contact['email'] ) {
+ $wpUser = get_user_by( 'email', $member_contact['email'] );
+ if ( $wpUser ) {
+ // $result .= '<pre>$wpUser: ' . print_r( $wpUser, true ) . '</pre>';
+ // Check for other roles assigned to this user and remove our roles
+ $userHasOtherRole = false;
+ foreach ($wpUser->roles as $r) {
+ if (!in_array($r, $this->config['contact_role_wordpress'])) {
+ // Apparently not, so we need to keep the Wordpress user
+ $userHasOtherRole = true;
+
+ //Otherwise, this is one of our roles so we should remove it just in case the Wordpress user isn't deleted
} else {
- delete_user_meta($wpUser->ID, 'glmMembersContactID');
- delete_user_meta($wpUser->ID, 'glmMembersContactActive');
+ // Remove this role from our user
+ $wpUser->remove_role($r);
}
}
- }
+ // If the user doesn't have a role other than our members contact roles
+ if (!$userHasOtherRole) {
- // Delete member_info records belonging to the archived members.
- $this->wpdb->delete(
- GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info',
- array( 'member' => $member['id'] ),
- array( '%d' )
- );
+ // Delete the wordpress user
+ wp_delete_user($wpUser->ID);
+ $wpUserDeleted = true;
- // Delete archived members.
- $this->wpdb->delete(
- GLM_MEMBERS_PLUGIN_DB_PREFIX . 'members',
- array( 'id' => $member['id'] ),
- array( '%d' )
- );
- $archiveCount++;
+ // Otherwise we need to drop the user meta data we added to the WP user.
+ } else {
+ delete_user_meta($wpUser->ID, 'glmMembersContactID');
+ delete_user_meta($wpUser->ID, 'glmMembersContactActive');
+ }
+ }
}
- $result .= '<p>Removed ' . $archiveCount . ' Archived Members</p>';
- break;
- default:
- break;
+ // Delete member_info records belonging to the archived members.
+ $this->wpdb->delete(
+ GLM_MEMBERS_PLUGIN_DB_PREFIX . 'member_info',
+ array( 'member' => $member['id'] ),
+ array( '%d' )
+ );
+
+ // Delete archived members.
+ $this->wpdb->delete(
+ GLM_MEMBERS_PLUGIN_DB_PREFIX . 'members',
+ array( 'id' => $member['id'] ),
+ array( '%d' )
+ );
+ $archiveCount++;
+ }
+ $result .= '<p>Removed ' . $archiveCount . ' Archived Members</p>';
+ break;
+
+ default:
+ break;
}
+ $thisOption = $option;
break;
'content' => $content,
'thisOption' => $thisOption,
'result' => $result,
+ 'resultData' => $resultData,
);
// Return status, suggested view, and data to controller