| @@ -2,32 +2,74 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Util; |
| 4 | 4 | |
| 5 | 5 | use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 6 | -use BitCode\BitForm\Core\Util\IpTool; | |
| 7 | 6 | |
| 8 | 7 | final class ApiResponse |
| 9 | 8 | { |
| 10 | - public function apiResponse($logID, $integrationId, $apiType, $responseType, $responseObj) | |
| 11 | - { | |
| 12 | - if (!$apiType || !$responseObj) { | |
| 13 | - return false; | |
| 14 | - } | |
| 15 | - $apiType = wp_json_encode($apiType); | |
| 16 | - $responseObj = wp_json_encode($responseObj); | |
| 9 | + /** | |
| 10 | + * Log API response | |
| 11 | + * | |
| 12 | + * @param int $logID | |
| 13 | + * @param int $integrationId | |
| 14 | + * @param array $apiType | |
| 15 | + * @param string $responseType | |
| 16 | + * @param mixed $apiResponse | |
| 17 | + * @param array $entryDetails = [] (optional) - (default: ['formId' => 0, 'entryId' => 0, 'fieldValues' => []]) | |
| 18 | + * | |
| 19 | + * @return bool | |
| 20 | + */ | |
| 21 | + public function apiResponse($logID, $integrationId, $apiType, $responseType, $apiResponse, $entryDetails = []) | |
| 22 | + { | |
| 23 | + if (!$apiType) { | |
| 24 | + return false; | |
| 25 | + } | |
| 17 | 26 | |
| 18 | - $ipTool = new IpTool(); | |
| 19 | - $user_details = $ipTool->getUserDetail(); | |
| 20 | - $formEntryModel = new FormEntryLogModel(); | |
| 21 | - $result = $formEntryModel->log_history_insert( | |
| 22 | - array( | |
| 23 | - "log_id" => $logID, | |
| 24 | - "integration_id" => $integrationId, | |
| 25 | - "api_type" => $apiType, | |
| 26 | - "response_type" => $responseType, | |
| 27 | - "response_obj" => $responseObj, | |
| 28 | - "created_at" => $user_details['time'], | |
| 29 | - ) | |
| 30 | - ); | |
| 31 | - return $result; | |
| 27 | + // An empty response means a failed HTTP call; log it or the failure leaves no trace. | |
| 28 | + if (is_wp_error($apiResponse)) { | |
| 29 | + $apiResponse = [ | |
| 30 | + 'error_code' => $apiResponse->get_error_code(), | |
| 31 | + 'description' => $apiResponse->get_error_message(), | |
| 32 | + ]; | |
| 33 | + } elseif (!$apiResponse && !is_array($apiResponse)) { | |
| 34 | + $apiResponse = ['description' => __('Empty response received from the API.', 'bit-form')]; | |
| 32 | 35 | } |
| 36 | + | |
| 37 | + $apiType = wp_json_encode($apiType); | |
| 38 | + $apiResponse = wp_json_encode($apiResponse); | |
| 39 | + | |
| 40 | + $ipTool = new IpTool(); | |
| 41 | + $user_details = $ipTool->getUserDetail(); | |
| 42 | + $formEntryModel = new FormEntryLogModel(); | |
| 43 | + $result = $formEntryModel->log_history_insert( | |
| 44 | + [ | |
| 45 | + 'log_id' => $logID, | |
| 46 | + 'integration_id' => $integrationId, | |
| 47 | + 'api_type' => $apiType, | |
| 48 | + 'response_type' => $responseType, | |
| 49 | + 'response_obj' => $apiResponse, | |
| 50 | + 'created_at' => $user_details['time'], | |
| 51 | + ] | |
| 52 | + ); | |
| 53 | + | |
| 54 | + $fieldValues = []; | |
| 55 | + | |
| 56 | + if (isset($entryDetails['fieldValues']) && $entryDetails['fieldValues']) { | |
| 57 | + $fieldValues = $entryDetails['fieldValues']; | |
| 58 | + } | |
| 59 | + | |
| 60 | + $formId = isset($entryDetails['formId']) && !empty($entryDetails['formId']) ? $entryDetails['formId'] : 0; | |
| 61 | + $entryId = isset($entryDetails['entryId']) && !empty($entryDetails['entryId']) ? $entryDetails['entryId'] : 0; | |
| 62 | + | |
| 63 | + if ('error' === $responseType) { | |
| 64 | + // $this->sendErrorEmail($logID, $integrationId, $apiType, $apiResponse, $entryDetails); | |
| 65 | + do_action('bitform_integration_api_response_error', $formId, $entryId, $logID, $integrationId, $apiType, $apiResponse, $fieldValues); | |
| 66 | + } | |
| 67 | + | |
| 68 | + if ('success' === $responseType) { | |
| 69 | + // $this->sendSuccessEmail($logID, $integrationId, $apiType, $apiResponse, $entryDetails); | |
| 70 | + do_action('bitform_integration_api_response_success', $formId, $entryId, $logID, $integrationId, $apiType, $apiResponse, $fieldValues); | |
| 71 | + } | |
| 72 | + | |
| 73 | + return $result; | |
| 74 | + } | |
| 33 | 75 | } |