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