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 35 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
35 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
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
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 < 21171 ) {
bb_update_to_2_7_21();
}

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,23 @@ function bb_update_to_2_6_70() {
}
}
}

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

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

bb_create_background_member_friends_count();
}
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
46 changes: 29 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,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 );
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
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