PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / V3.3.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder vV3.3.2
V3.3.2 3.3.1 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 All 139 releases
bit-form / includes / Core / Integration / OneDrive / RecordApiHelper.php

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

159 lines 5.1 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\OneDrive;
4
5 use BitCode\BitForm\Core\Util\ApiResponse;
6 use BitCode\BitForm\Core\Util\FileHandler;
7 use WP_Error;
8
9 class RecordApiHelper
10 {
11 protected $token;
12 protected $formId;
13 protected $entryId;
14 protected $errorApiResponse = [];
15 protected $successApiResponse = [];
16
17 private $logResponse;
18
19 public function __construct($token, $formId, $entryId)
20 {
21 $this->token = $token;
22 $this->formId = $formId;
23 $this->entryId = $entryId;
24 $this->logResponse = new ApiResponse();
25 }
26
27 public function uploadFile($folder, $filePath, $folderId, $parentId)
28 {
29 if ('' === $filePath) {
30 return;
31 }
32
33 $filePath = $this->makeFilePath($filePath);
34 if (is_null($parentId)) {
35 $parentId = $folderId;
36 }
37 $ids = explode('!', $folderId);
38 if (count($ids) < 1) {
39 return new WP_Error(400, 'Invalid folderId format.');
40 }
41 if ('' === $filePath) {
42 return false;
43 }
44 $body = FileHandler::readFile($filePath);
45 if (!$body) {
46 return new WP_Error(423, 'Can\'t open or read file!');
47 }
48 $apiEndpoint = 'https://api.onedrive.com/v1.0/drives/' . $ids[0] . '/items/' . $parentId . ':/' . basename($filePath) . ':/content';
49 $headers = [
50 'Authorization' => 'Bearer ' . $this->token,
51 'Content-Type' => 'application/octet-stream',
52 'Prefer' => 'respond-async',
53 'X-HTTP-Method' => 'PUT'
54 ];
55 $args = [
56 'body' => $body,
57 'headers' => $headers,
58 'timeout' => 30,
59 'method' => 'PUT',
60 ];
61 $response = wp_remote_request($apiEndpoint, $args);
62 $responseBody = wp_remote_retrieve_body($response);
63 return json_decode($responseBody);
64 }
65
66 public function handleAllFiles($folderWithFile, $actions, $folderId, $parentId)
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 $this->uploadAndCleanup($folder, $singleFilePath, $actions, $folderId, $parentId);
78 }
79 } else {
80 $this->uploadAndCleanup($folder, $filePath, $actions, $folderId, $parentId);
81 }
82 }
83 }
84
85 /**
86 * The local copy is the only copy until OneDrive confirms the upload, so a failed
87 * upload (expired token, API error, unreadable file) must keep it on disk.
88 */
89 private function uploadAndCleanup($folder, $filePath, $actions, $folderId, $parentId)
90 {
91 $response = $this->uploadFile($folder, $filePath, $folderId, $parentId);
92 if ($this->storeInState($response)) {
93 $this->deleteFile($filePath, $actions);
94 }
95 }
96
97 /**
98 * @return bool true when OneDrive returned the uploaded item's metadata
99 */
100 protected function storeInState($response)
101 {
102 $response = is_string($response) ? json_decode($response) : $response;
103 if (isset($response->id)) {
104 $this->successApiResponse[] = $response;
105 return true;
106 }
107 $this->errorApiResponse[] = $response;
108 return false;
109 }
110
111 public function deleteFile($filePath, $actions)
112 {
113 if (isset($actions->delete_from_wp) && $actions->delete_from_wp) {
114 $filePath = $this->makeFilePath($filePath);
115 FileHandler::deleteIsFileExists($filePath);
116 }
117 }
118
119 public function makeFilePath($filePath)
120 {
121 // Field values arrive as public file URLs (see IntegrationHandler::handleFileUrl),
122 // so reduce to the stored file name before resolving against the entry directory.
123 $fileName = basename(wp_parse_url($filePath, PHP_URL_PATH) ?: $filePath);
124 return FileHandler::getEntriesFileUploadDir($this->formId, $this->entryId) . DIRECTORY_SEPARATOR . $fileName;
125 }
126
127 public function executeRecordApi($integrationId, $logID, $fieldValues, $fieldMap, $actions, $folderId, $parentId, $formId)
128 {
129 $folderWithFile = [];
130 $actionsAttachments = explode(',', "$actions->attachments");
131
132 if (is_array($actionsAttachments)) {
133 foreach ($actionsAttachments as $actionAttachment) {
134 if (empty($fieldValues[$actionAttachment])) {
135 continue;
136 }
137 $files = is_array($fieldValues[$actionAttachment]) ? $fieldValues[$actionAttachment] : [$fieldValues[$actionAttachment]];
138 foreach ($files as $value) {
139 $folderWithFile[] = ["$actionAttachment" => $value];
140 }
141 }
142 $this->handleAllFiles($folderWithFile, $actions, $folderId, $parentId);
143 }
144
145 $entryDetails = [
146 'formId' => $formId,
147 'entryId' => $this->entryId,
148 'fieldValues' => $fieldValues
149 ];
150
151 if (count($this->errorApiResponse) > 0) {
152 $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);
153 }
154 if (count($this->successApiResponse) > 0) {
155 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'success', 'All Files Uploaded. ' . wp_json_encode($this->successApiResponse), $entryDetails);
156 }
157 }
158 }
159