# bit-form/1.1.8/includes/Frontend/Form/FrontendFormHandler.php

Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator &amp; Custom Form Builder, version 1.1.8. 280 lines.

- Page: https://pluginprobe.com/plugins/bit-form/1.1.8/code/includes/Frontend/Form/FrontendFormHandler.php
- Raw: https://pluginprobe.com/plugins/bit-form/1.1.8/raw/includes/Frontend/Form/FrontendFormHandler.php
- Modified: 2020-12-30T15:54:18+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/bit-form/1.1.8/code/includes/Frontend/Form/FrontendFormHandler.php#L10-L20`.

```php
<?php

namespace BitCode\BitForm\Frontend\Form;

use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper;
use BitCode\BitForm\Frontend\Form\FrontendFormManager;
use BitCode\BitForm\Core\Integration\IntegrationHandler;
use BitCode\BitForm\Core\Form\Validator\FormFieldValidator;

final class FrontendFormHandler
{
    public function __construct()
    {
        add_action('wp_enqueue_scripts', array($this, 'loadAssets'));
        add_shortcode('bitform', array($this, 'handleFrontendRenderRequest'));
    }

    public function handleFrontendRenderRequest($atts)
    {
        static $shortCodeCounter = 0;
        $shortCodeCounter += 1;
        $atts = shortcode_atts(
            array('id' => 0),
            $atts
        );
        $formID = intval($atts['id']);
        static $formsInpage = array();
        $FrontendFormManager = new FrontendFormManager($formID, $shortCodeCounter);
        $FormIdentifier = esc_js($FrontendFormManager->getFormIdentifier());
        $nonce = $FrontendFormManager->getFormToken();
        if ($FrontendFormManager->isSubmitted() && $FrontendFormManager->isExist()) {
            $verifyToken = $FrontendFormManager->verifySubmissionNonce($_POST);
            if (!$verifyToken || $FrontendFormManager->getCaptchaSettings()) {
                echo __("Your submission token was expired. please, submit again", "bitform");
            }
            $validateForm = $FrontendFormManager->validateFormSubmission($_POST);
            $form_fields = $FrontendFormManager->getFields();
            $formFieldValidator = new FormFieldValidator($form_fields, $_POST, $_FILES);
            $validateField = $formFieldValidator->validate('create', $formID);
            if ($validateForm && $validateField) {
                $saveResponse = $FrontendFormManager->saveFormEntry($_POST);
                $FrontendFormManager->setSubmissionCount();
                if (!empty($saveResponse[''])) {
                    echo esc_html($saveResponse['message']);
                } else {
                    esc_html_e('Form Submitted Successfully', 'bitapps');
                }
            } else {
                // wp_enqueue_style('bitforms-style-frontend');
                $errorMessages = count($formFieldValidator->getMessage()) > 0 ?
                    $formFieldValidator->getMessage() : null;
                $previousValue =  isset($_POST) ? $_POST : null;
                $file =  count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false;
                ob_start();
                echo $FrontendFormManager->formView(null, $file, $errorMessages, $previousValue);
                return ob_get_clean();
            }
        } elseif ($FrontendFormManager->isExist() && $FrontendFormManager->checkStatus()) {
            $FrontendFormManager->setViewCount();
            $reqField = $_SERVER['QUERY_STRING'];
            $previousValue = array();
            if (!empty($reqField)) {
                foreach (explode('&', $reqField) as $keyValue) {
                    list($field, $value) = explode('=', $keyValue);

                    if ('' == trim($value)) {
                        continue;
                    }

                    $previousValue[$field][] = sanitize_text_field(urldecode($value));
                }
            }
            $file =  count($FrontendFormManager->getUploadFields()) > 0 ? $FrontendFormManager->getUploadFields() : false;

            if ($FrontendFormManager->getCaptchaSettings()) {
                $integrationHandler = new IntegrationHandler(0);
                $allFormIntegrations  = $integrationHandler->getAllIntegration('app');
                if (!is_wp_error($allFormIntegrations)) {
                    foreach ($allFormIntegrations as $integration) {
                        if (!is_null($integration->integration_type) && $integration->integration_type === 'gReCaptcha') {
                            $integrationDetails = json_decode($integration->integration_details);
                            $integrationDetails->id = $integration->id;
                            $reCAPTCHA = $integrationDetails;
                        }
                    }
                }
                $reCAPTCHAVersion = 'v2';
            }
            wp_enqueue_script('bitforms-vendors');
            wp_enqueue_script('bitforms-runtime');
            wp_enqueue_script('bitforms-frontend-script');
            if ($file) {
                wp_enqueue_script('bitforms-frontend-file');
            }
            if (isset($_REQUEST) && count($previousValue) > 0) {
                $formContent = $FrontendFormManager->getFormContentWithValue($previousValue);
                $fields  =  $formContent->fields;
                $layout  = $formContent->layout;
                $buttons =  $formContent->buttons;
            } else {
                $formContent = $FrontendFormManager->getFormContent();
                $fields  =  $formContent->fields;
                $layout  = $formContent->layout;
                $buttons =  $formContent->buttons;
            }
            $fieldsKey =  $FrontendFormManager->getFieldsKey();
            $workFlowreturnedOnUserInput = null;
            if (!empty($formContent->workFlowExist)) {
                $workFlowRunHelper = new WorkFlowRunHelper($formID);
                if (!empty($formContent->workFlowExist->onload)) {
                    $workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad(
                        'create',
                        $fields
                    );
                    if (!empty($workFlowreturnedOnLoad['fields'])) {
                        $fields = $workFlowreturnedOnLoad['fields'];
                    }
                }
                if (!empty($formContent->workFlowExist->oninput)) {
                    $workFlowreturnedOnUserInput = $workFlowRunHelper->executeOnUserInput(
                        'create',
                        $fields
                    );
                }
            }

            if (!wp_style_is('bitform-style' . $formID)) {
                $this->loadAssets(true, $formID, $file);
            }

            $bitFormsFront = apply_filters(
                'bitforms_localized_script',
                array(
                    'ajaxURL'   => admin_url('admin-ajax.php'),
                    'fields' => $fields,
                    'layout' => $layout,
                    'buttons' => $buttons,
                    'fieldsKey' => $fieldsKey,
                    'file'   => $file,
                    'formId' => $formID,
                    'GCLID' => $FrontendFormManager->isGCLIDEnabled(),
                    'gRecaptchaSiteKey'   => !empty($reCAPTCHA->siteKey) ? $reCAPTCHA->siteKey : null,
                    'gRecaptchaVersion'   => !empty($reCAPTCHAVersion) ? $reCAPTCHAVersion : null,
                    'conditional' => !empty($workFlowreturnedOnUserInput['conditional']) ? $workFlowreturnedOnUserInput['conditional'] : false,
                    'fieldToCheck' => !empty($workFlowreturnedOnUserInput['fieldToCheck']) ? $workFlowreturnedOnUserInput['fieldToCheck'] : false,
                    'fieldToChange' => !empty($workFlowreturnedOnUserInput['fieldToChange']) ? $workFlowreturnedOnUserInput['fieldToChange'] : false
                )
            );
            $fields = \json_encode($fields);
            $layout = \json_encode($layout);
            $buttons = \json_encode($buttons);
            $formSpecificScripts = "var bitFormsFront = bitforms_{$formID};bitFormsFront.contentID = '{$FormIdentifier}';bitFormsFront.appID = 'bitforms_{$formID}';bitFormsFront.nonce = '{$nonce}';_bitforms.default(bitFormsFront);";

            wp_add_inline_script('bitforms-frontend-script', $formSpecificScripts, 'after');
            if ($shortCodeCounter === 1) {
                wp_localize_script('bitforms-frontend-script', "bitforms_{$formID}", $bitFormsFront);
            } elseif (!in_array($formID, $formsInpage)) {
                wp_localize_script('bitforms-frontend-script', "bitforms_{$formID}", $bitFormsFront);
            }
            $formsInpage[] = $formID;

            $html = $FrontendFormManager->formView($fields, $file);
            ob_start(); ?>
<div id=<?php echo esc_attr($FormIdentifier); ?>><?php echo trim($html); ?>
</div>
<?php
            return ob_get_clean();
        }
    }

    public function loadAssets($recheck = false, $formID = 0, $isFileExists = false)
    {
        global $post;
        if (!empty($post->post_content)) {
            \preg_match_all("/\[bitform\s+id\s*=\s*('|\")\s*(\d+)\s*('|\")\]/", $post->post_content, $shortCode);
        } else {
            $shortCode[0] = null;
            $shortCode[1] = null;
            $shortCode[2] = [];
            $shortCode[3] = null;
        }
        if ($formID !== 0) {
            $shortCode[2][] = $formID;
        }
        if (count($shortCode) === 4 && count($shortCode[2]) > 0) {
            wp_enqueue_style(
                'bitforms-style-frontend',
                BITFORMS_ASSET_URI . '/css/components.css',
                array(),
                BITFORMS_VERSION,
                'all'
            );
            foreach ($shortCode[2] as $formID) {
                if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css')) {
                    $styleModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-' . $formID . '.css');
                    wp_enqueue_style(
                        'bitform-style' . $formID,
                        BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-' . $formID . '.css',
                        array(),
                        $styleModifyTime,
                        'all'
                    );
                }
                if (is_readable(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css')) {
                    $layoutModifyTime = filemtime(BITFORMS_CONTENT_DIR . '/form-styles/bitform-layout-' . $formID . '.css');
                    wp_enqueue_style(
                        'bitform-layout-style' . $formID,
                        BITFORMS_UPLOAD_BASE_URL . '/form-styles/bitform-layout-' . $formID . '.css',
                        array(),
                        $layoutModifyTime,
                        'all'
                    );
                }
            }

            if (!$recheck) {
                wp_register_script(
                    'bitforms-vendors',
                    BITFORMS_ASSET_URI . '/js/vendors-frontend.js',
                    null,
                    BITFORMS_VERSION,
                    true
                );
                wp_register_script(
                    'bitforms-runtime',
                    BITFORMS_ASSET_URI . '/js/runtime.js',
                    null,
                    BITFORMS_VERSION,
                    true
                );
                wp_register_script(
                    'bitforms-frontend-script',
                    BITFORMS_ASSET_URI . '/js/bitformsFrontend.js',
                    array('bitforms-vendors', 'bitforms-runtime'),
                    BITFORMS_VERSION,
                    true
                );
                wp_register_script(
                    'bitforms-frontend-file',
                    BITFORMS_ASSET_URI . '/js/bitforms-file.js',
                    array('bitforms-vendors', 'bitforms-runtime'),
                    BITFORMS_VERSION,
                    true
                );
            } else {
                wp_enqueue_script(
                    'bitforms-vendors',
                    BITFORMS_ASSET_URI . '/js/vendors-frontend.js',
                    null,
                    BITFORMS_VERSION,
                    true
                );
                wp_enqueue_script(
                    'bitforms-runtime',
                    BITFORMS_ASSET_URI . '/js/runtime.js',
                    null,
                    BITFORMS_VERSION,
                    true
                );
                wp_enqueue_script(
                    'bitforms-frontend-script',
                    BITFORMS_ASSET_URI . '/js/bitformsFrontend.js',
                    array('bitforms-vendors', 'bitforms-runtime'),
                    BITFORMS_VERSION,
                    true
                );
                if ($isFileExists) {
                    wp_enqueue_script(
                        'bitforms-frontend-file',
                        BITFORMS_ASSET_URI . '/js/bitforms-file.js',
                        array('bitforms-vendors', 'bitforms-runtime'),
                        BITFORMS_VERSION,
                        true
                    );
                }
            }
        }
    }
}

```
