Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
11/01/2011 11:21:59 PM (15 years ago)
Author:
djpaul
Message:

Second pass of Akismet support for the Activity component. See #3660

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-akismet.php

    r5259 r5262  
    4747                add_action( 'bp_activity_before_save',     array( $this, 'check_activity' ), 1, 1 );
    4848
    49                 // Update activity meta after a spam check
    50                 add_action( 'bp_activity_after_save',      array( $this, 'update_activity_meta' ), 1, 1 );
    51 
    5249                // Tidy up member's latest (activity) update
    5350                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 );
    5458        }
    5559
     
    9094                        return;
    9195
    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'] )
    9498                        return;
    9599
     
    103107         * This function is intended to be used inside the activity stream loop.
    104108         *
    105          * @since 1.2
     109         * @since 1.6
    106110         */
    107111        public function add_activity_spam_button() {
    108                 if ( !BP_Akismet::user_can_mark_spam() )
     112                if ( !bp_activity_user_can_mark_spam() )
    109113                        return;
    110114
     
    131135         * This function is intended to be used inside the activity stream loop.
    132136         *
    133          * @since 1.2
     137         * @since 1.6
    134138         */
    135139        public function add_activity_comment_spam_button() {
    136                 if ( !BP_Akismet::user_can_mark_spam() )
     140                if ( !bp_activity_user_can_mark_spam() )
    137141                        return;
    138142
     
    156160
    157161        /**
    158          * Convenience function to control whether the current user is allowed to mark activity items as spam
    159          *
    160          * @global object $bp BuddyPress global settings
    161          * @return bool True if user is allowed to mark activity items as spam
    162          * @since 1.6
    163          * @static
    164          */
    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         /**
    171162         * Get a list of filterable types of activity item that we want Akismet to automatically check for spam.
    172163         *
     
    183174         *
    184175         * @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 ) {
    190180                // Record this item so we can do some tidyup in BP_Akismet::check_member_activity_update()
    191181                $this->last_activity = $activity;
    192182
    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 );
    200184        }
    201185
     
    204188         *
    205189         * @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 ) {
    220194                //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 );
    221197        }
    222198
    223199        /**
    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
    241210                $activity_data                          = array();
    242211                $activity_data['akismet_comment_nonce'] = 'inactive';
     
    264233                        $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';
    265234
     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
    266258                // 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' );
    268260
    269261                // Record this item
     
    279271
    280272                        // Mark as spam
    281                         $this->mark_as_spam( $activity );
     273                        bp_activity_mark_as_spam( $activity, 'by_akismet' );
    282274                }
    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
    288284         * @param BP_Activity_Activity $activity The activity to check
    289285         * @since 1.6
    290286         */
    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 ) {
    292325                // Check we're dealing with what was last updated by Akismet
    293326                if ( empty( $this->last_activity ) || !empty( $this->last_activity ) && $activity->id != $this->last_activity->id )
     
    330363         * @since 1.6
    331364         */
    332         protected function maybe_spam( $activity_data, $check = 'check', $spam = 'spam' ) {
     365        public function send_akismet_request( $activity_data, $check = 'check', $spam = 'spam' ) {
    333366                global $akismet_api_host, $akismet_api_port;
    334367
     
    481514         * @since 1.6
    482515         */
    483         private function update_activity_history( $activity_id = 0, $message = '', $event = '' ) {
     516        public function update_activity_history( $activity_id = 0, $message = '', $event = '' ) {
    484517                $event = array(
    485518                        'event'   => $event,
Note: See TracChangeset for help on using the changeset viewer.