Free & Easy Auto-Updating Spam Filter for WordPress

Wp settings filter spam with blacklist txt updateblacklistwithuserkeyspreserved snipsnip pro code snippet

This is a smart automated feature for website operators using WordPress and Code Snippets Pro ❤️ plugin. It allows for daily updates of the 'disallowed keys' option which essentially forms a blacklist for unwelcome keys while ensuring that user-defined keys are preserved even after updates.

What it does…

More specifically, this code fetches an updated blacklist from a URL (https://github.com/splorp/wordpress-comment-blacklist) daily and refreshes the website’s 'disallowed keys' option, which blocks the processing of any keys listed on the blacklist. The function maintains any user-defined keys, allowing site operators to permanently block specific keys.

Why it does it…

This automated approach is particularly beneficial for large sites where certain types of input can be undesirable or potentially harmful. Regular updates improve resistance against spam and harmful content, and also, preserving user-defined blacklist keys ensures that website operators can continually identify specific unwanted inputs.

How it does it…

The PHP class 'UpdateBlacklistWithUserKeysPreserved' is first defined and executed. The default configurations for enabling/disabling the update task and the scheduled update time are set.

  • In the constructor function, the update task is either initiated or canceled based on the $enableTask variable.
  • If the update task is enabled, the schedule_update_event method is triggered to set up a update_disallowed_keys_daily_event, and the update_disallowed_keys function is immediately activated. If the task is disabled, any existing scheduled event for the update_disallowed_keys_daily_event is stopped.
  • The schedule_update_event function sets up a scheduled task (via wp_schedule_event) to trigger a update_disallowed_keys_daily_event action.
  • The update_disallowed_keys function fetches and processes the updated blacklist from the URL, maintains any user-defined keys found in the old list, and finally saves the new list into the disallowed_keys option.

Download JSON for importing into Code Snippets Pro
Download PHP for adding as a plugin or copying into functions.php

See the code…

<?php

if (!class_exists('UpdateBlacklistWithUserKeysPreserved')) {
	// WP Settings - Filter Spam with blacklist.txt - UpdateBlacklistWithUserKeysPreserved [SnipSnip.pro] - https://snipsnip.pro/s/770
    class UpdateBlacklistWithUserKeysPreserved {
        // Config: Set to true to enable updating task, false to disable
        private $enableTask = true;
        // Config: Schedule task to run on following time
        private $scheduled_Hour = '4am';

        // Divider
        private $divider = "\n** Add custom filters above this line. Everything below will be auto-updated daily at ";

        public function __construct() {
            // Append additional instruction to divider
            $this->divider .= $this->scheduled_Hour . ". **\n** This auto-updating feature is controlled by a script in the site's Code Snippets Pro plugin. **"
                . "\n** Code from: https://SnipSnip.pro/s/770 -- Blacklist from: https://github.com/splorp/wordpress-comment-blacklist **\n";

            if ($this->enableTask) {
                add_action('wp', array($this, 'schedule_update_event'));
                add_action('update_disallowed_keys_daily_event', array($this, 'update_disallowed_keys'));
                $this->update_disallowed_keys(); // run the task immediately
            } else {
                // Unschedules the scheduled cron job when $enableTask is set to false
                if (wp_next_scheduled('update_disallowed_keys_daily_event')) {
                    wp_clear_scheduled_hook('update_disallowed_keys_daily_event');
                }
            }
        }

        // Schedule an action if it's not already scheduled
        public function schedule_update_event() {
            if (wp_next_scheduled('update_disallowed_keys_daily_event')) {
                wp_clear_scheduled_hook('update_disallowed_keys_daily_event');
            }
            $nextTime = strtotime('next day ' . $this->scheduled_Hour);
            wp_schedule_event($nextTime, 'daily', 'update_disallowed_keys_daily_event');
        }

        // Update 'disallowed_keys' option daily
        public function update_disallowed_keys() {
            $response = wp_remote_get('https://raw.githubusercontent.com/splorp/wordpress-comment-blacklist/master/blacklist.txt');
            if (!is_wp_error($response) && $response['response']['code'] == 200) {
                $blacklist = wp_remote_retrieve_body($response);
                $old_blacklist = get_option('disallowed_keys', ''); // get old blacklist
                $old_blacklist_array = explode($this->divider, $old_blacklist);

                // check if old blacklist had user keys
                if (count($old_blacklist_array) > 1) {
                    $user_keys = $old_blacklist_array[0];
                    $new_blacklist = $user_keys . $this->divider . $blacklist;
                } else {
                    $new_blacklist = $old_blacklist . $this->divider . $blacklist;
                }

                update_option('disallowed_keys', $new_blacklist);
            }
        }
    }

    // Instantiate the class
    new UpdateBlacklistWithUserKeysPreserved();
}