From 695d247ef7852ef52756606037b5e7719aa07465 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 19 Jul 2018 11:54:24 -0400 Subject: [PATCH] Adding script for updating usernames for missign (@ or .) Members Contacts and users where imported with missing ^ and .'s. --- models/admin/management/contacts.php | 214 +++++++++++++++++---------- views/admin/management/clean.html | 31 ++++ 2 files changed, 168 insertions(+), 77 deletions(-) diff --git a/models/admin/management/contacts.php b/models/admin/management/contacts.php index 4009a80..849651b 100644 --- a/models/admin/management/contacts.php +++ b/models/admin/management/contacts.php @@ -123,6 +123,7 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts $thisOption = ''; $requestedView = 'contacts.html'; $result = ''; + $resultData = array(); $templateData = array( 'thisOption' => '', @@ -140,100 +141,158 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts 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 = '
$option2: ' . print_r( $option2, true ) . '
'; 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 .= '

Total Archived Members: ' . $totalArchived . '

'; - $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 .= '
$members: ' . print_r( $members, true ) . '
'; - foreach ( $members as $member ) { - // $result .= '
$member: ' . print_r( $member, true ) . '
'; - // 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 .= '

Updating Usernames

'; + // 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 .= '
$member_contact: ' . print_r( $member_contact, true ) . '
'; - 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 .= '
$wpUser: ' . print_r( $wpUser, true ) . '
'; - // 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 .= '

Found ' . $contact_count . ' Contacts

'; + $result .= '

Found ' . $wp_count . ' Wordpress matches

'; + // $result .= '
$contacts: ' . print_r( $contacts, true ) . '
'; + 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 .= '

Total Archived Members: ' . $totalArchived . '

'; + $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 .= '
$members: ' . print_r( $members, true ) . '
'; + foreach ( $members as $member ) { + // $result .= '
$member: ' . print_r( $member, true ) . '
'; + // 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 .= '
$member_contact: ' . print_r( $member_contact, true ) . '
'; + if ( $member_contact['email'] ) { + $wpUser = get_user_by( 'email', $member_contact['email'] ); + if ( $wpUser ) { + // $result .= '
$wpUser: ' . print_r( $wpUser, true ) . '
'; + // 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 .= '

Removed ' . $archiveCount . ' Archived Members

'; - 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 .= '

Removed ' . $archiveCount . ' Archived Members

'; + break; + + default: + break; } + $thisOption = $option; break; @@ -346,6 +405,7 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts 'content' => $content, 'thisOption' => $thisOption, 'result' => $result, + 'resultData' => $resultData, ); // Return status, suggested view, and data to controller diff --git a/views/admin/management/clean.html b/views/admin/management/clean.html index 222f4e6..06bcfa1 100644 --- a/views/admin/management/clean.html +++ b/views/admin/management/clean.html @@ -12,10 +12,41 @@ +
+ + + + + +
+ {if $result} {$result} {/if} + {if $resultData} + + + + + + + + + + {foreach $resultData as $row} + + + + + + + + + {/foreach} +
ID Username Email WP ID WP Username WP Email
{$row.id} {$row.username} {$row.email} {$row.wp_ID} {$row.wp_userlogin} {$row.wp_email}
+ {/if} + -- 2.17.1