146 lines
4.9 KiB
PHP
146 lines
4.9 KiB
PHP
<?php
|
|
/*
|
|
Plugin Name: Affiliate Host 'n Post
|
|
Description: Handles form submissions and integrates with Salesforce and Five9 APIs.
|
|
Version: 1.2
|
|
Author: Anthony Volpe
|
|
*/
|
|
|
|
// Salesforce configuration
|
|
define('SALESFORCE_OID', "00D1I000000mJ0Q");
|
|
define('LEAD_SOURCE', "Web");
|
|
define('MEMBER_STATUS', "Web response");
|
|
|
|
// Handle the Salesforce form submission
|
|
function postsf($data) {
|
|
|
|
$sfdc_env = get_option('select-environment');
|
|
|
|
// Set up Salesforce configuration based on the environment
|
|
if ($sfdc_env == 'full') {
|
|
$sfdc_oid = '00DDh0000009Umu';
|
|
$webtolead_url = 'https://test.salesforce.com/servlet/servlet.WebToLead';
|
|
} else {
|
|
$sfdc_oid = '00D1I000000mJ0Q';
|
|
$webtolead_url = 'https://webto.salesforce.com/servlet/servlet.WebToLead';
|
|
}
|
|
|
|
// Prepare POST data with the selected environment's `Campaign_ID` and `sfdc_oid`
|
|
$cleanPOST = array(
|
|
'first_name' => sanitize_text_field($data['first-name']),
|
|
'last_name' => sanitize_text_field($data['last-name']),
|
|
'phone' => sanitize_text_field($data['phone']),
|
|
'email' => sanitize_email($data['your-email']),
|
|
'zip' => sanitize_text_field($data['zip']),
|
|
'Campaign_ID' => sanitize_text_field($data['campaign_ID']),
|
|
'oid' => $sfdc_oid, // Dynamic OID based on environment
|
|
'lead_source' => LEAD_SOURCE,
|
|
'Custom_Field_1__c' => sanitize_text_field($data['subid1']),
|
|
'Custom_Field_2__c' => sanitize_text_field($data['subid2']),
|
|
'Custom_Field_3__c' => sanitize_text_field($data['subid3']),
|
|
'Custom_Field_4__c' => sanitize_text_field($data['subid4'])
|
|
);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $webtolead_url); // Dynamic URL based on environment
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
curl_setopt($ch, CURLOPT_HEADER, true);
|
|
|
|
$response = curl_exec($ch);
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
|
$error = curl_error($ch);
|
|
|
|
curl_close($ch);
|
|
|
|
if ($error) {
|
|
return "CURL Error: $error";
|
|
}
|
|
|
|
// Check if Salesforce returned an HTTP error
|
|
if ($http_code >= 400) {
|
|
return "Salesforce HTTP Error: Code $http_code. Response: $response";
|
|
}
|
|
|
|
// If response is empty, indicate failure explicitly
|
|
return $response ? $response : "Failed to submit lead to Salesforce.";
|
|
}
|
|
|
|
// Function to handle the Five9 form submission
|
|
function post59($data) {
|
|
$Campaign_ID = '701130000026vNy';
|
|
date_default_timezone_set('America/New_York');
|
|
$F9domain = "connect america";
|
|
$F9list = sanitize_text_field($data['callback']);
|
|
$newphone = preg_replace('/^1|\D/', '', sanitize_text_field($data['phone']));
|
|
$webDialerKey = "YOUR_WEB_DIALER_KEY"; // Replace with actual WebDialer key
|
|
|
|
// Prepare POST data
|
|
$cleanPOST = array(
|
|
'first_name' => sanitize_text_field($data['first-name']),
|
|
'last_name' => sanitize_text_field($data['last-name']),
|
|
'number1' => $newphone,
|
|
'F9domain' => $F9domain,
|
|
'F9list' => $F9list,
|
|
'salesforce_id' => $Campaign_ID,
|
|
'WebDialer_Key' => $webDialerKey,
|
|
'F9CallASAP' => true
|
|
);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, "https://api.five9.com/web2campaign/AddToList");
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($cleanPOST));
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$response = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
// Parse HTML response with DOMDocument
|
|
$dom = new DOMDocument;
|
|
@$dom->loadHTML($response); // Suppress warnings for HTML formatting issues
|
|
|
|
$parsed_response = [
|
|
'error_code' => '',
|
|
'error_description' => ''
|
|
];
|
|
|
|
$inputs = $dom->getElementsByTagName('input');
|
|
foreach ($inputs as $input) {
|
|
$name = $input->getAttribute('name');
|
|
$value = $input->getAttribute('value');
|
|
|
|
if ($name === 'F9errCode') {
|
|
$parsed_response['error_code'] = $value;
|
|
} elseif ($name === 'F9errDesc') {
|
|
$parsed_response['error_description'] = $value;
|
|
}
|
|
}
|
|
|
|
return json_encode($parsed_response);
|
|
}
|
|
|
|
// Register REST API route for form submission
|
|
add_action('rest_api_init', function () {
|
|
register_rest_route('affiliates/v1', '/form', array(
|
|
'methods' => 'POST',
|
|
'callback' => 'handle_form_submission',
|
|
'permission_callback' => '__return_true' // Allows public access; adjust for security as needed
|
|
));
|
|
});
|
|
|
|
|
|
// Callback function to handle the form submission
|
|
// Handle REST API response formatting
|
|
function handle_form_submission($request) {
|
|
$data = $request->get_params();
|
|
$response1 = postsf($data);
|
|
$response2 = post59($data);
|
|
|
|
return new WP_REST_Response(array(
|
|
'salesforce_response' => $response1 ? "Success" : "Failed",
|
|
'five9_response' => strip_tags($response2)
|
|
), 200);
|
|
}
|