| @@ -8,9 +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 | +use BitCode\BitForm\Core\Util\Log; | |
| 12 | 14 | use BitCode\BitForm\Core\Util\SmartTags; |
| 15 | +use BitCode\BitForm\Core\Util\Utilities; | |
| 13 | 16 | /** |
| 14 | 17 | * Provides details of available integration and helps to |
| 15 | 18 | * execute available integrations |
| 16 | 19 | */ |
| @@ -16,17 +19,31 @@ | ||
| 16 | 19 | */ |
| 17 | 20 | |
| 18 | 21 | use FilesystemIterator; |
| 19 | 22 | |
| 20 | -final class Integrations { | |
| 23 | +final class Integrations | |
| 24 | +{ | |
| 21 | 25 | public $integrations = []; |
| 26 | + private static $_instance = null; | |
| 22 | 27 | |
| 23 | 28 | /** |
| 24 | 29 | * 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 | |
| 25 | 41 | * |
| 26 | - * @return all | |
| 42 | + * @return array | |
| 27 | 43 | */ |
| 28 | - public function getAllIntegrations() { | |
| 44 | + public function getAllIntegrations() | |
| 45 | + { | |
| 29 | 46 | return $this->allIntegrations(); |
| 30 | 47 | } |
| 31 | 48 | |
| 32 | 49 | /** |
| @@ -31,20 +48,21 @@ | ||
| 31 | 48 | |
| 32 | 49 | /** |
| 33 | 50 | * Undocumented function |
| 34 | 51 | * |
| 35 | - * @return void | |
| 52 | + * @return array | |
| 36 | 53 | */ |
| 37 | - protected function allIntegrations() { | |
| 54 | + protected function allIntegrations() | |
| 55 | + { | |
| 38 | 56 | $dirs = new FilesystemIterator(__DIR__); |
| 39 | 57 | foreach ($dirs as $dirInfo) { |
| 40 | 58 | if ($dirInfo->isDir()) { |
| 41 | - $integartionBaseName = basename($dirInfo); | |
| 59 | + $integrationsBaseName = basename($dirInfo); | |
| 42 | 60 | if ( |
| 43 | - file_exists(__DIR__ . '/' . $integartionBaseName) | |
| 44 | - && file_exists(__DIR__ . '/' . $integartionBaseName . '/' . $integartionBaseName . 'Handler.php') | |
| 61 | + file_exists(__DIR__ . '/' . $integrationsBaseName) | |
| 62 | + && file_exists(__DIR__ . '/' . $integrationsBaseName . '/' . $integrationsBaseName . 'Handler.php') | |
| 45 | 63 | ) { |
| 46 | - $integrations[] = $integartionBaseName; | |
| 64 | + $integrations[] = $integrationsBaseName; | |
| 47 | 65 | } |
| 48 | 66 | } |
| 49 | 67 | } |
| 50 | 68 | return $integrations; |
| @@ -52,19 +70,32 @@ | ||
| 52 | 70 | |
| 53 | 71 | /** |
| 54 | 72 | * Checks a Integration Exists or not |
| 55 | 73 | * |
| 56 | - * @param String $name Name of Integration | |
| 57 | - * @return boolean | |
| 74 | + * @param string $name Name of Integration | |
| 75 | + * @return boolean | string | |
| 58 | 76 | */ |
| 59 | - protected static function isExists($name) { | |
| 60 | - if (file_exists(__DIR__ . '/' . $name) && file_exists(__DIR__ . '/' . $name . '/' . $name . 'Handler.php')) { | |
| 61 | - return __NAMESPACE__ . "\\{$name}\\{$name}Handler"; | |
| 62 | - } elseif (class_exists("BitCode\\BitFormPro\\Integration\\{$name}\\{$name}Handler")) { | |
| 63 | - return "BitCode\\BitFormPro\\Integration\\{$name}\\{$name}Handler"; | |
| 64 | - } else { | |
| 77 | + protected static function isExists($name) | |
| 78 | + { | |
| 79 | + $path = Utilities::getRealPath(__DIR__, $name); | |
| 80 | + if (!empty($path) && $fileName = Utilities::getRealPath(__DIR__ . "/{$path}", "{$name}Handler.php")) { | |
| 81 | + $className = Utilities::getFileNameExceptExtension($fileName); | |
| 82 | + return __NAMESPACE__ . "\\{$path}\\{$className}"; | |
| 83 | + } | |
| 84 | + | |
| 85 | + if (!defined('BITFORMPRO_PLUGIN_DIR_PATH')) { | |
| 65 | 86 | return false; |
| 66 | 87 | } |
| 88 | + | |
| 89 | + $proPath = BITFORMPRO_PLUGIN_DIR_PATH . '/includes/Integration'; | |
| 90 | + $proNamespace = 'BitCode\\BitFormPro\\Integration'; | |
| 91 | + $path = Utilities::getRealPath($proPath, $name); | |
| 92 | + if (!empty($path) && $fileName = Utilities::getRealPath("{$proPath}/{$path}", "{$name}Handler.php")) { | |
| 93 | + $className = Utilities::getFileNameExceptExtension($fileName); | |
| 94 | + return "{$proNamespace}\\{$path}\\{$className}"; | |
| 95 | + } | |
| 96 | + | |
| 97 | + return false; | |
| 67 | 98 | } |
| 68 | 99 | |
| 69 | 100 | /** |
| 70 | 101 | * This function helps to get single integration information |
| @@ -70,22 +101,24 @@ | ||
| 70 | 101 | * This function helps to get single integration information |
| 71 | 102 | * |
| 72 | 103 | * @return bool setIntegration() |
| 73 | 104 | */ |
| 74 | - public function getIntegration($integartionBaseName) { | |
| 75 | - return $this->setIntegration($integartionBaseName); | |
| 105 | + public function getIntegration($integrationBaseName) | |
| 106 | + { | |
| 107 | + return $this->setIntegration($integrationBaseName); | |
| 76 | 108 | } |
| 77 | 109 | |
| 78 | - public function registerAjax() { | |
| 110 | + public function registerAjax() | |
| 111 | + { | |
| 79 | 112 | $dirs = new FilesystemIterator(__DIR__); |
| 80 | 113 | foreach ($dirs as $dirInfo) { |
| 81 | 114 | if ($dirInfo->isDir()) { |
| 82 | - $integartionBaseName = basename($dirInfo); | |
| 115 | + $integrationBaseName = basename($dirInfo); | |
| 83 | 116 | if ( |
| 84 | - file_exists(__DIR__ . '/' . $integartionBaseName) | |
| 85 | - && file_exists(__DIR__ . '/' . $integartionBaseName . '/' . $integartionBaseName . 'Handler.php') | |
| 117 | + file_exists(__DIR__ . '/' . $integrationBaseName) | |
| 118 | + && file_exists(__DIR__ . '/' . $integrationBaseName . '/' . $integrationBaseName . 'Handler.php') | |
| 86 | 119 | ) { |
| 87 | - $integration = __NAMESPACE__ . "\\{$integartionBaseName}\\{$integartionBaseName}Handler"; | |
| 120 | + $integration = __NAMESPACE__ . "\\{$integrationBaseName}\\{$integrationBaseName}Handler"; | |
| 88 | 121 | if (method_exists($integration, 'registerAjax')) { |
| 89 | 122 | $integration::registerAjax(); |
| 90 | 123 | // (new $integration(0, 0))->registerAjax(); |
| 91 | 124 | } |
| @@ -95,9 +128,10 @@ | ||
| 95 | 128 | } |
| 96 | 129 | } |
| 97 | 130 | } |
| 98 | 131 | |
| 99 | - protected static function entryDeleted($formId, $entryId) { | |
| 132 | + protected static function entryDeleted($formId, $entryId) | |
| 133 | + { | |
| 100 | 134 | $formModel = new FormModel(); |
| 101 | 135 | $formContent = $formModel->get( |
| 102 | 136 | [ |
| 103 | 137 | 'id', |
| @@ -122,9 +156,10 @@ | ||
| 122 | 156 | } |
| 123 | 157 | } |
| 124 | 158 | } |
| 125 | 159 | |
| 126 | - public static function specialTagFields($fieldMap) { | |
| 160 | + public static function specialTagFields($fieldMap) | |
| 161 | + { | |
| 127 | 162 | $specialTagFieldValue = []; |
| 128 | 163 | |
| 129 | 164 | foreach ($fieldMap as $value) { |
| 130 | 165 | $triggerValue = $value->formField; |
| @@ -135,17 +170,24 @@ | ||
| 135 | 170 | |
| 136 | 171 | /** |
| 137 | 172 | * This function helps to execute Integration |
| 138 | 173 | * |
| 139 | - * @param Array $integrations List of integrstion to execute. | |
| 174 | + * @param array $integrations List of integration to execute. | |
| 140 | 175 | * Element will be json string like {"id":1} |
| 141 | - * @param Array $fieldValues Values of submitted fields | |
| 142 | - * @param Integer $formID ID of current form | |
| 176 | + * @param array $fieldValues Values of submitted fields | |
| 177 | + * @param integer $formID ID of current form | |
| 143 | 178 | * |
| 144 | 179 | * @return void Nothing to return |
| 145 | 180 | */ |
| 146 | - public static function executeIntegrations($integrations, $fieldValues, $formID, $logID = null, $entryID = 0) { | |
| 181 | + public static function executeIntegrations($integrations, $fieldValues, $formID, $logID = null, $entryID = 0) | |
| 182 | + { | |
| 147 | 183 | $integrationHandler = new IntegrationHandler($formID); |
| 184 | + $logResponse = new ApiResponse(); | |
| 185 | + $entryDetails = [ | |
| 186 | + 'formId' => $formID, | |
| 187 | + 'entryId' => $entryID, | |
| 188 | + 'fieldValues' => $fieldValues | |
| 189 | + ]; | |
| 148 | 190 | if (is_array($integrations)) { |
| 149 | 191 | foreach ($integrations as $integrationIDStr) { |
| 150 | 192 | if (!is_string($integrationIDStr)) { |
| 151 | 193 | return; |
| @@ -152,11 +194,14 @@ | ||
| 152 | 194 | } |
| 153 | 195 | $integrationID = intval(json_decode($integrationIDStr)->id, 10); |
| 154 | 196 | if (!empty($integrationID) && is_int($integrationID)) { |
| 155 | 197 | $integrationResult |
| 156 | - = $integrationHandler->getAIntegration($integrationID); | |
| 198 | + = $integrationHandler->getAIntegration($integrationID); | |
| 157 | 199 | $integrationDetails = is_wp_error($integrationResult) ? null : $integrationResult[0]; |
| 158 | 200 | $integrationName = is_null($integrationDetails) ? null : ucfirst(str_replace(' ', '', $integrationDetails->integration_type)); |
| 201 | + if ('Brevo(SendinBlue)' === $integrationName) { | |
| 202 | + $integrationName = 'SendinBlue'; | |
| 203 | + } | |
| 159 | 204 | if (!is_null($integrationName) && static::isExists($integrationName)) { |
| 160 | 205 | $integration = static::isExists($integrationName); |
| 161 | 206 | $handler = new $integration($integrationID, $formID); |
| 162 | 207 | $integDetails = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details; |
| @@ -164,8 +209,23 @@ | ||
| 164 | 209 | $sptagData = self::specialTagFields($integDetails->field_map); |
| 165 | 210 | $fieldValues = $fieldValues + $sptagData; |
| 166 | 211 | } |
| 167 | 212 | $handler->execute($integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID); |
| 213 | + } else { | |
| 214 | + do_action('bitform_integration_run_failure', $integrationID, $formID); | |
| 215 | + | |
| 216 | + $errorMsg = apply_filters('bitform_filter_integration_run_failure_message', __('Integration execution failed', 'bit-form'), $integrationID, $formID); | |
| 217 | + | |
| 218 | + Log::debug_log('[+] Integration execution failed: ' . print_r($errorMsg, true)); | |
| 219 | + | |
| 220 | + $logResponse->apiResponse( | |
| 221 | + $logID, | |
| 222 | + $integrationID, | |
| 223 | + ['type' => 'record', 'type_name' => $integrationName || 'Integration'], | |
| 224 | + 'errors', | |
| 225 | + $errorMsg, | |
| 226 | + $entryDetails | |
| 227 | + ); | |
| 168 | 228 | } |
| 169 | 229 | } |
| 170 | 230 | } |
| 171 | 231 | } |
| @@ -170,14 +230,82 @@ | ||
| 170 | 230 | } |
| 171 | 231 | } |
| 172 | 232 | } |
| 173 | 233 | |
| 174 | - public static function integrationExecutionHelper($integrations, $fieldValue, $formID, $entryID, $logID) { | |
| 234 | + public static function integrationExecutionHelper($integrations, $fieldValue, $formID, $entryID, $logID) | |
| 235 | + { | |
| 175 | 236 | if (empty($integrations) || empty($formID)) { |
| 176 | 237 | return; |
| 177 | 238 | } |
| 239 | + $trnasientData = get_transient("bitform_trigger_transient_{$entryID}"); | |
| 240 | + $trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; | |
| 241 | + if (!empty($trnasientData['fields'])) { | |
| 242 | + $fieldValue = array_merge($fieldValue, $trnasientData['fields']); | |
| 243 | + } | |
| 178 | 244 | foreach ($integrations as $key => $value) { |
| 179 | 245 | self::executeIntegrations($value, $fieldValue, $formID, $logID, $entryID); |
| 180 | 246 | } |
| 181 | 247 | self::entryDeleted($formID, $entryID); |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * This function helps to save connected integration app | |
| 252 | + * | |
| 253 | + * @return wp_error | mixed | |
| 254 | + */ | |
| 255 | + public function saveConnectedIntegrationApp($appConfig) | |
| 256 | + { | |
| 257 | + // error_log('[+] saveConnectedIntegrationApp: ' . print_r($appConfig, true)); | |
| 258 | + $connectionName = $appConfig->name; | |
| 259 | + $connectionType = $appConfig->type; | |
| 260 | + // $connectionDetails = wp_json_encode($appConfig); | |
| 261 | + | |
| 262 | + if (empty($appConfig->details)) { | |
| 263 | + unset($appConfig->name, $appConfig->id); | |
| 264 | + | |
| 265 | + $connectionDetails = !is_string($appConfig) ? | |
| 266 | + wp_json_encode($appConfig) : $appConfig; | |
| 267 | + } else { | |
| 268 | + $connectionDetails = !is_string($appConfig->details) ? | |
| 269 | + wp_json_encode($appConfig->details) : $appConfig->details; | |
| 270 | + } | |
| 271 | + $connectionCategory = 'connected_integration_apps'; | |
| 272 | + | |
| 273 | + $integrationHandler = new IntegrationHandler(0); | |
| 274 | + | |
| 275 | + return $integrationHandler->saveIntegration($connectionName, $connectionType, $connectionDetails, $connectionCategory); | |
| 276 | + } | |
| 277 | + | |
| 278 | + // get connectd integration app | |
| 279 | + public function getConnectedIntegrationApp() | |
| 280 | + { | |
| 281 | + $connectionCategory = 'connected_integration_apps'; | |
| 282 | + | |
| 283 | + $integrationHandler = new IntegrationHandler(0); | |
| 284 | + | |
| 285 | + $allConnectedIntegApps = $integrationHandler->getAllIntegration($connectionCategory); | |
| 286 | + | |
| 287 | + if (!is_wp_error($allConnectedIntegApps)) { | |
| 288 | + foreach ($allConnectedIntegApps as $integrationkey => $integrationValue) { | |
| 289 | + $integrationData = [ | |
| 290 | + 'id' => $integrationValue->id, | |
| 291 | + 'name' => $integrationValue->integration_name, | |
| 292 | + 'type' => $integrationValue->integration_type, | |
| 293 | + ]; | |
| 294 | + $allConnectedIntegApps[$integrationkey]->integration_details = wp_json_encode(array_merge( | |
| 295 | + $integrationData, | |
| 296 | + is_string($integrationValue->integration_details) ? | |
| 297 | + (array) json_decode($integrationValue->integration_details) : | |
| 298 | + $integrationValue->integration_details | |
| 299 | + )); | |
| 300 | + } | |
| 301 | + } | |
| 302 | + return $allConnectedIntegApps; | |
| 303 | + } | |
| 304 | + | |
| 305 | + public function deleteConnectedApp($appId) | |
| 306 | + { | |
| 307 | + $integrationHandler = new IntegrationHandler(0); | |
| 308 | + | |
| 309 | + return $integrationHandler->deleteIntegration($appId); | |
| 182 | 310 | } |
| 183 | 311 | } |