Changeset 5262 for trunk/bp-activity/bp-activity-akismet.php
- Timestamp:
- 11/01/2011 11:21:59 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-activity/bp-activity-akismet.php
r5259 r5262 47 47 add_action( 'bp_activity_before_save', array( $this, 'check_activity' ), 1, 1 ); 48 48 49 // Update activity meta after a spam check50 add_action( 'bp_activity_after_save', array( $this, 'update_activity_meta' ), 1, 1 );51 52 49 // Tidy up member's latest (activity) update 53 50 add_action( 'bp_activity_posted_update', array( $this, 'check_member_activity_update' ), 1, 3 ); 51 52 // Hooks to extend Activity core spam/ham functions for Akismet 53 add_action( 'bp_activity_mark_as_spam', array( $this, 'mark_as_spam' ), 10, 2 ); 54 add_action( 'bp_activity_mark_as_ham', array( $this, 'mark_as_ham' ), 10, 2 ); 55 56 // Hook into the Activity wp-admin screen 57 //add_action( 'bp_activity_admin_comment_row_actions', array( $this, 'add_activity_status_row' ), 10, 2 ); 54 58 } 55 59 … … 90 94 return; 91 95 92 // Was this $activity_id just marked as spam? 93 if ( !$this->last_activity->id || $activity_id != $this->last_activity->id )96 // Was this $activity_id just marked as spam? If not, bail out. 97 if ( !$this->last_activity->id || $activity_id != $this->last_activity->id || 'false' == $this->last_activity->akismet_submission['bp_as_result'] ) 94 98 return; 95 99 … … 103 107 * This function is intended to be used inside the activity stream loop. 104 108 * 105 * @since 1. 2109 * @since 1.6 106 110 */ 107 111 public function add_activity_spam_button() { 108 if ( ! BP_Akismet::user_can_mark_spam() )112 if ( !bp_activity_user_can_mark_spam() ) 109 113 return; 110 114 … … 131 135 * This function is intended to be used inside the activity stream loop. 132 136 * 133 * @since 1. 2137 * @since 1.6 134 138 */ 135 139 public function add_activity_comment_spam_button() { 136 if ( ! BP_Akismet::user_can_mark_spam() )140 if ( !bp_activity_user_can_mark_spam() ) 137 141 return; 138 142 … … 156 160 157 161 /** 158 * Convenience function to control whether the current user is allowed to mark activity items as spam159 *160 * @global object $bp BuddyPress global settings161 * @return bool True if user is allowed to mark activity items as spam162 * @since 1.6163 * @static164 */165 public static function user_can_mark_spam() {166 global $bp;167 return apply_filters( 'bp_activity_akismet_user_can_mark_spam', $bp->loggedin_user->is_site_admin );168 }169 170 /**171 162 * Get a list of filterable types of activity item that we want Akismet to automatically check for spam. 172 163 * … … 183 174 * 184 175 * @param BP_Activity_Activity $activity 185 * @since 1.6 186 */ 187 public function mark_as_spam( &$activity ) { 188 $activity->is_spam = 1; 189 176 * @param string $source Either "by_a_person" (e.g. a person has manually marked the activity as spam) or "by_akismet" (automatically spammed). 177 * @since 1.6 178 */ 179 public function mark_as_spam( $activity, $source ) { 190 180 // Record this item so we can do some tidyup in BP_Akismet::check_member_activity_update() 191 181 $this->last_activity = $activity; 192 182 193 // Clear the activity stream first page cache 194 wp_cache_delete( 'bp_activity_sitewide_front', 'bp' ); 195 196 // Clear the activity comment cache for this activity item 197 wp_cache_delete( 'bp_activity_comments_' . $activity->id, 'bp' ); 198 199 do_action( 'bp_activity_akismet_mark_as_spam', $activity ); 183 do_action( 'bp_activity_akismet_mark_as_spam', $activity, $source ); 200 184 } 201 185 … … 204 188 * 205 189 * @param BP_Activity_Activity $activity 206 * @since 1.6 207 */ 208 public function mark_as_ham( &$activity ) { 209 $activity->is_spam = 0; 210 211 // Clear the activity stream first page cache 212 wp_cache_delete( 'bp_activity_sitewide_front', 'bp' ); 213 214 // Clear the activity comment cache for this activity item 215 wp_cache_delete( 'bp_activity_comments_' . $activity->id, 'bp' ); 216 217 //bp_activity_delete_meta( $activity->id, 'bpla_spam' ); 218 do_action( 'bp_activity_akismet_mark_as_ham', $activity ); 219 190 * @param string $source Either "by_a_person" (e.g. a person has manually marked the activity as spam) or "by_akismet" (automatically spammed). 191 * @since 1.6 192 */ 193 public function mark_as_ham( $activity, $source ) { 220 194 //DJPAULTODO: Run bp_activity_at_name_filter() somehow... but not twice, if we can help it. Maybe check if it was auto-spammed by Akismet? 195 196 do_action( 'bp_activity_akismet_mark_as_ham', $activity, $source ); 221 197 } 222 198 223 199 /** 224 * Check if the activity item is spam or ham 225 * 226 * @param BP_Activity_Activity $activity The activity item to check 227 * @see http://akismet.com/development/api/ 228 * @since 1.6 229 * @todo Spam counter? 230 * @todo Auto-delete old spam? 231 */ 232 public function check_activity( $activity ) { 233 // By default, only handle activity updates and activity comments. 234 if ( !in_array( $activity->type, BP_Akismet::get_activity_types() ) ) 235 return; 236 237 $this->last_activity = null; 238 $userdata = get_userdata( $activity->user_id ); 239 240 // Build up a data package for the Akismet service to inspect 200 * Build a data package for the Akismet service to inspect 201 * 202 * @param BP_Activity_Activity $activity 203 * @see http://akismet.com/development/api/#comment-check 204 * @since 1.6 205 * @static 206 */ 207 public static function build_akismet_data_package( $activity ) { 208 $userdata = get_userdata( $activity->user_id ); 209 241 210 $activity_data = array(); 242 211 $activity_data['akismet_comment_nonce'] = 'inactive'; … … 264 233 $activity_data['akismet_comment_nonce'] = wp_verify_nonce( $_POST["_bp_as_nonce_{$activity->secondary_item_id}"], "_bp_as_nonce_{$userdata->ID}_{$activity->secondary_item_id}" ) ? 'passed' : 'failed'; 265 234 235 return apply_filters( 'bp_akismet_build_akismet_data_package', $activity_data, $activity ); 236 } 237 238 /** 239 * Check if the activity item is spam or ham 240 * 241 * @param BP_Activity_Activity $activity The activity item to check 242 * @see http://akismet.com/development/api/ 243 * @since 1.6 244 * @todo Spam counter? 245 * @todo Auto-delete old spam? 246 */ 247 public function check_activity( $activity ) { 248 // By default, only handle activity updates and activity comments. 249 if ( !in_array( $activity->type, BP_Akismet::get_activity_types() ) ) 250 return; 251 252 // Make sure last_activity is clear to avoid any confusion 253 $this->last_activity = null; 254 255 // Build data package for Akismet 256 $activity_data = BP_Akismet::build_akismet_data_package( $activity ); 257 266 258 // Check with Akismet to see if this is spam 267 $activity_data = $this-> maybe_spam( $activity_data);259 $activity_data = $this->send_akismet_request( $activity_data, 'check', 'spam' ); 268 260 269 261 // Record this item … … 279 271 280 272 // Mark as spam 281 $this->mark_as_spam( $activity);273 bp_activity_mark_as_spam( $activity, 'by_akismet' ); 282 274 } 283 } 284 285 /** 286 * Update activity meta after a spam check 287 * 275 276 // Update activity meta after a spam check 277 add_action( 'bp_activity_after_save', array( $this, 'update_activity_akismet_meta' ), 1, 1 ); 278 } 279 280 /** 281 * Update activity meta after a manual spam change (user initiated) 282 * 283 * @global object $bp BuddyPress global settings 288 284 * @param BP_Activity_Activity $activity The activity to check 289 285 * @since 1.6 290 286 */ 291 public function update_activity_meta( $activity ) { 287 public function update_activity_spam_meta( $activity ) { 288 global $bp; 289 290 // By default, only handle activity updates and activity comments. 291 if ( !in_array( $activity->type, BP_Akismet::get_activity_types() ) ) 292 return; 293 294 $this->update_activity_history( $activity->id, sprintf( __( '%s reported this activity as spam', 'buddypress' ), $bp->loggedin_user->fullname ), 'report-spam' ); 295 bp_activity_update_meta( $activity->id, '_bp_akismet_user_result', 'true' ); 296 bp_activity_update_meta( $activity->id, '_bp_akismet_user', $bp->loggedin_user->fullname ); 297 } 298 299 /** 300 * Update activity meta after a manual ham change (user initiated) 301 * 302 * @global object $bp BuddyPress global settings 303 * @param BP_Activity_Activity $activity The activity to check 304 * @since 1.6 305 */ 306 public function update_activity_ham_meta( $activity ) { 307 global $bp; 308 309 // By default, only handle activity updates and activity comments. 310 if ( !in_array( $activity->type, BP_Akismet::get_activity_types() ) ) 311 return; 312 313 $this->update_activity_history( $activity->id, sprintf( __( '%s reported this activity as not spam', 'buddypress' ), $bp->loggedin_user->fullname ), 'report-ham' ); 314 bp_activity_update_meta( $activity->id, '_bp_akismet_user_result', 'false' ); 315 bp_activity_update_meta( $activity->id, '_bp_akismet_user', $bp->loggedin_user->fullname ); 316 } 317 318 /** 319 * Update activity meta after an automatic spam check (not user initiated) 320 * 321 * @param BP_Activity_Activity $activity The activity to check 322 * @since 1.6 323 */ 324 public function update_activity_akismet_meta( $activity ) { 292 325 // Check we're dealing with what was last updated by Akismet 293 326 if ( empty( $this->last_activity ) || !empty( $this->last_activity ) && $activity->id != $this->last_activity->id ) … … 330 363 * @since 1.6 331 364 */ 332 p rotected function maybe_spam( $activity_data, $check = 'check', $spam = 'spam' ) {365 public function send_akismet_request( $activity_data, $check = 'check', $spam = 'spam' ) { 333 366 global $akismet_api_host, $akismet_api_port; 334 367 … … 481 514 * @since 1.6 482 515 */ 483 p rivatefunction update_activity_history( $activity_id = 0, $message = '', $event = '' ) {516 public function update_activity_history( $activity_id = 0, $message = '', $event = '' ) { 484 517 $event = array( 485 518 'event' => $event,
Note: See TracChangeset
for help on using the changeset viewer.