<?php

if (!class_exists('WPFresh5Module')) {

    /**
     * Abstract class to define/implement base methods for all module classes
     */
    abstract class WPFresh5Module
    {
        private static $instances = array();


        /*
         * Magic methods
         */

        /**
         * Public getter for protected variables
         *
         * @mvc Model
         *
         * @param string $variable
         * @throws Exception
         * @return mixed
         */
        public function __get($variable)
        {
            $module = get_called_class();

            if (in_array($variable, $module::$readable_properties)) {
                return $this->$variable;
            } else {
                throw new Exception(__METHOD__ . " error: $" . $variable . " doesn't exist or isn't readable.");
            }
        }

        /**
         * Public setter for protected variables
         *
         * @mvc Model
         *
         * @param string $variable
         * @param mixed $value
         * @throws Exception
         */
        public function __set($variable, $value)
        {
            $module = get_called_class();

            if (in_array($variable, $module::$writeable_properties)) {
                $this->$variable = $value;

                if (!$this->is_valid()) {
                    throw new Exception(__METHOD__ . ' error: $' . $value . ' is not valid.');
                }
            } else {
                throw new Exception(__METHOD__ . " error: $" . $variable . " doesn't exist or isn't writable.");
            }
        }


        /*
         * Non-abstract methods
         */

        /**
         * Provides access to a single instance of a module using the singleton pattern
         *
         * @mvc Controller
         *
         * @return object
         */
        public static function get_instance()
        {
            $module = get_called_class();

            if (!isset(self::$instances[$module])) {
                self::$instances[$module] = new $module();
            }

            return self::$instances[$module];
        }

        /**
         * Render a template
         *
         * Allows parent/child themes to override the markup by placing the a file named basename( $default_template_path ) in their root folder,
         * and also allows plugins or themes to override the markup by a filter. Themes might prefer that method if they place their templates
         * in sub-directories to avoid cluttering the root folder. In both cases, the theme/plugin will have access to the variables so they can
         * fully customize the output.
         *
         * @mvc @model
         *
         * @param bool|string $default_template_path The path to the template, relative to the plugin's `views` folder
         * @param  array $variables An array of variables to pass into the template's scope, indexed with the variable name so that it can be extract()-ed
         * @param  string $require 'once' to use require_once() | 'always' to use require()
         * @return string
         */
        protected static function render_template($default_template_path = false, $variables = array(), $require = 'once')
        {
            do_action('wpsf_render_template_pre', $default_template_path, $variables);

            $template_path = locate_template(basename($default_template_path));
            if (!$template_path) {
                $template_path = dirname(__DIR__) . '/views/' . $default_template_path;
            }
            $template_path = apply_filters('wpsf_template_path', $template_path);

            if (is_file($template_path)) {
                extract($variables);
                ob_start();

                if ('always' == $require) {
                    require($template_path);
                } else {
                    require_once($template_path);
                }

                $template_content = apply_filters('wpsf_template_content', ob_get_clean(), $default_template_path, $template_path, $variables);
            } else {
                $template_content = '';
            }

            do_action('wpsf_render_template_post', $default_template_path, $variables, $template_path, $template_content);
            return $template_content;
        }


        /*
         * Abstract methods
         */

        /**
         * Constructor
         *
         * @mvc Controller
         */
        abstract protected function __construct();

        /**
         * Prepares sites to use the plugin during single or network-wide activation
         *
         * @mvc Controller
         *
         * @param bool $network_wide
         */
        abstract public function activate($network_wide);

        /**
         * Rolls back activation procedures when de-activating the plugin
         *
         * @mvc Controller
         */
        abstract public function deactivate();

        /**
         * Register callbacks for actions and filters
         *
         * @mvc Controller
         */
        abstract public function register_hook_callbacks();

        /**
         * Initializes variables
         *
         * @mvc Controller
         */
        abstract public function init();

        /**
         * Checks if the plugin was recently updated and upgrades if necessary
         *
         * @mvc Controller
         *
         * @param int|string $db_version
         * @return
         */
        abstract public function upgrade($db_version = 0);

        /**
         * Checks that the object is in a correct state
         *
         * @mvc Model
         *
         * @param string $property An individual property to check, or 'all' to check all of them
         * @return bool
         */
        abstract protected function is_valid($property = 'all');
    } // end WPFresh5Module
}

