| @@ -8,9 +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; | |
| 12 | 13 | use BitCode\BitForm\Core\Util\SmartTags; |
| 14 | +use BitCode\BitForm\Core\Util\Utilities; | |
| 13 | 15 | /** |
| 14 | 16 | * Provides details of available integration and helps to |
| 15 | 17 | * execute available integrations |
| 16 | 18 | */ |
| @@ -16,17 +18,19 @@ | ||
| 16 | 18 | */ |
| 17 | 19 | |
| 18 | 20 | use FilesystemIterator; |
| 19 | 21 | |
| 20 | -final class Integrations { | |
| 22 | +final class Integrations | |
| 23 | +{ | |
| 21 | 24 | public $integrations = []; |
| 22 | 25 | |
| 23 | 26 | /** |
| 24 | 27 | * Undocumented function |
| 25 | 28 | * |
| 26 | - * @return all | |
| 29 | + * @return array | |
| 27 | 30 | */ |
| 28 | - public function getAllIntegrations() { | |
| 31 | + public function getAllIntegrations() | |
| 32 | + { | |
| 29 | 33 | return $this->allIntegrations(); |
| 30 | 34 | } |
| 31 | 35 | |
| 32 | 36 | /** |
| @@ -31,20 +35,21 @@ | ||
| 31 | 35 | |
| 32 | 36 | /** |
| 33 | 37 | * Undocumented function |
| 34 | 38 | * |
| 35 | - * @return void | |
| 39 | + * @return array | |
| 36 | 40 | */ |
| 37 | - protected function allIntegrations() { | |
| 41 | + protected function allIntegrations() | |
| 42 | + { | |
| 38 | 43 | $dirs = new FilesystemIterator(__DIR__); |
| 39 | 44 | foreach ($dirs as $dirInfo) { |
| 40 | 45 | if ($dirInfo->isDir()) { |
| 41 | - $integartionBaseName = basename($dirInfo); | |
| 46 | + $integrationsBaseName = basename($dirInfo); | |
| 42 | 47 | if ( |
| 43 | - file_exists(__DIR__ . '/' . $integartionBaseName) | |
| 44 | - && file_exists(__DIR__ . '/' . $integartionBaseName . '/' . $integartionBaseName . 'Handler.php') | |
| 48 | + file_exists(__DIR__ . '/' . $integrationsBaseName) | |
| 49 | + && file_exists(__DIR__ . '/' . $integrationsBaseName . '/' . $integrationsBaseName . 'Handler.php') | |
| 45 | 50 | ) { |
| 46 | - $integrations[] = $integartionBaseName; | |
| 51 | + $integrations[] = $integrationsBaseName; | |
| 47 | 52 | } |
| 48 | 53 | } |
| 49 | 54 | } |
| 50 | 55 | return $integrations; |
| @@ -52,19 +57,32 @@ | ||
| 52 | 57 | |
| 53 | 58 | /** |
| 54 | 59 | * Checks a Integration Exists or not |
| 55 | 60 | * |
| 56 | - * @param String $name Name of Integration | |
| 57 | - * @return boolean | |
| 61 | + * @param string $name Name of Integration | |
| 62 | + * @return boolean | string | |
| 58 | 63 | */ |
| 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 { | |
| 64 | + protected static function isExists($name) | |
| 65 | + { | |
| 66 | + $path = Utilities::getRealPath(__DIR__, $name); | |
| 67 | + if (!empty($path) && $fileName = Utilities::getRealPath(__DIR__ . "/{$path}", "{$name}Handler.php")) { | |
| 68 | + $className = Utilities::getFileNameExceptExtension($fileName); | |
| 69 | + return __NAMESPACE__ . "\\{$path}\\{$className}"; | |
| 70 | + } | |
| 71 | + | |
| 72 | + if (!defined('BITFORMPRO_PLUGIN_DIR_PATH')) { | |
| 65 | 73 | return false; |
| 66 | 74 | } |
| 75 | + | |
| 76 | + $proPath = BITFORMPRO_PLUGIN_DIR_PATH . '/includes/Integration'; | |
| 77 | + $proNamespace = 'BitCode\\BitFormPro\\Integration'; | |
| 78 | + $path = Utilities::getRealPath($proPath, $name); | |
| 79 | + if (!empty($path) && $fileName = Utilities::getRealPath("{$proPath}/{$path}", "{$name}Handler.php")) { | |
| 80 | + $className = Utilities::getFileNameExceptExtension($fileName); | |
| 81 | + return "{$proNamespace}\\{$path}\\{$className}"; | |
| 82 | + } | |
| 83 | + | |
| 84 | + return false; | |
| 67 | 85 | } |
| 68 | 86 | |
| 69 | 87 | /** |
| 70 | 88 | * This function helps to get single integration information |
| @@ -70,22 +88,24 @@ | ||
| 70 | 88 | * This function helps to get single integration information |
| 71 | 89 | * |
| 72 | 90 | * @return bool setIntegration() |
| 73 | 91 | */ |
| 74 | - public function getIntegration($integartionBaseName) { | |
| 75 | - return $this->setIntegration($integartionBaseName); | |
| 92 | + public function getIntegration($integrationBaseName) | |
| 93 | + { | |
| 94 | + return $this->setIntegration($integrationBaseName); | |
| 76 | 95 | } |
| 77 | 96 | |
| 78 | - public function registerAjax() { | |
| 97 | + public function registerAjax() | |
| 98 | + { | |
| 79 | 99 | $dirs = new FilesystemIterator(__DIR__); |
| 80 | 100 | foreach ($dirs as $dirInfo) { |
| 81 | 101 | if ($dirInfo->isDir()) { |
| 82 | - $integartionBaseName = basename($dirInfo); | |
| 102 | + $integrationBaseName = basename($dirInfo); | |
| 83 | 103 | if ( |
| 84 | - file_exists(__DIR__ . '/' . $integartionBaseName) | |
| 85 | - && file_exists(__DIR__ . '/' . $integartionBaseName . '/' . $integartionBaseName . 'Handler.php') | |
| 104 | + file_exists(__DIR__ . '/' . $integrationBaseName) | |
| 105 | + && file_exists(__DIR__ . '/' . $integrationBaseName . '/' . $integrationBaseName . 'Handler.php') | |
| 86 | 106 | ) { |
| 87 | - $integration = __NAMESPACE__ . "\\{$integartionBaseName}\\{$integartionBaseName}Handler"; | |
| 107 | + $integration = __NAMESPACE__ . "\\{$integrationBaseName}\\{$integrationBaseName}Handler"; | |
| 88 | 108 | if (method_exists($integration, 'registerAjax')) { |
| 89 | 109 | $integration::registerAjax(); |
| 90 | 110 | // (new $integration(0, 0))->registerAjax(); |
| 91 | 111 | } |
| @@ -95,9 +115,10 @@ | ||
| 95 | 115 | } |
| 96 | 116 | } |
| 97 | 117 | } |
| 98 | 118 | |
| 99 | - protected static function entryDeleted($formId, $entryId) { | |
| 119 | + protected static function entryDeleted($formId, $entryId) | |
| 120 | + { | |
| 100 | 121 | $formModel = new FormModel(); |
| 101 | 122 | $formContent = $formModel->get( |
| 102 | 123 | [ |
| 103 | 124 | 'id', |
| @@ -122,9 +143,10 @@ | ||
| 122 | 143 | } |
| 123 | 144 | } |
| 124 | 145 | } |
| 125 | 146 | |
| 126 | - public static function specialTagFields($fieldMap) { | |
| 147 | + public static function specialTagFields($fieldMap) | |
| 148 | + { | |
| 127 | 149 | $specialTagFieldValue = []; |
| 128 | 150 | |
| 129 | 151 | foreach ($fieldMap as $value) { |
| 130 | 152 | $triggerValue = $value->formField; |
| @@ -135,17 +157,19 @@ | ||
| 135 | 157 | |
| 136 | 158 | /** |
| 137 | 159 | * This function helps to execute Integration |
| 138 | 160 | * |
| 139 | - * @param Array $integrations List of integrstion to execute. | |
| 161 | + * @param array $integrations List of integration to execute. | |
| 140 | 162 | * Element will be json string like {"id":1} |
| 141 | - * @param Array $fieldValues Values of submitted fields | |
| 142 | - * @param Integer $formID ID of current form | |
| 163 | + * @param array $fieldValues Values of submitted fields | |
| 164 | + * @param integer $formID ID of current form | |
| 143 | 165 | * |
| 144 | 166 | * @return void Nothing to return |
| 145 | 167 | */ |
| 146 | - public static function executeIntegrations($integrations, $fieldValues, $formID, $logID = null, $entryID = 0) { | |
| 168 | + public static function executeIntegrations($integrations, $fieldValues, $formID, $logID = null, $entryID = 0) | |
| 169 | + { | |
| 147 | 170 | $integrationHandler = new IntegrationHandler($formID); |
| 171 | + $logResponse = new ApiResponse(); | |
| 148 | 172 | if (is_array($integrations)) { |
| 149 | 173 | foreach ($integrations as $integrationIDStr) { |
| 150 | 174 | if (!is_string($integrationIDStr)) { |
| 151 | 175 | return; |
| @@ -152,11 +176,14 @@ | ||
| 152 | 176 | } |
| 153 | 177 | $integrationID = intval(json_decode($integrationIDStr)->id, 10); |
| 154 | 178 | if (!empty($integrationID) && is_int($integrationID)) { |
| 155 | 179 | $integrationResult |
| 156 | - = $integrationHandler->getAIntegration($integrationID); | |
| 180 | + = $integrationHandler->getAIntegration($integrationID); | |
| 157 | 181 | $integrationDetails = is_wp_error($integrationResult) ? null : $integrationResult[0]; |
| 158 | 182 | $integrationName = is_null($integrationDetails) ? null : ucfirst(str_replace(' ', '', $integrationDetails->integration_type)); |
| 183 | + if ('Brevo(SendinBlue)' === $integrationName) { | |
| 184 | + $integrationName = 'SendinBlue'; | |
| 185 | + } | |
| 159 | 186 | if (!is_null($integrationName) && static::isExists($integrationName)) { |
| 160 | 187 | $integration = static::isExists($integrationName); |
| 161 | 188 | $handler = new $integration($integrationID, $formID); |
| 162 | 189 | $integDetails = is_string($integrationDetails->integration_details) ? json_decode($integrationDetails->integration_details) : $integrationDetails->integration_details; |
| @@ -164,8 +191,22 @@ | ||
| 164 | 191 | $sptagData = self::specialTagFields($integDetails->field_map); |
| 165 | 192 | $fieldValues = $fieldValues + $sptagData; |
| 166 | 193 | } |
| 167 | 194 | $handler->execute($integrationHandler, $integrationDetails, $fieldValues, $entryID, $logID); |
| 195 | + } else { | |
| 196 | + do_action('bitform_integration_run_failure', $integrationID, $formID); | |
| 197 | + | |
| 198 | + $errorMsg = apply_filters('bitform_filter_integration_run_failure_message', __('Integration execution failed', 'bit-form'), $integrationID, $formID); | |
| 199 | + | |
| 200 | + error_log('[+] Integration execution failed: ' . print_r($errorMsg, true)); | |
| 201 | + | |
| 202 | + $logResponse->apiResponse( | |
| 203 | + $logID, | |
| 204 | + $integrationID, | |
| 205 | + ['type' => 'record', 'type_name' => $integrationName || 'Integration'], | |
| 206 | + 'errors', | |
| 207 | + $errorMsg | |
| 208 | + ); | |
| 168 | 209 | } |
| 169 | 210 | } |
| 170 | 211 | } |
| 171 | 212 | } |
| @@ -170,11 +211,17 @@ | ||
| 170 | 211 | } |
| 171 | 212 | } |
| 172 | 213 | } |
| 173 | 214 | |
| 174 | - public static function integrationExecutionHelper($integrations, $fieldValue, $formID, $entryID, $logID) { | |
| 215 | + public static function integrationExecutionHelper($integrations, $fieldValue, $formID, $entryID, $logID) | |
| 216 | + { | |
| 175 | 217 | if (empty($integrations) || empty($formID)) { |
| 176 | 218 | return; |
| 219 | + } | |
| 220 | + $trnasientData = get_transient("bitform_trigger_transient_{$entryID}"); | |
| 221 | + $trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; | |
| 222 | + if (!empty($trnasientData['fields'])) { | |
| 223 | + $fieldValue = array_merge($fieldValue, $trnasientData['fields']); | |
| 177 | 224 | } |
| 178 | 225 | foreach ($integrations as $key => $value) { |
| 179 | 226 | self::executeIntegrations($value, $fieldValue, $formID, $logID, $entryID); |
| 180 | 227 | } |