| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Integration; |
| 4 |
|
| 5 |
use BitCode\BitForm\Admin\Form\AdminFormManager; |
| 6 |
use BitCode\BitForm\Admin\Form\Helpers; |
| 7 |
use BitCode\BitForm\Core\Database\FormEntryLogModel; |
| 8 |
use BitCode\BitForm\Core\Database\FormEntryModel; |
| 9 |
use BitCode\BitForm\Core\Database\IntegrationModel; |
| 10 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 11 |
use BitCode\BitForm\Core\Util\Log; |
| 12 |
use BitCode\BitForm\Core\Util\MailNotifier; |
| 13 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 14 |
|
| 15 |
final class IntegrationHandler |
| 16 |
{ |
| 17 |
private static $_formID; |
| 18 |
private static $_integrationModel; |
| 19 |
private $_user_details; |
| 20 |
|
| 21 |
/** |
| 22 |
* Constructor of Integration Handler |
| 23 |
* |
| 24 |
* @param Integer $formID If Integration is accessible globally then |
| 25 |
* $formID will be 0 and category app |
| 26 |
* @param Array $user_details Details of user accessing data |
| 27 |
* @return void |
| 28 |
*/ |
| 29 |
public function __construct($formID, $user_details = null) |
| 30 |
{ |
| 31 |
static::$_formID = $formID; |
| 32 |
static::$_integrationModel = new IntegrationModel(); |
| 33 |
$this->_user_details = $user_details; |
| 34 |
} |
| 35 |
|
| 36 |
public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null) |
| 37 |
{ |
| 38 |
$conditions = [ |
| 39 |
'form_id' => static::$_formID, |
| 40 |
'id' => $integrationID, |
| 41 |
]; |
| 42 |
if (!is_null($integrationType)) { |
| 43 |
$conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 44 |
} |
| 45 |
if (!is_null($integrationCategory)) { |
| 46 |
$conditions = array_merge($conditions, ['category' => $integrationCategory]); |
| 47 |
} |
| 48 |
return static::$_integrationModel->get( |
| 49 |
[ |
| 50 |
'id', |
| 51 |
'integration_name', |
| 52 |
'integration_type', |
| 53 |
'integration_details', |
| 54 |
'form_id', |
| 55 |
], |
| 56 |
$conditions |
| 57 |
); |
| 58 |
} |
| 59 |
|
| 60 |
public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null) |
| 61 |
{ |
| 62 |
$conditions = [ |
| 63 |
'form_id' => static::$_formID, |
| 64 |
]; |
| 65 |
if (!is_null($integrationType)) { |
| 66 |
$conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 67 |
} |
| 68 |
|
| 69 |
if (!is_null($integrationCategory)) { |
| 70 |
$conditions = array_merge($conditions, ['category' => $integrationCategory]); |
| 71 |
} |
| 72 |
if (!is_null($status)) { |
| 73 |
$conditions = array_merge($conditions, ['status' => 1]); |
| 74 |
} |
| 75 |
if (!is_null($id)) { |
| 76 |
$conditions = array_merge($conditions, ['id' => $id]); |
| 77 |
} |
| 78 |
return static::$_integrationModel->get( |
| 79 |
[ |
| 80 |
'id', |
| 81 |
'form_id', |
| 82 |
'integration_name', |
| 83 |
'integration_type', |
| 84 |
'integration_details', |
| 85 |
'status', |
| 86 |
], |
| 87 |
$conditions |
| 88 |
); |
| 89 |
} |
| 90 |
|
| 91 |
public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null) |
| 92 |
{ |
| 93 |
$conditions = []; |
| 94 |
if (!is_null($integrationType)) { |
| 95 |
$conditions = array_merge($conditions, ['integration_type' => $integrationType]); |
| 96 |
} |
| 97 |
|
| 98 |
if (!is_null($integrationCategory)) { |
| 99 |
$conditions = array_merge($conditions, ['category' => $integrationCategory]); |
| 100 |
} |
| 101 |
if (!is_null($status)) { |
| 102 |
$conditions = array_merge($conditions, ['status' => 1]); |
| 103 |
} |
| 104 |
if (!is_null($id)) { |
| 105 |
$conditions = array_merge($conditions, ['id' => $id]); |
| 106 |
} |
| 107 |
return static::$_integrationModel->get( |
| 108 |
[ |
| 109 |
'id', |
| 110 |
'form_id', |
| 111 |
'integration_name', |
| 112 |
'integration_type', |
| 113 |
'integration_details', |
| 114 |
'status', |
| 115 |
], |
| 116 |
$conditions |
| 117 |
); |
| 118 |
} |
| 119 |
|
| 120 |
public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) |
| 121 |
{ |
| 122 |
if (null === $status) { |
| 123 |
$status = 1; |
| 124 |
} |
| 125 |
return static::$_integrationModel->insert( |
| 126 |
[ |
| 127 |
'integration_name' => $integrationName, |
| 128 |
'integration_type' => $integrationType, |
| 129 |
'integration_details' => $integrationDetails, |
| 130 |
'category' => $integrationCategory, |
| 131 |
'form_id' => static::$_formID, |
| 132 |
'user_id' => $this->_user_details['id'], |
| 133 |
'user_ip' => $this->_user_details['ip'], |
| 134 |
'status' => $status, |
| 135 |
'created_at' => $this->_user_details['time'], |
| 136 |
'updated_at' => $this->_user_details['time'], |
| 137 |
] |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) |
| 142 |
{ |
| 143 |
if (null === $status) { |
| 144 |
$status = 1; |
| 145 |
} |
| 146 |
return static::$_integrationModel->update( |
| 147 |
[ |
| 148 |
'integration_name' => $integrationName, |
| 149 |
'integration_type' => $integrationType, |
| 150 |
'integration_details' => $integrationDetails, |
| 151 |
'category' => $integrationCategory, |
| 152 |
'form_id' => static::$_formID, |
| 153 |
'user_id' => $this->_user_details['id'], |
| 154 |
'user_ip' => $this->_user_details['ip'], |
| 155 |
'status' => $status, |
| 156 |
'updated_at' => $this->_user_details['time'], |
| 157 |
], |
| 158 |
[ |
| 159 |
'id' => $integrationID, |
| 160 |
] |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
public function duplicateAllInAForm($oldFormId) |
| 165 |
{ |
| 166 |
$integCols = ['integration_name', 'integration_type', 'integration_details', 'category', 'form_id', 'user_id', 'user_ip', 'status', 'created_at', 'updated_at']; |
| 167 |
$integDupData = [ |
| 168 |
'integration_name', |
| 169 |
'integration_type', |
| 170 |
'integration_details', |
| 171 |
'category', |
| 172 |
static::$_formID, |
| 173 |
$this->_user_details['id'], |
| 174 |
$this->_user_details['ip'], |
| 175 |
'status', |
| 176 |
$this->_user_details['time'], |
| 177 |
$this->_user_details['time'], |
| 178 |
]; |
| 179 |
return static::$_integrationModel->duplicate($integCols, $integDupData, ['form_id' => $oldFormId]); |
| 180 |
} |
| 181 |
|
| 182 |
public function deleteIntegration($integrationID) |
| 183 |
{ |
| 184 |
return static::$_integrationModel->delete( |
| 185 |
[ |
| 186 |
'id' => $integrationID, |
| 187 |
'form_id' => static::$_formID, |
| 188 |
] |
| 189 |
); |
| 190 |
} |
| 191 |
|
| 192 |
public static function replaceFieldWithValue($dataToReplaceField, $fieldValues) |
| 193 |
{ |
| 194 |
if (empty($dataToReplaceField)) { |
| 195 |
return false; |
| 196 |
} |
| 197 |
if (is_string($dataToReplaceField)) { |
| 198 |
$dataToReplaceField = static::replaceFieldWithValueHelper($dataToReplaceField, $fieldValues); |
| 199 |
} elseif (is_array($dataToReplaceField)) { |
| 200 |
foreach ($dataToReplaceField as $field => $value) { |
| 201 |
if (is_array($value) && 1 === count($value)) { |
| 202 |
$dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value[0], $fieldValues); |
| 203 |
} elseif (is_array($value)) { |
| 204 |
$dataToReplaceField[$field] = static::replaceFieldWithValue($value, $fieldValues); |
| 205 |
} else { |
| 206 |
$dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value, $fieldValues); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
return $dataToReplaceField; |
| 211 |
} |
| 212 |
|
| 213 |
private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues) |
| 214 |
{ |
| 215 |
if (empty($stringToReplaceField)) { |
| 216 |
return $stringToReplaceField; |
| 217 |
} |
| 218 |
$fieldPattern = '/\${\w[^ ${}]*}/'; |
| 219 |
preg_match_all($fieldPattern, $stringToReplaceField, $matchedField); |
| 220 |
$uniqueFieldsInStr = array_unique($matchedField[0]); |
| 221 |
foreach ($uniqueFieldsInStr as $key => $value) { |
| 222 |
$fieldName = substr($value, 2, strlen($value) - 3); |
| 223 |
if (isset($fieldValues[$fieldName])) { |
| 224 |
$stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) : |
| 225 |
wp_json_encode($fieldValues[$fieldName]); |
| 226 |
} |
| 227 |
} |
| 228 |
return $stringToReplaceField; |
| 229 |
} |
| 230 |
|
| 231 |
public static function updatedHiddenFieldValue($formID) |
| 232 |
{ |
| 233 |
$hiddenFields = []; |
| 234 |
$frontendFormManger = new FrontendFormManager($formID); |
| 235 |
|
| 236 |
if ($frontendFormManger->isHoneypotActive()) { |
| 237 |
$str = '_bitform' . '_' . $formID . '_' . time() . '_'; |
| 238 |
$honpotToken = Helpers::honeypotEncryptedToken($str); |
| 239 |
$hiddenFields[] = [ |
| 240 |
'name' => 'b_h_t', |
| 241 |
'value' => $honpotToken |
| 242 |
]; |
| 243 |
} |
| 244 |
$csrfTokens = Helpers::csrfEecrypted(); |
| 245 |
$hiddenFields[] = [ |
| 246 |
'name' => 't_identity', |
| 247 |
'value' => $csrfTokens['t_identity'], |
| 248 |
]; |
| 249 |
$hiddenFields[] = [ |
| 250 |
'name' => 'csrf', |
| 251 |
'value' => $csrfTokens['csrf'] |
| 252 |
]; |
| 253 |
return $hiddenFields; |
| 254 |
} |
| 255 |
|
| 256 |
public static function maybeSetCronForIntegration($workFlowReturnedData, $opType) |
| 257 |
{ |
| 258 |
$responseData = []; |
| 259 |
$entryId = $workFlowReturnedData['triggerData']['entryID']; |
| 260 |
$responseData['entry_id'] = $entryId; |
| 261 |
if (isset($workFlowReturnedData['message'])) { |
| 262 |
$formID = $workFlowReturnedData['triggerData']['formID']; |
| 263 |
$responseData['message'] = $workFlowReturnedData['message']; |
| 264 |
$responseData['hidden_fields'] = self::updatedHiddenFieldValue($formID); |
| 265 |
$responseData['msg_id'] = isset($workFlowReturnedData['msg_id']) ? $workFlowReturnedData['msg_id'] : 0; |
| 266 |
if (isset($workFlowReturnedData['msg_duration'])) { |
| 267 |
$responseData['msg_duration'] = $workFlowReturnedData['msg_duration']; |
| 268 |
} |
| 269 |
} |
| 270 |
if (isset($workFlowReturnedData['redirectPage'])) { |
| 271 |
$responseData['redirectPage'] = $workFlowReturnedData['redirectPage']; |
| 272 |
} |
| 273 |
if ('update' === $opType && isset($workFlowReturnedData['updatedData'])) { |
| 274 |
$responseData['updatedData'] = $workFlowReturnedData['updatedData']; |
| 275 |
} |
| 276 |
if (!isset($workFlowReturnedData['triggerData'])) { |
| 277 |
return $responseData; |
| 278 |
} |
| 279 |
|
| 280 |
$triggerData = $workFlowReturnedData['triggerData']; |
| 281 |
|
| 282 |
$trnasientData = get_transient("bitform_trigger_transient_{$entryId}"); |
| 283 |
$trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData; |
| 284 |
if(!empty($trnasientData['fields'])){ |
| 285 |
$workFlowReturnedData['fields'] = array_merge($workFlowReturnedData['fields'], $trnasientData['fields']); |
| 286 |
} |
| 287 |
|
| 288 |
if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) { |
| 289 |
if (!headers_sent()) { |
| 290 |
header('Connection: close'); |
| 291 |
$contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html'; |
| 292 |
header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset')); |
| 293 |
status_header(200); |
| 294 |
$response = [ |
| 295 |
'success' => true, |
| 296 |
'data' => $responseData, |
| 297 |
]; |
| 298 |
} |
| 299 |
ob_start(); |
| 300 |
echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message']; |
| 301 |
ob_end_flush(); |
| 302 |
flush(); |
| 303 |
session_write_close(); |
| 304 |
|
| 305 |
if (isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) { |
| 306 |
$entryModel = new FormEntryModel(); |
| 307 |
$updatedStatus = $entryModel->update( |
| 308 |
[ |
| 309 |
'status' => 2, |
| 310 |
], |
| 311 |
[ |
| 312 |
'form_id' => $triggerData['formID'], |
| 313 |
'id' => $triggerData['entryID'], |
| 314 |
] |
| 315 |
); |
| 316 |
|
| 317 |
if (!is_wp_error($updatedStatus)) { |
| 318 |
if ($workFlowReturnedData['dflt_template']) { |
| 319 |
$triggerData['fields'] = $workFlowReturnedData['fields']; |
| 320 |
do_action('bf_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData); |
| 321 |
} elseif (isset($triggerData['dblOptin'])) { |
| 322 |
foreach ($triggerData['dblOptin'] as $value) { |
| 323 |
MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
if (function_exists('fastcgi_finish_request')) { |
| 328 |
fastcgi_finish_request(); |
| 329 |
} |
| 330 |
|
| 331 |
if (defined('REST_REQUEST') || wp_doing_ajax()) { |
| 332 |
die; |
| 333 |
} |
| 334 |
return $workFlowReturnedData; |
| 335 |
} |
| 336 |
|
| 337 |
if (isset($triggerData['mail'])) { |
| 338 |
$formManager = new AdminFormManager($triggerData['formID']); |
| 339 |
$formContent = $formManager->getFormContent(); |
| 340 |
$submitted_fields = $formContent->fields; |
| 341 |
$fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $workFlowReturnedData['fields']); |
| 342 |
foreach ($triggerData['mail'] as $value) { |
| 343 |
MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID']); |
| 344 |
} |
| 345 |
} |
| 346 |
do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']); |
| 347 |
|
| 348 |
if (function_exists('fastcgi_finish_request')) { |
| 349 |
fastcgi_finish_request(); |
| 350 |
} |
| 351 |
|
| 352 |
if (defined('REST_REQUEST') || wp_doing_ajax()) { |
| 353 |
die; |
| 354 |
} |
| 355 |
return $workFlowReturnedData; |
| 356 |
} |
| 357 |
|
| 358 |
$entryID = $triggerData['entryID']; |
| 359 |
if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && !isset($triggerData['mail']) && isset($workFlowReturnedData['integrationRun']) && $workFlowReturnedData['integrationRun']) { |
| 360 |
$eventScheuled = wp_schedule_single_event(time(), 'bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]); |
| 361 |
$scheduleTime = wp_next_scheduled('bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]); |
| 362 |
$responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}"); |
| 363 |
} elseif (!empty($triggerData['integrations']) || !empty($triggerData['mail']) || isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) { |
| 364 |
$triggerData['fields'] = $workFlowReturnedData['fields']; |
| 365 |
if (!$workFlowReturnedData['integrationRun'] && isset($workFlowReturnedData['integrationDetails'])) { |
| 366 |
$triggerData['dbl_opt_donf'] = $workFlowReturnedData['integrationDetails']; |
| 367 |
$triggerData['dbl_opt_dflt_template'] = $workFlowReturnedData['dflt_template']; |
| 368 |
$triggerData['integrationRun'] = $workFlowReturnedData['integrationRun']; |
| 369 |
} |
| 370 |
set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS); |
| 371 |
$entryLog = new FormEntryLogModel(); |
| 372 |
$queueuEntry = $entryLog->log_history_insert( |
| 373 |
[ |
| 374 |
'log_id' => $triggerData['logID'], |
| 375 |
'integration_id' => 0, |
| 376 |
'api_type' => wp_json_encode(['type' => 'trigger', 'type_name' => 'Workflow', 'on' => $opType]), |
| 377 |
'response_type' => 'queued', |
| 378 |
'response_obj' => json_encode(['status' => 'queued']), |
| 379 |
'created_at' => current_time('mysql'), |
| 380 |
] |
| 381 |
); |
| 382 |
$responseData['cronNotOk'] = [ |
| 383 |
$triggerData['entryID'], |
| 384 |
$triggerData['logID'], |
| 385 |
$queueuEntry, |
| 386 |
]; |
| 387 |
} |
| 388 |
return $responseData; |
| 389 |
} |
| 390 |
} |
| 391 |
|