[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/extend/ -> akismet.php (source)

   1  <?php
   2  
   3  /**
   4   * Main bbPress Akismet Class
   5   *
   6   * @package bbPress
   7   * @subpackage Akismet
   8   */
   9  
  10  // Exit if accessed directly
  11  defined( 'ABSPATH' ) || exit;
  12  
  13  if ( ! class_exists( 'BBP_Akismet' ) ) :
  14  /**
  15   * Loads Akismet extension
  16   *
  17   * @since 2.0.0 bbPress (r3277)
  18   *
  19   * @package bbPress
  20   * @subpackage Akismet
  21   */
  22  class BBP_Akismet {
  23  
  24      /**
  25       * The main bbPress Akismet loader
  26       *
  27       * @since 2.0.0 bbPress (r3277)
  28       */
  29  	public function __construct() {
  30          $this->setup_actions();
  31      }
  32  
  33      /**
  34       * Setup the admin hooks
  35       *
  36       * @since 2.0.0 bbPress (r3277)
  37       *
  38       * @access private
  39       */
  40  	private function setup_actions() {
  41  
  42          // Prevent debug notices
  43          $checks = array();
  44  
  45          // bbPress functions to check for spam
  46          $checks['check']  = array(
  47              'bbp_new_topic_pre_insert'  => 1,  // New topic check
  48              'bbp_new_reply_pre_insert'  => 1,  // New reply check
  49              'bbp_edit_topic_pre_insert' => 1,  // Edit topic check
  50              'bbp_edit_reply_pre_insert' => 1   // Edit reply check
  51          );
  52  
  53          // bbPress functions for spam and ham submissions
  54          $checks['submit'] = array(
  55              'bbp_spammed_topic'   => 10, // Spammed topic
  56              'bbp_unspammed_topic' => 10, // Unspammed reply
  57              'bbp_spammed_reply'   => 10, // Spammed reply
  58              'bbp_unspammed_reply' => 10, // Unspammed reply
  59          );
  60  
  61          // Add the checks
  62          foreach ( $checks as $type => $functions ) {
  63              foreach ( $functions as $function => $priority ) {
  64                  add_filter( $function, array( $this, $type . '_post'  ), $priority );
  65              }
  66          }
  67  
  68          // Update post meta
  69          add_action( 'wp_insert_post', array( $this, 'update_post_meta' ), 10, 2 );
  70  
  71          // Cleanup
  72          add_action( 'akismet_scheduled_delete', array( $this, 'delete_old_spam' ) );
  73          add_action( 'akismet_scheduled_delete', array( $this, 'delete_old_spam_meta' ) );
  74          add_action( 'akismet_scheduled_delete', array( $this, 'delete_orphaned_spam_meta' ) );
  75  
  76          // Admin
  77          if ( is_admin() ) {
  78              add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
  79          }
  80      }
  81  
  82      /**
  83       * Converts topic/reply data into Akismet comment checking format
  84       *
  85       * @since 2.0.0 bbPress (r3277)
  86       *
  87       * @param array $post_data
  88       *
  89       * @return array Array of post data
  90       */
  91  	public function check_post( $post_data = array() ) {
  92  
  93          // Define local variables
  94          $user_data = array();
  95          $post_permalink = '';
  96  
  97          // Cast the post_author to 0 if it's empty
  98          if ( empty( $post_data['post_author'] ) ) {
  99              $post_data['post_author'] = 0;
 100          }
 101  
 102          /** Author ************************************************************/
 103  
 104          $user_data['last_active'] = '';
 105          $user_data['registered']  = date( 'Y-m-d H:i:s');
 106          $user_data['total_posts'] = (int) bbp_get_user_post_count( $post_data['post_author'] );
 107  
 108          // Get user data
 109          $userdata       = get_userdata( $post_data['post_author'] );
 110          $anonymous_data = bbp_filter_anonymous_post_data();
 111  
 112          // Author is anonymous
 113          if ( ! bbp_has_errors() ) {
 114              $user_data['name']    = $anonymous_data['bbp_anonymous_name'];
 115              $user_data['email']   = $anonymous_data['bbp_anonymous_email'];
 116              $user_data['website'] = $anonymous_data['bbp_anonymous_website'];
 117  
 118          // Author is logged in
 119          } elseif ( ! empty( $userdata ) ) {
 120              $user_data['name']       = $userdata->display_name;
 121              $user_data['email']      = $userdata->user_email;
 122              $user_data['website']    = $userdata->user_url;
 123              $user_data['registered'] = $userdata->user_registered;
 124  
 125          // Missing author data, so set some empty strings
 126          } else {
 127              $user_data['name']    = '';
 128              $user_data['email']   = '';
 129              $user_data['website'] = '';
 130          }
 131  
 132          /** Post **************************************************************/
 133  
 134          if ( ! empty( $post_data['post_parent'] ) ) {
 135  
 136              // Use post parent for permalink
 137              $post_permalink = get_permalink( $post_data['post_parent'] );
 138  
 139              // Use post parent to get datetime of last reply on this topic
 140              $reply_id = bbp_get_topic_last_reply_id( $post_data['post_parent'] );
 141              if ( ! empty( $reply_id ) ) {
 142                  $user_data['last_active'] = get_post_field( 'post_date', $reply_id );
 143              }
 144          }
 145  
 146          // Pass title & content together into comment content
 147          $_post_content = trim( $post_data['post_title'] . "\n\n" . $post_data['post_content'] );
 148  
 149          // Check if the post data is spammy...
 150          $_post = $this->maybe_spam( array(
 151              'comment_author'                 => $user_data['name'],
 152              'comment_author_email'           => $user_data['email'],
 153              'comment_author_url'             => $user_data['website'],
 154              'comment_content'                => $_post_content,
 155              'comment_post_ID'                => $post_data['post_parent'],
 156              'comment_type'                   => $post_data['post_type'],
 157              'comment_total'                  => $user_data['total_posts'],
 158              'comment_last_active_gmt'        => $user_data['last_active'],
 159              'comment_account_registered_gmt' => $user_data['registered'],
 160              'permalink'                      => $post_permalink,
 161              'referrer'                       => wp_get_raw_referer(),
 162              'user_agent'                     => bbp_current_author_ua(),
 163              'user_ID'                        => $post_data['post_author'],
 164              'user_ip'                        => bbp_current_author_ip(),
 165              'user_role'                      => $this->get_user_roles( $post_data['post_author'] ),
 166          ) );
 167  
 168          // Set the results (from maybe_spam() above)
 169          $post_data['bbp_akismet_result_headers'] = $_post['bbp_akismet_result_headers'];
 170          $post_data['bbp_akismet_result']         = $_post['bbp_akismet_result'];
 171          $post_data['bbp_post_as_submitted']      = $_post;
 172  
 173          // Avoid recursion by unsetting results from post-as-submitted
 174          unset(
 175              $post_data['bbp_post_as_submitted']['bbp_akismet_result_headers'],
 176              $post_data['bbp_post_as_submitted']['bbp_akismet_result']
 177          );
 178  
 179          // Allow post_data to be manipulated
 180          $post_data = apply_filters( 'bbp_akismet_check_post', $post_data );
 181  
 182          // Parse and log the last response
 183          $this->last_post = $this->parse_response( $post_data );
 184  
 185          // Return the last response back to the filter
 186          return $this->last_post;
 187      }
 188  
 189      /**
 190       * Parse the response from the Akismet service, and alter the post data as
 191       * necessary. For example, switch the status to `spam` if spammy.
 192       *
 193       * Note: this method also is responsible for allowing users who can moderate to
 194       * never have their posts marked as spam. This is because they are "trusted"
 195       * users. However, their posts are still sent to Akismet to be checked.
 196       *
 197       * @since 2.6.0 bbPress (r6873)
 198       *
 199       * @param array $post_data
 200       *
 201       * @return array
 202       */
 203  	private function parse_response( $post_data = array() ) {
 204  
 205          // Get the parent ID of the post as submitted
 206          $parent_id = ! empty( $post_data['bbp_post_as_submitted']['comment_post_ID'] )
 207              ? absint( $post_data['bbp_post_as_submitted']['comment_post_ID'] )
 208              : 0;
 209  
 210          // Allow moderators to skip spam (includes per-forum moderators via $parent_id)
 211          $skip_spam = current_user_can( 'moderate', $parent_id );
 212  
 213          // Bail early if current user can skip spam enforcement
 214          if ( apply_filters( 'bbp_bypass_spam_enforcement', $skip_spam, $post_data ) ) {
 215              return $post_data;
 216          }
 217  
 218          // Discard obvious spam
 219          if ( get_option( 'akismet_strictness' ) ) {
 220  
 221              // Akismet is 100% confident this is spam
 222              if (
 223                  ! empty( $post_data['bbp_akismet_result_headers']['x-akismet-pro-tip'] )
 224                  &&
 225                  ( 'discard' === $post_data['bbp_akismet_result_headers']['x-akismet-pro-tip'] )
 226              ) {
 227  
 228                  // URL to redirect to (current, or forum root)
 229                  $redirect_to = ( ! empty( $_SERVER['HTTP_HOST'] ) && ! empty( $_SERVER['REQUEST_URI'] ) )
 230                      ? bbp_get_url_scheme() . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
 231                      : bbp_get_root_url();
 232  
 233                  // Do the redirect (post data not saved!)
 234                  bbp_redirect( $redirect_to );
 235              }
 236          }
 237  
 238          // Result is spam, so set the status as such
 239          if ( 'true' === $post_data['bbp_akismet_result'] ) {
 240  
 241              // Let plugins do their thing
 242              do_action( 'bbp_akismet_spam_caught' );
 243  
 244              // Set post_status to spam
 245              $post_data['post_status'] = bbp_get_spam_status_id();
 246  
 247              // Filter spammy tags into meta data
 248              add_filter( 'bbp_new_reply_pre_set_terms', array( $this, 'filter_post_terms' ), 1, 3 );
 249          }
 250  
 251          // Return the (potentially modified) post data
 252          return $post_data;
 253      }
 254  
 255      /**
 256       * Submit a post for spamming or hamming
 257       *
 258       * @since 2.0.0 bbPress (r3277)
 259       *
 260       * @param int $post_id
 261       *
 262       * @global string $akismet_api_host
 263       * @global string $akismet_api_port
 264       * @global object $current_user
 265       * @global object $current_site
 266       *
 267       * @return array Array of existing topic terms
 268       */
 269  	public function submit_post( $post_id = 0 ) {
 270          global $current_user, $current_site;
 271  
 272          // Innocent until proven guilty
 273          $request_type   = 'ham';
 274          $current_filter = current_filter();
 275  
 276          // Check this filter and adjust the $request_type accordingly
 277          switch ( $current_filter ) {
 278  
 279              // Mysterious, and straight from the can
 280              case 'bbp_spammed_topic' :
 281              case 'bbp_spammed_reply' :
 282                  $request_type = 'spam';
 283                  break;
 284  
 285              // Honey-glazed, a straight off the bone
 286              case 'bbp_unspammed_topic' :
 287              case 'bbp_unspammed_reply' :
 288                  $request_type = 'ham';
 289                  break;
 290  
 291              // Possibly poison...
 292              default :
 293                  return;
 294          }
 295  
 296          // Setup some variables
 297          $post_id = (int) $post_id;
 298  
 299          // Make sure we have a post
 300          $_post = get_post( $post_id );
 301  
 302          // Bail if get_post() fails
 303          if ( empty( $_post ) ) {
 304              return;
 305          }
 306  
 307          // Bail if we're spamming, but the post_status isn't spam
 308          if ( ( 'spam' === $request_type ) && ( bbp_get_spam_status_id() !== $_post->post_status ) ) {
 309              return;
 310          }
 311  
 312          // Pass title & content together into comment content
 313          $_post_content = trim( $_post->post_title . "\n\n" . $_post->post_content );
 314  
 315          // Set some default post_data
 316          $post_data = array(
 317              'comment_approved'     => $_post->post_status,
 318              'comment_author'       => $_post->post_author ? get_the_author_meta( 'display_name', $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_name',    true ),
 319              'comment_author_email' => $_post->post_author ? get_the_author_meta( 'email',        $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_email',   true ),
 320              'comment_author_url'   => $_post->post_author ? bbp_get_user_profile_url(            $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_website', true ),
 321              'comment_content'      => $_post_content,
 322              'comment_date_gmt'     => $_post->post_date_gmt,
 323              'comment_ID'           => $post_id,
 324              'comment_post_ID'      => $_post->post_parent,
 325              'comment_type'         => $_post->post_type,
 326              'permalink'            => get_permalink( $post_id ),
 327              'user_ID'              => $_post->post_author,
 328              'user_ip'              => get_post_meta( $post_id, '_bbp_author_ip', true ),
 329              'user_role'            => $this->get_user_roles( $_post->post_author ),
 330          );
 331  
 332          // Use the original version stored in post_meta if available
 333          $as_submitted = get_post_meta( $post_id, '_bbp_akismet_as_submitted', true );
 334          if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) ) {
 335              $post_data = array_merge( $post_data, $as_submitted );
 336          }
 337  
 338          // Add the reporter IP address
 339          $post_data['reporter_ip']  = bbp_current_author_ip();
 340  
 341          // Add some reporter info
 342          if ( is_object( $current_user ) ) {
 343              $post_data['reporter'] = $current_user->user_login;
 344          }
 345  
 346          // Add the current site domain
 347          if ( is_object( $current_site ) ) {
 348              $post_data['site_domain'] = $current_site->domain;
 349          }
 350  
 351          // Place your slide beneath the microscope
 352          $post_data = $this->maybe_spam( $post_data, 'submit', $request_type );
 353  
 354          // Manual user action
 355          if ( isset( $post_data['reporter'] ) ) {
 356  
 357              // What kind of action
 358              switch ( $request_type ) {
 359  
 360                  // Spammy
 361                  case 'spam' :
 362                      if ( 'topic' === $post_data['comment_type'] ) {
 363                          /* translators: %s: reporter name */
 364                          $message = sprintf( esc_html__( '%s reported this topic as spam', 'bbpress' ),
 365                              $post_data['reporter']
 366                          );
 367                      } elseif ( 'reply' === $post_data['comment_type'] ) {
 368                          /* translators: %s: reporter name */
 369                          $message = sprintf( esc_html__( '%s reported this reply as spam', 'bbpress' ),
 370                              $post_data['reporter']
 371                          );
 372                      } else {
 373                          /* translators: 1: reporter name, 2: comment type */
 374                          $message = sprintf( esc_html__( '%1$s reported this %2$s as spam', 'bbpress' ),
 375                              $post_data['reporter'],
 376                              $post_data['comment_type']
 377                          );
 378                      }
 379  
 380                      $this->update_post_history( $post_id, $message, 'report-spam' );
 381                      update_post_meta( $post_id, '_bbp_akismet_user_result', 'true'                 );
 382                      update_post_meta( $post_id, '_bbp_akismet_user',        $post_data['reporter'] );
 383                      break;
 384  
 385                  // Hammy
 386                  case 'ham' :
 387                      if ( 'topic' === $post_data['comment_type'] ) {
 388                          /* translators: %s: reporter name */
 389                          $message = sprintf( esc_html__( '%s reported this topic as not spam', 'bbpress' ),
 390                              $post_data['reporter']
 391                          );
 392                      } elseif ( 'reply' === $post_data['comment_type'] ) {
 393                          /* translators: %s: reporter name */
 394                          $message = sprintf( esc_html__( '%s reported this reply as not spam', 'bbpress' ),
 395                              $post_data['reporter']
 396                          );
 397                      } else {
 398                          /* translators: 1: reporter name, 2: comment type */
 399                          $message = sprintf( esc_html__( '%1$s reported this %2$s as not spam', 'bbpress' ),
 400                              $post_data['reporter'],
 401                              $post_data['comment_type']
 402                          );
 403                      }
 404  
 405                      $this->update_post_history( $post_id, $message, 'report-ham' );
 406                      update_post_meta( $post_id, '_bbp_akismet_user_result', 'false'                 );
 407                      update_post_meta( $post_id, '_bbp_akismet_user',         $post_data['reporter'] );
 408  
 409                      // @todo Topic term revision history
 410                      break;
 411  
 412                  // Possible other actions
 413                  default :
 414                      break;
 415              }
 416          }
 417  
 418          do_action( 'bbp_akismet_submit_' . $request_type . '_post', $post_id, $post_data['bbp_akismet_result'] );
 419      }
 420  
 421      /**
 422       * Ping Akismet service and check for spam/ham response
 423       *
 424       * @since 2.0.0 bbPress (r3277)
 425       *
 426       * @param array $post_data
 427       * @param string $check Accepts check|submit
 428       * @param string $spam Accepts spam|ham
 429       *
 430       * @global string $akismet_api_host
 431       * @global string $akismet_api_port
 432       *
 433       * @return array Array of post data
 434       */
 435  	private function maybe_spam( $post_data = array(), $check = 'check', $spam = 'spam' ) {
 436          global $akismet_api_host, $akismet_api_port;
 437  
 438          // Define variables
 439          $query_string = $path = '';
 440          $response = array( '', '' );
 441  
 442          // Make sure post data is an array
 443          if ( ! is_array( $post_data ) ) {
 444              $post_data = array();
 445          }
 446  
 447          // Populate post data
 448          $post_data['blog']         = get_option( 'home' );
 449          $post_data['blog_charset'] = get_option( 'blog_charset' );
 450          $post_data['blog_lang']    = get_locale();
 451          $post_data['referrer']     = wp_get_raw_referer();
 452          $post_data['user_agent']   = bbp_current_author_ua();
 453  
 454          // Loop through _POST args and rekey strings
 455          if ( ! empty( $_POST ) && is_countable( $_POST ) ) {
 456              foreach ( $_POST as $key => $value ) {
 457                  if ( is_string( $value ) ) {
 458                      $post_data[ 'POST_' . $key ] = $value;
 459                  }
 460              }
 461          }
 462  
 463          // Loop through _SERVER args and remove allowed keys
 464          if ( ! empty( $_SERVER ) && is_countable( $_SERVER ) ) {
 465  
 466              // Keys to ignore
 467              $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
 468  
 469              foreach ( $_SERVER as $key => $value ) {
 470  
 471                  // Key should not be ignored
 472                  if ( ! in_array( $key, $ignore, true ) && is_string( $value ) ) {
 473                      $post_data[ $key ] = $value;
 474  
 475                  // Key should be ignored
 476                  } else {
 477                      $post_data[ $key ] = '';
 478                  }
 479              }
 480          }
 481  
 482          // Encode post data
 483          if ( ! empty( $post_data ) && is_countable( $post_data ) ) {
 484              foreach ( $post_data as $key => $data ) {
 485                  $query_string .= $key . '=' . urlencode( wp_unslash( $data ) ) . '&';
 486              }
 487          }
 488  
 489          // Only accepts spam|ham
 490          if ( ! in_array( $spam, array( 'spam', 'ham' ), true ) ) {
 491              $spam = 'spam';
 492          }
 493  
 494          // Setup the API route
 495          if ( 'check' === $check ) {
 496              $path = '/1.1/comment-check';
 497          } elseif ( 'submit' === $check ) {
 498              $path = '/1.1/submit-' . $spam;
 499          }
 500  
 501          // Send data to Akismet
 502          if ( ! apply_filters( 'bbp_bypass_check_for_spam', false, $post_data ) ) {
 503              $response = $this->http_post( $query_string, $akismet_api_host, $path, $akismet_api_port );
 504          }
 505  
 506          // Set the result headers
 507          $post_data['bbp_akismet_result_headers'] = ! empty( $response[0] )
 508              ? $response[0] // raw
 509              : esc_html__( 'No response', 'bbpress' );
 510  
 511          // Set the result
 512          $post_data['bbp_akismet_result'] = ! empty( $response[1] )
 513              ? $response[1] // raw
 514              : esc_html__( 'No response', 'bbpress' );
 515  
 516          // Return the post data, with the results of the external Akismet request
 517          return $post_data;
 518      }
 519  
 520      /**
 521       * Update post meta after a spam check
 522       *
 523       * @since 2.0.0 bbPress (r3308)
 524       *
 525       * @param int $post_id
 526       * @param object $_post
 527       *
 528       * @global object $this->last_post
 529       */
 530  	public function update_post_meta( $post_id = 0, $_post = false ) {
 531  
 532          // Define local variable(s)
 533          $as_submitted = false;
 534  
 535          // Setup some variables
 536          $post_id = (int) $post_id;
 537  
 538          // Ensure we have a post object
 539          if ( empty( $_post ) ) {
 540              $_post = get_post( $post_id );
 541          }
 542  
 543          // Set up Akismet last post data
 544          if ( ! empty( $this->last_post['bbp_post_as_submitted'] ) ) {
 545              $as_submitted = $this->last_post['bbp_post_as_submitted'];
 546          }
 547  
 548          // wp_insert_post() might be called in other contexts. Ensure this is
 549          // the same topic/reply as was checked by BBP_Akismet::check_post()
 550          if ( is_object( $_post ) && ! empty( $this->last_post ) && is_array( $as_submitted ) ) {
 551  
 552              // Get user data
 553              $userdata       = get_userdata( $_post->post_author );
 554              $anonymous_data = bbp_filter_anonymous_post_data();
 555  
 556              // Which name?
 557              $name = ! empty( $anonymous_data['bbp_anonymous_name'] )
 558                  ? $anonymous_data['bbp_anonymous_name']
 559                  : $userdata->display_name;
 560  
 561              // Which email?
 562              $email = ! empty( $anonymous_data['bbp_anonymous_email'] )
 563                  ? $anonymous_data['bbp_anonymous_email']
 564                  : $userdata->user_email;
 565  
 566              // More checks
 567              if (
 568  
 569                  // Post matches
 570                  ( intval( $as_submitted['comment_post_ID'] ) === intval( $_post->post_parent ) )
 571  
 572                  &&
 573  
 574                  // Name matches
 575                  ( $as_submitted['comment_author'] === $name )
 576  
 577                  &&
 578  
 579                  // Email matches
 580                  ( $as_submitted['comment_author_email'] === $email )
 581              ) {
 582  
 583                  // Delete old content daily
 584                  if ( ! wp_next_scheduled( 'akismet_scheduled_delete' ) ) {
 585                      wp_schedule_event( time(), 'daily', 'akismet_scheduled_delete' );
 586                  }
 587  
 588                  // Normal result: true
 589                  if ( ! empty( $this->last_post['bbp_akismet_result'] ) && ( $this->last_post['bbp_akismet_result'] === 'true' ) ) {
 590  
 591                      // Leave a trail so other's know what we did
 592                      update_post_meta( $post_id, '_bbp_akismet_result', 'true' );
 593                      $this->update_post_history(
 594                          $post_id,
 595                          esc_html__( 'Akismet caught this post as spam', 'bbpress' ),
 596                          'check-spam'
 597                      );
 598  
 599                      // If post_status isn't the spam status, as expected, leave a note
 600                      if ( bbp_get_spam_status_id() !== $_post->post_status ) {
 601                          $this->update_post_history(
 602                              $post_id,
 603                              sprintf(
 604                                  esc_html__( 'Post status was changed to %s', 'bbpress' ),
 605                                  $_post->post_status
 606                              ),
 607                              'status-changed-' . $_post->post_status
 608                          );
 609                      }
 610  
 611                  // Normal result: false
 612                  } elseif ( ! empty( $this->last_post['bbp_akismet_result'] ) && ( $this->last_post['bbp_akismet_result'] === 'false' ) ) {
 613  
 614                      // Leave a trail so other's know what we did
 615                      update_post_meta( $post_id, '_bbp_akismet_result', 'false' );
 616                      $this->update_post_history(
 617                          $post_id,
 618                          esc_html__( 'Akismet cleared this post as not spam', 'bbpress' ),
 619                          'check-ham'
 620                      );
 621  
 622                      // If post_status is the spam status, which isn't expected, leave a note
 623                      if ( bbp_get_spam_status_id() === $_post->post_status ) {
 624                          $this->update_post_history(
 625                              $post_id,
 626                              sprintf(
 627                                  esc_html__( 'Post status was changed to %s', 'bbpress' ),
 628                                  $_post->post_status
 629                              ),
 630                              'status-changed-' . $_post->post_status
 631                          );
 632                      }
 633  
 634                  // Abnormal result: error
 635                  } else {
 636                      // Leave a trail so other's know what we did
 637                      update_post_meta( $post_id, '_bbp_akismet_error', time() );
 638                      $this->update_post_history(
 639                          $post_id,
 640                          sprintf(
 641                              esc_html__( 'Akismet was unable to check this post (response: %s), will automatically retry again later.', 'bbpress' ),
 642                              $this->last_post['bbp_akismet_result']
 643                          ),
 644                          'check-error'
 645                      );
 646                  }
 647  
 648                  // Record the complete original data as submitted for checking
 649                  if ( isset( $this->last_post['bbp_post_as_submitted'] ) ) {
 650                      update_post_meta(
 651                          $post_id,
 652                          '_bbp_akismet_as_submitted',
 653                          $this->last_post['bbp_post_as_submitted']
 654                      );
 655                  }
 656              }
 657          }
 658      }
 659  
 660      /**
 661       * Update Akismet history of a Post
 662       *
 663       * @since 2.0.0 bbPress (r3308)
 664       *
 665       * @param int $post_id
 666       * @param string $message
 667       * @param string $event
 668       */
 669  	private function update_post_history( $post_id = 0, $message = null, $event = null ) {
 670  
 671          // Define local variable(s)
 672          $user = '';
 673  
 674          // Get the current user
 675          $current_user = wp_get_current_user();
 676  
 677          // Get the user's login name if possible
 678          if ( is_object( $current_user ) && isset( $current_user->user_login ) ) {
 679              $user = $current_user->user_login;
 680          }
 681  
 682          // This used to be akismet_microtime() but it was removed in 3.0
 683          $mtime        = explode( ' ', microtime() );
 684          $message_time = $mtime[1] + $mtime[0];
 685  
 686          // Setup the event to be saved
 687          $event = array(
 688              'time'    => $message_time,
 689              'message' => $message,
 690              'event'   => $event,
 691              'user'    => $user,
 692          );
 693  
 694          // Save the event data
 695          add_post_meta( $post_id, '_bbp_akismet_history', $event );
 696      }
 697  
 698      /**
 699       * Get the Akismet history of a Post
 700       *
 701       * @since 2.0.0 bbPress (r3308)
 702       *
 703       * @param int $post_id
 704       *
 705       * @return array Array of Akismet history
 706       */
 707  	public function get_post_history( $post_id = 0 ) {
 708  
 709          // Retrieve any previous history
 710          $history = get_post_meta( $post_id, '_bbp_akismet_history' );
 711  
 712          // Sort it by the time recorded
 713          usort( $history, 'akismet_cmp_time' );
 714  
 715          return $history;
 716      }
 717  
 718      /**
 719       * Handle any terms submitted with a post flagged as spam
 720       *
 721       * @since 2.0.0 bbPress (r3308)
 722       *
 723       * @param string $terms Comma-separated list of terms
 724       * @param int $topic_id
 725       * @param int $reply_id
 726       *
 727       * @return array Array of existing topic terms
 728       */
 729  	public function filter_post_terms( $terms = '', $topic_id = 0, $reply_id = 0 ) {
 730  
 731          // Validate the reply_id and topic_id
 732          $reply_id = bbp_get_reply_id( $reply_id );
 733          $topic_id = bbp_get_topic_id( $topic_id );
 734  
 735          // Get any pre-existing terms
 736          $existing_terms = bbp_get_topic_tag_names( $topic_id );
 737  
 738          // Save the terms for later in case the reply gets hammed
 739          if ( ! empty( $terms ) ) {
 740              update_post_meta( $reply_id, '_bbp_akismet_spam_terms', $terms );
 741          }
 742  
 743          // Keep the topic tags the same for now
 744          return $existing_terms;
 745      }
 746  
 747      /**
 748       * Submit data to Akismet service with unique bbPress User Agent
 749       *
 750       * This code is directly taken from the akismet_http_post() function and
 751       * documented to bbPress 2.0 standard.
 752       *
 753       * @since 2.0.0 bbPress (r3466)
 754       *
 755       * @param string $request The request we are sending
 756       * @param string $host The host to send our request to
 757       * @param string $path The path from the host
 758       * @param string $port The port to use
 759       * @param string $ip Optional Override $host with an IP address
 760       * @return mixed WP_Error on error, array on success, empty on failure
 761       */
 762  	private function http_post( $request, $host, $path, $port = 80, $ip = '' ) {
 763  
 764          // Preload required variables
 765          $bbp_version  = bbp_get_version();
 766          $ak_version   = constant( 'AKISMET_VERSION' );
 767          $http_host    = $host;
 768          $blog_charset = get_option( 'blog_charset' );
 769  
 770          // User Agent & Content Type
 771          $akismet_ua   = "bbPress/{$bbp_version} | Akismet/{$ak_version}";
 772          $content_type = 'application/x-www-form-urlencoded; charset=' . $blog_charset;
 773  
 774          // Use specific IP (if provided)
 775          if ( ! empty( $ip ) && long2ip( ip2long( $ip ) ) ) {
 776              $http_host = $ip;
 777          }
 778  
 779          // Setup the arguments
 780          $http_args = array(
 781              'httpversion' => '1.0',
 782              'timeout'     => 15,
 783              'body'        => $request,
 784              'headers'     => array(
 785                  'Content-Type' => $content_type,
 786                  'Host'         => $host,
 787                  'User-Agent'   => $akismet_ua
 788              )
 789          );
 790  
 791          // Return the response
 792          return $this->get_response( $http_host . $path, $http_args );
 793      }
 794  
 795      /**
 796       * Handles the repeated calls to wp_remote_post(), including SSL support.
 797       *
 798       * @since 2.6.7 (bbPress r7194)
 799       *
 800       * @param string $host_and_path Scheme-less URL
 801       * @param array  $http_args     Array of arguments for wp_remote_post()
 802       * @return array
 803       */
 804  	private function get_response( $host_and_path = '', $http_args = array() ) {
 805  
 806          // Default variables
 807          $akismet_url = $http_akismet_url = 'http://' . $host_and_path;
 808          $is_ssl = $ssl_failed = false;
 809          $now = time();
 810  
 811          // Check if SSL requests were disabled fewer than 24 hours ago
 812          $ssl_disabled_time = get_option( 'akismet_ssl_disabled' );
 813  
 814          // Clean-up if 24 hours have passed
 815          if ( ! empty( $ssl_disabled_time ) && ( $ssl_disabled_time < ( $now - DAY_IN_SECONDS ) ) ) {
 816              delete_option( 'akismet_ssl_disabled' );
 817              $ssl_disabled_time = false;
 818          }
 819  
 820          // Maybe HTTPS if not disabled
 821          if ( empty( $ssl_disabled_time ) && ( $is_ssl = wp_http_supports( array( 'ssl' ) ) ) ) {
 822              $akismet_url = set_url_scheme( $akismet_url, 'https' );
 823          }
 824  
 825          // Initial remote request
 826          $response = wp_remote_post( $akismet_url, $http_args );
 827  
 828          // Initial request produced an error, so retry...
 829          if ( ! empty( $is_ssl ) && is_wp_error( $response ) ) {
 830  
 831              // Intermittent connection problems may cause the first HTTPS
 832              // request to fail and subsequent HTTP requests to succeed randomly.
 833              // Retry the HTTPS request once before disabling SSL for a time.
 834              $response = wp_remote_post( $akismet_url, $http_args );
 835  
 836              // SSL request failed twice, so try again without it
 837              if ( is_wp_error( $response ) ) {
 838                  $response   = wp_remote_post( $http_akismet_url, $http_args );
 839                  $ssl_failed = true;
 840              }
 841          }
 842  
 843          // Bail if errored
 844          if ( is_wp_error( $response ) ) {
 845              return array( '', '' );
 846          }
 847  
 848          // Maybe disable SSL for future requests
 849          if ( ! empty( $ssl_failed ) ) {
 850              update_option( 'akismet_ssl_disabled', $now );
 851          }
 852  
 853          // No errors so return response
 854          return array(
 855              $response['headers'],
 856              $response['body']
 857          );
 858      }
 859  
 860      /**
 861       * Return a user's roles on this site (including super_admin)
 862       *
 863       * @since 2.3.0 bbPress (r4812)
 864       *
 865       * @param int $user_id
 866       *
 867       * @return boolean
 868       */
 869  	private function get_user_roles( $user_id = 0 ) {
 870  
 871          // Default return value
 872          $roles = array();
 873  
 874          // Bail if cannot query the user
 875          if ( ! class_exists( 'WP_User' ) || empty( $user_id ) ) {
 876              return false;
 877          }
 878  
 879          // User ID
 880          $user = new WP_User( $user_id );
 881          if ( isset( $user->roles ) ) {
 882              $roles = (array) $user->roles;
 883          }
 884  
 885          // Super admin
 886          if ( is_multisite() && is_super_admin( $user_id ) ) {
 887              $roles[] = 'super_admin';
 888          }
 889  
 890          return implode( ',', $roles );
 891      }
 892  
 893      /** Admin *****************************************************************/
 894  
 895      /**
 896       * Add Aksimet History meta-boxes to topics and replies
 897       *
 898       * @since 2.4.0 bbPress (r5049)
 899       */
 900  	public function add_metaboxes() {
 901  
 902          // Topics
 903          add_meta_box(
 904              'bbp_akismet_topic_history',
 905              __( 'Akismet History', 'bbpress' ),
 906              array( $this, 'history_metabox' ),
 907              bbp_get_topic_post_type(),
 908              'normal',
 909              'core'
 910          );
 911  
 912          // Replies
 913          add_meta_box(
 914              'bbp_akismet_reply_history',
 915              __( 'Akismet History', 'bbpress' ),
 916              array( $this, 'history_metabox' ),
 917              bbp_get_reply_post_type(),
 918              'normal',
 919              'core'
 920          );
 921      }
 922  
 923      /**
 924       * Output for Akismet History meta-box
 925       *
 926       * @since 2.4.0 bbPress (r5049)
 927       */
 928  	public function history_metabox() {
 929  
 930          // Post ID
 931          $history = $this->get_post_history( get_the_ID() ); ?>
 932  
 933          <div class="akismet-history" style="margin: 13px 0;">
 934  
 935              <?php if ( ! empty( $history ) ) : ?>
 936  
 937                  <table>
 938                      <tbody>
 939  
 940                          <?php foreach ( $history as $row ) : ?>
 941  
 942                              <tr>
 943                                  <td style="color: #999; text-align: right; white-space: nowrap;">
 944                                      <span title="<?php echo esc_attr( date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT' ); ?>">
 945                                          <?php bbp_time_since( $row['time'], false, true ); ?>
 946                                      </span>
 947                                  </td>
 948                                  <td style="padding-left: 5px;">
 949                                      <?php echo esc_html( $row['message'] ); ?>
 950                                  </td>
 951                              </tr>
 952  
 953                          <?php endforeach; ?>
 954                      </tbody>
 955                  </table>
 956  
 957              <?php else : ?>
 958  
 959                  <p><?php esc_html_e( 'No recorded history. Akismet has not checked this post.', 'bbpress' ); ?></p>
 960  
 961              <?php endif; ?>
 962  
 963          </div>
 964  
 965          <?php
 966      }
 967  
 968      /**
 969       * Deletes old spam topics & replies from the queue after 15 days
 970       * (determined by `_bbp_akismet_delete_spam_interval` filter).
 971       *
 972       * @since 2.6.7 bbPress (r7203)
 973       *
 974       * @global wpdb $wpdb
 975       */
 976  	public function delete_old_spam() {
 977          global $wpdb;
 978  
 979          /**
 980           * Determines how many posts will be deleted in each batch.
 981           *
 982           * @param int The default as defined by AKISMET_DELETE_LIMIT (also used
 983           *            in Akismet WordPress plugin).
 984           */
 985          $delete_limit = (int) apply_filters( '_bbp_akismet_delete_spam_limit',
 986              defined( 'AKISMET_DELETE_LIMIT' )
 987                  ? AKISMET_DELETE_LIMIT
 988                  : 10000
 989          );
 990  
 991          // Validate the deletion limit
 992          $delete_limit = max( 1, intval( $delete_limit ) );
 993  
 994          /**
 995           * Determines how many days a piece of spam will be left in the Spam
 996           * queue before being deleted.
 997           *
 998           * @param int The default number of days.
 999           */
1000          $delete_interval = (int) apply_filters( '_bbp_akismet_delete_spam_interval', 15 );
1001  
1002          // Validate the deletion interval
1003          $delete_interval = max( 1, intval( $delete_interval ) );
1004  
1005          // Setup the query
1006          $sql = "SELECT id FROM {$wpdb->posts} WHERE post_type IN ('topic', 'reply') AND post_status = 'spam' AND DATE_SUB(NOW(), INTERVAL %d DAY) > post_date_gmt LIMIT %d";
1007  
1008          // Query loop of topic & reply IDs
1009          while ( $spam_ids = $wpdb->get_col( $wpdb->prepare( $sql, $delete_interval, $delete_limit ) ) ) {
1010  
1011              // Exit loop if no spam IDs
1012              if ( empty( $spam_ids ) ) {
1013                  break;
1014              }
1015  
1016              // Reset queries
1017              $wpdb->queries = array();
1018  
1019              // Loop through each of the topic/reply IDs
1020              foreach ( $spam_ids as $spam_id ) {
1021  
1022                  /**
1023                   * Perform a single action on the single topic/reply ID for
1024                   * simpler batch processing.
1025                   *
1026                   * Maybe we should run the bbp_delete_topic or bbp_delete_reply
1027                   * actions here, too?
1028                   *
1029                   * @param string The current function.
1030                   * @param int    The current topic/reply ID.
1031                   */
1032                  do_action( '_bbp_akismet_batch_delete', __FUNCTION__, $spam_id );
1033              }
1034  
1035              // Prepared as strings since id is an unsigned BIGINT, and using %
1036              // will constrain the value to the maximum signed BIGINT.
1037              $format_string = implode( ", ", array_fill( 0, count( $spam_ids ), '%s' ) );
1038  
1039              // Run the delete queries
1040              $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->posts} WHERE post_id IN ( " . $format_string . " )", $spam_ids ) );
1041              $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE post_id IN ( " . $format_string . " )", $spam_ids ) );
1042  
1043              // Clean the post cache for these topics & replies
1044              clean_post_cache( $spam_ids );
1045  
1046              /**
1047               * Single action that encompasses all topic/reply IDs after the
1048               * delete queries have been run.
1049               *
1050               * @param int   Count of topic/reply IDs
1051               * @param array Array of topic/reply IDs
1052               */
1053              do_action( '_bbp_akismet_delete_spam_count', count( $spam_ids ), $spam_ids );
1054          }
1055  
1056          /**
1057           * Determines whether tables should be optimized.
1058           *
1059           * @param int Random number between 1 and 5000.
1060           */
1061          $optimize = (int) apply_filters( '_bbp_akismet_optimize_tables', mt_rand( 1, 5000 ), array( $wpdb->posts, $wpdb->postmeta ) );
1062  
1063          // Lucky number 11
1064          if ( 11 === $optimize ) {
1065              $wpdb->query( "OPTIMIZE TABLE {$wpdb->posts}" );
1066              $wpdb->query( "OPTIMIZE TABLE {$wpdb->postmeta}" );
1067          }
1068      }
1069  
1070      /**
1071       * Deletes `_bbp_akismet_as_submitted` meta keys after 15 days, since they
1072       * are large and not useful in the long term.
1073       *
1074       * @since 2.6.7 bbPress (r7203)
1075       *
1076       * @global wpdb $wpdb
1077       */
1078  	public function delete_old_spam_meta() {
1079          global $wpdb;
1080  
1081          /**
1082           * Determines how many days a piece of spam will be left in the Spam
1083           * queue before being deleted.
1084           *
1085           * @param int The default number of days.
1086           */
1087          $interval = (int) apply_filters( '_bbp_akismet_delete_spam_meta_interval', 15 );
1088  
1089          // Validate the deletion interval
1090          $interval = max( 1, intval( $interval ) );
1091  
1092          // Setup the query
1093          $sql = "SELECT m.post_id FROM {$wpdb->postmeta} as m INNER JOIN {$wpdb->posts} as p ON m.post_id = p.id WHERE m.meta_key = '_bbp_akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > p.post_date_gmt LIMIT 10000";
1094  
1095          // Query loop of topic & reply IDs
1096          while ( $spam_ids = $wpdb->get_col( $wpdb->prepare( $sql, $interval ) ) ) {
1097  
1098              // Exit loop if no spam IDs
1099              if ( empty( $spam_ids ) ) {
1100                  break;
1101              }
1102  
1103              // Reset queries
1104              $wpdb->queries = array();
1105  
1106              // Loop through each of the topic/reply IDs
1107              foreach ( $spam_ids as $spam_id ) {
1108  
1109                  // Delete the as_submitted meta data
1110                  delete_post_meta( $spam_id, '_bbp_akismet_as_submitted' );
1111  
1112                  /**
1113                   * Perform a single action on the single topic/reply ID for
1114                   * simpler batch processing.
1115                   *
1116                   * @param string The current function.
1117                   * @param int    The current topic/reply ID.
1118                   */
1119                  do_action( '_bbp_akismet_batch_delete', __FUNCTION__, $spam_id );
1120              }
1121  
1122              /**
1123               * Single action that encompasses all topic/reply IDs after the
1124               * delete queries have been run.
1125               *
1126               * @param int   Count of topic/reply IDs
1127               * @param array Array of topic/reply IDs
1128               */
1129              do_action( '_bbp_akismet_delete_spam_meta_count', count( $spam_ids ), $spam_ids );
1130          }
1131  
1132          // Maybe optimize
1133          $this->maybe_optimize_postmeta();
1134      }
1135  
1136      /**
1137       * Clears post meta that no longer has corresponding posts in the database.
1138       *
1139       * @since 2.6.7 bbPress (r7203)
1140       *
1141       * @global wpdb $wpdb
1142       */
1143  	public function delete_orphaned_spam_meta() {
1144          global $wpdb;
1145  
1146          $last_meta_id = 0;
1147  
1148          // Start time (float)
1149          $start_time = isset( $_SERVER['REQUEST_TIME_FLOAT'] )
1150              ? (float) $_SERVER['REQUEST_TIME_FLOAT']
1151              : microtime( true );
1152  
1153          // Maximum time
1154          $max_exec_time = (float) max( ini_get( 'max_execution_time' ) - 5, 3 );
1155  
1156          // Setup the query
1157          $sql = "SELECT m.meta_id, m.post_id, m.meta_key FROM {$wpdb->postmeta} as m LEFT JOIN {$wpdb->posts} as p ON m.post_id = p.id WHERE p.id IS NULL AND m.meta_id > %d ORDER BY m.meta_id LIMIT 1000";
1158  
1159          // Query loop of topic & reply IDs
1160          while ( $spam_meta_results = $wpdb->get_results( $wpdb->prepare( $sql, $last_meta_id ) ) ) {
1161  
1162              // Exit loop if no spam IDs
1163              if ( empty( $spam_meta_results ) ) {
1164                  break;
1165              }
1166  
1167              // Reset queries
1168              $wpdb->queries = array();
1169  
1170              // Reset deleted meta count
1171              $spam_meta_deleted = array();
1172  
1173              // Loop through each of the metas
1174              foreach ( $spam_meta_results as $spam_meta ) {
1175  
1176                  // Skip if not an Akismet key
1177                  if ( 'akismet_' !== substr( $spam_meta->meta_key, 0, 8 ) ) {
1178                      continue;
1179                  }
1180  
1181                  // Delete the meta
1182                  delete_post_meta( $spam_meta->post_id, $spam_meta->meta_key );
1183  
1184                  /**
1185                   * Perform a single action on the single topic/reply ID for
1186                   * simpler batch processing.
1187                   *
1188                   * @param string The current function.
1189                   * @param int    The current topic/reply ID.
1190                   */
1191                  do_action( '_bbp_akismet_batch_delete', __FUNCTION__, $spam_meta );
1192  
1193                  // Stash the meta ID being deleted
1194                  $spam_meta_deleted[] = $last_meta_id = $spam_meta->meta_id;
1195              }
1196  
1197              /**
1198               * Single action that encompasses all topic/reply IDs after the
1199               * delete queries have been run.
1200               *
1201               * @param int   Count of spam meta IDs
1202               * @param array Array of spam meta IDs
1203               */
1204              do_action( '_bbp_akismet_delete_spam_meta_count', count( $spam_meta_deleted ), $spam_meta_deleted );
1205  
1206              // Break if getting close to max_execution_time.
1207              if ( ( microtime( true ) - $start_time ) > $max_exec_time ) {
1208                  break;
1209              }
1210          }
1211  
1212          // Maybe optimize
1213          $this->maybe_optimize_postmeta();
1214      }
1215  
1216      /**
1217       * Maybe OPTIMIZE the _postmeta database table.
1218       *
1219       * @since 2.7.0 bbPress (r7203)
1220       *
1221       * @global wpdb $wpdb
1222       */
1223  	private function maybe_optimize_postmeta() {
1224          global $wpdb;
1225  
1226          /**
1227           * Determines whether tables should be optimized.
1228           *
1229           * @param int Random number between 1 and 5000.
1230           */
1231          $optimize = (int) apply_filters( '_bbp_akismet_optimize_table', mt_rand( 1, 5000 ), $wpdb->postmeta );
1232  
1233          // Lucky number 11
1234          if ( 11 === $optimize ) {
1235              $wpdb->query( "OPTIMIZE TABLE {$wpdb->postmeta}" );
1236          }
1237      }
1238  }
1239  endif;


Generated: Wed Oct 20 01:00:57 2021 Cross-referenced by PHPXref 0.7.1