Advertisement
adczk

Forminator - check and prevent duplicate entries

Jun 21st, 2023
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.22 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: [Forminator Pro] - Prevent duplicate entries
  4. * Plugin URI: https://premium.wpmudev.org/
  5. * Description: Allow to filter values and prevent duplicate entries for selected forms and fields (as of 1.8.1)
  6. * Author: Alessandro Kaounas @ WPMUDEV
  7. * Author URI: https://premium.wpmudev.org/
  8. * License: GPLv2 or later
  9. */
  10. if ( ! defined( 'ABSPATH' ) ) {
  11.     exit;
  12. }
  13.  
  14. if ( ! class_exists( 'WPMUDEV_Forminator_Duplicate_Entries' ) ) {
  15.     class WPMUDEV_Forminator_Duplicate_Entries {
  16.         private static $_instance = null;
  17.         private $fields_to_check = array(
  18.             // User defined - One form per line with selected fields (must exists in forms' settings)
  19.             // Example: $form_id => array('field-1', 'field-2', ..., 'field-n')
  20.             22246 => array('phone-3', 'email-4')
  21.         );
  22.         public static function get_instance() {
  23.             if( is_null( self::$_instance ) ){
  24.                 self::$_instance = new WPMUDEV_Forminator_Duplicate_Entries();
  25.             }
  26.             return self::$_instance;
  27.         }
  28.         private function __construct() {
  29.             add_filter( 'forminator_custom_form_submit_errors', array( $this, 'forminator_avoid_duplicate_entries'), 999, 3 );     
  30.         }
  31.         function forminator_avoid_duplicate_entries($entry, $form_id, $field_data_array){
  32.             if( empty( $this->fields_to_check[$form_id] ) ) return $entry;
  33.             global $wpdb;
  34.             $duplicate = false;
  35.             $frmt_form_entry = $wpdb->prefix . 'frmt_form_entry';
  36.             $frmt_form_entry_meta = $wpdb->prefix . 'frmt_form_entry_meta';
  37.             foreach($this->fields_to_check as $form){
  38.                 foreach($form as $field){
  39.                     $field_key = array_search($field, array_column($field_data_array, 'name'));
  40.                     if($field_key === false) continue;
  41.                     $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`;";
  42.                     if( $wpdb->query( $wpdb->prepare( $sql_query, $field_data_array[$field_key]['value'] ) ) ){
  43.                         $duplicate = true;
  44.                         break;
  45.                     }
  46.                 }
  47.             }
  48.             if(!$duplicate) return $entry;
  49.             // Just in case something has changed in database schema
  50.             if(($wpdb->get_var("SHOW TABLES LIKE '$frmt_form_entry';") != $frmt_form_entry) ||
  51.             ($wpdb->get_var("SHOW TABLES LIKE '$frmt_form_entry_meta';") != $frmt_form_entry_meta)) return $entry;
  52.             $response = array(
  53.                 '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' ),
  54.                 'success' => false,
  55.                 'errors'  => array(),
  56.                 'behav'   => ''
  57.             );
  58.             echo wp_send_json(['success' => false, 'data' => $response]);
  59.             return $entry;
  60.         }
  61.     }
  62.     if ( ! function_exists( 'wpmudev_forminator_avoid_duplicate_entries' ) ) {
  63.         function wpmudev_forminator_avoid_duplicate_entries() {
  64.             return WPMUDEV_Forminator_Duplicate_Entries::get_instance();
  65.         };
  66.         add_action( 'plugins_loaded', 'wpmudev_forminator_avoid_duplicate_entries', 99 );
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement