Skip to:
Content

BuddyPress.org

Ticket #5099: 5099.01.patch

File 5099.01.patch, 5.2 KB (added by r-a-y, 12 years ago)
  • bp-groups/bp-groups-classes.php

    class BP_Groups_Group { 
    350350
    351351                $r = wp_parse_args( $args, $defaults );
    352352
    353                 $sql       = array();
    354                 $total_sql = array();
     353                $sql = array();
    355354
    356355                $sql['select'] = "SELECT g.*, gm1.meta_value AS total_member_count, gm2.meta_value AS last_activity";
    357356                $sql['from']   = " FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2,";
    class BP_Groups_Group { 
    447446                $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', join( ' ', (array) $sql ), $sql );
    448447                $paged_groups     = $wpdb->get_results( $paged_groups_sql );
    449448
    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 );
    463452
    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 );
    487456
    488457                // Get total group results
    489                 $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $t_sql, $total_sql );
     458                // For the 'bp_groups_get_total_groups_sql' filter, plugin devs new to BP 1.8
     459                // should do their checks against $sql and skip $total_sql (see above)
     460                $total_groups_sql = apply_filters( 'bp_groups_get_total_groups_sql', $t_sql, $total_sql, $sql );
    490461                $total_groups     = $wpdb->get_var( $total_groups_sql );
    491462
    492463                $group_ids = array();
    class BP_Groups_Group { 
    945916
    946917                return $ids;
    947918        }
     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        }
    948987}
    949988
    950989/**