Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROD-7724 - Fix: Count of connection and following in members page. #4498

Open
wants to merge 12 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 145 additions & 0 deletions src/bp-core/bp-core-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ function bp_version_updater() {
bb_update_to_2_6_70();
}

if ( $raw_db_version < 21171 ) {
bb_update_to_2_6_91();
}

if ( $raw_db_version !== $current_db ) {
// @todo - Write only data manipulate migration here. ( This is not for DB structure change ).

Expand Down Expand Up @@ -3804,3 +3808,144 @@ function bb_update_to_2_6_70() {
}
}
}

/**
* Fixed count for my connection and following.
*
* @since BuddyBoss [BBVERSION]
*/
function bb_update_to_2_6_91() {
if ( ! bp_is_active( 'moderation' ) ) {
return;
}
$is_already_run = get_transient( 'bb_update_to_2_6_91' );
if ( $is_already_run ) {
return;
}

// Set a transient to avoid running the update multiple times within an hour.
set_transient( 'bb_update_to_2_6_91', 'yes', HOUR_IN_SECONDS );

bb_create_background_member_friends_and_follow_count();
}

/**
* Initiates a background process to update member friendship counts and remove suspended members from follow table.
*
* @since BuddyBoss [BBVERSION]
*
* @param int $paged Current page of users to process. Defaults to 1.
*/
function bb_create_background_member_friends_and_follow_count( $paged = 1 ) {
global $bb_background_updater;

$per_page = apply_filters( 'bb_create_background_member_friends_and_follow_count_limit', 100 );
$paged = empty( $paged ) ? 1 : $paged;
$offset = ( ( $paged - 1 ) * $per_page );

// Fetch user IDs without restricting based on meta (get all users).
$user_ids = get_users(
array(
'fields' => 'ids',
'number' => $per_page,
'offset' => $offset,
'orderby' => 'ID',
'order' => 'DESC',
)
);

// If no more users to process, exit.
if ( empty( $user_ids ) ) {
return;
}

// Queue background process for friendship count and removing suspended follow data.
$bb_background_updater->data(
array(
'type' => 'update_member_friends_and_follow_count',
'group' => 'bb_migrate_member_friends_and_follow_count',
'priority' => 5,
'callback' => 'bb_migrate_member_friends_and_follow_count_callback',
'args' => array( $user_ids, $paged ),
)
);
$bb_background_updater->save()->schedule_event();
}

