Last active
May 29, 2023 18:06
-
-
Save ali7ali/c0b984c4d1202a8b576664c803caeb11 to your computer and use it in GitHub Desktop.
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] - Add blacklist user submission by email or IP | |
* Description: [Forminator Pro] - Add blacklist user submission by email or IP - 1155422814406806 | |
* Jira: SLS-1030 | |
* Author: Thobk @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) || ( defined( 'WP_CLI' ) && WP_CLI ) ) { | |
return; | |
} | |
add_action('plugins_loaded', 'wpmudev_forminator_blacklist_user_submission_by_email_or_ip_func', 100); | |
function wpmudev_forminator_blacklist_user_submission_by_email_or_ip_func() | |
{ | |
if (defined('FORMINATOR_PRO') && class_exists('Forminator')) { | |
class WPMUDEV_FM_Blacklist_Submission | |
{ | |
private $block_emails = ['test.com', 'ali@me.com']; // blacklist email, separated by comma (,), ex: ['test@gmail.com','test1@gmail.com']. You also can add blacklist email on Settings->General. | |
private $email_field_id = 'email-1'; | |
private $enable_block_ip = true; //set it to true to enable block by IP address | |
private $block_ips = []; // black list IP address, separated by comma (,), ex: ['123.45.678.910', '...'] | |
private $auto_block = false; // enable this option to auto block user if have number submission more than $max_submission_each_day | |
private $max_submission_each_day = 3; | |
private $block_msg = 'Hey, you are blocked!'; | |
public function __construct() | |
{ | |
add_filter('forminator_custom_form_submit_errors', array($this, 'submit_errors'), 10, 3); | |
if ($this->enable_block_ip) { | |
add_filter('forminator_custom_form_mail_admin_message', array($this, 'add_data_ip')); | |
} | |
add_action('admin_init', array($this, 'add_custom_options')); | |
if ($this->auto_block) { | |
// auto reset each day | |
$next_schedule = wp_next_scheduled('wpmudev_fm_reset_block_user'); | |
if (!$next_schedule) { | |
$schedule_time = 3600 * 2; //at 2h am | |
$now = current_time('timestamp', 1); | |
$next_event = strtotime(date("Y-m-d 00:00:00", $now)) + DAY_IN_SECONDS + $schedule_time; | |
wp_schedule_event($next_event, 'daily', 'wpmudev_fm_reset_block_user'); | |
} | |
add_action('wpmudev_fm_reset_block_user', array($this, 'reset_block_user')); | |
} | |
} | |
public function reset_block_user() | |
{ | |
return delete_option('wpmudev_fm_block_users'); | |
} | |
public function get_block_users() | |
{ | |
$block_users = get_option('wpmudev_fm_block_users', array()); | |
if (!$this->block_ips) { | |
$block_ips = get_option('wpmudev_blocked_ips'); | |
$this->block_ips = explode(',', $block_ips); | |
} | |
if (!$this->block_emails) { | |
$block_emails = get_option('wpmudev_blocked_emails'); | |
$this->block_emails = explode(',', $block_emails); | |
} | |
if ($this->auto_block && !empty($block_users)) { | |
$block_ips = array_filter($block_users, function ($no_submit) { | |
return $no_submit > $this->max_submission_each_day; | |
}); | |
if ($block_ips) { | |
$this->block_ips = array_merge($this->block_ips, array_keys($block_ips)); | |
} | |
} | |
return $block_users; | |
} | |
public function get_user_ip() | |
{ | |
static $user_ip; | |
if (!$user_ip) { | |
$user_ip = Forminator_Geo::get_user_ip(); | |
} | |
return $user_ip; | |
} | |
public function add_data_ip($message) | |
{ | |
$message .= '<p>IP: ' . $this->get_user_ip() . '</p>'; | |
return $message; | |
} | |
public function add_custom_options() | |
{ | |
register_setting( | |
'general', | |
'wpmudev_blocked_emails' | |
); | |
add_settings_field( | |
'wpmudev_blocked_emails', | |
'Enter blocklisted emails', | |
array($this, 'blocklisted_field'), | |
'general', | |
'default', | |
array('name' => 'wpmudev_blocked_emails', 'type' => 'Email') | |
); | |
register_setting( | |
'general', | |
'wpmudev_blocked_ips' | |
); | |
add_settings_field( | |
'wpmudev_blocked_ips', | |
'Enter blocklisted IPs', | |
array($this, 'blocklisted_field'), | |
'general', | |
'default', | |
array('name' => 'wpmudev_blocked_ips', 'type' => 'IP address') | |
); | |
} | |
public function blocklisted_field($args) | |
{ | |
// d( $args['name'] ); | |
$block_value = get_option($args['name'], ''); | |
// d( $block_value ); | |
printf('<input type="text" id="%s" size="100" name="%s" value="%s">', $args['name'], $args['name'], $block_value); | |
printf('<p class="description">Add %s you want to block from Forminator submission, separeted by comma(,).</p>', $args['type']); | |
} | |
public function submit_errors($submit_errors, $form_id, $field_data_array) | |
{ | |
if (empty($submit_errors)) { | |
// ini block users | |
$block_users = $this->get_block_users(); | |
$user_ip = $this->get_user_ip(); | |
$can_submit = 1; | |
if ($this->enable_block_ip) { | |
if ($this->block_ips) { | |
if (in_array($user_ip, $this->block_ips)) { | |
$can_submit = false; | |
} | |
} | |
if ($can_submit) { | |
// save current user IP if enabled auto block user | |
if ($this->auto_block) { | |
$block_users[$user_ip] = isset($block_users[$user_ip]) ? $block_users[$user_ip] : 0; | |
$block_users[$user_ip]++; | |
update_option('wpmudev_fm_block_users', $block_users, false); | |
} | |
} | |
} | |
$block_emails = get_option('wpmudev_blocked_emails'); | |
if (isset($block_emails) && !empty($block_emails)) { | |
$block_emails = array_map('trim', explode(',', $block_emails)); | |
if (is_array($block_emails)) { | |
$black_listed = array_merge($this->block_emails, $block_emails); | |
$black_listed = array_unique($black_listed); | |
} | |
} | |
// $custom_form = Forminator_Custom_Form_Model::model()->load( $form_id ); | |
// $post_data = $_POST; | |
// $fields = $custom_form->get_fields(); | |
if ($can_submit) { | |
foreach ($field_data_array as $field) { | |
if ($this->email_field_id === $field['name']) { | |
$email = trim($field['value']); | |
$domain = substr($email, strpos($email, '@') + 1); | |
if (in_array($email, $black_listed)) { | |
$can_submit = false; | |
} | |
if (in_array($domain, $black_listed)) { | |
$can_submit = false; | |
} | |
break; | |
} | |
} | |
} | |
if (!$can_submit) { | |
$submit_errors[]['block_email'] = $this->block_msg; | |
// custom error message | |
add_filter('forminator_custom_form_invalid_form_message', array($this, 'custom_error_message')); | |
} | |
} | |
return $submit_errors; | |
} | |
public function custom_error_message($error_msg) | |
{ | |
$error_msg = $this->block_msg; | |
// remove filter | |
remove_filter('forminator_custom_form_invalid_form_message', array($this, 'custom_error_message')); | |
return $error_msg; | |
} | |
} | |
$run = new WPMUDEV_FM_Blacklist_Submission; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment