| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration\Getgist; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 6 |
use BitCode\BitForm\Core\Util\HttpHelper; |
| 7 |
use WP_Error; |
| 8 |
|
| 9 |
class GetgistHandler { |
| 10 |
public const API_ENDPOINT = 'https://api.getgist.com'; |
| 11 |
|
| 12 |
public static function registerAjax() { |
| 13 |
add_action('wp_ajax_bitforms_getgist_authorize', [__CLASS__, 'getgistAuthorize']); |
| 14 |
add_action('wp_ajax_bitforms_getgist_tags', [__CLASS__, 'getAllTags']); |
| 15 |
} |
| 16 |
|
| 17 |
public static function getgistAuthorize() { |
| 18 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 19 |
$authorizationHeader = null; |
| 20 |
$inputJSON = file_get_contents('php://input'); |
| 21 |
$requestsParams = json_decode($inputJSON); |
| 22 |
|
| 23 |
if (empty($requestsParams->api_key)) { |
| 24 |
wp_send_json_error( |
| 25 |
__( |
| 26 |
'Requested parameter is empty', |
| 27 |
'bit-form' |
| 28 |
), |
| 29 |
400 |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
$apiEndpoint = self::API_ENDPOINT . '/contacts'; |
| 34 |
$authorizationHeader['Authorization'] = "Bearer {$requestsParams->api_key}"; |
| 35 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 36 |
|
| 37 |
if (is_wp_error($apiResponse) || 'authentication_failed' === $apiResponse->code) { |
| 38 |
wp_send_json_error( |
| 39 |
empty($apiResponse->code) ? 'Unknown' : $apiResponse->message, |
| 40 |
400 |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
wp_send_json_success(true); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public static function getAllTags() { |
| 49 |
if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { |
| 50 |
$authorizationHeader = null; |
| 51 |
$inputJSON = file_get_contents('php://input'); |
| 52 |
$requestsParams = json_decode($inputJSON); |
| 53 |
|
| 54 |
if (empty($requestsParams->apiKey)) { |
| 55 |
wp_send_json_error( |
| 56 |
__( |
| 57 |
'Requested parameter is empty', |
| 58 |
'bit-form' |
| 59 |
), |
| 60 |
400 |
| 61 |
); |
| 62 |
} |
| 63 |
|
| 64 |
$apiEndpoint = self::API_ENDPOINT . '/tags'; |
| 65 |
$authorizationHeader['Authorization'] = "Bearer {$requestsParams->apiKey}"; |
| 66 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 67 |
|
| 68 |
if (is_wp_error($apiResponse) || 'error' === $apiResponse->status) { |
| 69 |
wp_send_json_error( |
| 70 |
empty($apiResponse->code) ? 'Unknown' : $apiResponse->message, |
| 71 |
400 |
| 72 |
); |
| 73 |
} |
| 74 |
$response = []; |
| 75 |
|
| 76 |
foreach ($apiResponse->tags as $tag) { |
| 77 |
$response[] = [ |
| 78 |
'tagId' => $tag->id, |
| 79 |
'tagName' => $tag->name, |
| 80 |
]; |
| 81 |
} |
| 82 |
wp_send_json_success($response, 200); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { |
| 87 |
//$integrationDetails = $integrationData->flow_details; |
| 88 |
|
| 89 |
$integrationDetails = $integrationData->integration_details; |
| 90 |
if (is_string($integrationDetails)) { |
| 91 |
$integrationDetails = json_decode($integrationDetails); |
| 92 |
} |
| 93 |
|
| 94 |
$integId = $integrationData->id; |
| 95 |
|
| 96 |
$api_key = $integrationDetails->api_key; |
| 97 |
$fieldMap = $integrationDetails->field_map; |
| 98 |
$actions = $integrationDetails->actions; |
| 99 |
if ( |
| 100 |
empty($api_key) |
| 101 |
|| empty($fieldMap) |
| 102 |
) { |
| 103 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Gist api', 'bit-form')); |
| 104 |
} |
| 105 |
$recordApiHelper = new RecordApiHelper($api_key, $integId, $logID); |
| 106 |
$getgistApiResponse = $recordApiHelper->execute( |
| 107 |
$integId, |
| 108 |
$fieldValues, |
| 109 |
$fieldMap, |
| 110 |
$integrationDetails |
| 111 |
); |
| 112 |
|
| 113 |
if (is_wp_error($getgistApiResponse)) { |
| 114 |
return $getgistApiResponse; |
| 115 |
} |
| 116 |
return $getgistApiResponse; |
| 117 |
} |
| 118 |
} |
| 119 |
|