/**
* Callback function for updating member friendship count and removing suspended members from follow table.
*
* @since BuddyBoss [BBVERSION]
*
* @param array $user_ids Array of user IDs to process.
* @param int $paged Current page for processing.
*/
function bb_migrate_member_friends_and_follow_count_callback( $user_ids, $paged ) {
global $bb_background_updater;
if ( empty( $user_ids ) ) {
return;
}

$is_friends_active = bp_is_active( 'friends' );
$is_moderation_active = bp_is_active( 'moderation' );
$min_count = (int) apply_filters( 'bb_migration_update_following_for_suspend_member', 100 );

foreach ( $user_ids as $user_id ) {
// Update friendship count if the user has the friend component active.
if ( $is_friends_active ) {
$friends = bp_core_get_users(
array(
'type' => 'alphabetical',
'user_id' => $user_id,
'per_page' => 1,
'populate_extras' => false,
)
);

// Update the total friend count only if the user has friends.
if ( ! empty( $friends ) && (int) $friends['total'] > 0 ) {
bp_update_user_meta( $user_id, 'total_friend_count', (int) $friends['total'] );
}
}

// Remove follow data for suspended members.
if (
$is_moderation_active &&
bp_moderation_is_user_suspended( $user_id ) &&
function_exists( 'bp_get_followers' )
) {
$followers = bp_get_followers(
array(
'user_id' => $user_id,
)
);
if ( ! empty( $followers ) ) {
if ( count( $followers ) > $min_count ) {
$chunked_followers = array_chunk( $followers, $min_count );
if ( ! empty( $chunked_followers ) ) {
foreach ( $chunked_followers as $chunk ) {
$bb_background_updater->data(
array(
'type' => 'suspend_following',
'group' => 'bb_update_following_for_suspend_member',
'data_id' => $user_id,
'secondary_data_id' => $user_id,
'callback' => 'BP_Suspend_Member::bb_update_following_for_suspend_member',
'args' => array( $user_id, $chunk, 'hide' ),
),
);

$bb_background_updater->save()->dispatch();
}
}
} else {
BP_Suspend_Member::bb_update_following_for_suspend_member( $user_id, $followers );
}
}
}
}

// Recursively call the function to process the next page of users.
$paged++;
bb_create_background_member_friends_and_follow_count( $paged );
}
28 changes: 27 additions & 1 deletion src/bp-friends/classes/class-bp-friends-friendship.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,33 @@ public static function get_friendship_ids_for_user( $user_id ) {
$friendship_ids = wp_cache_get( $cache_key, 'bp_friends_friendships_for_user' );

if ( false === $friendship_ids ) {
$friendship_ids = $wpdb->get_col( $wpdb->prepare( "SELECT id FROM {$bp->friends->table_name} WHERE (initiator_user_id = %d OR friend_user_id = %d) ORDER BY date_created DESC", $user_id, $user_id ) );
// Initialize the SQL query components.
$sql['select'] = "SELECT id FROM {$bp->friends->table_name}";
/**
* Filters the SELECT clause for retrieving friendship IDs.
*
* @since BuddyBoss [BBVERSION]
*
* @param string $select The SELECT clause of the SQL query.
* @param int $user_id The user ID for whom friendship IDs are being fetched.
*/
$sql['select'] = apply_filters( 'bb_get_friendship_ids_for_user_select_sql', $sql['select'], $user_id );

$sql['where'][] = $wpdb->prepare( "(initiator_user_id = %d OR friend_user_id = %d)", $user_id, $user_id );
/**
* Filters the WHERE clause for retrieving friendship IDs.
*
* @since BuddyBoss [BBVERSION]
*
* @param array $where Array of WHERE clause conditions.
* @param int $user_id The user ID for whom friendship IDs are being fetched.
*/
$sql['where'] = apply_filters( 'bb_get_friendship_ids_for_user_where_sql', $sql['where'], $user_id );
$where_sql = 'WHERE ' . join( ' AND ', $sql['where'] );

$sql = "{$sql['select']} {$where_sql} ORDER BY date_created DESC";
$friendship_ids = $wpdb->get_col( $sql );

wp_cache_set( $cache_key, $friendship_ids, 'bp_friends_friendships_for_user' );
}

Expand Down
29 changes: 29 additions & 0 deletions src/bp-moderation/bp-moderation-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -1076,3 +1076,32 @@ function bb_moderation_async_request_batch_process( $batch ) {
}
add_action( 'bb_async_request_batch_process', 'bb_moderation_async_request_batch_process', 10, 1 );

/**
* Filters the WHERE clause for friendship IDs to exclude suspended users.
*
* @since BuddyBoss [BBVERSION]
*
* @param array $where Array of WHERE clause conditions.
* @param int $user_id The user ID for whom friendship IDs are being fetched.
*
* @return array Modified array of WHERE clause conditions.
*/
function bb_moderation_get_friendship_ids_for_user_where_sql( $where, $user_id ) {
global $wpdb;
$moderated_user_ids = bb_moderation_moderated_user_ids();

// If there are any suspended users, add conditions to exclude them.
if ( ! empty( $moderated_user_ids ) ) {
$placeholders = implode( ', ', array_fill( 0, count( $moderated_user_ids ), '%d' ) );

$where[] = $wpdb->prepare(
"(initiator_user_id NOT IN ( {$placeholders} ) AND friend_user_id NOT IN ( {$placeholders} ))",
...$moderated_user_ids,
...$moderated_user_ids
);
}

return $where;
}

add_filter( 'bb_get_friendship_ids_for_user_where_sql', 'bb_moderation_get_friendship_ids_for_user_where_sql', 10, 2 );
64 changes: 64 additions & 0 deletions src/bp-moderation/classes/suspend/class-bp-suspend-member.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ protected function get_related_contents( $member_id, $args = array() ) {
$action = ! empty( $args['action'] ) ? $args['action'] : '';
$page = ! empty( $args['page'] ) ? $args['page'] : - 1;
$related_contents = array();
$member_id = (int) $member_id;

$related_contents[ BP_Suspend_Comment::$type ] = BP_Suspend_Comment::get_member_comment_ids( $member_id, $action, $page );

Expand Down Expand Up @@ -709,6 +710,47 @@ protected function get_related_contents( $member_id, $args = array() ) {
}
}

// Update following once member suspend.
if (
'hide' === $action &&
(
! empty( $args['user_suspended'] ) ||
! empty( $args['action_suspend'] )
) &&
function_exists( 'bp_get_followers' )
) {
// Get followers for suspend member.
$get_followers = bp_get_followers(
array(
'user_id' => $member_id,
)
);
if ( ! empty( $get_followers ) ) {
if ( $this->background_disabled ) {
$this->bb_update_following_for_suspend_member( $member_id, $get_followers );
} else {
$min_count = (int) apply_filters( 'bb_update_following_for_suspend_member', 100 );
$chunk_results = array_chunk( $get_followers, $min_count );
if ( ! empty( $chunk_results ) ) {
foreach ( $chunk_results as $chunk_result ) {
$bb_background_updater->data(
array(
'type' => 'suspend_following',
'group' => 'bb_update_following_for_suspend_member',
'data_id' => $member_id,
'secondary_data_id' => $member_id,
'callback' => array( $this, 'bb_update_following_for_suspend_member' ),
'args' => array( $member_id, $chunk_result ),
),
);

$bb_background_updater->save()->dispatch();
}
}
}
}
}

if ( bp_is_active( 'groups' ) ) {
$groups = BP_Groups_Member::get_group_ids( $member_id, false, false, true );
$group_ids = ! empty( $groups['groups'] ) ? $groups['groups'] : array();
Expand Down Expand Up @@ -1133,4 +1175,26 @@ public function bb_setup_hash_domain_url( $domain, $user_id ) {

return $domain;
}

/**
* Update following for suspend member.
*
* @since BuddyBoss [BBVERSION]
*
* @param int $user_id User ID.
* @param array $member_ids Array member friend IDs.
*/
public static function bb_update_following_for_suspend_member( $user_id, $member_ids ) {
if ( ! empty( $member_ids ) ) {
foreach ( $member_ids as $follower ) {
// Stop following to that user who follows to suspend member.
bp_stop_following(
array(
'leader_id' => $user_id,
'follower_id' => $follower,
)
);
}
}
}
}
17 changes: 17 additions & 0 deletions src/bp-templates/bp-nouveau/js/buddypress-nouveau.js
Original file line number Diff line number Diff line change
Expand Up @@ -2263,6 +2263,23 @@ window.bp = window.bp || {};
item.find( '.followers-wrap' ).replaceWith( response.data.count );
}

// Update the following count.
if ( $( self.objectNavParent + ' [data-bp-scope="following"]' ).length ) {
var following_count = Number( $( self.objectNavParent + ' [data-bp-scope="following"] span' ).html() ) || 0;

if ( -1 !== $.inArray( action, [ 'unfollow' ] ) ) {
following_count -= 1;
} else if ( -1 !== $.inArray( action, [ 'follow' ] ) ) {
following_count += 1;
}

if ( following_count < 0 ) {
following_count = 0;
}

$( self.objectNavParent + ' [data-bp-scope="following"] span' ).html( following_count );
}

target.parent().replaceWith( response.data.contents );
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/bp-templates/bp-nouveau/js/buddypress-nouveau.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/class-buddypress.php
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ private function setup_globals() {

/** Versions */
$this->version = defined( 'BP_PLATFORM_VERSION' ) ? BP_PLATFORM_VERSION : ( defined( 'BP_VERSION' ) ? BP_VERSION : '1.0.0' );
$this->db_version = 21161;
$this->db_version = 21171;

/** Loading */

Expand Down