protected $_title = 'Gravity Forms StreamSend Integration';
protected $_short_title = 'StreamSend';
protected $_errorFlag = false;
+ protected $listID;
private static $_instance = null;
protected $streamSendFields = array(
$apiLogin = rgar($settings, 'glm_streamsend_login');
$apiKey = rgar($settings, 'glm_streamsend_key');
$ss = new StreamSend(STREAMSEND_BASE_URL, $apiLogin, $apiKey);
-// echo '<pre>', print_r($ss->listList(),true), '</pre>';
- $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',
'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' ),
'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' ),
- ),
+
)
)
);
'feedName' => __( 'Name', 'StreamSend' ),
);
}
+
+ // validate the api key and login input
public function is_valid_setting(){
$settings = $this->get_plugin_settings();
echo '<h4 style="color:red; margin: 2px;"> Invalid Login or API Key</h4>';
echo '</div>';
}
+
+ // use JS to auto input the audience ID into the audience field and make the input readOnly
?>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function(event) {
var audienceId = "<?php echo $audienceId; ?>";
document.getElementById("glm_streamsend_audience").value = audienceId;
document.getElementById("glm_streamsend_audience").readOnly = true;
+
});
</script>
<?php
}
+
+ // generate the error notice to be displayed if the api or login input is invalid
public function errorNotice(){
if($this->errorFlag){
echo '<div class="push-alert-red" style="border-left: 1px solid #E6DB55; border-right: 1px solid #E6DB55;"> ';
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' );
$ret->contact->id,
$contactData
);
+ }else if(!empty($listId)){
+ $listAdd = $ss->addToList($contactData, $this->listID);
} else {
$contacts = $ss->contactCreate(
$contactData,
STREAMSEND_DEFAULT_DELIVER_WELCOME
);
}
+
if (!$contacts) {
return false;
//echo '<p><h3>Debug Results</h3>'.$ss->debugBuffer.'</p>';
$post_data .= ' <'.trim($k).'>'.trim($v).'</'.trim($k).">\n";
}
}
+
$post_data .= " <activate>$activate</activate>\n"
." <deliver-activation>$deliver</deliver-activation>\n"
." <deliver-welcome>$welcome</deliver-welcome>\n";
$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.'/people') === false) {
$this->debug('contactCreate() Call to sendRequest() failed.');
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);
+
+ }
$this->debug('uploadContacts() A valid list of contacts was not provided.');
return false;
}
-
$this->debug('uploadContacts() '.count($contacts).' contacts provided.');
/**
$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 ) {
$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) ) {
$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;