}
+ /**
+ * Given a contact id change the role of both the contact
+ * and the wordpress user assigned to that contact.
+ */
+ public function updateContactRole( $contact_id, $old_role, $new_role )
+ {
+ $user_id = $this->getWpUserId( $contact_id );
+ $wpUser = get_user_by( 'id', $user_id );
+ $wpUser->remove_role( $old_role );
+ $wpUser->add_role( $new_role );
+ }
+
+ public function getContactIdByRefDest( $ref_dest )
+ {
+ return $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT id
+ FROM " . GLM_MEMBERS_CONTACTS_PLUGIN_DB_PREFIX . "contacts
+ WHERE ref_dest = %d",
+ $ref_dest
+ )
+ );
+ }
+
+ /**
+ * Find the wordpress user for a given contact.
+ */
+ public function getWpUserId( $contact_id )
+ {
+ if ( $contact_id = filter_var( $contact_id, FILTER_VALIDATE_INT ) ) {
+ return $this->wpdb->get_var(
+ "SELECT user_id
+ FROM " . $this->wpdb->prefix . "usermeta
+ WHERE meta_key = 'glmMembersContactID'
+ AND meta_value = '$contact_id'"
+ );
+
+ }
+ }
}
}
return $employees;
}, 10, 2 );
+
+add_filter( 'glm_contact_update_user_role_by_ref_dest', function( $content, $ref_dest, $old_role, $new_role ) {
+ require_once GLM_MEMBERS_CONTACTS_PLUGIN_CLASS_PATH . '/data/dataContacts.php';
+ $Contact = new GlmDataContacts( $this->wpdb, $this->config );
+ $contact_id = $Contact->getContactIdByRefDest( $ref_dest );
+ if ( $contact_id ) {
+ $Contact->updateContactRole( $contact_id, $old_role, $new_role );
+ }
+ return $content;
+}, 10, 4);
?>