Last active
January 14, 2018 23:49
-
-
Save julien731/a7a24ba8763af9c31bbe to your computer and use it in GitHub Desktop.
class-salesforce.php
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 | |
/** | |
* Salesforce Web-To-Lead cURL PHP Wrapper. | |
* | |
* This class is a helper for Salesforce web-to-lead | |
* feature. | |
* | |
* @package Salesforce_WTL | |
* @version 0.1.1 | |
* @author Julien Liabeuf <[email protected]> | |
* @license GPL-2.0+ | |
* @link http://n2clic.com | |
* @copyright 2014 N2Clic | |
* @link http://themeavenue.net/salesforce-web-lead-form-curl/ | |
*/ | |
class Salesforce_WTL { | |
/** | |
* Salesforce Auth Key. | |
* | |
* @var string | |
* @since 0.1.0 | |
*/ | |
protected $oid = null; | |
public function __construct() { | |
// Make sure cURL is enabled | |
if( !function_exists( 'curl_init' ) ) { | |
return new WP_Error( 'no_curl', __( 'cURL is not loaded on this server.', 'dba' ) ); | |
} | |
$this->endpoint = esc_url( 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8' ); | |
$this->mapped = array(); // Contains all mapped fields | |
$this->returl = home_url(); | |
$this->debug = null; | |
$this->followloc = TRUE; | |
// Map the default fields | |
$this->map_defaults(); | |
} | |
/** | |
* Set the Salesforce OID. | |
* | |
* The OID key is required for authentifaction | |
* when submitting the data to the service. | |
* | |
* @param string $oid Auth key | |
* @since 0.1.0 | |
*/ | |
public function setOID( $oid ) { | |
$this->oid = $oid; | |
} | |
/** | |
* Set return URL. | |
* | |
* @param string $url Return URL | |
* @since 0.1.0 | |
*/ | |
public function set_return_url( $url ) { | |
$this->returl = esc_url( $url ); | |
} | |
/** | |
* Set debug e-mail. | |
* | |
* @param string $email Debug e-mail | |
* @since 0.1.0 | |
*/ | |
public function set_debug_email( $email ) { | |
$this->debug = $email; | |
} | |
/** | |
* Map custom field. | |
* | |
* Salesforce custom fields are using | |
* a random chain. If custom fields are used | |
* they need to be mapped to the form's field. | |
* | |
* @param string $origin Form field name | |
* @param string $target Salesforce custom field name | |
* @since 0.1.0 | |
*/ | |
public function map_field( $field = array() ) { | |
$defaults = array( | |
'origin' => null, | |
'target' => null, | |
'callback' => 'sanitize_text_field', | |
'required' => false | |
); | |
$field = array_merge( $defaults, $field ); | |
if( is_null( $field['origin'] ) || is_null( $field['target'] ) ) | |
return false; | |
// Sanitize the input | |
$field['origin'] = sanitize_text_field( $field['origin'] ); | |
$field['target'] = sanitize_text_field( $field['target'] ); | |
// Map the field | |
$this->mapped[$field['origin']] = $field; | |
} | |
/** | |
* Map default Salesforce fields. | |
* | |
* @since 0.1.0 | |
*/ | |
protected function map_defaults() { | |
$defaults = array( | |
'first_name' => array( | |
'target' => 'first_name', | |
'callback' => 'sanitize_text_field' | |
), | |
'last_name' => array( | |
'target' => 'last_name', | |
'callback' => 'sanitize_text_field' | |
), | |
'email' => array( | |
'target' => 'email', | |
'callback' => 'sanitize_email' | |
), | |
'phone' => array( | |
'target' => 'phone', | |
'callback' => 'sanitize_text_field' | |
), | |
'company' => array( | |
'target' => 'company', | |
'callback' => 'sanitize_text_field' | |
), | |
'city' => array( | |
'target' => 'city', | |
'callback' => 'sanitize_text_field' | |
), | |
'state' => array( | |
'target' => 'state', | |
'callback' => 'sanitize_text_field' | |
), | |
'street' => array( | |
'target' => 'street', | |
'callback' => 'sanitize_text_field' | |
), | |
'zip' => array( | |
'target' => 'zip', | |
'callback' => 'sanitize_key' | |
), | |
'country' => array( | |
'target' => 'country', | |
'callback' => 'sanitize_text_field' | |
), | |
'lead_source' => array( | |
'target' => 'lead_source', | |
'callback' => 'sanitize_text_field' | |
), | |
'description' => array( | |
'target' => 'description', | |
'callback' => 'sanitize_text_field' | |
), | |
); | |
$this->mapped = array_merge( $this->mapped, apply_filters( 'swtl_default_fields', $defaults ) ); | |
} | |
/** | |
* Return all mapped fields. | |
* | |
* @return array Mapped fields | |
* @since 0.1.0 | |
*/ | |
public function get_mapped_fields() { | |
return $this->mapped; | |
} | |
/** | |
* Clean the POST data. | |
* | |
* We make sure that all the data in the POST | |
* is mapped to a Salesforce field. If not, we ditch it. | |
* | |
* @return array Cleaned data | |
* @since 0.1.0 | |
*/ | |
public function clean_data() { | |
if( !isset( $_POST ) || empty( $_POST ) ) | |
return new WP_Error( 'no_post', __( 'No POST data to send to Salesforce.', 'dba' ) ); | |
$mapped = $this->get_mapped_fields(); | |
$data = $_POST; | |
$clean = array(); | |
$missing = array(); | |
// Check for required fields | |
foreach( $mapped as $field => $settings ) { | |
if( isset( $settings['required'] ) && true === $settings['required'] ) { | |
if( !isset( $_POST[$field] ) || '' == $_POST[$field] ) { | |
array_push( $missing, $field ); | |
} | |
} | |
} | |
// If some fields are missing, save and abort | |
if( !empty( $missing ) ) { | |
$_SESSION['swtl_temp_values'] = $_POST; | |
$_SESSION['swtl_temp_missing'] = serialize( $missing ); | |
wp_redirect( add_query_arg( array( 'error' => 1 ), $this->returl ) ); // Should NOT be the returl | |
exit; | |
} | |
// Iterate through all the POST data | |
foreach( $_POST as $key => $value ) { | |
// Check if the current field is mapped | |
if( !array_key_exists( $key, $mapped ) ) | |
continue; | |
// Set the target field in Salesforce | |
$target = $mapped[$key]['target']; | |
// Sanitization callback | |
$callback = isset( $mapped[$key]['callback'] ) ? $mapped[$key]['callback'] : 'sanitize_text_field'; | |
// Sanitize the value and add it | |
if( function_exists( $callback ) ) { | |
$clean[$target] = $callback( $value ); | |
} else { | |
$clean[$target] = sanitize_text_field( $value ); | |
} | |
} | |
// Return the cleaned data | |
return apply_filters( 'swtl_clean_data', $clean ); | |
} | |
/** | |
* Send the data to Salesforce. | |
* | |
* Send the data contained in the POST to Salesforce | |
* using the mapped fields and the default | |
* Salesforce fields. | |
* | |
* @return bool Result | |
* @since 0.1.0 | |
*/ | |
public function post() { | |
// Get cleaned data | |
$data = $this->clean_data(); | |
// If an error occured during cleaning we return it | |
if( is_wp_error( $data ) ) | |
return $data; | |
// If no data to pass we return a WP_Error | |
if( empty( $data ) ) | |
return new WP_Error( 'no_data', __( 'There is no data to be sent to Salesforce.', 'dba' ) ); | |
// Time to add the OID for auth | |
$data['oid'] = $this->oid; | |
// Add the return URL | |
$data['retURL'] = $this->returl; | |
// Add the debug e-mail if any | |
if( !is_null( $this->debug ) ) | |
$data['debugEmail'] = $this->debug; | |
// Transform the array into a query as we don't use a multipart header | |
$data = http_build_query( $data ); | |
// Open cURL connection | |
$ch = curl_init(); | |
// Set the url, number of POST vars, POST data | |
curl_setopt( $ch, CURLOPT_URL, $this->endpoint ); | |
curl_setopt( $ch, CURLOPT_POST, TRUE ); | |
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); | |
// Set some settings that make it all work :) | |
curl_setopt( $ch, CURLOPT_HEADER, FALSE ); | |
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, FALSE ); | |
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, $this->followloc ); | |
// Execute SalesForce web to lead PHP cURL | |
$result = curl_exec( $ch ); | |
// Close cURL connection | |
curl_close( $ch ); | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello. I want update to contact. what should i do?? please help me write url.