hollosipeter

checkbox_deletion_UNCHECKED_forminator-duplicate-field-values

Apr 20th, 2023
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | Source Code | 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.  
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14.  
  15. if ( ! class_exists( 'WPMUDEV_Forminator_Duplicate_Entries' ) ) {
  16.  
  17. class WPMUDEV_Forminator_Duplicate_Entries {
  18.  
  19. private static $_instance = null;
  20.  
  21. private $fields_to_check = array(
  22. // User defined - One form per line with selected fields (must exists in forms' settings)
  23. // Example: $form_id => array('field-1', 'field-2', ..., 'field-n')
  24. 22246 => array('phone-1', 'phone-2', 'email-1', 'email-2')
  25. );
  26.  
  27. public static function get_instance() {
  28.  
  29. if( is_null( self::$_instance ) ){
  30. self::$_instance = new WPMUDEV_Forminator_Duplicate_Entries();
  31. }
  32. return self::$_instance;
  33.  
  34. }
  35.  
  36. private function __construct() {
  37. add_action( 'forminator_custom_form_submit_errors', array( $this, 'forminator_avoid_duplicate_entries'), 999, 3 );
  38. }
  39.  
  40. function forminator_avoid_duplicate_entries($entry, $form_id, $field_data_array){
  41.  
  42. if( empty( $this->fields_to_check[$form_id] ) ) return;
  43.  
  44. global $wpdb;
  45.  
  46. $duplicate = false;
  47.  
  48. $frmt_form_entry = $wpdb->prefix . 'frmt_form_entry';
  49. $frmt_form_entry_meta = $wpdb->prefix . 'frmt_form_entry_meta';
  50.  
  51. foreach($this->fields_to_check as $form){
  52.  
  53. foreach($form as $field){
  54.  
  55. $field_key = array_search($field, array_column($field_data_array, 'name'));
  56.  
  57. if($field_key === false) continue;
  58.  
  59. $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`;";
  60.  
  61. if( $wpdb->query( $wpdb->prepare( $sql_query, $field_data_array[$field_key]['value'] ) ) ){
  62. $duplicate = true;
  63. break;
  64. }
  65.  
  66. }
  67.  
  68. }
  69.  
  70. if(!$duplicate) return;
  71.  
  72. // Just in case something has changed in database schema
  73. if(($wpdb->get_var("SHOW TABLES LIKE '$frmt_form_entry';") != $frmt_form_entry) ||
  74. ($wpdb->get_var("SHOW TABLES LIKE '$frmt_form_entry_meta';") != $frmt_form_entry_meta)) return;
  75.  
  76. $response = array(
  77. 'message' => __( 'The registration with similar data has been already submitted / A regisztráció a megadott adatokkal már rögzítésre került', 'forminator' ),
  78. 'success' => false,
  79. 'errors' => array(),
  80. 'behav' => ''
  81. );
  82.  
  83. echo wp_send_json(['success' => false, 'data' => $response]);
  84.  
  85. return true;
  86.  
  87. }
  88.  
  89. }
  90.  
  91. if ( ! function_exists( 'wpmudev_forminator_avoid_duplicate_entries' ) ) {
  92.  
  93. function wpmudev_forminator_avoid_duplicate_entries() {
  94. return WPMUDEV_Forminator_Duplicate_Entries::get_instance();
  95. };
  96.  
  97. add_action( 'plugins_loaded', 'wpmudev_forminator_avoid_duplicate_entries', 99 );
  98. }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment