added double opt-in condition
authorAnthony Talarico <talarico@gaslightmedia.com>
Wed, 25 May 2016 15:27:48 +0000 (11:27 -0400)
committerAnthony Talarico <talarico@gaslightmedia.com>
Wed, 25 May 2016 15:27:48 +0000 (11:27 -0400)
class-streamsendfeedaddon.php
lib/class_streamsend_api.php

index 12081e0..eaedc9b 100644 (file)
@@ -14,6 +14,7 @@ class GFStreamSendAddOn extends GFFeedAddOn
     protected $_short_title = 'StreamSend';
     protected $_errorFlag = false;
     protected $listID;
+    protected $optIn;
 
     private static $_instance = null;
     protected $streamSendFields = array(
@@ -134,6 +135,18 @@ class GFStreamSendAddOn extends GFFeedAddOn
                          'choices' => $lists
                                
                         ),
+                    array(
+                         'label'   => 'Enable Opt-In',
+                         'type'    => 'checkbox',
+                         'name'    => 'opt-in',
+                         'tooltip' => 'Enable Opt-In',
+                         'choices' => array(
+                                          array(
+                                              'label' => '',
+                                              'name'  => 'opt-in'
+                                          ),
+                             )
+                        ),
                     array(
                         'name'      => 'mappedFields',
                         'label'     => esc_html__( 'Map Fields', 'StreamSend' ),
@@ -196,6 +209,8 @@ class GFStreamSendAddOn extends GFFeedAddOn
                         'instructions'   => esc_html__( 'Process this StreamSend feed if', 'StreamSend' ),
                     ),
                     
+                    
+                    
                 )
             )
         );
@@ -253,7 +268,13 @@ class GFStreamSendAddOn extends GFFeedAddOn
         $feedName           = $feed['meta']['feedName'];
         $field_map = $this->get_field_map_fields( $feed, 'mappedFields' );
         $this->listID = $feed['meta']['lists']; // get the list ID from a selected list
+        $this->optIn = $feed['meta']['opt-in'];
         
+        if($this->optIn == 1){
+            $this->optIn = 'false';
+        } else if($this->optIn == 0){
+            $this->optIn = 'true';
+        }
         $merge_vars = array();
         foreach ( $field_map as $name => $field_id ) {
             $merge_vars[ $name ] = $this->get_field_value( $form, $entry, $field_id );
@@ -272,6 +293,8 @@ class GFStreamSendAddOn extends GFFeedAddOn
         $apiLogin = rgar( $settings, 'glm_streamsend_login' );
         $apiKey   = rgar( $settings, 'glm_streamsend_key' );
         $audience = rgar( $settings, 'glm_streamsend_audience' );
+       
+        
 
         // check the array $values to make sure it is correct
         if (is_array($values) && !empty($values)) {
@@ -285,17 +308,23 @@ class GFStreamSendAddOn extends GFFeedAddOn
             foreach ($this->streamSendFields as $glmName => $ssName) {
                 $contactData[$ssName] = $values[$glmName];
             }
-            if ($ret->contact) {
-                $contacts = $ss->contactUpdate(
+            if(!empty($this->listID)){
+                $contacts = $ss->contactCreate(
+                    $contactData,
+                    $this->optIn,
+                    STREAMSEND_DEFAULT_DELIVER_ACTIVATION,
+                    STREAMSEND_DEFAULT_DELIVER_WELCOME,
+                    $listId
+                );
+            }else if($ret->contact){
+                 $contacts = $ss->contactUpdate(
                     $ret->contact->id,
                     $contactData
                 );
-            }else if(!empty($listId)){
-                $listAdd = $ss->addToList($contactData, $this->listID);
             } else {
                 $contacts = $ss->contactCreate(
                     $contactData,
-                    STREAMSEND_DEFAULT_ACTIVATE,
+                    $this->optIn,
                     STREAMSEND_DEFAULT_DELIVER_ACTIVATION,
                     STREAMSEND_DEFAULT_DELIVER_WELCOME
                 );
index 78fce49..e3b41d7 100644 (file)
@@ -1389,7 +1389,9 @@ class StreamSend {
     function contactCreate( $contact,
                             $activate = STREAMSEND_DEFAULT_ACTIVATE,
                             $deliver = STREAMSEND_DEFAULT_DELIVER_ACTIVATION,
-                            $welcome = STREAMSEND_DEFAULT_DELIVER_WELCOME )
+                            $welcome = STREAMSEND_DEFAULT_DELIVER_WELCOME,
+                            $listId = " "
+            )
     {
         $this->debug('contactCreate() called');
 
@@ -1433,6 +1435,23 @@ class StreamSend {
             $this->debug('contactCreate() Call to sendRequest() failed.');
             return false;
         }
+        if(!empty($listId)){
+            $email = $contact['email-address'];
+            $post_data = "<membership>\n";
+            $post_data .= "  <list-id>$listId</list-id>\n"
+                         ."  <email-address>$email</email-address>\n";
+            $post_data .= "</membership>\n";
+            $this->postData = $post_data;
+            $this->debug('contactCreate() XML POST request data<br /><table border="1"><tr><td><pre>'
+                        ."\n".htmlentities($this->postData)."\n"
+                        .'</pre></td></tr></table>');
+
+             /** This operation requires the "POST" Method */
+            if ($this->sendRequest('POST', 'audiences/'.STREAMSEND_AUDIENCE.'/lists/'.$listId.'/memberships') === false) {
+                $this->debug('contactCreate() Call to sendRequest() failed.');
+                return false;
+            }
+        }
 
         /**
          * Check if the response request was successful
@@ -1587,43 +1606,6 @@ class StreamSend {
 
         return(clone $this);
     }
-    function addToList($contact, $listId)
-    {
-        
-        $this->debug('addToList() called');    
-        
-        $email = $contact['email-address'];
-        
-        $post_data = "<membership>\n";
-        $post_data .= "  <list-id>$listId</list-id>\n"
-                     ."  <email-address>$email</email-address>\n";
-        $post_data .= "</membership>\n";
-        $this->postData = $post_data;
-        $this->debug('contactCreate() XML POST request data<br /><table border="1"><tr><td><pre>'
-                    ."\n".htmlentities($this->postData)."\n"
-                    .'</pre></td></tr></table>');
-        
-         /** This operation requires the "POST" Method */
-        if ($this->sendRequest('POST', 'audiences/'.STREAMSEND_AUDIENCE.'/lists/'.$listId.'/memberships') === false) {
-            $this->debug('contactCreate() Call to sendRequest() failed.');
-            return false;
-        }
-
-        /**
-         * Check if the response request was successful
-         */
-        if ($this->responseError > 0) {
-            $this->debug('addToList() Call to sendRequest() returned an error code.');
-        } else {
-            $this->debug('addToList() Received good response from sendRequest().');
-        }
-
-        return(clone $this);
-        
-    }
-
-
-
     /**
      * Memberships (subscription) Methods
      *