From: Anthony Talarico Date: Tue, 17 May 2016 14:29:58 +0000 (-0400) Subject: added list function to add subscriber to membership list X-Git-Tag: v1.0.0^2~3 X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=cfc6e7fe0842b47bbe607714a23536788b393b3b;p=WP-Plugins%2Fglm-gravityform-streamsend.git added list function to add subscriber to membership list --- diff --git a/class-streamsendfeedaddon.php b/class-streamsendfeedaddon.php index 8233033..12081e0 100644 --- a/class-streamsendfeedaddon.php +++ b/class-streamsendfeedaddon.php @@ -13,6 +13,7 @@ class GFStreamSendAddOn extends GFFeedAddOn protected $_title = 'Gravity Forms StreamSend Integration'; protected $_short_title = 'StreamSend'; protected $_errorFlag = false; + protected $listID; private static $_instance = null; protected $streamSendFields = array( @@ -105,18 +106,19 @@ class GFStreamSendAddOn extends GFFeedAddOn $apiLogin = rgar($settings, 'glm_streamsend_login'); $apiKey = rgar($settings, 'glm_streamsend_key'); $ss = new StreamSend(STREAMSEND_BASE_URL, $apiLogin, $apiKey); -// echo '
', print_r($ss->listList(),true), '
'; - $lists = $ss->listList(); - $lists = $lists->responseData->list; - for($i = 0; $i < count($lists); $i++){ - + $getList = $ss->listList(); + $listData = $getList->responseData; + $lists = array(); + for($i = 0; $i < count($listData->list[$i]); $i++){ + $lists[] =array('label'=>$listData->list[$i]->name,'value'=>$listData->list[$i]->id); } - + array_unshift($lists,array('label'=>'Select a List', 'value'=>'')); return array( array( 'title' => 'StreamSend Form Settings', 'fields' => array( + array( 'label' => esc_html__( 'Feed name', 'StreamSend' ), 'type' => 'text', @@ -124,6 +126,14 @@ class GFStreamSendAddOn extends GFFeedAddOn 'tooltip' => esc_html__( 'Just a name', 'StreamSend' ), 'class' => 'small', ), + array( + 'label' => 'Lists', + 'type' => 'select', + 'name' => 'lists', + 'tooltip' => 'Select a list to add the contact', + 'choices' => $lists + + ), array( 'name' => 'mappedFields', 'label' => esc_html__( 'Map Fields', 'StreamSend' ), @@ -185,13 +195,7 @@ class GFStreamSendAddOn extends GFFeedAddOn 'checkbox_label' => esc_html__( 'Enable Condition', 'StreamSend' ), 'instructions' => esc_html__( 'Process this StreamSend feed if', 'StreamSend' ), ), - array( - 'name' => 'Lists', - 'label' => esc_html__( 'List', 'StreamSend' ), - 'type' => 'select', - 'checkbox_label' => esc_html__( 'Enable Condition', 'StreamSend' ), - 'instructions' => esc_html__( 'Process this StreamSend feed if', 'StreamSend' ), - ), + ) ) ); @@ -202,6 +206,8 @@ class GFStreamSendAddOn extends GFFeedAddOn 'feedName' => __( 'Name', 'StreamSend' ), ); } + + // validate the api key and login input public function is_valid_setting(){ $settings = $this->get_plugin_settings(); @@ -216,6 +222,8 @@ class GFStreamSendAddOn extends GFFeedAddOn echo '

Invalid Login or API Key

'; echo ''; } + + // use JS to auto input the audience ID into the audience field and make the input readOnly ?> errorFlag){ echo '
'; @@ -241,16 +252,21 @@ class GFStreamSendAddOn extends GFFeedAddOn public function process_feed( $feed, $entry, $form ) { $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 + $merge_vars = array(); foreach ( $field_map as $name => $field_id ) { $merge_vars[ $name ] = $this->get_field_value( $form, $entry, $field_id ); } - + // if a list is selected there will be an id, then use the contactUpload function since it takes a list id parameter + if(!empty($this->listID)){ + // contact upload function here + $return = $this->add_contact( $merge_vars, $this->listID ); + } $return = $this->add_contact( $merge_vars ); } - public function add_contact($values) + public function add_contact($values, $listId='') { $settings = $this->get_plugin_settings(); $apiLogin = rgar( $settings, 'glm_streamsend_login' ); @@ -274,6 +290,8 @@ class GFStreamSendAddOn extends GFFeedAddOn $ret->contact->id, $contactData ); + }else if(!empty($listId)){ + $listAdd = $ss->addToList($contactData, $this->listID); } else { $contacts = $ss->contactCreate( $contactData, @@ -282,6 +300,7 @@ class GFStreamSendAddOn extends GFFeedAddOn STREAMSEND_DEFAULT_DELIVER_WELCOME ); } + if (!$contacts) { return false; //echo '

Debug Results

'.$ss->debugBuffer.'

'; diff --git a/lib/class_streamsend_api.php b/lib/class_streamsend_api.php index 7e4dd1e..78fce49 100644 --- a/lib/class_streamsend_api.php +++ b/lib/class_streamsend_api.php @@ -1417,6 +1417,7 @@ class StreamSend { $post_data .= ' <'.trim($k).'>'.trim($v).'\n"; } } + $post_data .= " $activate\n" ." $deliver\n" ." $welcome\n"; @@ -1425,7 +1426,8 @@ class StreamSend { $this->debug('contactCreate() XML POST request data
'
                     ."\n".htmlentities($this->postData)."\n"
                     .'
'); - + + /** This operation requires the "POST" Method */ if ($this->sendRequest('POST', 'audiences/'.STREAMSEND_AUDIENCE.'/people') === false) { $this->debug('contactCreate() Call to sendRequest() failed.'); @@ -1585,6 +1587,40 @@ class StreamSend { return(clone $this); } + function addToList($contact, $listId) + { + + $this->debug('addToList() called'); + + $email = $contact['email-address']; + + $post_data = "\n"; + $post_data .= " $listId\n" + ." $email\n"; + $post_data .= "\n"; + $this->postData = $post_data; + $this->debug('contactCreate() XML POST request data
'
+                    ."\n".htmlentities($this->postData)."\n"
+                    .'
'); + + /** 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); + + } @@ -1946,7 +1982,6 @@ class StreamSend { $this->debug('uploadContacts() A valid list of contacts was not provided.'); return false; } - $this->debug('uploadContacts() '.count($contacts).' contacts provided.'); /** @@ -1956,7 +1991,6 @@ class StreamSend { $this->debug('uploadContacts() Call to fieldsList() failed.'); return false; } - /** Email_address is not returned in fieldsList() but is required. Curiously it doesn't have an ID like the other fields */ $available_fields = array( 'email_address' => 'email_address' ); foreach( $fields_list->responseData->field as $field ) { @@ -1971,18 +2005,15 @@ class StreamSend { $upload_data = $contact_fields = $contact_data_header = $contact_data_header_sep = ''; $numb_contacts = $numb_fields = 0; $have_email_field = false; - foreach( $contacts as $fields ) { $field = 0; $contact_data = ''; - /** If this is not the first contact, make sure we have the same number of fields as the first. */ if( $numb_contacts > 0 && count($fields) != $numb_fields ) { $this->debug('uploadContacts() Contact # '.($numb_contacts+1).' does not have the same number of fields as the first.'); return false; } - /** For each field in this contact */ while( list($k, $v) = each($fields) ) { @@ -2014,7 +2045,6 @@ class StreamSend { $this->debug('uploadContacts() Fields for contact # '.($numb_contacts+1).' did not match first contact.'); return false; } - /** Add field to contact, use "|" separator if it's not the first field */ $contact_data .= ($field>0?"\t":'').$v;