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 55 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
c62ed93
PROD-7724 - Fix: Count of connection and following in members page.
istiaq-bb Sep 9, 2024
9659b18
PROD-7724 - When user suspend then following member not remove.
jitendrabanjara1991 Sep 16, 2024
b537e32
PROD-7724: Fix total_friend_count meta update issue when member is su…
jitendrabanjara1991 Sep 16, 2024
f57069a
PROD-7724 - Add filter to exclude moderated users from friendship SQL
jitendrabanjara1991 Sep 16, 2024
a97be86
PROD-7724 - Revert changes
jitendrabanjara1991 Sep 16, 2024
9a9e864
PROD-7724 - Update db version to run migrate code
jitendrabanjara1991 Sep 16, 2024
4de08dc
PROD-7724 - Check user is suspend and moderation active or not
jitendrabanjara1991 Sep 16, 2024
88b6306
PROD-7724 - Update code for following when member suspend
jitendrabanjara1991 Sep 16, 2024
ca3bca5
PROD-7724 - Update min count
jitendrabanjara1991 Sep 17, 2024
58fb624
PROD-7724 - Update doc comment
jitendrabanjara1991 Sep 17, 2024
c0503ab
PROD-7724 - Remove condition as its not required because getting foll…
jitendrabanjara1991 Sep 17, 2024
6ab7905
PROD-7724 - Fixed - Update the following count when click on follow/u…
jitendrabanjara1991 Sep 17, 2024
c8ee4b0
Merge branch 'release' into PROD-7724
istiaq-bb Oct 4, 2024
f02341c
PROD-7724 - Remove logic to stop following user upon member block ( F…
jitendrabanjara1991 Oct 9, 2024
cdc34e7
PROD-7724 - Remove logic to stop following user upon member suspend …
jitendrabanjara1991 Oct 9, 2024
9b0931b
PROD-7724 - Remove code as not required
jitendrabanjara1991 Oct 9, 2024
25627ac
PROD-7724 - Update query
jitendrabanjara1991 Oct 9, 2024
ce38efa
PROD-7724 - Updated code for member count, as we don't need to remove…
jitendrabanjara1991 Oct 9, 2024
30a5f24
PROD-7724 - Update function name
jitendrabanjara1991 Oct 9, 2024
ec099e1
PROD-7724 - Update doc block comment
jitendrabanjara1991 Oct 9, 2024
995d433
PROD-7724 - Update db version
jitendrabanjara1991 Oct 11, 2024
fcf63e2
PROD-7724 - Update function name
jitendrabanjara1991 Oct 11, 2024
bd71eda
PROD-7724 - Fix: Add condition in filter to exclude moderated user.
istiaqhossain Oct 18, 2024
5df4bee
Merge branch 'release' into PROD-7724
KartikSuthar Oct 21, 2024
023c2e8
PROD-7724: Add SQL join filter for retrieving friend IDs
surajkrsingh Nov 4, 2024
37efa99
Resolved conflicts and merged release branch into PROD-7724
surajkrsingh Nov 4, 2024
d592c2c
PROD-7724: Add join query to check moderation for friend connections
surajkrsingh Nov 5, 2024
ee8de44
PROD-7724: Removed error_log
surajkrsingh Nov 5, 2024
9fff0be
PROD-7724: Fixed the join sql for friendship ids
surajkrsingh Nov 5, 2024
aab9bbc
PROD-7724: Fixed friends connection requests page
surajkrsingh Nov 5, 2024
3209a33
Merge branch 'release' into PROD-7724
surajkrsingh Nov 6, 2024
1e0dfc4
PROD-7724 - fix the issue about the member friend count while blockin…
KartikSuthar Nov 6, 2024
3ce2014
PROD-7724: Fixed member connections count when moderation component i…
surajkrsingh Nov 6, 2024
c5d1355
PROD-7724: Fixed total connections count for each member repair tool
surajkrsingh Nov 6, 2024
cc0d809
PROD-7724: Fixed moderation join sql for friendship ids
surajkrsingh Nov 6, 2024
bcbd87e
Merge branch 'release' into PROD-7724
chetansatasiya Dec 3, 2024
b0af100
PROD-7724 - Remove unwanted code
jitendrabanjara1991 Dec 10, 2024
1c2b8d2
PROD-7724 - Updated - exclude suspended users from the friendship query.
jitendrabanjara1991 Dec 10, 2024
c441a1d
PROD-7724 - Update doc block comment
jitendrabanjara1991 Dec 11, 2024
9f3a77d
PROD-7724 - Remove newly added action and used that code inside alrea…
jitendrabanjara1991 Dec 11, 2024
93b5873
PROD-7724: Updated function name and transient key as per change request
surajkrsingh Dec 12, 2024
58c451a
PROD-7724 - Fixed - when block/report and member suspend then count n…
jitendrabanjara1991 Dec 12, 2024
9cb9206
PROD-7724: Fix join and where SQL query to check moderation for frien…
surajkrsingh Dec 23, 2024
7b592b6
PROD-7724: Fix join SQL query to prevent the blocked users in connect…
surajkrsingh Dec 23, 2024
2dda581
PROD-7724: Prevent removing friends on block and exclude suspended/bl…
jitendrabanjara1991 Jan 2, 2025
53d0878
PROD-7724: Exclude suspended/blocked members from friend fetch process
jitendrabanjara1991 Jan 2, 2025
5a8d1f9
Merge branch 'release' into PROD-7724
jitendrabanjara1991 Jan 6, 2025
929b3aa
PROD-7724 - Remove is_confirmed from query which is creating issue in…
jitendrabanjara1991 Jan 6, 2025
3331a1f
PROD-7724 - Revert code for tool migration
jitendrabanjara1991 Jan 6, 2025
3b61f4c
PROD-7724 - Revert code for tool migration - The purpose of this func…
jitendrabanjara1991 Jan 6, 2025
ccc6531
PROD-7724 - Fix phpcs
jitendrabanjara1991 Jan 6, 2025
48b353d
[PROD-7724] fix the friend count meta for the blocked or suspended user
KartikSuthar Jan 10, 2025
f9310d7
[PROD-7724] fix the friend count meta for the blocked or suspended user
KartikSuthar Jan 10, 2025
cc947be
[PROD-7724] removed error log
KartikSuthar Jan 10, 2025
b9dcc80
PROD-7724 - Updated DB version
jitendrabanjara1991 Jan 10, 2025
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
73 changes: 71 additions & 2 deletions src/bp-activity/classes/class-bp-activity-follow.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,14 +243,83 @@ public static function get_counts( $user_id ) {
$followers = wp_cache_get( 'bp_total_follower_for_user_' . $user_id, 'bp' );

if ( false === $followers ) {
$followers = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->activity->table_name_follow} WHERE leader_id = %d", $user_id ) );
/**
* Retrieves the count of users following a specific user.
*
* @since BuddyBoss [BBVERSION]
*
* @param int $user_id The user ID for whom the follower count is being retrieved.
*
* @return int The count of followers for the specified user.
*/
$sql['select'] = "SELECT COUNT(u.id) FROM {$bp->activity->table_name_follow} u";
/**
* Filters the SELECT clause for retrieving the follower count.
*
* @since BuddyBoss [BBVERSION]
*
* @param string $select The SELECT clause of the SQL query.
* @param string $type The type of data being queried, e.g., 'follower_id'.
*/
$sql['select'] = apply_filters( 'bp_user_query_join_sql', $sql['select'], 'follower_id' );

$sql['where'][] = $wpdb->prepare( "u.leader_id = %d", $user_id );
/**
* Filters the WHERE clause for retrieving the follower count.
*
* @since BuddyBoss [BBVERSION]
*
* @param array $where Array of WHERE clause conditions.
* @param string $type The type of data being queried, e.g., 'follower_id'.
*/
$sql['where'] = apply_filters( 'bp_user_query_where_sql', $sql['where'], 'follower_id' );

$where_sql = 'WHERE ' . join( ' AND ', $sql['where'] );

$sql = "{$sql['select']} {$where_sql}";
$followers = $wpdb->get_var( $sql );
wp_cache_set( 'bp_total_follower_for_user_' . $user_id, $followers, 'bp' );
}

$following = wp_cache_get( 'bp_total_following_for_user_' . $user_id, 'bp' );

if ( false === $following ) {
$following = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->activity->table_name_follow} WHERE follower_id = %d", $user_id ) );
$sql = array();
/**
* Retrieves the count of users being followed by a specific user.
*
* @since BuddyBoss [BBVERSION]
*
* @param int $user_id The user ID for whom the follow count is being retrieved.
*
* @return int The count of users the specified user is following.
*/
$sql['select'] = "SELECT COUNT(u.id) FROM {$bp->activity->table_name_follow} u";
/**
* Filters the SELECT clause for retrieving the follow count.
*
* @since BuddyBoss [BBVERSION]
*
* @param string $select The SELECT clause of the SQL query.
* @param string $type The type of data being queried, e.g., 'leader_id'.
*/
$sql['select'] = apply_filters( 'bp_user_query_join_sql', $sql['select'], 'leader_id' );

$sql['where'][] = $wpdb->prepare( "u.follower_id = %d", $user_id );
/**
* Filters the WHERE clause for retrieving the follow count.
*
* @since BuddyBoss [BBVERSION]
*
* @param array $where Array of WHERE clause conditions.
* @param string $type The type of data being queried, e.g., 'leader_id'.
*/
$sql['where'] = apply_filters( 'bp_user_query_where_sql', $sql['where'], 'leader_id' );

$where_sql = 'WHERE ' . join( ' AND ', $sql['where'] );

$sql = "{$sql['select']} {$where_sql}";
$following = $wpdb->get_var( $sql );
wp_cache_set( 'bp_total_following_for_user_' . $user_id, $following, 'bp' );
}

