PageRenderTime 69ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/public/frontend/wp-content/plugins/akismet/akismet.php

https://bitbucket.org/floppyxyz/musical
PHP | 608 lines | 439 code | 94 blank | 75 comment | 116 complexity | 6c4fed35bcc4b1a558fef6d7168a5c1a MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @package Akismet
  4. */
  5. /*
  6. Plugin Name: Akismet
  7. Plugin URI: http://akismet.com/?return=true
  8. Description: Used by millions, Akismet is quite possibly the best way in the world to <strong>protect your blog from comment and trackback spam</strong>. It keeps your site protected from spam even while you sleep. To get started: 1) Click the "Activate" link to the left of this description, 2) <a href="http://akismet.com/get/?return=true">Sign up for an Akismet API key</a>, and 3) Go to your <a href="admin.php?page=akismet-key-config">Akismet configuration</a> page, and save your API key.
  9. Version: 2.5.6
  10. Author: Automattic
  11. Author URI: http://automattic.com/wordpress-plugins/
  12. License: GPLv2 or later
  13. */
  14. /*
  15. This program is free software; you can redistribute it and/or
  16. modify it under the terms of the GNU General Public License
  17. as published by the Free Software Foundation; either version 2
  18. of the License, or (at your option) any later version.
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. */
  27. define('AKISMET_VERSION', '2.5.6');
  28. define('AKISMET_PLUGIN_URL', plugin_dir_url( __FILE__ ));
  29. /** If you hardcode a WP.com API key here, all key config screens will be hidden */
  30. if ( defined('WPCOM_API_KEY') )
  31. $wpcom_api_key = constant('WPCOM_API_KEY');
  32. else
  33. $wpcom_api_key = '';
  34. // Make sure we don't expose any info if called directly
  35. if ( !function_exists( 'add_action' ) ) {
  36. echo "Hi there! I'm just a plugin, not much I can do when called directly.";
  37. exit;
  38. }
  39. if ( isset($wp_db_version) && $wp_db_version <= 9872 )
  40. include_once dirname( __FILE__ ) . '/legacy.php';
  41. include_once dirname( __FILE__ ) . '/widget.php';
  42. if ( is_admin() )
  43. require_once dirname( __FILE__ ) . '/admin.php';
  44. function akismet_init() {
  45. global $wpcom_api_key, $akismet_api_host, $akismet_api_port;
  46. if ( $wpcom_api_key )
  47. $akismet_api_host = $wpcom_api_key . '.rest.akismet.com';
  48. else
  49. $akismet_api_host = get_option('wordpress_api_key') . '.rest.akismet.com';
  50. $akismet_api_port = 80;
  51. }
  52. add_action('init', 'akismet_init');
  53. function akismet_get_key() {
  54. global $wpcom_api_key;
  55. if ( !empty($wpcom_api_key) )
  56. return $wpcom_api_key;
  57. return get_option('wordpress_api_key');
  58. }
  59. function akismet_check_key_status( $key, $ip = null ) {
  60. global $akismet_api_host, $akismet_api_port, $wpcom_api_key;
  61. $blog = urlencode( get_option('home') );
  62. if ( $wpcom_api_key )
  63. $key = $wpcom_api_key;
  64. $response = akismet_http_post("key=$key&blog=$blog", 'rest.akismet.com', '/1.1/verify-key', $akismet_api_port, $ip);
  65. return $response;
  66. }
  67. // given a response from an API call like akismet_check_key_status(), update the alert code options if an alert is present.
  68. function akismet_update_alert( $response ) {
  69. $code = $msg = null;
  70. if ( isset($response[0]['x-akismet-alert-code']) ) {
  71. $code = $response[0]['x-akismet-alert-code'];
  72. $msg = $response[0]['x-akismet-alert-msg'];
  73. }
  74. // only call update_option() if the value has changed
  75. if ( $code != get_option( 'akismet_alert_code' ) ) {
  76. update_option( 'akismet_alert_code', $code );
  77. update_option( 'akismet_alert_msg', $msg );
  78. }
  79. }
  80. function akismet_verify_key( $key, $ip = null ) {
  81. $response = akismet_check_key_status( $key, $ip );
  82. akismet_update_alert( $response );
  83. if ( !is_array($response) || !isset($response[1]) || $response[1] != 'valid' && $response[1] != 'invalid' )
  84. return 'failed';
  85. return $response[1];
  86. }
  87. // if we're in debug or test modes, use a reduced service level so as not to polute training or stats data
  88. function akismet_test_mode() {
  89. if ( defined('AKISMET_TEST_MODE') && AKISMET_TEST_MODE )
  90. return true;
  91. return false;
  92. }
  93. // return a comma-separated list of role names for the given user
  94. function akismet_get_user_roles($user_id ) {
  95. $roles = false;
  96. if ( !class_exists('WP_User') )
  97. return false;
  98. if ( $user_id > 0 ) {
  99. $comment_user = new WP_User($user_id);
  100. if ( isset($comment_user->roles) )
  101. $roles = join(',', $comment_user->roles);
  102. }
  103. if ( is_multisite() && is_super_admin( $user_id ) ) {
  104. if ( empty( $roles ) ) {
  105. $roles = 'super_admin';
  106. } else {
  107. $comment_user->roles[] = 'super_admin';
  108. $roles = join( ',', $comment_user->roles );
  109. }
  110. }
  111. return $roles;
  112. }
  113. // Returns array with headers in $response[0] and body in $response[1]
  114. function akismet_http_post($request, $host, $path, $port = 80, $ip=null) {
  115. global $wp_version;
  116. $akismet_ua = "WordPress/{$wp_version} | ";
  117. $akismet_ua .= 'Akismet/' . constant( 'AKISMET_VERSION' );
  118. $akismet_ua = apply_filters( 'akismet_ua', $akismet_ua );
  119. $content_length = strlen( $request );
  120. $http_host = $host;
  121. // use a specific IP if provided
  122. // needed by akismet_check_server_connectivity()
  123. if ( $ip && long2ip( ip2long( $ip ) ) ) {
  124. $http_host = $ip;
  125. } else {
  126. $http_host = $host;
  127. }
  128. // use the WP HTTP class if it is available
  129. if ( function_exists( 'wp_remote_post' ) ) {
  130. $http_args = array(
  131. 'body' => $request,
  132. 'headers' => array(
  133. 'Content-Type' => 'application/x-www-form-urlencoded; ' .
  134. 'charset=' . get_option( 'blog_charset' ),
  135. 'Host' => $host,
  136. 'User-Agent' => $akismet_ua
  137. ),
  138. 'httpversion' => '1.0',
  139. 'timeout' => 15
  140. );
  141. $akismet_url = "http://{$http_host}{$path}";
  142. $response = wp_remote_post( $akismet_url, $http_args );
  143. if ( is_wp_error( $response ) )
  144. return '';
  145. return array( $response['headers'], $response['body'] );
  146. } else {
  147. $http_request = "POST $path HTTP/1.0\r\n";
  148. $http_request .= "Host: $host\r\n";
  149. $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n";
  150. $http_request .= "Content-Length: {$content_length}\r\n";
  151. $http_request .= "User-Agent: {$akismet_ua}\r\n";
  152. $http_request .= "\r\n";
  153. $http_request .= $request;
  154. $response = '';
  155. if( false != ( $fs = @fsockopen( $http_host, $port, $errno, $errstr, 10 ) ) ) {
  156. fwrite( $fs, $http_request );
  157. while ( !feof( $fs ) )
  158. $response .= fgets( $fs, 1160 ); // One TCP-IP packet
  159. fclose( $fs );
  160. $response = explode( "\r\n\r\n", $response, 2 );
  161. }
  162. return $response;
  163. }
  164. }
  165. // filter handler used to return a spam result to pre_comment_approved
  166. function akismet_result_spam( $approved ) {
  167. // bump the counter here instead of when the filter is added to reduce the possibility of overcounting
  168. if ( $incr = apply_filters('akismet_spam_count_incr', 1) )
  169. update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr );
  170. // this is a one-shot deal
  171. remove_filter( 'pre_comment_approved', 'akismet_result_spam' );
  172. return 'spam';
  173. }
  174. function akismet_result_hold( $approved ) {
  175. // once only
  176. remove_filter( 'pre_comment_approved', 'akismet_result_hold' );
  177. return '0';
  178. }
  179. // how many approved comments does this author have?
  180. function akismet_get_user_comments_approved( $user_id, $comment_author_email, $comment_author, $comment_author_url ) {
  181. global $wpdb;
  182. if ( !empty($user_id) )
  183. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE user_id = %d AND comment_approved = 1", $user_id ) );
  184. if ( !empty($comment_author_email) )
  185. return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_author_email = %s AND comment_author = %s AND comment_author_url = %s AND comment_approved = 1", $comment_author_email, $comment_author, $comment_author_url ) );
  186. return 0;
  187. }
  188. function akismet_microtime() {
  189. $mtime = explode( ' ', microtime() );
  190. return $mtime[1] + $mtime[0];
  191. }
  192. // log an event for a given comment, storing it in comment_meta
  193. function akismet_update_comment_history( $comment_id, $message, $event=null ) {
  194. global $current_user;
  195. // failsafe for old WP versions
  196. if ( !function_exists('add_comment_meta') )
  197. return false;
  198. $user = '';
  199. if ( is_object($current_user) && isset($current_user->user_login) )
  200. $user = $current_user->user_login;
  201. $event = array(
  202. 'time' => akismet_microtime(),
  203. 'message' => $message,
  204. 'event' => $event,
  205. 'user' => $user,
  206. );
  207. // $unique = false so as to allow multiple values per comment
  208. $r = add_comment_meta( $comment_id, 'akismet_history', $event, false );
  209. }
  210. // get the full comment history for a given comment, as an array in reverse chronological order
  211. function akismet_get_comment_history( $comment_id ) {
  212. // failsafe for old WP versions
  213. if ( !function_exists('add_comment_meta') )
  214. return false;
  215. $history = get_comment_meta( $comment_id, 'akismet_history', false );
  216. usort( $history, 'akismet_cmp_time' );
  217. return $history;
  218. }
  219. function akismet_cmp_time( $a, $b ) {
  220. return $a['time'] > $b['time'] ? -1 : 1;
  221. }
  222. // this fires on wp_insert_comment. we can't update comment_meta when akismet_auto_check_comment() runs
  223. // because we don't know the comment ID at that point.
  224. function akismet_auto_check_update_meta( $id, $comment ) {
  225. global $akismet_last_comment;
  226. // failsafe for old WP versions
  227. if ( !function_exists('add_comment_meta') )
  228. return false;
  229. // wp_insert_comment() might be called in other contexts, so make sure this is the same comment
  230. // as was checked by akismet_auto_check_comment
  231. if ( is_object($comment) && !empty($akismet_last_comment) && is_array($akismet_last_comment) ) {
  232. if ( intval($akismet_last_comment['comment_post_ID']) == intval($comment->comment_post_ID)
  233. && $akismet_last_comment['comment_author'] == $comment->comment_author
  234. && $akismet_last_comment['comment_author_email'] == $comment->comment_author_email ) {
  235. // normal result: true or false
  236. if ( $akismet_last_comment['akismet_result'] == 'true' ) {
  237. update_comment_meta( $comment->comment_ID, 'akismet_result', 'true' );
  238. akismet_update_comment_history( $comment->comment_ID, __('Akismet caught this comment as spam'), 'check-spam' );
  239. if ( $comment->comment_approved != 'spam' )
  240. akismet_update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s'), $comment->comment_approved), 'status-changed'.$comment->comment_approved );
  241. } elseif ( $akismet_last_comment['akismet_result'] == 'false' ) {
  242. update_comment_meta( $comment->comment_ID, 'akismet_result', 'false' );
  243. akismet_update_comment_history( $comment->comment_ID, __('Akismet cleared this comment'), 'check-ham' );
  244. if ( $comment->comment_approved == 'spam' ) {
  245. if ( wp_blacklist_check($comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent) )
  246. akismet_update_comment_history( $comment->comment_ID, __('Comment was caught by wp_blacklist_check'), 'wp-blacklisted' );
  247. else
  248. akismet_update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s'), $comment->comment_approved), 'status-changed-'.$comment->comment_approved );
  249. }
  250. // abnormal result: error
  251. } else {
  252. update_comment_meta( $comment->comment_ID, 'akismet_error', time() );
  253. akismet_update_comment_history( $comment->comment_ID, sprintf( __('Akismet was unable to check this comment (response: %s), will automatically retry again later.'), substr($akismet_last_comment['akismet_result'], 0, 50)), 'check-error' );
  254. }
  255. // record the complete original data as submitted for checking
  256. if ( isset($akismet_last_comment['comment_as_submitted']) )
  257. update_comment_meta( $comment->comment_ID, 'akismet_as_submitted', $akismet_last_comment['comment_as_submitted'] );
  258. }
  259. }
  260. }
  261. add_action( 'wp_insert_comment', 'akismet_auto_check_update_meta', 10, 2 );
  262. function akismet_auto_check_comment( $commentdata ) {
  263. global $akismet_api_host, $akismet_api_port, $akismet_last_comment;
  264. $comment = $commentdata;
  265. $comment['user_ip'] = $_SERVER['REMOTE_ADDR'];
  266. $comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
  267. $comment['referrer'] = $_SERVER['HTTP_REFERER'];
  268. $comment['blog'] = get_option('home');
  269. $comment['blog_lang'] = get_locale();
  270. $comment['blog_charset'] = get_option('blog_charset');
  271. $comment['permalink'] = get_permalink($comment['comment_post_ID']);
  272. if ( !empty( $comment['user_ID'] ) ) {
  273. $comment['user_role'] = akismet_get_user_roles($comment['user_ID']);
  274. }
  275. $akismet_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
  276. $comment['akismet_comment_nonce'] = 'inactive';
  277. if ( $akismet_nonce_option == 'true' || $akismet_nonce_option == '' ) {
  278. $comment['akismet_comment_nonce'] = 'failed';
  279. if ( isset( $_POST['akismet_comment_nonce'] ) && wp_verify_nonce( $_POST['akismet_comment_nonce'], 'akismet_comment_nonce_' . $comment['comment_post_ID'] ) )
  280. $comment['akismet_comment_nonce'] = 'passed';
  281. // comment reply in wp-admin
  282. if ( isset( $_POST['_ajax_nonce-replyto-comment'] ) && check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ) )
  283. $comment['akismet_comment_nonce'] = 'passed';
  284. }
  285. if ( akismet_test_mode() )
  286. $comment['is_test'] = 'true';
  287. foreach ($_POST as $key => $value ) {
  288. if ( is_string($value) )
  289. $comment["POST_{$key}"] = $value;
  290. }
  291. $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
  292. foreach ( $_SERVER as $key => $value ) {
  293. if ( !in_array( $key, $ignore ) && is_string($value) )
  294. $comment["$key"] = $value;
  295. else
  296. $comment["$key"] = '';
  297. }
  298. $post = get_post( $comment['comment_post_ID'] );
  299. $comment[ 'comment_post_modified_gmt' ] = $post->post_modified_gmt;
  300. $query_string = '';
  301. foreach ( $comment as $key => $data )
  302. $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
  303. $commentdata['comment_as_submitted'] = $comment;
  304. $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
  305. akismet_update_alert( $response );
  306. $commentdata['akismet_result'] = $response[1];
  307. if ( 'true' == $response[1] ) {
  308. // akismet_spam_count will be incremented later by akismet_result_spam()
  309. add_filter('pre_comment_approved', 'akismet_result_spam');
  310. do_action( 'akismet_spam_caught' );
  311. $last_updated = strtotime( $post->post_modified_gmt );
  312. $diff = time() - $last_updated;
  313. $diff = $diff / 86400;
  314. if ( $post->post_type == 'post' && $diff > 30 && get_option( 'akismet_discard_month' ) == 'true' && empty($comment['user_ID']) ) {
  315. // akismet_result_spam() won't be called so bump the counter here
  316. if ( $incr = apply_filters('akismet_spam_count_incr', 1) )
  317. update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr );
  318. wp_safe_redirect( $_SERVER['HTTP_REFERER'] );
  319. die();
  320. }
  321. }
  322. // if the response is neither true nor false, hold the comment for moderation and schedule a recheck
  323. if ( 'true' != $response[1] && 'false' != $response[1] ) {
  324. if ( !current_user_can('moderate_comments') ) {
  325. add_filter('pre_comment_approved', 'akismet_result_hold');
  326. }
  327. if ( !wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
  328. wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
  329. }
  330. }
  331. if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_event') ) {
  332. // WP 2.1+: delete old comments daily
  333. if ( !wp_next_scheduled('akismet_scheduled_delete') )
  334. wp_schedule_event(time(), 'daily', 'akismet_scheduled_delete');
  335. } elseif ( (mt_rand(1, 10) == 3) ) {
  336. // WP 2.0: run this one time in ten
  337. akismet_delete_old();
  338. }
  339. $akismet_last_comment = $commentdata;
  340. akismet_fix_scheduled_recheck();
  341. return $commentdata;
  342. }
  343. add_action('preprocess_comment', 'akismet_auto_check_comment', 1);
  344. function akismet_delete_old() {
  345. global $wpdb;
  346. $now_gmt = current_time('mysql', 1);
  347. $comment_ids = $wpdb->get_col("SELECT comment_id FROM $wpdb->comments WHERE DATE_SUB('$now_gmt', INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam'");
  348. if ( empty( $comment_ids ) )
  349. return;
  350. $comma_comment_ids = implode( ', ', array_map('intval', $comment_ids) );
  351. do_action( 'delete_comment', $comment_ids );
  352. $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_id IN ( $comma_comment_ids )");
  353. $wpdb->query("DELETE FROM $wpdb->commentmeta WHERE comment_id IN ( $comma_comment_ids )");
  354. clean_comment_cache( $comment_ids );
  355. $n = mt_rand(1, 5000);
  356. if ( apply_filters('akismet_optimize_table', ($n == 11)) ) // lucky number
  357. $wpdb->query("OPTIMIZE TABLE $wpdb->comments");
  358. }
  359. function akismet_delete_old_metadata() {
  360. global $wpdb;
  361. $now_gmt = current_time( 'mysql', 1 );
  362. $interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 );
  363. # enfore a minimum of 1 day
  364. $interval = absint( $interval );
  365. if ( $interval < 1 ) {
  366. return;
  367. }
  368. // akismet_as_submitted meta values are large, so expire them
  369. // after $interval days regardless of the comment status
  370. while ( TRUE ) {
  371. $comment_ids = $wpdb->get_col( "SELECT $wpdb->comments.comment_id FROM $wpdb->commentmeta INNER JOIN $wpdb->comments USING(comment_id) WHERE meta_key = 'akismet_as_submitted' AND DATE_SUB('$now_gmt', INTERVAL {$interval} DAY) > comment_date_gmt LIMIT 10000" );
  372. if ( empty( $comment_ids ) ) {
  373. return;
  374. }
  375. foreach ( $comment_ids as $comment_id ) {
  376. delete_comment_meta( $comment_id, 'akismet_as_submitted' );
  377. }
  378. }
  379. /*
  380. $n = mt_rand( 1, 5000 );
  381. if ( apply_filters( 'akismet_optimize_table', ( $n == 11 ), 'commentmeta' ) ) { // lucky number
  382. $wpdb->query( "OPTIMIZE TABLE $wpdb->commentmeta" );
  383. }
  384. */
  385. }
  386. add_action('akismet_scheduled_delete', 'akismet_delete_old');
  387. add_action('akismet_scheduled_delete', 'akismet_delete_old_metadata');
  388. function akismet_check_db_comment( $id, $recheck_reason = 'recheck_queue' ) {
  389. global $wpdb, $akismet_api_host, $akismet_api_port;
  390. $id = (int) $id;
  391. $c = $wpdb->get_row( "SELECT * FROM $wpdb->comments WHERE comment_ID = '$id'", ARRAY_A );
  392. if ( !$c )
  393. return;
  394. $c['user_ip'] = $c['comment_author_IP'];
  395. $c['user_agent'] = $c['comment_agent'];
  396. $c['referrer'] = '';
  397. $c['blog'] = get_option('home');
  398. $c['blog_lang'] = get_locale();
  399. $c['blog_charset'] = get_option('blog_charset');
  400. $c['permalink'] = get_permalink($c['comment_post_ID']);
  401. $id = $c['comment_ID'];
  402. if ( akismet_test_mode() )
  403. $c['is_test'] = 'true';
  404. $c['recheck_reason'] = $recheck_reason;
  405. $query_string = '';
  406. foreach ( $c as $key => $data )
  407. $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
  408. $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
  409. return $response[1];
  410. }
  411. function akismet_cron_recheck() {
  412. global $wpdb;
  413. $status = akismet_verify_key( akismet_get_key() );
  414. if ( get_option( 'akismet_alert_code' ) || $status == 'invalid' ) {
  415. // since there is currently a problem with the key, reschedule a check for 6 hours hence
  416. wp_schedule_single_event( time() + 21600, 'akismet_schedule_cron_recheck' );
  417. return false;
  418. }
  419. delete_option('akismet_available_servers');
  420. $comment_errors = $wpdb->get_col( "
  421. SELECT comment_id
  422. FROM {$wpdb->prefix}commentmeta
  423. WHERE meta_key = 'akismet_error'
  424. LIMIT 100
  425. " );
  426. foreach ( (array) $comment_errors as $comment_id ) {
  427. // if the comment no longer exists, or is too old, remove the meta entry from the queue to avoid getting stuck
  428. $comment = get_comment( $comment_id );
  429. if ( !$comment || strtotime( $comment->comment_date_gmt ) < strtotime( "-15 days" ) ) {
  430. delete_comment_meta( $comment_id, 'akismet_error' );
  431. continue;
  432. }
  433. add_comment_meta( $comment_id, 'akismet_rechecking', true );
  434. $status = akismet_check_db_comment( $comment_id, 'retry' );
  435. $msg = '';
  436. if ( $status == 'true' ) {
  437. $msg = __( 'Akismet caught this comment as spam during an automatic retry.' );
  438. } elseif ( $status == 'false' ) {
  439. $msg = __( 'Akismet cleared this comment during an automatic retry.' );
  440. }
  441. // If we got back a legit response then update the comment history
  442. // other wise just bail now and try again later. No point in
  443. // re-trying all the comments once we hit one failure.
  444. if ( !empty( $msg ) ) {
  445. delete_comment_meta( $comment_id, 'akismet_error' );
  446. akismet_update_comment_history( $comment_id, $msg, 'cron-retry' );
  447. update_comment_meta( $comment_id, 'akismet_result', $status );
  448. // make sure the comment status is still pending. if it isn't, that means the user has already moved it elsewhere.
  449. $comment = get_comment( $comment_id );
  450. if ( $comment && 'unapproved' == wp_get_comment_status( $comment_id ) ) {
  451. if ( $status == 'true' ) {
  452. wp_spam_comment( $comment_id );
  453. } elseif ( $status == 'false' ) {
  454. // comment is good, but it's still in the pending queue. depending on the moderation settings
  455. // we may need to change it to approved.
  456. if ( check_comment($comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent, $comment->comment_type) )
  457. wp_set_comment_status( $comment_id, 1 );
  458. }
  459. }
  460. } else {
  461. delete_comment_meta( $comment_id, 'akismet_rechecking' );
  462. wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
  463. return;
  464. }
  465. delete_comment_meta( $comment_id, 'akismet_rechecking' );
  466. }
  467. $remaining = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->commentmeta WHERE meta_key = 'akismet_error'" ) );
  468. if ( $remaining && !wp_next_scheduled('akismet_schedule_cron_recheck') ) {
  469. wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
  470. }
  471. }
  472. add_action( 'akismet_schedule_cron_recheck', 'akismet_cron_recheck' );
  473. function akismet_add_comment_nonce( $post_id ) {
  474. echo '<p style="display: none;">';
  475. wp_nonce_field( 'akismet_comment_nonce_' . $post_id, 'akismet_comment_nonce', FALSE );
  476. echo '</p>';
  477. }
  478. $akismet_comment_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
  479. if ( $akismet_comment_nonce_option == 'true' || $akismet_comment_nonce_option == '' )
  480. add_action( 'comment_form', 'akismet_add_comment_nonce' );
  481. global $wp_version;
  482. if ( '3.0.5' == $wp_version ) {
  483. remove_filter( 'comment_text', 'wp_kses_data' );
  484. if ( is_admin() )
  485. add_filter( 'comment_text', 'wp_kses_post' );
  486. }
  487. function akismet_fix_scheduled_recheck() {
  488. $future_check = wp_next_scheduled( 'akismet_schedule_cron_recheck' );
  489. if ( !$future_check ) {
  490. return;
  491. }
  492. if ( get_option( 'akismet_alert_code' ) > 0 ) {
  493. return;
  494. }
  495. $check_range = time() + 1200;
  496. if ( $future_check > $check_range ) {
  497. wp_clear_scheduled_hook( 'akismet_schedule_cron_recheck' );
  498. wp_schedule_single_event( time() + 300, 'akismet_schedule_cron_recheck' );
  499. }
  500. }