| 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\Form\Validator\FormFieldValidator; |
| 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 |
public function handleFrontendRenderRequest($atts) |
| 19 |
{ |
| 20 |
static $shortCodeCounter = 0; |
| 21 |
$shortCodeCounter += 1; |
| 22 |
$atts = shortcode_atts( |
| 23 |
array('id' => 0), |
| 24 |
$atts |
| 25 |
); |
| 26 |
$formID = intval($atts['id']); |
| 27 |
static $formsInpage = array(); |
| 28 |
$FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter); |
| 29 |
$FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier()); |
| 30 |
$nonce = $FrontendFormManager->getFormToken(); |
| 31 |
if ($FrontendFormManager->isSubmitted() && $FrontendFormManager->isExist()) { |
| 32 |
$verifyToken = $FrontendFormManager->verifySubmissionNonce($_POST); |
| 33 |
if (!$verifyToken || $FrontendFormManager->getCaptchaSettings()) { |
| 34 |
echo __("Your submission token was expired. please, submit again", "bitform"); |
| 35 |
} |
| 36 |
$validateForm = $FrontendFormManager->validateFormSubmission($_POST); |
| 37 |
$form_fields = $FrontendFormManager->getFields(); |
| 38 |
$formFieldValidator = new FormFieldValidator($form_fields, $_POST, $_FILES); |
| 39 |
$validateField = $formFieldValidator->validate('create', $formID); |
| 40 |
if ($validateForm && $validateField) { |
| 41 |
$saveResponse = $FrontendFormManager->saveFormEntry($_POST); |
| 42 |
$FrontendFormManager->setSubmissionCount(); |
| 43 |
if (!empty($saveResponse[''])) { |
| 44 |
echo esc_html($saveResponse['message']); |
| 45 |
} else { |
| 46 |
esc_html_e('Form Submitted Successfully', 'bitapps'); |
| 47 |
} |
| 48 |
} else { |
| 49 |
// wp_enqueue_style('bitforms-style-frontend'); |
| 50 |
$errorMessages = count($formFieldValidator->getMessage()) > 0 ? |
| 51 |
$formFieldValidator->getMessage() : null; |
| 52 |
$previousValue = isset($_POST) ? $_POST : null; |
| 53 |
$file = count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false; |
| 54 |
ob_start(); |
| 55 |
echo $FrontendFormManager->formView(null, $file, $errorMessages, $previousValue); |
| 56 |
return ob_get_clean(); |
| 57 |
} |
| 58 |
} elseif ($FrontendFormManager->isExist() && $FrontendFormManager->checkStatus()) { |
| 59 |
$FrontendFormManager->setViewCount(); |
| 60 |
$reqField = $_SERVER['QUERY_STRING']; |
| 61 |
$previousValue = array(); |
| 62 |
if (!empty($reqField)) { |
| 63 |
foreach (explode('&', $reqField) as $keyValue) { |
| 64 |
list($field, $value) = explode('=', $keyValue); |
| 65 |
|
| 66 |
if ('' == trim($value)) { |
| 67 |
continue; |
| 68 |
} |
| 69 |
|
| 70 |
$previousValue[$field][] = sanitize_text_field(urldecode($value)); |
| 71 |
} |
| 72 |
} |
| 73 |
$file = count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false; |
| 74 |
|
| 75 |
if ($FrontendFormManager->getCaptchaSettings()) { |
| 76 |
$integrationHandler = new IntegrationHandler(0); |
| 77 |
$allFormIntegrations = $integrationHandler->getAllIntegration('app'); |
| 78 |
if (!is_wp_error($allFormIntegrations)) { |
| 79 |
foreach ($allFormIntegrations as $integration) { |
| 80 |
if (!is_null($integration->integration_type) && $integration->integration_type === 'gReCaptcha') { |
| 81 |
$integrationDetails = json_decode($integration->integration_details); |
| 82 |
$integrationDetails->id = $integration->id; |
| 83 |
$reCAPTCHA = $integrationDetails; |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
$reCAPTCHAVersion = 'v2'; |
| 88 |
} |
| 89 |
wp_enqueue_script('bitforms-vendors'); |
| 90 |
wp_enqueue_script('bitforms-runtime'); |
| 91 |
wp_enqueue_script('bitforms-frontend-script'); |
| 92 |
if ($file) { |
| 93 |
wp_enqueue_script('bitforms-frontend-file'); |
| 94 |
} |
| 95 |
if (isset($_REQUEST) && count($previousValue) > 0) { |
| 96 |
$formContent = $FrontendFormManager->getFormContentWithValue($previousValue); |
| 97 |
$fields = $formContent->fields; |
| 98 |
$layout = $formContent->layout; |
| 99 |
$buttons = $formContent->buttons; |
| 100 |
} else { |
| 101 |
$formContent = $FrontendFormManager->getFormContent(); |
| 102 |
$fields = $formContent->fields; |
| 103 |
$layout = $formContent->layout; |
| 104 |
$buttons = $formContent->buttons; |
| 105 |
} |
| 106 |
$fieldsKey = $FrontendFormManager->getFieldsKey(); |
| 107 |
$workFlowreturnedOnUserInput = null; |
| 108 |
if (!empty($formContent->workFlowExist)) { |
| 109 |
$workFlowRunHelper = new WorkFlowRunHelper($formID); |
| 110 |
if (!empty($formContent->workFlowExist->onload)) { |
| 111 |
$workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad( |
| 112 |
'create', |
| 113 |
$fields |
| 114 |
); |
| 115 |
if (!empty($workFlowreturnedOnLoad['fields'])) { |
| 116 |
$fields = $workFlowreturnedOnLoad['fields']; |
| 117 |
} |
| 118 |
} |
| 119 |
if (!empty($formContent->workFlowExist->oninput)) { |
| 120 |
$workFlowreturnedOnUserInput = $workFlowRunHelper->executeOnUserInput( |
| 121 |
'create', |
| 122 |
$fields |
| 123 |
); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
if (!wp_style_is('bitform-style' . $formID)) { |
| 128 |
$this->loadAssets(true, $formID, $file); |
| 129 |
} |
| 130 |
|
| 131 |
$bitFormsFront = apply_filters( |
| 132 |
'bitforms_localized_script', |
| 133 |
array( |
| 134 |
'ajaxURL' => admin_url('admin-ajax.php'), |
| 135 |
'fields' => $fields, |
| 136 |
'layout' => $layout, |
| 137 |
'buttons' => $buttons, |
| 138 |
'fieldsKey' => $fieldsKey, |
| 139 |
'file' => $file, |
| 140 |
'formId' => $formID, |
| 141 |
'GCLID' => $FrontendFormManager->isGCLIDEnabled(), |
| 142 |
'gRecaptchaSiteKey' => !empty($reCAPTCHA->siteKey) ? $reCAPTCHA->siteKey : null, |
| 143 |
'gRecaptchaVersion' => !empty($reCAPTCHAVersion) ? $reCAPTCHAVersion : null, |
| 144 |
'conditional' => !empty($workFlowreturnedOnUserInput['conditional']) ? $workFlowreturnedOnUserInput['conditional'] : false, |
| 145 |
'fieldToCheck' => !empty($workFlowreturnedOnUserInput['fieldToCheck']) ? $workFlowreturnedOnUserInput['fieldToCheck'] : false, |
| 146 |
'fieldToChange' => !empty($workFlowreturnedOnUserInput['fieldToChange']) ? $workFlowreturnedOnUserInput['fieldToChange'] : false |
| 147 |
) |
| 148 |
); |
| 149 |
$fields = \json_encode($fields); |
| 150 |
$layout = \json_encode($layout); |
| 151 |
$buttons = \json_encode($buttons); |
| 152 |
$formSpecificScripts = "var bitFormsFront = bitforms_{$formID};bitFormsFront.contentID = '{$FormIdentifier}';bitFormsFront.appID = 'bitforms_{$formID}';bitFormsFront.nonce = '{$nonce}';_bitforms.default(bitFormsFront);"; |
| 153 |
|
| 154 |
wp_add_inline_script('bitforms-frontend-script', $formSpecificScripts, 'after'); |
| 155 |
if ($shortCodeCounter === 1) { |
| 156 |
wp_localize_script('bitforms-frontend-script', "bitforms_{$formID}", $bitFormsFront); |
| 157 |
} elseif (!in_array($formID, $formsInpage)) { |
| 158 |
wp_localize_script('bitforms-frontend-script', "bitforms_{$formID}", $bitFormsFront); |
| 159 |
} |
| 160 |
$formsInpage[] = $formID; |
| 161 |
|
| 162 |
$html = $FrontendFormManager->formView($fields, $file); |
| 163 |
ob_start(); ?> |
| 164 |
<div id=<?php echo esc_attr($FormIdentifier); ?>><?php echo trim($html); ?> |
| 165 |
</div> |
| 166 |
<?php |
| 167 |
return ob_get_clean(); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
public function loadAssets($recheck = false, $formID = 0, $isFileExists = false) |
| 172 |
{ |
| 173 |
global $post; |
| 174 |
if (!empty($post->post_content)) { |
| 175 |
\preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $post->post_content, $shortCode); |
| 176 |
} else { |
| 177 |
$shortCode[0] = null; |
| 178 |
$shortCode[1] = null; |
| 179 |
$shortCode[2] = []; |
| 180 |
$shortCode[3] = null; |
| 181 |
} |
| 182 |
if ($formID !== 0) { |
| 183 |
$shortCode[2][] = $formID; |
| 184 |
} |
| 185 |
if (count($shortCode) === 4 && count($shortCode[2]) > 0) { |
| 186 |
wp_enqueue_style( |
| 187 |
'bitforms-style-frontend', |
| 188 |
BITFORMS_ASSET_URI . '/css/components.css', |
| 189 |
array(), |
| 190 |
BITFORMS_VERSION, |
| 191 |
'all' |
| 192 |
); |
| 193 |
foreach ($shortCode[2] as $formID) { |
| 194 |
if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css')) { |
| 195 |
$styleModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css'); |
| 196 |
wp_enqueue_style( |
| 197 |
'bitform-style' . $formID, |
| 198 |
BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-' . $formID . '.css', |
| 199 |
array(), |
| 200 |
$styleModifyTime, |
| 201 |
'all' |
| 202 |
); |
| 203 |
} |
| 204 |
if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css')) { |
| 205 |
$layoutModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css'); |
| 206 |
wp_enqueue_style( |
| 207 |
'bitform-layout-style' . $formID, |
| 208 |
BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-layout-' . $formID . '.css', |
| 209 |
array(), |
| 210 |
$layoutModifyTime, |
| 211 |
'all' |
| 212 |
); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
if (!$recheck) { |
| 217 |
wp_register_script( |
| 218 |
'bitforms-vendors', |
| 219 |
BITFORMS_ASSET_URI . '/js/vendors-frontend.js', |
| 220 |
null, |
| 221 |
BITFORMS_VERSION, |
| 222 |
true |
| 223 |
); |
| 224 |
wp_register_script( |
| 225 |
'bitforms-runtime', |
| 226 |
BITFORMS_ASSET_URI . '/js/runtime.js', |
| 227 |
null, |
| 228 |
BITFORMS_VERSION, |
| 229 |
true |
| 230 |
); |
| 231 |
wp_register_script( |
| 232 |
'bitforms-frontend-script', |
| 233 |
BITFORMS_ASSET_URI . '/js/bitformsFrontend.js', |
| 234 |
array('bitforms-vendors', 'bitforms-runtime'), |
| 235 |
BITFORMS_VERSION, |
| 236 |
true |
| 237 |
); |
| 238 |
wp_register_script( |
| 239 |
'bitforms-frontend-file', |
| 240 |
BITFORMS_ASSET_URI . '/js/bitforms-file.js', |
| 241 |
array('bitforms-vendors', 'bitforms-runtime'), |
| 242 |
BITFORMS_VERSION, |
| 243 |
true |
| 244 |
); |
| 245 |
} else { |
| 246 |
wp_enqueue_script( |
| 247 |
'bitforms-vendors', |
| 248 |
BITFORMS_ASSET_URI . '/js/vendors-frontend.js', |
| 249 |
null, |
| 250 |
BITFORMS_VERSION, |
| 251 |
true |
| 252 |
); |
| 253 |
wp_enqueue_script( |
| 254 |
'bitforms-runtime', |
| 255 |
BITFORMS_ASSET_URI . '/js/runtime.js', |
| 256 |
null, |
| 257 |
BITFORMS_VERSION, |
| 258 |
true |
| 259 |
); |
| 260 |
wp_enqueue_script( |
| 261 |
'bitforms-frontend-script', |
| 262 |
BITFORMS_ASSET_URI . '/js/bitformsFrontend.js', |
| 263 |
array('bitforms-vendors', 'bitforms-runtime'), |
| 264 |
BITFORMS_VERSION, |
| 265 |
true |
| 266 |
); |
| 267 |
if ($isFileExists) { |
| 268 |
wp_enqueue_script( |
| 269 |
'bitforms-frontend-file', |
| 270 |
BITFORMS_ASSET_URI . '/js/bitforms-file.js', |
| 271 |
array('bitforms-vendors', 'bitforms-runtime'), |
| 272 |
BITFORMS_VERSION, |
| 273 |
true |
| 274 |
); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|