Expand Down
49 changes: 27 additions & 22 deletions src/bp-core/admin/bp-core-admin-tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,36 +496,41 @@ function bp_admin_repair_friend_count() {
$statement = __( 'Repairing total connections count for each member … %s', 'buddyboss' );
$result = __( 'Failed!', 'buddyboss' );

$sql_delete = "DELETE FROM {$wpdb->usermeta} WHERE meta_key IN ( 'total_friend_count' );";
if ( is_wp_error( $wpdb->query( $sql_delete ) ) ) {
return array( 1, sprintf( $statement, $result ) );
$bp = buddypress();
$sql = "SELECT COUNT(u.ID) as c FROM {$wpdb->users} u";

// Check if moderation is enabled then exclude suspended users.
if ( bp_is_active( 'moderation' ) ) {
$sql .= " LEFT JOIN {$bp->moderation->table_name} s
ON u.ID = s.item_id
AND s.item_type = 'user'
AND ( s.user_suspended = 1 OR s.hide_sitewide = 1 )
WHERE s.item_id IS NULL";
}

$bp = buddypress();

// Walk through all users on the site.
$total_users = $wpdb->get_row( "SELECT count(ID) as c FROM {$wpdb->users}" )->c;
$total_users = $wpdb->get_row( $sql )->c;

$updated = array();
if ( $total_users > 0 ) {
$per_query = 500;
$per_query = 50;
$offset = 0;
while ( $offset < $total_users ) {
// Only bother updating counts for users who actually have friendships.
$friendships = $wpdb->get_results( $wpdb->prepare( "SELECT initiator_user_id, friend_user_id FROM {$bp->friends->table_name} WHERE is_confirmed = 1 AND ( ( initiator_user_id > %d AND initiator_user_id <= %d ) OR ( friend_user_id > %d AND friend_user_id <= %d ) )", $offset, $offset + $per_query, $offset, $offset + $per_query ) );

// The previous query will turn up duplicates, so we
// filter them here.
foreach ( $friendships as $friendship ) {
if ( ! isset( $updated[ $friendship->initiator_user_id ] ) ) {
BP_Friends_Friendship::total_friend_count( $friendship->initiator_user_id );
$updated[ $friendship->initiator_user_id ] = 1;
}
$select = "SELECT DISTINCT u.ID FROM {$wpdb->users} u";
$join = '';
$where = 'WHERE u.ID IS NOT NULL';
$limit = $wpdb->prepare('LIMIT %d OFFSET %d', $per_query, $offset);

// Check if moderation is enabled then exclude suspended users.
if ( bp_is_active( 'moderation' ) ) {
$join .= " LEFT JOIN {$bp->moderation->table_name} s
ON ( u.ID = s.item_id AND s.item_type = 'user' )
AND (s.user_suspended = 1 OR s.hide_sitewide = 1)";
$where .= ' AND s.item_id IS NULL';
}

if ( ! isset( $updated[ $friendship->friend_user_id ] ) ) {
BP_Friends_Friendship::total_friend_count( $friendship->friend_user_id );
$updated[ $friendship->friend_user_id ] = 1;
}
$members = $wpdb->get_results( "{$select} {$join} {$where} {$limit}" );
foreach ( $members as $member ) {
BP_Friends_Friendship::total_friend_count( $member->ID );
}

$offset += $per_query;
Expand Down
24 changes: 24 additions & 0 deletions src/bp-core/bp-core-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2650,3 +2650,27 @@ function bb_redirection_allowed_third_party_domains( $hosts ) {

return $hosts;
}

/**
* Handles moderation component is activated or deactivated.
* Runs a background update to recalculate member friends count if the moderation component status changes.
*
* @since BuddyBoss [BBVERSION]
*
* @param array $active_components Array of active components.
*
* @return void
*/
function bb_update_moderation_component_status( $active_components = array() ) {
$active_components = array_keys( $active_components );
$db_components = array_keys( bp_get_option( 'bp-active-components', array() ) );

$is_moderation_disabled = in_array( 'moderation', $db_components, true ) && ! in_array( 'moderation', $active_components, true );
$is_moderation_enabled = ! in_array( 'moderation', $db_components, true ) && in_array( 'moderation', $active_components, true );

if ( $is_moderation_disabled || $is_moderation_enabled ) {
bb_create_background_member_friends_count();
}
}

add_action( 'bp_core_install', 'bb_update_moderation_component_status', 10, 1 );
24 changes: 24 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 < 21311 ) {
bb_update_to_2_6_80();
jitendrabanjara1991 marked this conversation as resolved.
Show resolved Hide resolved
}

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

Expand Down Expand Up @@ -3809,3 +3813,23 @@ function bb_update_to_2_6_70() {
}
}
}

/**
* Fixed count for my connection.
*
* @since BuddyBoss [BBVERSION]
*/
function bb_update_to_2_6_80() {
jitendrabanjara1991 marked this conversation as resolved.
Show resolved Hide resolved
if ( ! bp_is_active( 'moderation' ) ) {
return;
}
$is_already_run = get_transient( 'bb_update_to_2_6_80' );
jitendrabanjara1991 marked this conversation as resolved.
Show resolved Hide resolved
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_80', 'yes', HOUR_IN_SECONDS );
jitendrabanjara1991 marked this conversation as resolved.
Show resolved Hide resolved

bb_create_background_member_friends_count();
}
39 changes: 38 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,44 @@ 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 f.id FROM {$bp->friends->table_name} as f";
$sql['join'] = '';

/**
* 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 );

/**
* Filters the JOIN clause for retrieving friendship IDs.
*
* @since BuddyBoss [BBVERSION]
*
* @param string $join The JOIN clause of the SQL query.
* @param int $user_id The user ID for whom friendship IDs are being fetched.
*/
$sql['join'] = apply_filters( 'bb_get_friendship_ids_for_user_join_sql', $sql['join'], $user_id );

$sql['where'][] = $wpdb->prepare( "(f.initiator_user_id = %d OR f.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']} {$sql['join']} {$where_sql} ORDER BY f.date_created DESC";
$friendship_ids = $wpdb->get_col( $sql );
wp_cache_set( $cache_key, $friendship_ids, 'bp_friends_friendships_for_user' );
}

Expand Down
93 changes: 76 additions & 17 deletions src/bp-moderation/bp-moderation-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,23 +283,6 @@ function bp_moderation_block_member() {
friends_remove_friend( bp_loggedin_user_id(), $item_id );
}

if (
function_exists( 'bp_is_following' ) &&
bp_is_following(
array(
'leader_id' => $item_id,
'follower_id' => bp_loggedin_user_id(),
)
)
) {
bp_stop_following(
array(
'leader_id' => $item_id,
'follower_id' => bp_loggedin_user_id(),
)
);
}

$response['button'] = bp_moderation_get_report_button(
array(
'button_attr' => array(
Expand Down Expand Up @@ -1076,3 +1059,79 @@ 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 ) {
$where[] = '(s.user_suspended = 0 OR s.user_suspended IS NULL)';
$where[] = '(s.hide_sitewide = 0 OR s.hide_sitewide IS NULL)';

return $where;
}

add_filter( 'bb_get_friendship_ids_for_user_where_sql', 'bb_moderation_get_friendship_ids_for_user_where_sql', 10, 2 );

/**
* Filters the JOIN clause for friendship IDs to exclude suspended users.
*
* @since BuddyBoss [BBVERSION]
*
* @param string $join The JOIN clause of the SQL query.
* @param int $user_id The user ID for whom friendship IDs are being fetched.
*
* @return string Modified JOIN clause of the SQL query.
*/
function bb_moderation_get_friendship_ids_for_user_join_sql( $join, $user_id ) {
$bp = buddypress();

// Join with the wp_bp_suspend table to filter out users based on suspension criteria.
$join .= ' LEFT JOIN ' . $bp->moderation->table_name . ' s ON (';
$join .= ' (f.initiator_user_id = s.item_id OR f.friend_user_id = s.item_id)';
$join .= ' AND s.item_type = "user"';
$join .= ' AND (s.hide_sitewide = 1 OR s.user_suspended = 1)';
$join .= ')';

// Join with the wp_bp_moderation table to apply moderation checks.
$join .= ' LEFT JOIN ' . $bp->moderation->table_name_reports . ' m ON (';
$join .= ' s.id = m.moderation_id';
$join .= ' AND (';
$join .= ' (m.user_id = ' . $user_id . ' AND m.user_report = 0)';
$join .= ' OR m.user_report != 1';
$join .= ')';
$join .= ')';

return $join;
}

add_filter( 'bb_get_friendship_ids_for_user_join_sql', 'bb_moderation_get_friendship_ids_for_user_join_sql', 10, 2 );


function bb_test_moderation_get_friendship_ids_for_user_where_sql( $where, $user_id ) {
global $wpdb;
$moderated_user_ids = bb_moderation_moderated_user_ids();

// If user ID is also in fetched modarated user ids, remove it.
if ( in_array( $user_id, $moderated_user_ids, true ) ) {
$moderated_user_ids = array_diff( $moderated_user_ids, array( $user_id ) );
}

// 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_test_moderation_get_friendship_ids_for_user_where_sql', 10, 2 );
jitendrabanjara1991 marked this conversation as resolved.
Show resolved Hide resolved
16 changes: 8 additions & 8 deletions src/bp-moderation/classes/suspend/class-bp-suspend-member.php
Original file line number Diff line number Diff line change
Expand Up @@ -715,9 +715,16 @@ 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;

// Update friend count.
if ( bp_is_active( 'friends' ) && $page <= 1 ) {
if (
bp_is_active( 'friends' ) &&
$page <= 1 &&
! empty( $args['action_suspend'] )
) {

// Update friend count for the suspend users only not for the blocked users.
$friend_ids = friends_get_friend_user_ids( $member_id );

if ( ! empty( $friend_ids ) ) {
Expand Down Expand Up @@ -1182,13 +1189,6 @@ public function bb_update_member_friend_count( $user_id, $member_ids, $type ) {

if ( ! empty( $friend_ids ) ) {
$total_friend_count = count( $friend_ids );

if ( 'hide' === $type && in_array( $user_id, $friend_ids, true ) ) {
--$total_friend_count;
} elseif ( 'unhide' === $type && ! in_array( $user_id, $friend_ids, true ) ) {
++$total_friend_count;
}

bp_update_user_meta( $member_id, 'total_friend_count', (int) $total_friend_count );
}

Expand Down
Loading
Loading