← All changes
|
includes/Core/Integration/ZohoBigin/FilesApiHelper.php
+26
-5
2.0
→
V3.3.2
View file →
| @@ -6,14 +6,16 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace BitCode\BitForm\Core\Integration\ZohoBigin; |
| 9 | 9 | |
| 10 | +use BitCode\BitForm\Core\Util\FileHandler; | |
| 10 | 11 | use BitCode\BitForm\Core\Util\HttpHelper; |
| 11 | 12 | |
| 12 | 13 | /** |
| 13 | 14 | * Provide functionality for Upload files |
| 14 | 15 | */ |
| 15 | -final class FilesApiHelper { | |
| 16 | +final class FilesApiHelper | |
| 17 | +{ | |
| 16 | 18 | private $_defaultHeader; |
| 17 | 19 | private $_apiDomain; |
| 18 | 20 | private $_payloadBoundary; |
| 19 | 21 | private $_basepath; |
| @@ -23,14 +25,15 @@ | ||
| 23 | 25 | * @param Object $tokenDetails Api token details |
| 24 | 26 | * @param Integer $formID ID of the form, for which integration is executing |
| 25 | 27 | * @param Integer $entryID Current submittion ID |
| 26 | 28 | */ |
| 27 | - public function __construct($tokenDetails, $formID, $entryID) { | |
| 29 | + public function __construct($tokenDetails, $formID, $entryID) | |
| 30 | + { | |
| 28 | 31 | $this->_payloadBoundary = wp_generate_password(24); |
| 29 | 32 | $this->_defaultHeader['Authorization'] = "Zoho-oauthtoken {$tokenDetails->access_token}"; |
| 30 | 33 | $this->_defaultHeader['content-type'] = 'multipart/form; boundary=' . $this->_payloadBoundary; |
| 31 | 34 | $this->_apiDomain = \urldecode($tokenDetails->api_domain); |
| 32 | - $this->_basepath = BITFORMS_UPLOAD_DIR . DIRECTORY_SEPARATOR . $formID . DIRECTORY_SEPARATOR . $entryID . DIRECTORY_SEPARATOR; | |
| 35 | + $this->_basepath = FileHandler::getEntriesFileUploadDir($formID, $entryID) . DIRECTORY_SEPARATOR; | |
| 33 | 36 | } |
| 34 | 37 | |
| 35 | 38 | /** |
| 36 | 39 | * Helps to execute upload files api |
| @@ -38,11 +41,13 @@ | ||
| 38 | 41 | * @param Mixed $files Files path |
| 39 | 42 | * @param Mixed $recordID Record id |
| 40 | 43 | * @param String $zohoField zoho bigin upload fieldname |
| 41 | 44 | * |
| 42 | - * @return Array $uploadedFiles ID's of uploaded file in Zoho Bigin | |
| 45 | + * @return mixed $uploadedFiles ID's of uploaded file in Zoho Bigin | |
| 43 | 46 | */ |
| 44 | - public function uploadFiles($files, $module, $recordID, $isPhoto = null) { | |
| 47 | + public function uploadFiles($files, $module, $recordID, $isPhoto = null) | |
| 48 | + { | |
| 49 | + $files = self::normalizeFileNames($files); | |
| 45 | 50 | $uploadFileEndpoint = ''; |
| 46 | 51 | |
| 47 | 52 | if ($isPhoto) { |
| 48 | 53 | $uploadFileEndpoint = "https://www.zohoapis.com/bigin/v1/{$module}/{$recordID}/photo"; |
| @@ -77,6 +82,22 @@ | ||
| 77 | 82 | } |
| 78 | 83 | $payload .= '--' . $this->_payloadBoundary . '--'; |
| 79 | 84 | |
| 80 | 85 | return HttpHelper::post($uploadFileEndpoint, $payload, $this->_defaultHeader); |
| 86 | + } | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * Field values may arrive as public file URLs (see IntegrationHandler::handleFileUrl), | |
| 90 | + * so reduce each to the stored file name before resolving against the entry directory. | |
| 91 | + * | |
| 92 | + * @param mixed $files file name(s) or URL(s) | |
| 93 | + * | |
| 94 | + * @return mixed | |
| 95 | + */ | |
| 96 | + private static function normalizeFileNames($files) | |
| 97 | + { | |
| 98 | + if (is_array($files)) { | |
| 99 | + return array_map([__CLASS__, 'normalizeFileNames'], $files); | |
| 100 | + } | |
| 101 | + return is_string($files) ? basename(wp_parse_url($files, PHP_URL_PATH) ?: $files) : $files; | |
| 81 | 102 | } |
| 82 | 103 | } |