Last active
June 20, 2023 11:06
-
-
Save wpmudev-sls/5e77f39c6b87c43a9529554a322cc337 to your computer and use it in GitHub Desktop.
[Forminator Pro] - Prevent duplicate entries. Allow to filter values and prevent duplicate entries for selected forms and fields
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] - Prevent duplicate entries | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Allow to filter values and prevent duplicate entries for selected forms and fields (as of 1.8.1) | |
* Author: Alessandro Kaounas @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
if ( ! class_exists( 'WPMUDEV_Forminator_Duplicate_Entries' ) ) { | |
class WPMUDEV_Forminator_Duplicate_Entries { | |
private static $_instance = null; | |
private $fields_to_check = array( | |
// User defined - One form per line with selected fields (must exists in forms' settings) | |
// Example: $form_id => array('field-1', 'field-2', ..., 'field-n') | |
1097 => array( 'email-1' ) | |
); | |
public static function get_instance() { | |
if( is_null( self::$_instance ) ){ | |
self::$_instance = new WPMUDEV_Forminator_Duplicate_Entries(); | |
} | |
return self::$_instance; | |
} | |
private function __construct() { | |
add_filter( 'forminator_custom_form_submit_errors', array( $this, 'forminator_avoid_duplicate_entries'), 999, 3 ); | |
} | |
function forminator_avoid_duplicate_entries($entry, $form_id, $field_data_array){ | |
if( empty( $this->fields_to_check[$form_id] ) ) return $entry; | |
global $wpdb; | |
$duplicate = false; | |
$frmt_form_entry = $wpdb->prefix . 'frmt_form_entry'; | |
$frmt_form_entry_meta = $wpdb->prefix . 'frmt_form_entry_meta'; | |
foreach($this->fields_to_check as $form){ | |
foreach($form as $field){ | |
$field_key = array_search($field, array_column($field_data_array, 'name')); | |
if($field_key === false) continue; | |
$sql_query = "SELECT COUNT(`meta_value`) FROM `$frmt_form_entry_meta` WHERE `entry_id` IN (SELECT `entry_id` FROM `$frmt_form_entry` WHERE `form_id` = $form_id) AND `meta_key` = '$field' AND `meta_value` = '%s' GROUP BY `meta_value`;"; | |
if( $wpdb->query( $wpdb->prepare( $sql_query, $field_data_array[$field_key]['value'] ) ) ){ | |
$duplicate = true; | |
break; | |
} | |
} | |
} | |
if(!$duplicate) return $entry; | |
// Just in case something has changed in database schema | |
if(($wpdb->get_var("SHOW TABLES LIKE '$frmt_form_entry';") != $frmt_form_entry) || | |
($wpdb->get_var("SHOW TABLES LIKE '$frmt_form_entry_meta';") != $frmt_form_entry_meta)) return $entry; | |
$response = array( | |
'message' => __( 'The survey with similar data has been already submitted / A felmérés a megadott adatokkal már rögzítésre került', 'forminator' ), | |
'success' => false, | |
'errors' => array(), | |
'behav' => '' | |
); | |
echo wp_send_json(['success' => false, 'data' => $response]); | |
return $entry; | |
} | |
} | |
if ( ! function_exists( 'wpmudev_forminator_avoid_duplicate_entries' ) ) { | |
function wpmudev_forminator_avoid_duplicate_entries() { | |
return WPMUDEV_Forminator_Duplicate_Entries::get_instance(); | |
}; | |
add_action( 'plugins_loaded', 'wpmudev_forminator_avoid_duplicate_entries', 99 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment