|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Wiz // Gravity Forms // Enable Honeypot on All Forms |
| 4 | + * http://gravitywiz.com/ |
| 5 | + * |
| 6 | + * Enable Gravity Forms' honeypot functionality on all forms. By default, entries will be sent to spam. Additionally, |
| 7 | + * if users are logged in, the honeypot will not be enabled (unless already enabled on form). |
| 8 | + * |
| 9 | + * Instructions: |
| 10 | + * |
| 11 | + * 1. Install the snippet. |
| 12 | + * https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ |
| 13 | + * 2. Customize instantiation of class if you'd like to abort submission instead of sending to spam and/or exclude |
| 14 | + * specific forms. |
| 15 | + */ |
| 16 | +class GWiz_Global_Honeypot { |
| 17 | + protected $excluded_form_ids = array(); |
| 18 | + |
| 19 | + protected $honeypot_action = true; |
| 20 | + |
| 21 | + public function __construct( $excluded_form_ids = array(), $honeypot_action = 'spam' ) { |
| 22 | + add_filter( 'gform_form_post_get_meta', array( $this, 'enable_honeypot' ) ); |
| 23 | + |
| 24 | + $this->excluded_form_ids = $excluded_form_ids; |
| 25 | + $this->honeypot_action = $honeypot_action; |
| 26 | + } |
| 27 | + |
| 28 | + public function is_excluded_form( $form ) { |
| 29 | + return in_array( $form['id'], $this->excluded_form_ids, false ); |
| 30 | + } |
| 31 | + |
| 32 | + public function enable_honeypot( $form ) { |
| 33 | + if ( rgar( $form, 'enableHoneypot' ) ) { |
| 34 | + return $form; |
| 35 | + } |
| 36 | + |
| 37 | + if ( |
| 38 | + is_user_logged_in() |
| 39 | + || ! rgar( $form, 'id' ) |
| 40 | + || is_admin() |
| 41 | + || $this->is_excluded_form( $form ) |
| 42 | + ) { |
| 43 | + return $form; |
| 44 | + } |
| 45 | + |
| 46 | + $form['enableHoneypot'] = true; |
| 47 | + $form['honeypotAction'] = $this->honeypot_action; |
| 48 | + |
| 49 | + return $form; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +// Initialize the global honeypot with default settings (no form exclusions, action set to 'spam'). |
| 54 | +new GWiz_Global_Honeypot(); |
| 55 | + |
| 56 | +// Advanced usage: |
| 57 | +// Excludes specific form IDs and sets the honeypot action to 'abort'. |
| 58 | +// new GWiz_Global_Honeypot( array( 1, 2, 3 ), 'abort' ); |
0 commit comments