Last active
November 7, 2020 03:02
-
-
Save wpmudev-sls/3cb87478d13142783fdd59a7b049ecc0 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Disable the form submission on a range time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Forminator Pro] - Disable the form submission on a range time. | |
* Description: [Forminator Pro] - Disable the form submission on a range time. | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; } | |
add_action( 'after_setup_theme', function(){ | |
if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { | |
class WPMUDEV_FM_DSRT{ | |
public $from = '08:30';//H:i:s. | |
public $to = '17:30';//H:i:s. | |
public $closed_msg = 'Sorry, we are closed. We will open from %s to %s daily.'; | |
public $form_ids = [];//Enter the form id to apply, e.g [123,234]. | |
public function __construct(){ | |
add_action( 'forminator_custom_form_submit_errors', array( $this, 'submit_errors' ), 10, 2 ); | |
} | |
public function submit_errors( $submit_errors, $form_id ){ | |
if( ! ( $this->form_ids && ! in_array( $form_id, $this->form_ids ) ) ){ | |
if( empty( $submit_errors ) ){ | |
$this->from = strtotime( date('Y-m-d '. $this->from ) ); | |
$this->to = strtotime( date('Y-m-d '. $this->to ) ); | |
$now = current_time( 'timestamp' ); | |
if( $now < $this->from || $now > $this->to ){ | |
$submit_errors[]['closed'] = 'Sorry, we are closed!'; | |
add_filter( 'forminator_custom_form_invalid_form_message', array( $this, 'custom_error_message' ) ); | |
} | |
} | |
} | |
return $submit_errors; | |
} | |
public function custom_error_message( $error_msg ){ | |
if( strpos( $this->closed_msg, '%s' ) ){ | |
$error_msg = sprintf( $this->closed_msg, sprintf( '%sh:%s', date('H', $this->from ), date('i', $this->from ) ), sprintf( '%sh:%s', date('H', $this->to ), date('i', $this->to ) ) ); | |
}else{ | |
$error_msg = $this->closed_msg; | |
} | |
remove_filter( 'forminator_custom_form_invalid_form_message', array( $this, 'custom_error_message' ) ); | |
return $error_msg; | |
} | |
} | |
$run = new WPMUDEV_FM_DSRT(); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment