bp_core_process_spammer_status( int $user_id, string $status, bool $do_wp_cleanup = true )

Process a spammed or unspammed user.


Description Description

This function is called from three places:


Top ↑

Parameters Parameters

$user_id

(int) (Required) The ID of the user being spammed/hammed.

$status

(string) (Required) 'spam' if being marked as spam, 'ham' otherwise.

$do_wp_cleanup

(bool) (Optional) True to force the cleanup of WordPress content and status, otherwise false. Generally, this should only be false if WordPress is expected to have performed this cleanup independently, as when hooked to 'make_spam_user'.

Default value: true


Top ↑

Return Return

(bool) True on success, false on failure.


Source Source

File: bp-members/bp-members-functions.php

function bp_core_process_spammer_status( $user_id, $status, $do_wp_cleanup = true ) {
	global $wpdb;

	// Bail if no user ID.
	if ( empty( $user_id ) ) {
		return;
	}

	// Bail if user ID is super admin.
	if ( is_super_admin( $user_id ) ) {
		return;
	}

	// Get the functions file.
	if ( is_multisite() ) {
		require_once( ABSPATH . 'wp-admin/includes/ms.php' );
	}

	$is_spam = ( 'spam' == $status );

	// Only you can prevent infinite loops.
	remove_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' );
	remove_action( 'make_ham_user',  'bp_core_mark_user_ham_admin'  );

	// Force the cleanup of WordPress content and status for multisite configs.
	if ( $do_wp_cleanup ) {

		// Mark blogs as spam if the user is the sole admin of a site.
		if ( is_multisite() ) {
			/*
			 * No native function to fetch a user's blogs by role, so do it manually.
			 *
			 * This logic is mostly copied from get_blogs_of_user().
			 */
			$meta = get_user_meta( $user_id );

			foreach ( $meta as $key => $val ) {
				if ( 'capabilities' !== substr( $key, -12 ) ) {
					continue;
				}
				if ( $wpdb->base_prefix && 0 !== strpos( $key, $wpdb->base_prefix ) ) {
					continue;
				}
				$site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key );
				if ( ! is_numeric( $site_id ) ) {
					continue;
				}

				$site_id = (int) $site_id;

				// Do not mark the main or current root blog as spam.
				if ( 1 === $site_id || bp_get_root_blog_id() === $site_id ) {
					continue;
				}

				// Now, do check for administrator role.
				$role = maybe_unserialize( $val );
				if ( empty( $role['administrator'] ) ) {
					continue;
				}

				// Check if the site has more than 1 admin. If so, bail.
				$counts = count_users( 'time', $site_id );
				if ( empty( $counts['avail_roles']['administrator'] ) || $counts['avail_roles']['administrator'] > 1 ) {
					continue;
				}

				// Now we can spam the blog.
				update_blog_status( $site_id, 'spam', $is_spam );
			}
		}

		// Finally, mark this user as a spammer.
		bp_core_update_member_status( $user_id, $is_spam );
	}

	// Update the user status.
	$wpdb->update( $wpdb->users, array( 'user_status' => $is_spam ), array( 'ID' => $user_id ) );

	// Clean user cache.
	clean_user_cache( $user_id );

	if ( ! is_multisite() ) {
		// Call multisite actions in single site mode for good measure.
		if ( true === $is_spam ) {

			/**
			 * Fires at end of processing spammer in Dashboard if not multisite and user is spam.
			 *
			 * @since 1.5.0
			 *
			 * @param int $value user ID.
			 */
			do_action( 'make_spam_user', $user_id );
		} else {

			/**
			 * Fires at end of processing spammer in Dashboard if not multisite and user is not spam.
			 *
			 * @since 1.5.0
			 *
			 * @param int $value user ID.
			 */
			do_action( 'make_ham_user', $user_id );
		}
	}

	// Hide this user's activity.
	if ( ( true === $is_spam ) && bp_is_active( 'activity' ) ) {
		bp_activity_hide_user_activity( $user_id );
	}

	// We need a special hook for is_spam so that components can delete data at spam time.
	if ( true === $is_spam ) {

		/**
		 * Fires at the end of the process spammer process if the user is spam.
		 *
		 * @since 1.5.0
		 *
		 * @param int $value Displayed user ID.
		 */
		do_action( 'bp_make_spam_user', $user_id );
	} else {

		/**
		 * Fires at the end of the process spammer process if the user is not spam.
		 *
		 * @since 1.5.0
		 *
		 * @param int $value Displayed user ID.
		 */
		do_action( 'bp_make_ham_user', $user_id );
	}

	/**
	 * Fires at the end of the process for hanlding spammer status.
	 *
	 * @since 1.5.5
	 *
	 * @param int  $user_id ID of the processed user.
	 * @param bool $is_spam The determined spam status of processed user.
	 */
	do_action( 'bp_core_process_spammer_status', $user_id, $is_spam );

	// Put things back how we found them.
	add_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' );
	add_action( 'make_ham_user',  'bp_core_mark_user_ham_admin'  );

	return true;
}


Top ↑

Changelog Changelog

Changelog
Version Description
1.6.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.