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

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

- Page: https://pluginprobe.com/plugins/bit-form/1.1.8/code/includes/Frontend/Form/FrontendFormManager.php
- Raw: https://pluginprobe.com/plugins/bit-form/1.1.8/raw/includes/Frontend/Form/FrontendFormManager.php
- Modified: 2020-12-28T13:18:40+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/FrontendFormManager.php#L10-L20`.

```php
<?php

/**
 * Get set Form,fields
 */

namespace BitCode\BitForm\Frontend\Form;

/**
 * FrontendFormManager class
 */

use BitCode\BitForm\Core\Util\IpTool;
use BitCode\BitForm\Core\Form\FormManager;
use BitCode\BitForm\Core\Util\DateTimeHelper;
use BitCode\BitForm\Core\Database\FormEntryModel;
use BitCode\BitForm\Frontend\Form\View\FormViewer;
use BitCode\BitForm\Core\WorkFlow\WorkFlowRunHelper;

final class FrontendFormManager extends FormManager
{
    private $_form_identifier;
    private $_form_token;
    // private $_has_upload = false;
    public function __construct($form_id, $shortCodeCounter = null)
    {
        parent::__construct($form_id);
        $this->_form_identifier = 'bitforms_' . $form_id . '_submit_';
        $this->_form_identifier .= !empty(get_post()->ID) ? get_post()->ID : '';
        $this->_form_identifier .= !empty($shortCodeCounter) ? "_$shortCodeCounter" : '';
        $this->_form_token = wp_create_nonce('bitforms_' . $form_id);
    }

    public function getFormIdentifier()
    {
        return $this->_form_identifier;
    }

    public function getFormID()
    {
        return $this->form_id;
    }

    public function getFormToken()
    {
        return $this->_form_token;
    }

    public function isSubmitted()
    {
        return isset($_POST[$this->_form_identifier]) ? true : false;
    }

    public function getSubmittedFields($submitted_data)
    {
        unset($submitted_data[$this->_form_identifier]);
        return array_keys($submitted_data);
    }

    public function formView($fields = null, $hasFile = false, $errorMessages = null, $previousValue = null)
    {
        $formContents = $this->getFormContent();
        if (!empty($fields)) {
            $formContents->fields = is_string($fields) ? json_decode($fields) : $fields;
        } else {
            $workFlowRunHelper = new WorkFlowRunHelper($this->form_id);
            $workFlowreturnedOnLoad = $workFlowRunHelper->executeOnLoad(
                'create',
                $formContents->fields
            );
            $formContents->fields = empty($workFlowreturnedOnLoad['fields']) ? $formContents->fields : $workFlowreturnedOnLoad['fields'];
        }
        $formViewer = new FormViewer($this, $formContents, $errorMessages, $previousValue);
        return $formViewer->getView($hasFile);
    }

    public function validateFormSubmission($submitted_data)
    {
        $submitted_fields = $this->getSubmittedFields($submitted_data);
        $form_fields = $this->getFields();
        $form_fields_names = array_keys($form_fields);
        if ($this->isGCLIDEnabled()) {
            array_push($form_fields_names, 'GCLID');
        }
        foreach ($submitted_fields as $key => $field) {
            if (!in_array($field, $form_fields_names)) {
                unset($submitted_data[$field]);
            }
        }
        return $submitted_data;
    }

    public function verifySubmissionNonce($submitted_data)
    {
        if (!isset($submitted_data['bitforms_token'])) {
            return false;
        }
        return wp_verify_nonce(sanitize_text_field($submitted_data['bitforms_token']), "bitforms_{$this->form_id}");
    }

    public function setViewCount()
    {
        if (!current_user_can('manage_options')) {
            $update_status =  $this->formModel->update(
                array(
                    'views' => intval(static::$form[0]->views)  + 1
                ),
                array(
                    'id' => $this->form_id
                )
            );
        }
    }

    public function checkSubmissionRestriction()
    {
        $formContents = $this->getFormContent();
        $fromRestrictionSetitingsEnabled = empty($formContents->additional->enabled) ? null : $formContents->additional->enabled;
        $fromRestrictionSetitings = empty($formContents->additional->settings) ? null : $formContents->additional->settings;
        if (is_null($formContents->additional->enabled) || is_null($formContents->additional->settings)) {
            return false;
        }
        $restrictionMessage = array();
        $ipTool = new IpTool();
        $ipAddress = $ipTool->getIP();
        foreach ($fromRestrictionSetitingsEnabled as $restrictionKey => $isEnabled) {
            if ($isEnabled) {
                if ($restrictionKey === 'entry_limit'  && isset($fromRestrictionSetitings->{$restrictionKey})) {
                    $formEntry = new FormEntryModel();
                    $countResult = $formEntry->count(
                        array(
                            'form_id' => $this->form_id
                        )
                    );
                    $count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : false;
                    if ($count && $count >= intval($fromRestrictionSetitings->{$restrictionKey})) {
                        $restrictionMessage[] = __('Sorry!! Entry limit exceeded', "bitform");
                    }
                }
                if ($restrictionKey === 'onePerIp') {
                    $formEntry = new FormEntryModel();
                    $countResult = $formEntry->count(
                        array(
                            'form_id' => $this->form_id,
                            'user_ip' => ip2long($ipAddress)
                        )
                    );
                    $count = !empty($countResult[0]) && !empty($countResult[0]->count) ? $countResult[0]->count : false;

                    if ($count && $count > 0) {
                        $restrictionMessage[] = __('Sorry!! You have already submitted', "bitform");
                    }
                }
                if ($restrictionKey === 'restrict_form'  && isset($fromRestrictionSetitings->{$restrictionKey})) {
                    $day = empty($fromRestrictionSetitings->{$restrictionKey}->day) ? null : $fromRestrictionSetitings->{$restrictionKey}->day;
                    $date = empty($fromRestrictionSetitings->{$restrictionKey}->date) ? null : $fromRestrictionSetitings->{$restrictionKey}->date;
                    $time = empty($fromRestrictionSetitings->{$restrictionKey}->time) ? null : $fromRestrictionSetitings->{$restrictionKey}->time;

                    $isdayOk = $isdateOk = $istimeOk = true;
                    $dayNotOkMsg = $dateNotOkMsg = $timeNotOkMsg = '';
                    $dateTimeHelper = new DateTimeHelper();
                    if (
                        !empty($day)
                        && is_array($day)
                        && (in_array("Friday", $day)
                            || in_array("Saturday", $day)
                            || in_array("Sunday", $day)
                            || in_array("Monday", $day)
                            || in_array("Tuesday", $day)
                            || in_array("Wednesday", $day)
                            || in_array("Thursday", $day))
                        && (!in_array($dateTimeHelper->getDay('full-name'), $day))
                    ) {
                        $isdayOk = false;
                        $dayMsgVarsFormat = '';
                        foreach ($day as $dayIndex => $dayValue) {
                            if ($dayIndex > 0) {
                                $dayMsgVarsFormat .= ', ';
                            }
                            $dayMsgVarsFormat .= '%s';
                        }
                        $dayNotOkMsg = vsprintf(__("in $dayMsgVarsFormat", 'bitform'), $day);
                    }
                    if (
                        !empty($day)
                        && is_array($day)
                        && (in_array("Custom", $day))
                    ) {
                        $startDate = empty($date->from) ? '00-00-0000' : $date->from;
                        $endDate = empty($date->to) ? '00-00-0000' : $date->to;
                        if (!empty($date->from) && strpos($startDate, 'T') !== false) {
                            $startDate = $dateTimeHelper->getDate($startDate, false, null, 'm-d-Y');
                        }
                        if (!empty($date->to) && strpos($endDate, 'T') !== false) {
                            $endDate = $dateTimeHelper->getDate($endDate, false, null, 'm-d-Y');
                        }
                        $currentDate = $dateTimeHelper->getDate(null, null, null, 'm-d-Y');
                        if (!($currentDate >= $startDate && $currentDate <= $endDate)) {
                            $isdateOk = false;
                            $dateNotOkMsg = sprintf(__("within %s to %s", 'bitform'), $startDate, $endDate);
                        }
                    }

                    if (!empty($time)) {
                        $startTime = empty($time->from) ? '00:00' : $time->from;
                        $endTime = empty($time->to) ? '23:59.999' : $time->to;
                        $currentTime = $dateTimeHelper->getTime(null, null, null, 'H:i');
                        if (!($currentTime >= $startTime && $currentTime <= $endTime)) {
                            $istimeOk = false;
                            $startTime = $dateTimeHelper->getTime($startTime, 'H:i', null);
                            $endTime = $dateTimeHelper->getTime($endTime, 'H:i', null);
                            $isTimeOk = false;
                            $timeNotOkMsg = sprintf(__("%s to %s", 'bitform'), $startTime, $endTime);
                        }
                    }

                    if (!($isdateOk && $isdayOk && $istimeOk)) {
                        if (!$isdayOk) {
                            $restrictionMessage[] = !empty($timeNotOkMsg) ? sprintf(__("Form is available %s From %s", 'bitform'), $dayNotOkMsg, $timeNotOkMsg) :
                                sprintf(__("Form is available %s", 'bitform'), $dayNotOkMsg, $timeNotOkMsg);
                        } elseif (!$isdateOk) {
                            $restrictionMessage[] = !empty($timeNotOkMsg) ? sprintf(__("Form is available %s From %s", 'bitform'), $dateNotOkMsg, $timeNotOkMsg) :
                                sprintf(__("Form is available %s", 'bitform'), $dateNotOkMsg, $timeNotOkMsg);
                        } elseif (!$istimeOk) {
                            $restrictionMessage[] = sprintf(__("Form is available on %s", 'bitform'), $timeNotOkMsg);
                        }
                    }
                }
                if ($restrictionKey === 'blocked_ip'  && isset($fromRestrictionSetitings->{$restrictionKey})) {
                    $isIpBlocked = false;
                    foreach ($fromRestrictionSetitings->{$restrictionKey} as $ipIndex => $ipDetails) {
                        if (!empty($ipDetails->status) && $ipDetails->status && !empty($ipDetails->ip) && $ipDetails->ip === $ipAddress) {
                            $isIpBlocked = true;
                            break;
                        }
                    }
                    if ($isIpBlocked) {
                        $restrictionMessage[] = sprintf(__("Sorry!! Your IP address is %s, Blocked from submitting the form", 'bitform'), $ipAddress);
                    }
                }
                if ($restrictionKey === 'private_ip'  && isset($fromRestrictionSetitings->{$restrictionKey})) {
                    $isIpWhiteListed = false;
                    foreach ($fromRestrictionSetitings->{$restrictionKey} as $ipIndex => $ipDetails) {
                        if (!empty($ipDetails->status) && $ipDetails->status && !empty($ipDetails->ip) && $ipDetails->ip === $ipAddress) {
                            $isIpWhiteListed = true;
                            break;
                        }
                    }
                    if (!$isIpWhiteListed) {
                        $restrictionMessage[] = sprintf(__("Sorry!! Your IP address is %s, Blocked from submitting the form", 'bitform'), $ipAddress);
                    }
                }
            }
        }
        return $restrictionMessage;
    }
}

```
