| 1 |
<?php |
| 2 |
/* |
| 3 |
Plugin Name: En Spam |
| 4 |
Description: Block Spam with Cookies and JavaScript Filtering. |
| 5 |
Plugin URI: http://hatul.info/en-spam |
| 6 |
Version: 1.1 |
| 7 |
Author: Hatul |
| 8 |
Author URI: http://hatul.info |
| 9 |
License: GPL http://www.gnu.org/copyleft/gpl.html |
| 10 |
*/ |
| 11 |
|
| 12 |
class EnSpam { |
| 13 |
public function __construct() { |
| 14 |
// Load text domain |
| 15 |
load_plugin_textdomain('en-spam', false, dirname(plugin_basename(__FILE__))); |
| 16 |
|
| 17 |
// Add hooks |
| 18 |
add_filter('preprocess_comment', [$this, 'check_comment']); |
| 19 |
add_action('wp_enqueue_scripts', [$this, 'enqueue_scripts']); |
| 20 |
add_action('wp_dashboard_setup', [$this, 'add_dashboard_widgets']); |
| 21 |
add_action('wpcf7_before_send_mail', [$this, 'check_cf7'], 10, 3); |
| 22 |
add_action('elementor_pro/forms/validation', [$this, 'check_elementor_form'], 10, 2); |
| 23 |
} |
| 24 |
|
| 25 |
public function check_comment($comment) { |
| 26 |
if ($this->valid_no_bot() || wp_verify_nonce($_POST['code'], 'en-spam')) { |
| 27 |
$comment['comment_content'] = stripcslashes($comment['comment_content']); |
| 28 |
return $comment; |
| 29 |
} else { |
| 30 |
$this->block_page(); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
private function block_page() { |
| 35 |
$this->count_block(); |
| 36 |
$message = sprintf( |
| 37 |
__('For to post comment, you need to enable cookies and JavaScript or to click on "%s" button in this page', 'en-spam'), |
| 38 |
__('Post Comment') |
| 39 |
); |
| 40 |
$message .= '<form method="post">'; |
| 41 |
foreach ($_POST as $name => $value) { |
| 42 |
if ($name === 'comment') { |
| 43 |
$message .= sprintf('<label for="comment">%s</label><br /><textarea id="comment" name="comment">%s</textarea><br />', __('Your comment:', 'en-spam'), $value); |
| 44 |
} else { |
| 45 |
$message .= sprintf('<input type="hidden" name="%s" value="%s" />', $name, stripcslashes($value)); |
| 46 |
} |
| 47 |
} |
| 48 |
$message .= sprintf('<input type="hidden" name="code" value="%s" />', wp_create_nonce('en-spam')); |
| 49 |
$message .= sprintf('<input type="submit" name="submit" value="%s" />', __('Post Comment')); |
| 50 |
$message .= '</form>'; |
| 51 |
|
| 52 |
wp_die($message); |
| 53 |
} |
| 54 |
|
| 55 |
private function count_block() { |
| 56 |
$counter = get_option('ens_counter', 0) + 1; |
| 57 |
update_option('ens_counter', $counter); |
| 58 |
} |
| 59 |
|
| 60 |
public function enqueue_scripts() { |
| 61 |
wp_enqueue_script('en-spam', plugins_url('en-spam.js', __FILE__)); |
| 62 |
} |
| 63 |
|
| 64 |
public function add_dashboard_widgets() { |
| 65 |
wp_add_dashboard_widget( |
| 66 |
'en_spam_dashboard_widget', |
| 67 |
__('Blocked Spambots by En Spam', 'en-spam'), |
| 68 |
[$this, 'dashboard_widget_function'] |
| 69 |
); |
| 70 |
} |
| 71 |
|
| 72 |
public function dashboard_widget_function() { |
| 73 |
echo get_option('ens_counter', 0); |
| 74 |
} |
| 75 |
|
| 76 |
public function valid_no_bot() { |
| 77 |
return isset($_COOKIE['comment_author_email_' . COOKIEHASH]) |
| 78 |
|| is_user_logged_in() |
| 79 |
|| isset($_COOKIE['en_spam_validate']); |
| 80 |
} |
| 81 |
|
| 82 |
public function check_cf7($form, &$abort, $submission) { |
| 83 |
if (!$this->valid_no_bot()) { |
| 84 |
$abort = true; |
| 85 |
$submission->set_status('validation_failed'); |
| 86 |
$submission->set_response($form->filter_message(__('Enable JavaScript and cookies', 'en-spam'))); |
| 87 |
|
| 88 |
$this->count_block(); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
public function check_elementor_form($record, $ajax_handler) { |
| 93 |
if (!$this->valid_no_bot()) { |
| 94 |
$ajax_handler->add_error('email', __('Enable JavaScript and cookies', 'en-spam')); // Replace 'email' with the actual field ID |
| 95 |
$ajax_handler->add_error_message(__('Enable JavaScript and cookies', 'en-spam')); |
| 96 |
|
| 97 |
$this->count_block(); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// Initialize the plugin |
| 103 |
new EnSpam(); |