Skip to:
Content

BuddyPress.org

Changeset 8224


Ignore:
Timestamp:
03/31/2014 09:27:11 PM (11 years ago)
Author:
boonebgorges
Message:

When prefetching group data for an activity loop, avoid the query when there's nothing to fetch

When there are no uncached_ids, don't attempt to query. There's nothing to get,
and it will result in an invalidly formatted SQL query.

Fixes #5503

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-groups/bp-groups-activity.php

    r8127 r8224  
    144144        }
    145145
    146         global $wpdb, $bp;
    147         $uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) );
    148         $groups = $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name} WHERE id IN ({$uncached_ids_sql})" );
    149         foreach ( $groups as $group ) {
    150             wp_cache_set( $group->id, $group, 'bp_groups' );
     146        if ( ! empty( $uncached_ids ) ) {
     147            global $wpdb, $bp;
     148            $uncached_ids_sql = implode( ',', wp_parse_id_list( $uncached_ids ) );
     149            $groups = $wpdb->get_results( "SELECT * FROM {$bp->groups->table_name} WHERE id IN ({$uncached_ids_sql})" );
     150            foreach ( $groups as $group ) {
     151                wp_cache_set( $group->id, $group, 'bp_groups' );
     152            }
    151153        }
    152154    }
  • trunk/tests/testcases/groups/cache.php

    r7956 r8224  
    102102    }
    103103
     104    /**
     105     * @group bp_groups_prefetch_activity_object_data
     106     */
     107    public function test_bp_groups_prefetch_activity_object_data_all_cached() {
     108        $g = $this->factory->group->create();
     109
     110        // Prime cache
     111        groups_get_group( array( 'group_id' => $g ) );
     112
     113        // fake an activity
     114        $a = new stdClass;
     115        $a->component = buddypress()->groups->id;
     116        $a->item_id = $g;
     117        $activities = array(
     118            $a,
     119        );
     120
     121        bp_groups_prefetch_activity_object_data( $activities );
     122
     123        // This assertion is not really necessary - just checks to see
     124        // whether a fatal error has occurred above
     125        $this->assertNotEmpty( wp_cache_get( $g, 'bp_groups' ) );
     126    }
     127
     128    /**
     129     * @group bp_groups_prefetch_activity_object_data
     130     */
     131    public function test_bp_groups_prefetch_activity_object_data_some_cached() {
     132        $g1 = $this->factory->group->create();
     133        $g2 = $this->factory->group->create();
     134
     135        // Prime cache
     136        groups_get_group( array( 'group_id' => $g1 ) );
     137
     138        // fake activities
     139        $a1 = new stdClass;
     140        $a1->component = buddypress()->groups->id;
     141        $a1->item_id = $g1;
     142
     143        $a2 = new stdClass;
     144        $a2->component = buddypress()->groups->id;
     145        $a2->item_id = $g2;
     146
     147        $activities = array(
     148            $a1,
     149            $a2,
     150        );
     151
     152        bp_groups_prefetch_activity_object_data( $activities );
     153
     154        $this->assertNotEmpty( wp_cache_get( $g1, 'bp_groups' ) );
     155        $this->assertNotEmpty( wp_cache_get( $g2, 'bp_groups' ) );
     156    }
    104157}
Note: See TracChangeset for help on using the changeset viewer.