Update for more contact cleanup
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 19 Jul 2018 20:56:01 +0000 (16:56 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 19 Jul 2018 20:56:01 +0000 (16:56 -0400)
Adding script for removing contacts and users not assigned to a member.

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

index 849651b..ea32539 100644 (file)
@@ -147,6 +147,58 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts
             // $result = '<pre>$option2: ' . print_r( $option2, true ) . '</pre>';
 
             switch ( $option2 ) {
+            case 'nomember':
+                $sql = "
+                SELECT id,email
+                  FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts
+                 WHERE ref_dest NOT IN (
+                        SELECT distinct id FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "members
+                        )";
+                $member_contacts = $this->wpdb->get_results( $sql, ARRAY_A );
+                foreach ( $member_contacts as $member_contact ) {
+                    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 {
+                                    // 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;
+
+                            // 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');
+                            }
+                        }
+
+                    }
+                    if ( $member_contact['id'] ) {
+                        // Delete the Member Contact
+                        $this->wpdb->delete(
+                            GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . 'contacts',
+                            array( 'id' => $member_contact['id'] ),
+                            array( '%d' )
+                        );
+                    }
+                }
+                break;
+
             case 'usernames':
                 $wp_count = 0;
                 $contact_count = 0;
@@ -267,6 +319,15 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts
                                    delete_user_meta($wpUser->ID, 'glmMembersContactActive');
                             }
                         }
+
+                    }
+                    if ( $member_contact['id'] ) {
+                        // Delete the Member Contact
+                        $this->wpdb->delete(
+                            GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . 'contacts',
+                            array( 'id' => $member_contact['id'] ),
+                            array( '%d' )
+                        );
                     }
 
                     // Delete member_info records belonging to the archived members.
index 06bcfa1..0c1d57d 100644 (file)
         <input type="submit" value="Update Usernames" >
     </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="nomember" />
+        <input type="submit" value="Contacts w/o Members" >
+    </form>
+
     {if $result}
         {$result}
     {/if}