Skip to content

Instantly share code, notes, and snippets.

@atwellpub
Created September 13, 2016 23:27
Show Gist options
  • Save atwellpub/adca9138ccda80fea1eff694f28562a6 to your computer and use it in GitHub Desktop.
Save atwellpub/adca9138ccda80fea1eff694f28562a6 to your computer and use it in GitHub Desktop.
For Ninja Forms Devs
<?php if (!defined('ABSPATH')) exit;
/**
* Class NF_Action_Save
*/
if (class_exists('NF_Abstracts_Action')) {
final class NF_Actions_Inbound_Store_Lead extends NF_Abstracts_Action {
/**
* @var string
*/
protected $_name = 'inbound-now-integration';
/**
* @var array
*/
protected $_tags = array();
/**
* @var string
*/
protected $_timing = 'normal';
/**
* @var int
*/
protected $_priority = '10';
/**
* Constructor
*/
public function __construct() {
if (!class_exists('Inbound_Leads')) {
return;
}
parent::__construct();
$this->_nicename = __('Inbound Now Store Lead', 'inbound-pro');
/* check to see if we can leverage Inbound Now's email system */
if (class_exists('Inbound_Mailer_Post_Type')) {
$emails = Inbound_Mailer_Post_Type::get_automation_emails_as('ARRAY');
if ($emails) {
$email_list[] = array(
'key' => 0,
'label' => __('Do not send email.', 'inbound-pro')
);
foreach ($emails as $id => $label) {
$email_list[] = array(
'key' => $id,
'label' => $label
);
}
} else {
$email_list[] = array(
'key' => 0,
'label' => __('No Automation emails detected.', 'inbound-pro')
);
}
$this->_settings['email_actions'] = array(
'name' => 'email_actions',
'type' => 'fieldset',
'width' => 'full',
'label' => __('Email Actions', 'inbound-pro'),
'group' => 'primary',
'settings' => array(
array(
'name' => 'email_id',
'type' => 'select',
'label' => __('Send Lead This Email', 'inbound-pro'),
'width' => 'full',
'group' => 'primary',
'options' => $email_list,
'use_merge_tags' => false,
'help' => __('This feature requires the user set up automated emails in the Inbound eMail component.', 'inbound-pro')
)
)
);
}
/* build lead list segmentation toggles */
$lists = Inbound_Leads::get_lead_lists_as_array();
foreach ($lists as $id => $label) {
$lists_fields[] = array(
'name' => 'lead_list_' . $id,
//'name' => 'lead_list[]',
'type' => 'toggle',
'label' => $label,
'width' => 'one-third',
'group' => 'primary',
'value' => $id,
'use_merge_tags' => false
);
}
$fields = Leads_Field_Map::get_lead_fields();
$fields = Leads_Field_Map::prioritize_lead_fields($fields);
/* add lead to list settings group */
$this->_settings['list_segmentation'] = array(
'name' => 'list_segmentation',
'type' => 'fieldset',
'width' => 'full',
'label' => __('List Segmentation', 'inbound-pro'),
'group' => 'primary',
'settings' => $lists_fields
);
/* create field mapping settings group */
$field_mapping_settings = array();
foreach ($fields as $key => $field) {
$field_mapping_settings[] = array(
'name' => $field['key'],
'type' => 'textbox',
'label' => $field['label'],
'width' => 'one-half',
'group' => 'primary',
'value' => '',
'use_merge_tags' => TRUE,
'help' => __('Map your Ninja Form fields to Inbound Now\'s Leads database', 'inbound-pro')
);
}
$this->_settings['field_mapping'] = array(
'name' => 'field_mapping',
'type' => 'fieldset',
'width' => 'full',
'label' => __('Field Mapping', 'inbound-pro'),
'group' => 'primary',
'settings' => $field_mapping_settings
);
}
/*
* PUBLIC METHODS
*/
public function save($action_settings) {
}
public function process($action_settings, $form_id, $data) {
/* discover lead lists */
$data['lead_lists'] = array();
foreach ($data as $key=>$value) {
if (strstr($key,'lead_list_')) {
$data['lead_lists'][] = str_replace('lead_list_id' , '' , $key);
}
}
/* store lead */
$lead_id = inbound_store_lead( $data , $return = true );
$lead = get_lead($lead_id);
/* send email */
$vid = Inbound_Mailer_Variations::get_next_variant_marker( $template_id );
$args = array(
'email_id' => $template_id,
'vid' => $vid,
'email_address' => $lead->post_title,
'lead_id' => $lead_id,
'tags' => array('inbound-forms'),
'email_recipients' => explode(',' , $form_post_data['inbound_form_lists'])
);
$response = Inbound_Mail_Daemon::send_solo_email( $args );
/* */
return $data;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment