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