[ Root ] [ Search ] [ Index ]

PHP Cross Reference of WordPress 3.0.1

Provided by Yoast

title

Body

[close]

/wp-content/plugins/akismet/ -> admin.php (source)

   1  <?php
   2  add_action( 'admin_menu', 'akismet_config_page' );
   3  add_action( 'admin_menu', 'akismet_stats_page' );
   4  akismet_admin_warnings();
   5  
   6  function akismet_admin_init() {
   7      global $wp_version;
   8      
   9      // all admin functions are disabled in old versions
  10      if ( version_compare( $wp_version, '3.0', '<' ) ) {
  11          
  12          function akismet_version_warning() {
  13              echo "
  14              <div id='akismet-warning' class='updated fade'><p><strong>".sprintf(__('Akismet %s required WordPress 3.0 or higher.'), AKISMET_VERSION) ."</strong> ".sprintf(__('Please <a href="%s">upgrade WordPress</a> to a current version, or <a href="%s">downgrade to version 2.4 of the Akismet plugin</a>.'), 'http://codex.wordpress.org/Upgrading_WordPress', 'http://wordpress.org/extend/plugins/akismet/download/'). "</p></div>
  15              ";
  16          }
  17          add_action('admin_notices', 'akismet_version_warning'); 
  18          
  19          return; 
  20      }
  21  
  22      if ( function_exists( 'get_plugin_page_hook' ) )
  23          $hook = get_plugin_page_hook( 'akismet-stats-display', 'index.php' );
  24      else
  25          $hook = 'dashboard_page_akismet-stats-display';
  26      add_action('admin_head-'.$hook, 'akismet_stats_script');
  27      add_meta_box('akismet-status', __('Akismet Status'), 'akismet_comment_status_meta_box', 'comment', 'normal');
  28  }
  29  add_action('admin_init', 'akismet_admin_init');
  30  
  31  function akismet_nonce_field($action = -1) { return wp_nonce_field($action); }
  32  $akismet_nonce = 'akismet-update-key';
  33  
  34  function akismet_config_page() {
  35      if ( function_exists('add_submenu_page') )
  36          add_submenu_page('plugins.php', __('Akismet Configuration'), __('Akismet Configuration'), 'manage_options', 'akismet-key-config', 'akismet_conf');
  37  
  38  }
  39  
  40  function akismet_conf() {
  41      global $akismet_nonce, $wpcom_api_key;
  42  
  43      if ( isset($_POST['submit']) ) {
  44          if ( function_exists('current_user_can') && !current_user_can('manage_options') )
  45              die(__('Cheatin&#8217; uh?'));
  46  
  47          check_admin_referer( $akismet_nonce );
  48          $key = preg_replace( '/[^a-h0-9]/i', '', $_POST['key'] );
  49  
  50          if ( empty($key) ) {
  51              $key_status = 'empty';
  52              $ms[] = 'new_key_empty';
  53              delete_option('wordpress_api_key');
  54          } else {
  55              $key_status = akismet_verify_key( $key );
  56          }
  57  
  58          if ( $key_status == 'valid' ) {
  59              update_option('wordpress_api_key', $key);
  60              $ms[] = 'new_key_valid';
  61          } else if ( $key_status == 'invalid' ) {
  62              $ms[] = 'new_key_invalid';
  63          } else if ( $key_status == 'failed' ) {
  64              $ms[] = 'new_key_failed';
  65          }
  66  
  67          if ( isset( $_POST['akismet_discard_month'] ) )
  68              update_option( 'akismet_discard_month', 'true' );
  69          else
  70              update_option( 'akismet_discard_month', 'false' );
  71      } elseif ( isset($_POST['check']) ) {
  72          akismet_get_server_connectivity(0);
  73      }
  74  
  75      if ( empty( $key_status) ||  $key_status != 'valid' ) {
  76          $key = get_option('wordpress_api_key');
  77          if ( empty( $key ) ) {
  78              if ( empty( $key_status ) || $key_status != 'failed' ) {
  79                  if ( akismet_verify_key( '1234567890ab' ) == 'failed' )
  80                      $ms[] = 'no_connection';
  81                  else
  82                      $ms[] = 'key_empty';
  83              }
  84              $key_status = 'empty';
  85          } else {
  86              $key_status = akismet_verify_key( $key );
  87          }
  88          if ( $key_status == 'valid' ) {
  89              $ms[] = 'key_valid';
  90          } else if ( $key_status == 'invalid' ) {
  91              delete_option('wordpress_api_key');
  92              $ms[] = 'key_empty';
  93          } else if ( !empty($key) && $key_status == 'failed' ) {
  94              $ms[] = 'key_failed';
  95          }
  96      }
  97  
  98      $messages = array(
  99          'new_key_empty' => array('color' => 'aa0', 'text' => __('Your key has been cleared.')),
 100          'new_key_valid' => array('color' => '2d2', 'text' => __('Your key has been verified. Happy blogging!')),
 101          'new_key_invalid' => array('color' => 'd22', 'text' => __('The key you entered is invalid. Please double-check it.')),
 102          'new_key_failed' => array('color' => 'd22', 'text' => __('The key you entered could not be verified because a connection to akismet.com could not be established. Please check your server configuration.')),
 103          'no_connection' => array('color' => 'd22', 'text' => __('There was a problem connecting to the Akismet server. Please check your server configuration.')),
 104          'key_empty' => array('color' => 'aa0', 'text' => sprintf(__('Please enter an API key. (<a href="%s" style="color:#fff">Get your key.</a>)'), 'http://akismet.com/get/')),
 105          'key_valid' => array('color' => '2d2', 'text' => __('This key is valid.')),
 106          'key_failed' => array('color' => 'aa0', 'text' => __('The key below was previously validated but a connection to akismet.com can not be established at this time. Please check your server configuration.')));
 107  ?>
 108  <?php if ( !empty($_POST['submit'] ) ) : ?>
 109  <div id="message" class="updated fade"><p><strong><?php _e('Options saved.') ?></strong></p></div>
 110  <?php endif; ?>
 111  <div class="wrap">
 112  <h2><?php _e('Akismet Configuration'); ?></h2>
 113  <div class="narrow">
 114  <form action="" method="post" id="akismet-conf" style="margin: auto; width: 400px; ">
 115  <?php if ( !$wpcom_api_key ) { ?>
 116      <p><?php printf(__('For many people, <a href="%1$s">Akismet</a> will greatly reduce or even completely eliminate the comment and trackback spam you get on your site. If one does happen to get through, simply mark it as "spam" on the moderation screen and Akismet will learn from the mistakes. If you don\'t have an API key yet, you can get one at <a href="%2$s">Akismet.com</a>.'), 'http://akismet.com/', 'http://akismet.com/get/'); ?></p>
 117  
 118  <h3><label for="key"><?php _e('Akismet API Key'); ?></label></h3>
 119  <?php foreach ( $ms as $m ) : ?>
 120      <p style="padding: .5em; background-color: #<?php echo $messages[$m]['color']; ?>; color: #fff; font-weight: bold;"><?php echo $messages[$m]['text']; ?></p>
 121  <?php endforeach; ?>
 122  <p><input id="key" name="key" type="text" size="15" maxlength="12" value="<?php echo get_option('wordpress_api_key'); ?>" style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;" /> (<?php _e('<a href="http://akismet.com/get/">What is this?</a>'); ?>)</p>
 123  <?php if ( isset( $invalid_key) && $invalid_key ) { ?>
 124  <h3><?php _e('Why might my key be invalid?'); ?></h3>
 125  <p><?php _e('This can mean one of two things, either you copied the key wrong or that the plugin is unable to reach the Akismet servers, which is most often caused by an issue with your web host around firewalls or similar.'); ?></p>
 126  <?php } ?>
 127  <?php } ?>
 128  <?php akismet_nonce_field($akismet_nonce) ?>
 129  <p><label><input name="akismet_discard_month" id="akismet_discard_month" value="true" type="checkbox" <?php if ( get_option('akismet_discard_month') == 'true' ) echo ' checked="checked" '; ?> /> <?php _e('Automatically discard spam comments on posts older than a month.'); ?></label></p>
 130      <p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
 131  </form>
 132  
 133  <form action="" method="post" id="akismet-connectivity" style="margin: auto; width: 400px; ">
 134  
 135  <h3><?php _e('Server Connectivity'); ?></h3>
 136  <?php
 137      if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') ) {
 138          ?>
 139              <p style="padding: .5em; background-color: #d22; color: #fff; font-weight:bold;"><?php _e('Network functions are disabled.'); ?></p>
 140              <p><?php echo sprintf( __('Your web host or server administrator has disabled PHP\'s <code>fsockopen</code> or <code>gethostbynamel</code> functions.  <strong>Akismet cannot work correctly until this is fixed.</strong>  Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet\'s system requirements</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
 141          <?php
 142      } else {
 143          $servers = akismet_get_server_connectivity();
 144          $fail_count = count($servers) - count( array_filter($servers) );
 145          if ( is_array($servers) && count($servers) > 0 ) {
 146              // some connections work, some fail
 147              if ( $fail_count > 0 && $fail_count < count($servers) ) { ?>
 148                  <p style="padding: .5em; background-color: #aa0; color: #fff; font-weight:bold;"><?php _e('Unable to reach some Akismet servers.'); ?></p>
 149                  <p><?php echo sprintf( __('A network problem or firewall is blocking some connections from your web server to Akismet.com.  Akismet is working but this may cause problems during times of network congestion.  Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
 150              <?php
 151              // all connections fail
 152              } elseif ( $fail_count > 0 ) { ?>
 153                  <p style="padding: .5em; background-color: #d22; color: #fff; font-weight:bold;"><?php _e('Unable to reach any Akismet servers.'); ?></p>
 154                  <p><?php echo sprintf( __('A network problem or firewall is blocking all connections from your web server to Akismet.com.  <strong>Akismet cannot work correctly until this is fixed.</strong>  Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
 155              <?php
 156              // all connections work
 157              } else { ?>
 158                  <p style="padding: .5em; background-color: #2d2; color: #fff; font-weight:bold;"><?php  _e('All Akismet servers are available.'); ?></p>
 159                  <p><?php _e('Akismet is working correctly.  All servers are accessible.'); ?></p>
 160              <?php
 161              }
 162          } else {
 163              ?>
 164                  <p style="padding: .5em; background-color: #d22; color: #fff; font-weight:bold;"><?php _e('Unable to find Akismet servers.'); ?></p>
 165                  <p><?php echo sprintf( __('A DNS problem or firewall is preventing all access from your web server to Akismet.com.  <strong>Akismet cannot work correctly until this is fixed.</strong>  Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
 166              <?php
 167          }
 168      }
 169      
 170      if ( !empty($servers) ) {
 171  ?>
 172  <table style="width: 100%;">
 173  <thead><th><?php _e('Akismet server'); ?></th><th><?php _e('Network Status'); ?></th></thead>
 174  <tbody>
 175  <?php
 176          asort($servers);
 177          foreach ( $servers as $ip => $status ) {
 178              $color = ( $status ? '#2d2' : '#d22');
 179      ?>
 180          <tr>
 181          <td><?php echo htmlspecialchars($ip); ?></td>
 182          <td style="padding: 0 .5em; font-weight:bold; color: #fff; background-color: <?php echo $color; ?>"><?php echo ($status ? __('No problems') : __('Obstructed') ); ?></td>
 183          
 184      <?php
 185          }
 186      }
 187  ?>
 188  </tbody>
 189  </table>
 190      <p><?php if ( get_option('akismet_connectivity_time') ) echo sprintf( __('Last checked %s ago.'), human_time_diff( get_option('akismet_connectivity_time') ) ); ?></p>
 191      <p class="submit"><input type="submit" name="check" value="<?php _e('Check network status &raquo;'); ?>" /></p>
 192  </form>
 193  
 194  </div>
 195  </div>
 196  <?php
 197  }
 198  
 199  function akismet_stats_page() {
 200      if ( function_exists('add_submenu_page') )
 201          add_submenu_page('index.php', __('Akismet Stats'), __('Akismet Stats'), 'manage_options', 'akismet-stats-display', 'akismet_stats_display');
 202  
 203  }
 204  
 205  function akismet_stats_script() {
 206      ?>
 207  <script type="text/javascript">
 208  function resizeIframe() {
 209      var height = document.documentElement.clientHeight;
 210      height -= document.getElementById('akismet-stats-frame').offsetTop;
 211      height += 100; // magic padding
 212      
 213      document.getElementById('akismet-stats-frame').style.height = height +"px";
 214      
 215  };
 216  function resizeIframeInit() {
 217      document.getElementById('akismet-stats-frame').onload = resizeIframe;
 218      window.onresize = resizeIframe;
 219  }
 220  addLoadEvent(resizeIframeInit);
 221  </script><?php
 222  }
 223  
 224  
 225  function akismet_stats_display() {
 226      global $akismet_api_host, $akismet_api_port, $wpcom_api_key;
 227      $blog = urlencode( get_option('home') );
 228  
 229      $url = 'http://';
 230      if ( is_ssl() )
 231          $url = 'https://';
 232  
 233      $url .= 'akismet.com/web/1.0/user-stats.php';
 234      $url .= "?blog={$blog}&api_key=" . akismet_get_key();
 235      ?>
 236      <div class="wrap">
 237      <iframe src="<?php echo $url; ?>" width="100%" height="100%" frameborder="0" id="akismet-stats-frame"></iframe>
 238      </div>
 239      <?php
 240  }
 241  
 242  function akismet_stats() {
 243      if ( !function_exists('did_action') || did_action( 'rightnow_end' ) ) // We already displayed this info in the "Right Now" section
 244          return;
 245      if ( !$count = get_option('akismet_spam_count') )
 246          return;
 247      $path = plugin_basename(__FILE__);
 248      echo '<h3>'.__('Spam').'</h3>';
 249      global $submenu;
 250      if ( isset( $submenu['edit-comments.php'] ) )
 251          $link = 'edit-comments.php';
 252      else
 253          $link = 'edit.php';
 254      echo '<p>'.sprintf(__('<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.'), 'http://akismet.com/', clean_url("$link?page=akismet-admin"), number_format_i18n($count) ).'</p>';
 255  }
 256  add_action('activity_box_end', 'akismet_stats');
 257  
 258  function akismet_admin_warnings() {
 259      global $wpcom_api_key;
 260      if ( !get_option('wordpress_api_key') && !$wpcom_api_key && !isset($_POST['submit']) ) {
 261  		function akismet_warning() {
 262              echo "
 263              <div id='akismet-warning' class='updated fade'><p><strong>".__('Akismet is almost ready.')."</strong> ".sprintf(__('You must <a href="%1$s">enter your Akismet API key</a> for it to work.'), "plugins.php?page=akismet-key-config")."</p></div>
 264              ";
 265          }
 266          add_action('admin_notices', 'akismet_warning');
 267          return;
 268      } elseif ( get_option('akismet_connectivity_time') && empty($_POST) && is_admin() && !akismet_server_connectivity_ok() ) {
 269  		function akismet_warning() {
 270              echo "
 271              <div id='akismet-warning' class='updated fade'><p><strong>".__('Akismet has detected a problem.')."</strong> ".sprintf(__('A server or network problem is preventing Akismet from working correctly.  <a href="%1$s">Click here for more information</a> about how to fix the problem.'), "plugins.php?page=akismet-key-config")."</p></div>
 272              ";
 273          }
 274          add_action('admin_notices', 'akismet_warning');
 275          return;
 276      }
 277  }
 278  
 279  // FIXME placeholder
 280  
 281  function akismet_comment_row_action( $a, $comment ) {
 282      
 283      
 284      $akismet_result = get_comment_meta( $comment->comment_ID, 'akismet_result', true );
 285      $user_result = get_comment_meta( $comment->comment_ID, 'akismet_user_result', true);
 286      $desc = null;
 287      if ( !$user_result || $user_result == $akismet_result ) {
 288          // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same
 289          if ( $akismet_result == 'true' )
 290              $desc = 'Flagged as spam by Akismet';
 291          elseif ( $akismet_result == 'false' )
 292              $desc = 'Cleared by Akismet';
 293      } else {
 294          $who = get_comment_meta( $comment->comment_ID, 'akismet_user', true );
 295          if ( $user_result == 'true' )
 296              $desc = sprintf( __('Flagged as spam by %s'), $who );
 297          else
 298              $desc = sprintf( __('Un-spammed by %s'), $who );
 299      }
 300      
 301      if ( $desc )
 302          echo '<span style="background: #EEE; border: 1px solid #E4E4E4; margin-top: 3px; color: #999; padding: 1px 8px 2px 8px; -moz-border-radius:6px; border-radius:6px; -webkit-border-radius:6px; float: right; line-height: 1.2em;"><a href="comment.php?action=editcomment&amp;c='.$comment->comment_ID.'#akismet-status" title="' . esc_attr__( 'View comment history' ) . '">'.htmlspecialchars($desc).'</a></span>';
 303      
 304      return $a;
 305  }
 306  
 307  add_filter( 'comment_row_actions', 'akismet_comment_row_action', 10, 2 );
 308  
 309  
 310  function akismet_comment_status_meta_box($comment) {
 311      $history = akismet_get_comment_history( $comment->comment_ID );
 312  
 313      if ( $history ) {
 314          echo '<div class="akismet-history" style="margin: 13px;">';
 315          foreach ( $history as $row ) {
 316              $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
 317              echo '<div style="margin-bottom: 13px;"><span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( __('%s ago'), human_time_diff( $row['time'] ) ) . '</span> - ';
 318              echo htmlspecialchars( $row['message'] ) . '</div>';
 319          }
 320          
 321          echo '</div>';
 322  
 323      }
 324  }
 325  
 326  
 327  // add an extra column header to the comments screen
 328  function akismet_comments_columns( $columns ) {
 329      $columns[ 'akismet' ] = __( 'Akismet' );
 330      return $columns;
 331  }
 332  
 333  #add_filter( 'manage_edit-comments_columns', 'akismet_comments_columns' );
 334  
 335  // Show stuff in the extra column
 336  function akismet_comment_column_row( $column, $comment_id ) {
 337      if ( $column != 'akismet' )
 338          return;
 339          
 340      $history = akismet_get_comment_history( $comment_id );
 341      
 342      if ( $history ) {
 343          echo '<dl class="akismet-history">';
 344          foreach ( $history as $row ) {
 345              echo '<dt>' . sprintf( __('%s ago'), human_time_diff( $row['time'] ) ) . '</dt>';
 346              echo '<dd>' . htmlspecialchars( $row['message'] ) . '</dd>';
 347          }
 348          
 349          echo '</dl>';
 350      }
 351  }
 352  
 353  #add_action( 'manage_comments_custom_column', 'akismet_comment_column_row', 10, 2 );
 354  
 355  // END FIXME
 356  
 357  // WP 2.5+
 358  function akismet_rightnow() {
 359      global $submenu, $wp_db_version;
 360  
 361      $plural_func = '__ngettext';
 362      if ( function_exists( '_n' ) )
 363          $plural_func = '_n';
 364  
 365      // clean_url was deprecated in WP 3.0
 366      $esc_url = 'clean_url';
 367      if ( function_exists( 'esc_url' ) )
 368          $esc_url = 'esc_url';
 369  
 370      if ( 8645 < $wp_db_version  ) // 2.7
 371          $link = 'edit-comments.php?comment_status=spam';
 372      elseif ( isset( $submenu['edit-comments.php'] ) )
 373          $link = 'edit-comments.php?page=akismet-admin';
 374      else
 375          $link = 'edit.php?page=akismet-admin';
 376  
 377      if ( $count = get_option('akismet_spam_count') ) {
 378          $intro = sprintf( $plural_func(
 379              '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already,',
 380              '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already,',
 381              $count
 382          ), 'http://akismet.com/', number_format_i18n( $count ) );
 383      } else {
 384          $intro = sprintf( __('<a href="%1$s">Akismet</a> blocks spam from getting to your blog,'), 'http://akismet.com/' );
 385      }
 386  
 387      if ( $queue_count = akismet_spam_count() ) {
 388          $queue_text = sprintf( $plural_func(
 389              'and there\'s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
 390              'and there are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
 391              $queue_count
 392          ), number_format_i18n( $queue_count ), $esc_url($link) );
 393      } else {
 394          $queue_text = sprintf( __( " but there's nothing in your <a href='%1\$s'>spam queue</a> at the moment." ), $esc_url($link) );
 395      }
 396  
 397      // _c was deprecated in WP 2.9.0
 398      if ( function_exists( '_x' ) )
 399          $text = sprintf( _x( '%1$s%2$s', 'akismet_rightnow' ), $intro, $queue_text );
 400      else 
 401          $text = sprintf( _c( '%1$s%2$s|akismet_rightnow' ), $intro, $queue_text );
 402  
 403      echo "<p class='akismet-right-now'>$text</p>\n";
 404  }
 405      
 406  add_action('rightnow_end', 'akismet_rightnow');
 407  
 408  
 409  // For WP >= 2.5
 410  function akismet_check_for_spam_button($comment_status) {
 411      if ( 'approved' == $comment_status )
 412          return;
 413      if ( function_exists('plugins_url') )
 414          $link = 'admin.php?action=akismet_recheck_queue';
 415      else
 416          $link = 'edit-comments.php?page=akismet-admin&amp;recheckqueue=true&amp;noheader=true';
 417      echo "</div><div class='alignleft'><a class='button-secondary checkforspam' href='$link'>" . __('Check for Spam') . "</a>";
 418  }
 419  add_action('manage_comments_nav', 'akismet_check_for_spam_button');
 420  
 421  function akismet_submit_nonspam_comment ( $comment_id ) {
 422      global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
 423      $comment_id = (int) $comment_id;
 424  
 425      $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
 426      if ( !$comment ) // it was deleted
 427          return;
 428      $comment->blog = get_option('home');
 429      $comment->blog_lang = get_locale();
 430      $comment->blog_charset = get_option('blog_charset');
 431      $comment->permalink = get_permalink($comment->comment_post_ID);
 432      if ( is_object($current_user) ) {
 433          $comment->reporter = $current_user->user_login;
 434      }
 435      if ( is_object($current_site) ) {
 436          $comment->site_domain = $current_site->domain;
 437      }
 438  
 439      $comment->user_role = '';
 440      if ( isset( $comment->user_ID ) )
 441          $comment->user_role = akismet_get_user_roles($comment->user_ID);
 442  
 443      $query_string = '';
 444      foreach ( $comment as $key => $data )
 445          $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
 446  
 447      $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-ham", $akismet_api_port);
 448      if ( $comment->reporter ) {
 449          akismet_update_comment_history( $comment_id, sprintf( __('%s un-spammed this comment'), $comment->reporter ), 'report-ham' );
 450          update_comment_meta( $comment_id, 'akismet_user_result', 'false' );
 451          update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
 452      } else {
 453          akismet_update_comment_history( $comment_id, 'A plugin un-spammed this comment', 'report-ham' );
 454      }
 455      
 456      do_action('akismet_submit_nonspam_comment', $comment_id, $response[1]);
 457  }
 458  
 459  function akismet_submit_spam_comment ( $comment_id ) {
 460      global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
 461      $comment_id = (int) $comment_id;
 462  
 463      $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
 464      if ( !$comment ) // it was deleted
 465          return;
 466      if ( 'spam' != $comment->comment_approved )
 467          return;
 468      $comment->blog = get_option('home');
 469      $comment->blog_lang = get_locale();
 470      $comment->blog_charset = get_option('blog_charset');
 471      $comment->permalink = get_permalink($comment->comment_post_ID);
 472      if ( is_object($current_user) ) {
 473          $comment->reporter = $current_user->user_login;
 474      }
 475      if ( is_object($current_site) ) {
 476          $comment->site_domain = $current_site->domain;
 477      }
 478  
 479      $comment->user_role = '';
 480      if ( !isset( $comment->user_id ) )
 481          $comment->user_role = akismet_get_user_roles($comment->user_ID);
 482  
 483      $query_string = '';
 484      foreach ( $comment as $key => $data )
 485          $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
 486  
 487      $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-spam", $akismet_api_port);
 488      if ( $comment->reporter ) {
 489          akismet_update_comment_history( $comment_id, sprintf( __('%s spammed this comment'), $comment->reporter ), 'report-spam' );
 490          update_comment_meta( $comment_id, 'akismet_user_result', 'true' );
 491          update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
 492      } else
 493          akismet_update_comment_history( $comment_id, 'A plugin spammed this comment', 'report-ham' );
 494      do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
 495  }
 496  
 497  // For WP 2.7+
 498  function akismet_transition_comment_status( $new_status, $old_status, $comment ) {
 499      if ( $new_status == $old_status )
 500          return;
 501          
 502      if ( $new_status == 'spam' ) {
 503          akismet_submit_spam_comment( $comment->comment_ID );
 504      } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) {
 505          akismet_submit_nonspam_comment( $comment->comment_ID );
 506      }
 507  }
 508  
 509  add_action( 'transition_comment_status', 'akismet_transition_comment_status', 10, 3 );
 510  
 511  // Total spam in queue
 512  // get_option( 'akismet_spam_count' ) is the total caught ever
 513  function akismet_spam_count( $type = false ) {
 514      global $wpdb;
 515  
 516      if ( !$type ) { // total
 517          $count = wp_cache_get( 'akismet_spam_count', 'widget' );
 518          if ( false === $count ) {
 519              if ( function_exists('wp_count_comments') ) {
 520                  $count = wp_count_comments();
 521                  $count = $count->spam;
 522              } else {
 523                  $count = (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'");
 524              }
 525              wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
 526          }
 527          return $count;
 528      } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
 529          $type = '';
 530      } else { // pingback, trackback, ...
 531          $type  = $wpdb->escape( $type );
 532      }
 533  
 534      return (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type='$type'");
 535  }
 536  
 537  
 538  function akismet_recheck_queue() {
 539      global $wpdb, $akismet_api_host, $akismet_api_port;
 540  
 541      if ( ! ( isset( $_GET['recheckqueue'] ) || ( isset( $_REQUEST['action'] ) && 'akismet_recheck_queue' == $_REQUEST['action'] ) ) )
 542          return;
 543          
 544      $moderation = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = '0'", ARRAY_A );
 545      foreach ( (array) $moderation as $c ) {
 546          $c['user_ip']    = $c['comment_author_IP'];
 547          $c['user_agent'] = $c['comment_agent'];
 548          $c['referrer']   = '';
 549          $c['blog']       = get_option('home');
 550          $c['blog_lang']  = get_locale();
 551          $c['blog_charset'] = get_option('blog_charset');
 552          $c['permalink']  = get_permalink($c['comment_post_ID']);
 553  
 554          $c['user_role'] = '';
 555          if ( isset( $c['user_ID'] ) )
 556              $c['user_role']  = akismet_get_user_roles($c['user_ID']);
 557  
 558          $id = (int) $c['comment_ID'];
 559  
 560          $query_string = '';
 561          foreach ( $c as $key => $data )
 562          $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
 563  
 564          $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
 565          if ( 'true' == $response[1] ) {
 566              wp_set_comment_status($c['comment_ID'], 'spam');
 567              update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
 568              akismet_update_comment_history( $c['comment_ID'], __('Akismet re-checked and caught this comment as spam'), 'check-spam' );
 569          
 570          } elseif ( 'false' == $response[1] ) {
 571              update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' );
 572              akismet_update_comment_history( $c['comment_ID'], __('Akismet re-checked and cleared this comment'), 'check-ham' );
 573          // abnormal result: error
 574          } else {
 575              update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' );
 576              akismet_update_comment_history( $c['comment_ID'], sprintf( __('Akismet was unable to re-check this comment (response: %s)'), $response[1]), 'check-error' );
 577          }
 578  
 579      }
 580      wp_redirect( $_SERVER['HTTP_REFERER'] );
 581      exit;
 582  }
 583  
 584  add_action('admin_action_akismet_recheck_queue', 'akismet_recheck_queue');


Generated: Thu Oct 14 05:12:05 2010 Cross-referenced by PHPXref 0.7