| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper; |
| 6 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 7 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 8 |
use BitCode\BitForm\Core\Util\FieldValueHandler; |
| 9 |
|
| 10 |
final class FrontendFormHandler |
| 11 |
{ |
| 12 |
public function __construct() |
| 13 |
{ |
| 14 |
add_action('wp_enqueue_scripts', array($this, 'loadAssets')); |
| 15 |
add_shortcode('bitform', array($this, 'handleFrontendRenderRequest')); |
| 16 |
} |
| 17 |
|
| 18 |
private function validPassowordResetToken($token, $userID, $formId) |
| 19 |
{ |
| 20 |
$existResetInteg = (new IntegrationHandler($formId))->getAllIntegration('wp_user_auth', 'wp_auth', 1); |
| 21 |
if (!is_wp_error($existResetInteg) && count($existResetInteg) > 0) { |
| 22 |
if ($existResetInteg[0]->integration_name === 'reset') { |
| 23 |
$user = get_userdata($userID); |
| 24 |
if ($user) { |
| 25 |
$validKey = check_password_reset_key($token, $user->user_login); |
| 26 |
if (is_wp_error($validKey)) { |
| 27 |
echo "<div id='bf-resp' style='display:grid;justify-content:center;color:#860000;'>This password reset token is invalid.</div>"; |
| 28 |
exit(); |
| 29 |
} |
| 30 |
} else { |
| 31 |
echo "<div id='bf-resp' style='display:grid;justify-content:center;color:#860000;'>Invalid User!!</div>"; |
| 32 |
exit(); |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public function handleFrontendRenderRequest($atts) |
| 39 |
{ |
| 40 |
static $shortCodeCounter = 0; |
| 41 |
$shortCodeCounter += 1; |
| 42 |
$atts = shortcode_atts( |
| 43 |
array('id' => 0), |
| 44 |
$atts |
| 45 |
); |
| 46 |
$formID = intval($atts['id']); |
| 47 |
|
| 48 |
if (!$formID) { |
| 49 |
return __('Form ID cannot be empty', 'bit-form'); |
| 50 |
} |
| 51 |
static $formsInpage = array(); |
| 52 |
$FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter); |
| 53 |
if (!$FrontendFormManager->isExist()) { |
| 54 |
return sprintf(__('#%s no. Form doesn\'t exists', 'bit-form'), $formID); |
| 55 |
} |
| 56 |
if (!empty($_GET['token']) && !empty($_GET['id'])) { |
| 57 |
$this->validPassowordResetToken($_GET['token'], $_GET['id'], $formID); |
| 58 |
} |
| 59 |
|
| 60 |
$isBufferStarted = false; |
| 61 |
$reqField = $_SERVER['QUERY_STRING']; |
| 62 |
$previousValue = []; |
| 63 |
$errorMessages = []; |
| 64 |
if (!empty($reqField)) { |
| 65 |
|
| 66 |
foreach (explode('&', $reqField) as $keyValue) { |
| 67 |
// $pattern = '/([a-zA-Z0-9])([a-zA-Z])\=+/'; |
| 68 |
$pattern = '/([^.]+)=(.*?)([^.]+)/'; |
| 69 |
$matches = preg_match($pattern, $keyValue, $matchFormat); |
| 70 |
if ($matches) { |
| 71 |
list($field, $value) = explode('=', $keyValue, 2); |
| 72 |
|
| 73 |
if ('' == trim($value)) { |
| 74 |
continue; |
| 75 |
} |
| 76 |
|
| 77 |
$previousValue[$field][] = sanitize_text_field(urldecode($value)); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
$FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier()); |
| 82 |
$nonce = $FrontendFormManager->getFormToken(); |
| 83 |
$file = count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false; |
| 84 |
if ($FrontendFormManager->isSubmitted()) { |
| 85 |
$submitResp = $FrontendFormManager->handleSubmission(); |
| 86 |
if (!is_wp_error($submitResp) && isset($submitResp['redirectPage'])) { |
| 87 |
if (!headers_sent()) { |
| 88 |
wp_safe_redirect($submitResp['redirectPage']); |
| 89 |
exit; |
| 90 |
} else { |
| 91 |
$isBufferStarted = true; |
| 92 |
echo "<script type='text/javascript'>window.onload = function() {window.location.replace('" . $submitResp['redirectPage'] . "')}</script>"; |
| 93 |
} |
| 94 |
} elseif (is_wp_error($submitResp)) { |
| 95 |
$errors = $submitResp->get_error_message(); |
| 96 |
if (\is_string($errors)) { |
| 97 |
if (!$isBufferStarted) { |
| 98 |
$isBufferStarted = true; |
| 99 |
ob_start(); |
| 100 |
} |
| 101 |
echo "<div id='bf-resp' style='display:grid;justify-content:center'>" . wp_kses_post($errors) . "</div>"; |
| 102 |
} elseif (isset($errors['$form'])) { |
| 103 |
if (!$isBufferStarted) { |
| 104 |
$isBufferStarted = true; |
| 105 |
ob_start(); |
| 106 |
} |
| 107 |
foreach ($errors['$form'] as $error) { |
| 108 |
echo "<div id='bf-resp' style='display:grid;justify-content:center'>" . wp_kses_post($error) . "</div>"; |
| 109 |
} |
| 110 |
unset($errors['$form']); |
| 111 |
$errorMessages = $errors; |
| 112 |
} |
| 113 |
if (!empty($errors)) { |
| 114 |
$errorMessages = $errors; |
| 115 |
} |
| 116 |
$previousValue = $_POST; |
| 117 |
$_POST = []; |
| 118 |
} |
| 119 |
} |
| 120 |
if ($FrontendFormManager->checkStatus()) { |
| 121 |
$FrontendFormManager->setViewCount(); |
| 122 |
|
| 123 |
if (isset($_REQUEST) && count($previousValue) > 0) { |
| 124 |
$formContent = $FrontendFormManager->getFormContentWithValue($previousValue); |
| 125 |
$fields = $formContent->fields; |
| 126 |
$layout = $formContent->layout; |
| 127 |
$buttons = !empty($formContent->buttons) ? $formContent->buttons : ''; |
| 128 |
$additional = $formContent->additional; |
| 129 |
} else { |
| 130 |
$formContent = $FrontendFormManager->getFormContent(); |
| 131 |
$fields = $formContent->fields; |
| 132 |
$layout = $formContent->layout; |
| 133 |
$buttons = !empty($formContent->buttons) ? $formContent->buttons : ''; |
| 134 |
$additional = $formContent->additional; |
| 135 |
} |
| 136 |
|
| 137 |
$captchaV3Settings = $FrontendFormManager->getCaptchaV3Settings(); |
| 138 |
if ($FrontendFormManager->getCaptchaSettings() || $captchaV3Settings) { |
| 139 |
$integrationHandler = new IntegrationHandler(0); |
| 140 |
$allFormIntegrations = $integrationHandler->getAllIntegration('app'); |
| 141 |
if (!is_wp_error($allFormIntegrations)) { |
| 142 |
foreach ($allFormIntegrations as $integration) { |
| 143 |
if ( |
| 144 |
$FrontendFormManager->getCaptchaSettings() |
| 145 |
&& !is_null($integration->integration_type) |
| 146 |
&& $integration->integration_type === 'gReCaptcha' |
| 147 |
) { |
| 148 |
$integrationDetails = json_decode($integration->integration_details); |
| 149 |
$integrationDetails->id = $integration->id; |
| 150 |
$reCAPTCHA = $integrationDetails; |
| 151 |
$reCAPTCHAVersion = 'v2'; |
| 152 |
} |
| 153 |
|
| 154 |
if ($captchaV3Settings) { |
| 155 |
if (!is_null($integration->integration_type) && $integration->integration_type === 'gReCaptchaV3') { |
| 156 |
$integrationDetails = json_decode($integration->integration_details); |
| 157 |
$integrationDetails->id = $integration->id; |
| 158 |
$reCAPTCHA = $integrationDetails; |
| 159 |
$reCAPTCHAVersion = 'v3'; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
wp_enqueue_script('bitforms-frontend-script'); |
| 166 |
if ($file) { |
| 167 |
wp_enqueue_script('bitforms-frontend-file'); |
| 168 |
} |
| 169 |
|
| 170 |
// if ($captchaV3Settings && !empty($reCAPTCHA->siteKey)) { |
| 171 |
// wp_enqueue_script('bitform-recaptchav3', "https://www.google.com/recaptcha/api.js?render={$reCAPTCHA->siteKey}"); |
| 172 |
// } |
| 173 |
$fieldsKey = $FrontendFormManager->getFieldsKey(); |
| 174 |
$workFlowreturnedOnUserInput = null; |
| 175 |
if (!empty($formContent->workFlowExist)) { |
| 176 |
$workFlowRunHelper = new WorkFlowRunHelper($formID); |
| 177 |
if (!empty($formContent->workFlowExist->onload)) { |
| 178 |
$workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad( |
| 179 |
'create', |
| 180 |
$fields |
| 181 |
); |
| 182 |
if (!empty($workFlowreturnedOnLoad['fields'])) { |
| 183 |
$fields = $workFlowreturnedOnLoad['fields']; |
| 184 |
} |
| 185 |
} |
| 186 |
if (!empty($formContent->workFlowExist->oninput)) { |
| 187 |
$workFlowreturnedOnUserInput = $workFlowRunHelper->executeOnUserInput( |
| 188 |
'create', |
| 189 |
$fields |
| 190 |
); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if (!wp_style_is('bitform-style' . $formID)) { |
| 195 |
$this->loadAssets(true, $formID, $file); |
| 196 |
} |
| 197 |
|
| 198 |
if ($captchaV3Settings && !empty($captchaV3Settings->hideReCaptcha)) { |
| 199 |
$styles = '.grecaptcha-badge { visibility: hidden; }'; |
| 200 |
add_action('wp_footer', function () use ($styles) { |
| 201 |
echo '<style>' . $styles . '</style>'; |
| 202 |
}); |
| 203 |
} |
| 204 |
$noLabel = ['decision-box', 'html', 'button', 'paypal', 'razorpay', 'recaptcha']; |
| 205 |
$objectToArrayConvert = json_decode(json_encode($fields), true); |
| 206 |
foreach ($objectToArrayConvert as $fldKey => $field) { |
| 207 |
if (!in_array($field['typ'], $noLabel) && isset($field['lbl'])) { |
| 208 |
$lblReplaceToBackslash = str_replace('$_bf_$', '\\', $field['lbl']); |
| 209 |
$objectToArrayConvert[$fldKey]['lbl'] = FieldValueHandler::replaceSmartTagWithValue($lblReplaceToBackslash); |
| 210 |
} |
| 211 |
} |
| 212 |
$FrontendFormManager->honeypotTrap(); |
| 213 |
|
| 214 |
$bitFormFrontArr = array( |
| 215 |
'ajaxURL' => admin_url('admin-ajax.php'), |
| 216 |
'nonce' => $nonce, |
| 217 |
'version' => BITFORMS_VERSION, |
| 218 |
// 'contentID' => $FormIdentifier, |
| 219 |
'layout' => $layout, |
| 220 |
'fields' => json_decode(json_encode($objectToArrayConvert), FALSE), |
| 221 |
'buttons' => $buttons, |
| 222 |
'fieldsKey' => $fieldsKey, |
| 223 |
'file' => $file, |
| 224 |
'formId' => $formID, |
| 225 |
'GCLID' => $FrontendFormManager->isGCLIDEnabled(), |
| 226 |
'assetUrl' => BITFORMS_ASSET_URI, |
| 227 |
'gRecaptchaSiteKey' => !empty($reCAPTCHA->siteKey) ? $reCAPTCHA->siteKey : null, |
| 228 |
'gRecaptchaVersion' => !empty($reCAPTCHAVersion) ? $reCAPTCHAVersion : null, |
| 229 |
'conditional' => !empty($workFlowreturnedOnUserInput['conditional']) ? $workFlowreturnedOnUserInput['conditional'] : false, |
| 230 |
'fieldToCheck' => !empty($workFlowreturnedOnUserInput['fieldToCheck']) ? $workFlowreturnedOnUserInput['fieldToCheck'] : false, |
| 231 |
'fieldToChange' => !empty($workFlowreturnedOnUserInput['fieldToChange']) ? $workFlowreturnedOnUserInput['fieldToChange'] : false |
| 232 |
); |
| 233 |
|
| 234 |
$paymentKeys = $FrontendFormManager->checkPaymentFields(); |
| 235 |
if (count($paymentKeys)) { |
| 236 |
$bitFormFrontArr['paymentKeys'] = $paymentKeys; |
| 237 |
} |
| 238 |
|
| 239 |
if (isset($additional->enabled->validateFocusLost)) { |
| 240 |
$bitFormFrontArr['validateFocusLost'] = true; |
| 241 |
} |
| 242 |
|
| 243 |
$bitFormsFront = apply_filters( |
| 244 |
'bitforms_localized_script', |
| 245 |
$bitFormFrontArr |
| 246 |
); |
| 247 |
|
| 248 |
$fields = \json_encode($fields); |
| 249 |
$layout = \json_encode($layout); |
| 250 |
$buttons = \json_encode($buttons); |
| 251 |
if (!\in_array($formID, $formsInpage)) { |
| 252 |
wp_localize_script('bitforms-frontend-script', $FormIdentifier, $bitFormsFront); |
| 253 |
} else { |
| 254 |
$formsInpage[] = $formID; |
| 255 |
} |
| 256 |
$formSpecificScripts = "if(typeof window._bitforms_front==='function'){_bitforms_front('$FormIdentifier')}else{document.addEventListener('DOMContentLoaded',(event)=>{ window._bitforms_front && _bitforms_front('$FormIdentifier')})}"; |
| 257 |
wp_add_inline_script('bitforms-frontend-script', $formSpecificScripts, 'after'); |
| 258 |
$html = $FrontendFormManager->formView($fields, $file, $errorMessages); |
| 259 |
if (!$isBufferStarted) { |
| 260 |
ob_start(); |
| 261 |
} ?> |
| 262 |
<div id=<?php echo esc_attr($FormIdentifier); ?>><?php echo trim($html); ?> |
| 263 |
</div> |
| 264 |
<?php |
| 265 |
return ob_get_clean(); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
public function loadAssets($recheck = false, $formID = 0, $isFileExists = false) |
| 270 |
{ |
| 271 |
global $post; |
| 272 |
if (!empty($post->post_content)) { |
| 273 |
\preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $post->post_content, $shortCode); |
| 274 |
} else { |
| 275 |
$isSCExists = false; |
| 276 |
if (isset($post->ID) && \is_int($post->ID)) { |
| 277 |
global $wpdb; |
| 278 |
$pMetadata = $wpdb->get_results("SELECT meta_value FROM `" . $wpdb->postmeta . "` WHERE `post_id`={$post->ID} AND meta_value LIKE '%[bitform id=%' LIMIT 1"); |
| 279 |
if ($pMetadata && !empty($pMetadata[0]->meta_value)) { |
| 280 |
$isSCExists = true; |
| 281 |
\preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $pMetadata[0]->meta_value, $shortCode); |
| 282 |
} |
| 283 |
} |
| 284 |
if (!$isSCExists) { |
| 285 |
$shortCode[0] = null; |
| 286 |
$shortCode[1] = null; |
| 287 |
$shortCode[2] = []; |
| 288 |
$shortCode[3] = null; |
| 289 |
} |
| 290 |
} |
| 291 |
if ($formID !== 0) { |
| 292 |
$shortCode[2][] = $formID; |
| 293 |
} |
| 294 |
if (count($shortCode) === 4 && count($shortCode[2]) > 0) { |
| 295 |
wp_enqueue_style( |
| 296 |
'bitforms-style-frontend', |
| 297 |
BITFORMS_ASSET_URI . '/css/components.css', |
| 298 |
array(), |
| 299 |
BITFORMS_VERSION, |
| 300 |
'all' |
| 301 |
); |
| 302 |
foreach ($shortCode[2] as $formID) { |
| 303 |
if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css')) { |
| 304 |
$styleModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css'); |
| 305 |
wp_enqueue_style( |
| 306 |
'bitform-style' . $formID, |
| 307 |
BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-' . $formID . '.css', |
| 308 |
array(), |
| 309 |
$styleModifyTime, |
| 310 |
'all' |
| 311 |
); |
| 312 |
} |
| 313 |
if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css')) { |
| 314 |
$layoutModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css'); |
| 315 |
wp_enqueue_style( |
| 316 |
'bitform-layout-style' . $formID, |
| 317 |
BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-layout-' . $formID . '.css', |
| 318 |
array(), |
| 319 |
$layoutModifyTime, |
| 320 |
'all' |
| 321 |
); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
if (!$recheck) { |
| 326 |
wp_register_script( |
| 327 |
'bitforms-frontend-script', |
| 328 |
BITFORMS_ASSET_FRNT_JS_URI . '/bitformsFrontend.js', |
| 329 |
array(), |
| 330 |
BITFORMS_VERSION, |
| 331 |
true |
| 332 |
); |
| 333 |
wp_register_script( |
| 334 |
'bitforms-frontend-file', |
| 335 |
BITFORMS_ASSET_FRNT_JS_URI . '/bitforms-file.js', |
| 336 |
array(), |
| 337 |
BITFORMS_VERSION, |
| 338 |
true |
| 339 |
); |
| 340 |
} else { |
| 341 |
wp_enqueue_script( |
| 342 |
'bitforms-frontend-script', |
| 343 |
BITFORMS_ASSET_FRNT_JS_URI . '/bitformsFrontend.js', |
| 344 |
array(), |
| 345 |
BITFORMS_VERSION, |
| 346 |
true |
| 347 |
); |
| 348 |
if ($isFileExists) { |
| 349 |
wp_enqueue_script( |
| 350 |
'bitforms-frontend-file', |
| 351 |
BITFORMS_ASSET_JS_URI . '/bitforms-file.js', |
| 352 |
array(), |
| 353 |
BITFORMS_VERSION, |
| 354 |
true |
| 355 |
); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
} |
| 360 |
} |
| 361 |
|