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