Skip to:
Content

BuddyPress.org

Changeset 14195


Ignore:
Timestamp:
01/02/2026 07:10:26 PM (11 days ago)
Author:
espellcaste
Message:

Switch the function that updates the spam status of the member on multisite configs and deprecate bp_core_update_member_status.

Since WordPress 5.3, the user status is now updated using the globally available wp_update_user() function. So let us use it instead and deprecate bp_core_update_member_status().

It includes some minor Docblock changes.

Props imath.

Closes https://github.com/buddypress/buddypress/pull/434/
See #9173
Fixes #9167

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bp-core/deprecated/15.0.php

    r14187 r14195  
    107107    return bp_activity_delete( array( 'id' => $activity_id ) );
    108108}
     109
     110/**
     111 * Update the spam status of the member on multisite configs.
     112 *
     113 * @since 5.0.0
     114 * @deprecated 15.0.0
     115 *
     116 * @param int    $user_id The user ID to spam or ham.
     117 * @param string $value   '0' to mark the user as `ham`, '1' to mark as `spam`.
     118 * @return bool
     119 */
     120function bp_core_update_member_status( $user_id = 0, $value = 0 ) {
     121    _deprecated_function( __FUNCTION__, '15.0.0' );
     122
     123    if ( ! is_multisite() || ! $user_id ) {
     124        return false;
     125    }
     126
     127    /**
     128     * The `update_user_status()` function is deprecated since WordPress 5.3.0.
     129     * Continue to use it if WordPress current major version is lower than 5.3.
     130     */
     131    if ( bp_get_major_wp_version() < 5.3 ) {
     132        return update_user_status( $user_id, 'spam', $value );
     133    }
     134
     135    if ( $value ) {
     136        $value = '1';
     137    }
     138
     139    // Otherwise use the replacement function.
     140    $user = wp_update_user(
     141        array(
     142            'ID'   => $user_id,
     143            'spam' => $value,
     144        )
     145    );
     146
     147    if ( is_wp_error( $user ) ) {
     148        return false;
     149    }
     150
     151    return true;
     152}
  • trunk/src/bp-members/bp-members-functions.php

    r14077 r14195  
    644644
    645645/**
    646  * Update the spam status of the member on multisite configs.
    647  *
    648  * @since 5.0.0
    649  *
    650  * @param int    $user_id The user ID to spam or ham.
    651  * @param string $value   '0' to mark the user as `ham`, '1' to mark as `spam`.
    652  * @return bool          True if the spam status of the member changed.
    653  *                       False otherwise.
    654  */
    655 function bp_core_update_member_status( $user_id = 0, $value = 0 ) {
    656     if ( ! is_multisite() || ! $user_id ) {
    657         return false;
    658     }
    659 
    660     /**
    661      * The `update_user_status()` function is deprecated since WordPress 5.3.0.
    662      * Continue to use it if WordPress current major version is lower than 5.3.
    663      */
    664     if ( bp_get_major_wp_version() < 5.3 ) {
    665         return update_user_status( $user_id, 'spam', $value );
    666     }
    667 
    668     if ( $value ) {
    669         $value = '1';
    670     }
    671 
    672     // Otherwise use the replacement function.
    673     $user = wp_update_user(
    674         array(
    675             'ID'   => $user_id,
    676             'spam' => $value,
    677         )
    678     );
    679 
    680     if ( is_wp_error( $user ) ) {
    681         return false;
    682     }
    683 
    684     return true;
    685 }
    686 
    687 /**
    688646 * Process a spammed or unspammed user.
    689647 *
     
    712670    // Bail if no user ID.
    713671    if ( empty( $user_id ) ) {
    714         return;
     672        return false;
    715673    }
    716674
    717675    // Bail if user ID is super admin.
    718676    if ( is_super_admin( $user_id ) ) {
    719         return;
     677        return false;
    720678    }
    721679
    722680    // Get the functions file.
    723681    if ( is_multisite() ) {
    724         require_once( ABSPATH . 'wp-admin/includes/ms.php' );
    725     }
    726 
    727     $is_spam = ( 'spam' == $status );
     682        require_once ABSPATH . 'wp-admin/includes/ms.php';
     683    }
     684
     685    $is_spam = 'spam' === $status;
    728686
    729687    // Only you can prevent infinite loops.
    730688    remove_action( 'make_spam_user', 'bp_core_mark_user_spam_admin' );
    731     remove_action( 'make_ham_user',  'bp_core_mark_user_ham_admin' );
     689    remove_action( 'make_ham_user', 'bp_core_mark_user_ham_admin' );
    732690
    733691    // Force the cleanup of WordPress content and status for multisite configs.
     
    778736            }
    779737        }
    780 
    781         // Finally, mark this user as a spammer.
    782         bp_core_update_member_status( $user_id, $is_spam );
    783     }
    784 
    785     // Update the user status.
    786     $wpdb->update( $wpdb->users, array( 'user_status' => $is_spam ), array( 'ID' => $user_id ) );
    787 
    788     // Clean user cache.
    789     clean_user_cache( $user_id );
    790 
    791     if ( ! is_multisite() ) {
     738    }
     739
     740    // Update user status on multisite configs.
     741    if ( is_multisite() ) {
     742        $updated_user = wp_update_user(
     743            array(
     744                'ID'   => $user_id,
     745                'spam' => $is_spam ? '1' : '0',
     746            )
     747        );
     748
     749        if ( is_wp_error( $updated_user ) ) {
     750            return false;
     751        }
     752    } else {
     753        // We need to perform the query as WordPress sends an error when using `wp_update_user()` for non-multisite configs.
     754        $wpdb->update( $wpdb->users, array( 'user_status' => $is_spam ), array( 'ID' => $user_id ) );
     755
     756        // Clean user cache.
     757        clean_user_cache( $user_id );
     758
    792759        // Call multisite actions in single site mode for good measure.
    793760        if ( true === $is_spam ) {
    794761
    795762            /**
    796              * Fires at end of processing spammer in Dashboard if not multisite and user is spam.
     763             * Fires at end of processing spammer in Dashboard if not multisite and user IS spam.
    797764             *
    798765             * @since 1.5.0
    799766             *
    800              * @param int $value user ID.
     767             * @param int $user_id user ID.
    801768             */
    802769            do_action( 'make_spam_user', $user_id );
     
    804771
    805772            /**
    806              * Fires at end of processing spammer in Dashboard if not multisite and user is not spam.
     773             * Fires at end of processing spammer in Dashboard if not multisite and user IS NOT spam.
    807774             *
    808775             * @since 1.5.0
    809776             *
    810              * @param int $value user ID.
     777             * @param int $user_id user ID.
    811778             */
    812779            do_action( 'make_ham_user', $user_id );
     
    827794         * @since 1.5.0
    828795         *
    829          * @param int $value Displayed user ID.
     796         * @param int $user_id Displayed user ID.
    830797         */
    831798        do_action( 'bp_make_spam_user', $user_id );
     
    837804         * @since 1.5.0
    838805         *
    839          * @param int $value Displayed user ID.
     806         * @param int $user_id Displayed user ID.
    840807         */
    841808        do_action( 'bp_make_ham_user', $user_id );
     
    858825    return true;
    859826}
     827
    860828/**
    861829 * Hook to WP's make_spam_user and run our custom BP spam functions.
  • trunk/tests/phpunit/testcases/members/functions.php

    r14071 r14195  
    478478
    479479        // Bulk spam in network admin uses update_user_status
    480         bp_core_update_member_status( $u1, '1' );
     480        wp_update_user(
     481            array(
     482                'ID'   => $u1,
     483                'spam' => '1',
     484            )
     485        );
    481486
    482487        $this->assertTrue( bp_is_user_spammer( $u1 ) );
     
    509514
    510515        // Bulk unspam in network admin uses update_user_status
    511         bp_core_update_member_status( $u1, '0' );
     516        wp_update_user(
     517            array(
     518                'ID'   => $u1,
     519                'spam' => '0',
     520            )
     521        );
    512522
    513523        $this->assertFalse( bp_is_user_spammer( $u1 ) );
Note: See TracChangeset for help on using the changeset viewer.