secret = wp_hash('honeypot_plugin_secret_v2'); // Field names look like legitimate form fields to trick bots $this->hp_name = 'website_url_confirm'; $this->token_name = 'form_session_id'; $this->time_name = 'form_render_ts'; add_action('init', [$this, 'init']); } /* ------------------------------------------------------------------ * INIT — register all hooks * ----------------------------------------------------------------*/ public function init() { if (is_admin()) { add_action('admin_notices', [$this, 'activation_notice']); return; } // --- Inject honeypot into forms --- // WordPress core add_filter('the_content', [$this, 'add_to_content_forms'], 99); add_filter('comment_form_defaults', [$this, 'add_to_comment_form_defaults']); add_action('comment_form_after_fields', [$this, 'echo_honeypot']); add_action('comment_form_logged_in_after', [$this, 'echo_honeypot']); // WooCommerce add_action('woocommerce_register_form', [$this, 'echo_honeypot']); add_action('woocommerce_login_form', [$this, 'echo_honeypot']); add_action('woocommerce_after_order_notes', [$this, 'echo_honeypot']); // WordPress registration add_action('register_form', [$this, 'echo_honeypot']); add_action('login_form', [$this, 'echo_honeypot']); // Elementor Pro forms add_action('elementor_pro/forms/render_field', [$this, 'add_to_elementor_form'], 10, 2); add_action('elementor/widget/render_content', [$this, 'filter_elementor_widget'], 10, 2); // Gravity Forms add_filter('gform_form_tag', [$this, 'add_to_gravity_forms'], 10, 2); // Contact Form 7 add_filter('wpcf7_form_elements', [$this, 'add_to_cf7']); // Generic form search add_filter('get_search_form', [$this, 'add_to_search_form'], 99); // --- Validate on POST --- // WooCommerce registration (proper hook) add_filter('woocommerce_process_registration_errors', [$this, 'validate_wc_registration'], 10, 4); // WooCommerce login add_filter('woocommerce_process_login_errors', [$this, 'validate_wc_login'], 10, 3); // WooCommerce checkout add_action('woocommerce_after_checkout_validation', [$this, 'validate_wc_checkout'], 10, 2); // WordPress core registration add_filter('registration_errors', [$this, 'validate_wp_registration'], 10, 3); // Comments add_filter('preprocess_comment', [$this, 'validate_comment']); // Elementor Pro forms add_action('elementor_pro/forms/validation', [$this, 'validate_elementor_form'], 10, 2); // Generic early POST check for other forms add_action('template_redirect', [$this, 'validate_generic_post']); // --- CSS & JS --- add_action('wp_head', [$this, 'print_css']); add_action('wp_footer', [$this, 'print_js'], 99); } /* ------------------------------------------------------------------ * HONEYPOT FIELD HTML * ----------------------------------------------------------------*/ private function get_honeypot_html() { $ts = time(); $token_data = $ts . '|' . wp_create_nonce('hp_form_' . $ts); // The wrapper uses a generic class name return sprintf( '
', esc_attr($this->hp_name), esc_attr($this->token_name), esc_attr($this->time_name), esc_attr($ts) ); } /** Echo the honeypot (for action hooks) */ public function echo_honeypot() { echo $this->get_honeypot_html(); } /* ------------------------------------------------------------------ * INJECTION HELPERS * ----------------------------------------------------------------*/ public function add_to_content_forms($content) { if (is_admin() || is_feed()) { return $content; } return preg_replace_callback( '/(