PluginProbe ʕ •ᴥ•ʔ
Gravity Forms Zero Spam / 1.3
Gravity Forms Zero Spam v1.3
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
170 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.3
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
47 public function __construct() {
48 add_action( 'gform_register_init_scripts', array( $this, 'add_key_field' ), 9999 );
49 add_filter( 'gform_entry_is_spam', array( $this, 'check_key_field' ), 10, 3 );
50 }
51
52 /**
53 * Retrieves the zero spam key (generating if needed)
54 *
55 * @return false|mixed|void
56 */
57 public function get_key() {
58
59 $key = get_option( 'gf_zero_spam_key' );
60
61 if ( ! $key ) {
62 $key = wp_generate_password( 64, false, false );
63 update_option( 'gf_zero_spam_key', $key, false );
64 }
65
66 return $key;
67 }
68
69 /**
70 * Injects the hidden field and key into the form at submission
71 *
72 * @uses GFFormDisplay::add_init_script() to inject the code into the `gform_post_render` jQuery hook.
73 *
74 * @param array $form The Form Object
75 *
76 * @return void
77 */
78 public function add_key_field( $form ) {
79
80 $spam_key = esc_js( $this->get_key() );
81
82 $autocomplete = RGFormsModel::is_html5_enabled() ? ".attr( 'autocomplete', 'new-password' )\n\t\t" : '';
83
84 $script = <<<EOD
85 jQuery( document ).on( 'submit.gravityforms', '.gform_wrapper form', function( event ) {
86 jQuery( '<input>' )
87 .attr( 'type', 'hidden' )
88 .attr( 'name', 'gf_zero_spam_key' )
89 .attr( 'value', '{$spam_key}' )
90 $autocomplete.appendTo( jQuery( this ) );
91 } );
92 EOD;
93
94 GFFormDisplay::add_init_script( $form['id'], 'gf-zero-spam', GFFormDisplay::ON_PAGE_RENDER, $script );
95 }
96
97 /**
98 * Checks for our zero spam key during validation
99 *
100 * @param bool $is_spam Indicates if the submission has been flagged as spam.
101 * @param array $form The form currently being processed.
102 * @param array $entry The entry currently being processed.
103 *
104 * @return bool True: it's spam; False: it's not spam!
105 */
106 public function check_key_field( $is_spam = false, $form = array(), $entry = array() ) {
107
108 $should_check_key_field = ! GFCommon::is_preview();
109
110 /**
111 * Modify whether to process this entry submission for spam.
112 *
113 * @since 1.2
114 *
115 * @param bool $should_check_key_field Whether the Zero Spam plugin should check for the existence and validity of the key field. Default: true.
116 * @param array $form The form currently being processed.
117 * @param array $entry The entry currently being processed.
118 */
119 $should_check_key_field = gf_apply_filters( 'gf_zero_spam_check_key_field', rgar( $form, 'id' ), $should_check_key_field, $form, $entry );
120
121 if( false === $should_check_key_field ) {
122 return $is_spam;
123 }
124
125 $supports_context = method_exists( 'GFFormDisplay', 'get_submission_context' );
126 if ( $supports_context && GFFormDisplay::get_submission_context() !== 'form-submit' ) {
127 return $is_spam;
128 }
129
130 // This was not submitted using a web form; created using API
131 if ( ! $supports_context && ! did_action( 'gform_pre_submission' ) ) {
132 return $is_spam;
133 }
134
135 // Created using REST API or GFAPI
136 if ( isset( $entry['user_agent'] ) && 'API' === $entry['user_agent'] ) {
137 return $is_spam;
138 }
139
140 if ( ! isset( $_POST['gf_zero_spam_key'] ) || html_entity_decode( $_POST['gf_zero_spam_key'] ) !== $this->get_key() ) {
141 add_action( 'gform_entry_created', array( $this, 'add_entry_note' ) );
142
143 return true;
144 }
145
146 return $is_spam;
147 }
148
149 /**
150 * Adds a note to the entry once the spam status is set (GF 2.4.18+).
151 *
152 * @since 1.1.3
153 *
154 * @param array $entry The entry data.
155 */
156 public function add_entry_note( $entry ) {
157
158 if ( 'spam' !== rgar( $entry, 'status' ) ) {
159 return;
160 }
161
162 if ( ! method_exists( 'GFAPI', 'add_note' ) ) {
163 return;
164 }
165
166 GFAPI::add_note( $entry['id'], 0, 'Zero Spam', __( 'This entry has been marked as spam.', 'gf-zero-spam' ), 'gf-zero-spam', 'success' );
167 }
168
169 }
170