| 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 |
$response = $this->uploadFile($folder, $singleFilePath, $folderId, $parentId); |
| 78 |
$this->storeInState($response); |
| 79 |
$this->deleteFile($singleFilePath, $actions); |
| 80 |
} |
| 81 |
} else { |
| 82 |
$response = $this->uploadFile($folder, $filePath, $folderId, $parentId); |
| 83 |
$this->storeInState($response); |
| 84 |
$this->deleteFile($filePath, $actions); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
protected function storeInState($response) |
| 90 |
{ |
| 91 |
$response = is_string($response) ? json_decode($response) : $response; |
| 92 |
if (isset($response->id)) { |
| 93 |
$this->successApiResponse[] = $response; |
| 94 |
} else { |
| 95 |
$this->errorApiResponse[] = $response; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
public function deleteFile($filePath, $actions) |
| 100 |
{ |
| 101 |
if (isset($actions->delete_from_wp) && $actions->delete_from_wp) { |
| 102 |
$filePath = $this->makeFilePath($filePath); |
| 103 |
FileHandler::deleteIsFileExists($filePath); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
public function makeFilePath($filePath) |
| 108 |
{ |
| 109 |
// Field values arrive as public file URLs (see IntegrationHandler::handleFileUrl), |
| 110 |
// so reduce to the stored file name before resolving against the entry directory. |
| 111 |
$fileName = basename(wp_parse_url($filePath, PHP_URL_PATH) ?: $filePath); |
| 112 |
return FileHandler::getEntriesFileUploadDir($this->formId, $this->entryId) . DIRECTORY_SEPARATOR . $fileName; |
| 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 |
if (empty($fieldValues[$actionAttachment])) { |
| 123 |
continue; |
| 124 |
} |
| 125 |
$files = is_array($fieldValues[$actionAttachment]) ? $fieldValues[$actionAttachment] : [$fieldValues[$actionAttachment]]; |
| 126 |
foreach ($files as $value) { |
| 127 |
$folderWithFile[] = ["$actionAttachment" => $value]; |
| 128 |
} |
| 129 |
} |
| 130 |
$this->handleAllFiles($folderWithFile, $actions, $folderId, $parentId); |
| 131 |
} |
| 132 |
|
| 133 |
$entryDetails = [ |
| 134 |
'formId' => $formId, |
| 135 |
'entryId' => $this->entryId, |
| 136 |
'fieldValues' => $fieldValues |
| 137 |
]; |
| 138 |
|
| 139 |
if (count($this->errorApiResponse) > 0) { |
| 140 |
$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); |
| 141 |
} |
| 142 |
if (count($this->successApiResponse) > 0) { |
| 143 |
$this->logResponse->apiResponse($logID, $integrationId, ['type' => 'record', 'type_name' => 'insert'], 'success', 'All Files Uploaded. ' . wp_json_encode($this->successApiResponse), $entryDetails); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|