Last active
August 18, 2023 21:14
-
-
Save wpmudev-sls/9b31be37eafdbb63f19c7cf229f52535 to your computer and use it in GitHub Desktop.
forminator-single-daily-submissions.php
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] - Prevent duplicate submission. | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Prevent duplicate submission on same day. | |
* Task: SLS-1859 | |
* Author: Panos Lyrakis @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action( | |
'plugins_loaded', | |
function () { | |
if ( ! class_exists( 'WPMUDEV_Forminator_Prevent_Duplicate_Submissions' ) && class_exists( 'Forminator_CForm_Front_Action' ) ) { | |
class WPMUDEV_Forminator_Prevent_Duplicate_Submissions { | |
private $form_fields_to_check = array( | |
140 => array( 'name-1', 'date-1' ), | |
270 => array( 'name-', 'date-1' ), | |
); | |
private static $_instance = null; | |
public static function get_instance() { | |
if ( is_null( self::$_instance ) ) { | |
self::$_instance = new WPMUDEV_Forminator_Prevent_Duplicate_Submissions(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'validate_submissions' ), 10, 3 ); | |
} | |
public function validate_submissions( $registration_error, $form_id, $field_data_array ) { | |
if ( isset( $this->form_fields_to_check[ $form_id ] ) ) { | |
global $wpdb; | |
$date_field_needle = 'date-'; | |
$today_date = date( 'Y-m-d' ); | |
$where_fields_joins = array(); | |
$where_fields_part_array = array( 'e.form_id=%d', 'e.date_created LIKE %s' ); | |
$where_fields_part_args = array( $form_id, $wpdb->esc_like( $today_date ) . '%' ); | |
$where_fields_part = ''; | |
$submitted_values = $form_ids = wp_list_pluck( $field_data_array, 'value', 'name' ); | |
$query = "SELECT e.entry_id FROM {$wpdb->prefix}frmt_form_entry AS e "; | |
foreach ( $this->form_fields_to_check[ $form_id ] as $join_num => $field_id ) { | |
$where_fields_joins[] = "{$wpdb->prefix}frmt_form_entry_meta AS m_{$join_num} ON e.entry_id=m_{$join_num}.entry_id"; | |
$where_fields_part_array[] = "m_{$join_num}.meta_key=%s"; | |
$where_fields_part_args[] = $field_id; | |
if ( substr( $field_id, 0, strlen( $date_field_needle ) ) === $date_field_needle ) { | |
// $submitted_date = date( 'Y-m-d', strtotime( $submitted_values[ $field_id ] ) ); | |
// $where_fields_part_array[] = "m_{$join_num}.meta_value LIKE %s"; | |
// $where_fields_part_args[] = $submitted_date ? $wpdb->esc_like( $submitted_date ) . '%' : ''; | |
} else { | |
// $where_fields_part_array[] = "m_{$join_num}.meta_value=%s"; | |
// $where_fields_part_args[] = isset( $submitted_values[ $field_id ] ) ? ( $submitted_values[ $field_id ] ) : ''; | |
} | |
$where_fields_part_array[] = "m_{$join_num}.meta_value=%s"; | |
$where_fields_part_args[] = isset( $submitted_values[ $field_id ] ) ? ( $submitted_values[ $field_id ] ) : ''; | |
} | |
// 1st add JOINS | |
$query .= 'INNER JOIN ' . implode( ' INNER JOIN ', $where_fields_joins ); | |
// 2nd add WHERE clauses | |
$query .= ' WHERE 1=1 AND '; | |
$query .= implode( ' AND ', $where_fields_part_array ); | |
// We don't need all results. One is enough. | |
$query .= ' LIMIT 1'; | |
$prepared_query = $wpdb->prepare( $query, $where_fields_part_args ); | |
$results = $wpdb->get_results( $prepared_query ); | |
if ( $results ) { | |
add_filter( | |
'forminator_custom_form_invalid_form_message', | |
function( $invalid_form_message, $form_id ) { | |
return $invalid_form_message . '<br />It looks like you have already submitted this form today.'; | |
}, | |
10, | |
2 | |
); | |
} | |
} | |
return $submit_errors; | |
} | |
} | |
WPMUDEV_Forminator_Prevent_Duplicate_Submissions::get_instance(); | |
} | |
}, | |
11 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment