Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
09/10/2009 01:24:01 AM (12 years ago)
Author:
apeatling
Message:

Added support for significantly more advanced activity stream filtering.

File:
1 edited

Legend:

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

    r1819 r1839  
    144144    }
    145145   
    146     function get_activity_for_user( $user_id, $max_items, $since, $limit, $page, $filter ) {
     146    function get_activity_for_user( $user_id, $max_items, $limit, $page, $filter ) {
    147147        global $wpdb, $bp;
    148148
     
    155155            $max_sql = $wpdb->prepare( "LIMIT %d", $max_items );
    156156       
     157        /* Sort out filtering */
    157158        if ( $filter )
    158             $filter_sql = $wpdb->prepare( "AND component_name = %s", $filter );
     159            $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
    159160       
    160161        if ( $limit && $page && $max_items )
    161             $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql", $user_id, $since ) );
     162            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql", $user_id ) );
    162163        else
    163             $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql", $user_id, $since ) );
    164        
    165         $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->activity->table_name} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql $filter_sql ORDER BY date_recorded DESC $max_sql", $user_id, $since ) );
     164            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql", $user_id ) );
     165       
     166        $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->activity->table_name} WHERE user_id = %d $privacy_sql $filter_sql ORDER BY date_recorded DESC $max_sql", $user_id ) );
    166167       
    167168        return array( 'activities' => $activities, 'total' => (int)$total_activities );
    168169    }
    169170   
    170     function get_activity_for_friends( $user_id, $max_items, $since, $max_items_per_friend, $limit, $page, $filter ) {
     171    function get_activity_for_friends( $user_id, $max_items, $max_items_per_friend, $limit, $page, $filter ) {
    171172        global $wpdb, $bp;
    172173       
     
    182183            $max_sql = $wpdb->prepare( "LIMIT %d", $max_items );
    183184
     185        /* Sort out filtering */
    184186        if ( $filter )
    185             $filter_sql = $wpdb->prepare( "AND component_name = %s", $filter );
    186 
    187         $since = strtotime($since);
     187            $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
    188188
    189189        $friend_ids = friends_get_friend_user_ids( $user_id );
     
    195195       
    196196        if ( $limit && $page && $max_items )
    197             $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $pag_sql", $since ) );
     197            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $pag_sql" ) );
    198198        else
    199             $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql", $since ) );             
    200 
    201         $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(user_id) FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) $filter_sql ORDER BY date_recorded DESC $max_sql", $since ) );
     199            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $pag_sql $max_sql" ) );           
     200
     201        $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(user_id) FROM {$bp->activity->table_name} WHERE user_id IN ({$friend_ids}) $filter_sql ORDER BY date_recorded DESC $max_sql" ) );
    202202       
    203203        return array( 'activities' => $activities, 'total' => (int)$total_activities );
     
    212212        if ( $max )
    213213            $max_sql = $wpdb->prepare( "LIMIT %d", $max );
    214 
     214           
     215        /* Sort out filtering */
    215216        if ( $filter )
    216             $filter_sql = $wpdb->prepare( "AND component_name = %s", $filter );
    217 
     217            $filter_sql = BP_Activity_Activity::get_filter_sql( $filter );
     218       
    218219        if ( $limit && $page && $max )
    219220            $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE hide_sitewide = 0 $filter_sql ORDER BY date_recorded DESC $pag_sql" ) );
     
    248249    }
    249250   
     251    function get_filter_sql( $filter_array ) {
     252        global $wpdb;
     253       
     254        if ( !empty( $filter_array['object'] ) ) {
     255            $object_filter = explode( ',', $filter_array['object'] );
     256            $object_sql = ' AND ( ';
     257           
     258            $counter = 1;
     259            foreach( (array) $object_filter as $object ) {
     260                $object_sql .= $wpdb->prepare( "component_name = %s", trim( $object ) );
     261               
     262                if ( $counter != count( $object_filter ) )
     263                    $object_sql .= ' || ';
     264               
     265                $counter++;
     266            }
     267           
     268            $object_sql .= ' )';
     269        }
     270
     271        if ( !empty( $filter_array['action'] ) ) {
     272            $action_filter = explode( ',', $filter_array['action'] );
     273            $action_sql = ' AND ( ';
     274           
     275            $counter = 1;
     276            foreach( (array) $action_filter as $action ) {
     277                $action_sql .= $wpdb->prepare( "component_action = %s", trim( $action ) );
     278               
     279                if ( $counter != count( $action_filter ) )
     280                    $action_sql .= ' || ';
     281               
     282                $counter++;
     283            }
     284           
     285            $action_sql .= ' )';
     286        }
     287
     288        if ( !empty( $filter_array['primary_id'] ) ) {
     289            $pid_filter = explode( ',', $filter_array['action'] );
     290            $pid_sql = ' AND ( ';
     291           
     292            $counter = 1;
     293            foreach( (array) $pid_filter as $pid ) {
     294                $pid_sql .= $wpdb->prepare( "item_id = %s", trim( $pid ) );
     295               
     296                if ( $counter != count( $pid_filter ) )
     297                    $pid_sql .= ' || ';
     298               
     299                $counter++;
     300            }
     301           
     302            $pid_sql .= ' )';
     303        }
     304
     305        if ( !empty( $filter_array['secondary_id'] ) ) {
     306            $sid_filter = explode( ',', $filter_array['action'] );
     307            $sid_sql = ' AND ( ';
     308           
     309            $counter = 1;
     310            foreach( (array) $sid_filter as $sid ) {
     311                $sid_sql .= $wpdb->prepare( "secondary_item_id = %s", trim( $sid ) );
     312               
     313                if ( $counter != count( $sid_filter ) )
     314                    $sid_sql .= ' || ';
     315               
     316                $counter++;
     317            }
     318           
     319            $sid_sql .= ' )';
     320        }
     321       
     322        return $object_sql . $action_sql . $pid_sql . $sid_sql;
     323    }
     324   
    250325    function get_last_updated() {
    251326        global $bp, $wpdb;
Note: See TracChangeset for help on using the changeset viewer.