| 450 | | $total_sql['select'] = "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name} g, {$bp->groups->table_name_members} gm1, {$bp->groups->table_name_groupmeta} gm2"; |
| 451 | | |
| 452 | | if ( ! empty( $r['user_id'] ) ) { |
| 453 | | $total_sql['select'] .= ", {$bp->groups->table_name_members} m"; |
| 454 | | } |
| 455 | | |
| 456 | | if ( ! empty( $sql['hidden'] ) ) { |
| 457 | | $total_sql['where'][] = "g.status != 'hidden'"; |
| 458 | | } |
| 459 | | |
| 460 | | if ( ! empty( $sql['search'] ) ) { |
| 461 | | $total_sql['where'][] = "( g.name LIKE '%%{$search_terms}%%' OR g.description LIKE '%%{$search_terms}%%' )"; |
| 462 | | } |
| | 449 | // Get total groups |
| | 450 | // Use $paged_groups_sql, but replace "g.*" with COUNT |
| | 451 | $t_sql = str_replace( 'g.*', 'COUNT(DISTINCT g.id)', $paged_groups_sql ); |
| 464 | | if ( ! empty( $r['user_id'] ) ) { |
| 465 | | $total_sql['where'][] = $wpdb->prepare( "m.group_id = g.id AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $r['user_id'] ); |
| 466 | | } |
| 467 | | |
| 468 | | // Already escaped in the paginated results block |
| 469 | | if ( ! empty( $include ) ) { |
| 470 | | $total_sql['where'][] = "g.id IN ({$include})"; |
| 471 | | } |
| 472 | | |
| 473 | | // Already escaped in the paginated results block |
| 474 | | if ( ! empty( $exclude ) ) { |
| 475 | | $total_sql['where'][] = "g.id NOT IN ({$exclude})"; |
| 476 | | } |
| 477 | | |
| 478 | | $total_sql['where'][] = "g.id = gm1.group_id"; |
| 479 | | $total_sql['where'][] = "g.id = gm2.group_id"; |
| 480 | | $total_sql['where'][] = "gm2.meta_key = 'last_activity'"; |
| 481 | | |
| 482 | | $t_sql = $total_sql['select']; |
| 483 | | |
| 484 | | if ( ! empty( $total_sql['where'] ) ) { |
| 485 | | $t_sql .= " WHERE " . join( ' AND ', (array) $total_sql['where'] ); |
| 486 | | } |
| | 453 | // Do not use! This is passed as the second parameter in the |
| | 454 | // 'bp_groups_get_total_groups_sql' filter for backpat only |
| | 455 | $total_sql = self::get_deprecated_total_groups_sql( $sql, $r ); |
| | 919 | |
| | 920 | /** |
| | 921 | * Get older total groups SQL array. |
| | 922 | * |
| | 923 | * The new total groups SQL statement uses the paged groups SQL instead of |
| | 924 | * regenerating a separate SQL statement that will become inaccurate. |
| | 925 | * |
| | 926 | * This will be deprecated in a future release and is only used for backpat |
| | 927 | * for plugins already utilizing this older SQL statement in the |
| | 928 | * 'bp_groups_get_total_groups_sql' filter. |
| | 929 | * |
| | 930 | * @since BuddyPress (1.8) |
| | 931 | * |
| | 932 | * @param array $sql The paged groups SQL array |
| | 933 | * @param array $r The passed arguments from bp_has_groups() |
| | 934 | * @return array |
| | 935 | */ |
| | 936 | protected static function get_deprecated_total_groups_sql( $sql = array(), $r = array() ) { |
| | 937 | global $wpdb; |
| | 938 | |
| | 939 | $bp = buddypress(); |
| | 940 | |
| | 941 | $total_sql = array(); |
| | 942 | |
| | 943 | // Setup some variables again |
| | 944 | $include = wp_parse_id_list( $r['include'] ); |
| | 945 | $include = $wpdb->escape( implode( ',', $include ) ); |
| | 946 | |
| | 947 | $exclude = wp_parse_id_list( $r['exclude'] ); |
| | 948 | $exclude = $wpdb->escape( implode( ',', $exclude ) ); |
| | 949 | |
| | 950 | // start $total_sql array |
| | 951 | |
| | 952 | $total_sql['select'] = "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name} g, {$bp->groups->table_name_members} gm1, {$bp->groups->table_name_groupmeta} gm2"; |
| | 953 | |
| | 954 | if ( ! empty( $r['user_id'] ) ) { |
| | 955 | $total_sql['select'] .= ", {$bp->groups->table_name_members} m"; |
| | 956 | } |
| | 957 | |
| | 958 | if ( ! empty( $sql['hidden'] ) ) { |
| | 959 | $total_sql['where'][] = "g.status != 'hidden'"; |
| | 960 | } |
| | 961 | |
| | 962 | if ( ! empty( $sql['search'] ) ) { |
| | 963 | $search_terms = esc_sql( like_escape( $r['search_terms'] ) ); |
| | 964 | $total_sql['where'][] = "( g.name LIKE '%%{$search_terms}%%' OR g.description LIKE '%%{$search_terms}%%' )"; |
| | 965 | } |
| | 966 | |
| | 967 | if ( ! empty( $r['user_id'] ) ) { |
| | 968 | $total_sql['where'][] = $wpdb->prepare( "m.group_id = g.id AND m.user_id = %d AND m.is_confirmed = 1 AND m.is_banned = 0", $r['user_id'] ); |
| | 969 | } |
| | 970 | |
| | 971 | // Already escaped in the paginated results block |
| | 972 | if ( ! empty( $include ) ) { |
| | 973 | $total_sql['where'][] = "g.id IN ({$include})"; |
| | 974 | } |
| | 975 | |
| | 976 | // Already escaped in the paginated results block |
| | 977 | if ( ! empty( $exclude ) ) { |
| | 978 | $total_sql['where'][] = "g.id NOT IN ({$exclude})"; |
| | 979 | } |
| | 980 | |
| | 981 | $total_sql['where'][] = "g.id = gm1.group_id"; |
| | 982 | $total_sql['where'][] = "g.id = gm2.group_id"; |
| | 983 | $total_sql['where'][] = "gm2.meta_key = 'last_activity'"; |
| | 984 | |
| | 985 | return $total_sql; |
| | 986 | } |