| @@ -8,8 +8,11 @@ | ||
| 8 | 8 | namespace BitCode\BitForm\Core\Integration; |
| 9 | 9 | |
| 10 | 10 | use BitCode\BitForm\Core\Database\FormEntryModel; |
| 11 | 11 | use BitCode\BitForm\Core\Database\FormModel; |
| 12 | +use BitCode\BitForm\Core\Util\ApiResponse; | |
| 13 | +use BitCode\BitForm\Core\Util\IpTool; | |
| 14 | +use BitCode\BitForm\Core\Util\Log; | |
| 12 | 15 | use BitCode\BitForm\Core\Util\SmartTags; |
| 13 | 16 | use BitCode\BitForm\Core\Util\Utilities; |
| 14 | 17 | /** |
| 15 | 18 | * Provides details of available integration and helps to |
| @@ -20,11 +23,23 @@ | ||
| 20 | 23 | |
| 21 | 24 | final class Integrations |
| 22 | 25 | { |
| 23 | 26 | public $integrations = []; |
| 27 | + private static $_instance = null; | |
| 24 | 28 | |
| 25 | 29 | /** |
| 26 | 30 | * Undocumented function |
| 31 | + */ | |
| 32 | + public static function getInstance() | |
| 33 | + { | |
| 34 | + if (null === self::$_instance) { | |
| 35 | + self::$_instance = new Integrations(); | |
| 36 | + } | |
| 37 | + return self::$_instance; | |
| 38 | + } | |
| 39 | + | |
| 40 | + /** | |
| 41 | + * Undocumented function | |
| 27 | 42 | * |
| 28 | 43 | * @return array |
| 29 | 44 | */ |
| 30 | 45 | public function getAllIntegrations() |
| @@ -56,10 +71,10 @@ | ||
| 56 | 71 | |
| 57 | 72 | /** |
| 58 | 73 | * Checks a Integration Exists or not |
| 59 | 74 | * |
| 60 | - * @param String $name Name of Integration | |
| 61 | - * @return boolean | |
| 75 | + * @param string $name Name of Integration | |
| 76 | + * @return boolean | string | |
| 62 | 77 | */ |
| 63 | 78 | protected static function isExists($name) |
| 64 | 79 | { |
| 65 | 80 | $path = Utilities::getRealPath(__DIR__, $name); |
| @@ -87,11 +102,11 @@ | ||
| 87 | 102 | * This function helps to get single integration information |
| 88 | 103 | * |
| 89 | 104 | * @return bool setIntegration() |
| 90 | 105 | */ |
| 91 | - public function getIntegration($integartionBaseName) | |
| 106 | + public function getIntegration($integrationBaseName) | |
| 92 | 107 | { |
| 93 | - return $this->setIntegration($integartionBaseName); | |
| 108 | + return $this->setIntegration($integrationBaseName); | |
| 94 | 109 | } |
| 95 | 110 | |
| 96 | 111 | public function registerAjax() |
| 97 | 112 | { |
| @@ -97,14 +112,14 @@ | ||
| 97 | 112 | { |
| 98 | 113 | $dirs = new FilesystemIterator(__DIR__); |
| 99 | 114 | foreach ($dirs as $dirInfo) { |
| 100 | 115 | if ($dirInfo->isDir()) { |
| 101 | - $integartionBaseName = basename($dirInfo); | |
| 116 | + $integrationBaseName = basename($dirInfo); | |
| 102 | 117 | if ( |
| 103 | - file_exists(__DIR__ . '/' . $integartionBaseName) | |
| 104 | - && file_exists(__DIR__ . '/' . $integartionBaseName . '/' . $integartionBaseName . 'Handler.php') | |
| 118 | + file_exists(__DIR__ . '/' . $integrationBaseName) | |
| 119 | + && file_exists(__DIR__ . '/' . $integrationBaseName . '/' . $integrationBaseName . 'Handler.php') | |
| 105 | 120 | ) { |
| 106 | - $integration = __NAMESPACE__ . "\\{$integartionBaseName}\\{$integartionBaseName}Handler"; | |
| 121 | + $integration = __NAMESPACE__ . "\\{$integrationBaseName}\\{$integrationBaseName}Handler"; | |
| 107 | 122 | if (method_exists($integration, 'registerAjax')) { |
| 108 | 123 | $integration::registerAjax(); |
| 109 | 124 | // (new $integration(0, 0))->registerAjax(); |
| 110 | 125 | } |
| @@ -156,12 +171,12 @@ | ||
| 156 | 171 | |
| 157 | 172 | /** |
| 158 | 173 | * This function helps to execute Integration |
| 159 | 174 | * |
| 160 | - * @param Array $integrations List of integrstion to execute. | |
| 175 | + * @param array $integrations List of integration to execute. | |
| 161 | 176 | * Element will be json string like {"id":1} |
| 162 | - * @param Array $fieldValues Values of submitted fields | |
| 163 | - * @param Integer $formID ID of current form | |
| 177 | + * @param array $fieldValues Values of submitted fields | |
| 178 | + * @param integer $formID ID of current form | |
| 164 | 179 | * |
| 165 | 180 | * @return void Nothing to return |
| 166 | 181 | */ |
| 167 | 182 | public static function executeIntegrations($integrations, $fieldValues, $formID, $logID = null, $entryID = 0) |
| @@ -166,28 +181,80 @@ | ||
| 166 | 181 | */ |
| 167 | 182 | public static function executeIntegrations($integrations, $fieldValues, $formID, $logID = null, $entryID = 0) |
| 168 | 183 | { |
| 169 | 184 | $integrationHandler = new IntegrationHandler($formID); |
| 185 | + $logResponse = new ApiResponse(); | |
| 186 | + $intThatNeedNoFileHandling = ['ACF', 'MetaBox', 'Pods']; //these takes raw file and upload it | |
| 187 | + $baseFieldValues = $fieldValues; | |
| 188 | + $entryDetails = [ | |
| 189 | + 'formId' => $formID, | |
| 190 | + 'entryId' => $entryID, | |
| 191 | + 'fieldValues' => $fieldValues | |
| 192 | + ]; | |
| 170 | 193 | if (is_array($integrations)) { |
| 171 | 194 | foreach ($integrations as $integrationIDStr) { |
| 172 | 195 | if (!is_string($integrationIDStr)) { |
| 173 | 196 | return; |
| 174 | 197 | } |
| 175 | - $integrationID = intval(json_decode($integrationIDStr)->id, 10); | |
| 198 | + $currentFieldValues = $baseFieldValues; | |
| 199 | + $integrationID = intval(Utilities::jsonObj($integrationIDStr)->id ?? 0, 10); | |
| 176 | 200 | if (!empty($integrationID) && is_int($integrationID)) { |
| 177 | 201 | $integrationResult |
| 178 | 202 | = $integrationHandler->getAIntegration($integrationID); |
| 179 | 203 | $integrationDetails = is_wp_error($integrationResult) ? null : $integrationResult[0]; |
| 204 | + // Honor enable/disable: a disabled integration is never executed. | |
| 205 | + if (!is_null($integrationDetails) && isset($integrationDetails->status) && empty($integrationDetails->status)) { | |
| 206 | + continue; | |
| 207 | + } | |
| 180 | 208 | $integrationName = is_null($integrationDetails) ? null : ucfirst(str_replace(' ', '', $integrationDetails->integration_type)); |
| 209 | + if ('Brevo(SendinBlue)' === $integrationName) { | |
| 210 | + $integrationName = 'SendinBlue'; | |
| 211 | + } | |
| 181 | 212 | if (!is_null($integrationName) && static::isExists($integrationName)) { |
| 213 | + if (!in_array($integrationName, $intThatNeedNoFileHandling)) { | |
| 214 | + $currentFieldValues = IntegrationHandler::handleFileUrl($currentFieldValues, $formID, $entryID); | |
| 215 | + } | |
| 182 | 216 | $integration = static::isExists($integrationName); |
| 183 | 217 | $handler = new $integration($integrationID, $formID); |
| 184 | 218 | $integDetails = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details; |
| 185 | 219 | if (isset($integDetails->field_map)) { |
| 186 | 220 | $sptagData = self::specialTagFields($integDetails->field_map); |
| 187 | - $fieldValues = $fieldValues + $sptagData; | |
| 221 | + $currentFieldValues = $currentFieldValues + $sptagData; | |
| 188 | 222 | } |
| 189 | - $handler->execute($integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID); | |
| 223 | + // fresh time budget per handler: a chain of slow integrations (30s HTTP | |
| 224 | + // timeout each) must not be killed by max_execution_time mid-loop | |
| 225 | + if (\function_exists('set_time_limit') && false === strpos((string) ini_get('disable_functions'), 'set_time_limit')) { | |
| 226 | + set_time_limit(60); | |
| 227 | + } | |
| 228 | + try { | |
| 229 | + $handler->execute($integrationHandler, $integrationDetails, $currentFieldValues, $entryID, $logID); | |
| 230 | + } catch (\Throwable $executionError) { | |
| 231 | + // one failing integration must not abort the remaining ones | |
| 232 | + Log::debug_log('[+] Integration execution threw: ' . $executionError->getMessage()); | |
| 233 | + $logResponse->apiResponse( | |
| 234 | + $logID, | |
| 235 | + $integrationID, | |
| 236 | + ['type' => 'record', 'type_name' => $integrationName ?: 'Integration'], | |
| 237 | + 'errors', | |
| 238 | + $executionError->getMessage(), | |
| 239 | + $entryDetails | |
| 240 | + ); | |
| 241 | + } | |
| 242 | + } else { | |
| 243 | + do_action('bitform_integration_run_failure', $integrationID, $formID); | |
| 244 | + | |
| 245 | + $errorMsg = apply_filters('bitform_filter_integration_run_failure_message', __('Integration execution failed', 'bit-form'), $integrationID, $formID); | |
| 246 | + | |
| 247 | + Log::debug_log('[+] Integration execution failed: ' . $errorMsg); | |
| 248 | + | |
| 249 | + $logResponse->apiResponse( | |
| 250 | + $logID, | |
| 251 | + $integrationID, | |
| 252 | + ['type' => 'record', 'type_name' => $integrationName || 'Integration'], | |
| 253 | + 'errors', | |
| 254 | + $errorMsg, | |
| 255 | + $entryDetails | |
| 256 | + ); | |
| 190 | 257 | } |
| 191 | 258 | } |
| 192 | 259 | } |
| 193 | 260 | } |
| @@ -206,6 +273,77 @@ | ||
| 206 | 273 | foreach ($integrations as $key => $value) { |
| 207 | 274 | self::executeIntegrations($value, $fieldValue, $formID, $logID, $entryID); |
| 208 | 275 | } |
| 209 | 276 | self::entryDeleted($formID, $entryID); |
| 277 | + } | |
| 278 | + | |
| 279 | + /** | |
| 280 | + * This function helps to save connected integration app | |
| 281 | + * | |
| 282 | + * @return wp_error | mixed | |
| 283 | + */ | |
| 284 | + public function saveConnectedIntegrationApp($appConfig) | |
| 285 | + { | |
| 286 | + // error_log('[+] saveConnectedIntegrationApp: ' . print_r($appConfig, true)); | |
| 287 | + $connectionName = $appConfig->name; | |
| 288 | + $connectionType = $appConfig->type; | |
| 289 | + // $connectionDetails = wp_json_encode($appConfig); | |
| 290 | + | |
| 291 | + if (empty($appConfig->details)) { | |
| 292 | + unset($appConfig->name, $appConfig->id); | |
| 293 | + | |
| 294 | + $connectionDetails = !is_string($appConfig) ? | |
| 295 | + wp_json_encode($appConfig) : $appConfig; | |
| 296 | + } else { | |
| 297 | + $connectionDetails = !is_string($appConfig->details) ? | |
| 298 | + wp_json_encode($appConfig->details) : $appConfig->details; | |
| 299 | + } | |
| 300 | + $connectionCategory = 'connected_integration_apps'; | |
| 301 | + | |
| 302 | + // saveIntegration reads user_details['id'|'ip'|'time']; without them the insert | |
| 303 | + // raises null-offset warnings and stores null user_id/created_at | |
| 304 | + $integrationHandler = new IntegrationHandler(0, IpTool::getUserDetail()); | |
| 305 | + | |
| 306 | + return $integrationHandler->saveIntegration($connectionName, $connectionType, $connectionDetails, $connectionCategory); | |
| 307 | + } | |
| 308 | + | |
| 309 | + // get connectd integration app | |
| 310 | + public function getConnectedIntegrationApp($integration_type = null) | |
| 311 | + { | |
| 312 | + $connectionCategory = 'connected_integration_apps'; | |
| 313 | + | |
| 314 | + $integrationHandler = new IntegrationHandler(0); | |
| 315 | + $allConnectedIntegApps = $integrationHandler->getAllIntegration($connectionCategory, $integration_type); | |
| 316 | + | |
| 317 | + if (!is_wp_error($allConnectedIntegApps)) { | |
| 318 | + foreach ($allConnectedIntegApps as $integrationkey => $integrationValue) { | |
| 319 | + $integrationData = [ | |
| 320 | + 'id' => $integrationValue->id, | |
| 321 | + 'name' => $integrationValue->integration_name, | |
| 322 | + 'type' => $integrationValue->integration_type, | |
| 323 | + ]; | |
| 324 | + $allConnectedIntegApps[$integrationkey]->integration_details = wp_json_encode(array_merge( | |
| 325 | + $integrationData, | |
| 326 | + is_string($integrationValue->integration_details) ? | |
| 327 | + (array) json_decode($integrationValue->integration_details) : | |
| 328 | + $integrationValue->integration_details | |
| 329 | + )); | |
| 330 | + } | |
| 331 | + } | |
| 332 | + return $allConnectedIntegApps; | |
| 333 | + } | |
| 334 | + | |
| 335 | + public function deleteConnectedApp($appId) | |
| 336 | + { | |
| 337 | + $integrationHandler = new IntegrationHandler(0); | |
| 338 | + | |
| 339 | + return $integrationHandler->deleteIntegration($appId); | |
| 340 | + } | |
| 341 | + | |
| 342 | + public function renameConnectedApp($appId, $name) | |
| 343 | + { | |
| 344 | + // User details carry the time renameConnectedApp() stamps into updated_at. | |
| 345 | + $integrationHandler = new IntegrationHandler(0, IpTool::getUserDetail()); | |
| 346 | + | |
| 347 | + return $integrationHandler->renameConnectedApp($appId, $name); | |
| 210 | 348 | } |
| 211 | 349 | } |