Created
February 16, 2021 16:32
-
-
Save wpmudev-sls/5be15261d7f8835acda5a50dc3e3e46f to your computer and use it in GitHub Desktop.
[Forminator] Limit Submissions by Birthdate
This file contains hidden or 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] Limit Submissions by Birthdate | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: If the user does not have the minimum age necessary, the submission is not finished | |
* Author: Glauber Silva @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* Task: SLS-1644 | |
* License: GPLv2 or later | |
* | |
* @package WPMUDEV_Forminator_Limit_Submissions_by_Birthdate | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
if ( ! class_exists( 'WPMUDEV_Forminator_Limit_Submissions_by_Birthdate' ) ) { | |
class WPMUDEV_Forminator_Limit_Submissions_by_Birthdate { | |
// Form field to validate | |
private $field = 'date-1'; | |
// Form ID to validate | |
private $form_id = 607; | |
// Is necessary to have this minimum age or more to be able to do the submission | |
private $minimum_age = 55; | |
// Default message for the users with less than minimum age necessary | |
private $message = 'You must be 55 or older to join the club!'; | |
private static $instance = null; | |
public static function get_instance() { | |
if ( is_null( self::$instance ) ) { | |
self::$instance = new WPMUDEV_Forminator_Limit_Submissions_by_Birthdate(); | |
} | |
return self::$instance; | |
} | |
private function __construct() { | |
$this->init(); | |
} | |
private function init(){ | |
// Show custom notifications | |
add_action( 'forminator_custom_form_submit_errors', array( $this, 'wpmudev_forminator_limit_form_submissions' ), 10, 3 ); | |
} | |
private function wpmudev_get_days_interval_between_two_dates( $date1, $date2 ) { | |
$date1 = (int) $date1; | |
$date2 = (int) $date2; | |
if ($date1 > $date2) { | |
$date_diff = $date1 - $date2; | |
} else { | |
$date_diff = $date2 - $date1; | |
} | |
$days_diff = floor( $date_diff / ( 60 * 60 * 24 ) ); | |
return $days_diff; | |
} | |
public function wpmudev_forminator_limit_form_submissions( $submit_errors, $form_id, $field_data_array ){ | |
if ( absint( $form_id ) !== $this->form_id ){ | |
return $submit_errors; | |
} | |
$response = array( | |
'success' => false, | |
'errors' => false, | |
); | |
foreach( $field_data_array as $field ){ | |
if ( $field['name'] === $this->field ){ | |
$birthdate = strtotime( sanitize_text_field( $field['value'] ) ); | |
$today = current_time( 'timestamp' ); | |
$days_diff = $this->wpmudev_get_days_interval_between_two_dates( $birthdate, $today ); | |
if ( $days_diff > 0 ) { | |
$years_diff = $days_diff / 365; | |
if( $years_diff < $this->minimum_age ){ | |
$response['message'] = __( $this->message, 'wpmudev' ); | |
wp_send_json_error( $response ); | |
} | |
} | |
} | |
} | |
return $submit_errors; | |
} | |
} | |
add_action( | |
'plugins_loaded', | |
function() { | |
return WPMUDEV_Forminator_Limit_Submissions_by_Birthdate::get_instance(); | |
} | |
); | |
} |
I changed $form_id and $field according to my form, but it looks like not working for me. Is there any specific guide to workaround with this.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi how to workaround with this ?