Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak - Enable minimum time for form submission #1265

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions assets/js/admin/form-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,7 @@
EVFPanelBuilder.init_datepickers();
EVFPanelBuilder.bindBulkOptionActions();
EVFPanelBuilder.bindAkismetInit();
EVFPanelBuilder.bindFormSubmissionMinWaitingTime();

// Fields Panel.
EVFPanelBuilder.bindUIActionsFields();
Expand Down Expand Up @@ -3204,6 +3205,32 @@
$(document).find('.everest-forms-akismet-protection-type').hide();
}
},

/**
* Form Submission minimum waiting time.
*
* @since 3.0.2
*/
bindFormSubmissionMinWaitingTime:function(){
var submissionWaitingTimeEnabler = $(document).find('#everest-forms-panel-field-settings-form_submission_min_waiting_time');
EVFPanelBuilder.formSubmissionMinTimeToggler(submissionWaitingTimeEnabler);
$(document).on('change', '#everest-forms-panel-field-settings-form_submission_min_waiting_time', function(){
EVFPanelBuilder.formSubmissionMinTimeToggler($(this));
})
},
/**
* Form Submission waiting time Toggler.
*
* @param {object} submissionWaitingTimeEnabler
*/
formSubmissionMinTimeToggler:function(submissionWaitingTimeEnabler){
if($(submissionWaitingTimeEnabler).is(':checked')){
$(document).find('.everest-forms-form-submission-minimum-waiting-time').show();
}else{
$(document).find('.everest-forms-form-submission-minimum-waiting-time').hide();
}
},

bindPrivacyPolicyActions: function() {
// Consent message change handler.
$( document.body ).on( 'input', '.everest-forms-field-option .evf-privacy-policy-consent-message', function ( e ) {
Expand Down
50 changes: 48 additions & 2 deletions assets/js/frontend/everest-forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jQuery( function ( $ ) {
this.loadPhoneField();
this.loadCountryFlags();
this.ratingInit();

this.FormSubmissionWaitingTime();

// Inline validation.
this.$everest_form.on( 'input validate change', '.input-text, select, input:checkbox, input:radio', this.validate_field );
Expand All @@ -47,6 +47,8 @@ jQuery( function ( $ ) {
$(this).removeClass('everest-forms-field-active');
});

;



$( document.body ).trigger( 'everest_forms_loaded' );
Expand Down Expand Up @@ -597,6 +599,7 @@ jQuery( function ( $ ) {
var recaptchaID = $submit.get( 0 ).recaptchaID;
var razorpayForms = $form.find( "[data-gateway='razorpay']" );
var stripeForms = $form.find( "[data-gateway*='stripe']" );

// Process form.
if ( processText ) {
$submit.text( processText ).prop( 'disabled', true );
Expand Down Expand Up @@ -948,7 +951,50 @@ jQuery( function ( $ ) {
}
return $( '<div class="iti__flag-box"><div class="iti__flag iti__' + country.id.toLowerCase() + '"></div></div><span class="iti__country-name">' + country.text + '</span>' );
},


FormSubmissionWaitingTime: function(){
$(document).ready(function() {
var form_settings = everest_forms_params.form_settings['settings'];
var wait_form_submission_status = form_settings['form_submission_min_waiting_time'];

if (wait_form_submission_status === '1') {
$('#evf_submission_start_time').val(Date.now());

// Create a MutationObserver to observe changes in the DOM.
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes.length > 0) {
$(mutation.addedNodes).each(function() {
var display = $('#evf_submission_duration');
if (display.length) {
var duration = parseInt(display.data('duration'), 10);
var timer = duration;
var interval = setInterval(function() {
display.text(timer);
if (--timer < 0) {
clearInterval(interval);
$('#evf_submission_duration').parent().remove();
}
}, 1000);

// Once the element is found, disconnect the observer.
observer.disconnect();
}
});
}
});
});

// Start observing the target node for configured mutations.
var targetNode = document.body;
var config = { childList: true, subtree: true };
observer.observe(targetNode, config);
} else {
return '';
}
});
},

getFirstBrowserLanguage: function() {
var nav = window.navigator,
browserLanguagePropertyKeys = [ 'language', 'browserLanguage', 'systemLanguage', 'userLanguage' ],
Expand Down
3 changes: 3 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
== Changelog ==

= 3.0.2 - xx-xx-2024
* Feature - Enable minimum time for form submission.

= 3.0.1 - 03-07-2024
* Feature - Import entries in our form using csv file.
* Enhancement - Global Setting Premium Sidebar.
Expand Down
38 changes: 38 additions & 0 deletions includes/admin/builder/class-evf-builder-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,44 @@ public function output_content() {
echo '</div>';
echo '</div>';
do_action( 'everest_forms_inline_security_settings', $this );

/**
* Minimum time for form submission.
*
* @since 3.0.1
*/
echo '<div class="everest-forms-border-container"><h4 class="everest-forms-border-container-title">' . esc_html__( 'Waiting time for form submission', 'everest-forms' ) . '</h4>';
everest_forms_panel_field(
'toggle',
'settings',
'form_submission_min_waiting_time',
$this->form_data,
esc_html__( 'Enable minimum waiting time for form submission', 'everest-forms' ),
array(
'default' => '0',
'tooltip' => esc_html__( 'Prevents the form submission before the specified time', 'everest-forms' ),
)
);

echo '<div class="everest-forms-border-container everest-forms-form-submission-minimum-waiting-time">';
everest_forms_panel_field(
'number',
'settings',
'form_submission_min_waiting_time_input',
$this->form_data,
esc_html__( 'Form submission minimum waiting time (In seconds)', 'everest-forms' ),
array(
'default' => '5',
'tooltip' => esc_html__( 'Enter the minimum time waiting time for form submission.', 'everest-forms' ),
'min_value' => 0,
)
);

do_action( 'everest_forms_inline_form_submission_min_waiting_time_settings', $this, 'form_submission_min_waiting_time', 'connection_1' );

do_action( 'everest_forms_inline_form_submission_min_waiting_time_section_settings', $this, 'form_submission_min_waiting_time_section', 'connection_1' );
echo '<div>';
echo '</div>';
echo '</div>';

do_action( 'everest_forms_settings_panel_content', $this );
Expand Down
12 changes: 7 additions & 5 deletions includes/admin/evf-admin-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
*/
function everest_forms_admin_fields( $options ) {
if ( ! class_exists( 'EVF_Admin_Settings', false ) ) {
include dirname( __FILE__ ) . '/class-evf-admin-settings.php';
include __DIR__ . '/class-evf-admin-settings.php';
}

EVF_Admin_Settings::output_fields( $options );
Expand All @@ -140,7 +140,7 @@
*/
function everest_forms_update_options( $options, $data = null ) {
if ( ! class_exists( 'EVF_Admin_Settings', false ) ) {
include dirname( __FILE__ ) . '/class-evf-admin-settings.php';
include __DIR__ . '/class-evf-admin-settings.php';
}

EVF_Admin_Settings::save_fields( $options, $data );
Expand All @@ -156,7 +156,7 @@
*/
function everest_forms_settings_get_option( $option_name, $default = '' ) {
if ( ! class_exists( 'EVF_Admin_Settings', false ) ) {
include dirname( __FILE__ ) . '/class-evf-admin-settings.php';
include __DIR__ . '/class-evf-admin-settings.php';
}

return EVF_Admin_Settings::get_option( $option_name, $default );
Expand Down Expand Up @@ -192,6 +192,7 @@
$default = isset( $args['default'] ) ? $args['default'] : '';
$tinymce = isset( $args['tinymce'] ) ? $args['tinymce'] : '';
$placeholder = ! empty( $args['placeholder'] ) ? esc_attr( $args['placeholder'] ) : '';
$min_value = ! empty( $args['min_value'] ) ? esc_attr( $args['min_value'] ) : '';
$data_attr = '';
$output = '';

Expand Down Expand Up @@ -239,13 +240,14 @@
case 'number':
case 'text':
$output = sprintf(
'<input type="%s" id="everest-forms-panel-field-%s-%s" name="%s" value="%s" placeholder="%s" class="widefat %s" %s %s>',
'<input type="%s" id="everest-forms-panel-field-%s-%s" name="%s" value="%s" placeholder="%s" min=%d class="widefat %s" %s %s>',
$option,
sanitize_html_class( $panel_id ),
sanitize_html_class( $field ),
$field_name,
esc_attr( $value ),
$placeholder,
$min_value,
$input_class,
$data_attr,
$custom_attributes
Expand Down Expand Up @@ -349,7 +351,7 @@
$output .= sprintf( ' <i class="dashicons dashicons-editor-help everest-forms-help-tooltip" title="%s"></i>', esc_attr( $item['tooltip'] ) );
}
$output .= '</label></span>';
$x ++;
$x++;
}
break;

Expand Down Expand Up @@ -466,7 +468,7 @@
esc_html( $item['image'] )
);
$output .= '</label>';
$x ++;

Check failure on line 471 in includes/admin/evf-admin-functions.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected no spaces between $x and the increment operator; 1 found
}
$output .= '</div>';
break;
Expand Down
59 changes: 55 additions & 4 deletions includes/class-evf-form-task.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@

$settings = $this->form_data['settings'];
$success_message = isset( $settings['successful_form_submission_message'] ) ? $settings['successful_form_submission_message'] : __( 'Thanks for contacting us! We will be in touch with you shortly.', 'everest-forms' );
// Send 400 Bad Request when there are errors.
// Send 400 Bad Request when there are errors.
if ( empty( $this->errors[ $form_id ] ) ) {
wp_send_json(
array(
Expand Down Expand Up @@ -357,6 +357,18 @@
}
// Initial error check.
$errors = apply_filters( 'everest_forms_process_initial_errors', $this->errors, $this->form_data );

// Minimum time to submit check.
$min_submit_time = $this->form_submission_waiting_time( $this->errors, $this->form_data );
if ( isset( $min_submit_time[ $form_id ]['header'] ) && ! empty( $min_submit_time ) ) {
$this->errors[ $form_id ]['header'] = $min_submit_time[ $form_id ]['header'];
$logger->error(
$min_submit_time[ $form_id ]['header'],
array( 'source' => 'Minimum time to submit' )
);
return $this->errors;
}

if ( isset( $_POST['__amp_form_verify'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
if ( empty( $errors[ $form_id ] ) ) {
wp_send_json( array(), 200 );
Expand Down Expand Up @@ -649,7 +661,7 @@
$response_data = apply_filters( 'everest_forms_after_success_ajax_message', $response_data, $this->form_data, $entry );
return $response_data;
} elseif ( ( 'same' === $this->form_data['settings']['redirect_to'] && empty( $submission_redirection_process ) ) || ( ! empty( $submission_redirection_process ) && 'same_page' == $submission_redirection_process['redirect_to'] ) ) {
evf_add_notice( $message, 'success' );
evf_add_notice( $message, 'success' );
}
$logger->info(
'Everest Forms After success Message.',
Expand Down Expand Up @@ -974,7 +986,7 @@
$emails->send( trim( $address ), $email['subject'], $email['message'], '', $connection_id );
}

endforeach;
endforeach;
if ( isset( $attachment ) ) {
do_action( 'everest_forms_remove_attachments_after_send_email', $attachment, $fields, $form_data, 'entry-email', $connection_id, $entry_id );
}
Expand Down Expand Up @@ -1207,7 +1219,7 @@
}
}
} elseif ( ! is_array( $data ) ) {
$properties['inputs']['primary']['attr']['value'] = esc_attr( $data );
$properties['inputs']['primary']['attr']['value'] = esc_attr( $data );
}
return $properties;
}
Expand Down Expand Up @@ -1432,4 +1444,43 @@
update_option( 'everest_forms_admin_entry_approval_token', $evf_new_token );
}
}

/**
* Prevents form submission before the specified duration.
*
* @param array $errors Form submit errors.
* @param object $form_data An object containing settings for the form.
*/
public function form_submission_waiting_time( $errors, $form_data ) {
$form_submission_waiting_time_enable = isset( $form_data['settings']['form_submission_min_waiting_time'] ) ? $form_data['settings']['form_submission_min_waiting_time'] : '';
$submission_duration = $form_data['settings']['form_submission_min_waiting_time_input'];

if ( '1' === $form_submission_waiting_time_enable && 0 <= absint( $submission_duration ) ) {
$evf_submission_start_time = isset( $_POST['evf_submission_start_time'] ) ? sanitize_text_field( wp_unslash( $_POST['evf_submission_start_time'] ) ) : ''; //phpcs:ignore WordPress.Security.NonceVerification
$atts = $form_data['id'];
$submission_time = time() * 1000;

$waiting_time = absint( $submission_time ) - absint( $evf_submission_start_time );
$form_id = ! empty( $form_data['id'] ) ? $form_data['id'] : 0;

if ( absint( $submission_time ) - absint( $evf_submission_start_time ) <= absint( $submission_duration ) * 1000 ) {
$form_submission_err_msg = apply_filters(
'everest_forms_minimum_waiting_time_form_submission',
sprintf(
"%s <span id='evf_submission_duration' data-duration='%s'>%s</span> %s",
esc_html__( 'Please wait', 'everest-forms' ),
$submission_duration,
$submission_duration,
esc_html__( 'seconds, security checkup is being executed.', 'everest-forms' )
)
);

$errors[ $form_id ]['header'] = $form_submission_err_msg;
}

return $errors;
}
}


}

Check failure on line 1486 in includes/class-evf-form-task.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

The closing brace for the class must go on the next line after the body
1 change: 1 addition & 0 deletions includes/class-evf-frontend-scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,13 @@
'version' => '1.0.8',
),
'jquery-intl-tel-input' => array(
'src' => self::get_asset_url('/assets/js/intlTelInput/jquery.intlTelInput' . $suffix . '.js'),

Check failure on line 186 in includes/class-evf-frontend-scripts.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 186 in includes/class-evf-frontend-scripts.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected 1 spaces before closing parenthesis; 0 found
'deps' => array('jquery'),

Check failure on line 187 in includes/class-evf-frontend-scripts.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected 1 space after the array opener in a single line array. Found: no spaces

Check failure on line 187 in includes/class-evf-frontend-scripts.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected 1 space before the array closer in a single line array. Found: no spaces
'version' => '16.0.7',
),
'jquery-validate' => array(
'src' => self::get_asset_url('assets/js/jquery-validate/jquery.validate' . $suffix . '.js'),

Check failure on line 191 in includes/class-evf-frontend-scripts.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 191 in includes/class-evf-frontend-scripts.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected 1 spaces before closing parenthesis; 0 found
'deps' => array('jquery'),

Check failure on line 192 in includes/class-evf-frontend-scripts.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected 1 space after the array opener in a single line array. Found: no spaces

Check failure on line 192 in includes/class-evf-frontend-scripts.php

View workflow job for this annotation

GitHub Actions / Code sniff (PHP 7.4 , WP Latest)

Expected 1 space before the array closer in a single line array. Found: no spaces
'version' => '1.19.2',
),
'everest-forms' => array(
Expand Down Expand Up @@ -327,6 +327,7 @@
'plugin_url' => plugin_dir_url( EVF_PLUGIN_FILE ),
'i18n_messages_phone' => get_option( 'everest_forms_phone_validation', __( 'Please enter a valid phone number.', 'everest-forms' ) ),
'i18n_field_rating_greater_than_max_value_error' => esc_html__( 'Please enter in a value less than 100.', 'everest-forms' ),
'form_settings' => isset( $_GET['form_id'] ) ? evf()->form->get( absint( $_GET['form_id'] ), array( 'content_only' => true ) ) : 0,
);
break;
case 'everest-forms-text-limit':
Expand Down
23 changes: 21 additions & 2 deletions includes/shortcodes/class-evf-shortcode-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public static function hooks() {

// reCaptcha Language.
add_filter( 'everest_forms_frontend_recaptcha_url', array( __CLASS__, 'evf_recaptcha_language' ), 10, 1 );

// Enable for submission waiting time.
add_filter( 'everest_forms_display_fields_before', array( 'EVF_Shortcode_Form', 'evf_form_submission_waiting_time' ) );
}

/**
Expand Down Expand Up @@ -1157,9 +1160,7 @@ private static function view( $atts ) {
* @return $url
*/
public static function evf_recaptcha_language( $url ) {

return esc_url_raw( add_query_arg( array( 'hl' => get_option( 'everest_forms_recaptcha_recaptcha_language', 'en-GB' ) ), $url ) );

}

/**
Expand Down Expand Up @@ -1245,4 +1246,22 @@ function () use ( $custom_css, $form_id ) {
}
}
}

/**
* Function to enable the minimum form submission waiting time.
*
* @since 3.0.2
*
* @param array $form_data Form Data.
*/
public static function evf_form_submission_waiting_time( $form_data ) {
$form_submission_waiting_time_enable = isset( $form_data['settings']['form_submission_min_waiting_time'] ) ? $form_data['settings']['form_submission_min_waiting_time'] : '';
$submission_duration = isset( $form_data['settings']['form_submission_min_waiting_time_input'] ) ? $form_data['settings']['form_submission_min_waiting_time_input'] : '';

if ( '1' === $form_submission_waiting_time_enable ) {
echo "<input type='hidden' id='evf_submission_start_time' name='evf_submission_start_time'/>";
} else {
return '';
}
}
}
Loading
Loading