Adding script for updating usernames for missign (@ or .)
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 19 Jul 2018 15:54:24 +0000 (11:54 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 19 Jul 2018 15:54:24 +0000 (11:54 -0400)
Members Contacts and users where imported with missing ^ and .'s.

models/admin/management/contacts.php
views/admin/management/clean.html

index 4009a80..849651b 100644 (file)
@@ -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 = '<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;
 
 
@@ -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
index 222f4e6..06bcfa1 100644 (file)
         <input type="submit" value="Clear Archived" >
     </form>
 
+    <form action="{$thisUrl}?page={$thisPage}" method="post" enctype="multipart/form-data">
+        <input type="hidden" name="page" value="{$thisPage}" />
+        <input type="hidden" name="glm_action" value="contacts" />
+        <input type="hidden" name="option" value="clean" />
+        <input type="hidden" name="option2" value="usernames" />
+        <input type="submit" value="Update Usernames" >
+    </form>
+
     {if $result}
         {$result}
     {/if}
 
+    {if $resultData}
+    <table>
+        <tr>
+            <th> ID </th>
+            <th> Username </th>
+            <th> Email </th>
+            <th> WP ID </th>
+            <th> WP Username </th>
+            <th> WP Email </th>
+        </tr>
+        {foreach $resultData as $row}
+            <tr {if $row@iteration is div by 2}style="background-color:lightgrey;"{/if}>
+                <td> {$row.id} </td>
+                <td> {$row.username} </td>
+                <td> {$row.email} </td>
+                <td> {$row.wp_ID} </td>
+                <td> {$row.wp_userlogin} </td>
+                <td> {$row.wp_email} </td>
+            </tr>
+        {/foreach}
+    </table>
+    {/if}
+
 
     <script type="text/javascript">
     </script>