gravity-forms-zero-spam
Last commit date
LICENSE
5 years ago
gravityforms-zero-spam.php
5 years ago
readme.txt
5 years ago
gravityforms-zero-spam.php
121 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin Name: Gravity Forms Zero Spam |
| 4 | * Plugin URI: https://gravityview.co?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.1 |
| 7 | * Author: GravityView |
| 8 | * Author URI: https://gravityview.co?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 | // clean up after ourselves |
| 19 | register_deactivation_hook( __FILE__, array( 'GF_Zero_Spam', 'deactivate' ) ); |
| 20 | |
| 21 | // Fire it up |
| 22 | add_action( 'gform_loaded', array( 'GF_Zero_Spam', 'gform_loaded' ) ); |
| 23 | |
| 24 | class GF_Zero_Spam { |
| 25 | |
| 26 | /** |
| 27 | * Instantiate the plugin on Gravity Forms loading |
| 28 | */ |
| 29 | public static function gform_loaded() { |
| 30 | new self; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Cleans up plugin options when deactivating. |
| 35 | * |
| 36 | * @return void |
| 37 | */ |
| 38 | public static function deactivate() { |
| 39 | delete_option( 'gf_zero_spam_key' ); |
| 40 | } |
| 41 | |
| 42 | public function __construct() { |
| 43 | add_action( 'gform_register_init_scripts', array( $this, 'add_key_field' ), 9999 ); |
| 44 | add_filter( 'gform_entry_is_spam', array( $this, 'check_key_field' ), 10, 3 ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Retrieves the zero spam key (generating if needed) |
| 49 | * |
| 50 | * @return false|mixed|void |
| 51 | */ |
| 52 | public function get_key() { |
| 53 | |
| 54 | $key = get_option( 'gf_zero_spam_key' ); |
| 55 | |
| 56 | if ( ! $key ) { |
| 57 | $key = wp_generate_password( 64, false, false ); |
| 58 | update_option( 'gf_zero_spam_key', $key, false ); |
| 59 | } |
| 60 | |
| 61 | return $key; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Injects the hidden field and key into the form at submission |
| 66 | * |
| 67 | * @uses GFFormDisplay::add_init_script() to inject the code into the `gform_post_render` jQuery hook. |
| 68 | * |
| 69 | * @param array $form The Form Object |
| 70 | * |
| 71 | * @return void |
| 72 | */ |
| 73 | public function add_key_field( $form ) { |
| 74 | |
| 75 | $spam_key = esc_js( $this->get_key() ); |
| 76 | |
| 77 | $script = <<<EOD |
| 78 | jQuery( document ).on( 'submit.gravityforms', '.gform_wrapper form', function( event ) { |
| 79 | jQuery( '<input>' ).attr( 'type', 'hidden' ) |
| 80 | .attr( 'name', 'gf_zero_spam_key' ) |
| 81 | .attr( 'value', '{$spam_key}' ) |
| 82 | .appendTo( jQuery( this ) ); |
| 83 | } ); |
| 84 | EOD; |
| 85 | |
| 86 | GFFormDisplay::add_init_script( $form['id'], 'placeholders', GFFormDisplay::ON_PAGE_RENDER, $script ); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Checks for our zero spam key during validation |
| 91 | * |
| 92 | * @param bool $is_spam Indicates if the submission has been flagged as spam. |
| 93 | * @param array $form The form currently being processed. |
| 94 | * @param array $entry The entry currently being processed. |
| 95 | * |
| 96 | * @return bool True: it's spam; False: it's not spam! |
| 97 | */ |
| 98 | public function check_key_field( $is_spam = false, $form = array(), $entry = array() ) { |
| 99 | |
| 100 | // This was not submitted using a web form; created using API |
| 101 | if ( ! did_action( 'gform_pre_submission' ) ) { |
| 102 | return $is_spam; |
| 103 | } |
| 104 | |
| 105 | // Created using REST API or GFAPI |
| 106 | if ( isset( $entry['user_agent'] ) && 'API' === $entry['user_agent'] ) { |
| 107 | return $is_spam; |
| 108 | } |
| 109 | |
| 110 | if ( ! isset( $_POST['gf_zero_spam_key'] ) ) { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | if ( $_POST['gf_zero_spam_key'] !== $this->get_key() ) { |
| 115 | return true; |
| 116 | } |
| 117 | |
| 118 | return $is_spam; |
| 119 | } |
| 120 | } |
| 121 |