Changeset 10253 for trunk/src/bp-activity/bp-activity-akismet.php
- Timestamp:
- 10/12/2015 05:45:14 AM (11 years ago)
- File:
-
- 1 edited
-
trunk/src/bp-activity/bp-activity-akismet.php (modified) (25 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/bp-activity/bp-activity-akismet.php
r10248 r10253 3 3 * Akismet support for BuddyPress' Activity Stream. 4 4 * 5 * @since 1.6.06 5 * @package BuddyPress 7 6 * @subpackage ActivityAkismet 7 * @since 1.6.0 8 8 */ 9 9 10 // Exit if accessed directly 10 // Exit if accessed directly. 11 11 defined( 'ABSPATH' ) || exit; 12 12 … … 18 18 */ 19 19 class BP_Akismet { 20 20 21 /** 21 22 * The activity last marked as spam. 22 23 * 24 * @since 1.6.0 25 * 23 26 * @var BP_Activity_Activity 24 * @since 1.6.025 27 */ 26 28 protected $last_activity = null; … … 41 43 */ 42 44 protected function setup_actions() { 43 // Add nonces to activity stream lists 45 // Add nonces to activity stream lists. 44 46 add_action( 'bp_after_activity_post_form', array( $this, 'add_activity_stream_nonce' ) ); 45 47 add_action( 'bp_activity_entry_comments', array( $this, 'add_activity_stream_nonce' ) ); 46 48 47 // Add a "mark as spam" button to individual activity items 49 // Add a "mark as spam" button to individual activity items. 48 50 add_action( 'bp_activity_entry_meta', array( $this, 'add_activity_spam_button' ) ); 49 51 add_action( 'bp_activity_comment_options', array( $this, 'add_activity_comment_spam_button' ) ); 50 52 51 // Check activity for spam 53 // Check activity for spam. 52 54 add_action( 'bp_activity_before_save', array( $this, 'check_activity' ), 4, 1 ); 53 55 54 // Tidy up member's latest (activity) update 56 // Tidy up member's latest (activity) update. 55 57 add_action( 'bp_activity_posted_update', array( $this, 'check_member_activity_update' ), 1, 3 ); 56 58 57 // Hooks to extend Activity core spam/ham functions for Akismet 59 // Hooks to extend Activity core spam/ham functions for Akismet. 58 60 add_action( 'bp_activity_mark_as_spam', array( $this, 'mark_as_spam' ), 10, 2 ); 59 61 add_action( 'bp_activity_mark_as_ham', array( $this, 'mark_as_ham' ), 10, 2 ); 60 62 61 // Hook into the Activity wp-admin screen 63 // Hook into the Activity wp-admin screen. 62 64 add_action( 'bp_activity_admin_comment_row_actions', array( $this, 'comment_row_action' ), 10, 2 ); 63 65 add_action( 'bp_activity_admin_load', array( $this, 'add_history_metabox' ) ); … … 74 76 * @param array $actions The hover links. 75 77 * @param array $activity The activity for the current row being processed. 76 *77 78 * @return array The hover links. 78 79 */ … … 83 84 84 85 if ( !$user_result || $user_result == $akismet_result ) { 85 // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same 86 // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same. 86 87 if ( 'true' == $akismet_result && $activity['is_spam'] ) 87 88 $desc = __( 'Flagged as spam by Akismet', 'buddypress' ); … … 99 100 } 100 101 101 // add a History item to the hover links, just after Edit102 // Add a History item to the hover links, just after Edit. 102 103 if ( $akismet_result ) { 103 104 $b = array(); … … 139 140 $value = '_bp_as_nonce_' . bp_loggedin_user_id(); 140 141 141 // If we're in the activity stream loop, we can use the current item's ID to make the nonce unique 142 // If we're in the activity stream loop, we can use the current item's ID to make the nonce unique. 142 143 if ( 'bp_activity_entry_comments' == current_filter() ) { 143 144 $form_id .= '_' . bp_get_activity_id(); … … 245 246 * @static 246 247 * 247 * @return array List of activity types.248 * @return array $value List of activity types. 248 249 */ 249 250 public static function get_activity_types() { … … 270 271 */ 271 272 public function mark_as_spam( $activity, $source ) { 272 // Record this item so we can do some tidyup in BP_Akismet::check_member_activity_update() 273 // Record this item so we can do some tidyup in BP_Akismet::check_member_activity_update(). 273 274 $this->last_activity = $activity; 274 275 … … 325 326 * 326 327 * @param BP_Activity_Activity $activity Activity item data. 327 * 328 * @return array 328 * @return array $activity_data 329 329 */ 330 330 public static function build_akismet_data_package( $activity ) { … … 383 383 return; 384 384 385 // Make sure last_activity is clear to avoid any confusion 385 // Make sure last_activity is clear to avoid any confusion. 386 386 $this->last_activity = null; 387 387 388 // Build data package for Akismet 388 // Build data package for Akismet. 389 389 $activity_data = BP_Akismet::build_akismet_data_package( $activity ); 390 390 391 // Check with Akismet to see if this is spam 391 // Check with Akismet to see if this is spam. 392 392 $activity_data = $this->send_akismet_request( $activity_data, 'check', 'spam' ); 393 393 394 // Record this item 394 // Record this item. 395 395 $this->last_activity = $activity; 396 396 397 // Store a copy of the data that was submitted to Akismet 397 // Store a copy of the data that was submitted to Akismet. 398 398 $this->last_activity->akismet_submission = $activity_data; 399 399 400 // Spam 400 // Spam. 401 401 if ( 'true' == $activity_data['bp_as_result'] ) { 402 402 /** … … 411 411 do_action_ref_array( 'bp_activity_akismet_spam_caught', array( &$activity, $activity_data ) ); 412 412 413 // Mark as spam 413 // Mark as spam. 414 414 bp_activity_mark_as_spam( $activity, 'by_akismet' ); 415 415 } 416 416 417 // Update activity meta after a spam check 417 // Update activity meta after a spam check. 418 418 add_action( 'bp_activity_after_save', array( $this, 'update_activity_akismet_meta' ), 1, 1 ); 419 419 } … … 461 461 */ 462 462 public function update_activity_akismet_meta( $activity ) { 463 // Check we're dealing with what was last updated by Akismet 463 // Check we're dealing with what was last updated by Akismet. 464 464 if ( empty( $this->last_activity ) || !empty( $this->last_activity ) && $activity->id != $this->last_activity->id ) 465 465 return; … … 469 469 return; 470 470 471 // Spam 471 // Spam. 472 472 if ( 'true' == $this->last_activity->akismet_submission['bp_as_result'] ) { 473 473 bp_activity_update_meta( $activity->id, '_bp_akismet_result', 'true' ); 474 474 $this->update_activity_history( $activity->id, __( 'Akismet caught this item as spam', 'buddypress' ), 'check-spam' ); 475 475 476 // Not spam 476 // Not spam. 477 477 } elseif ( 'false' == $this->last_activity->akismet_submission['bp_as_result'] ) { 478 478 bp_activity_update_meta( $activity->id, '_bp_akismet_result', 'false' ); … … 485 485 } 486 486 487 // Record the original data which was submitted to Akismet for checking 487 // Record the original data which was submitted to Akismet for checking. 488 488 bp_activity_update_meta( $activity->id, '_bp_akismet_submission', $this->last_activity->akismet_submission ); 489 489 } … … 499 499 * @param string $check "check" or "submit". 500 500 * @param string $spam "spam" or "ham". 501 *502 501 * @return array $activity_data Activity data, with Akismet data added. 503 502 */ … … 515 514 $activity_data['is_test'] = 'true'; 516 515 517 // Loop through _POST args and rekey strings 516 // Loop through _POST args and rekey strings. 518 517 foreach ( $_POST as $key => $value ) 519 518 if ( is_string( $value ) && 'cookie' != $key ) 520 519 $activity_data['POST_' . $key] = $value; 521 520 522 // Keys to ignore 521 // Keys to ignore. 523 522 $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ); 524 523 525 // Loop through _SERVER args and remove whitelisted keys 524 // Loop through _SERVER args and remove whitelisted keys. 526 525 foreach ( $_SERVER as $key => $value ) { 527 526 528 // Key should not be ignored 527 // Key should not be ignored. 529 528 if ( !in_array( $key, $ignore ) && is_string( $value ) ) { 530 529 $activity_data[$key] = $value; 531 530 532 // Key should be ignored 531 // Key should be ignored. 533 532 } else { 534 533 $activity_data[$key] = ''; … … 544 543 $path = 'submit-' . $spam; 545 544 546 // Send to Akismet 545 // Send to Akismet. 547 546 add_filter( 'akismet_ua', array( $this, 'buddypress_ua' ) ); 548 547 $response = Akismet::http_post( $query_string, $path ); 549 548 remove_filter( 'akismet_ua', array( $this, 'buddypress_ua' ) ); 550 549 551 // Get the response 550 // Get the response. 552 551 if ( ! empty( $response[1] ) && ! is_wp_error( $response[1] ) ) 553 552 $activity_data['bp_as_result'] = $response[1]; … … 555 554 $activity_data['bp_as_result'] = false; 556 555 557 // Perform a daily tidy up 556 // Perform a daily tidy up. 558 557 if ( ! wp_next_scheduled( 'bp_activity_akismet_delete_old_metadata' ) ) 559 558 wp_schedule_event( time(), 'daily', 'bp_activity_akismet_delete_old_metadata' ); … … 568 567 * 569 568 * @param string $user_agent User agent string, as generated by Akismet. 570 *571 569 * @return string $user_agent Modified user agent string. 572 570 */ … … 584 582 */ 585 583 function add_history_metabox( $screen_action ) { 586 // Only proceed if we're on the edit screen 584 // Only proceed if we're on the edit screen. 587 585 if ( 'edit' != $screen_action ) 588 586 return; 589 587 590 // Display meta box with a low priority (low position on screen by default) 588 // Display meta box with a low priority (low position on screen by default). 591 589 add_meta_box( 'bp_activity_history', __( 'Activity History', 'buddypress' ), array( $this, 'history_metabox' ), get_current_screen()->id, 'normal', 'low' ); 592 590 } … … 630 628 ); 631 629 632 // Save the history data 630 // Save the history data. 633 631 bp_activity_update_meta( $activity_id, '_bp_akismet_history', $event ); 634 632 } … … 640 638 * 641 639 * @param int $activity_id Activity item ID. 642 *643 640 * @return array The activity item's Akismet history. 644 641 */ … … 648 645 $history = array(); 649 646 650 // Sort it by the time recorded 647 // Sort it by the time recorded. 651 648 usort( $history, 'akismet_cmp_time' ); 652 649 … … 679 676 $interval = apply_filters( 'bp_activity_akismet_delete_meta_interval', 15 ); 680 677 681 // Enforce a minimum of 1 day 678 // Enforce a minimum of 1 day. 682 679 $interval = max( 1, absint( $interval ) ); 683 680
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)