From 1c1d42bc50b696200cf1f1d6abf63f3a33280277 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 22 Mar 2018 13:42:55 -0400 Subject: [PATCH] Update how the member contact are being listed. Fixing an issue with contacts with no email address. This happens when being imported. I have it now so if they were not imported with an email they can still find their wordpress user and they get updated for other member management properly. Also updating how to pull list of contacts. If the member has no original contact (like wmta) it will see if any other contacts can edit that user and show them in the list. If the member has an original contact then it will use that contact wp users meta data to get the list of additional contact for the contact list. --- classes/data/dataContacts.php | 33 +++++++++++--------- models/admin/contacts/index.php | 54 ++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 21 deletions(-) diff --git a/classes/data/dataContacts.php b/classes/data/dataContacts.php index 599436b..0b662a8 100644 --- a/classes/data/dataContacts.php +++ b/classes/data/dataContacts.php @@ -570,24 +570,29 @@ class GlmDataContacts extends GlmDataAbstract } $r = array( - 'wpUserEmail' => false, - 'wordpressEmail' => false, - 'wpUserLogin' => false, - 'wordpressLogin' => false, - 'wpUser' => false, - 'contactsEmail' => false, + 'wpUserEmail' => false, + 'wordpressEmail' => false, + 'wpUserLogin' => false, + 'wordpressLogin' => false, + 'wpUser' => false, + 'contactsEmail' => false, 'contactsUsername' => false, - 'active' => false + 'active' => false ); // Check Wordpress - $r['wpUserEmail'] = get_user_by( 'email', $email ); - If ($r['wpUserEmail']) { - $r['wordpressEmail'] = true; - } - $r['wpUserLogin'] = get_user_by( 'login', $username ); - If ($r['wpUserLogin']) { - $r['wordpressLogin'] = true; + if ( $email ) { + $r['wpUserEmail'] = get_user_by( 'email', $email ); + If ($r['wpUserEmail']) { + $r['wordpressEmail'] = true; + } + } else { + $wpUser = get_user_by( 'login', $username ); + $r['wpUserLogin'] = $wpUser; + $r['wpUserEmail'] = $wpUser; + If ($r['wpUserLogin']) { + $r['wordpressLogin'] = true; + } } // Check Contacts diff --git a/models/admin/contacts/index.php b/models/admin/contacts/index.php index c765882..e8b13ca 100644 --- a/models/admin/contacts/index.php +++ b/models/admin/contacts/index.php @@ -446,6 +446,12 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts // Get the wordpress user ID $wpUser = get_user_by('email', $contactInfo['fieldData']['email']); + // If we cannot find user by email try the login using username + if ( !$wpUser ) { + $wpUser = get_user_by('login', $contactInfo['fieldData']['username']); + } + // echo '
$wpUser: ' . print_r( $wpUser, true ) . '
'; + // Check for password changes and update Wordpress user if (trim($_REQUEST['password']) != '') { @@ -463,7 +469,12 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts // If there's a role change, update the user role for WordPress if ($wpRole != $savedContactRole['wpRole']) { $contactCheck = $this->checkContact($contactInfo['fieldData']['email']); - $wpUser = new WP_User($contactCheck['wpUserEmail']->ID); + // Updating this to deal with imported members that did not have an email. + if ( isset( $contactCheck['wpUserEmail'] ) ) { + $wpUser = new WP_User($contactCheck['wpUserEmail']->ID); + } else if ( isset( $contactCheck['wpUserLogin'] ) ) { + $wpUser = new WP_User($contactCheck['wpUserLogin']->ID); + } $wpUser->remove_role($savedContactRole['wpRole']); $wpUser->add_role($wpRole); } @@ -539,15 +550,41 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts // Only list member contacts for the selected member if ($haveMember) { + $other_members_refs = false; // Have to query the meta data before sending it back into // db query. Would work but it's getting back a string // instead of an array of ints. - $other_members_refs = $this->wpdb->get_var( - "SELECT meta_value - FROM " . $this->wpdb->prefix . "usermeta - WHERE meta_key = 'glmMembersContactMembers' - AND meta_value LIKE '%" . $this->memberID . "%'" + // Should only be getting the meta data for the real contact. + // Get userID for original member contact. + $originalMemberContact = $this->wpdb->get_row( + $this->wpdb->prepare( + "SELECT id,email,username + FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts + WHERE ref_dest = %d", + $this->memberID + ), + ARRAY_A ); + if ( isset( $originalMemberContact ) ) { + $wpUser = $this->checkContact( $originalMemberContact['email'], $originalMemberContact['username'] ); + // echo '
$wpUser: ' . print_r( $wpUser, true ) . '
'; + $other_members_refs = $this->wpdb->get_var( + "SELECT meta_value + FROM " . $this->wpdb->prefix . "usermeta + WHERE meta_key = 'glmMembersContactMembers' + AND meta_value LIKE '%" . $this->memberID . "%' + AND user_id = " . $wpUser['wpUserEmail']->ID + ); + // echo '
$other_members_refs: ' . print_r( $other_members_refs, true ) . '
'; + // Get wordpress user + } else { + $other_members_refs = $this->wpdb->get_var( + "SELECT meta_value + FROM " . $this->wpdb->prefix . "usermeta + WHERE meta_key = 'glmMembersContactMembers' + AND meta_value LIKE '%" . $this->memberID . "%'" + ); + } $where .= " AND ref_type = " . $this->config['ref_type_numb']['Member']; if ( $other_members_refs ) { // Make sure the $other_members_refs is correct @@ -744,10 +781,13 @@ class GlmMembersAdmin_contacts_index extends GlmDataContacts // Attempt to get WordPress user data $wpUserID = false; - $contactCheck = $this->checkContact($contactInfo['fieldData']['email']); + $contactCheck = $this->checkContact( $contactInfo['fieldData']['email'], $contactInfo['fieldData']['username'] ); if ($contactCheck['wpUserEmail']) { $wpUser = new WP_User($contactCheck['wpUserEmail']->ID); $wpUserID = $wpUser->ID; + } else if ( $contactCheck['wpUserLogin'] ) { + $wpUser = new WP_User($contactCheck['wpUserLogin']->ID); + $wpUserID = $wpUser->ID; } $contactMembersList = ''; -- 2.17.1