PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
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 2.15.3, at includes/Core/Integration/Dropbox/RecordApiHelper.php

133 lines 3.9 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 $filesize = filesize($filePath);
37 $fp = fopen($filePath, 'rb');
38 $body = fread($fp, $filesize);
39 if (!$body) {
40 return new WP_Error(423, 'Can\'t open file!');
41 }
42
43 $apiEndPoint = $this->contentBaseUri . '/2/files/upload';
44 $headers = [
45 'Authorization' => 'Bearer ' . $this->token,
46 'Content-Type' => 'application/octet-stream',
47 'Dropbox-API-Arg' => wp_json_encode([
48 'path' => $folder . '/' . $this->fileName($filePath),
49 'mode' => 'add',
50 'autorename' => true,
51 'mute' => true,
52 'strict_conflict' => false
53 ]),
54 ];
55 return HttpHelper::post($apiEndPoint, $body, $headers);
56 }
57
58 public function fileName($filePath)
59 {
60 $filePathArray = explode('/', $filePath);
61 $key = count($filePathArray) - 1;
62 $fileName = null;
63 if (key_exists($key, $filePathArray)) {
64 $fileName = $filePathArray[$key];
65 }
66 return $fileName;
67 }
68
69 public function handleAllFiles($folderWithFile, $actions)
70 {
71 foreach ($folderWithFile as $folder => $filePath) {
72 if ('' === $filePath) {
73 continue;
74 }
75 if (is_array($filePath)) {
76 foreach ($filePath as $singleFilePath) {
77 if ('' === $singleFilePath) {
78 continue;
79 }
80 $response = $this->uploadFile($folder, $singleFilePath);
81 $this->storeInState($response);
82 $this->deleteFile($singleFilePath, $actions);
83 }
84 } else {
85 $response = $this->uploadFile($folder, $filePath);
86 $this->storeInState($response);
87 $this->deleteFile($filePath, $actions);
88 }
89 }
90 }
91
92 protected function storeInState($response)
93 {
94 if (isset($response->id)) {
95 $this->successApiResponse[] = $response;
96 } else {
97 $this->errorApiResponse[] = $response;
98 }
99 }
100
101 public function deleteFile($filePath, $actions)
102 {
103 if (isset($actions->delete_from_wp) && $actions->delete_from_wp) {
104 $filePath = $this->makeFilePath($filePath);
105 FileHandler::deleteIsFileExists($filePath);
106 }
107 }
108
109 public function makeFilePath($filePath)
110 {
111 $upDir = wp_upload_dir();
112 return $upDir['basedir'] . '/bitforms/uploads/' . $this->formId . '/' . $this->entryId . '/' . $filePath;
113 }
114
115 public function executeRecordApi($integrationId, $logID, $fieldValues, $fieldMap, $actions)
116 {
117 $folderWithFile = [];
118 foreach ($fieldMap as $value) {
119 if (!is_null($fieldValues[$value->formField])) {
120 $folderWithFile[$value->dropboxFormField] = $fieldValues[$value->formField];
121 }
122 }
123 $this->handleAllFiles($folderWithFile, $actions);
124
125 if (count($this->errorApiResponse) > 0) {
126 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'error', 'Some Files Can\'t Upload For Some Reason. ' . wp_json_encode($this->errorApiResponse));
127 }
128 if (count($this->successApiResponse) > 0) {
129 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'success', 'All Files Uploaded. ' . wp_json_encode($this->successApiResponse));
130 }
131 }
132 }
133