if (!class_exists('WPFresh5Antispam')) {

    /**
     * Main / front controller class
     */
    class WPFresh5Antispam extends WPFresh5Module
    {
        /**
         * @var array
         */
        protected static $readable_properties = array(); // These should really be constants, but PHP doesn't allow class constants to be arrays

        /**
         * @var array
         */
        protected static $writeable_properties = array();

        /**
         * Modules of this plugin. Currently only WPSF_Settings.
         *
         * @var array
         */
        protected $modules;

        /**
         * Plugin version number
         */
        const VERSION = '0.5.1';

        /**
         * prefix for this plugin, used for enqueued styles and scripts.
         */
        const PREFIX = 'wpsf_';

        /*
         * Magic methods
         */

        /**
         * Constructor
         *
         * @mvc Controller
         */
        protected function __construct()
        {
            $this->register_hook_callbacks();

            $this->modules = array(
                'WPSF_Settings' => /*WPSF_Settings::get_instance()*/ false
            );
        }


        /*
         * Static methods
         */

        /**
         * Enqueues CSS, JavaScript, etc
         *
         * @mvc Controller
         */
        public static function load_resources()
        {
            // wp_register_script(
            //     self::PREFIX . 'wp-spam-fighter-admin',
            //     plugins_url('javascript/wp-spamfighter-admin.js', dirname(__FILE__)),
            //     array('jquery'),
            //     self::VERSION,
            //     true
            // );

            // wp_register_script(
            //     self::PREFIX . 'wp-spam-fighter',
            //     plugins_url('javascript/wp-spamfighter.js', dirname(__FILE__)),
            //     array('jquery'),
            //     self::VERSION,
            //     true
            // );

            wp_register_style(
                self::PREFIX . 'admin',
                plugins_url('css/admin.css', dirname(__FILE__)),
                array(),
                self::VERSION,
                'all'
            );

            wp_register_style(
                self::PREFIX . 'wpsf',
                plugins_url('css/wpsf.css', dirname(__FILE__)),
                array(),
                self::VERSION,
                'all'
            );

            wp_register_script("recaptcha", "https://www.google.com/recaptcha/api.js");

            if (is_admin()) {
                wp_enqueue_style(self::PREFIX . 'admin');
                wp_enqueue_script(self::PREFIX . 'wp-spam-fighter-admin');
            } else {
                wp_enqueue_style(self::PREFIX . 'wpsf');
                wp_enqueue_script(self::PREFIX . 'wp-spam-fighter');
                wp_enqueue_script("recaptcha");
            }
        }

        /**
         * Clears caches of content generated by caching plugins like WP Super Cache
         *
         * @mvc Model
         */
        protected static function clear_caching_plugins()
        {
            // WP Super Cache
            if (function_exists('wp_cache_clear_cache')) {
                wp_cache_clear_cache();
            }

            // W3 Total Cache
            if (class_exists('W3_Plugin_TotalCacheAdmin')) {
                $w3_total_cache = w3_instance('W3_Plugin_TotalCacheAdmin');

                if (method_exists($w3_total_cache, 'flush_all')) {
                    $w3_total_cache->flush_all();
                }
            }
        }


        /*
         * Instance methods
         */

        /**
         * Prepares sites to use the plugin during single or network-wide activation
         *
         * @mvc Controller
         *
         * @param bool $network_wide
         */
        public function activate($network_wide)
        {
            global $wpdb;

            if (function_exists('is_multisite') && is_multisite()) {
                if ($network_wide) {
                    $blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs");

                    foreach ($blogs as $blog) {
                        switch_to_blog($blog);
                        $this->single_activate($network_wide);
                    }

                    restore_current_blog();
                } else {
                    $this->single_activate($network_wide);
                }
            } else {
                $this->single_activate($network_wide);
            }
        }

        /**
         * Runs activation code on a new WPMS site when it's created
         *
         * @mvc Controller
         *
         * @param int $blog_id
         */
        public function activate_new_site($blog_id)
        {
            switch_to_blog($blog_id);
            $this->single_activate(true);
            restore_current_blog();
        }

        /**
         * Prepares a single blog to use the plugin
         *
         * @mvc Controller
         *
         * @param bool $network_wide
         */
        protected function single_activate($network_wide)
        {
            foreach ($this->modules as $module) {
                $module->activate($network_wide);
            }
        }

        /**
         * Rolls back activation procedures when de-activating the plugin
         *
         * @mvc Controller
         */
        public function deactivate()
        {
            foreach ($this->modules as $module) {
                $module->deactivate();
            }
        }

        /**
         * Register callbacks for actions and filters
         *
         * @mvc Controller
         */
        public function register_hook_callbacks()
        {
            add_action('wpmu_new_blog', __CLASS__ . '::activate_new_site');
            add_action('wp_enqueue_scripts', __CLASS__ . '::load_resources');
            // add_action('admin_enqueue_scripts', __CLASS__ . '::load_resources');
            add_action('login_enqueue_scripts', __CLASS__ . '::load_resources');

            add_action('comment_form_before', array($this, 'comment_form_before'));
            add_action('comment_form_after_fields', array($this, 'comment_form_after_fields'), 1);
            add_action('comment_form_logged_in_after', array($this, 'comment_form_after_fields'), 1);
            add_action('pre_comment_on_post', array($this, 'pre_comment_on_post'));
            add_filter('pre_comment_approved', array($this, 'pre_comment_approved'), 10, 2);
            add_action('comment_post', array($this, 'comment_post'), 10, 2);
            // add_filter('preprocess_comment', array($this, 'verify_comment_captcha'));


            add_action('register_form', array($this, 'register_form'));
            add_action('signup_extra_fields', array($this, 'register_form'));
            add_filter('registration_errors', array($this, 'registration_errors'), 10, 3);
            add_filter('wpmu_validate_user_signup', array($this, 'wpmu_validate_user_signup'), 10, 1);

            // add_action('init', 'session_start');

            add_action('init', array($this, 'init'));
            // add_action('init', array($this, 'upgrade'), 11);
        }

        /**
         * Initializes variables
         *
         * @mvc Controller
         */
        public function init()
        {
            try {
            } catch (Exception $exception) {
                add_notice(__METHOD__ . ' error: ' . $exception->getMessage(), 'error');
            }
        }


        /**
         * Checks if the plugin was recently updated and upgrades if necessary
         *
         * @mvc Controller
         *
         * @param int|string $db_version
         */
        public function upgrade($db_version = 0)
        {
            return false;
            // if (version_compare($this->modules['WPSF_Settings']->settings['db-version'], self::VERSION, '==')) {
            //     return;
            // }

            // foreach ($this->modules as $module) {
            //     $module->upgrade($this->modules['WPSF_Settings']->settings['db-version']);
            // }

            // $this->modules['WPSF_Settings']->settings = array('db-version' => self::VERSION);
            // self::clear_caching_plugins();
        }

        /**
         * Checks that the object is in a correct state
         *
         * @mvc Model
         *
         * @param string $property An individual property to check, or 'all' to check all of them
         * @return bool
         */
        protected function is_valid($property = 'all')
        {
            return true;
        }

        public function verify_comment_captcha($commentdata)
        {
            if (isset($_POST['g-recaptcha-response'])) {
                $recaptcha_secret = $this->modules['WPSF_Settings']->settings['recaptcha']['captcha_secret_key'];
                $response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret="
                    . $recaptcha_secret . "&response=" . $_POST['g-recaptcha-response']);
                $response = json_decode($response, true);
                if (true == $response["success"]) {
                    return $commentdata;
                } else {
                    if ($commentdata != "verify_comment_captcha") {
                        echo __("Bots are not allowed to submit comments.");
                    }
                    return null;
                }
            } else {
                if ($this->modules['WPSF_Settings']->settings['others']['logged_in_users'] || !is_user_logged_in() &&
                    ($this->modules['WPSF_Settings']->settings['recaptcha']['recaptcha'])
                ) {
                    if ($commentdata != "verify_comment_captcha") {
                        echo __("Bots are not allowed to submit comments. If you are not a bot then please enable JavaScript in browser.");
                    }
                    return null;
                } else {
                    return $commentdata;
                }
            }
        }

        /**
         * Fired before the comment form.
         * Adds JavaScript for the timestamp spam protection.
         */
        public function comment_form_before()
        {
            $timestamp = 0;
            /*if ($this->modules['WPSF_Settings']->settings['timestamp']['timestamp']) {
                $timestamp = 10 * 1000;
            }*/

            //$client_message = $this->modules['WPSF_Settings']->settings['timestamp']['client_message'];
            $timestamp = 10000;
            $client_message = 'This site is protected by an anti-spam feature that requires 10 seconds to have elapsed between the page load and the form submission. \n\n Please close this alert window. The form may be resubmitted successfully in 10 seconds.';
            ?>

            <script type="text/javascript">
                window.wpsf_timestamp_enabled = true;
                window.wpsf_threshold = <?php echo $timestamp; ?>;
                window.wpsf_message = '<?php echo $client_message; ?>';

                String.prototype.format = function (args) {
                    var str = this;
                    return str.replace(String.prototype.format.regex, function (item) {
                        var intVal = parseInt(item.substring(1, item.length - 1));
                        var replace;
                        if (intVal >= 0) {
                            replace = args[intVal];
                        } else if (intVal === -1) {
                            replace = "{";
                        } else if (intVal === -2) {
                            replace = "}";
                        } else {
                            replace = "";
                        }
                        return replace;
                    });
                };
                String.prototype.format.regex = new RegExp("{-?[0-9]+}", "g");
            </script>
        <?php
        }

        /**
         * Fired before a comment is posted.
         * Checks the timestamps in case the timestamp spam protection is on.
         */
        public function pre_comment_on_post()
        {
            if (1/*$this->modules['WPSF_Settings']->settings['timestamp']['timestamp']*/) {
                $timestamp = 10 * 1000;
                $wpsfTS1 = (isset($_POST['wpsfTS1'])) ? trim($_POST['wpsfTS1']) : 1;
                $wpsfTS2 = (isset($_POST['wpsfTS2'])) ? trim($_POST['wpsfTS2']) : 2;
                if (($wpsfTS2 - $wpsfTS1) < $timestamp['threshold']) {
                    wp_die(/*$this->modules['WPSF_Settings']->settings['timestamp']['server_message']*/ 'This site is protected by an anti-spam feature that requires {0} seconds to have elapsed between the page load and the form submission. \n\n Please close this alert window.  The form may be resubmitted successfully in {1} seconds.');
                }
            }
        }

        /**
         * Fired after the comment fields in the comment form.
         * Adds JavaScript to switch on spam protectors and HTML element required for some spam protectors.
         *
         * @param $postID
         */
        function comment_form_after_fields($postID)
        {
            global $wpsf_recaptcha_enabled, $wpsf_not_a_spammer_enabled, $wpsf_timestamp_enabled, $wpsf_honeypot_enabled, $wpsf_javascript_enabled;

            if (/*$this->modules['WPSF_Settings']->settings['others']['logged_in_users'] || */!is_user_logged_in()) {
                if (1/*$this->modules['WPSF_Settings']->settings['others']['javascript']*/) {
                    if (!$wpsf_javascript_enabled) {
                        ?>
                        <script type="text/javascript">
                            window.wpsf_javascript_enabled = true;
                        </script>
                        <?php
                        $wpsf_javascript_enabled = true;
                    }
                }

                if (1/*$this->modules['WPSF_Settings']->settings['timestamp']['timestamp']*/) {
                    if (!$wpsf_timestamp_enabled) {
                        ?>
                        <script type="text/javascript">
                            window.wpsf_timestamp_enabled = true;
                        </script>
                        <?php
                        $wpsf_timestamp_enabled = true;
                    }
                }

                if (1 /*$this->modules['WPSF_Settings']->settings['honeypot']['honeypot']*/) {
                    if (!$wpsf_honeypot_enabled) {
                        if (1/*$this->modules['WPSF_Settings']->settings['honeypot']['honeypot_type'] === "textarea"*/) {
                            ?>
                            <textarea
                                id="<?php echo 'more_comment' ?>"
                                aria-required="true" rows="8" cols="45"
                                name="<?php echo 'more_comment' ?>"
                                style="display: none;"></textarea>
                        <?php
                        } elseif (0) {
                            ?>
                            <input type="hidden"
                                   id="<?php echo 'more_comment' ?>"
                                   aria-required="true"
                                   name="<?php echo 'more_comment' ?>">
                        <?php
                        } elseif (0) {
                            ?>
                            <input type="text"
                                   id="<?php echo 'more_comment' ?>"
                                   aria-required="true" size="30" value=""
                                   name="<?php echo 'more_comment' ?>"
                                   style="display: none;">
                        <?php
                        }
                        $wpsf_honeypot_enabled = true;
                    }
                }

                if (0/*$this->modules['WPSF_Settings']->settings['others']['not_a_spammer']*/) {
                    if (!$wpsf_not_a_spammer_enabled) {
                        ?>
                        <p id="wpsf_p" style="clear:both;"></p>
                        <script type="text/javascript">
                            window.wpsf_not_a_spammer_enabled = true;
                            window.not_a_spammer_label = "<?php echo __('I am not a spammer', 'wpsf_domain'); ?>";
                            window.not_a_spammer_user_message = "<?php echo __('Please confirm that you are not a spammer', 'wpsf_domain'); ?>";
                        </script>
                        <noscript><?php echo __('Please enable javascript in order to be allowed to comment', 'wpsf_domain'); ?></noscript>
                        <?php
                        $wpsf_not_a_spammer_enabled = true;
                    }
                }

                if (0/*$this->modules['WPSF_Settings']->settings['recaptcha']['recaptcha']*/) {
                    if (!$wpsf_recaptcha_enabled) {
                        ?>
                        <p id="wpsf_p" style="clear:both;"></p>
                        <script type="text/javascript">
                            window.wpsf_recaptcha_enabled = true;
                            window.captcha_site_key = "<?php echo $this->modules['WPSF_Settings']->settings['recaptcha']['captcha_site_key']; ?>";
                        </script>
                        <noscript><?php echo __('Please enable javascript in order to be allowed to comment', 'wpsf_domain'); ?></noscript>
                        <?php
                        $wpsf_recaptcha_enabled = true;
                    }
                }
            }
        }

        function comment_post($comment_ID, $approved)
        {
        }

        /**
         * Filter a comment’s approval status before it is set.
         * Checks the comment data for spam.
         *
         * @param $approved
         * @param $comment_data
         * @return string
         */
        function pre_comment_approved($approved, $comment_data)
        {
            if (!$this->modules['WPSF_Settings']->settings['others']['trackbacks'] || $comment_data['comment_type'] == 'pingback' || $comment_data['comment_type'] == 'trackback') {
                if ($this->modules['WPSF_Settings']->settings['others']['logged_in_users'] || !is_user_logged_in()) {
                    if ((1/*$this->modules['WPSF_Settings']->settings['honeypot']['honeypot']*/)
                        && (!empty($_POST['more_comment']))
                    ) {
                        $approved = 'spam';
                    } elseif (($this->modules['WPSF_Settings']->settings['others']['avatar'])
                        && !$this->check_avatar($comment_data['comment_author_email'])
                    ) {
                        $approved = 'spam';
                    } elseif (($this->modules['WPSF_Settings']->settings['others']['not_a_spammer'])
                        && (empty($_POST["wpsf_not_a_spammer"]))
                    ) {
                        $approved = 'spam';
                    } elseif (($this->modules['WPSF_Settings']->settings['others']['javascript'])
                        && (empty($_POST["wpsf_javascript"]) || $_POST["wpsf_javascript"] != "WPSF_JAVASCRIPT_TOKEN")
                    ) {
                        $approved = 'spam';
                    }
                }
            }
            if ($approved === 'spam') {
                if (isset($this->modules['WPSF_Settings']->settings['others']['delete']) && $this->modules['WPSF_Settings']->settings['others']['delete']) {
                    add_action('wp_insert_comment', array(&$this, 'handle_auto_trash'), 0, 2);
                }
                if (isset($this->modules['WPSF_Settings']->settings['others']['discard']) && $this->modules['WPSF_Settings']->settings['others']['discard']) {
                    add_action('wp_insert_comment', array(&$this, 'handle_auto_delete'), 0, 2);
                }
            }
            return $approved;
        }

        public function handle_auto_delete($id, $comment)
        {
            if (!$comment && !is_object($cmt = get_comment($comment))) {
                return;
            }
            wp_delete_comment($id, true);
        }

        public function handle_auto_trash($id, $comment)
        {
            if (!$comment && !is_object($cmt = get_comment($comment))) {
                return;
            }
            wp_trash_comment($id, false);
        }

        /**
         * Fired following the ‘E-mail’ field in the user registration form.
         * Called both in single site and multi site deployment.
         * Adds the JavaScript code and HTML elements required by the different spam protectors.
         */
        public function register_form()
        {
            global $wpsf_recaptcha_enabled, $wpsf_not_a_spammer_enabled, $wpsf_timestamp_enabled, $wpsf_honeypot_enabled, $wpsf_javascript_enabled;

            if (/*$this->modules['WPSF_Settings']->settings['others']['registration']*/1) {
                if (1/*$this->modules['WPSF_Settings']->settings['others']['javascript']*/) {
                    if (!$wpsf_javascript_enabled) {
                        ?>
                        <script type="text/javascript">
                            window.wpsf_javascript_enabled = true;
                        </script>
                        <?php
                        $wpsf_javascript_enabled = true;
                    }
                }

                if (1/*$this->modules['WPSF_Settings']->settings['timestamp']['timestamp']*/) {
                    if (!$wpsf_timestamp_enabled) {
                        ?>
                        <script type="text/javascript">
                            window.wpsf_timestamp_enabled = true;
                        </script>
                        <?php
                        $wpsf_timestamp_enabled = true;
                    }
                }

                if (1/*$this->modules['WPSF_Settings']->settings['honeypot']['honeypot']*/) {
                    if (!$wpsf_honeypot_enabled) {
                        if (1/*$this->modules['WPSF_Settings']->settings['honeypot']['honeypot_type'] === "textarea"*/) {
                            ?>
                            <textarea
                                id="<?php echo 'more_comment' ?>"
                                aria-required="true" rows="8" cols="45"
                                name="<?php echo 'more_comment' ?>"
                                style="display: none;"></textarea>
                        <?php
                        } elseif (0/*$this->modules['WPSF_Settings']->settings['honeypot']['honeypot_type'] === "hidden"*/) {
                            ?>
                            <input type="hidden"
                                   id="<?php echo 'more_comment' ?>"
                                   aria-required="true"
                                   name="<?php echo 'more_comment' ?>">
                        <?php
                        } elseif (0/*$this->modules['WPSF_Settings']->settings['honeypot']['honeypot_type'] === "text"*/) {
                            ?>
                            <input type="text"
                                   id="<?php echo 'more_comment' ?>"
                                   aria-required="true" size="30" value=""
                                   name="<?php echo 'more_comment' ?>"
                                   style="display: none;">
                        <?php
                        }
                        $wpsf_honeypot_enabled = true;
                    }
                }

                if (0/*$this->modules['WPSF_Settings']->settings['others']['not_a_spammer']*/) {
                    if (!$wpsf_not_a_spammer_enabled) {
                        ?>
                        <p id="wpsf_p" style="clear:both;"></p>
                        <script type="text/javascript">
                            window.wpsf_not_a_spammer_enabled = true;
                            window.not_a_spammer_label = "<?php echo __('I am not a spammer', 'wpsf_domain'); ?>";
                            window.not_a_spammer_user_message = "<?php echo __('Please confirm that you are not a spammer', 'wpsf_domain'); ?>";
                        </script>
                        <noscript><?php echo __('Please enable javascript in order to be allowed to comment', 'wpsf_domain'); ?></noscript>
                        <?php
                        $wpsf_not_a_spammer_enabled = true;
                    }
                }

                if (0/*$this->modules['WPSF_Settings']->settings['recaptcha']['recaptcha']*/) {
                    if (!$wpsf_recaptcha_enabled) {
                        ?>
                        <p id="wpsf_p" style="clear:both;"></p>
                        <script type="text/javascript">
                            window.wpsf_recaptcha_enabled = true;
                            window.captcha_site_key = "<?php echo $this->modules['WPSF_Settings']->settings['recaptcha']['captcha_site_key']; ?>";
                        </script>
                        <noscript><?php echo __('Please enable javascript in order to be allowed to comment', 'wpsf_domain'); ?></noscript>
                        <?php
                        $wpsf_recaptcha_enabled = true;
                    }
                }
            }
        }

        /**
         * Filter the validated user registration details.
         * This does not allow you to override the username or email of the user during registration. The values are solely used for validation and error handling.
         * It is called in multi site deployment and just forwards the call to registration_errors.
         *
         * @param $result
         * @return mixed
         */
        public function wpmu_validate_user_signup($result)
        {
            $result['errors'] = $this->registration_errors($result['errors'], $result['user_name'], $result['user_email']);
            return $result;
        }

        /**
         * Filter the errors encountered when a new user is being registered.
         * The filtered WP_Error object may, for example, contain errors for an invalid or existing username or email address. A WP_Error object should always returned, but may or may not contain errors.
         * If any errors are present in $errors, this will abort the user’s registration.
         * Implements the same logic as pre_comment_approved but for the registration form.
         *
         * @param $errors
         * @param $sanitized_user_login
         * @param $user_email
         * @return mixed
         */
        public function registration_errors($errors, $sanitized_user_login, $user_email)
        {
            if (/*$this->modules['WPSF_Settings']->settings['others']['registration']*/1) {
                if ((1/*$this->modules['WPSF_Settings']->settings['honeypot']['honeypot']*/)
                    && (!empty($_POST['more_comment']))
                ) {
                    $errors->add('spam_error', __('<strong>ERROR</strong>: There was a problem processing your registration.', 'wpsf_domain'));
                } elseif (($this->modules['WPSF_Settings']->settings['others']['avatar'])
                    && !$this->check_avatar($user_email)
                ) {
                    $errors->add('spam_error', __('<strong>ERROR</strong>: There was a problem processing your registration.', 'wpsf_domain'));
                } elseif (($this->modules['WPSF_Settings']->settings['recaptcha']['recaptcha'])
                    && $this->verify_comment_captcha("verify_comment_captcha") != "verify_comment_captcha"
                ) {
                    $errors->add('spam_error', __('<strong>ERROR</strong>: There was a problem processing your registration.', 'wpsf_domain'));
                } elseif (($this->modules['WPSF_Settings']->settings['others']['not_a_spammer'])
                    && (empty($_POST["wpsf_not_a_spammer"]))
                ) {
                    $errors->add('spam_error', __('<strong>ERROR</strong>: There was a problem processing your registration.', 'wpsf_domain'));
                } elseif (($this->modules['WPSF_Settings']->settings['others']['javascript'])
                    && (empty($_POST["wpsf_javascript"]) || $_POST["wpsf_javascript"] != "WPSF_JAVASCRIPT_TOKEN")
                ) {
                    $errors->add('spam_error', __('<strong>ERROR</strong>: There was a problem processing your registration.', 'wpsf_domain'));
                }
            }
            return $errors;
        }

        /**
         * Checks whether an avata is available on www.gravator.com for this email address.
         *
         * @param $email
         * @return bool
         */
        public function check_avatar($email)
        {
            $headers = get_headers("http://www.gravatar.com/avatar/" . md5(strtolower($email)) . "?d=404", 1);
            if (strpos($headers[0], '404')) {
                return false;
            }
            return true;
        }
    }

    ; // end WPFresh5Antispam
}

