| @@ -6,14 +6,17 @@ | ||
| 6 | 6 | use BitCode\BitForm\Admin\Form\Helpers; |
| 7 | 7 | use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 8 | 8 | use BitCode\BitForm\Core\Database\FormEntryModel; |
| 9 | 9 | use BitCode\BitForm\Core\Database\IntegrationModel; |
| 10 | +use BitCode\BitForm\Core\Form\FormManager; | |
| 10 | 11 | use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 12 | +use BitCode\BitForm\Core\Util\Log; | |
| 11 | 13 | use BitCode\BitForm\Core\Util\MailNotifier; |
| 12 | 14 | use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 13 | 15 | |
| 14 | -final class IntegrationHandler { | |
| 15 | - private static $_formID; | |
| 16 | +final class IntegrationHandler | |
| 17 | +{ | |
| 18 | + private $_formID; | |
| 16 | 19 | private static $_integrationModel; |
| 17 | 20 | private $_user_details; |
| 18 | 21 | |
| 19 | 22 | /** |
| @@ -23,17 +26,19 @@ | ||
| 23 | 26 | * $formID will be 0 and category app |
| 24 | 27 | * @param Array $user_details Details of user accessing data |
| 25 | 28 | * @return void |
| 26 | 29 | */ |
| 27 | - public function __construct($formID, $user_details = null) { | |
| 28 | - static::$_formID = $formID; | |
| 30 | + public function __construct($formID, $user_details = null) | |
| 31 | + { | |
| 32 | + $this->_formID = $formID; | |
| 29 | 33 | static::$_integrationModel = new IntegrationModel(); |
| 30 | 34 | $this->_user_details = $user_details; |
| 31 | 35 | } |
| 32 | 36 | |
| 33 | - public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null) { | |
| 37 | + public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null) | |
| 38 | + { | |
| 34 | 39 | $conditions = [ |
| 35 | - 'form_id' => static::$_formID, | |
| 40 | + 'form_id' => $this->_formID, | |
| 36 | 41 | 'id' => $integrationID, |
| 37 | 42 | ]; |
| 38 | 43 | if (!is_null($integrationType)) { |
| 39 | 44 | $conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| @@ -46,8 +51,9 @@ | ||
| 46 | 51 | 'id', |
| 47 | 52 | 'integration_name', |
| 48 | 53 | 'integration_type', |
| 49 | 54 | 'integration_details', |
| 55 | + 'status', | |
| 50 | 56 | 'form_id', |
| 51 | 57 | ], |
| 52 | 58 | $conditions |
| 53 | 59 | ); |
| @@ -52,11 +58,12 @@ | ||
| 52 | 58 | $conditions |
| 53 | 59 | ); |
| 54 | 60 | } |
| 55 | 61 | |
| 56 | - public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null) { | |
| 62 | + public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null) | |
| 63 | + { | |
| 57 | 64 | $conditions = [ |
| 58 | - 'form_id' => static::$_formID, | |
| 65 | + 'form_id' => $this->_formID, | |
| 59 | 66 | ]; |
| 60 | 67 | if (!is_null($integrationType)) { |
| 61 | 68 | $conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 62 | 69 | } |
| @@ -82,9 +89,10 @@ | ||
| 82 | 89 | $conditions |
| 83 | 90 | ); |
| 84 | 91 | } |
| 85 | 92 | |
| 86 | - public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null) { | |
| 93 | + public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null) | |
| 94 | + { | |
| 87 | 95 | $conditions = []; |
| 88 | 96 | if (!is_null($integrationType)) { |
| 89 | 97 | $conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 90 | 98 | } |
| @@ -110,9 +118,10 @@ | ||
| 110 | 118 | $conditions |
| 111 | 119 | ); |
| 112 | 120 | } |
| 113 | 121 | |
| 114 | - public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) { | |
| 122 | + public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) | |
| 123 | + { | |
| 115 | 124 | if (null === $status) { |
| 116 | 125 | $status = 1; |
| 117 | 126 | } |
| 118 | 127 | return static::$_integrationModel->insert( |
| @@ -120,9 +129,9 @@ | ||
| 120 | 129 | 'integration_name' => $integrationName, |
| 121 | 130 | 'integration_type' => $integrationType, |
| 122 | 131 | 'integration_details' => $integrationDetails, |
| 123 | 132 | 'category' => $integrationCategory, |
| 124 | - 'form_id' => static::$_formID, | |
| 133 | + 'form_id' => $this->_formID, | |
| 125 | 134 | 'user_id' => $this->_user_details['id'], |
| 126 | 135 | 'user_ip' => $this->_user_details['ip'], |
| 127 | 136 | 'status' => $status, |
| 128 | 137 | 'created_at' => $this->_user_details['time'], |
| @@ -130,9 +139,10 @@ | ||
| 130 | 139 | ] |
| 131 | 140 | ); |
| 132 | 141 | } |
| 133 | 142 | |
| 134 | - public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) { | |
| 143 | + public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) | |
| 144 | + { | |
| 135 | 145 | if (null === $status) { |
| 136 | 146 | $status = 1; |
| 137 | 147 | } |
| 138 | 148 | return static::$_integrationModel->update( |
| @@ -140,9 +150,9 @@ | ||
| 140 | 150 | 'integration_name' => $integrationName, |
| 141 | 151 | 'integration_type' => $integrationType, |
| 142 | 152 | 'integration_details' => $integrationDetails, |
| 143 | 153 | 'category' => $integrationCategory, |
| 144 | - 'form_id' => static::$_formID, | |
| 154 | + 'form_id' => $this->_formID, | |
| 145 | 155 | 'user_id' => $this->_user_details['id'], |
| 146 | 156 | 'user_ip' => $this->_user_details['ip'], |
| 147 | 157 | 'status' => $status, |
| 148 | 158 | 'updated_at' => $this->_user_details['time'], |
| @@ -152,9 +162,46 @@ | ||
| 152 | 162 | ] |
| 153 | 163 | ); |
| 154 | 164 | } |
| 155 | 165 | |
| 156 | - public function duplicateAllInAForm($oldFormId) { | |
| 166 | + /** | |
| 167 | + * Name-only rename of a connected account row, scoped to | |
| 168 | + * category=connected_integration_apps and this handler's form id (0). | |
| 169 | + * updateIntegration() is unusable here: it overwrites details/type/category/status. | |
| 170 | + */ | |
| 171 | + public function renameConnectedApp($integrationID, $integrationName) | |
| 172 | + { | |
| 173 | + $data = ['integration_name' => $integrationName]; | |
| 174 | + if (!empty($this->_user_details['time'])) { | |
| 175 | + $data['updated_at'] = $this->_user_details['time']; | |
| 176 | + } | |
| 177 | + return static::$_integrationModel->update( | |
| 178 | + $data, | |
| 179 | + [ | |
| 180 | + 'id' => $integrationID, | |
| 181 | + 'form_id' => $this->_formID, | |
| 182 | + 'category' => 'connected_integration_apps', | |
| 183 | + ] | |
| 184 | + ); | |
| 185 | + } | |
| 186 | + | |
| 187 | + public function updateIntegrationStatus($integrationID, $status) | |
| 188 | + { | |
| 189 | + $data = ['status' => (int) $status]; | |
| 190 | + if (!empty($this->_user_details['time'])) { | |
| 191 | + $data['updated_at'] = $this->_user_details['time']; | |
| 192 | + } | |
| 193 | + return static::$_integrationModel->update( | |
| 194 | + $data, | |
| 195 | + [ | |
| 196 | + 'id' => $integrationID, | |
| 197 | + 'form_id' => $this->_formID, | |
| 198 | + ] | |
| 199 | + ); | |
| 200 | + } | |
| 201 | + | |
| 202 | + public function duplicateAllInAForm($oldFormId) | |
| 203 | + { | |
| 157 | 204 | $integCols = ['integration_name', 'integration_type', 'integration_details', 'category', 'form_id', 'user_id', 'user_ip', 'status', 'created_at', 'updated_at']; |
| 158 | 205 | $integDupData = [ |
| 159 | 206 | 'integration_name', |
| 160 | 207 | 'integration_type', |
| @@ -159,9 +206,9 @@ | ||
| 159 | 206 | 'integration_name', |
| 160 | 207 | 'integration_type', |
| 161 | 208 | 'integration_details', |
| 162 | 209 | 'category', |
| 163 | - static::$_formID, | |
| 210 | + $this->_formID, | |
| 164 | 211 | $this->_user_details['id'], |
| 165 | 212 | $this->_user_details['ip'], |
| 166 | 213 | 'status', |
| 167 | 214 | $this->_user_details['time'], |
| @@ -169,18 +216,20 @@ | ||
| 169 | 216 | ]; |
| 170 | 217 | return static::$_integrationModel->duplicate($integCols, $integDupData, ['form_id' => $oldFormId]); |
| 171 | 218 | } |
| 172 | 219 | |
| 173 | - public function deleteIntegration($integrationID) { | |
| 220 | + public function deleteIntegration($integrationID) | |
| 221 | + { | |
| 174 | 222 | return static::$_integrationModel->delete( |
| 175 | 223 | [ |
| 176 | 224 | 'id' => $integrationID, |
| 177 | - 'form_id' => static::$_formID, | |
| 225 | + 'form_id' => $this->_formID, | |
| 178 | 226 | ] |
| 179 | 227 | ); |
| 180 | 228 | } |
| 181 | 229 | |
| 182 | - public static function replaceFieldWithValue($dataToReplaceField, $fieldValues) { | |
| 230 | + public static function replaceFieldWithValue($dataToReplaceField, $fieldValues) | |
| 231 | + { | |
| 183 | 232 | if (empty($dataToReplaceField)) { |
| 184 | 233 | return false; |
| 185 | 234 | } |
| 186 | 235 | if (is_string($dataToReplaceField)) { |
| @@ -198,9 +247,10 @@ | ||
| 198 | 247 | } |
| 199 | 248 | return $dataToReplaceField; |
| 200 | 249 | } |
| 201 | 250 | |
| 202 | - private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues) { | |
| 251 | + private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues) | |
| 252 | + { | |
| 203 | 253 | if (empty($stringToReplaceField)) { |
| 204 | 254 | return $stringToReplaceField; |
| 205 | 255 | } |
| 206 | 256 | $fieldPattern = '/\${\w[^ ${}]*}/'; |
| @@ -205,21 +255,38 @@ | ||
| 205 | 255 | } |
| 206 | 256 | $fieldPattern = '/\${\w[^ ${}]*}/'; |
| 207 | 257 | preg_match_all($fieldPattern, $stringToReplaceField, $matchedField); |
| 208 | 258 | $uniqueFieldsInStr = array_unique($matchedField[0]); |
| 209 | - foreach ($uniqueFieldsInStr as $key => $value) { | |
| 259 | + | |
| 260 | + foreach ($uniqueFieldsInStr as $value) { | |
| 210 | 261 | $fieldName = substr($value, 2, strlen($value) - 3); |
| 211 | - if (!empty($fieldValues[$fieldName])) { | |
| 212 | - $stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) : | |
| 213 | - wp_json_encode($fieldValues[$fieldName]); | |
| 262 | + | |
| 263 | + $repeaterArr = explode('.', $fieldName); | |
| 264 | + if (isset($fieldValues[$fieldName])) { | |
| 265 | + $stringToReplaceField = is_string($fieldValues[$fieldName]) | |
| 266 | + ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) | |
| 267 | + : wp_json_encode($fieldValues[$fieldName]); | |
| 268 | + } elseif (2 === count($repeaterArr) && isset($fieldValues[$repeaterArr[0]])) { | |
| 269 | + $repeaterValues = []; | |
| 270 | + $repeaterFieldValues = $fieldValues[$repeaterArr[0]]; | |
| 271 | + foreach ($repeaterFieldValues as $value) { | |
| 272 | + if (isset($value[$repeaterArr[1]])) { | |
| 273 | + $repeaterValues[] = $value[$repeaterArr[1]]; | |
| 274 | + } | |
| 275 | + } | |
| 276 | + | |
| 277 | + $stringToReplaceField = $repeaterValues; | |
| 278 | + } else { | |
| 279 | + $stringToReplaceField = FieldValueHandler::replaceSmartTagWithValue($value); | |
| 214 | 280 | } |
| 215 | 281 | } |
| 216 | 282 | return $stringToReplaceField; |
| 217 | 283 | } |
| 218 | 284 | |
| 219 | - public static function updatedHiddenFieldValue($formID) { | |
| 285 | + public static function updatedHiddenFieldValue($formID) | |
| 286 | + { | |
| 220 | 287 | $hiddenFields = []; |
| 221 | - $frontendFormManger = new FrontendFormManager($formID); | |
| 288 | + $frontendFormManger = FrontendFormManager::getInstance($formID); | |
| 222 | 289 | |
| 223 | 290 | if ($frontendFormManger->isHoneypotActive()) { |
| 224 | 291 | $str = '_bitform' . '_' . $formID . '_' . time() . '_'; |
| 225 | 292 | $honpotToken = Helpers::honeypotEncryptedToken($str); |
| @@ -229,10 +296,10 @@ | ||
| 229 | 296 | ]; |
| 230 | 297 | } |
| 231 | 298 | $csrfTokens = Helpers::csrfEecrypted(); |
| 232 | 299 | $hiddenFields[] = [ |
| 233 | - 'name' => 't_identy', | |
| 234 | - 'value' => $csrfTokens['t_identy'], | |
| 300 | + 'name' => 't_identity', | |
| 301 | + 'value' => $csrfTokens['t_identity'], | |
| 235 | 302 | ]; |
| 236 | 303 | $hiddenFields[] = [ |
| 237 | 304 | 'name' => 'csrf', |
| 238 | 305 | 'value' => $csrfTokens['csrf'] |
| @@ -239,9 +306,10 @@ | ||
| 239 | 306 | ]; |
| 240 | 307 | return $hiddenFields; |
| 241 | 308 | } |
| 242 | 309 | |
| 243 | - public static function maybeSetCronForIntegration($workFlowReturnedData, $opType) { | |
| 310 | + public static function maybeSetCronForIntegration($workFlowReturnedData, $opType, $isFormRequest = true) | |
| 311 | + { | |
| 244 | 312 | $responseData = []; |
| 245 | 313 | $entryId = $workFlowReturnedData['triggerData']['entryID']; |
| 246 | 314 | $responseData['entry_id'] = $entryId; |
| 247 | 315 | if (isset($workFlowReturnedData['message'])) { |
| @@ -251,8 +319,11 @@ | ||
| 251 | 319 | $responseData['msg_id'] = isset($workFlowReturnedData['msg_id']) ? $workFlowReturnedData['msg_id'] : 0; |
| 252 | 320 | if (isset($workFlowReturnedData['msg_duration'])) { |
| 253 | 321 | $responseData['msg_duration'] = $workFlowReturnedData['msg_duration']; |
| 254 | 322 | } |
| 323 | + if (isset($workFlowReturnedData['afterSubmit'])) { | |
| 324 | + $responseData['afterSubmit'] = $workFlowReturnedData['afterSubmit']; | |
| 325 | + } | |
| 255 | 326 | } |
| 256 | 327 | if (isset($workFlowReturnedData['redirectPage'])) { |
| 257 | 328 | $responseData['redirectPage'] = $workFlowReturnedData['redirectPage']; |
| 258 | 329 | } |
| @@ -258,8 +329,11 @@ | ||
| 258 | 329 | } |
| 259 | 330 | if ('update' === $opType && isset($workFlowReturnedData['updatedData'])) { |
| 260 | 331 | $responseData['updatedData'] = $workFlowReturnedData['updatedData']; |
| 261 | 332 | } |
| 333 | + if (isset($workFlowReturnedData['new_nonce'])) { | |
| 334 | + $responseData['new_nonce'] = $workFlowReturnedData['new_nonce']; | |
| 335 | + } | |
| 262 | 336 | if (!isset($workFlowReturnedData['triggerData'])) { |
| 263 | 337 | return $responseData; |
| 264 | 338 | } |
| 265 | 339 | |
| @@ -264,8 +338,21 @@ | ||
| 264 | 338 | } |
| 265 | 339 | |
| 266 | 340 | $triggerData = $workFlowReturnedData['triggerData']; |
| 267 | 341 | |
| 342 | + if (!isset($workFlowReturnedData['fields'])) { | |
| 343 | + $workFlowReturnedData['fields'] = isset($workFlowReturnedData['updatedData']) | |
| 344 | + ? $workFlowReturnedData['updatedData'] | |
| 345 | + : []; | |
| 346 | + } | |
| 347 | + | |
| 348 | + $trnasientData = get_transient("bitform_trigger_transient_{$entryId}"); | |
| 349 | + $trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; | |
| 350 | + | |
| 351 | + if (!empty($trnasientData['fields'])) { | |
| 352 | + $workFlowReturnedData['fields'] = array_merge($workFlowReturnedData['fields'], $trnasientData['fields']); | |
| 353 | + } | |
| 354 | + | |
| 268 | 355 | if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) { |
| 269 | 356 | if (!headers_sent()) { |
| 270 | 357 | header('Connection: close'); |
| 271 | 358 | $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html'; |
| @@ -270,15 +357,24 @@ | ||
| 270 | 357 | header('Connection: close'); |
| 271 | 358 | $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html'; |
| 272 | 359 | header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset')); |
| 273 | 360 | status_header(200); |
| 274 | - $response = [ | |
| 275 | - 'success' => true, | |
| 276 | - 'data' => $responseData, | |
| 277 | - ]; | |
| 361 | + if (!$isFormRequest) { | |
| 362 | + $response = [ | |
| 363 | + 'status' => 200, | |
| 364 | + 'code' => 4000, | |
| 365 | + 'message' => 'Data Added Successfully!!', | |
| 366 | + 'success' => true | |
| 367 | + ]; | |
| 368 | + } else { | |
| 369 | + $response = [ | |
| 370 | + 'success' => true, | |
| 371 | + 'data' => $responseData, | |
| 372 | + ]; | |
| 373 | + } | |
| 278 | 374 | } |
| 279 | 375 | ob_start(); |
| 280 | - echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message']; | |
| 376 | + echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : esc_html($workFlowReturnedData['message']); | |
| 281 | 377 | ob_end_flush(); |
| 282 | 378 | flush(); |
| 283 | 379 | session_write_close(); |
| 284 | 380 | |
| @@ -293,15 +389,21 @@ | ||
| 293 | 389 | 'id' => $triggerData['entryID'], |
| 294 | 390 | ] |
| 295 | 391 | ); |
| 296 | 392 | |
| 393 | + $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 297 | 394 | if (!is_wp_error($updatedStatus)) { |
| 298 | 395 | if ($workFlowReturnedData['dflt_template']) { |
| 299 | - $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 300 | - do_action('bf_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData); | |
| 396 | + // $triggerData['fields'] = $workFlowReturnedData['fields']; | |
| 397 | + do_action('bitform_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData); | |
| 301 | 398 | } elseif (isset($triggerData['dblOptin'])) { |
| 302 | 399 | foreach ($triggerData['dblOptin'] as $value) { |
| 303 | - MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']); | |
| 400 | + try { | |
| 401 | + MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']); | |
| 402 | + } catch (\Throwable $mailError) { | |
| 403 | + // response already flushed — a mail failure here must not kill the rest of the run | |
| 404 | + Log::debug_log('[+] Double opt-in mail failed: ' . $mailError->getMessage()); | |
| 405 | + } | |
| 304 | 406 | } |
| 305 | 407 | } |
| 306 | 408 | } |
| 307 | 409 | if (function_exists('fastcgi_finish_request')) { |
| @@ -312,16 +414,20 @@ | ||
| 312 | 414 | die; |
| 313 | 415 | } |
| 314 | 416 | return $workFlowReturnedData; |
| 315 | 417 | } |
| 316 | - | |
| 317 | - if (isset($triggerData['mail'])) { | |
| 418 | + if (isset($triggerData['mail']) && !empty($triggerData['mail'])) { | |
| 318 | 419 | $formManager = new AdminFormManager($triggerData['formID']); |
| 319 | 420 | $formContent = $formManager->getFormContent(); |
| 320 | 421 | $submitted_fields = $formContent->fields; |
| 321 | 422 | $fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $workFlowReturnedData['fields']); |
| 322 | 423 | foreach ($triggerData['mail'] as $value) { |
| 323 | - MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID']); | |
| 424 | + try { | |
| 425 | + MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID'], false, $triggerData['logID']); | |
| 426 | + } catch (\Throwable $mailError) { | |
| 427 | + // response already flushed — a mail failure here must not skip the integrations below | |
| 428 | + Log::debug_log('[+] Mail notification failed: ' . $mailError->getMessage()); | |
| 429 | + } | |
| 324 | 430 | } |
| 325 | 431 | } |
| 326 | 432 | do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']); |
| 327 | 433 | |
| @@ -335,9 +441,9 @@ | ||
| 335 | 441 | return $workFlowReturnedData; |
| 336 | 442 | } |
| 337 | 443 | |
| 338 | 444 | $entryID = $triggerData['entryID']; |
| 339 | - if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && !isset($triggerData['mail']) && isset($workFlowReturnedData['integrationRun']) && $workFlowReturnedData['integrationRun']) { | |
| 445 | + if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && empty($triggerData['mail']) && isset($workFlowReturnedData['integrationRun']) && $workFlowReturnedData['integrationRun']) { | |
| 340 | 446 | $eventScheuled = wp_schedule_single_event(time(), 'bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]); |
| 341 | 447 | $scheduleTime = wp_next_scheduled('bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]); |
| 342 | 448 | $responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}"); |
| 343 | 449 | } elseif (!empty($triggerData['integrations']) || !empty($triggerData['mail']) || isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) { |
| @@ -346,9 +452,15 @@ | ||
| 346 | 452 | $triggerData['dbl_opt_donf'] = $workFlowReturnedData['integrationDetails']; |
| 347 | 453 | $triggerData['dbl_opt_dflt_template'] = $workFlowReturnedData['dflt_template']; |
| 348 | 454 | $triggerData['integrationRun'] = $workFlowReturnedData['integrationRun']; |
| 349 | 455 | } |
| 456 | + | |
| 457 | + // Generate one-time trigger token for security | |
| 458 | + $triggerToken = wp_hash($triggerData['entryID'] . $triggerData['logID'] . time() . wp_rand(), 'nonce'); | |
| 459 | + $triggerData['trigger_token'] = $triggerToken; | |
| 460 | + | |
| 350 | 461 | set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS); |
| 462 | + $queuedAt = current_time('mysql'); | |
| 351 | 463 | $entryLog = new FormEntryLogModel(); |
| 352 | 464 | $queueuEntry = $entryLog->log_history_insert( |
| 353 | 465 | [ |
| 354 | 466 | 'log_id' => $triggerData['logID'], |
| @@ -354,17 +466,257 @@ | ||
| 354 | 466 | 'log_id' => $triggerData['logID'], |
| 355 | 467 | 'integration_id' => 0, |
| 356 | 468 | 'api_type' => wp_json_encode(['type' => 'trigger', 'type_name' => 'Workflow', 'on' => $opType]), |
| 357 | 469 | 'response_type' => 'queued', |
| 358 | - 'response_obj' => json_encode(['status' => 'queued']), | |
| 359 | - 'created_at' => current_time('mysql'), | |
| 470 | + // 'trigger' is a durable copy of the transient payload: object-cache hosts can | |
| 471 | + // evict the transient (and it expires after 1h), which would otherwise lose the run | |
| 472 | + 'response_obj' => wp_json_encode(['status' => 'queued', 'queued_at' => $queuedAt, 'trigger' => $triggerData]), | |
| 473 | + 'created_at' => $queuedAt, | |
| 360 | 474 | ] |
| 361 | 475 | ); |
| 476 | + if (\is_int($queueuEntry)) { | |
| 477 | + // Safety net: the browser-fired bitforms_trigger_workflow request can be lost | |
| 478 | + // (navigation abort, WAF, network drop). This event re-runs the queued workflow; | |
| 479 | + // it no-ops if the browser trigger already claimed the run. | |
| 480 | + wp_schedule_single_event( | |
| 481 | + time() + 2 * MINUTE_IN_SECONDS, | |
| 482 | + 'bitforms_reclaim_workflow', | |
| 483 | + [(int) $entryID, (int) $triggerData['formID'], (int) $triggerData['logID'], $queueuEntry] | |
| 484 | + ); | |
| 485 | + if (!wp_next_scheduled('bitforms_reclaim_sweep')) { | |
| 486 | + wp_schedule_event(time() + HOUR_IN_SECONDS, 'hourly', 'bitforms_reclaim_sweep'); | |
| 487 | + } | |
| 488 | + } | |
| 362 | 489 | $responseData['cronNotOk'] = [ |
| 363 | 490 | $triggerData['entryID'], |
| 364 | 491 | $triggerData['logID'], |
| 365 | 492 | $queueuEntry, |
| 493 | + $triggerToken, // Add one-time token as 4th element | |
| 366 | 494 | ]; |
| 367 | 495 | } |
| 368 | 496 | return $responseData; |
| 369 | 497 | } |
| 498 | + | |
| 499 | + public static function repeaterFieldValueFormatter($fieldValues) | |
| 500 | + { | |
| 501 | + $formattedArray = []; | |
| 502 | + | |
| 503 | + foreach ($fieldValues as $key => $value) { | |
| 504 | + if (is_array($value)) { | |
| 505 | + foreach ($value as $index => $v) { | |
| 506 | + if (is_array($v)) { | |
| 507 | + foreach ($v as $k1 => $v1) { | |
| 508 | + $formattedArray[$k1][] = $v1; | |
| 509 | + } | |
| 510 | + } else { | |
| 511 | + $formattedArray[$key][] = $v; | |
| 512 | + } | |
| 513 | + } | |
| 514 | + } else { | |
| 515 | + $formattedArray[$key] = $value; | |
| 516 | + } | |
| 517 | + } | |
| 518 | + | |
| 519 | + return $formattedArray; | |
| 520 | + } | |
| 521 | + | |
| 522 | + /** | |
| 523 | + * | |
| 524 | + * @param mixed $fieldValues | |
| 525 | + * @param mixed $returnRepeaterValue default is array | |
| 526 | + * @param mixed $formID form ID required for string formatting | |
| 527 | + * @return array and repeater field value as array or string | |
| 528 | + */ | |
| 529 | + public static function formattedRepeaterValue($fieldValues, $returnRepeaterValue = '', $formID = null) | |
| 530 | + { | |
| 531 | + // get the repeater field and store it in an array | |
| 532 | + $repeaterField = []; | |
| 533 | + foreach ($fieldValues as $key => $value) { | |
| 534 | + if (!preg_match('/\./', $key)) { | |
| 535 | + continue; | |
| 536 | + } | |
| 537 | + $repeaterField[] = $key; | |
| 538 | + } | |
| 539 | + | |
| 540 | + // put the value of the child key to the parent repeater field\ | |
| 541 | + foreach ($repeaterField as $key) { | |
| 542 | + list($parentKey, $childKey) = explode('.', $key); | |
| 543 | + $repeatValues = isset($fieldValues[$parentKey]) ? $fieldValues[$parentKey] : $key; | |
| 544 | + | |
| 545 | + if (!is_array($repeatValues)) { | |
| 546 | + $fieldValues[$key] = $fieldValues[$childKey]; | |
| 547 | + continue; | |
| 548 | + } | |
| 549 | + | |
| 550 | + foreach ($repeatValues as $value) { | |
| 551 | + if (isset($value[$childKey])) { | |
| 552 | + if (!isset($fieldValues[$key]) || !is_array($fieldValues[$key])) { | |
| 553 | + $fieldValues[$key] = []; | |
| 554 | + } | |
| 555 | + if (!empty($value[$childKey])) { | |
| 556 | + $fieldValues[$key][] = $value[$childKey]; | |
| 557 | + } | |
| 558 | + } | |
| 559 | + } | |
| 560 | + } | |
| 561 | + | |
| 562 | + // convert the array to string repeater field value | |
| 563 | + if ('string' === $returnRepeaterValue) { | |
| 564 | + $form = new FormManager($formID); | |
| 565 | + foreach ($repeaterField as $key) { | |
| 566 | + if (isset($fieldValues[$key]) && is_array($fieldValues[$key])) { | |
| 567 | + $fieldValues[$key] = implode(', ', self::flattenArray($fieldValues[$key])); | |
| 568 | + } | |
| 569 | + } | |
| 570 | + | |
| 571 | + foreach ($fieldValues as $key=>$value) { | |
| 572 | + if (!$form->isRepeaterField($key)) { | |
| 573 | + continue; | |
| 574 | + } | |
| 575 | + $repeaterValues = $fieldValues[$key]; | |
| 576 | + | |
| 577 | + $newFieldValues = self::constructRepeaterValue($repeaterValues, $formID); | |
| 578 | + | |
| 579 | + $fieldValues[$key] = is_array($newFieldValues) ? json_encode(array_values($newFieldValues), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) : $fieldValues[$key]; | |
| 580 | + } | |
| 581 | + } | |
| 582 | + | |
| 583 | + return $fieldValues; | |
| 584 | + } | |
| 585 | + | |
| 586 | + private static function flattenArray($array) | |
| 587 | + { | |
| 588 | + return array_map(function ($val) { | |
| 589 | + if (is_array($val)) { | |
| 590 | + return count($val) > 1 ? ('[' . implode(', ') . ']') : implode(', ', $val); | |
| 591 | + } else { | |
| 592 | + return $val; | |
| 593 | + } | |
| 594 | + }, $array); | |
| 595 | + } | |
| 596 | + | |
| 597 | + /** | |
| 598 | + * Replace file field value with web url | |
| 599 | + * @param mixed $fieldValues | |
| 600 | + * @param number $formId | |
| 601 | + * @param number $entryId | |
| 602 | + * @return mixed $fieldValues | |
| 603 | + */ | |
| 604 | + public static function handleFileUrl($fieldValues, $formId, $entryId) | |
| 605 | + { | |
| 606 | + if (!$formId || !$entryId) { | |
| 607 | + return $fieldValues; | |
| 608 | + } | |
| 609 | + $form = FormManager::getInstance($formId); | |
| 610 | + $formFields = $form->getFields(); | |
| 611 | + | |
| 612 | + $fldsIncludeFile = ['file-up', 'advanced-file-up', 'repeater', 'signature']; | |
| 613 | + foreach ($fieldValues as $key =>$value) { | |
| 614 | + $fldTyp = $formFields[$key]['type'] ?? null; | |
| 615 | + if (!in_array($fldTyp, $fldsIncludeFile)) { | |
| 616 | + continue; | |
| 617 | + } | |
| 618 | + | |
| 619 | + if ('repeater' === $fldTyp && is_array($value)) { | |
| 620 | + foreach ($value as $rIndex => $rValue) { | |
| 621 | + foreach ($rValue as $subKey=>$subValue) { | |
| 622 | + if (!in_array($formFields[$subKey]['type'], $fldsIncludeFile)) { | |
| 623 | + continue; | |
| 624 | + } | |
| 625 | + // for file fields inside repeater | |
| 626 | + if (is_array($subValue)) { | |
| 627 | + foreach ($subValue as $fileIndex=>$fileValue) { | |
| 628 | + $fieldValues[$key][$rIndex][$subKey][$fileIndex] = self::constructFileUrl($formId, $entryId, $fileValue); | |
| 629 | + } | |
| 630 | + } else { | |
| 631 | + // for signature field inside repeater; | |
| 632 | + $fieldValues[$key][$rIndex][$subKey] = self::constructFileUrl($formId, $entryId, $subValue); | |
| 633 | + } | |
| 634 | + } | |
| 635 | + } | |
| 636 | + } | |
| 637 | + | |
| 638 | + if (in_array($fldTyp, ['file-up', 'advanced-file-up', 'signature'], true)) { | |
| 639 | + if (is_array($value)) { | |
| 640 | + foreach ($value as $fldIndex=>$fldValue) { | |
| 641 | + $fieldValues[$key][$fldIndex] = self::constructFileUrl($formId, $entryId, $fldValue); | |
| 642 | + } | |
| 643 | + } else { | |
| 644 | + $fieldValues[$key] = self::constructFileUrl($formId, $entryId, $value); | |
| 645 | + } | |
| 646 | + } | |
| 647 | + } | |
| 648 | + | |
| 649 | + return $fieldValues; | |
| 650 | + } | |
| 651 | + | |
| 652 | + public static function assignRepeaterFieldValue($fieldValues, $formId) | |
| 653 | + { | |
| 654 | + if (!$formId) { | |
| 655 | + return $fieldValues; | |
| 656 | + } | |
| 657 | + $form = new FormManager($formId); | |
| 658 | + $formFields = $form->getFields(); | |
| 659 | + | |
| 660 | + foreach ($fieldValues as $key=>$value) { | |
| 661 | + if (!$form->isRepeaterField($key)) { | |
| 662 | + continue; | |
| 663 | + } | |
| 664 | + | |
| 665 | + if ('repeater' === $formFields[$key]['type']) { | |
| 666 | + if (is_array($value)) { | |
| 667 | + foreach ($value as $rIndex=>$rValue) { | |
| 668 | + foreach ($rValue as $subKey =>$subValue) { | |
| 669 | + $fieldValues[$subKey] = $subValue; | |
| 670 | + } | |
| 671 | + } | |
| 672 | + } | |
| 673 | + } | |
| 674 | + } | |
| 675 | + | |
| 676 | + return $fieldValues; | |
| 677 | + } | |
| 678 | + | |
| 679 | + public static function constructRepeaterValue($repeaterValues, $formId) | |
| 680 | + { | |
| 681 | + if (!$formId || !is_array($repeaterValues)) { | |
| 682 | + return $repeaterValues; | |
| 683 | + } | |
| 684 | + $form = FormManager::getInstance($formId); | |
| 685 | + $formFields = $form->getFields(); | |
| 686 | + $newRptrValue = []; | |
| 687 | + foreach ($repeaterValues as $key=>$value) { | |
| 688 | + $newRepeatedValue = []; | |
| 689 | + foreach ($value as $rKey =>$rValue) { | |
| 690 | + $fldLbl = isset($formFields[$rKey]) ? $formFields[$rKey]['label'] : $rKey; | |
| 691 | + $newRepeatedValue[$fldLbl] = $rValue; | |
| 692 | + } | |
| 693 | + $newRptrValue[$key] = $newRepeatedValue; | |
| 694 | + } | |
| 695 | + | |
| 696 | + return $newRptrValue; | |
| 697 | + } | |
| 698 | + | |
| 699 | + private static function constructFileUrl($formId, $entryId, $fileName) | |
| 700 | + { | |
| 701 | + $webPath = Helpers::getWebPathWithEncryptedEntryId($formId, $entryId); | |
| 702 | + | |
| 703 | + return rtrim($webPath, '/') . '/' . ltrim($fileName, '/'); | |
| 704 | + } | |
| 705 | + | |
| 706 | + // private static function constructFileUrl($formId, $entryId, $fileName) | |
| 707 | + // { | |
| 708 | + // $restBaseUrl = get_rest_url(); | |
| 709 | + // if (!$restBaseUrl) { | |
| 710 | + // return $fileName; | |
| 711 | + // } | |
| 712 | + // //http://bitlocal.local/wp-json/bitform/v1/bitform-file-download/?formID=1&entryID=16&fileID=bit-form-1005.pdf | |
| 713 | + // $queryParam = build_query([ | |
| 714 | + // 'formID' => $formId, | |
| 715 | + // 'entryID' => $entryId, | |
| 716 | + // 'fileID' => $fileName | |
| 717 | + // ]); | |
| 718 | + | |
| 719 | + // $path = $restBaseUrl . 'bitform' . '/' . 'v1' . '/' . 'bitform-file-download' . '/' . '?' . $queryParam; | |
| 720 | + // return rtrim($path, '/'); | |
| 721 | + // } | |
| 370 | 722 | } |