Wordpress - 如果用户删除了他们的帐户,则更新功能

Wordpress - Update function if users deleted their account

我受困于此,如果我们的某个用户删除了他们的帐户,我该如何更新此功能? 它应该自行更新,我不知道我该怎么做才能自动更新。 请任何帮助。

/**
 * Retrieve following count
 *
 * Gets the total number of users that the specified user is following
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve a count for
 * @return      int
 */

function pwuf_get_following_count( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $following = pwuf_get_following( $user_id );

    $count = 0;

    if ( $following ) {
        $count = count( $following );
    }

    return (int) apply_filters( 'pwuf_get_following_count', $count, $user_id );
}

这里是获取以下用户的函数

/**
 * Retrieves all users that the specified user follows
 *
 * Gets all users that $user_id followers
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to retrieve following for
 * @return      array
 */

function pwuf_get_following( $user_id = 0 ) {

    if ( empty( $user_id ) ) {
        $user_id = get_current_user_id();
    }

    $following = get_user_meta( $user_id, '_pwuf_following', true );

    if ( empty( $following ) ) {

    return;

    }
    return (array) apply_filters( 'pwuf_get_following', $following, $user_id );
}

这一项用于增加或减少用户数量。

/**
 * Increase follower count
 *
 * Increments the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to increease the count for
 * @return      int
 */

function pwuf_increase_followed_by_count( $user_id = 0 ) {

    do_action( 'pwuf_pre_increase_followed_count', $user_id );

    $followed_count = pwuf_get_follower_count( $user_id );

    if ( $followed_count !== false ) {

        $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', $followed_count + 1 );

    } else {

        $new_followed_count = update_user_meta( $user_id, '_pwuf_followed_by_count', 1 );

    }

    do_action( 'pwuf_post_increase_followed_count', $user_id );

    return $new_followed_count;
}


/**
 * Decrease follower count
 *
 * Decrements the total count for how many users a specified user is followed by
 *
 * @access      private
 * @since       1.0
 * @param   int $user_id - the ID of the user to decrease the count for
 * @return      int
 */

function pwuf_decrease_followed_by_count( $user_id ) {

    do_action( 'pwuf_pre_decrease_followed_count', $user_id );

    $followed_count = pwuf_get_follower_count( $user_id );

    if ( $followed_count ) {

        $count = update_user_meta( $user_id, '_pwuf_followed_by_count', ( $followed_count - 1 ) );

        do_action( 'pwuf_post_increase_followed_count', $user_id );

    }
    return $count;
}

现在我不知道为什么当一些用户删除他们的帐户时计数没有改变。

在尝试此操作之前备份您的数据库,或者在实时实施之前尝试在暂存站点中使用。将此添加到您的主题 'functions.php'.

function follow_delete_user( $user_id ) {
    $userslist = get_users();

    foreach ( $userslist as $user ) {
        pwuf_unfollow_user( $user->ID, $user_id );
        pwuf_unfollow_user( $user_id, $user->ID );
    }
}
add_action( 'delete_user', 'follow_delete_user' );