[ 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          // Admin
  72          if ( is_admin() ) {
  73              add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) );
  74          }
  75      }
  76  
  77      /**
  78       * Converts topic/reply data into Akismet comment checking format
  79       *
  80       * @since 2.0.0 bbPress (r3277)
  81       *
  82       * @param string $post_data
  83       *
  84       * @return array Array of post data
  85       */
  86  	public function check_post( $post_data ) {
  87  
  88          // Define local variables
  89          $user_data = array();
  90          $post_permalink = '';
  91  
  92          // Post is not published
  93          if ( bbp_get_public_status_id() !== $post_data['post_status'] ) {
  94              return $post_data;
  95          }
  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 ( ! empty( $anonymous_data ) ) {
 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 result (from maybe_spam() above)
 169          $post_data['bbp_akismet_result'] = ! empty( $_post['bbp_akismet_result'] )
 170              ? $_post['bbp_akismet_result'] // raw
 171              : esc_html__( 'No response', 'bbpress' );
 172  
 173          // Set the data-as-submitted value without the result (recursion avoidance)
 174          unset( $_post['bbp_akismet_result'] );
 175          $post_data['bbp_post_as_submitted'] = $_post;
 176  
 177          // Cleanup to avoid touching this variable again below
 178          unset( $_post );
 179  
 180          // Allow post_data to be manipulated
 181          do_action_ref_array( 'bbp_akismet_check_post', $post_data );
 182  
 183          // Parse and log the last response
 184          $this->last_post = $this->parse_response( $post_data );
 185  
 186          // Return the last response back to the filter
 187          return $this->last_post;
 188      }
 189  
 190      /**
 191       * Parse the response from the Akismet service, and alter the post data as
 192       * necessary. For example, switch the status to `spam` if spammy.
 193       *
 194       * Note: this method also skiis responsible for allowing users who can moderate, to
 195       * never have their posts marked as spam. This is because they are "trusted"
 196       * users. However, their posts are still sent to Akismet to be checked.
 197       *
 198       * @since 2.6.0 bbPress (r6873)
 199       *
 200       * @param array $post_data
 201       *
 202       * @return array
 203       */
 204  	private function parse_response( $post_data = array() ) {
 205  
 206          // Get the parent ID of the post as submitted
 207          $parent_id = ! empty( $post_data['bbp_post_as_submitted']['comment_post_ID'] )
 208              ? absint( $post_data['bbp_post_as_submitted']['comment_post_ID'] )
 209              : 0;
 210  
 211          // Allow moderators to skip spam (includes per-forum moderators via $parent_id)
 212          $skip_spam = current_user_can( 'moderate', $parent_id );
 213  
 214          // Bail early if current user can skip spam enforcement
 215          if ( apply_filters( 'bbp_bypass_spam_enforcement', $skip_spam, $post_data ) ) {
 216              return $post_data;
 217          }
 218  
 219          // Result is spam, so set the status as such
 220          if ( 'true' === $post_data['bbp_akismet_result'] ) {
 221  
 222              // Let plugins do their thing
 223              do_action( 'bbp_akismet_spam_caught' );
 224  
 225              // Set post_status to spam
 226              $post_data['post_status'] = bbp_get_spam_status_id();
 227  
 228              // Filter spammy tags into meta data
 229              add_filter( 'bbp_new_reply_pre_set_terms', array( $this, 'filter_post_terms' ), 1, 3 );
 230          }
 231  
 232          // Return the (potentially modified) post data
 233          return $post_data;
 234      }
 235  
 236      /**
 237       * Submit a post for spamming or hamming
 238       *
 239       * @since 2.0.0 bbPress (r3277)
 240       *
 241       * @param int $post_id
 242       *
 243       * @global string $akismet_api_host
 244       * @global string $akismet_api_port
 245       * @global object $current_user
 246       * @global object $current_site
 247       *
 248       * @return array Array of existing topic terms
 249       */
 250  	public function submit_post( $post_id = 0 ) {
 251          global $current_user, $current_site;
 252  
 253          // Innocent until proven guilty
 254          $request_type   = 'ham';
 255          $current_filter = current_filter();
 256  
 257          // Check this filter and adjust the $request_type accordingly
 258          switch ( $current_filter ) {
 259  
 260              // Mysterious, and straight from the can
 261              case 'bbp_spammed_topic' :
 262              case 'bbp_spammed_reply' :
 263                  $request_type = 'spam';
 264                  break;
 265  
 266              // Honey-glazed, a straight off the bone
 267              case 'bbp_unspammed_topic' :
 268              case 'bbp_unspammed_reply' :
 269                  $request_type = 'ham';
 270                  break;
 271  
 272              // Possibly poison...
 273              default :
 274                  return;
 275          }
 276  
 277          // Setup some variables
 278          $post_id = (int) $post_id;
 279  
 280          // Make sure we have a post
 281          $_post = get_post( $post_id );
 282  
 283          // Bail if get_post() fails
 284          if ( empty( $_post ) ) {
 285              return;
 286          }
 287  
 288          // Bail if we're spamming, but the post_status isn't spam
 289          if ( ( 'spam' === $request_type ) && ( bbp_get_spam_status_id() !== $_post->post_status ) ) {
 290              return;
 291          }
 292  
 293          // Pass title & content together into comment content
 294          $_post_content = trim( $_post->post_title . "\n\n" . $_post->post_content );
 295  
 296          // Set some default post_data
 297          $post_data = array(
 298              'comment_approved'     => $_post->post_status,
 299              'comment_author'       => $_post->post_author ? get_the_author_meta( 'display_name', $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_name',    true ),
 300              'comment_author_email' => $_post->post_author ? get_the_author_meta( 'email',        $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_email',   true ),
 301              'comment_author_url'   => $_post->post_author ? bbp_get_user_profile_url(            $_post->post_author ) : get_post_meta( $post_id, '_bbp_anonymous_website', true ),
 302              'comment_content'      => $_post_content,
 303              'comment_date'         => $_post->post_date,
 304              'comment_ID'           => $post_id,
 305              'comment_post_ID'      => $_post->post_parent,
 306              'comment_type'         => $_post->post_type,
 307              'permalink'            => get_permalink( $post_id ),
 308              'user_ID'              => $_post->post_author,
 309              'user_ip'              => get_post_meta( $post_id, '_bbp_author_ip', true ),
 310              'user_role'            => $this->get_user_roles( $_post->post_author ),
 311          );
 312  
 313          // Use the original version stored in post_meta if available
 314          $as_submitted = get_post_meta( $post_id, '_bbp_akismet_as_submitted', true );
 315          if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) ) {
 316              $post_data = array_merge( $post_data, $as_submitted );
 317          }
 318  
 319          // Add the reporter IP address
 320          $post_data['reporter_ip']  = bbp_current_author_ip();
 321  
 322          // Add some reporter info
 323          if ( is_object( $current_user ) ) {
 324              $post_data['reporter'] = $current_user->user_login;
 325          }
 326  
 327          // Add the current site domain
 328          if ( is_object( $current_site ) ) {
 329              $post_data['site_domain'] = $current_site->domain;
 330          }
 331  
 332          // Place your slide beneath the microscope
 333          $post_data = $this->maybe_spam( $post_data, 'submit', $request_type );
 334  
 335          // Manual user action
 336          if ( isset( $post_data['reporter'] ) ) {
 337  
 338              // What kind of action
 339              switch ( $request_type ) {
 340  
 341                  // Spammy
 342                  case 'spam' :
 343                      if ( 'topic' === $post_data['comment_type'] ) {
 344                          /* translators: %s: reporter name */
 345                          $message = sprintf( esc_html__( '%s reported this topic as spam', 'bbpress' ),
 346                              $post_data['reporter']
 347                          );
 348                      } elseif ( 'reply' === $post_data['comment_type'] ) {
 349                          /* translators: %s: reporter name */
 350                          $message = sprintf( esc_html__( '%s reported this reply as spam', 'bbpress' ),
 351                              $post_data['reporter']
 352                          );
 353                      } else {
 354                          /* translators: 1: reporter name, 2: comment type */
 355                          $message = sprintf( esc_html__( '%1$s reported this %2$s as spam', 'bbpress' ),
 356                              $post_data['reporter'],
 357                              $post_data['comment_type']
 358                          );
 359                      }
 360  
 361                      $this->update_post_history( $post_id, $message, 'report-spam' );
 362                      update_post_meta( $post_id, '_bbp_akismet_user_result', 'true'                 );
 363                      update_post_meta( $post_id, '_bbp_akismet_user',        $post_data['reporter'] );
 364                      break;
 365  
 366                  // Hammy
 367                  case 'ham' :
 368                      if ( 'topic' === $post_data['comment_type'] ) {
 369                          /* translators: %s: reporter name */
 370                          $message = sprintf( esc_html__( '%s reported this topic as not spam', 'bbpress' ),
 371                              $post_data['reporter']
 372                          );
 373                      } elseif ( 'reply' === $post_data['comment_type'] ) {
 374                          /* translators: %s: reporter name */
 375                          $message = sprintf( esc_html__( '%s reported this reply as not spam', 'bbpress' ),
 376                              $post_data['reporter']
 377                          );
 378                      } else {
 379                          /* translators: 1: reporter name, 2: comment type */
 380                          $message = sprintf( esc_html__( '%1$s reported this %2$s as not spam', 'bbpress' ),
 381                              $post_data['reporter'],
 382                              $post_data['comment_type']
 383                          );
 384                      }
 385  
 386                      $this->update_post_history( $post_id, $message, 'report-ham' );
 387                      update_post_meta( $post_id, '_bbp_akismet_user_result', 'false'                 );
 388                      update_post_meta( $post_id, '_bbp_akismet_user',         $post_data['reporter'] );
 389  
 390                      // @todo Topic term revision history
 391                      break;
 392  
 393                  // Possible other actions
 394                  default :
 395                      break;
 396              }
 397          }
 398  
 399          do_action( 'bbp_akismet_submit_' . $request_type . '_post', $post_id, $post_data['bbp_akismet_result'] );
 400      }
 401  
 402      /**
 403       * Ping Akismet service and check for spam/ham response
 404       *
 405       * @since 2.0.0 bbPress (r3277)
 406       *
 407       * @param array $post_data
 408       * @param string $check Accepts check|submit
 409       * @param string $spam Accepts spam|ham
 410       *
 411       * @global string $akismet_api_host
 412       * @global string $akismet_api_port
 413       *
 414       * @return array Array of post data
 415       */
 416  	private function maybe_spam( $post_data, $check = 'check', $spam = 'spam' ) {
 417          global $akismet_api_host, $akismet_api_port;
 418  
 419          // Define variables
 420          $query_string = $path = $response = '';
 421  
 422          // Populate post data
 423          $post_data['blog']         = get_option( 'home' );
 424          $post_data['blog_charset'] = get_option( 'blog_charset' );
 425          $post_data['blog_lang']    = get_locale();
 426          $post_data['referrer']     = wp_get_raw_referer();
 427          $post_data['user_agent']   = bbp_current_author_ua();
 428  
 429          // Loop through _POST args and rekey strings
 430          foreach ( $_POST as $key => $value ) {
 431              if ( is_string( $value ) ) {
 432                  $post_data['POST_' . $key] = $value;
 433              }
 434          }
 435  
 436          // Keys to ignore
 437          $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
 438  
 439          // Loop through _SERVER args and remove allowed keys
 440          foreach ( $_SERVER as $key => $value ) {
 441  
 442              // Key should not be ignored
 443              if ( ! in_array( $key, $ignore, true ) && is_string( $value ) ) {
 444                  $post_data[ $key ] = $value;
 445  
 446              // Key should be ignored
 447              } else {
 448                  $post_data[ $key ] = '';
 449              }
 450          }
 451  
 452          // Ready...
 453          foreach ( $post_data as $key => $data ) {
 454              $query_string .= $key . '=' . urlencode( wp_unslash( $data ) ) . '&';
 455          }
 456  
 457          // Aim...
 458          if ( 'check' === $check ) {
 459              $path = '/1.1/comment-check';
 460          } elseif ( 'submit' === $check ) {
 461              $path = '/1.1/submit-' . $spam;
 462          }
 463  
 464          // Fire!
 465          $response = ! apply_filters( 'bbp_bypass_check_for_spam', false, $post_data )
 466              ? $this->http_post( $query_string, $akismet_api_host, $path, $akismet_api_port )
 467              : false;
 468  
 469          // Check the high-speed cam
 470          if ( ! empty( $response[1] ) ) {
 471              $post_data['bbp_akismet_result'] = $response[1];
 472          } else {
 473              $post_data['bbp_akismet_result'] = esc_html__( 'No response', 'bbpress' );
 474          }
 475  
 476          // Return the post data, with the results of the external Akismet request
 477          return $post_data;
 478      }
 479  
 480      /**
 481       * Update post meta after a spam check
 482       *
 483       * @since 2.0.0 bbPress (r3308)
 484       *
 485       * @param int $post_id
 486       * @param object $_post
 487       *
 488       * @global object $this->last_post
 489       */
 490  	public function update_post_meta( $post_id = 0, $_post = false ) {
 491  
 492          // Define local variable(s)
 493          $as_submitted = false;
 494  
 495          // Setup some variables
 496          $post_id = (int) $post_id;
 497  
 498          // Ensure we have a post object
 499          if ( empty( $_post ) ) {
 500              $_post = get_post( $post_id );
 501          }
 502  
 503          // Set up Akismet last post data
 504          if ( ! empty( $this->last_post ) ) {
 505              $as_submitted = $this->last_post['bbp_post_as_submitted'];
 506          }
 507  
 508          // wp_insert_post() might be called in other contexts. Ensure this is
 509          // the same topic/reply as was checked by BBP_Akismet::check_post()
 510          if ( is_object( $_post ) && ! empty( $this->last_post ) && is_array( $as_submitted ) ) {
 511  
 512              // Get user data
 513              $userdata       = get_userdata( $_post->post_author );
 514              $anonymous_data = bbp_filter_anonymous_post_data();
 515  
 516              // More checks
 517              if (    intval( $as_submitted['comment_post_ID'] )    === intval( $_post->post_parent )
 518                      &&      $as_submitted['comment_author']       === ( $anonymous_data ? $anonymous_data['bbp_anonymous_name']  : $userdata->display_name )
 519                      &&      $as_submitted['comment_author_email'] === ( $anonymous_data ? $anonymous_data['bbp_anonymous_email'] : $userdata->user_email   )
 520                  ) {
 521  
 522                  // Normal result: true
 523                  if ( $this->last_post['bbp_akismet_result'] === 'true' ) {
 524  
 525                      // Leave a trail so other's know what we did
 526                      update_post_meta( $post_id, '_bbp_akismet_result', 'true' );
 527                      $this->update_post_history( $post_id, esc_html__( 'Akismet caught this post as spam', 'bbpress' ), 'check-spam' );
 528  
 529                      // If post_status isn't the spam status, as expected, leave a note
 530                      if ( bbp_get_spam_status_id() !== $_post->post_status ) {
 531                          $this->update_post_history( $post_id, sprintf( esc_html__( 'Post status was changed to %s', 'bbpress' ), $_post->post_status ), 'status-changed-' . $_post->post_status );
 532                      }
 533  
 534                  // Normal result: false
 535                  } elseif ( $this->last_post['bbp_akismet_result'] === 'false' ) {
 536  
 537                      // Leave a trail so other's know what we did
 538                      update_post_meta( $post_id, '_bbp_akismet_result', 'false' );
 539                      $this->update_post_history( $post_id, esc_html__( 'Akismet cleared this post as not spam', 'bbpress' ), 'check-ham' );
 540  
 541                      // If post_status is the spam status, which isn't expected, leave a note
 542                      if ( bbp_get_spam_status_id() === $_post->post_status ) {
 543                          $this->update_post_history( $post_id, sprintf( esc_html__( 'Post status was changed to %s', 'bbpress' ), $_post->post_status ), 'status-changed-' . $_post->post_status );
 544                      }
 545  
 546                  // Abnormal result: error
 547                  } else {
 548                      // Leave a trail so other's know what we did
 549                      update_post_meta( $post_id, '_bbp_akismet_error', time() );
 550                      $this->update_post_history( $post_id, sprintf( esc_html__( 'Akismet was unable to check this post (response: %s), will automatically retry again later.', 'bbpress' ), $this->last_post['bbp_akismet_result'] ), 'check-error' );
 551                  }
 552  
 553                  // Record the complete original data as submitted for checking
 554                  if ( isset( $this->last_post['bbp_post_as_submitted'] ) ) {
 555                      update_post_meta( $post_id, '_bbp_akismet_as_submitted', $this->last_post['bbp_post_as_submitted'] );
 556                  }
 557              }
 558          }
 559      }
 560  
 561      /**
 562       * Update Akismet history of a Post
 563       *
 564       * @since 2.0.0 bbPress (r3308)
 565       *
 566       * @param int $post_id
 567       * @param string $message
 568       * @param string $event
 569       */
 570  	private function update_post_history( $post_id = 0, $message = null, $event = null ) {
 571  
 572          // Define local variable(s)
 573          $user = '';
 574  
 575          // Get the current user
 576          $current_user = wp_get_current_user();
 577  
 578          // Get the user's login name if possible
 579          if ( is_object( $current_user ) && isset( $current_user->user_login ) ) {
 580              $user = $current_user->user_login;
 581          }
 582  
 583          // This used to be akismet_microtime() but it was removed in 3.0
 584          $mtime        = explode( ' ', microtime() );
 585          $message_time = $mtime[1] + $mtime[0];
 586  
 587          // Setup the event to be saved
 588          $event = array(
 589              'time'    => $message_time,
 590              'message' => $message,
 591              'event'   => $event,
 592              'user'    => $user,
 593          );
 594  
 595          // Save the event data
 596          add_post_meta( $post_id, '_bbp_akismet_history', $event );
 597      }
 598  
 599      /**
 600       * Get the Akismet history of a Post
 601       *
 602       * @since 2.0.0 bbPress (r3308)
 603       *
 604       * @param int $post_id
 605       *
 606       * @return array Array of Akismet history
 607       */
 608  	public function get_post_history( $post_id = 0 ) {
 609  
 610          // Retrieve any previous history
 611          $history = get_post_meta( $post_id, '_bbp_akismet_history' );
 612  
 613          // Sort it by the time recorded
 614          usort( $history, 'akismet_cmp_time' );
 615  
 616          return $history;
 617      }
 618  
 619      /**
 620       * Handle any terms submitted with a post flagged as spam
 621       *
 622       * @since 2.0.0 bbPress (r3308)
 623       *
 624       * @param string $terms Comma-separated list of terms
 625       * @param int $topic_id
 626       * @param int $reply_id
 627       *
 628       * @return array Array of existing topic terms
 629       */
 630  	public function filter_post_terms( $terms = '', $topic_id = 0, $reply_id = 0 ) {
 631  
 632          // Validate the reply_id and topic_id
 633          $reply_id = bbp_get_reply_id( $reply_id );
 634          $topic_id = bbp_get_topic_id( $topic_id );
 635  
 636          // Get any pre-existing terms
 637          $existing_terms = bbp_get_topic_tag_names( $topic_id );
 638  
 639          // Save the terms for later in case the reply gets hammed
 640          if ( ! empty( $terms ) ) {
 641              update_post_meta( $reply_id, '_bbp_akismet_spam_terms', $terms );
 642          }
 643  
 644          // Keep the topic tags the same for now
 645          return $existing_terms;
 646      }
 647  
 648      /**
 649       * Submit data to Akismet service with unique bbPress User Agent
 650       *
 651       * This code is directly taken from the akismet_http_post() function and
 652       * documented to bbPress 2.0 standard.
 653       *
 654       * @since 2.0.0 bbPress (r3466)
 655       *
 656       * @param string $request The request we are sending
 657       * @param string $host The host to send our request to
 658       * @param string $path The path from the host
 659       * @param string $port The port to use
 660       * @param string $ip Optional Override $host with an IP address
 661       * @return mixed WP_Error on error, array on success, empty on failure
 662       */
 663  	private function http_post( $request, $host, $path, $port = 80, $ip = '' ) {
 664  
 665          // Preload required variables
 666          $bbp_version  = bbp_get_version();
 667          $http_host    = $host;
 668          $blog_charset = get_option( 'blog_charset' );
 669          $response     = '';
 670  
 671          // Untque User Agent
 672          $akismet_ua     = "bbPress/{$bbp_version} | ";
 673          $akismet_ua    .= 'Akismet/' . constant( 'AKISMET_VERSION' );
 674  
 675          // Use specific IP (if provided)
 676          if ( ! empty( $ip ) && long2ip( ip2long( $ip ) ) ) {
 677              $http_host = $ip;
 678          }
 679  
 680          // Setup the arguments
 681          $http_args = array(
 682              'body'             => $request,
 683              'headers'          => array(
 684                  'Content-Type' => 'application/x-www-form-urlencoded; charset=' . $blog_charset,
 685                  'Host'         => $host,
 686                  'User-Agent'   => $akismet_ua
 687              ),
 688              'httpversion'      => '1.0',
 689              'timeout'          => 15
 690          );
 691  
 692          // Where we are sending our request
 693          $akismet_url = 'http://' . $http_host . $path;
 694  
 695          // Send the request
 696          $response    = wp_remote_post( $akismet_url, $http_args );
 697  
 698          // Bail if the response is an error
 699          if ( is_wp_error( $response ) ) {
 700              return '';
 701          }
 702  
 703          // No errors so return response
 704          return array( $response['headers'], $response['body'] );
 705      }
 706  
 707      /**
 708       * Return a user's roles on this site (including super_admin)
 709       *
 710       * @since 2.3.0 bbPress (r4812)
 711       *
 712       * @param int $user_id
 713       *
 714       * @return boolean
 715       */
 716  	private function get_user_roles( $user_id = 0 ) {
 717  
 718          // Default return value
 719          $roles = array();
 720  
 721          // Bail if cannot query the user
 722          if ( ! class_exists( 'WP_User' ) || empty( $user_id ) ) {
 723              return false;
 724          }
 725  
 726          // User ID
 727          $user = new WP_User( $user_id );
 728          if ( isset( $user->roles ) ) {
 729              $roles = (array) $user->roles;
 730          }
 731  
 732          // Super admin
 733          if ( is_multisite() && is_super_admin( $user_id ) ) {
 734              $roles[] = 'super_admin';
 735          }
 736  
 737          return implode( ',', $roles );
 738      }
 739  
 740      /** Admin *****************************************************************/
 741  
 742      /**
 743       * Add Aksimet History meta-boxes to topics and replies
 744       *
 745       * @since 2.4.0 bbPress (r5049)
 746       */
 747  	public function add_metaboxes() {
 748  
 749          // Topics
 750          add_meta_box(
 751              'bbp_akismet_topic_history',
 752              __( 'Akismet History', 'bbpress' ),
 753              array( $this, 'history_metabox' ),
 754              bbp_get_topic_post_type(),
 755              'normal',
 756              'core'
 757          );
 758  
 759          // Replies
 760          add_meta_box(
 761              'bbp_akismet_reply_history',
 762              __( 'Akismet History', 'bbpress' ),
 763              array( $this, 'history_metabox' ),
 764              bbp_get_reply_post_type(),
 765              'normal',
 766              'core'
 767          );
 768      }
 769  
 770      /**
 771       * Output for Akismet History meta-box
 772       *
 773       * @since 2.4.0 bbPress (r5049)
 774       */
 775  	public function history_metabox() {
 776  
 777          // Post ID
 778          $history = $this->get_post_history( get_the_ID() ); ?>
 779  
 780          <div class="akismet-history" style="margin: 13px 0;">
 781  
 782              <?php if ( ! empty( $history ) ) : ?>
 783  
 784                  <table>
 785                      <tbody>
 786  
 787                          <?php foreach ( $history as $row ) : ?>
 788  
 789                              <tr>
 790                                  <td style="color: #999; text-align: right; white-space: nowrap;">
 791                                      <span title="<?php echo esc_attr( date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT' ); ?>">
 792                                          <?php bbp_time_since( $row['time'], false, true ); ?>
 793                                      </span>
 794                                  </td>
 795                                  <td style="padding-left: 5px;">
 796                                      <?php echo esc_html( $row['message'] ); ?>
 797                                  </td>
 798                              </tr>
 799  
 800                          <?php endforeach; ?>
 801                      </tbody>
 802                  </table>
 803  
 804              <?php else : ?>
 805  
 806                  <p><?php esc_html_e( 'No recorded history. Akismet has not checked this post.', 'bbpress' ); ?></p>
 807  
 808              <?php endif; ?>
 809  
 810          </div>
 811  
 812          <?php
 813      }
 814  }
 815  endif;


Generated: Tue Jul 16 01:01:32 2019 Cross-referenced by PHPXref 0.7.1