| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Groundhogg Integration |
| 5 |
*/ |
| 6 |
|
| 7 |
namespace BitCode\BitForm\Core\Integration\Groundhogg; |
| 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 |
/** |
| 19 |
* Provide functionality for Groundhogg integration |
| 20 |
*/ |
| 21 |
class GroundhoggHandler |
| 22 |
{ |
| 23 |
private $integrationID; |
| 24 |
|
| 25 |
private $formId; |
| 26 |
|
| 27 |
public function __construct($integrationID, $fromID) |
| 28 |
{ |
| 29 |
$this->integrationID = $integrationID; |
| 30 |
|
| 31 |
$this->formId = $fromID; |
| 32 |
} |
| 33 |
|
| 34 |
public static function registerAjax() |
| 35 |
{ |
| 36 |
add_action('wp_ajax_bitforms_groundhogg_authorization_and_fetch_contacts', [__CLASS__, 'fetchAllContacts']); |
| 37 |
add_action('wp_ajax_bitforms_groundhogg_fetch_all_tags', [__CLASS__, 'groundhoggFetchAllTags']); |
| 38 |
} |
| 39 |
|
| 40 |
public static function groundhoggFetchAllTags() |
| 41 |
{ |
| 42 |
if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 43 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 44 |
} |
| 45 |
|
| 46 |
GlobalHelper::requirePostMethod(); |
| 47 |
|
| 48 |
try { |
| 49 |
$requestParams = GlobalHelper::formatRequestData(); |
| 50 |
} catch (\InvalidArgumentException $e) { |
| 51 |
wp_send_json_error($e->getMessage(), 400); |
| 52 |
} |
| 53 |
|
| 54 |
if ( |
| 55 |
empty($requestParams->public_key) || empty($requestParams->token) |
| 56 |
|| empty($requestParams->domainName) |
| 57 |
) { |
| 58 |
wp_send_json_error( |
| 59 |
__( |
| 60 |
'Requested parameter is empty', |
| 61 |
'bit-form' |
| 62 |
), |
| 63 |
400 |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
$authorizationHeader = [ |
| 68 |
'gh-token' => $requestParams->token, |
| 69 |
'gh-public-key' => $requestParams->public_key |
| 70 |
]; |
| 71 |
|
| 72 |
$apiEndpoint = $requestParams->domainName . '/wp-json/gh/v3/tags'; |
| 73 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 74 |
|
| 75 |
if ('success' === $apiResponse->status) { |
| 76 |
$apiResponse; |
| 77 |
wp_send_json_success($apiResponse, 200); |
| 78 |
} else { |
| 79 |
wp_send_json_error( |
| 80 |
'There is an error .', |
| 81 |
400 |
| 82 |
); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
public static function fetchAllContacts() |
| 87 |
{ |
| 88 |
if (!isset($_REQUEST['_ajax_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { |
| 89 |
wp_send_json_error(__('Token expired', 'bit-form'), 401); |
| 90 |
} |
| 91 |
|
| 92 |
GlobalHelper::requirePostMethod(); |
| 93 |
|
| 94 |
try { |
| 95 |
$requestParams = GlobalHelper::formatRequestData(); |
| 96 |
} catch (\InvalidArgumentException $e) { |
| 97 |
wp_send_json_error($e->getMessage(), 400); |
| 98 |
} |
| 99 |
|
| 100 |
if ( |
| 101 |
empty($requestParams->public_key) || empty($requestParams->token) |
| 102 |
|| empty($requestParams->domainName) |
| 103 |
) { |
| 104 |
wp_send_json_error( |
| 105 |
__( |
| 106 |
'Requested parameter is empty', |
| 107 |
'bit-form' |
| 108 |
), |
| 109 |
400 |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
$authorizationHeader = [ |
| 114 |
'gh-token' => $requestParams->token, |
| 115 |
'gh-public-key' => $requestParams->public_key |
| 116 |
]; |
| 117 |
|
| 118 |
$apiEndpoint = $requestParams->domainName . '/wp-json/gh/v3/contacts'; |
| 119 |
$apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 120 |
|
| 121 |
if (!empty($apiResponse->status) && 'success' === $apiResponse->status) { |
| 122 |
$apiResponse; |
| 123 |
wp_send_json_success($apiResponse, 200); |
| 124 |
} else { |
| 125 |
wp_send_json_error( |
| 126 |
'There is an error .', |
| 127 |
400 |
| 128 |
); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) |
| 133 |
{ |
| 134 |
$integrationDetails = json_decode($integrationData->integration_details); |
| 135 |
$token = $integrationDetails->token; |
| 136 |
$public_key = $integrationDetails->public_key; |
| 137 |
$domainName = $integrationDetails->domainName; |
| 138 |
$mainAction = $integrationDetails->mainAction; |
| 139 |
$fieldMap = $integrationDetails->field_map; |
| 140 |
$actions = $integrationDetails->actions; |
| 141 |
$defaultDataConf = $integrationDetails->default; |
| 142 |
|
| 143 |
if ( |
| 144 |
empty($token) |
| 145 |
|| empty($public_key) |
| 146 |
|| empty($domainName) |
| 147 |
|| empty($fieldMap) |
| 148 |
) { |
| 149 |
return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Trello api', 'bit-form')); |
| 150 |
} |
| 151 |
$recordApiHelper = new RecordApiHelper($this->integrationID, $logID, $entryID); |
| 152 |
$acumbamailApiResponse = $recordApiHelper->execute( |
| 153 |
$mainAction, |
| 154 |
$defaultDataConf, |
| 155 |
$fieldValues, |
| 156 |
$fieldMap, |
| 157 |
$public_key, |
| 158 |
$token, |
| 159 |
$actions, |
| 160 |
$integrationDetails, |
| 161 |
$this->formId |
| 162 |
); |
| 163 |
|
| 164 |
if (is_wp_error($acumbamailApiResponse)) { |
| 165 |
return $acumbamailApiResponse; |
| 166 |
} |
| 167 |
return $acumbamailApiResponse; |
| 168 |
} |
| 169 |
} |
| 170 |
|