Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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')
- 22246 => array('phone-3', 'email-4')
- );
- 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 );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement