Anti-spam - Version 2.3

Version Description

  • 2014-11-23 =
  • enqueue script only for pages with comments form and in the footer (thanks to dougvdotcom)
  • clean up code
Download this release

Release Info

Developer webvitaly
Plugin Icon 128x128 Anti-spam
Version 2.3
Comparing to
See all releases

Code changes from version 2.2 to 2.3

Files changed (3) hide show
  1. anti-spam.php +47 -59
  2. js/anti-spam.js +3 -3
  3. readme.txt +6 -2
anti-spam.php CHANGED
@@ -3,7 +3,7 @@
3
  Plugin Name: Anti-spam
4
  Plugin URI: http://wordpress.org/plugins/anti-spam/
5
  Description: No spam in comments. No captcha.
6
- Version: 2.2
7
  Author: webvitaly
8
  Author URI: http://web-profile.com.ua/wordpress/plugins/
9
  License: GPLv3
@@ -15,77 +15,73 @@ $antispam_allow_trackbacks = false; // if true, than trackbacks will be allowed
15
  // trackbacks almost not used by users, but mostly used by spammers; pingbacks are always enabled
16
  // more about the difference between trackback and pingback - http://web-profile.com.ua/web/trackback-vs-pingback/
17
 
18
- $antispam_version = '2.2';
19
 
20
 
21
- if ( ! function_exists( 'antispam_scripts_styles_init' ) ) :
22
- function antispam_scripts_styles_init() {
23
  global $antispam_version;
24
- if ( !is_admin() ) { // && is_singular() && comments_open() && get_option( 'thread_comments' )
25
- //wp_enqueue_script('jquery');
26
- wp_enqueue_script( 'anti-spam-script', plugins_url( '/js/anti-spam.js', __FILE__ ), array( 'jquery' ), $antispam_version );
27
  }
28
  }
29
- add_action('init', 'antispam_scripts_styles_init');
30
- endif; // end of antispam_scripts_styles_init()
31
 
32
 
33
- if ( ! function_exists( 'antispam_form_part' ) ) :
34
  function antispam_form_part() {
 
35
  if ( ! is_user_logged_in() ) { // add anti-spam fields only for not logged in users
36
- $antispam_form_part = '
37
- <p class="comment-form-ant-spm" style="clear:both;">
38
- <strong>Current ye@r</strong> <span class="required">*</span>
39
- <input type="hidden" name="ant-spm-a" id="ant-spm-a" value="'.date('Y').'" />
40
- <input type="text" name="ant-spm-q" id="ant-spm-q" size="30" value="2.2" />
41
- </p>
42
- '; // question (hidden with js) [required="required"]
43
- $antispam_form_part .= '
44
- <p class="comment-form-ant-spm-2" style="display:none;">
45
- <strong>Leave this field empty</strong>
46
- <input type="text" name="ant-spm-e-email-url" id="ant-spm-e-email-url" size="30" value=""/>
47
- </p>
48
- '; // empty field (hidden with css)
49
- echo $antispam_form_part;
50
  }
51
  }
52
- add_action( 'comment_form', 'antispam_form_part' ); // add anti-spam input to the comment form
53
  endif; // end of antispam_form_part()
54
 
55
 
56
- if ( ! function_exists( 'antispam_check_comment' ) ) :
57
- function antispam_check_comment( $commentdata ) {
58
  global $antispam_send_spam_comment_to_admin, $antispam_allow_trackbacks;
59
- extract( $commentdata );
60
 
61
  $antispam_pre_error_message = '<p><strong><a href="javascript:window.history.back()">Go back</a></strong> and try again.</p>';
62
  $antispam_error_message = '';
63
 
64
- if ( $antispam_send_spam_comment_to_admin ) { // if sending email to admin is enabled
65
  $antispam_admin_email = get_option('admin_email'); // admin email
66
 
67
- $post = get_post( $comment->comment_post_ID );
68
  $antispam_message_spam_info = 'Spam for post: "'.$post->post_title.'"' . "\r\n";
69
- $antispam_message_spam_info .= get_permalink( $comment->comment_post_ID ) . "\r\n\r\n";
70
 
71
  $antispam_message_spam_info .= 'IP : ' . $_SERVER['REMOTE_ADDR'] . "\r\n";
72
  $antispam_message_spam_info .= 'User agent : ' . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
73
  $antispam_message_spam_info .= 'Referer : ' . $_SERVER['HTTP_REFERER'] . "\r\n\r\n";
74
 
75
  $antispam_message_spam_info .= 'Comment data:'."\r\n"; // lets see what comment data spammers try to submit
76
- foreach ( $commentdata as $key => $value ) {
77
  $antispam_message_spam_info .= '$commentdata['.$key. '] = '.$value."\r\n"; // .chr(13).chr(10)
78
  }
79
  $antispam_message_spam_info .= "\r\n\r\n";
80
 
81
  $antispam_message_spam_info .= 'Post vars:'."\r\n"; // lets see what post vars spammers try to submit
82
- foreach ( $_POST as $key => $value ) {
83
  $antispam_message_spam_info .= '$_POST['.$key. '] = '.$value."\r\n"; // .chr(13).chr(10)
84
  }
85
  $antispam_message_spam_info .= "\r\n\r\n";
86
 
87
  $antispam_message_spam_info .= 'Cookie vars:'."\r\n"; // lets see what cookie vars spammers try to submit
88
- foreach ( $_COOKIE as $key => $value ) {
89
  $antispam_message_spam_info .= '$_COOKIE['.$key. '] = '.$value."\r\n"; // .chr(13).chr(10)
90
  }
91
  $antispam_message_spam_info .= "\r\n\r\n";
@@ -99,7 +95,7 @@ if ( ! function_exists( 'antispam_check_comment' ) ) :
99
  if ( ! is_user_logged_in() && $comment_type != 'pingback' && $comment_type != 'trackback' ) { // logged in user is not a spammer
100
  $spam_flag = false;
101
 
102
- if ( trim( $_POST['ant-spm-q'] ) != date('Y') ) { // answer is wrong - maybe spam
103
  $spam_flag = true;
104
  if ( empty( $_POST['ant-spm-q'] ) ) { // empty answer - maybe spam
105
  $antispam_error_message .= 'Error: empty answer. ['.$_POST['ant-spm-q'].']<br> ';
@@ -107,45 +103,37 @@ if ( ! function_exists( 'antispam_check_comment' ) ) :
107
  $antispam_error_message .= 'Error: answer is wrong. ['.$_POST['ant-spm-q'].']<br> ';
108
  }
109
  }
 
110
  if ( ! empty( $_POST['ant-spm-e-email-url'] ) ) { // field is not empty - maybe spam
111
  $spam_flag = true;
112
  $antispam_error_message .= 'Error: field should be empty. ['.$_POST['ant-spm-e-email-url'].']<br> ';
113
  }
 
114
  if ( $spam_flag ) { // if we have spam
115
  if ( $antispam_send_spam_comment_to_admin ) { // if sending email to admin is enabled
116
-
117
- $antispam_subject = 'Spam comment on site ['.get_bloginfo( 'name' ).']'; // email subject
118
  $antispam_message = '';
119
-
120
  $antispam_message .= $antispam_error_message . "\r\n\r\n";
121
-
122
  $antispam_message .= $antispam_message_spam_info; // spam comment, post, cookie and other data
123
-
124
  $antispam_message .= $antispam_message_append;
125
-
126
- @wp_mail( $antispam_admin_email, $antispam_subject, $antispam_message ); // send spam comment to admin email
127
  }
128
  wp_die( $antispam_pre_error_message . $antispam_error_message ); // die - do not send comment and show errors
129
  }
130
  }
131
 
132
  if ( ! $antispam_allow_trackbacks ) { // if trackbacks are blocked (pingbacks are alowed)
133
- if ( $comment_type == 'trackback' ) { // if trackbacks ( || $comment_type == 'pingback' )
134
  $antispam_error_message .= 'Error: trackbacks are disabled.<br> ';
135
  if ( $antispam_send_spam_comment_to_admin ) { // if sending email to admin is enabled
136
- $antispam_subject = 'Spam trackback on site ['.get_bloginfo( 'name' ).']'; // email subject
137
-
138
  $antispam_message = '';
139
-
140
  $antispam_message .= $antispam_error_message . "\r\n\r\n";
141
-
142
  $antispam_message .= $antispam_message_spam_info; // spam comment, post, cookie and other data
143
-
144
  $antispam_message .= $antispam_message_append;
145
-
146
- @wp_mail( $antispam_admin_email, $antispam_subject, $antispam_message ); // send trackback comment to admin email
147
  }
148
- wp_die( $antispam_pre_error_message . $antispam_error_message ); // die - do not send trackback
149
  }
150
  }
151
 
@@ -153,19 +141,19 @@ if ( ! function_exists( 'antispam_check_comment' ) ) :
153
  }
154
 
155
  if ( ! is_admin() ) {
156
- add_filter( 'preprocess_comment', 'antispam_check_comment', 1 );
157
  }
158
  endif; // end of antispam_check_comment()
159
 
160
 
161
- if ( ! function_exists( 'antispam_plugin_meta' ) ) :
162
- function antispam_plugin_meta( $links, $file ) { // add 'Plugin page' and 'Donate' links to plugin meta row
163
- if ( strpos( $file, 'anti-spam.php' ) !== false ) {
164
- $links = array_merge( $links, array( '<a href="http://web-profile.com.ua/wordpress/plugins/anti-spam/" title="Plugin page">Anti-spam</a>' ) );
165
- $links = array_merge( $links, array( '<a href="http://web-profile.com.ua/donate/" title="Support the development">Donate</a>' ) );
166
- $links = array_merge( $links, array( '<a href="http://codecanyon.net/item/antispam-pro/6491169?ref=webvitaly" title="Upgrade to Pro">Anti-spam Pro</a>' ) );
167
  }
168
  return $links;
169
  }
170
- add_filter( 'plugin_row_meta', 'antispam_plugin_meta', 10, 2 );
171
  endif; // end of antispam_plugin_meta()
3
  Plugin Name: Anti-spam
4
  Plugin URI: http://wordpress.org/plugins/anti-spam/
5
  Description: No spam in comments. No captcha.
6
+ Version: 2.3
7
  Author: webvitaly
8
  Author URI: http://web-profile.com.ua/wordpress/plugins/
9
  License: GPLv3
15
  // trackbacks almost not used by users, but mostly used by spammers; pingbacks are always enabled
16
  // more about the difference between trackback and pingback - http://web-profile.com.ua/web/trackback-vs-pingback/
17
 
18
+ $antispam_version = '2.3';
19
 
20
 
21
+ if ( ! function_exists('antispam_enqueue_script') ) :
22
+ function antispam_enqueue_script() {
23
  global $antispam_version;
24
+ if (is_singular() && comments_open() && get_option('thread_comments')) { // load script only for pages with comments form
25
+ wp_enqueue_script('anti-spam-script', plugins_url('/js/anti-spam.js', __FILE__), array('jquery'), $antispam_version, true);
 
26
  }
27
  }
28
+ add_action('wp_enqueue_scripts', 'antispam_enqueue_script');
29
+ endif; // end of antispam_enqueue_script()
30
 
31
 
32
+ if ( ! function_exists('antispam_form_part') ) :
33
  function antispam_form_part() {
34
+ global $antispam_version;
35
  if ( ! is_user_logged_in() ) { // add anti-spam fields only for not logged in users
36
+ echo '<p class="comment-form-ant-spm" style="clear:both;">
37
+ <strong>Current ye@r</strong> <span class="required">*</span>
38
+ <input type="hidden" name="ant-spm-a" id="ant-spm-a" value="'.date('Y').'" />
39
+ <input type="text" name="ant-spm-q" id="ant-spm-q" size="30" value="'.$antispam_version.'" />
40
+ </p>'; // question (hidden with js) [required="required"]
41
+
42
+ echo '<p class="comment-form-ant-spm-2" style="display:none;">
43
+ <strong>Leave this field empty</strong>
44
+ <input type="text" name="ant-spm-e-email-url" id="ant-spm-e-email-url" size="30" value="" />
45
+ </p>'; // empty field (hidden with css)
 
 
 
 
46
  }
47
  }
48
+ add_action('comment_form', 'antispam_form_part'); // add anti-spam input to the comment form
49
  endif; // end of antispam_form_part()
50
 
51
 
52
+ if ( ! function_exists('antispam_check_comment') ) :
53
+ function antispam_check_comment($commentdata) {
54
  global $antispam_send_spam_comment_to_admin, $antispam_allow_trackbacks;
55
+ extract($commentdata);
56
 
57
  $antispam_pre_error_message = '<p><strong><a href="javascript:window.history.back()">Go back</a></strong> and try again.</p>';
58
  $antispam_error_message = '';
59
 
60
+ if ($antispam_send_spam_comment_to_admin) { // if sending email to admin is enabled
61
  $antispam_admin_email = get_option('admin_email'); // admin email
62
 
63
+ $post = get_post($comment->comment_post_ID);
64
  $antispam_message_spam_info = 'Spam for post: "'.$post->post_title.'"' . "\r\n";
65
+ $antispam_message_spam_info .= get_permalink($comment->comment_post_ID) . "\r\n\r\n";
66
 
67
  $antispam_message_spam_info .= 'IP : ' . $_SERVER['REMOTE_ADDR'] . "\r\n";
68
  $antispam_message_spam_info .= 'User agent : ' . $_SERVER['HTTP_USER_AGENT'] . "\r\n";
69
  $antispam_message_spam_info .= 'Referer : ' . $_SERVER['HTTP_REFERER'] . "\r\n\r\n";
70
 
71
  $antispam_message_spam_info .= 'Comment data:'."\r\n"; // lets see what comment data spammers try to submit
72
+ foreach ($commentdata as $key => $value) {
73
  $antispam_message_spam_info .= '$commentdata['.$key. '] = '.$value."\r\n"; // .chr(13).chr(10)
74
  }
75
  $antispam_message_spam_info .= "\r\n\r\n";
76
 
77
  $antispam_message_spam_info .= 'Post vars:'."\r\n"; // lets see what post vars spammers try to submit
78
+ foreach ($_POST as $key => $value) {
79
  $antispam_message_spam_info .= '$_POST['.$key. '] = '.$value."\r\n"; // .chr(13).chr(10)
80
  }
81
  $antispam_message_spam_info .= "\r\n\r\n";
82
 
83
  $antispam_message_spam_info .= 'Cookie vars:'."\r\n"; // lets see what cookie vars spammers try to submit
84
+ foreach ($_COOKIE as $key => $value) {
85
  $antispam_message_spam_info .= '$_COOKIE['.$key. '] = '.$value."\r\n"; // .chr(13).chr(10)
86
  }
87
  $antispam_message_spam_info .= "\r\n\r\n";
95
  if ( ! is_user_logged_in() && $comment_type != 'pingback' && $comment_type != 'trackback' ) { // logged in user is not a spammer
96
  $spam_flag = false;
97
 
98
+ if ( array_key_exists('ant-spm-q', $_POST) && trim($_POST['ant-spm-q']) != date('Y') ) { // answer is wrong - maybe spam
99
  $spam_flag = true;
100
  if ( empty( $_POST['ant-spm-q'] ) ) { // empty answer - maybe spam
101
  $antispam_error_message .= 'Error: empty answer. ['.$_POST['ant-spm-q'].']<br> ';
103
  $antispam_error_message .= 'Error: answer is wrong. ['.$_POST['ant-spm-q'].']<br> ';
104
  }
105
  }
106
+
107
  if ( ! empty( $_POST['ant-spm-e-email-url'] ) ) { // field is not empty - maybe spam
108
  $spam_flag = true;
109
  $antispam_error_message .= 'Error: field should be empty. ['.$_POST['ant-spm-e-email-url'].']<br> ';
110
  }
111
+
112
  if ( $spam_flag ) { // if we have spam
113
  if ( $antispam_send_spam_comment_to_admin ) { // if sending email to admin is enabled
114
+ $antispam_subject = 'Spam comment on site ['.get_bloginfo('name').']'; // email subject
 
115
  $antispam_message = '';
 
116
  $antispam_message .= $antispam_error_message . "\r\n\r\n";
 
117
  $antispam_message .= $antispam_message_spam_info; // spam comment, post, cookie and other data
 
118
  $antispam_message .= $antispam_message_append;
119
+ @wp_mail($antispam_admin_email, $antispam_subject, $antispam_message); // send spam comment to admin email
 
120
  }
121
  wp_die( $antispam_pre_error_message . $antispam_error_message ); // die - do not send comment and show errors
122
  }
123
  }
124
 
125
  if ( ! $antispam_allow_trackbacks ) { // if trackbacks are blocked (pingbacks are alowed)
126
+ if ($comment_type == 'trackback') { // if trackbacks ( || $comment_type == 'pingback')
127
  $antispam_error_message .= 'Error: trackbacks are disabled.<br> ';
128
  if ( $antispam_send_spam_comment_to_admin ) { // if sending email to admin is enabled
129
+ $antispam_subject = 'Spam trackback on site ['.get_bloginfo('name').']'; // email subject
 
130
  $antispam_message = '';
 
131
  $antispam_message .= $antispam_error_message . "\r\n\r\n";
 
132
  $antispam_message .= $antispam_message_spam_info; // spam comment, post, cookie and other data
 
133
  $antispam_message .= $antispam_message_append;
134
+ @wp_mail($antispam_admin_email, $antispam_subject, $antispam_message); // send trackback comment to admin email
 
135
  }
136
+ wp_die($antispam_pre_error_message . $antispam_error_message); // die - do not send trackback
137
  }
138
  }
139
 
141
  }
142
 
143
  if ( ! is_admin() ) {
144
+ add_filter('preprocess_comment', 'antispam_check_comment', 1);
145
  }
146
  endif; // end of antispam_check_comment()
147
 
148
 
149
+ if ( ! function_exists('antispam_plugin_meta') ) :
150
+ function antispam_plugin_meta($links, $file) { // add some links to plugin meta row
151
+ if (strpos($file, 'anti-spam.php') !== false) {
152
+ $links = array_merge($links, array('<a href="http://web-profile.com.ua/wordpress/plugins/anti-spam/" title="Plugin page">Anti-spam</a>'));
153
+ $links = array_merge($links, array('<a href="http://web-profile.com.ua/donate/" title="Support the development">Donate</a>'));
154
+ $links = array_merge($links, array('<a href="http://codecanyon.net/item/antispam-pro/6491169?ref=webvitaly" title="Upgrade to Pro">Anti-spam Pro</a>'));
155
  }
156
  return $links;
157
  }
158
+ add_filter('plugin_row_meta', 'antispam_plugin_meta', 10, 2);
159
  endif; // end of antispam_plugin_meta()
js/anti-spam.js CHANGED
@@ -16,15 +16,15 @@ wordpress.org/plugins/anti-spam/
16
  var current_year = current_date.getFullYear();
17
 
18
  if ( $('#comments form input#ant-spm-q').length == 0 ) { // anti-spam input does not exist (could be because of cache or because theme does not use 'comment_form' action)
19
- $('#comments form').append('<input type="hidden" name="ant-spm-q" id="ant-spm-q" value="'+current_year+'" />'); // add whole input with answer via javascript to comment form
20
  }
21
 
22
  if ( $('#respond form input#ant-spm-q').length == 0 ) { // similar, just in case (used because user could bot have #comments)
23
- $('#respond form').append('<input type="hidden" name="ant-spm-q" id="ant-spm-q" value="'+current_year+'" />'); // add whole input with answer via javascript to comment form
24
  }
25
 
26
  if ( $('form#commentform input#ant-spm-q').length == 0 ) { // similar, just in case (used because user could bot have #respond)
27
- $('form#commentform').append('<input type="hidden" name="ant-spm-q" id="ant-spm-q" value="'+current_year+'" />'); // add whole input with answer via javascript to comment form
28
  }
29
  }
30
 
16
  var current_year = current_date.getFullYear();
17
 
18
  if ( $('#comments form input#ant-spm-q').length == 0 ) { // anti-spam input does not exist (could be because of cache or because theme does not use 'comment_form' action)
19
+ $('#comments form').append('<input type="hidden" name="ant-spm-q" id="ant-spm-q" value="'+current_year+'" />'); // add whole input with answer via js to comment form
20
  }
21
 
22
  if ( $('#respond form input#ant-spm-q').length == 0 ) { // similar, just in case (used because user could bot have #comments)
23
+ $('#respond form').append('<input type="hidden" name="ant-spm-q" id="ant-spm-q" value="'+current_year+'" />'); // add whole input with answer via js to comment form
24
  }
25
 
26
  if ( $('form#commentform input#ant-spm-q').length == 0 ) { // similar, just in case (used because user could bot have #respond)
27
+ $('form#commentform').append('<input type="hidden" name="ant-spm-q" id="ant-spm-q" value="'+current_year+'" />'); // add whole input with answer via js to comment form
28
  }
29
  }
30
 
readme.txt CHANGED
@@ -3,8 +3,8 @@ Contributors: webvitaly
3
  Donate link: http://web-profile.com.ua/donate/
4
  Tags: spam, spammer, spammers, comment, comments, antispam, anti-spam, block-spam, spamfree, spam-free, spambot, spam-bot, bot
5
  Requires at least: 3.0
6
- Tested up to: 3.9
7
- Stable tag: 2.2
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl.html
10
 
@@ -112,6 +112,10 @@ Do not order products from spam. If spam will be less effective than spammers wi
112
 
113
  == Changelog ==
114
 
 
 
 
 
115
  = 2.2 - 2014-08-03 =
116
  * clear value of the empty input because some themes are adding some value for all inputs
117
  * updated FAQ section
3
  Donate link: http://web-profile.com.ua/donate/
4
  Tags: spam, spammer, spammers, comment, comments, antispam, anti-spam, block-spam, spamfree, spam-free, spambot, spam-bot, bot
5
  Requires at least: 3.0
6
+ Tested up to: 4.0
7
+ Stable tag: 2.3
8
  License: GPLv3
9
  License URI: http://www.gnu.org/licenses/gpl.html
10
 
112
 
113
  == Changelog ==
114
 
115
+ = 2.3 - 2014-11-23 =
116
+ * enqueue script only for pages with comments form and in the footer (thanks to dougvdotcom)
117
+ * clean up code
118
+
119
  = 2.2 - 2014-08-03 =
120
  * clear value of the empty input because some themes are adding some value for all inputs
121
  * updated FAQ section