| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* SendFox Integration |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Integration\SendFox; |
| 8 |
|
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 14 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 15 |
use BitCode\BitForm\GlobalHelper; |
| 16 |
use WP_Error; |
| 17 |
|
| 18 |
class SendFoxHandler |
| 19 |
{ |
| 20 |
private $integrationID; |
| 21 |
public static $baseUrl = 'https://api.sendfox.com/'; |
| 22 |
|
| 23 |
private $formId; |
| 24 |
|
| 25 |
public function __construct($integrationID, $fromID) |
| 26 |
{ |
| 27 |
$this->integrationID = $integrationID; |
| 28 |
$this->formId = $fromID; |
| 29 |
} |
| 30 |
|
| 31 |
public static function registerAjax() |
| 32 |
{ |
| 33 |
add_action('wp_ajax_bitforms_sendFox_authorize', [__CLASS__, 'sendFoxAuthorize']); |
| 34 |
add_action('wp_ajax_bitforms_sendfox_fetch_all_list', [__CLASS__, 'fetchContactLists']); |
| 35 |
} |
| 36 |
|
| 37 |
public static function sendFoxAuthorize($requestParams) |
| 38 |
{ |
| 39 |
if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 40 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 41 |
} |
| 42 |
|
| 43 |
GlobalHelper::requirePostMethod(); |
| 44 |
|
| 45 |
try { |
| 46 |
$requestParams = GlobalHelper::formatRequestData(); |
| 47 |
} catch (\InvalidArgumentException $e) { |
| 48 |
wp_send_json_error($e->getMessage(), 400); |
| 49 |
} |
| 50 |
if (empty($requestParams->access_token)) { |
| 51 |
wp_send_json_error( |
| 52 |
__( |
| 53 |
'Requested parameter is empty', |
| 54 |
'bit-form' |
| 55 |
), |
| 56 |
400 |
| 57 |
); |
| 58 |
} |
| 59 |
$apiEndpoints = self::$baseUrl . 'me'; |
| 60 |
$header = [ |
| 61 |
'Authorization' => "Bearer {$requestParams->access_token}", |
| 62 |
'Accept' => 'application/json', |
| 63 |
]; |
| 64 |
|
| 65 |
$response = HttpHelper::get($apiEndpoints, null, $header); |
| 66 |
if ('Unauthenticated.' !== $response->message) { |
| 67 |
wp_send_json_success($response, 200); |
| 68 |
} else { |
| 69 |
wp_send_json_error( |
| 70 |
'The token is invalid', |
| 71 |
400 |
| 72 |
); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
public static function fetchContactLists($requestParams) |
| 77 |
{ |
| 78 |
if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 79 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 80 |
} |
| 81 |
|
| 82 |
GlobalHelper::requirePostMethod(); |
| 83 |
|
| 84 |
try { |
| 85 |
$requestParams = GlobalHelper::formatRequestData(); |
| 86 |
} catch (\InvalidArgumentException $e) { |
| 87 |
wp_send_json_error($e->getMessage(), 400); |
| 88 |
} |
| 89 |
|
| 90 |
if (empty($requestParams->access_token)) { |
| 91 |
wp_send_json_error( |
| 92 |
__( |
| 93 |
'Requested parameter is empty', |
| 94 |
'bit-form' |
| 95 |
), |
| 96 |
400 |
| 97 |
); |
| 98 |
} |
| 99 |
$apiEndpoints = self::$baseUrl . 'lists'; |
| 100 |
|
| 101 |
$requestParams = [ |
| 102 |
'Authorization' => "Bearer {$requestParams->access_token}", |
| 103 |
'Accept' => 'application/json', |
| 104 |
]; |
| 105 |
|
| 106 |
$response = HttpHelper::get($apiEndpoints, null, $requestParams); |
| 107 |
|
| 108 |
if ('Unauthenticated.' !== $response->message) { |
| 109 |
wp_send_json_success($response, 200); |
| 110 |
} else { |
| 111 |
wp_send_json_error( |
| 112 |
'The token is invalid', |
| 113 |
400 |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 119 |
{ |
| 120 |
$integrationDetails = json_decode($integrationData->integration_details); |
| 121 |
$access_token = $integrationDetails->access_token; |
| 122 |
$listId = $integrationDetails->listId; |
| 123 |
$mainAction = $integrationDetails->mainAction; |
| 124 |
$fieldMap = $integrationDetails->field_map; |
| 125 |
|
| 126 |
if ( |
| 127 |
empty($mainAction) |
| 128 |
|| empty($access_token) |
| 129 |
) { |
| 130 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for SendFox api', 'bit-form')); |
| 131 |
} |
| 132 |
$recordApiHelper = new RecordApiHelper($access_token, $this->integrationID, $logID, $entryID); |
| 133 |
$sendFoxApiResponse = $recordApiHelper->execute( |
| 134 |
$listId, |
| 135 |
$mainAction, |
| 136 |
$fieldValues, |
| 137 |
$fieldMap, |
| 138 |
$access_token, |
| 139 |
$integrationDetails, |
| 140 |
$this->formId |
| 141 |
); |
| 142 |
|
| 143 |
if (is_wp_error($sendFoxApiResponse)) { |
| 144 |
return $sendFoxApiResponse; |
| 145 |
} |
| 146 |
return $sendFoxApiResponse; |
| 147 |
} |
| 148 |
} |
| 149 |
|