| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Frontend\Form; |
| 4 |
|
| 5 |
use WP_Error; |
| 6 |
use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper; |
| 7 |
use BitCode\BitForm\Frontend\Form\FrontendFormManager; |
| 8 |
use BitCode\BitForm\Core\Integration\IntegrationHandler; |
| 9 |
use BitCode\BitForm\Core\Form\Validator\FormFieldValidator; |
| 10 |
|
| 11 |
final class FrontendFormHandler |
| 12 |
{ |
| 13 |
public function __construct() |
| 14 |
{ |
| 15 |
add_action('wp_enqueue_scripts', array($this, 'loadAssets')); |
| 16 |
add_shortcode('bitform', array($this, 'handleFrontendRenderRequest')); |
| 17 |
} |
| 18 |
|
| 19 |
public function handleFrontendRenderRequest($atts) |
| 20 |
{ |
| 21 |
static $shortCodeCounter = 0; |
| 22 |
$shortCodeCounter += 1; |
| 23 |
$atts = shortcode_atts( |
| 24 |
array('id' => 0), |
| 25 |
$atts |
| 26 |
); |
| 27 |
$formID = intval($atts['id']); |
| 28 |
if (!$formID) { |
| 29 |
return __('Form ID cannot be empty', 'bitform'); |
| 30 |
} |
| 31 |
static $formsInpage = array(); |
| 32 |
$FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter); |
| 33 |
if (!$FrontendFormManager->isExist()) { |
| 34 |
return sprintf(__('#%s no. Form doesn\'t exists', 'bitform'), $formID); |
| 35 |
} |
| 36 |
$isBufferStarted = false; |
| 37 |
$reqField = $_SERVER['QUERY_STRING']; |
| 38 |
$previousValue = []; |
| 39 |
$errorMessages = []; |
| 40 |
if (!empty($reqField)) { |
| 41 |
foreach (explode('&', $reqField) as $keyValue) { |
| 42 |
list($field, $value) = explode('=', $keyValue); |
| 43 |
|
| 44 |
if ('' == trim($value)) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
$previousValue[$field][] = sanitize_text_field(urldecode($value)); |
| 49 |
} |
| 50 |
} |
| 51 |
$FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier()); |
| 52 |
$nonce = $FrontendFormManager->getFormToken(); |
| 53 |
$file = count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false; |
| 54 |
if ($FrontendFormManager->isSubmitted()) { |
| 55 |
$submitResp = $FrontendFormManager->handleSubmission(); |
| 56 |
if (!is_wp_error($submitResp) && isset($submitResp['redirectPage'])) { |
| 57 |
if (!headers_sent()) { |
| 58 |
wp_safe_redirect($submitResp['redirectPage']); |
| 59 |
exit; |
| 60 |
} else { |
| 61 |
$isBufferStarted = true; |
| 62 |
echo "<script type='text/javascript'>window.onload = function() {window.location.replace('". $saveResponse['redirectPage']."')}</script>"; |
| 63 |
} |
| 64 |
} elseif (is_wp_error($submitResp)) { |
| 65 |
$errors = $submitResp->get_error_message(); |
| 66 |
if (\is_string($errors)) { |
| 67 |
if (!$isBufferStarted) { |
| 68 |
$isBufferStarted = true; |
| 69 |
ob_start(); |
| 70 |
} |
| 71 |
echo "<div id='bf-resp' style='display:grid;justify-content:center'>" . wp_kses_post($errors) . "</div>"; |
| 72 |
; |
| 73 |
} elseif (isset($errors['$form'])) { |
| 74 |
if (!$isBufferStarted) { |
| 75 |
$isBufferStarted = true; |
| 76 |
ob_start(); |
| 77 |
} |
| 78 |
foreach ($errors['$form'] as $error) { |
| 79 |
echo "<div id='bf-resp' style='display:grid;justify-content:center'>" . wp_kses_post($error) . "</div>"; |
| 80 |
} |
| 81 |
unset($errors['$form']); |
| 82 |
$errorMessages = $errors; |
| 83 |
} |
| 84 |
$previousValue = $_POST; |
| 85 |
$_POST = []; |
| 86 |
} |
| 87 |
} |
| 88 |
if ($FrontendFormManager->checkStatus()) { |
| 89 |
$FrontendFormManager->setViewCount(); |
| 90 |
|
| 91 |
if ($FrontendFormManager->getCaptchaSettings()) { |
| 92 |
$integrationHandler = new IntegrationHandler(0); |
| 93 |
$allFormIntegrations = $integrationHandler->getAllIntegration('app'); |
| 94 |
if (!is_wp_error($allFormIntegrations)) { |
| 95 |
foreach ($allFormIntegrations as $integration) { |
| 96 |
if (!is_null($integration->integration_type) && $integration->integration_type === 'gReCaptcha') { |
| 97 |
$integrationDetails = json_decode($integration->integration_details); |
| 98 |
$integrationDetails->id = $integration->id; |
| 99 |
$reCAPTCHA = $integrationDetails; |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
$reCAPTCHAVersion = 'v2'; |
| 104 |
} |
| 105 |
wp_enqueue_script('bitforms-frontend-script'); |
| 106 |
if ($file) { |
| 107 |
wp_enqueue_script('bitforms-frontend-file'); |
| 108 |
} |
| 109 |
if (isset($_REQUEST) && count($previousValue) > 0) { |
| 110 |
$formContent = $FrontendFormManager->getFormContentWithValue($previousValue); |
| 111 |
$fields = $formContent->fields; |
| 112 |
$layout = $formContent->layout; |
| 113 |
$buttons = $formContent->buttons; |
| 114 |
} else { |
| 115 |
$formContent = $FrontendFormManager->getFormContent(); |
| 116 |
$fields = $formContent->fields; |
| 117 |
$layout = $formContent->layout; |
| 118 |
$buttons = $formContent->buttons; |
| 119 |
} |
| 120 |
$fieldsKey = $FrontendFormManager->getFieldsKey(); |
| 121 |
$workFlowreturnedOnUserInput = null; |
| 122 |
if (!empty($formContent->workFlowExist)) { |
| 123 |
$workFlowRunHelper = new WorkFlowRunHelper($formID); |
| 124 |
if (!empty($formContent->workFlowExist->onload)) { |
| 125 |
$workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad( |
| 126 |
'create', |
| 127 |
$fields |
| 128 |
); |
| 129 |
if (!empty($workFlowreturnedOnLoad['fields'])) { |
| 130 |
$fields = $workFlowreturnedOnLoad['fields']; |
| 131 |
} |
| 132 |
} |
| 133 |
if (!empty($formContent->workFlowExist->oninput)) { |
| 134 |
$workFlowreturnedOnUserInput = $workFlowRunHelper->executeOnUserInput( |
| 135 |
'create', |
| 136 |
$fields |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
if (!wp_style_is('bitform-style' . $formID)) { |
| 142 |
$this->loadAssets(true, $formID, $file); |
| 143 |
} |
| 144 |
|
| 145 |
$bitFormsFront = apply_filters( |
| 146 |
'bitforms_localized_script', |
| 147 |
array( |
| 148 |
'ajaxURL' => admin_url('admin-ajax.php'), |
| 149 |
'nonce' => $nonce, |
| 150 |
// 'contentID' => $FormIdentifier, |
| 151 |
'fields' => $fields, |
| 152 |
'layout' => $layout, |
| 153 |
'buttons' => $buttons, |
| 154 |
'fieldsKey' => $fieldsKey, |
| 155 |
'file' => $file, |
| 156 |
'formId' => $formID, |
| 157 |
'GCLID' => $FrontendFormManager->isGCLIDEnabled(), |
| 158 |
'gRecaptchaSiteKey' => !empty($reCAPTCHA->siteKey) ? $reCAPTCHA->siteKey : null, |
| 159 |
'gRecaptchaVersion' => !empty($reCAPTCHAVersion) ? $reCAPTCHAVersion : null, |
| 160 |
'conditional' => !empty($workFlowreturnedOnUserInput['conditional']) ? $workFlowreturnedOnUserInput['conditional'] : false, |
| 161 |
'fieldToCheck' => !empty($workFlowreturnedOnUserInput['fieldToCheck']) ? $workFlowreturnedOnUserInput['fieldToCheck'] : false, |
| 162 |
'fieldToChange' => !empty($workFlowreturnedOnUserInput['fieldToChange']) ? $workFlowreturnedOnUserInput['fieldToChange'] : false |
| 163 |
) |
| 164 |
); |
| 165 |
$fields = \json_encode($fields); |
| 166 |
$layout = \json_encode($layout); |
| 167 |
$buttons = \json_encode($buttons); |
| 168 |
if (!\in_array($formID, $formsInpage)) { |
| 169 |
wp_localize_script('bitforms-frontend-script', "bitforms_{$formID}", $bitFormsFront); |
| 170 |
} else { |
| 171 |
$formsInpage[] = $formID; |
| 172 |
} |
| 173 |
$formSpecificScripts = "if(typeof window._bitforms_front==='function'){_bitforms_front('$FormIdentifier')}else{document.addEventListener('DOMContentLoaded',(event)=>{ window._bitforms_front && _bitforms_front('$FormIdentifier')})}"; |
| 174 |
wp_add_inline_script('bitforms-frontend-script', $formSpecificScripts, 'after'); |
| 175 |
$html = $FrontendFormManager->formView($fields, $file, $errorMessages); |
| 176 |
if (!$isBufferStarted) { |
| 177 |
ob_start(); |
| 178 |
} ?> |
| 179 |
<div id=<?php echo esc_attr($FormIdentifier); ?>><?php echo trim($html); ?> |
| 180 |
</div> |
| 181 |
<?php |
| 182 |
return ob_get_clean(); |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
public function loadAssets($recheck = false, $formID = 0, $isFileExists = false) |
| 187 |
{ |
| 188 |
global $post; |
| 189 |
if (!empty($post->post_content)) { |
| 190 |
\preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $post->post_content, $shortCode); |
| 191 |
} else { |
| 192 |
$isSCExists = false; |
| 193 |
if (isset($post->ID) && \is_int($post->ID)) { |
| 194 |
global $wpdb; |
| 195 |
$pMetadata = $wpdb->get_results("SELECT meta_value FROM `".$wpdb->postmeta."` WHERE `post_id`={$post->ID} AND meta_value LIKE '%[bitform id=%' LIMIT 1"); |
| 196 |
if ($pMetadata && !empty($pMetadata[0]->meta_value)) { |
| 197 |
$isSCExists = true; |
| 198 |
\preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $pMetadata[0]->meta_value, $shortCode); |
| 199 |
} |
| 200 |
} |
| 201 |
if (!$isSCExists) { |
| 202 |
$shortCode[0] = null; |
| 203 |
$shortCode[1] = null; |
| 204 |
$shortCode[2] = []; |
| 205 |
$shortCode[3] = null; |
| 206 |
} |
| 207 |
} |
| 208 |
if ($formID !== 0) { |
| 209 |
$shortCode[2][] = $formID; |
| 210 |
} |
| 211 |
if (count($shortCode) === 4 && count($shortCode[2]) > 0) { |
| 212 |
wp_enqueue_style( |
| 213 |
'bitforms-style-frontend', |
| 214 |
BITFORMS_ASSET_URI . '/css/components.css', |
| 215 |
array(), |
| 216 |
BITFORMS_VERSION, |
| 217 |
'all' |
| 218 |
); |
| 219 |
foreach ($shortCode[2] as $formID) { |
| 220 |
if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css')) { |
| 221 |
$styleModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css'); |
| 222 |
wp_enqueue_style( |
| 223 |
'bitform-style' . $formID, |
| 224 |
BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-' . $formID . '.css', |
| 225 |
array(), |
| 226 |
$styleModifyTime, |
| 227 |
'all' |
| 228 |
); |
| 229 |
} |
| 230 |
if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css')) { |
| 231 |
$layoutModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css'); |
| 232 |
wp_enqueue_style( |
| 233 |
'bitform-layout-style' . $formID, |
| 234 |
BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-layout-' . $formID . '.css', |
| 235 |
array(), |
| 236 |
$layoutModifyTime, |
| 237 |
'all' |
| 238 |
); |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
if (!$recheck) { |
| 243 |
wp_register_script( |
| 244 |
'bitforms-frontend-script', |
| 245 |
BITFORMS_ASSET_URI . '/js/bitformsFrontend.js', |
| 246 |
array(), |
| 247 |
BITFORMS_VERSION, |
| 248 |
true |
| 249 |
); |
| 250 |
wp_register_script( |
| 251 |
'bitforms-frontend-file', |
| 252 |
BITFORMS_ASSET_URI . '/js/bitforms-file.js', |
| 253 |
array(), |
| 254 |
BITFORMS_VERSION, |
| 255 |
true |
| 256 |
); |
| 257 |
} else { |
| 258 |
wp_enqueue_script( |
| 259 |
'bitforms-frontend-script', |
| 260 |
BITFORMS_ASSET_URI . '/js/bitformsFrontend.js', |
| 261 |
array(), |
| 262 |
BITFORMS_VERSION, |
| 263 |
true |
| 264 |
); |
| 265 |
if ($isFileExists) { |
| 266 |
wp_enqueue_script( |
| 267 |
'bitforms-frontend-file', |
| 268 |
BITFORMS_ASSET_URI . '/js/bitforms-file.js', |
| 269 |
array(), |
| 270 |
BITFORMS_VERSION, |
| 271 |
true |
| 272 |
); |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|