Skip to content

Commit 758ce7c

Browse files
committed
Add Basic spam protection.
1 parent 19042a6 commit 758ce7c

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

assets/js/mailchimp.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
// Change our submit type from HTML (default) to JS
3232
$('#mc_submit_type').val('js');
3333

34+
// Remove the no JS field.
35+
$('.mailchimp_sf_no_js').remove();
36+
3437
// Attach our form submitter action
3538
$('#mc_signup_form').ajaxForm({
3639
url: window.mailchimpSF.ajax_url,

includes/blocks/mailchimp/markup.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ function ( $single_list ) {
256256
</div><!-- /mc-indicates-required -->
257257
<?php
258258
}
259+
260+
// Add a honeypot field.
261+
mailchimp_sf_honeypot_field();
259262
?>
260263
<div class="mc_signup_submit">
261264
<input type="submit" name="mc_signup_submit" id="mc_signup_submit" value="<?php echo esc_attr( $submit_text ); ?>" class="button" />

includes/class-mailchimp-form-submission.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ public function request_handler() {
8282
* @return string|WP_Error Success message or error.
8383
*/
8484
public function handle_form_submission() {
85+
$is_valid = $this->validate_form_submission();
86+
if ( is_wp_error( $is_valid ) || ! $is_valid ) {
87+
if ( is_wp_error( $is_valid ) ) {
88+
return $is_valid;
89+
}
90+
91+
// If the form submission is invalid, return an error.
92+
return new WP_Error( 'mailchimp-invalid-form', esc_html__( 'Invalid form submission.', 'mailchimp' ) );
93+
}
94+
8595
$list_id = get_option( 'mc_list_id' );
8696
$update_existing = get_option( 'mc_update_existing' );
8797
$double_opt_in = get_option( 'mc_double_optin' );
@@ -466,4 +476,37 @@ public function remove_empty_merge_fields( $merge ) {
466476

467477
return $merge;
468478
}
479+
480+
/**
481+
* Validate the form submission.
482+
* Basic checks for the prevention of spam.
483+
*
484+
* @return bool|WP_Error True if valid, WP_Error if invalid.
485+
*/
486+
protected function validate_form_submission() {
487+
$spam_message = esc_html__( "We couldn't process your submission as it was flagged as potential spam. Please try again.", 'mailchimp' );
488+
// Make sure the honeypot field is set, but not filled (if it is, then it's a spam).
489+
if ( ! isset( $_POST['mailchimp_sf_alt_email'] ) || ! empty( $_POST['mailchimp_sf_alt_email'] ) ) {
490+
return new WP_Error( 'spam', $spam_message );
491+
}
492+
493+
// Make sure that no-js field is not present (if it is, then it's a spam).
494+
if ( isset( $_POST['mailchimp_sf_no_js'] ) ) {
495+
return new WP_Error( 'spam', $spam_message );
496+
}
497+
498+
// Make sure that user-agent is set and it has reasonable length.
499+
$user_agent = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
500+
if ( strlen( $user_agent ) < 2 ) {
501+
return new WP_Error( 'spam', $spam_message );
502+
}
503+
504+
/**
505+
* Filter to allow for custom validation of the form submission.
506+
*
507+
* @since x.x.x
508+
* @param bool $is_valid True if valid, false if invalid, return WP_Error to provide error message.
509+
*/
510+
return apply_filters( 'mailchimp_sf_form_submission_validation', true );
511+
}
469512
}

mailchimp_widget.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,10 @@ function mailchimp_sf_signup_form( $args = array() ) {
266266
<?php
267267
}
268268

269-
$submit_text = get_option( 'mc_submit_text' );
269+
// Add a honeypot field.
270+
mailchimp_sf_honeypot_field();
270271

272+
$submit_text = get_option( 'mc_submit_text' );
271273
?>
272274

273275
<div class="mc_signup_submit">
@@ -300,6 +302,21 @@ function mailchimp_sf_signup_form( $args = array() ) {
300302
}
301303
}
302304

305+
/**
306+
* Add a hidden honeypot field
307+
*
308+
* @return void
309+
*/
310+
function mailchimp_sf_honeypot_field() {
311+
?>
312+
<div style="display: none; !important">
313+
<label for="mailchimp_sf_alt_email"><?php esc_html_e( 'Alternative Email:', 'mailchimp' ); ?></label>
314+
<input type="text" name="mailchimp_sf_alt_email" autocomplete="off"/>
315+
</div>
316+
<input type="hidden" class="mailchimp_sf_no_js" name="mailchimp_sf_no_js" value="1" />
317+
<?php
318+
}
319+
303320
/**
304321
* Generate and display markup for Interest Groups
305322
*

0 commit comments

Comments
 (0)