← All changes
|
includes/Core/Integration/ZohoDesk/FilesApiHelper.php
+34
-12
2.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,14 +6,16 @@ | ||
| 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 |
| 13 | 15 | */ |
| 14 | -final class FilesApiHelper { | |
| 16 | +final class FilesApiHelper | |
| 17 | +{ | |
| 15 | 18 | private $_defaultHeader; |
| 16 | 19 | private $_apiDomain; |
| 17 | 20 | private $_payloadBoundary; |
| 18 | 21 | private $_basepath; |
| @@ -18,32 +21,35 @@ | ||
| 18 | 21 | private $_basepath; |
| 19 | 22 | |
| 20 | 23 | /** |
| 21 | 24 | * |
| 22 | - * @param Object $tokenDetails Api token details | |
| 23 | - * @param Integer $formID ID of the form, for which integration is executing | |
| 24 | - * @param Integer $entryID Current submittion ID | |
| 25 | + * @param object $tokenDetails Api token details | |
| 26 | + * @param integer $formID ID of the form, for which integration is executing | |
| 27 | + * @param integer $entryID Current submission ID | |
| 25 | 28 | */ |
| 26 | - public function __construct($tokenDetails, $orgId, $formID, $entryID) { | |
| 29 | + public function __construct($tokenDetails, $orgId, $formID, $entryID) | |
| 30 | + { | |
| 27 | 31 | $this->_payloadBoundary = wp_generate_password(24); |
| 28 | 32 | $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 29 | 33 | $this->_defaultHeader['orgId'] = $orgId; |
| 30 | 34 | $this->_defaultHeader['content-type'] = 'multipart/form-data; boundary=' . $this->_payloadBoundary; |
| 31 | 35 | $this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 32 | - $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; | |
| 36 | + $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; | |
| 33 | 37 | } |
| 34 | 38 | |
| 35 | 39 | /** |
| 36 | 40 | * Helps to execute upload files api |
| 37 | 41 | * |
| 38 | - * @param Mixed $files Files path | |
| 39 | - * @param Bool $isAttachment Check upload type | |
| 40 | - * @param Mixed $module Attachment Module name | |
| 41 | - * @param Mixed $recordID Record id | |
| 42 | + * @param mixed $files Files path | |
| 43 | + * @param bool $isAttachment Check upload type | |
| 44 | + * @param mixed $module Attachment Module name | |
| 45 | + * @param mixed $recordID Record id | |
| 42 | 46 | * |
| 43 | - * @return Array $uploadedFiles ID's of uploaded file in Zoho Desk | |
| 47 | + * @return mixed $uploadedFiles ID's of uploaded file in Zoho Desk | |
| 44 | 48 | */ |
| 45 | - public function uploadFiles($files, $ticketId, $dataCenter) { | |
| 49 | + public function uploadFiles($files, $ticketId, $dataCenter) | |
| 50 | + { | |
| 51 | + $files = self::normalizeFileNames($files); | |
| 46 | 52 | $uploadFileEndpoint = "https://desk.zoho.{$dataCenter}/api/v1/tickets/{$ticketId}/attachments"; |
| 47 | 53 | $payload = ''; |
| 48 | 54 | if (is_array($files)) { |
| 49 | 55 | foreach ($files as $fileIndex => $fileName) { |
| @@ -74,6 +80,22 @@ | ||
| 74 | 80 | |
| 75 | 81 | $uploadResponse = HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 76 | 82 | |
| 77 | 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; | |
| 78 | 100 | } |
| 79 | 101 | } |