PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.4
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Integration / Dropbox / RecordApiHelper.php

RecordApiHelper.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.1.4, at includes/Core/Integration/Dropbox/RecordApiHelper.php

135 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\Integration\Dropbox;
4
5 use BitCode\BitForm\Core\Util\ApiResponse;
6 use BitCode\BitForm\Core\Util\FileHandler;
7 use BitCode\BitForm\Core\Util\HttpHelper;
8 use WP_Error;
9
10 class RecordApiHelper
11 {
12 protected $token;
13 protected $formId;
14 protected $entryId;
15 protected $apiBaseUri = 'https://api.dropboxapi.com';
16 protected $contentBaseUri = 'https://content.dropboxapi.com';
17 protected $errorApiResponse = [];
18 protected $successApiResponse = [];
19
20 private $logResponse;
21
22 public function __construct($token, $formId, $entryId)
23 {
24 $this->token = $token;
25 $this->formId = $formId;
26 $this->entryId = $entryId;
27 $this->logResponse = new ApiResponse();
28 }
29
30 public function uploadFile($folder, $filePath)
31 {
32 if ('' === $filePath) {
33 return;
34 }
35 $filePath = $this->makeFilePath($filePath);
36 $body = FileHandler::readFile($filePath);
37 if (!$body) {
38 return new WP_Error(423, 'Can\'t open file!');
39 }
40
41 $apiEndPoint = $this->contentBaseUri . '/2/files/upload';
42 $headers = [
43 'Authorization' => 'Bearer ' . $this->token,
44 'Content-Type' => 'application/octet-stream',
45 'Dropbox-API-Arg' => wp_json_encode([
46 'path' => $this->normalizePath($folder) . '/' . $this->fileName($filePath),
47 'mode' => 'add',
48 'autorename' => true,
49 'mute' => true,
50 'strict_conflict' => false
51 ]),
52 ];
53 return HttpHelper::post($apiEndPoint, $body, $headers);
54 }
55
56 public function fileName($filePath)
57 {
58 return basename($filePath);
59 }
60
61 public function normalizePath($path)
62 {
63 return str_replace('\\', '/', $path);
64 }
65
66 public function handleAllFiles($folderWithFile, $actions)
67 {
68 foreach ($folderWithFile as $folder => $filePath) {
69 if ('' === $filePath) {
70 continue;
71 }
72 if (is_array($filePath)) {
73 foreach ($filePath as $singleFilePath) {
74 if ('' === $singleFilePath) {
75 continue;
76 }
77 $response = $this->uploadFile($folder, $singleFilePath);
78 $this->storeInState($response);
79 $this->deleteFile($singleFilePath, $actions);
80 }
81 } else {
82 $response = $this->uploadFile($folder, $filePath);
83 $this->storeInState($response);
84 $this->deleteFile($filePath, $actions);
85 }
86 }
87 }
88
89 protected function storeInState($response)
90 {
91 if (isset($response->id)) {
92 $this->successApiResponse[] = $response;
93 } else {
94 $this->errorApiResponse[] = $response;
95 }
96 }
97
98 public function deleteFile($filePath, $actions)
99 {
100 if (isset($actions->delete_from_wp) && $actions->delete_from_wp) {
101 $filePath = $this->makeFilePath($filePath);
102 FileHandler::deleteIsFileExists($filePath);
103 }
104 }
105
106 public function makeFilePath($filePath)
107 {
108 return realpath(FileHandler::getEntriesFileUploadDir($this->formId, $this->entryId) . DIRECTORY_SEPARATOR . $filePath);
109 }
110
111 public function executeRecordApi($integrationId, $logID, $fieldValues, $fieldMap, $actions)
112 {
113 $folderWithFile = [];
114 foreach ($fieldMap as $value) {
115 if (!is_null($fieldValues[$value->formField])) {
116 $folderWithFile[$value->dropboxFormField] = $fieldValues[$value->formField];
117 }
118 }
119 $this->handleAllFiles($folderWithFile, $actions);
120
121 $entryDetails = [
122 'formId' => $this->formId,
123 'entryId' => $this->entryId,
124 'fieldValues' => $fieldValues
125 ];
126
127 if (count($this->errorApiResponse) > 0) {
128 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Some Files Can\'t Upload For Some Reason. ' . wp_json_encode($this->errorApiResponse), $entryDetails);
129 }
130 if (count($this->successApiResponse) > 0) {
131 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'success', 'All Files Uploaded. ' . wp_json_encode($this->successApiResponse), $entryDetails);
132 }
133 }
134 }
135