Add deleting of the member contacts.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 15 Oct 2019 19:53:14 +0000 (15:53 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 15 Oct 2019 19:53:14 +0000 (15:53 -0400)
Checks if they have other roles.
Also checks other contacts or wordpress users that have access to this
member.

setup/adminHooks.php

index 7c43eed..bde1ebc 100644 (file)
@@ -126,5 +126,105 @@ add_filter( 'glm_contact_is_moderated', function( $user ) {
         return false;
     }
 } );
+// Delete member contacts when member is deleted.
+add_action( 'glm-member-db-delete-member', function( $memberId ){
+    $contact = false;
+    // Verify member id is number.
+    $memberId = filter_var( $memberId, FILTER_VALIDATE_INT );
+    if ( $memberId ) {
+        require_once GLM_MEMBERS_CONTACTS_PLUGIN_CLASS_PATH . '/data/dataContacts.php';
+        $ContactData = new GlmDataContacts( $this->wpdb, $this->config );
+        // Before deleting the Contact we have a few things to check.
+        // Delete Wordpress user if they aren't needed by other members or have extra roles.
+        $contactId = $ContactData->getContactIdByRefDest( $memberId );
+        if ( $contactId ) {
+            $contact = $ContactData->getEntry( $contactId );
+        } else {
+            $contact = false;
+        }
+
+        if ( $contact ) {
+            // Get the wordpress user ID
+            $wpUser = get_user_by( 'email', $contact['email'] );
+
+            // Check for other roles assigned to this user and remove our roles
+            $userHasOtherRole = false;
+            foreach ( $wpUser->roles as $role ) {
+
+                // Is this role not one of ours?
+                if ( !in_array( $role, $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( $role );
+
+                }
+
+            }
+
+            // 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' );
+            }
+
+            $deleted = $ContactData->deleteEntry( $contactId, true );
+        }
+
+        // Check to see if any other member has permission to edit this member.
+        // Get user meta.
+        // For users with glmMembersContactMemberSelected set to this member
+        // you'll have to reset it to another member in their list.
+        $otherUsers = $this->wpdb->get_results(
+            $this->wpdb->prepare(
+                "SELECT user_id
+                   FROM " . $this->wpdb->prefix . "usermeta
+                  WHERE meta_key = %s
+                    ANd meta_value = %s" ,
+                'glmMembersContactMemberSelected',
+                $memberId
+            ),
+            ARRAY_A
+        );
+        if ( !empty( $otherUsers ) ) {
+            foreach ( $otherUsers as $wpUser ) {
+                $allMembers = $this->wpdb->get_var(
+                    $this->wpdb->prepare(
+                        "SELECT meta_value
+                           FROM " . $this->wpdb->prefix . "usermeta
+                          WHERE user_id = %d
+                            AND meta_key = 'glmMembersContactMembers'",
+                        $wpUser['user_id']
+                    )
+                );
+                if ( !empty( $allMembers ) ) {
+                    $otherMemberIds = array_unique( explode( ',', $allMembers ) );
+                    if ( !empty( $otherMemberIds ) ) {
+                        foreach ( $otherMemberIds as $mKey => $mId ) {
+                            if ( $mId == $memberId ) {
+                                unset( $otherMemberIds[$mKey] );
+                            }
+                        }
+                    }
+                    // Pick another member id to use for Selected
+                    update_user_meta( $wpUser['user_id'], 'glmMembersContactMemberSelected', $otherMemberIds[0] );
+                    // Update the user meta value.
+                    $glmMembersContactMembers = ( !empty( $otherMemberIds ) ) ? implode( ',', $otherMemberIds ) : '';
+                    update_user_meta( $wpUser['user_id'], 'glmMembersContactMembers', $glmMembersContactMembers );
+                }
+            }
+        }
+    }
+}, 10, 1 );