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 54 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
79 changes: 77 additions & 2 deletions src/bp-activity/classes/class-bp-activity-follow.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ public static function get_following( $user_id, $query_args = array() ) {
* @since BuddyBoss 1.0.0
*
* @param int $user_id The user ID to fetch counts for.
*
* @return array
*/
public static function get_counts( $user_id ) {
Expand All @@ -243,14 +244,88 @@ 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
7 changes: 7 additions & 0 deletions src/bp-core/bp-core-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -2327,6 +2327,13 @@ function bb_update_digest_schedule_event_on_change_component_status( $active_com
}
}

$is_moderation_disabled = in_array( 'moderation', $db_component, true ) && ! in_array( 'moderation', $active_components, true );
$is_moderation_enabled = ! in_array( 'moderation', $db_component, 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_digest_schedule_event_on_change_component_status', 10, 1 );

Expand Down
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_on_update_moderation_friends_count_update();
}

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_on_update_moderation_friends_count_update() {
if ( ! bp_is_active( 'moderation' ) ) {
return;
}
$is_already_run = get_transient( 'bb_on_update_moderation_friends_count_update' );
if ( $is_already_run ) {
return;
}

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

bb_create_background_member_friends_count();
}
45 changes: 44 additions & 1 deletion src/bp-friends/classes/class-bp-friends-friendship.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ public static function get_friendships( $user_id, $args = array(), $operator = '
* @since BuddyPress 2.7.0
*
* @param int $user_id ID of the user.
*
* @return array
*/
public static function get_friendship_ids_for_user( $user_id ) {
Expand All @@ -380,7 +381,49 @@ 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 );

// Prepare the WHERE clause.
$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'] );

// Combine the SQL components.
$sql = "{$sql['select']} {$sql['join']} {$where_sql} ORDER BY f.date_created DESC";
$friendship_ids = $wpdb->get_col( $sql );

// Cache the result.
wp_cache_set( $cache_key, $friendship_ids, 'bp_friends_friendships_for_user' );
}

Expand Down
85 changes: 63 additions & 22 deletions src/bp-moderation/bp-moderation-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,28 +278,6 @@ function bp_moderation_block_member() {
if ( ! empty( $moderation->id ) && ! empty( $moderation->report_id ) ) {
$response['moderation'] = $moderation;

$friend_status = function_exists( 'bp_is_friend' ) && bp_is_active( 'friends' ) ? bp_is_friend( $item_id ) : array();
if ( ! empty( $friend_status ) && in_array( $friend_status, array( 'is_friend', 'pending', 'awaiting_response' ), true ) ) {
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 +1054,66 @@ function bb_moderation_async_request_batch_process( $batch ) {
}
add_action( 'bb_async_request_batch_process', 'bb_moderation_async_request_batch_process', 10, 1 );

/**
* Function to exclude suspended users from the friendship query.
*
* @since BuddyBoss [BBVERSION]
*
* @param array $where Array of where conditions.
*
* @return array Modified where conditions.
*/
function bb_moderation_get_friendship_ids_for_user_where_sql( $where ) {
global $bp;

$suspend_table_name = esc_sql( $bp->moderation->table_name );
$moderation_table_name = esc_sql( $bp->moderation->table_name_reports );
$item_type = esc_sql( BP_Suspend_Member::$type );

// Add a single NOT EXISTS condition to exclude suspended users.
$where[] = "NOT EXISTS (
SELECT 1
FROM {$suspend_table_name} s3
LEFT JOIN {$moderation_table_name} m ON m.moderation_id = s3.id
WHERE s3.item_type = '{$item_type}'
AND m.user_id IN (f.initiator_user_id, f.friend_user_id)
AND s3.item_id IN (f.initiator_user_id, f.friend_user_id)
AND s3.reported != 0 AND m.user_report != 1
)";

// Combine conditions to exclude suspended users.
$where[] = "(
(s1.user_suspended = 0 OR s1.user_suspended IS NULL)
AND (s1.hide_parent = 0 OR s1.hide_parent IS NULL)
AND (s1.hide_sitewide = 0 OR s1.hide_sitewide IS NULL)
AND (s2.user_suspended = 0 OR s2.user_suspended IS NULL)
AND (s2.hide_parent = 0 OR s2.hide_parent IS NULL)
AND (s2.hide_sitewide = 0 OR s2.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, 1 );

/**
* Function to modify JOIN clauses to filter friendships based on suspension and moderation criteria.
*
* @since BuddyBoss [BBVERSION]
*
* @param string $join The JOIN SQL part of the query.
* @param int $user_id The ID of the user for whom friendships are being queried.
*
* @return string Updated JOIN SQL part with suspension and moderation checks.
*/
function bb_moderation_get_friendship_ids_for_user_join_sql( $join, $user_id ) {
global $bp;

// Join with the wp_bp_suspend table to filter out suspended users.
$join .= " LEFT JOIN {$bp->moderation->table_name} s1 ON f.friend_user_id = s1.item_id AND s1.item_type = 'user'";
$join .= " LEFT JOIN {$bp->moderation->table_name} s2 ON f.initiator_user_id = s2.item_id AND s2.item_type = 'user'";

return $join;
}

add_filter( 'bb_get_friendship_ids_for_user_join_sql', 'bb_moderation_get_friendship_ids_for_user_join_sql', 10, 2 );
31 changes: 23 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,11 +715,23 @@ 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
) {

remove_filter( 'bb_get_friendship_ids_for_user_join_sql', 'bb_moderation_get_friendship_ids_for_user_join_sql', 10, 2 );
remove_filter( 'bb_get_friendship_ids_for_user_where_sql', 'bb_moderation_get_friendship_ids_for_user_where_sql', 10, 1 );

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

add_filter( 'bb_get_friendship_ids_for_user_join_sql', 'bb_moderation_get_friendship_ids_for_user_join_sql', 10, 2 );
add_filter( 'bb_get_friendship_ids_for_user_where_sql', 'bb_moderation_get_friendship_ids_for_user_where_sql', 10, 1 );

if ( ! empty( $friend_ids ) ) {
if ( $this->background_disabled || count( $friend_ids ) < 50 ) {
$this->bb_update_member_friend_count( $member_id, $friend_ids, $action );
Expand All @@ -744,6 +756,16 @@ protected function get_related_contents( $member_id, $args = array() ) {
}
}
}

wp_cache_delete( $member_id, 'bp_friends_friendships_for_user' );
wp_cache_delete( 'get_friendship_ids_for_user_' . $member_id, 'bp_friends_friendships_for_user' );

// Update friend count for the suspend users only not for the blocked users.
$current_friend_ids = friends_get_friend_user_ids( $member_id );
if ( ! empty( $current_friend_ids ) ) {
$current_total_friend_count = count( $current_friend_ids );
bp_update_user_meta( $member_id, 'total_friend_count', (int) $current_total_friend_count );
}
}

// Return the loop when the user is blocked/unblocked but not suspended/unsuspended.
Expand Down Expand Up @@ -1182,13 +1204,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
5 changes: 1 addition & 4 deletions src/bp-templates/bp-nouveau/includes/ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,7 @@ function bp_nouveau_object_template_results_members_tabs( $results, $object ) {
if ( bp_is_active( 'activity' ) && bp_is_activity_follow_active() ) {
$counts = bp_total_follow_counts();
if ( ! empty( $counts['following'] ) ) {
add_filter( 'bp_ajax_querystring', 'bp_nouveau_object_template_results_members_following_scope', 20, 2 );
bp_has_members( bp_ajax_querystring( 'members' ) );
$results['scopes']['following'] = bp_core_number_format( $GLOBALS['members_template']->total_member_count );
remove_filter( 'bp_ajax_querystring', 'bp_nouveau_object_template_results_members_following_scope', 20, 2 );
$results['scopes']['following'] = bp_core_number_format( $counts['following'] );
}
}

Expand Down
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.

Loading
Loading