PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.2.0
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 / OneDrive / RecordApiHelper.php

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

143 lines 4.4 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\Admin\Form\Helpers;
6 use BitCode\BitForm\Core\Util\ApiResponse;
7 use BitCode\BitForm\Core\Util\FileHandler;
8 use WP_Error;
9
10 class RecordApiHelper
11 {
12 protected $token;
13 protected $formId;
14 protected $entryId;
15 protected $errorApiResponse = [];
16 protected $successApiResponse = [];
17
18 private $logResponse;
19
20 public function __construct($token, $formId, $entryId)
21 {
22 $this->token = $token;
23 $this->formId = $formId;
24 $this->entryId = $entryId;
25 $this->logResponse = new ApiResponse();
26 }
27
28 public function uploadFile($folder, $filePath, $folderId, $parentId)
29 {
30 if ('' === $filePath) {
31 return;
32 }
33
34 $filePath = $this->makeFilePath($filePath);
35 if (is_null($parentId)) {
36 $parentId = $folderId;
37 }
38 $ids = explode('!', $folderId);
39 if (count($ids) < 1) {
40 return new WP_Error(400, 'Invalid folderId format.');
41 }
42 if ('' === $filePath) {
43 return false;
44 }
45 $body = FileHandler::readFile($filePath);
46 if (!$body) {
47 return new WP_Error(423, 'Can\'t open or read file!');
48 }
49 $apiEndpoint = 'https://api.onedrive.com/v1.0/drives/' . $ids[0] . '/items/' . $parentId . ':/' . basename($filePath) . ':/content';
50 $headers = [
51 'Authorization' => 'Bearer ' . $this->token,
52 'Content-Type' => 'application/octet-stream',
53 'Prefer' => 'respond-async',
54 'X-HTTP-Method' => 'PUT'
55 ];
56 $args = [
57 'body' => $body,
58 'headers' => $headers,
59 'timeout' => 30,
60 'method' => 'PUT',
61 ];
62 $response = wp_remote_request($apiEndpoint, $args);
63 $responseBody = wp_remote_retrieve_body($response);
64 return json_decode($responseBody);
65 }
66
67 public function handleAllFiles($folderWithFile, $actions, $folderId, $parentId)
68 {
69 foreach ($folderWithFile as $folder => $filePath) {
70 if ('' === $filePath) {
71 continue;
72 }
73 if (is_array($filePath)) {
74 foreach ($filePath as $singleFilePath) {
75 if ('' === $singleFilePath) {
76 continue;
77 }
78 $response = $this->uploadFile($folder, $singleFilePath, $folderId, $parentId);
79 $this->storeInState($response);
80 $this->deleteFile($singleFilePath, $actions);
81 }
82 } else {
83 $response = $this->uploadFile($folder, $filePath, $folderId, $parentId);
84 $this->storeInState($response);
85 $this->deleteFile($filePath, $actions);
86 }
87 }
88 }
89
90 protected function storeInState($response)
91 {
92 $response = is_string($response) ? json_decode($response) : $response;
93 if (isset($response->id)) {
94 $this->successApiResponse[] = $response;
95 } else {
96 $this->errorApiResponse[] = $response;
97 }
98 }
99
100 public function deleteFile($filePath, $actions)
101 {
102 if (isset($actions->delete_from_wp) && $actions->delete_from_wp) {
103 $filePath = $this->makeFilePath($filePath);
104 FileHandler::deleteIsFileExists($filePath);
105 }
106 }
107
108 public function makeFilePath($filePath)
109 {
110 $upDir = wp_upload_dir();
111 $encriptedPath = Helpers::getEncryptedEntryId($this->entryId);
112 return $upDir['basedir'] . '/bitforms/uploads/' . $this->formId . '/' . $encriptedPath . '/' . $filePath;
113 }
114
115 public function executeRecordApi($integrationId, $logID, $fieldValues, $fieldMap, $actions, $folderId, $parentId, $formId)
116 {
117 $folderWithFile = [];
118 $actionsAttachments = explode(',', "$actions->attachments");
119
120 if (is_array($actionsAttachments)) {
121 foreach ($actionsAttachments as $actionAttachment) {
122 foreach ($fieldValues[$actionAttachment] as $value) {
123 $folderWithFile[] = ["$actionAttachment" => $value];
124 }
125 $this->handleAllFiles($folderWithFile, $actions, $folderId, $parentId);
126 }
127 }
128
129 $entryDetails = [
130 'formId' => $formId,
131 'entryId' => $this->entryId,
132 'fieldValues' => $fieldValues
133 ];
134
135 if (count($this->errorApiResponse) > 0) {
136 $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);
137 }
138 if (count($this->successApiResponse) > 0) {
139 $this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'success', 'All Files Uploaded. ' . wp_json_encode($this->successApiResponse), $entryDetails);
140 }
141 }
142 }
143