-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpforms-honeypot.php
77 lines (63 loc) · 2.14 KB
/
wpforms-honeypot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/**
* Plugin Name: WPForms Honeypot
* Description: Allows you to add a custom honeypot field to WPForms to help combat spam.
* Version: 1.0.0
* Author: Eric Mathison
* Text Domain: wpforms-honeypot
* Domain Path: /languages
* License: GPL-2.0-or-later
* License URI: http://www.gnu.org/licenses/gpl-2.0.html
* GitHub Plugin URI: eric-mathison/wpforms-honeypot
*
* @package WPForms_Honeypot
* @since 1.0.0
* @copyright Copyright (c) 2021, Eric Mathison
* @license GPL-2.0+
*/
// Exit if accessed directly.
if ( !defined( 'ABSPATH' ) ) {
exit;
}
// Definitons.
define( 'WPFORMS_HONEYPOT_VERSION' , '1.0.0' );
define( 'WPFORMS_HONEYPOT_CLASS', 'wpfhp-field' );
/**
* Load plugin files.
*/
function wpforms_honeypot_load_text_domain() {
load_plugin_textdomain( 'wpforms-honeypot', false, dirname( plugin_basename( __FILE__ ) ) .'/languages/' );
}
add_action( 'plugins_loaded', 'wpforms_honeypot_load_text_domain' );
// Load CSS file
function wpforms_honeypot_enqueue_css() {
wp_enqueue_style( 'wpforms-honeypot-css', plugins_url( '/includes/css/wpforms-honeypot.css', __FILE__ ), false, WPFORMS_HONEYPOT_VERSION, 'all' );
}
add_action( 'wp_enqueue_scripts', 'wpforms_honeypot_enqueue_css' );
// Build custom honeypot
function wpforms_honeypot_field( $honeypot, $fields, $entry, $form_data ) {
$honeypot_class = WPFORMS_HONEYPOT_CLASS;
$honeypot_field = false;
foreach( $form_data['fields'] as $form_field ) {
if ( false !== strpos( $form_field['css'], $honeypot_class ) ) {
$honeypot_field = absint( $form_field['id'] );
}
}
if ( !empty( $entry['fields'][$honeypot_field] ) ) {
$honeypot = 'WPForms Honeypot';
}
return $honeypot;
}
add_filter( 'wpforms_process_honeypot', 'wpforms_honeypot_field', 10, 4 );
// Setup Optional Logging
add_action ( 'init', function() {
$debug = get_option( 'wpforms_logging' );
if ( empty( $debug ) || !in_array( 'spam', $debug ) ) {
update_option( 'wpforms_logging', [ 'spam' ] );
}
});
add_filter( 'wpforms_log_cpt', function( $args ) {
$args['show_ui'] = true;
unset( $args['capability_type'] );
return $args;
});