From 541413dcd9ebdfed59c0528af4ef9fed40a59579 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 18 Jul 2018 16:34:42 -0400 Subject: [PATCH] Add script for deleting archived members and contacts Grab archived members and remove contact wp_user member_info member --- models/admin/management/contacts.php | 118 ++++++++++++++++++++-- views/admin/management/clean.html | 24 +++++ views/admin/management/contactHeader.html | 3 +- 3 files changed, 135 insertions(+), 10 deletions(-) create mode 100644 views/admin/management/clean.html diff --git a/models/admin/management/contacts.php b/models/admin/management/contacts.php index 993d439..312f4aa 100644 --- a/models/admin/management/contacts.php +++ b/models/admin/management/contacts.php @@ -107,7 +107,7 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts */ public function modelAction($actionData = false) { - + $option = 'importSetup'; $importResult = array( 'status' => false, @@ -122,11 +122,12 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts $content = ''; $thisOption = ''; $requestedView = 'contacts.html'; + $result = ''; $templateData = array( 'thisOption' => '', ); - + if ($actionData) { $templateData = $actionData; } @@ -137,6 +138,104 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts switch ($option) { + case 'clean': + $requestedView = 'clean.html'; + + $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 25 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 { + // 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'); + } + } + } + + // 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; + + } + + break; + case 'clearContacts': @@ -245,7 +344,8 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts 'db_password' => $db_password, 'db_schema' => $db_schema, 'content' => $content, - 'thisOption' => $thisOption + 'thisOption' => $thisOption, + 'result' => $result, ); // Return status, suggested view, and data to controller @@ -258,7 +358,7 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts ); - } + } /** @@ -885,11 +985,11 @@ class GlmMembersAdmin_management_contacts extends GlmDataContacts // Update the contact Email data $rows = $this->wpdb->update( - GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . 'contacts', - array('email' => $memberContactEmail), - array( 'id' => $contactData['id']), - array('%s'), - array( '%d' ) + GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . 'contacts', + array('email' => $memberContactEmail), + array( 'id' => $contactData['id']), + array('%s'), + array( '%d' ) ); // Check for failure diff --git a/views/admin/management/clean.html b/views/admin/management/clean.html new file mode 100644 index 0000000..222f4e6 --- /dev/null +++ b/views/admin/management/clean.html @@ -0,0 +1,24 @@ +{include file='admin/management/header.html'} +{include file='admin/management/contactHeader.html'} + +

Clear Archived Members/Contacts

+

Clear archived members (+info), and contacts (+users)

+ +
+ + + + + +
+ + {if $result} + {$result} + {/if} + + + + +{include file='admin/footer.html'} + diff --git a/views/admin/management/contactHeader.html b/views/admin/management/contactHeader.html index ec30c65..2b980e3 100644 --- a/views/admin/management/contactHeader.html +++ b/views/admin/management/contactHeader.html @@ -1,6 +1,7 @@ \ No newline at end of file + -- 2.17.1