Skip to content

Commit 261c798

Browse files
allow specifying more detailed error keys by using a dot in the error key. example: spam.honeypot
1 parent c3bca2c commit 261c798

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

includes/forms/class-form.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,14 @@ public function validate()
392392

393393
// perform some basic anti-spam checks
394394
// User-Agent header should be set and not bot-like
395-
if (empty($_SERVER['HTTP_USER_AGENT']) || preg_match('/bot|crawl|spider|seo|lighthouse|facebookexternalhit|preview/i', $_SERVER['HTTP_USER_AGENT'])) {
396-
$errors[] = 'spam';
395+
if (empty($_SERVER['HTTP_USER_AGENT']) || preg_match('/bot|crawl|spider|seo|lighthouse|facebookexternalhit|preview/', strtolower($_SERVER['HTTP_USER_AGENT']))) {
396+
$errors[] = 'spam.user_agent';
397397
// _mc4wp_timestamp field should be between 30 days ago (to deal with aggressively cached pages) and 2 seconds ago
398-
} elseif (! isset($this->raw_data['_mc4wp_timestamp']) || $this->raw_data['_mc4wp_timestamp'] < (time() - DAY_IN_SECONDS * 30) || $this->raw_data['_mc4wp_timestamp'] > ( time() - 2 )) {
399-
$errors[] = 'spam';
398+
} elseif (! isset($this->raw_data['_mc4wp_timestamp']) || $this->raw_data['_mc4wp_timestamp'] < (time() - DAY_IN_SECONDS * 90) || $this->raw_data['_mc4wp_timestamp'] > ( time() - 2 )) {
399+
$errors[] = 'spam.timestamp';
400400
// _mc4wp_honeypot field should be submitted and empty
401401
} elseif (! isset($this->raw_data['_mc4wp_honeypot']) || '' !== $this->raw_data['_mc4wp_honeypot']) {
402-
$errors[] = 'spam';
402+
$errors[] = 'spam.honeypot';
403403
}
404404

405405
if (empty($errors)) {
@@ -746,7 +746,18 @@ public function get_stylesheet()
746746
*/
747747
public function get_message($key)
748748
{
749-
$message = isset($this->messages[ $key ]) ? $this->messages[ $key ] : $this->messages['error'];
749+
// default to generic error message
750+
$message = $this->messages['error'];
751+
752+
// if error key contains a dot, use only part before the dot (example: spam.honeypot)
753+
if (($dot_pos = strpos($key, '.')) !== false) {
754+
$key = substr($key, 0, $dot_pos);
755+
}
756+
757+
// if a more specific message exists for this error key, use that
758+
if (isset($this->messages[$key])) {
759+
$message = $this->messages[$key];
760+
}
750761

751762
if ($key === 'no_lists_selected' && current_user_can('manage_options')) {
752763
$message .= sprintf(' (<a href="%s">%s</a>)', mc4wp_get_edit_form_url($this->ID, 'settings'), 'edit form settings');

0 commit comments

Comments
 (0)