From 1d41c908b7820ece1dcf656f2fb9d9fdf5ebce50 Mon Sep 17 00:00:00 2001 From: Amy Hill Date: Thu, 11 Jun 2020 11:00:41 -0400 Subject: [PATCH] Conditionally delete user Only delete user if user has no other role and is not administrator --- .../classes/ListAffiliatesTable.php | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/wpaffiliatemanager/classes/ListAffiliatesTable.php b/wpaffiliatemanager/classes/ListAffiliatesTable.php index 7dba016..db4f718 100644 --- a/wpaffiliatemanager/classes/ListAffiliatesTable.php +++ b/wpaffiliatemanager/classes/ListAffiliatesTable.php @@ -105,9 +105,7 @@ function process_bulk_action() { $aff_email = $selectdb->email; $user = get_user_by('email', $aff_email); if ($user) { - if (!in_array('administrator', $user->roles)) { - wp_delete_user($user->ID); - } + $this->maybe_delete_user($user); } $updatedb = "DELETE FROM $record_table_name WHERE affiliateId='$row'"; $results = $wpdb->query($updatedb); @@ -130,9 +128,7 @@ function process_individual_action() { $aff_email = $selectdb->email; $user = get_user_by('email', $aff_email); if ($user) { - if (!in_array('administrator', $user->roles)) { - wp_delete_user($user->ID); - } + $this->maybe_delete_user($user); } $updatedb = "DELETE FROM $record_table_name WHERE affiliateId='$aid'"; $result = $wpdb->query($updatedb); @@ -224,5 +220,20 @@ function prepare_items($ignore_pagination = false) { // Now we add our *sorted* data to the items property, where it can be used by the rest of the class. $this->items = $data; } + + public function maybe_delete_user($user){ + if ( in_array('affiliate', $user->roles) ){ + if ( count($user->roles) === 1 ) { + wp_delete_user($user->ID); + return; + } + else { + $user->remove_role('affiliate'); + } + } + if ( ! in_array('administrator', $user->roles) ) { + $user->remove_cap(WPAM_PluginConfig::$AffiliateCap); + } + } }