Skip to:
Content

BuddyPress.org

Changeset 5805


Ignore:
Timestamp:
02/17/2012 09:53:04 PM (10 years ago)
Author:
boonebgorges
Message:

Refactors mark-as-spam/ham process, so that it works properly from Network Admin as well as front-end.
A port of r5803 to the 1.5 branch.
Fixes #3787.

Location:
branches/1.5/bp-members
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/1.5/bp-members/bp-members-actions.php

    r4961 r5805  
    1212
    1313/**
    14  * Listens to the $bp component and action variables to determine if the user is viewing the members
    15  * directory page. If they are, it will set up the directory and load the members directory template.
    16  *
    17  * @package BuddyPress Core
    18  * @global object $bp Global BuddyPress settings object
    19  * @uses wp_enqueue_script() Loads a JS script into the header of the page.
    20  * @uses bp_core_load_template() Loads a specific template file.
    21  */
    22 /**
    2314 * When a site admin selects "Mark as Spammer/Not Spammer" from the admin menu
    2415 * this action will fire and mark or unmark the user and their blogs as spam.
     
    3021 */
    3122function bp_core_action_set_spammer_status( $user_id = 0 ) {
    32     global $wpdb;
    33 
    34     // Only super admins can currently spam users
    35     if ( !is_super_admin() || bp_is_my_profile() )
    36         return;
    3723
    3824    // Use displayed user if it's not yourself
    39     if ( empty( $user_id ) && bp_is_user() )
     25    if ( empty( $user_id ) )
    4026        $user_id = bp_displayed_user_id();
    41 
    42     // Bail if no user ID
    43     if ( empty( $user_id ) )
    44         return;
    45 
    46     // Bail if user ID is super admin
    47     if ( is_super_admin( $user_id ) )
    48         return;
    4927
    5028    if ( bp_is_current_component( 'admin' ) && ( in_array( bp_current_action(), array( 'mark-spammer', 'unmark-spammer' ) ) ) ) {
     
    5331        check_admin_referer( 'mark-unmark-spammer' );
    5432
    55         // Get the functions file
    56         if ( is_multisite() ) {
    57             require( ABSPATH . 'wp-admin/includes/ms.php' );
    58         }
     33        // To spam or not to spam
     34        $status = bp_is_current_action( 'mark-spammer' ) ? 'spam' : 'ham';
    5935
    60         // To spam or not to spam
    61         $is_spam = bp_is_current_action( 'mark-spammer' ) ? 1 : 0;
     36        // The heavy lifting
     37        bp_core_process_spammer_status( $user_id, $status );
    6238
    63         // Get the blogs for the user
    64         $blogs = get_blogs_of_user( $user_id, true );
    65 
    66         foreach ( (array) $blogs as $key => $details ) {
    67 
    68             // Do not mark the main or current root blog as spam
    69             if ( 1 == $details->userblog_id || bp_get_root_blog_id() == $details->userblog_id ) {
    70                 continue;
    71             }
    72 
    73             // Update the blog status
    74             update_blog_status( $details->userblog_id, 'spam', $is_spam );
    75         }
    76 
    77         // Finally, mark this user as a spammer
    78         if ( is_multisite() ) {
    79             update_user_status( $user_id, 'spam', $is_spam );
    80         }
    81 
    82         // Always set single site status
    83         $wpdb->update( $wpdb->users, array( 'user_status' => $is_spam ), array( 'ID' => $user_id ) );
    84 
    85         // Add feedback message
    86         if ( $is_spam ) {
     39        // Add feedback message. @todo - Error reporting
     40        if ( 'spam' == $status ) {
    8741            bp_core_add_message( __( 'User marked as spammer. Spam users are visible only to site admins.', 'buddypress' ) );
    8842        } else {
     
    9044        }
    9145
    92         // Hide this user's activity
    93         if ( $is_spam && bp_is_active( 'activity' ) ) {
    94             bp_activity_hide_user_activity( $user_id );
    95         }
    96 
    97         // We need a special hook for is_spam so that components can delete data at spam time
    98         $bp_action = $is_spam ? 'bp_make_spam_user' : 'bp_make_ham_user';
    99         do_action( $bp_action, bp_displayed_user_id() );
    100 
    101         // Call multisite actions in single site mode for good measure
    102         if ( !is_multisite() ) {
    103             $wp_action = $is_spam ? 'make_spam_user' : 'make_ham_user';
    104             do_action( $wp_action, bp_displayed_user_id() );
    105         }
    106 
    107         // Allow plugins to do neat things
     46        // Deprecated. Use bp_core_process_spammer_status.
     47        $is_spam = 'spam' == $status;
    10848        do_action( 'bp_core_action_set_spammer_status', bp_displayed_user_id(), $is_spam );
    10949
  • branches/1.5/bp-members/bp-members-functions.php

    r5778 r5805  
    488488
    489489/**
     490 * Processes a spammed or unspammed user
     491 *
     492 * This function is called in two ways:
     493 *  - in bp_core_action_set_spammer_status() (when spamming from the front-end)
     494 *  - by bp_core_mark_user_spam_admin() or bp_core_mark_user_ham_admin() (when spamming from the
     495 *    Dashboard)
     496 *
     497 * @since 1.5.5
     498 *
     499 * @param int $user_id The user being spammed/hammed
     500 * @param string $status 'spam' if being marked as spam, 'ham' otherwise
     501 */
     502function bp_core_process_spammer_status( $user_id, $status ) {
     503    global $wpdb;
     504   
     505    // Only super admins can currently spam users
     506    if ( !is_super_admin() || bp_is_my_profile() )
     507        return;
     508
     509    // Bail if no user ID
     510    if ( empty( $user_id ) )
     511        return;
     512       
     513    // Bail if user ID is super admin
     514    if ( is_super_admin( $user_id ) )
     515        return;
     516   
     517    // Get the functions file
     518    if ( is_multisite() ) {
     519        require_once( ABSPATH . 'wp-admin/includes/ms.php' );
     520    }
     521
     522    $is_spam = 'spam' == $status;
     523
     524    // Only you can prevent infinite loops
     525    remove_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' );
     526    remove_action( 'make_ham_user', 'bp_core_mark_user_ham_admin' );
     527
     528    // When marking as spam in the Dashboard, these actions are handled by WordPress
     529    if ( !is_admin() ) {
     530        // Get the blogs for the user
     531        $blogs = get_blogs_of_user( $user_id, true );
     532       
     533        foreach ( (array) $blogs as $key => $details ) {
     534       
     535            // Do not mark the main or current root blog as spam
     536            if ( 1 == $details->userblog_id || bp_get_root_blog_id() == $details->userblog_id ) {
     537                continue;
     538            }
     539       
     540            // Update the blog status
     541            update_blog_status( $details->userblog_id, 'spam', $is_spam );
     542        }
     543       
     544        // Finally, mark this user as a spammer
     545        if ( is_multisite() ) {
     546            update_user_status( $user_id, 'spam', $is_spam );
     547        }
     548       
     549        // Always set single site status
     550        $wpdb->update( $wpdb->users, array( 'user_status' => $is_spam ), array( 'ID' => $user_id ) );
     551               
     552        // Call multisite actions in single site mode for good measure
     553        if ( !is_multisite() ) {
     554            $wp_action = $is_spam ? 'make_spam_user' : 'make_ham_user';
     555            do_action( $wp_action, bp_displayed_user_id() );
     556        }
     557    }
     558
     559    // Hide this user's activity
     560    if ( $is_spam && bp_is_active( 'activity' ) ) {
     561        bp_activity_hide_user_activity( $user_id );
     562    }
     563
     564    // We need a special hook for is_spam so that components can delete data at spam time
     565    $bp_action = $is_spam ? 'bp_make_spam_user' : 'bp_make_ham_user';
     566    do_action( $bp_action, $user_id );
     567
     568    // Allow plugins to do neat things
     569    do_action( 'bp_core_process_spammer_status', $user_id, $is_spam );
     570   
     571    return true;
     572}
     573
     574/**
     575 * Hook to WP's make_spam_user and run our custom BP spam functions
     576 *
     577 * @since 1.5.5
     578 *
     579 * @param int $user_id The user id passed from the make_spam_user hook
     580 */
     581function bp_core_mark_user_spam_admin( $user_id ) {
     582    bp_core_process_spammer_status( $user_id, 'spam' );
     583}
     584add_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' );
     585
     586/**
     587 * Hook to WP's make_ham_user and run our custom BP spam functions
     588 *
     589 * @since 1.5.5
     590 *
     591 * @param int $user_id The user id passed from the make_ham_user hook
     592 */
     593function bp_core_mark_user_ham_admin( $user_id ) {
     594    bp_core_process_spammer_status( $user_id, 'ham' );
     595}
     596add_action( 'make_ham_user', 'bp_core_mark_user_ham_admin' );
     597
     598/**
    490599 * Checks if the user has been marked as a spammer.
    491600 *
Note: See TracChangeset for help on using the changeset viewer.