Update how the member contact are being listed.
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 22 Mar 2018 17:42:55 +0000 (13:42 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 22 Mar 2018 17:42:55 +0000 (13:42 -0400)
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
models/admin/contacts/index.php

index 599436b..0b662a8 100644 (file)
@@ -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
index c765882..e8b13ca 100644 (file)
@@ -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 '<pre>$wpUser: ' . print_r( $wpUser, true ) . '</pre>';
+
                     // 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 '<pre>$wpUser: ' . print_r( $wpUser, true ) . '</pre>';
+                        $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 '<pre>$other_members_refs: ' . print_r( $other_members_refs, true ) . '</pre>';
+                        // 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 = '';