← All changes
|
includes/Core/Integration/Encharge/EnchargeHandler.php
+44
-19
2.0
→
3.3.1
View file →
| @@ -6,21 +6,32 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\Encharge; |
| 9 | 9 | |
| 10 | +if (!defined('ABSPATH')) { | |
| 11 | + exit; | |
| 12 | +} | |
| 13 | + | |
| 10 | 14 | use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 11 | 15 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 16 | +use BitCode\BitForm\GlobalHelper; | |
| 12 | 17 | use WP_Error; |
| 13 | 18 | |
| 14 | 19 | /** |
| 15 | 20 | * Provide functionality for Encharge integration |
| 16 | 21 | */ |
| 17 | -class EnchargeHandler { | |
| 22 | +class EnchargeHandler | |
| 23 | +{ | |
| 18 | 24 | private $_integrationID; |
| 19 | - public const API_ENDPOINT = 'https://api.encharge.io/v1/'; | |
| 20 | 25 | |
| 21 | - public function __construct($integrationID, $fromID) { | |
| 26 | + private $formId; | |
| 27 | + public static $api_endpoint = 'https://api.encharge.io/v1/'; | |
| 28 | + | |
| 29 | + public function __construct($integrationID, $fromID) | |
| 30 | + { | |
| 22 | 31 | $this->_integrationID = $integrationID; |
| 32 | + | |
| 33 | + $this->formId = $fromID; | |
| 23 | 34 | } |
| 24 | 35 | |
| 25 | 36 | /** |
| 26 | 37 | * Helps to register ajax function's with wp |
| @@ -26,9 +37,10 @@ | ||
| 26 | 37 | * Helps to register ajax function's with wp |
| 27 | 38 | * |
| 28 | 39 | * @return null |
| 29 | 40 | */ |
| 30 | - public static function registerAjax() { | |
| 41 | + public static function registerAjax() | |
| 42 | + { | |
| 31 | 43 | add_action('wp_ajax_bitforms_encharge_authorize', [__CLASS__, 'enChargeAuthorize']); |
| 32 | 44 | add_action('wp_ajax_bitforms_encharge_headers', [__CLASS__, 'enchargeHeaders']); |
| 33 | 45 | } |
| 34 | 46 | |
| @@ -36,13 +48,20 @@ | ||
| 36 | 48 | * Process ajax request for generate_token |
| 37 | 49 | * |
| 38 | 50 | * @return JSON enchagre user Authorization |
| 39 | 51 | */ |
| 40 | - public static function enChargeAuthorize() { | |
| 52 | + public static function enChargeAuthorize() | |
| 53 | + { | |
| 41 | 54 | $authorizationHeader = null; |
| 42 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 43 | - $inputJSON = file_get_contents('php://input'); | |
| 44 | - $requestsParams = json_decode($inputJSON); | |
| 55 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 56 | + GlobalHelper::requirePostMethod(); | |
| 57 | + | |
| 58 | + try { | |
| 59 | + $requestsParams = GlobalHelper::formatRequestData(); | |
| 60 | + } catch (\InvalidArgumentException $e) { | |
| 61 | + wp_send_json_error($e->getMessage(), 400); | |
| 62 | + } | |
| 63 | + | |
| 45 | 64 | if ( |
| 46 | 65 | empty($requestsParams->api_key) |
| 47 | 66 | ) { |
| 48 | 67 | wp_send_json_error( |
| @@ -53,15 +72,13 @@ | ||
| 53 | 72 | 400 |
| 54 | 73 | ); |
| 55 | 74 | } |
| 56 | 75 | |
| 57 | - $apiEndpoint = self::API_ENDPOINT . 'accounts/info'; | |
| 76 | + $apiEndpoint = self::$api_endpoint . 'accounts/info'; | |
| 58 | 77 | $authorizationHeader['Accept'] = 'application/json'; |
| 59 | 78 | $authorizationHeader['X-Encharge-Token'] = $requestsParams->api_key; |
| 60 | 79 | $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 61 | 80 | |
| 62 | - // var_dump($apiResponse); die; | |
| 63 | - | |
| 64 | 81 | if (is_wp_error($apiResponse) || $apiResponse->error) { |
| 65 | 82 | wp_send_json_error( |
| 66 | 83 | empty($apiResponse->code) ? 'Unknown' : $apiResponse->error->message, |
| 67 | 84 | 400 |
| @@ -84,14 +101,21 @@ | ||
| 84 | 101 | * Process ajax request for refresh crm modules |
| 85 | 102 | * |
| 86 | 103 | * @return JSON Encharge field |
| 87 | 104 | */ |
| 88 | - public static function enchargeHeaders() { | |
| 105 | + public static function enchargeHeaders() | |
| 106 | + { | |
| 89 | 107 | $authorizationHeader = null; |
| 90 | 108 | $response = null; |
| 91 | - if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) { | |
| 92 | - $inputJSON = file_get_contents('php://input'); | |
| 93 | - $queryParams = json_decode($inputJSON); | |
| 109 | + if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) { | |
| 110 | + GlobalHelper::requirePostMethod(); | |
| 111 | + | |
| 112 | + try { | |
| 113 | + $queryParams = GlobalHelper::formatRequestData(); | |
| 114 | + } catch (\InvalidArgumentException $e) { | |
| 115 | + wp_send_json_error($e->getMessage(), 400); | |
| 116 | + } | |
| 117 | + | |
| 94 | 118 | if ( |
| 95 | 119 | empty($queryParams->api_key) |
| 96 | 120 | ) { |
| 97 | 121 | wp_send_json_error( |
| @@ -101,13 +125,12 @@ | ||
| 101 | 125 | ), |
| 102 | 126 | 400 |
| 103 | 127 | ); |
| 104 | 128 | } |
| 105 | - $apiEndpoint = self::API_ENDPOINT . 'fields'; | |
| 129 | + $apiEndpoint = self::$api_endpoint . 'fields'; | |
| 106 | 130 | $authorizationHeader['Accept'] = 'application/json'; |
| 107 | 131 | $authorizationHeader['X-Encharge-Token'] = $queryParams->api_key; |
| 108 | 132 | $enChargeResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader); |
| 109 | - // var_dump($enChargeResponse);die; | |
| 110 | 133 | $fields = []; |
| 111 | 134 | if (!is_wp_error($enChargeResponse)) { |
| 112 | 135 | $allFields = $enChargeResponse->items; |
| 113 | 136 | // wp_send_json_success($allFields); |
| @@ -132,9 +155,10 @@ | ||
| 132 | 155 | ); |
| 133 | 156 | } |
| 134 | 157 | } |
| 135 | 158 | |
| 136 | - public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) { | |
| 159 | + public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID) | |
| 160 | + { | |
| 137 | 161 | $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details; |
| 138 | 162 | |
| 139 | 163 | $api_key = $integrationDetails->api_key; |
| 140 | 164 | $fieldMap = $integrationDetails->field_map; |
| @@ -149,9 +173,10 @@ | ||
| 149 | 173 | $recordApiHelper = new RecordApiHelper($api_key, $this->_integrationID, $logID, $entryID); |
| 150 | 174 | $enchagreApiResponse = $recordApiHelper->executeRecordApi( |
| 151 | 175 | $fieldValues, |
| 152 | 176 | $fieldMap, |
| 153 | - $tags | |
| 177 | + $tags, | |
| 178 | + $this->formId | |
| 154 | 179 | ); |
| 155 | 180 | |
| 156 | 181 | if (is_wp_error($enchagreApiResponse)) { |
| 157 | 182 | return $enchagreApiResponse; |