Created
June 12, 2015 23:10
-
-
Save juanjdlt/363b9e18335faad9f0d9 to your computer and use it in GitHub Desktop.
Plugin that saves a lead from OptinMonster to HubSpot CRM (directly)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form role="form" method="POST" action="/" onsubmit="return false;" class="optin-tk-forms"> | |
<input placeholder="Fullname" id="nombre" name="name" type="text" /> | |
<input placeholder="Email" id="email" name="email" type="text" /> | |
<input autcomplete="off" placeholder="ej. @twitter" name="twitter" type="text" class="optinMonster-twitter"/> | |
<!-- [name=twitter] imput field should be set to display:none or any other technique that makes that imput dissapear (HoneyPot Technique) --> | |
<input type="radio" name="contact_type" value="recruiter" checked/>are you recruiter? <input type="radio" name="contact_type" value="candidate"/> Are you candidateo?<br/> | |
<input type="submit" value="Enviar" /> | |
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: OptinMonster + HubSpot CRM | |
* Plugin URI: https://blog.talenteca.com | |
* Description: Saves the lead from an optin directly to HubSpot CRM with custom fields | |
* Author: Juan José de la Torre | |
* Author URI: http://twitter.com/juanjodlt | |
* Version: 0.0.1 | |
*/ | |
add_action( 'wp_footer', 'save_lead_to_hubspot_javascript' ); | |
add_action( 'wp_ajax_save_lead_to_hubspot', 'save_lead_to_hubspot_callback' ); | |
add_action( 'wp_ajax_nopriv_save_lead_to_hubspot', 'save_lead_to_hubspot_callback' ); | |
function save_lead_to_hubspot_javascript() { ?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function() { | |
jQuery(document).on('OptinMonsterOptinSuccess', function(event, props, object) { | |
var email = props.optin_data.fields.email; | |
var name = props.optin_data.fields.name; | |
var twitterHandler = props.optin_data.fields.twitter; | |
var contactType = jQuery('.optin-tk-forms input:radio[name=contact_type]:checked').val() || 'candidato'; | |
var data = { | |
"action": "save_lead_to_hubspot", | |
"email": email, | |
"fullname": name, | |
"twitter_username" : twitterHandler, | |
"contact_type" : contactType | |
}; | |
jQuery.post("<?php echo admin_url('admin-ajax.php');?>", data, function(data, textStatus, jqXHR) { | |
var result = jQuery.parseJSON(data); | |
if (result.error == '') { | |
alert('Thanks for subscribing!.'); | |
object.trackEvent('conversion', object.getProp('ga_campaign'), object.getProp('ga_campaign_id')); | |
} else { | |
alert(result.error); | |
} | |
}); | |
}); | |
}); | |
</script> <?php | |
} | |
function save_lead_to_hubspot_callback() { | |
$fullname = sanitize_text_field($_POST['fullname']); | |
$email = sanitize_email($_POST['email']); | |
$honey_pot_value = $_POST['twitter_username']; #http://haacked.com/archive/2007/09/11/honeypot-captcha.aspx/ | |
$contact_type = sanitize_text_field($_POST['contact_type']); | |
$response = array('error' => '' ); | |
if ($fullname == '' || $email == '' || $contact_type == '') { | |
$response['error'] = 'All fields are requiered!'; | |
echo json_encode($response); | |
wp_die(); | |
} | |
if ($honey_pot_value !== '') { | |
wp_mail('[email protected]', '[blog-alert] ** SPAM ALERT **', admin_email_body_generator($_SERVER, 'honey_pot form field was filled, someones is trying to do spam')); | |
echo json_encode($response); | |
wp_die(); | |
} | |
$hapikey = HUBSPOT_SALES_API_KEY; | |
$hubspot_url_endpoint = 'http://api.hubapi.com/contacts/v1/contact/createOrUpdate/email/'.$email.'/?hapikey='. $hapikey; | |
@list($firstName, $lastName) = @explode(' ', $fullname, 2); | |
$data = array( | |
'properties' => array( | |
array( | |
'property' => 'email', | |
'value' => $email | |
), | |
array( | |
'property' => 'firstname', | |
'value' => $firstName | |
), | |
array( | |
'property' => 'lastname', | |
'value' => $lastName | |
), | |
array( | |
'property' => 'contact_type', | |
'value' => $contact_type | |
) | |
) | |
); | |
$data_as_json = json_encode($data); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_POST, true); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_as_json); | |
curl_setopt($ch, CURLOPT_URL, $hubspot_url_endpoint); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$hubSpot_response = curl_exec($ch); | |
$call_info = curl_getinfo($ch); | |
curl_close($ch); | |
if ($call_info['http_code'] !== 200) { | |
$response['error'] = 'Error!'; | |
wp_mail('[email protected]', '[blog-alert] ** HubSpot API call failed **', admin_email_body_generator(array('server_data' => $_SERVER), 'Failed when trying to save a lead. HubSpot API response :: '. "\n\n" . var_export(json_decode($hubSpot_response), true))); | |
echo json_encode($response); | |
wp_die(); | |
} else { | |
echo json_encode($response); | |
wp_die(); | |
} | |
} | |
function admin_email_body_generator($request, $message) { | |
$body = $message . "\n\n"; | |
$body .= 'Server data (IP and stuff) :: ' ."\n\n"; | |
$body .= var_export($request, true); | |
return $body; | |
} | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment