Setup filter for fetching the primary email for member.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 2 Dec 2016 18:38:28 +0000 (13:38 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 2 Dec 2016 19:08:01 +0000 (14:08 -0500)
Looks for a primary contact record.
If that is not found then it looks for the email from the members active
record.

setup/adminHooks.php

index 7fcc24a..d520aa1 100644 (file)
@@ -44,4 +44,47 @@ add_filter('glm-member-db-admin-management-hooksHelp', function($content) {
     2
 );
 
-?>
\ No newline at end of file
+add_filter(
+    'glm-member-db-admin-get-member-primary-email',
+    function( $content, $memberId ){
+        global $wpdb;
+        //$content = $memberId;
+        // See if this member has a member contact
+        $contact_email = $wpdb->get_var(
+            $wpdb->prepare(
+                "SELECT email
+                   FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts
+                  WHERE ref_dest = %d
+                    AND primary_contact",
+                $memberId
+            )
+        );
+        if ( $contact_email ) {
+            return $contact_email;
+        }
+        require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataMemberInfo.php';
+        // First get the active member info record id
+        $memberInfoData     = new GlmDataMemberInfo( $wpdb, $this->config );
+        $activeMemberInfoId = $memberInfoData->getActiveInfoIdForMember( $memberId );
+        if ( !$activeMemberInfoId ) {
+            return false;
+        }
+        // If we're here then we didn't find primary contact record so get the email from the member info.
+        $contact_email = $wpdb->get_var(
+            $wpdb->prepare(
+                "SELECT email
+                   FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info
+                  WHERE id = %d",
+                $activeMemberInfoId
+            )
+        );
+        if ( $contact_email ) {
+            return $contact_email;
+        }
+        return $content;
+    },
+    10,
+    2
+);
+
+?>