← All changes
|
includes/Core/Integration/ZohoDesk/FilesApiHelper.php
+20
-1
2.10.0
→
3.3.1
View file →
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | + | |
| 2 | 3 | /** |
| 3 | 4 | * ZohoDesk Files Api |
| 4 | 5 | * |
| 5 | 6 | */ |
| @@ -5,8 +6,9 @@ | ||
| 5 | 6 | */ |
| 6 | 7 | |
| 7 | 8 | namespace BitCode\BitForm\Core\Integration\ZohoDesk; |
| 8 | 9 | |
| 10 | +use BitCode\BitForm\Core\Util\FileHandler; | |
| 9 | 11 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 10 | 12 | |
| 11 | 13 | /** |
| 12 | 14 | * Provide functionality for Upload files |
| @@ -30,9 +32,9 @@ | ||
| 30 | 32 | $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 31 | 33 | $this->_defaultHeader['orgId'] = $orgId; |
| 32 | 34 | $this->_defaultHeader['content-type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary; |
| 33 | 35 | $this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 34 | - $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; | |
| 36 | + $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; | |
| 35 | 37 | } |
| 36 | 38 | |
| 37 | 39 | /** |
| 38 | 40 | * Helps to execute upload files api |
| @@ -45,8 +47,9 @@ | ||
| 45 | 47 | * @return mixed $uploadedFiles ID's of uploaded file in Zoho Desk |
| 46 | 48 | */ |
| 47 | 49 | public function uploadFiles($files, $ticketId, $dataCenter) |
| 48 | 50 | { |
| 51 | + $files = self::normalizeFileNames($files); | |
| 49 | 52 | $uploadFileEndpoint = "https://desk.zoho.{$dataCenter}/api/v1/tickets/{$ticketId}/attachments"; |
| 50 | 53 | $payload = ''; |
| 51 | 54 | if (is_array($files)) { |
| 52 | 55 | foreach ($files as $fileIndex => $fileName) { |
| @@ -77,6 +80,22 @@ | ||
| 77 | 80 | |
| 78 | 81 | $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 79 | 82 | |
| 80 | 83 | return $uploadResponse; |
| 84 | + } | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl), | |
| 88 | + * so reduce each to the stored file name before resolving against the entry directory. | |
| 89 | + * | |
| 90 | + * @param mixed $files file name(s) or URL(s) | |
| 91 | + * | |
| 92 | + * @return mixed | |
| 93 | + */ | |
| 94 | + private static function normalizeFileNames($files) | |
| 95 | + { | |
| 96 | + if (is_array($files)) { | |
| 97 | + return array_map([__CLASS__, 'normalizeFileNames'], $files); | |
| 98 | + } | |
| 99 | + return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files; | |
| 81 | 100 | } |
| 82 | 101 | } |