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