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