| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Twilio Integration |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace BitCode\BitForm\Core\Integration\Twilio; |
| 9 |
|
| 10 |
if (!defined('ABSPATH')) { |
| 11 |
exit; |
| 12 |
} |
| 13 |
|
| 14 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 15 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 16 |
use BitCode\BitForm\GlobalHelper; |
| 17 |
use WP_Error; |
| 18 |
|
| 19 |
final class TwilioHandler |
| 20 |
{ |
| 21 |
private $_integrationID; |
| 22 |
public static $apiBaseUri = 'https://api.twilio.com/2010-04-01'; |
| 23 |
protected $_defaultHeader; |
| 24 |
|
| 25 |
private $_formID; |
| 26 |
|
| 27 |
public function __construct($integrationID, $formID) |
| 28 |
{ |
| 29 |
$this->_integrationID = $integrationID; |
| 30 |
|
| 31 |
$this->_formID = $formID; |
| 32 |
} |
| 33 |
|
| 34 |
public static function registerAjax() |
| 35 |
{ |
| 36 |
add_action('wp_ajax_bitforms_twilio_authorization', [__CLASS__, 'checkAuthorization']); |
| 37 |
} |
| 38 |
|
| 39 |
public static function checkAuthorization() |
| 40 |
{ |
| 41 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 42 |
GlobalHelper::requirePostMethod(); |
| 43 |
|
| 44 |
try { |
| 45 |
$tokenRequestParams = GlobalHelper::formatRequestData(); |
| 46 |
} catch (\InvalidArgumentException $e) { |
| 47 |
wp_send_json_error($e->getMessage(), 400); |
| 48 |
} |
| 49 |
|
| 50 |
if ( |
| 51 |
empty($tokenRequestParams->sid) |
| 52 |
|| empty($tokenRequestParams->token) |
| 53 |
|| empty($tokenRequestParams->from_num) |
| 54 |
) { |
| 55 |
wp_send_json_error( |
| 56 |
__( |
| 57 |
'Requested parameter is empty', |
| 58 |
'bit-form' |
| 59 |
), |
| 60 |
400 |
| 61 |
); |
| 62 |
} |
| 63 |
$header = [ |
| 64 |
'Authorization' => 'Basic ' . base64_encode("$tokenRequestParams->sid:$tokenRequestParams->token"), |
| 65 |
'Accept' => '*/*', |
| 66 |
'verify' => false |
| 67 |
]; |
| 68 |
$apiEndpoint = self::$apiBaseUri . '/Accounts'; |
| 69 |
|
| 70 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $header); |
| 71 |
|
| 72 |
$xml = simplexml_load_string($apiResponse); |
| 73 |
$json = wp_json_encode($xml); |
| 74 |
$response = json_decode($json, true); |
| 75 |
|
| 76 |
if (array_key_exists('RestException', $response)) { |
| 77 |
wp_send_json_error( |
| 78 |
'Unauthorize', |
| 79 |
400 |
| 80 |
); |
| 81 |
} else { |
| 82 |
wp_send_json_success($apiResponse, 200); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 88 |
{ |
| 89 |
$integrationDetails = $integrationData->integration_details; |
| 90 |
if (is_string($integrationDetails)) { |
| 91 |
$integrationDetails = json_decode($integrationDetails); |
| 92 |
} |
| 93 |
$fieldMap = $integrationDetails->field_map; |
| 94 |
$sid = $integrationDetails->sid; |
| 95 |
$token = $integrationDetails->token; |
| 96 |
$from_num = $integrationDetails->from_num; |
| 97 |
if ( |
| 98 |
empty($sid) |
| 99 |
|| empty($token) |
| 100 |
|| empty($from_num) |
| 101 |
|| empty($fieldMap) |
| 102 |
) { |
| 103 |
$error = new WP_Error('REQ_FIELD_EMPTY', __('SID, Auth Token,From Number and mapping fields are required for rapidmail api', 'bit-form')); |
| 104 |
return $error; |
| 105 |
} |
| 106 |
$recordApiHelper = new RecordApiHelper($integrationDetails, $sid, $token, $from_num, $logID, $this->_formID, $entryID); |
| 107 |
$twilioResponse = $recordApiHelper->executeRecordApi( |
| 108 |
$this->_integrationID, |
| 109 |
$fieldValues, |
| 110 |
$fieldMap |
| 111 |
); |
| 112 |
return $twilioResponse; |
| 113 |
} |
| 114 |
} |
| 115 |
|