add_action('init', 'wpfresh5_check_antispam' );

function wpfresh5_check_antispam() {
    $block_spam = wpfresh5_get_settings( array( 'security', 'hacker', 'block_spam' ) );
    if( $block_spam ) {
        if (wpfresh5_requirements_met()) {

            if (class_exists('WPFresh5Antispam')) {
                $wpfresh5_antispam = /*$GLOBALS['wpfresh5']*/ WPFresh5Antispam::get_instance();
                // register_activation_hook(__FILE__, array($GLOBALS['wpsf'], 'activate'));
                // register_deactivation_hook(__FILE__, array($GLOBALS['wpsf'], 'deactivate'));
            }
        } /*else {
            add_action('admin_notices', 'wpsf_requirements_error');
        }*/
    }
}


if (!defined('ABSPATH')) {
    die('Access denied.');
}

/**
 *
 */
/**
 *
 */
define('WPF5_REQUIRED_PHP_VERSION', '5.3'); // because of get_called_class()
/**
 *
 */
define('WPF5_REQUIRED_WP_VERSION', '3.1'); // because of esc_textarea()

/**
 * Checks if the system requirements are met
 *
 * @return bool True if system requirements are met, false if not
 */
function wpfresh5_requirements_met()
{
    global $wp_version;

    if (version_compare(PHP_VERSION, WPF5_REQUIRED_PHP_VERSION, '<')) {
        return false;
    }

    if (version_compare($wp_version, WPF5_REQUIRED_WP_VERSION, '<')) {
        return false;
    }

    return true;
}

/**
 * Prints an error that the system requirements weren't met.
 */
/*function wpfresh5_requirements_error()
{
    global $wp_version;

    // require_once(dirname(__FILE__) . '/views/requirements-error.php');
}
*/
/*
 * Check requirements and load main class
 * The main program needs to be in a separate file that only gets loaded if the plugin requirements are met. Otherwise older PHP installations could crash when trying to parse it.
 */


?>