| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Getgist; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 10 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 |
use BitCode\BitForm\GlobalHelper; |
| 12 |
use WP_Error; |
| 13 |
|
| 14 |
class GetgistHandler |
| 15 |
{ |
| 16 |
public static $api_endpoint = 'https://api.getgist.com'; |
| 17 |
|
| 18 |
private $integrationID; |
| 19 |
|
| 20 |
private $formId; |
| 21 |
|
| 22 |
public function __construct($integrationID, $fromID) |
| 23 |
{ |
| 24 |
$this->integrationID = $integrationID; |
| 25 |
$this->formId = $fromID; |
| 26 |
} |
| 27 |
|
| 28 |
public static function registerAjax() |
| 29 |
{ |
| 30 |
add_action('wp_ajax_bitforms_getgist_authorize', [__CLASS__, 'getgistAuthorize']); |
| 31 |
add_action('wp_ajax_bitforms_getgist_tags', [__CLASS__, 'getAllTags']); |
| 32 |
} |
| 33 |
|
| 34 |
public static function getgistAuthorize() |
| 35 |
{ |
| 36 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 37 |
$authorizationHeader = null; |
| 38 |
|
| 39 |
|
| 40 |
GlobalHelper::requirePostMethod(); |
| 41 |
|
| 42 |
try { |
| 43 |
$requestsParams = GlobalHelper::formatRequestData(); |
| 44 |
} catch (\InvalidArgumentException $e) { |
| 45 |
wp_send_json_error($e->getMessage(), 400); |
| 46 |
} |
| 47 |
|
| 48 |
if (empty($requestsParams->api_key)) { |
| 49 |
wp_send_json_error( |
| 50 |
__( |
| 51 |
'Requested parameter is empty', |
| 52 |
'bit-form' |
| 53 |
), |
| 54 |
400 |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
$apiEndpoint = self::$api_endpoint . '/contacts'; |
| 59 |
$authorizationHeader['Authorization'] = "Bearer {$requestsParams->api_key}"; |
| 60 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 61 |
|
| 62 |
if (is_wp_error($apiResponse) || 'authentication_failed' === $apiResponse->code) { |
| 63 |
wp_send_json_error( |
| 64 |
empty($apiResponse->code) ? 'Unknown' : $apiResponse->message, |
| 65 |
400 |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
wp_send_json_success(true); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public static function getAllTags() |
| 74 |
{ |
| 75 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 76 |
$authorizationHeader = null; |
| 77 |
|
| 78 |
|
| 79 |
GlobalHelper::requirePostMethod(); |
| 80 |
|
| 81 |
try { |
| 82 |
$requestsParams = GlobalHelper::formatRequestData(); |
| 83 |
} catch (\InvalidArgumentException $e) { |
| 84 |
wp_send_json_error($e->getMessage(), 400); |
| 85 |
} |
| 86 |
|
| 87 |
if (empty($requestsParams->apiKey)) { |
| 88 |
wp_send_json_error( |
| 89 |
__( |
| 90 |
'Requested parameter is empty', |
| 91 |
'bit-form' |
| 92 |
), |
| 93 |
400 |
| 94 |
); |
| 95 |
} |
| 96 |
|
| 97 |
$apiEndpoint = self::$api_endpoint . '/tags'; |
| 98 |
$authorizationHeader['Authorization'] = "Bearer {$requestsParams->apiKey}"; |
| 99 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 100 |
|
| 101 |
if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) { |
| 102 |
wp_send_json_error( |
| 103 |
empty($apiResponse->code) ? 'Unknown' : $apiResponse->message, |
| 104 |
400 |
| 105 |
); |
| 106 |
} |
| 107 |
$response = []; |
| 108 |
|
| 109 |
foreach ($apiResponse->tags as $tag) { |
| 110 |
$response[] = [ |
| 111 |
'tagId' => $tag->id, |
| 112 |
'tagName' => $tag->name, |
| 113 |
]; |
| 114 |
} |
| 115 |
wp_send_json_success($response, 200); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 120 |
{ |
| 121 |
//$integrationDetails = $integrationData->flow_details; |
| 122 |
|
| 123 |
$integrationDetails = $integrationData->integration_details; |
| 124 |
if (is_string($integrationDetails)) { |
| 125 |
$integrationDetails = json_decode($integrationDetails); |
| 126 |
} |
| 127 |
|
| 128 |
$integId = $integrationData->id; |
| 129 |
|
| 130 |
$api_key = $integrationDetails->api_key; |
| 131 |
$fieldMap = $integrationDetails->field_map; |
| 132 |
$actions = $integrationDetails->actions; |
| 133 |
if ( |
| 134 |
empty($api_key) |
| 135 |
|| empty($fieldMap) |
| 136 |
) { |
| 137 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Gist api', 'bit-form')); |
| 138 |
} |
| 139 |
$recordApiHelper = new RecordApiHelper($api_key, $integId, $logID, $entryID); |
| 140 |
$getgistApiResponse = $recordApiHelper->execute( |
| 141 |
$integId, |
| 142 |
$fieldValues, |
| 143 |
$fieldMap, |
| 144 |
$integrationDetails, |
| 145 |
$this->formId, |
| 146 |
); |
| 147 |
|
| 148 |
if (is_wp_error($getgistApiResponse)) { |
| 149 |
return $getgistApiResponse; |
| 150 |
} |
| 151 |
return $getgistApiResponse; |
| 152 |
} |
| 153 |
} |
| 154 |
|