1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-09 08:17:12 +02:00

Add @iamwebrocker PR #18 which adds honeypot option to comments form

This commit is contained in:
Ryan Cramer
2016-11-03 11:58:06 -04:00
parent 9b385168d0
commit 8405c586f0
2 changed files with 41 additions and 1 deletions

View File

@@ -110,6 +110,10 @@ class CommentForm extends Wire implements CommentFormInterface {
// to use it, YOU must set this with a <input hidden> field from your own javascript, somewhere in the form // to use it, YOU must set this with a <input hidden> field from your own javascript, somewhere in the form
'requireSecurityField' => '', 'requireSecurityField' => '',
// the name of a field that must NOT be set
// creates an input field that a (human) visitor should ignore, maybe hiding it with css is a good idea
'requireHoneypotField' => '',
// should a redirect be performed immediately after a comment is successfully posted? // should a redirect be performed immediately after a comment is successfully posted?
'redirectAfterPost' => null, // null=unset (must be set to true to enable) 'redirectAfterPost' => null, // null=unset (must be set to true to enable)
@@ -254,7 +258,10 @@ class CommentForm extends Wire implements CommentFormInterface {
$attrs = $options['attrs']; $attrs = $options['attrs'];
$id = $attrs['id']; $id = $attrs['id'];
$submitKey = $id . "_submit"; $submitKey = $id . "_submit";
$honeypot = $options['requireHoneypotField'];
$inputValues = array('cite' => '', 'email' => '', 'website' => '', 'stars' => '', 'text' => '', 'notify' => ''); $inputValues = array('cite' => '', 'email' => '', 'website' => '', 'stars' => '', 'text' => '', 'notify' => '');
if($honeypot) $inputValues[$honeypot] = '';
$user = $this->wire('user'); $user = $this->wire('user');
if($user->isLoggedin()) { if($user->isLoggedin()) {
@@ -366,6 +373,18 @@ class CommentForm extends Wire implements CommentFormInterface {
"\n\t</p>"; "\n\t</p>";
} }
// do we need to show the honeypot field?
$honeypot = $this->options['requireHoneypotField'];
if($honeypot) {
$honeypotLabel = isset($labels[$honeypot]) ? $labels[$honeypot] : '';
$honeypotValue = isset($inputValues[$honeypot]) ? $inputValues[$honeypot] : '';
$form .=
"\n\t<p class='CommentFormHP {$id}_hp'>" .
"\n\t\t<label for='{$id}_$honeypot'>$honeypotLabel</label>" .
"\n\t\t<input type='text' id='{$id}_$honeypot' name='$honeypot' value='$honeypotValue' size='3' />" .
"\n\t</p>";
}
$form .= $form .=
"\n\t<p class='CommentFormText {$id}_text'>" . "\n\t<p class='CommentFormText {$id}_text'>" .
"\n\t\t<label for='{$id}_text'>$labels[text]</label>" . "\n\t\t<label for='{$id}_text'>$labels[text]</label>" .
@@ -427,6 +446,19 @@ class CommentForm extends Wire implements CommentFormInterface {
"\n\t</p>"; "\n\t</p>";
} }
// do we need to show the honeypot field?
$honeypot = $this->options['requireHoneypotField'];
if($honeypot) {
$honeypotLabel = isset($labels[$honeypot]) ? $labels[$honeypot] : '';
$honeypotValue = isset($inputValues[$honeypot]) ? $inputValues[$honeypot] : '';
$form .=
"\n\t<p class='CommentFormHP {$id}_hp'>" .
"\n\t\t<label><span>$honeypotLabel</span>" .
"\n\t\t<input type='text' name='$honeypot' value='$honeypotValue' size='3' />" .
"\n\t\t</label>" .
"\n\t</p>";
}
$form .= $form .=
"\n\t<p class='CommentFormText {$id}_text'>" . "\n\t<p class='CommentFormText {$id}_text'>" .
"\n\t\t<label>" . "\n\t\t<label>" .
@@ -491,6 +523,10 @@ class CommentForm extends Wire implements CommentFormInterface {
if(empty($data[$key])) return false; if(empty($data[$key])) return false;
} }
if($key = $this->options['requireHoneypotField']) {
if(!empty($data[$key])) return false;
}
$comment = $this->wire(new Comment()); $comment = $this->wire(new Comment());
$comment->user_agent = $_SERVER['HTTP_USER_AGENT']; $comment->user_agent = $_SERVER['HTTP_USER_AGENT'];
$comment->ip = $this->wire('session')->getIP(); $comment->ip = $this->wire('session')->getIP();

View File

@@ -35,6 +35,10 @@
clear: both; clear: both;
} }
.CommentFormHP {
display: none;
}
@media only screen and (max-width: 767px) { @media only screen and (max-width: 767px) {
.CommentFormCite, .CommentFormCite,
.CommentFormEmail, .CommentFormEmail,