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