PluginProbe ʕ •ᴥ•ʔ
Gravity Forms Zero Spam / 1.4
Gravity Forms Zero Spam v1.4
1.9.0 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.6.1 1.0.7 1.1 1.1.1 1.1.2 1.1.3 1.2 1.2.0.1 1.2.1 1.2.2 1.2.3 1.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.5.0 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.8.0
gravity-forms-zero-spam / gravityforms-zero-spam.php
gravity-forms-zero-spam Last commit date
LICENSE 3 years ago gravityforms-zero-spam-form-settings.php 3 years ago gravityforms-zero-spam.php 3 years ago readme.txt 3 years ago
gravityforms-zero-spam.php
185 lines
1 <?php
2 /**
3 * Plugin Name: Gravity Forms Zero Spam
4 * Plugin URI: https://www.gravitykit.com?utm_source=plugin&utm_campaign=zero-spam&utm_content=pluginuri
5 * Description: Enhance Gravity Forms to include effective anti-spam measures—without using a CAPTCHA.
6 * Version: 1.4
7 * Author: GravityKit
8 * Author URI: https://www.gravitykit.com?utm_source=plugin&utm_campaign=zero-spam&utm_content=authoruri
9 * License: GPL-2.0+
10 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11 */
12
13 // my mother always said to use things as they're intended or not at all
14 if ( ! defined( 'WPINC' ) ) {
15 die;
16 }
17
18 define( 'GF_ZERO_SPAM_BASENAME', plugin_basename( __FILE__ ) );
19
20 // clean up after ourselves
21 register_deactivation_hook( __FILE__, array( 'GF_Zero_Spam', 'deactivate' ) );
22
23 // Fire it up
24 add_action( 'gform_loaded', array( 'GF_Zero_Spam', 'gform_loaded' ) );
25
26 class GF_Zero_Spam {
27
28 /**
29 * Instantiate the plugin on Gravity Forms loading
30 */
31 public static function gform_loaded() {
32
33 include_once plugin_dir_path( __FILE__ ) . 'gravityforms-zero-spam-form-settings.php';
34
35 new self;
36 }
37
38 /**
39 * Cleans up plugin options when deactivating.
40 *
41 * @return void
42 */
43 public static function deactivate() {
44 delete_option( 'gf_zero_spam_key' );
45
46 if ( class_exists( 'GF_Zero_Spam_AddOn' ) ) {
47 wp_clear_scheduled_hook( GF_Zero_Spam_AddOn::REPORT_CRON_HOOK_NAME );
48 }
49 }
50
51 public function __construct() {
52 add_action( 'gform_register_init_scripts', array( $this, 'add_key_field' ), 9999 );
53 add_filter( 'gform_entry_is_spam', array( $this, 'check_key_field' ), 10, 3 );
54 }
55
56 /**
57 * Retrieves the zero spam key (generating if needed)
58 *
59 * @return false|mixed|void
60 */
61 public function get_key() {
62
63 $key = get_option( 'gf_zero_spam_key' );
64
65 if ( ! $key ) {
66 $key = wp_generate_password( 64, false, false );
67 update_option( 'gf_zero_spam_key', $key, false );
68 }
69
70 return $key;
71 }
72
73 /**
74 * Injects the hidden field and key into the form at submission
75 *
76 * @uses GFFormDisplay::add_init_script() to inject the code into the `gform_post_render` jQuery hook.
77 *
78 * @param array $form The Form Object
79 *
80 * @return void
81 */
82 public function add_key_field( $form ) {
83
84 /**
85 * Allows the zero spam key field to be disabled by returning false.
86 * @since 1.4
87 * @param bool $add_key_field Whether to add the key field to the form. Default true.
88 */
89 $add_key_field = apply_filters( 'gf_zero_spam_add_key_field', true );
90
91 if ( ! $add_key_field ) {
92 return;
93 }
94
95 $spam_key = esc_js( $this->get_key() );
96
97 $autocomplete = RGFormsModel::is_html5_enabled() ? ".attr( 'autocomplete', 'new-password' )\n\t\t" : '';
98
99 $script = <<<EOD
100 jQuery( document ).on( 'submit.gravityforms', '.gform_wrapper form', function( event ) {
101 jQuery( '<input>' )
102 .attr( 'type', 'hidden' )
103 .attr( 'name', 'gf_zero_spam_key' )
104 .attr( 'value', '{$spam_key}' )
105 $autocomplete.appendTo( jQuery( this ) );
106 } );
107 EOD;
108
109 GFFormDisplay::add_init_script( $form['id'], 'gf-zero-spam', GFFormDisplay::ON_PAGE_RENDER, $script );
110 }
111
112 /**
113 * Checks for our zero spam key during validation
114 *
115 * @param bool $is_spam Indicates if the submission has been flagged as spam.
116 * @param array $form The form currently being processed.
117 * @param array $entry The entry currently being processed.
118 *
119 * @return bool True: it's spam; False: it's not spam!
120 */
121 public function check_key_field( $is_spam = false, $form = array(), $entry = array() ) {
122
123 $should_check_key_field = ! GFCommon::is_preview();
124
125 /**
126 * Modify whether to process this entry submission for spam.
127 *
128 * @since 1.2
129 *
130 * @param bool $should_check_key_field Whether the Zero Spam plugin should check for the existence and validity of the key field. Default: true.
131 * @param array $form The form currently being processed.
132 * @param array $entry The entry currently being processed.
133 */
134 $should_check_key_field = gf_apply_filters( 'gf_zero_spam_check_key_field', rgar( $form, 'id' ), $should_check_key_field, $form, $entry );
135
136 if( false === $should_check_key_field ) {
137 return $is_spam;
138 }
139
140 $supports_context = method_exists( 'GFFormDisplay', 'get_submission_context' );
141 if ( $supports_context && GFFormDisplay::get_submission_context() !== 'form-submit' ) {
142 return $is_spam;
143 }
144
145 // This was not submitted using a web form; created using API
146 if ( ! $supports_context && ! did_action( 'gform_pre_submission' ) ) {
147 return $is_spam;
148 }
149
150 // Created using REST API or GFAPI
151 if ( isset( $entry['user_agent'] ) && 'API' === $entry['user_agent'] ) {
152 return $is_spam;
153 }
154
155 if ( ! isset( $_POST['gf_zero_spam_key'] ) || html_entity_decode( $_POST['gf_zero_spam_key'] ) !== $this->get_key() ) {
156 add_action( 'gform_entry_created', array( $this, 'add_entry_note' ) );
157
158 return true;
159 }
160
161 return $is_spam;
162 }
163
164 /**
165 * Adds a note to the entry once the spam status is set (GF 2.4.18+).
166 *
167 * @since 1.1.3
168 *
169 * @param array $entry The entry data.
170 */
171 public function add_entry_note( $entry ) {
172
173 if ( 'spam' !== rgar( $entry, 'status' ) ) {
174 return;
175 }
176
177 if ( ! method_exists( 'GFAPI', 'add_note' ) ) {
178 return;
179 }
180
181 GFAPI::add_note( $entry['id'], 0, 'Zero Spam', __( 'This entry has been marked as spam.', 'gf-zero-spam' ), 'gf-zero-spam', 'success' );
182 }
183
184 }
185