Plugin Directory

Changeset 3205588


Ignore:
Timestamp:
12/10/2024 12:00:57 PM (16 months ago)
Author:
hatul
Message:

v1.1

File:
1 edited

Legend:

Unmodified
Added
Removed
  • en-spam/trunk/en-spam.php

    r3114115 r3205588  
    44Description: Block Spam with Cookies and JavaScript Filtering.
    55Plugin URI: http://hatul.info/en-spam
    6 Version: 1.0
     6Version: 1.1
    77Author: Hatul
    88Author URI: http://hatul.info
     
    1010*/
    1111
    12 load_plugin_textdomain('en-spam', false, dirname( plugin_basename( __FILE__ ) ) );
     12class EnSpam {
     13    public function __construct() {
     14        // Load text domain
     15        load_plugin_textdomain('en-spam', false, dirname(plugin_basename(__FILE__)));
    1316
    14 add_filter('preprocess_comment','ens_check_comment');
    15 function ens_check_comment($comment){
    16     if (ens_valid_no_bot() || wp_verify_nonce($_POST['code'], 'en-spam')) {
    17         $comment['comment_content'] = stripcslashes($comment['comment_content']);
    18         return $comment;
    19     }
    20     else ens_block_page();
     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    }
    21100}
    22101
    23 function ens_block_page(){
    24     ens_count_block();
    25 
    26     $message = sprintf(__('For to post comment, you need to enable cookies and JavaScript or to click on "%s" button in this page', 'en-spam'), __( 'Post Comment' ));
    27     $message .= '<form method="post">';
    28     foreach ($_POST as $name=>$value){
    29         if ($name == 'comment')
    30             $message .= sprintf('<label for="comment">%s</label><br /><textarea id="comment" name="comment">%s</textarea><br />',__('Your comment:', 'en-spam'), $value);
    31         else
    32             $message .= sprintf('<input type="hidden" name="%s" value="%s" />', $name, stripcslashes($value));
    33     }
    34     $message .= sprintf('<input type="hidden" name="code" value="%s" />', wp_create_nonce('en-spam'));
    35     $message .= sprintf('<input type="submit" name="submit" value="%s" />', __( 'Post Comment' ));
    36     $message .= '</form>';
    37 
    38     wp_die($message);
    39 }
    40 
    41 function ens_count_block(){
    42     $counter = get_option('ens_counter', 0) + 1;
    43     update_option('ens_counter', $counter);
    44 }
    45 
    46 add_action('wp_enqueue_scripts', 'ens_scripts');
    47 function ens_scripts() {
    48     wp_enqueue_script('en-spam', plugins_url('en-spam.js', __FILE__));
    49 }
    50 
    51 function ens_add_dashboard_widgets() {
    52     wp_add_dashboard_widget(
    53                  'en_spam_dashboard_widget',
    54                  __('Blocked Spambots by En Spam', 'en-spam'),
    55                  'ens_dashboard_widget_function'
    56         );
    57 }
    58 
    59 add_action( 'wp_dashboard_setup', 'ens_add_dashboard_widgets' );
    60 function ens_dashboard_widget_function() {
    61     echo get_option('ens_counter', 0);
    62 }
    63 
    64 function ens_valid_no_bot(){
    65     return isset($_COOKIE['comment_author_email_' . COOKIEHASH])
    66     || is_user_logged_in()
    67     || isset($_COOKIE['en_spam_validate']);
    68 }
    69 
    70 // CF7
    71 function ens_check_cf7($form, &$abort, $submission) {
    72     if (! ens_valid_no_bot()) {
    73         $abort = true;
    74         $submission->set_status( 'validation_failed' );
    75         $submission->set_response($form->filter_message(__('Enable JavaScript and cookies', 'en-spam')));
    76        
    77         ens_count_block();
    78     }
    79 }
    80 add_action( 'wpcf7_before_send_mail', 'ens_check_cf7', 10, 3);
    81 
    82 // Elementor Form
    83 function ens_check_elementor_form($record, $ajax_handler) {
    84     if (! ens_valid_no_bot()){
    85         $ajax_handler->add_error('field_id', __('Enable JavaScript and cookies', 'en-spam')); // stop sending
    86         $ajax_handler->add_error_message(__('Enable JavaScript and cookies', 'en-spam'));
    87     }
    88 }
    89 add_action( 'elementor_pro/forms/validation', 'ens_check_elementor_form');
     102// Initialize the plugin
     103new EnSpam();
Note: See TracChangeset for help on using the changeset viewer.