PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/akismet/class.akismet-admin.php

https://bitbucket.org/acipriani/madeinapulia.com
PHP | 862 lines | 671 code | 151 blank | 40 comment | 172 complexity | 86524cbbb5b2ea8b12f764a35cec6f4c MD5 | raw file
Possible License(s): GPL-3.0, MIT, BSD-3-Clause, LGPL-2.1, GPL-2.0, Apache-2.0
  1. <?php
  2. class Akismet_Admin {
  3. const NONCE = 'akismet-update-key';
  4. private static $initiated = false;
  5. private static $notices = array();
  6. public static function init() {
  7. if ( ! self::$initiated ) {
  8. self::init_hooks();
  9. }
  10. if ( isset( $_POST['action'] ) && $_POST['action'] == 'enter-key' ) {
  11. self::enter_api_key();
  12. }
  13. }
  14. public static function init_hooks() {
  15. // The standalone stats page was removed in 3.0 for an all-in-one config and stats page.
  16. // Redirect any links that might have been bookmarked or in browser history.
  17. if ( isset( $_GET['page'] ) && 'akismet-stats-display' == $_GET['page'] ) {
  18. wp_safe_redirect( esc_url_raw( self::get_page_url( 'stats' ) ), 301 );
  19. die;
  20. }
  21. self::$initiated = true;
  22. add_action( 'admin_init', array( 'Akismet_Admin', 'admin_init' ) );
  23. add_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 ); # Priority 5, so it's called before Jetpack's admin_menu.
  24. add_action( 'admin_notices', array( 'Akismet_Admin', 'display_notice' ) );
  25. add_action( 'admin_enqueue_scripts', array( 'Akismet_Admin', 'load_resources' ) );
  26. add_action( 'activity_box_end', array( 'Akismet_Admin', 'dashboard_stats' ) );
  27. add_action( 'rightnow_end', array( 'Akismet_Admin', 'rightnow_stats' ) );
  28. add_action( 'manage_comments_nav', array( 'Akismet_Admin', 'check_for_spam_button' ) );
  29. add_action( 'admin_action_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
  30. add_action( 'wp_ajax_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
  31. add_action( 'wp_ajax_comment_author_deurl', array( 'Akismet_Admin', 'remove_comment_author_url' ) );
  32. add_action( 'wp_ajax_comment_author_reurl', array( 'Akismet_Admin', 'add_comment_author_url' ) );
  33. add_filter( 'plugin_action_links', array( 'Akismet_Admin', 'plugin_action_links' ), 10, 2 );
  34. add_filter( 'comment_row_actions', array( 'Akismet_Admin', 'comment_row_action' ), 10, 2 );
  35. add_filter( 'comment_text', array( 'Akismet_Admin', 'text_add_link_class' ) );
  36. add_filter( 'plugin_action_links_'.plugin_basename( plugin_dir_path( __FILE__ ) . 'akismet.php'), array( 'Akismet_Admin', 'admin_plugin_settings_link' ) );
  37. add_filter( 'wxr_export_skip_commentmeta', array( 'Akismet_Admin', 'exclude_commentmeta_from_export' ), 10, 3 );
  38. }
  39. public static function admin_init() {
  40. load_plugin_textdomain( 'akismet' );
  41. add_meta_box( 'akismet-status', __('Comment History', 'akismet'), array( 'Akismet_Admin', 'comment_status_meta_box' ), 'comment', 'normal' );
  42. }
  43. public static function admin_menu() {
  44. if ( class_exists( 'Jetpack' ) )
  45. add_action( 'jetpack_admin_menu', array( 'Akismet_Admin', 'load_menu' ) );
  46. else
  47. self::load_menu();
  48. }
  49. public static function admin_head() {
  50. if ( !current_user_can( 'manage_options' ) )
  51. return;
  52. }
  53. public static function admin_plugin_settings_link( $links ) {
  54. $settings_link = '<a href="'.esc_url( self::get_page_url() ).'">'.__('Settings', 'akismet').'</a>';
  55. array_unshift( $links, $settings_link );
  56. return $links;
  57. }
  58. public static function load_menu() {
  59. if ( class_exists( 'Jetpack' ) )
  60. $hook = add_submenu_page( 'jetpack', __( 'Akismet' , 'akismet'), __( 'Akismet' , 'akismet'), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
  61. else
  62. $hook = add_options_page( __('Akismet', 'akismet'), __('Akismet', 'akismet'), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
  63. if ( version_compare( $GLOBALS['wp_version'], '3.3', '>=' ) ) {
  64. add_action( "load-$hook", array( 'Akismet_Admin', 'admin_help' ) );
  65. }
  66. }
  67. public static function load_resources() {
  68. global $hook_suffix;
  69. if ( in_array( $hook_suffix, array(
  70. 'index.php', # dashboard
  71. 'edit-comments.php',
  72. 'comment.php',
  73. 'post.php',
  74. 'settings_page_akismet-key-config',
  75. 'jetpack_page_akismet-key-config',
  76. ) ) ) {
  77. wp_register_style( 'akismet.css', AKISMET__PLUGIN_URL . '_inc/akismet.css', array(), AKISMET_VERSION );
  78. wp_enqueue_style( 'akismet.css');
  79. wp_register_script( 'akismet.js', AKISMET__PLUGIN_URL . '_inc/akismet.js', array('jquery','postbox'), AKISMET_VERSION );
  80. wp_enqueue_script( 'akismet.js' );
  81. wp_localize_script( 'akismet.js', 'WPAkismet', array(
  82. 'comment_author_url_nonce' => wp_create_nonce( 'comment_author_url_nonce' ),
  83. 'strings' => array(
  84. 'Remove this URL' => __( 'Remove this URL' , 'akismet'),
  85. 'Removing...' => __( 'Removing...' , 'akismet'),
  86. 'URL removed' => __( 'URL removed' , 'akismet'),
  87. '(undo)' => __( '(undo)' , 'akismet'),
  88. 'Re-adding...' => __( 'Re-adding...' , 'akismet'),
  89. )
  90. ) );
  91. }
  92. }
  93. /**
  94. * Add help to the Akismet page
  95. *
  96. * @return false if not the Akismet page
  97. */
  98. public static function admin_help() {
  99. $current_screen = get_current_screen();
  100. // Screen Content
  101. if ( current_user_can( 'manage_options' ) ) {
  102. if ( !Akismet::get_api_key() || ( isset( $_GET['view'] ) && $_GET['view'] == 'start' ) ) {
  103. //setup page
  104. $current_screen->add_help_tab(
  105. array(
  106. 'id' => 'overview',
  107. 'title' => __( 'Overview' , 'akismet'),
  108. 'content' =>
  109. '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
  110. '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
  111. '<p>' . esc_html__( 'On this page, you are able to setup the Akismet plugin.' , 'akismet') . '</p>',
  112. )
  113. );
  114. $current_screen->add_help_tab(
  115. array(
  116. 'id' => 'setup-signup',
  117. 'title' => __( 'New to Akismet' , 'akismet'),
  118. 'content' =>
  119. '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
  120. '<p>' . esc_html__( 'You need to enter an API key to activate the Akismet service on your site.' , 'akismet') . '</p>' .
  121. '<p>' . sprintf( __( 'Signup for an account on %s to get an API Key.' , 'akismet'), '<a href="https://akismet.com/plugin-signup/" target="_blank">Akismet.com</a>' ) . '</p>',
  122. )
  123. );
  124. $current_screen->add_help_tab(
  125. array(
  126. 'id' => 'setup-manual',
  127. 'title' => __( 'Enter an API Key' , 'akismet'),
  128. 'content' =>
  129. '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
  130. '<p>' . esc_html__( 'If you already have an API key' , 'akismet') . '</p>' .
  131. '<ol>' .
  132. '<li>' . esc_html__( 'Copy and paste the API key into the text field.' , 'akismet') . '</li>' .
  133. '<li>' . esc_html__( 'Click the Use this Key button.' , 'akismet') . '</li>' .
  134. '</ol>',
  135. )
  136. );
  137. }
  138. elseif ( isset( $_GET['view'] ) && $_GET['view'] == 'stats' ) {
  139. //stats page
  140. $current_screen->add_help_tab(
  141. array(
  142. 'id' => 'overview',
  143. 'title' => __( 'Overview' , 'akismet'),
  144. 'content' =>
  145. '<p><strong>' . esc_html__( 'Akismet Stats' , 'akismet') . '</strong></p>' .
  146. '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
  147. '<p>' . esc_html__( 'On this page, you are able to view stats on spam filtered on your site.' , 'akismet') . '</p>',
  148. )
  149. );
  150. }
  151. else {
  152. //configuration page
  153. $current_screen->add_help_tab(
  154. array(
  155. 'id' => 'overview',
  156. 'title' => __( 'Overview' , 'akismet'),
  157. 'content' =>
  158. '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
  159. '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
  160. '<p>' . esc_html__( 'On this page, you are able to enter/remove an API key, view account information and view spam stats.' , 'akismet') . '</p>',
  161. )
  162. );
  163. $current_screen->add_help_tab(
  164. array(
  165. 'id' => 'settings',
  166. 'title' => __( 'Settings' , 'akismet'),
  167. 'content' =>
  168. '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
  169. '<p><strong>' . esc_html__( 'API Key' , 'akismet') . '</strong> - ' . esc_html__( 'Enter/remove an API key.' , 'akismet') . '</p>' .
  170. '<p><strong>' . esc_html__( 'Comments' , 'akismet') . '</strong> - ' . esc_html__( 'Show the number of approved comments beside each comment author in the comments list page.' , 'akismet') . '</p>' .
  171. '<p><strong>' . esc_html__( 'Strictness' , 'akismet') . '</strong> - ' . esc_html__( 'Choose to either discard the worst spam automatically or to always put all spam in spam folder.' , 'akismet') . '</p>',
  172. )
  173. );
  174. $current_screen->add_help_tab(
  175. array(
  176. 'id' => 'account',
  177. 'title' => __( 'Account' , 'akismet'),
  178. 'content' =>
  179. '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
  180. '<p><strong>' . esc_html__( 'Subscription Type' , 'akismet') . '</strong> - ' . esc_html__( 'The Akismet subscription plan' , 'akismet') . '</p>' .
  181. '<p><strong>' . esc_html__( 'Status' , 'akismet') . '</strong> - ' . esc_html__( 'The subscription status - active, cancelled or suspended' , 'akismet') . '</p>',
  182. )
  183. );
  184. }
  185. }
  186. // Help Sidebar
  187. $current_screen->set_help_sidebar(
  188. '<p><strong>' . esc_html__( 'For more information:' , 'akismet') . '</strong></p>' .
  189. '<p><a href="https://akismet.com/faq/" target="_blank">' . esc_html__( 'Akismet FAQ' , 'akismet') . '</a></p>' .
  190. '<p><a href="https://akismet.com/support/" target="_blank">' . esc_html__( 'Akismet Support' , 'akismet') . '</a></p>'
  191. );
  192. }
  193. public static function enter_api_key() {
  194. if ( function_exists('current_user_can') && !current_user_can('manage_options') )
  195. die(__('Cheatin&#8217; uh?', 'akismet'));
  196. if ( !wp_verify_nonce( $_POST['_wpnonce'], self::NONCE ) )
  197. return false;
  198. foreach( array( 'akismet_strictness', 'akismet_show_user_comments_approved' ) as $option ) {
  199. update_option( $option, isset( $_POST[$option] ) && (int) $_POST[$option] == 1 ? '1' : '0' );
  200. }
  201. if ( defined( 'WPCOM_API_KEY' ) )
  202. return false; //shouldn't have option to save key if already defined
  203. $new_key = preg_replace( '/[^a-h0-9]/i', '', $_POST['key'] );
  204. $old_key = Akismet::get_api_key();
  205. if ( empty( $new_key ) ) {
  206. if ( !empty( $old_key ) ) {
  207. delete_option( 'wordpress_api_key' );
  208. self::$notices[] = 'new-key-empty';
  209. }
  210. }
  211. elseif ( $new_key != $old_key ) {
  212. self::save_key( $new_key );
  213. }
  214. return true;
  215. }
  216. public static function save_key( $api_key ) {
  217. $key_status = Akismet::verify_key( $api_key );
  218. if ( $key_status == 'valid' ) {
  219. $akismet_user = self::get_akismet_user( $api_key );
  220. if ( $akismet_user ) {
  221. if ( in_array( $akismet_user->status, array( 'active', 'active-dunning', 'no-sub' ) ) )
  222. update_option( 'wordpress_api_key', $api_key );
  223. if ( $akismet_user->status == 'active' )
  224. self::$notices['status'] = 'new-key-valid';
  225. else
  226. self::$notices['status'] = $akismet_user->status;
  227. }
  228. else
  229. self::$notices['status'] = 'new-key-invalid';
  230. }
  231. elseif ( in_array( $key_status, array( 'invalid', 'failed' ) ) )
  232. self::$notices['status'] = 'new-key-'.$key_status;
  233. }
  234. public static function dashboard_stats() {
  235. if ( !function_exists('did_action') || did_action( 'rightnow_end' ) )
  236. return; // We already displayed this info in the "Right Now" section
  237. if ( !$count = get_option('akismet_spam_count') )
  238. return;
  239. global $submenu;
  240. echo '<h3>' . esc_html( _x( 'Spam', 'comments' , 'akismet') ) . '</h3>';
  241. echo '<p>'.sprintf( _n(
  242. '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comment</a>.',
  243. '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.',
  244. $count
  245. , 'akismet'), 'https://akismet.com/wordpress/', esc_url( add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( isset( $submenu['edit-comments.php'] ) ? 'edit-comments.php' : 'edit.php' ) ) ), number_format_i18n($count) ).'</p>';
  246. }
  247. // WP 2.5+
  248. public static function rightnow_stats() {
  249. global $submenu, $wp_db_version;
  250. if ( 8645 < $wp_db_version ) // 2.7
  251. $link = add_query_arg( array( 'comment_status' => 'spam' ), admin_url( 'edit-comments.php' ) );
  252. elseif ( isset( $submenu['edit-comments.php'] ) )
  253. $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit-comments.php' ) );
  254. else
  255. $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit.php' ) );
  256. if ( $count = get_option('akismet_spam_count') ) {
  257. $intro = sprintf( _n(
  258. '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already. ',
  259. '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already. ',
  260. $count
  261. , 'akismet'), 'https://akismet.com/wordpress/', number_format_i18n( $count ) );
  262. } else {
  263. $intro = sprintf( __('<a href="%s">Akismet</a> blocks spam from getting to your blog. ', 'akismet'), 'https://akismet.com/wordpress/' );
  264. }
  265. $link = function_exists( 'esc_url' ) ? esc_url( $link ) : clean_url( $link );
  266. if ( $queue_count = self::get_spam_count() ) {
  267. $queue_text = sprintf( _n(
  268. 'There&#8217;s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
  269. 'There are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
  270. $queue_count
  271. , 'akismet'), number_format_i18n( $queue_count ), $link );
  272. } else {
  273. $queue_text = sprintf( __( "There&#8217;s nothing in your <a href='%s'>spam queue</a> at the moment." , 'akismet'), $link );
  274. }
  275. $text = $intro . '<br />' . $queue_text;
  276. echo "<p class='akismet-right-now'>$text</p>\n";
  277. }
  278. public static function check_for_spam_button( $comment_status ) {
  279. if ( 'approved' == $comment_status )
  280. return;
  281. if ( function_exists('plugins_url') )
  282. $link = add_query_arg( array( 'action' => 'akismet_recheck_queue' ), admin_url( 'admin.php' ) );
  283. else
  284. $link = add_query_arg( array( 'page' => 'akismet-admin', 'recheckqueue' => 'true', 'noheader' => 'true' ), admin_url( 'edit-comments.php' ) );
  285. echo '</div><div class="alignleft"><a class="button-secondary checkforspam" href="' . esc_url( $link ) . '">' . esc_html__('Check for Spam', 'akismet') . '</a><span class="checkforspam-spinner"></span>';
  286. }
  287. public static function recheck_queue() {
  288. global $wpdb;
  289. Akismet::fix_scheduled_recheck();
  290. if ( ! ( isset( $_GET['recheckqueue'] ) || ( isset( $_REQUEST['action'] ) && 'akismet_recheck_queue' == $_REQUEST['action'] ) ) )
  291. return;
  292. $paginate = '';
  293. if ( isset( $_POST['limit'] ) && isset( $_POST['offset'] ) ) {
  294. $paginate = $wpdb->prepare( " LIMIT %d OFFSET %d", array( $_POST['limit'], $_POST['offset'] ) );
  295. }
  296. $moderation = $wpdb->get_results( "SELECT * FROM {$wpdb->comments} WHERE comment_approved = '0'{$paginate}", ARRAY_A );
  297. foreach ( (array) $moderation as $c ) {
  298. $c['user_ip'] = $c['comment_author_IP'];
  299. $c['user_agent'] = $c['comment_agent'];
  300. $c['referrer'] = '';
  301. $c['blog'] = get_bloginfo('url');
  302. $c['blog_lang'] = get_locale();
  303. $c['blog_charset'] = get_option('blog_charset');
  304. $c['permalink'] = get_permalink($c['comment_post_ID']);
  305. $c['user_role'] = '';
  306. if ( isset( $c['user_ID'] ) )
  307. $c['user_role'] = Akismet::get_user_roles($c['user_ID']);
  308. if ( Akismet::is_test_mode() )
  309. $c['is_test'] = 'true';
  310. add_comment_meta( $c['comment_ID'], 'akismet_rechecking', true );
  311. $response = Akismet::http_post( Akismet::build_query( $c ), 'comment-check' );
  312. if ( 'true' == $response[1] ) {
  313. wp_set_comment_status( $c['comment_ID'], 'spam' );
  314. update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
  315. delete_comment_meta( $c['comment_ID'], 'akismet_error' );
  316. delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' );
  317. Akismet::update_comment_history( $c['comment_ID'], __('Akismet re-checked and caught this comment as spam', 'akismet'), 'check-spam' );
  318. } elseif ( 'false' == $response[1] ) {
  319. update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' );
  320. delete_comment_meta( $c['comment_ID'], 'akismet_error' );
  321. delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' );
  322. Akismet::update_comment_history( $c['comment_ID'], __('Akismet re-checked and cleared this comment', 'akismet'), 'check-ham' );
  323. // abnormal result: error
  324. } else {
  325. update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' );
  326. Akismet::update_comment_history( $c['comment_ID'], sprintf( __('Akismet was unable to re-check this comment (response: %s)', 'akismet'), substr($response[1], 0, 50)), 'check-error' );
  327. }
  328. delete_comment_meta( $c['comment_ID'], 'akismet_rechecking' );
  329. }
  330. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  331. wp_send_json( array(
  332. 'processed' => count((array) $moderation),
  333. ));
  334. }
  335. else {
  336. $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url( 'edit-comments.php' );
  337. wp_safe_redirect( $redirect_to );
  338. exit;
  339. }
  340. }
  341. // Adds an 'x' link next to author URLs, clicking will remove the author URL and show an undo link
  342. public static function remove_comment_author_url() {
  343. if ( !empty( $_POST['id'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
  344. $comment = get_comment( intval( $_POST['id'] ), ARRAY_A );
  345. if ( $comment && current_user_can( 'edit_comment', $comment['comment_ID'] ) ) {
  346. $comment['comment_author_url'] = '';
  347. do_action( 'comment_remove_author_url' );
  348. print( wp_update_comment( $comment ) );
  349. die();
  350. }
  351. }
  352. }
  353. public static function add_comment_author_url() {
  354. if ( !empty( $_POST['id'] ) && !empty( $_POST['url'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
  355. $comment = get_comment( intval( $_POST['id'] ), ARRAY_A );
  356. if ( $comment && current_user_can( 'edit_comment', $comment['comment_ID'] ) ) {
  357. $comment['comment_author_url'] = esc_url( $_POST['url'] );
  358. do_action( 'comment_add_author_url' );
  359. print( wp_update_comment( $comment ) );
  360. die();
  361. }
  362. }
  363. }
  364. public static function comment_row_action( $a, $comment ) {
  365. // failsafe for old WP versions
  366. if ( !function_exists('add_comment_meta') )
  367. return $a;
  368. $akismet_result = get_comment_meta( $comment->comment_ID, 'akismet_result', true );
  369. $akismet_error = get_comment_meta( $comment->comment_ID, 'akismet_error', true );
  370. $user_result = get_comment_meta( $comment->comment_ID, 'akismet_user_result', true);
  371. $comment_status = wp_get_comment_status( $comment->comment_ID );
  372. $desc = null;
  373. if ( $akismet_error ) {
  374. $desc = __( 'Awaiting spam check' , 'akismet');
  375. } elseif ( !$user_result || $user_result == $akismet_result ) {
  376. // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same
  377. if ( $akismet_result == 'true' && $comment_status != 'spam' && $comment_status != 'trash' )
  378. $desc = __( 'Flagged as spam by Akismet' , 'akismet');
  379. elseif ( $akismet_result == 'false' && $comment_status == 'spam' )
  380. $desc = __( 'Cleared by Akismet' , 'akismet');
  381. } else {
  382. $who = get_comment_meta( $comment->comment_ID, 'akismet_user', true );
  383. if ( $user_result == 'true' )
  384. $desc = sprintf( __('Flagged as spam by %s', 'akismet'), $who );
  385. else
  386. $desc = sprintf( __('Un-spammed by %s', 'akismet'), $who );
  387. }
  388. // add a History item to the hover links, just after Edit
  389. if ( $akismet_result ) {
  390. $b = array();
  391. foreach ( $a as $k => $item ) {
  392. $b[ $k ] = $item;
  393. if (
  394. $k == 'edit'
  395. || ( $k == 'unspam' && $GLOBALS['wp_version'] >= 3.4 )
  396. ) {
  397. $b['history'] = '<a href="comment.php?action=editcomment&amp;c='.$comment->comment_ID.'#akismet-status" title="'. esc_attr__( 'View comment history' , 'akismet') . '"> '. esc_html__('History', 'akismet') . '</a>';
  398. }
  399. }
  400. $a = $b;
  401. }
  402. if ( $desc )
  403. echo '<span class="akismet-status" commentid="'.$comment->comment_ID.'"><a href="comment.php?action=editcomment&amp;c='.$comment->comment_ID.'#akismet-status" title="' . esc_attr__( 'View comment history' , 'akismet') . '">'.esc_html( $desc ).'</a></span>';
  404. $show_user_comments = apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') );
  405. $show_user_comments = $show_user_comments === 'false' ? false : $show_user_comments; //option used to be saved as 'false' / 'true'
  406. if ( $show_user_comments ) {
  407. $comment_count = Akismet::get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
  408. $comment_count = intval( $comment_count );
  409. echo '<span class="akismet-user-comment-count" commentid="'.$comment->comment_ID.'" style="display:none;"><br><span class="akismet-user-comment-counts">'. sprintf( esc_html( _n( '%s approved', '%s approved', $comment_count , 'akismet') ), number_format_i18n( $comment_count ) ) . '</span></span>';
  410. }
  411. return $a;
  412. }
  413. public static function comment_status_meta_box( $comment ) {
  414. $history = Akismet::get_comment_history( $comment->comment_ID );
  415. if ( $history ) {
  416. echo '<div class="akismet-history" style="margin: 13px;">';
  417. foreach ( $history as $row ) {
  418. $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
  419. echo '<div style="margin-bottom: 13px;"><span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( esc_html__('%s ago', 'akismet'), human_time_diff( $row['time'] ) ) . '</span> - ';
  420. echo esc_html( $row['message'] ) . '</div>';
  421. }
  422. echo '</div>';
  423. }
  424. }
  425. public static function plugin_action_links( $links, $file ) {
  426. if ( $file == plugin_basename( AKISMET__PLUGIN_URL . '/akismet.php' ) ) {
  427. $links[] = '<a href="' . esc_url( self::get_page_url() ) . '">'.esc_html__( 'Settings' , 'akismet').'</a>';
  428. }
  429. return $links;
  430. }
  431. public static function text_add_link_callback( $m ) {
  432. // bare link?
  433. if ( $m[4] == $m[2] )
  434. return '<a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a>';
  435. else
  436. return '<span title="'.$m[2].'" class="comment-link"><a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a></span>';
  437. }
  438. public static function text_add_link_class( $comment_text ) {
  439. return preg_replace_callback( '#<a ([^>]*)href="([^"]+)"([^>]*)>(.*?)</a>#i', array( 'Akismet_Admin', 'text_add_link_callback' ), $comment_text );
  440. }
  441. // Total spam in queue
  442. // get_option( 'akismet_spam_count' ) is the total caught ever
  443. public static function get_spam_count( $type = false ) {
  444. global $wpdb;
  445. if ( !$type ) { // total
  446. $count = wp_cache_get( 'akismet_spam_count', 'widget' );
  447. if ( false === $count ) {
  448. if ( function_exists('wp_count_comments') ) {
  449. $count = wp_count_comments();
  450. $count = $count->spam;
  451. } else {
  452. $count = (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam'");
  453. }
  454. wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
  455. }
  456. return $count;
  457. } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
  458. $type = '';
  459. }
  460. return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type = %s", $type ) );
  461. }
  462. // Check connectivity between the WordPress blog and Akismet's servers.
  463. // Returns an associative array of server IP addresses, where the key is the IP address, and value is true (available) or false (unable to connect).
  464. public static function check_server_ip_connectivity() {
  465. $servers = $ips = array();
  466. // Some web hosts may disable this function
  467. if ( function_exists('gethostbynamel') ) {
  468. $ips = gethostbynamel( 'rest.akismet.com' );
  469. if ( $ips && is_array($ips) && count($ips) ) {
  470. $api_key = Akismet::get_api_key();
  471. foreach ( $ips as $ip ) {
  472. $response = Akismet::verify_key( $api_key, $ip );
  473. // even if the key is invalid, at least we know we have connectivity
  474. if ( $response == 'valid' || $response == 'invalid' )
  475. $servers[$ip] = 'connected';
  476. else
  477. $servers[$ip] = $response ? $response : 'unable to connect';
  478. }
  479. }
  480. }
  481. return $servers;
  482. }
  483. // Simpler connectivity check
  484. public static function check_server_connectivity($cache_timeout = 86400) {
  485. $debug = array();
  486. $debug[ 'PHP_VERSION' ] = PHP_VERSION;
  487. $debug[ 'WORDPRESS_VERSION' ] = $GLOBALS['wp_version'];
  488. $debug[ 'AKISMET_VERSION' ] = AKISMET_VERSION;
  489. $debug[ 'AKISMET__PLUGIN_DIR' ] = AKISMET__PLUGIN_DIR;
  490. $debug[ 'SITE_URL' ] = site_url();
  491. $debug[ 'HOME_URL' ] = home_url();
  492. $servers = get_option('akismet_available_servers');
  493. if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false ) {
  494. $servers = self::check_server_ip_connectivity();
  495. update_option('akismet_available_servers', $servers);
  496. update_option('akismet_connectivity_time', time());
  497. }
  498. $response = wp_remote_get( 'http://rest.akismet.com/1.1/test' );
  499. $debug[ 'gethostbynamel' ] = function_exists('gethostbynamel') ? 'exists' : 'not here';
  500. $debug[ 'Servers' ] = $servers;
  501. $debug[ 'Test Connection' ] = $response;
  502. Akismet::log( $debug );
  503. if ( $response && 'connected' == wp_remote_retrieve_body( $response ) )
  504. return true;
  505. return false;
  506. }
  507. // Check the server connectivity and store the available servers in an option.
  508. public static function get_server_connectivity($cache_timeout = 86400) {
  509. return self::check_server_connectivity( $cache_timeout );
  510. }
  511. public static function get_number_spam_waiting() {
  512. global $wpdb;
  513. return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'" );
  514. }
  515. public static function get_page_url( $page = 'config' ) {
  516. $args = array( 'page' => 'akismet-key-config' );
  517. if ( $page == 'stats' )
  518. $args = array( 'page' => 'akismet-key-config', 'view' => 'stats' );
  519. elseif ( $page == 'delete_key' )
  520. $args = array( 'page' => 'akismet-key-config', 'view' => 'start', 'action' => 'delete-key', '_wpnonce' => wp_create_nonce( self::NONCE ) );
  521. $url = add_query_arg( $args, class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'options-general.php' ) );
  522. return $url;
  523. }
  524. public static function get_akismet_user( $api_key ) {
  525. $akismet_user = Akismet::http_post( Akismet::build_query( array( 'key' => $api_key ) ), 'get-subscription' );
  526. if ( ! empty( $akismet_user[1] ) )
  527. $akismet_user = json_decode( $akismet_user[1] );
  528. else
  529. $akismet_user = false;
  530. return $akismet_user;
  531. }
  532. public static function get_stats( $api_key ) {
  533. $stat_totals = array();
  534. foreach( array( '6-months', 'all' ) as $interval ) {
  535. $response = Akismet::http_post( Akismet::build_query( array( 'blog' => urlencode( get_bloginfo('url') ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
  536. if ( ! empty( $response[1] ) ) {
  537. $stat_totals[$interval] = json_decode( $response[1] );
  538. }
  539. }
  540. return $stat_totals;
  541. }
  542. public static function verify_wpcom_key( $api_key, $user_id, $token = '' ) {
  543. $akismet_account = Akismet::http_post( Akismet::build_query( array(
  544. 'user_id' => $user_id,
  545. 'api_key' => $api_key,
  546. 'token' => $token,
  547. 'get_account_type' => 'true'
  548. ) ), 'verify-wpcom-key' );
  549. if ( ! empty( $akismet_account[1] ) )
  550. $akismet_account = json_decode( $akismet_account[1] );
  551. Akismet::log( compact( 'akismet_account' ) );
  552. return $akismet_account;
  553. }
  554. public static function display_alert() {
  555. Akismet::view( 'notice', array(
  556. 'type' => 'alert',
  557. 'code' => (int) get_option( 'akismet_alert_code' ),
  558. 'msg' => get_option( 'akismet_alert_msg' )
  559. ) );
  560. }
  561. public static function display_spam_check_warning() {
  562. Akismet::fix_scheduled_recheck();
  563. if ( wp_next_scheduled('akismet_schedule_cron_recheck') > time() && self::get_number_spam_waiting() > 0 ) {
  564. $link_text = apply_filters( 'akismet_spam_check_warning_link_text', sprintf( __( 'Please check your <a href="%s">Akismet configuration</a> and contact your web host if problems persist.', 'akismet'), esc_url( self::get_page_url() ) ) );
  565. Akismet::view( 'notice', array( 'type' => 'spam-check', 'link_text' => $link_text ) );
  566. }
  567. }
  568. public static function display_invalid_version() {
  569. Akismet::view( 'notice', array( 'type' => 'version' ) );
  570. }
  571. public static function display_api_key_warning() {
  572. Akismet::view( 'notice', array( 'type' => 'plugin' ) );
  573. }
  574. public static function display_page() {
  575. if ( !Akismet::get_api_key() || ( isset( $_GET['view'] ) && $_GET['view'] == 'start' ) )
  576. self::display_start_page();
  577. elseif ( isset( $_GET['view'] ) && $_GET['view'] == 'stats' )
  578. self::display_stats_page();
  579. else
  580. self::display_configuration_page();
  581. }
  582. public static function display_start_page() {
  583. if ( isset( $_GET['action'] ) ) {
  584. if ( $_GET['action'] == 'delete-key' ) {
  585. if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], self::NONCE ) )
  586. delete_option( 'wordpress_api_key' );
  587. }
  588. }
  589. if ( $api_key = Akismet::get_api_key() ) {
  590. self::display_configuration_page();
  591. return;
  592. }
  593. //the user can choose to auto connect their API key by clicking a button on the akismet done page
  594. //if jetpack, get verified api key by using connected wpcom user id
  595. //if no jetpack, get verified api key by using an akismet token
  596. $akismet_user = false;
  597. if ( isset( $_GET['token'] ) && preg_match('/^(\d+)-[0-9a-f]{20}$/', $_GET['token'] ) )
  598. $akismet_user = self::verify_wpcom_key( '', '', $_GET['token'] );
  599. elseif ( $jetpack_user = self::get_jetpack_user() )
  600. $akismet_user = self::verify_wpcom_key( $jetpack_user['api_key'], $jetpack_user['user_id'] );
  601. if ( isset( $_GET['action'] ) ) {
  602. if ( $_GET['action'] == 'save-key' ) {
  603. if ( is_object( $akismet_user ) ) {
  604. self::save_key( $akismet_user->api_key );
  605. self::display_notice();
  606. self::display_configuration_page();
  607. return;
  608. }
  609. }
  610. }
  611. echo '<h2 class="ak-header">'.esc_html__('Akismet', 'akismet').'</h2>';
  612. self::display_status();
  613. Akismet::view( 'start', compact( 'akismet_user' ) );
  614. }
  615. public static function display_stats_page() {
  616. Akismet::view( 'stats' );
  617. }
  618. public static function display_configuration_page() {
  619. $api_key = Akismet::get_api_key();
  620. $akismet_user = self::get_akismet_user( $api_key );
  621. $stat_totals = self::get_stats( $api_key );
  622. // If unset, create the new strictness option using the old discard option to determine its default
  623. if ( get_option( 'akismet_strictness' ) === false )
  624. add_option( 'akismet_strictness', (get_option('akismet_discard_month') === 'true' ? '1' : '0') );
  625. if ( empty( self::$notices ) ) {
  626. //show status
  627. if ( ! empty( $stat_totals['all'] ) && isset( $stat_totals['all']->time_saved ) && $akismet_user->status == 'active' && $akismet_user->account_type == 'free-api-key' ) {
  628. $time_saved = false;
  629. if ( $stat_totals['all']->time_saved > 1800 ) {
  630. $total_in_minutes = round( $stat_totals['all']->time_saved / 60 );
  631. $total_in_hours = round( $total_in_minutes / 60 );
  632. $total_in_days = round( $total_in_hours / 8 );
  633. $cleaning_up = __( 'Cleaning up spam takes time.' , 'akismet');
  634. if ( $total_in_days > 1 )
  635. $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %s days!' , 'akismet'), number_format_i18n( $total_in_days ) );
  636. elseif ( $total_in_hours > 1 )
  637. $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %d hours!' , 'akismet'), $total_in_hours );
  638. elseif ( $total_in_minutes >= 30 )
  639. $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %d minutes!' , 'akismet'), $total_in_minutes );
  640. }
  641. Akismet::view( 'notice', array( 'type' => 'active-notice', 'time_saved' => $time_saved ) );
  642. }
  643. if ( !empty( $akismet_user->limit_reached ) && in_array( $akismet_user->limit_reached, array( 'yellow', 'red' ) ) ) {
  644. Akismet::view( 'notice', array( 'type' => 'limit-reached', 'level' => $akismet_user->limit_reached ) );
  645. }
  646. }
  647. if ( !isset( self::$notices['status'] ) && in_array( $akismet_user->status, array( 'cancelled', 'suspended', 'missing', 'no-sub' ) ) )
  648. Akismet::view( 'notice', array( 'type' => $akismet_user->status ) );
  649. Akismet::log( compact( 'stat_totals', 'akismet_user' ) );
  650. Akismet::view( 'config', compact( 'api_key', 'akismet_user', 'stat_totals' ) );
  651. }
  652. public static function display_notice() {
  653. global $hook_suffix;
  654. if ( in_array( $hook_suffix, array( 'jetpack_page_akismet-key-config', 'settings_page_akismet-key-config', 'edit-comments.php' ) ) && (int) get_option( 'akismet_alert_code' ) > 0 ) {
  655. self::display_alert();
  656. }
  657. elseif ( $hook_suffix == 'plugins.php' && !Akismet::get_api_key() ) {
  658. self::display_api_key_warning();
  659. }
  660. elseif ( $hook_suffix == 'edit-comments.php' && wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
  661. self::display_spam_check_warning();
  662. }
  663. elseif ( in_array( $hook_suffix, array( 'jetpack_page_akismet-key-config', 'settings_page_akismet-key-config' ) ) && Akismet::get_api_key() ) {
  664. self::display_status();
  665. }
  666. }
  667. public static function display_status() {
  668. $type = '';
  669. if ( !self::get_server_connectivity() )
  670. $type = 'servers-be-down';
  671. if ( !empty( $type ) )
  672. Akismet::view( 'notice', compact( 'type' ) );
  673. elseif ( !empty( self::$notices ) ) {
  674. foreach ( self::$notices as $type )
  675. Akismet::view( 'notice', compact( 'type' ) );
  676. }
  677. }
  678. private static function get_jetpack_user() {
  679. if ( !class_exists('Jetpack') )
  680. return false;
  681. Jetpack::load_xml_rpc_client();
  682. $xml = new Jetpack_IXR_ClientMulticall( array( 'user_id' => get_current_user_id() ) );
  683. $xml->addCall( 'wpcom.getUserID' );
  684. $xml->addCall( 'akismet.getAPIKey' );
  685. $xml->query();
  686. Akismet::log( compact( 'xml' ) );
  687. if ( !$xml->isError() ) {
  688. $responses = $xml->getResponse();
  689. if ( count( $responses ) > 1 ) {
  690. $api_key = array_shift( $responses[0] );
  691. $user_id = (int) array_shift( $responses[1] );
  692. return compact( 'api_key', 'user_id' );
  693. }
  694. }
  695. return false;
  696. }
  697. /**
  698. * Some commentmeta isn't useful in an export file. Suppress it (when supported).
  699. *
  700. * @param bool $exclude
  701. * @param string $key The meta key
  702. * @param object $meta The meta object
  703. * @return bool Whether to exclude this meta entry from the export.
  704. */
  705. public static function exclude_commentmeta_from_export( $exclude, $key, $meta ) {
  706. if ( in_array( $key, array( 'akismet_as_submitted', 'akismet_rechecking', 'akismet_delayed_moderation_email' ) ) ) {
  707. return true;
  708. }
  709. return $exclude;
  710. }
  711. }