Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add honeypot for newsletter signup forms #164

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion wp-content/themes/citylimits/css/child-style.css

Large diffs are not rendered by default.

41 changes: 33 additions & 8 deletions wp-content/themes/citylimits/js/newsletter.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,21 @@ jQuery(document).ready(function($) {
}
}
})

// actions to take place once the form is submitted, such as
// displaying the success message
function form_submission_success(message) {

if ($('body').hasClass('newsletter-landing')) {
$('#main').html(message);
} else {
$('.newsletter-signup form, .newsletter-signup .not-expanded').hide();
$('.newsletter-response-content').html(message);
$('.newsletter-response').show();
$('.newsletter-signup.maincolumn').removeClass('open');
}

}


/*FORM SUBMIT*/
Expand All @@ -148,6 +163,23 @@ jQuery(document).ready(function($) {
e.preventDefault();

var $this = $(e.target);

// find our honeypot field
var honeypot_name = $this.find('input[name=name]').val();

// if our hidden honeypot field has a value, it's probably a bot filling out the form
// let's return a false success
if( honeypot_name ) {

Cookies.set('signed_up_for_newsletter', true, { expires: Infinity });
Cookies.set('newsletter_modal_snooze', true, { expires: Infinity });

form_submission_success( 'Submitted.' );

return;

}

$this.find('input[type=submit]').attr('disabled', true)
var email = $this.find('input[name=newsletter_email]').val()
var fname = $this.find('input[name=newsletter_fname]').val()
Expand Down Expand Up @@ -175,14 +207,7 @@ jQuery(document).ready(function($) {
Cookies.set('signed_up_for_newsletter', true, { expires: Infinity });
Cookies.set('newsletter_modal_snooze', true, { expires: Infinity });
}
if ($('body').hasClass('newsletter-landing')) {
$('#main').html(response.message)
} else {
$('.newsletter-signup form, .newsletter-signup .not-expanded').hide()
$('.newsletter-response-content').html(response.message)
$('.newsletter-response').show()
$('.newsletter-signup.maincolumn').removeClass('open')
}
form_submission_success( response.message );
}
})
})
Expand Down
9 changes: 9 additions & 0 deletions wp-content/themes/citylimits/less/newsletters.less
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@
margin-bottom: 10px;
padding: 4px 16px;
}
.uhohhoneypot {
opacity: 0;
position: absolute;
top: 0;
left: 0;
height: 0;
width: 0;
z-index: -1;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Largo has a .visuallyhidden class that can be used here instead of adding our own.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benlk I figured Largo would have something like that, but wasn't sure if we should be concerned about using a class name with hidden in the name since I'm not sure if more advanced bots could detect that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd go with whatever Mailchimp provides in its default form markup, since that's probably backed by a lot of research.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e19e508 switches the fields to have the .visuallyhidden class

}

@media (min-width: 769px) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@
<input type="text" name="newsletter_fname" placeholder="First Name" required>
<!--<input type="text" name="newsletter_lname" placeholder="Last Name" required>-->
<input type="email" name="newsletter_email" placeholder="Email" required>
<!-- H o n e y p o t -->
<label class="uhohhoneypot" for="name"></label>
<input class="uhohhoneypot" autocomplete="off" type="text" id="name" name="name" placeholder="Your name here">
<!-- End H o n e y p o t -->
<input type="submit" class="subscribe_button" value="Sign Up">
</form>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
<input type="text" name="newsletter_fname" placeholder="<?php esc_attr_e( 'First Name', 'citylimits' ); ?>" required>
<label for="newsletter_fname"><?php esc_html_e( 'Email address', 'citylimits' ); ?></label>
<input type="email" name="newsletter_email" placeholder="<?php esc_attr_e( 'Email address', 'citylimits' ); ?>" required>
<!-- H o n e y p o t -->
<label class="uhohhoneypot" for="name"></label>
<input class="uhohhoneypot" autocomplete="off" type="text" id="name" name="name" placeholder="Your name here">
<!-- End H o n e y p o t -->
<input type="submit" class="btn btn-primary" value="Sign Up">
</div>
</div><!--.row-->
Expand Down