Skip to:
Content

BuddyPress.org

Changeset 1566


Ignore:
Timestamp:
06/23/2009 12:57:13 AM (17 years ago)
Author:
apeatling
Message:

Re-factored bp-actvity so that activity items are not linked so closely to database records. Item id's are now optional for activity items and activity content is also added at the point of record. Eliminated the sitewide and non-cached tables as only the one is needed with this re-factoring.

The code is still completely backwards compatible, however you should be using the bp_activity_add() bp_activity_update() and bp_activity_delete_*() functions from here forwards. All BuddyPress components will be updated to use the new functions and further work will be done on the bp-activity data schema for 1.1 to eliminate deprecated fields.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity.php

    r1545 r1566  
    11<?php
    22
    3 define ( 'BP_ACTIVITY_DB_VERSION', '1300' );
     3define ( 'BP_ACTIVITY_DB_VERSION', '1716' );
    44
    55/* Define the slug for the component */
    66if ( !defined( 'BP_ACTIVITY_SLUG' ) )
    77        define ( 'BP_ACTIVITY_SLUG', 'activity' );
    8 
    9 /* How long before activity items in streams are re-cached? */
    10 if ( !defined( 'BP_ACTIVITY_CACHE_LENGTH' ) )
    11         define ( 'BP_ACTIVITY_CACHE_LENGTH', '6 HOURS' );
    128
    139require ( BP_PLUGIN_DIR . '/bp-activity/bp-activity-classes.php' );
     
    3026                $charset_collate = "DEFAULT CHARACTER SET $wpdb->charset";
    3127       
    32         $sql[] = "CREATE TABLE {$bp->activity->table_name_user_activity} (
    33                                 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    34                                 user_id bigint(20) NOT NULL,
    35                                 component_name varchar(75) NOT NULL,
    36                                 component_action varchar(75) NOT NULL,
    37                                 item_id bigint(20) NOT NULL,
    38                                 secondary_item_id bigint(20) NOT NULL,
    39                                 date_recorded datetime NOT NULL,
    40                                 is_private tinyint(1) NOT NULL DEFAULT 0,
    41                                 no_sitewide_cache tinyint(1) NOT NULL DEFAULT 0,
    42                             KEY item_id (item_id),
    43                                 KEY user_id (user_id),
    44                             KEY is_private (is_private),
    45                                 KEY component_name (component_name)
    46                            ) {$charset_collate};";
    47 
    48         $sql[] = "CREATE TABLE {$bp->activity->table_name_user_activity_cached} (
     28        $sql[] = "CREATE TABLE {$bp->activity->table_name} (
    4929                                id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    5030                                user_id bigint(20) NOT NULL,
     
    5535                                item_id bigint(20) NOT NULL,
    5636                                secondary_item_id bigint(20) NOT NULL,
    57                                 date_cached datetime NOT NULL,
    5837                                date_recorded datetime NOT NULL,
    59                                 is_private tinyint(1) NOT NULL DEFAULT 0,
    60                                 KEY date_cached (date_cached),
    61                                 KEY date_recorded (date_recorded),
    62                             KEY is_private (is_private),
    63                                 KEY user_id (user_id),
    64                                 KEY item_id (item_id),
    65                                 KEY component_name (component_name)
    66                            ) {$charset_collate};";
    67 
    68         $sql[] = "CREATE TABLE {$bp->activity->table_name_sitewide} (
    69                                 id bigint(20) NOT NULL AUTO_INCREMENT PRIMARY KEY,
    70                                 user_id bigint(20) NOT NULL,
    71                                 item_id bigint(20) NOT NULL,
    72                                 secondary_item_id bigint(20),
    73                                 content longtext NOT NULL,
    74                                 primary_link varchar(150) NOT NULL,
    75                                 component_name varchar(75) NOT NULL,
    76                                 component_action varchar(75) NOT NULL,
    77                                 date_cached datetime NOT NULL,
    78                                 date_recorded datetime NOT NULL,
    79                                 KEY date_cached (date_cached),
     38                                hide_sitewide bool DEFAULT 0,
    8039                                KEY date_recorded (date_recorded),
    8140                                KEY user_id (user_id),
     
    8746        dbDelta($sql);
    8847       
    89         if ( '' == get_site_option( 'bp-activity-db-merge' ) || !get_site_option( 'bp-activity-db-merge' ) ) {
    90                 $users = $wpdb->get_col( "SELECT ID FROM " . CUSTOM_USER_TABLE );
    91                
    92                 foreach ( $users as $user_id ) {
    93                         BP_Activity_Activity::convert_tables_for_user( $user_id );
    94                         BP_Activity_Activity::kill_tables_for_user( $user_id );
    95                 }
    96                
    97                 add_site_option( 'bp-activity-db-merge', 1 );
    98         }
    99 
     48        /* Drop the old sitewide and user activity tables */
     49        $wpdb->query( "DROP TABLE {$wpdb->base_prefix}bp_activity_user_activity" );
     50        $wpdb->query( "DROP TABLE {$wpdb->base_prefix}bp_activity_sitewide" );
     51       
    10052        update_site_option( 'bp-activity-db-version', BP_ACTIVITY_DB_VERSION );
    10153}
     
    11163        global $bp, $wpdb, $current_blog;
    11264
    113         $bp->activity->table_name_user_activity = $wpdb->base_prefix . 'bp_activity_user_activity';
    114         $bp->activity->table_name_user_activity_cached = $wpdb->base_prefix . 'bp_activity_user_activity_cached';
    115         $bp->activity->table_name_sitewide = $wpdb->base_prefix . 'bp_activity_sitewide';
    116        
     65        $bp->activity->table_name = $wpdb->base_prefix . 'bp_activity_user_activity_cached';
    11766        $bp->activity->image_base = BP_PLUGIN_URL . '/bp-activity/images';
    11867        $bp->activity->slug = BP_ACTIVITY_SLUG;
    119        
    120         $bp->version_numbers->activity = BP_ACTIVITY_VERSION;
    12168
    12269        if ( is_site_admin() && get_site_option( 'bp-activity-db-version' ) < BP_ACTIVITY_DB_VERSION  )
     
    12774
    12875function bp_activity_setup_root_component() {
    129         /* Register 'groups' as a root component */
     76        /* Register 'activity' as a root component (for RSS feed use) */
    13077        bp_core_add_root_component( BP_ACTIVITY_SLUG );
    13178}
     
    178125}
    179126
    180 /***** Actions **********/
    181 
     127/***** Actions *********/
     128
     129function bp_activity_action_sitewide_feed() {
     130        global $bp, $wp_query;
     131
     132        if ( $bp->current_component != $bp->activity->slug || $bp->current_action != 'feed' || $bp->displayed_user->id )
     133                return false;
     134
     135        $wp_query->is_404 = false;
     136        status_header( 200 );
     137
     138        include_once( 'bp-activity/feeds/bp-activity-sitewide-feed.php' );
     139        die;
     140}
     141add_action( 'wp', 'bp_activity_action_sitewide_feed', 3 );
     142
     143function bp_activity_action_personal_feed() {
     144        global $bp, $wp_query; 
     145
     146        if ( $bp->current_component != $bp->activity->slug || !$bp->displayed_user->id || $bp->current_action != 'feed' )
     147                return false;
     148       
     149        $wp_query->is_404 = false;
     150        status_header( 200 );
     151
     152        include_once( 'bp-activity/feeds/bp-activity-personal-feed.php' );
     153        die;
     154}
     155add_action( 'wp', 'bp_activity_action_personal_feed', 3 );
     156
     157function bp_activity_action_friends_feed() {
     158        global $bp, $wp_query;
     159
     160        if ( $bp->current_component != $bp->activity->slug || !$bp->displayed_user->id || $bp->current_action != 'my-friends' || $bp->action_variables[0] != 'feed' )
     161                return false;
     162
     163        $wp_query->is_404 = false;     
     164        status_header( 200 );
     165
     166        include_once( 'bp-activity/feeds/bp-activity-friends-feed.php' );
     167        die;   
     168}
     169add_action( 'wp', 'bp_activity_action_friends_feed', 3 );
     170
     171
     172/**** BUSINESS FUNCTIONS *********/
     173
     174function bp_activity_add( $args ) {
     175        global $bp, $wpdb;
     176       
     177        $defaults = array(
     178                'user_id' => $bp->loggedin_user->id,
     179                'content' => false,
     180                'primary_link' => false,
     181                'component_name' => false,
     182                'component_action' => false,
     183                'item_id' => false,
     184                'secondary_item_id' => false,
     185                'recorded_time' => time(),
     186                'hide_sitewide' => false
     187        );
     188
     189        $r = wp_parse_args( $args, $defaults );
     190        extract( $r, EXTR_SKIP );
     191       
     192        $activity = new BP_Activity_Activity;
     193        $activity->user_id = $user_id;
     194        $activity->content = $content;
     195        $activity->primary_link = $primary_link;
     196        $activity->component_name = $component_name;
     197        $activity->component_action = $component_action;
     198        $activity->item_id = $item_id;
     199        $activity->secondary_item_id = $secondary_item_id;
     200        $activity->date_recorded = $recorded_time;
     201        $activity->hide_sitewide = $hide_sitewide;
     202
     203        if ( !$activity->save() )
     204                return false;
     205
     206        do_action( 'bp_activity_add', $args );
     207       
     208        return true;
     209}
     210
     211function bp_activity_update( $args ) {
     212        global $bp, $wpdb;
     213       
     214        extract( $args );
     215       
     216        $defaults = array(
     217                'user_id' => $bp->loggedin_user->id,
     218                'content' => false,
     219                'component_name' => false,
     220                'component_action' => false,
     221                'item_id' => false,
     222                'secondary_item_id' => false,
     223                'recorded_time' => time(),
     224                'hide_sitewide' => false
     225        );
     226       
     227        $activity = new BP_Activity_Activity( $user_id, $component_name, $component_action, $item_id, $secondary_item_id );
     228        $activity->user_id = $user_id;
     229        $activity->content = $content;
     230        $activity->primary_link = $primary_link;
     231        $activity->component_name = $component_name;
     232        $activity->component_action = $component_action;
     233        $activity->item_id = $item_id;
     234        $activity->secondary_item_id = $secondary_item_id;
     235        $activity->date_recorded = $recorded_time;
     236        $activity->hide_sitewide = $hide_sitewide;
     237               
     238        if ( !$activity->save() )
     239                return false;
     240
     241        do_action( 'bp_activity_update', $args );
     242       
     243        return true;
     244}
     245
     246/* There are multiple ways to delete activity items, depending on the information you have at the time. */
     247
     248function bp_activity_delete_by_item_id( $user_id, $component_name, $component_action, $item_id, $secondary_item_id = false ) { 
     249        if ( !BP_Activity_Activity::delete_by_item_id( $user_id, $component_name, $component_action, $item_id, $secondary_item_id ) )
     250                return false;
     251
     252        do_action( 'bp_activity_delete_by_item_id', $user_id, $component_name, $component_action, $item_id, $secondary_item_id );
     253
     254        return true;
     255}
     256
     257function bp_activity_delete_by_activity_id( $activity_id ) {
     258        if ( !BP_Activity_Activity::delete_by_activity_id( $activity_id ) )
     259                return false;
     260
     261        do_action( 'bp_activity_delete_by_activity_id', $activity_id );
     262
     263        return true;   
     264}
     265
     266function bp_activity_delete_by_content( $user_id, $content, $component_name, $component_action ) {
     267        if ( !BP_Activity_Activity::delete_by_content( $user_id, $content, $component_name, $component_action ) )
     268                return false;
     269
     270        do_action( 'bp_activity_delete_by_content', $user_id, $content, $component_name, $component_action );
     271
     272        return true;
     273}
     274
     275function bp_activity_delete_for_user_by_component( $user_id, $component_name ) {
     276        if ( !BP_Activity_Activity::delete_for_user_by_component( $user_id, $component_name ) )
     277                return false;
     278               
     279        do_action( 'bp_activity_delete_for_user_by_component', $user_id, $component_name );
     280       
     281        return true;
     282}
     283
     284function bp_activity_get_last_updated() {
     285        return BP_Activity_Activity::get_last_updated();
     286}
     287
     288function bp_activity_get_sitewide_activity( $max_items = 30, $pag_num = false, $pag_page = false ) {
     289        return BP_Activity_Activity::get_sitewide_activity( $max_items, $pag_num, $pag_page );
     290}
     291
     292function bp_activity_get_user_activity( $user_id, $max_items = 30, $since = '-4 weeks', $pag_num = false, $pag_page = false ) {
     293        return BP_Activity_Activity::get_activity_for_user( $user_id, $max_items, $since, $pag_num, $pag_page );
     294}
     295
     296function bp_activity_get_friends_activity( $user_id, $max_items = 30, $since = '-4 weeks', $max_items_per_friend = false, $pag_num = false, $pag_page = false ) {
     297        return BP_Activity_Activity::get_activity_for_friends( $user_id, $max_items, $since, $max_items_per_friend, $pag_num, $pag_page );
     298}
     299
     300function bp_activity_remove_data( $user_id ) {
     301        // Clear the user's activity from the sitewide stream and clear their activity tables
     302        BP_Activity_Activity::delete_for_user( $user_id );
     303       
     304        // Remove the deleted users activity tables
     305        BP_Activity_Activity::kill_tables_for_user( $user_id );
     306       
     307        do_action( 'bp_activity_remove_data', $user_id );
     308}
     309add_action( 'wpmu_delete_user', 'bp_activity_remove_data' );
     310add_action( 'delete_user', 'bp_activity_remove_data' );
     311
     312/* Ordering function - don't call this directly */
     313function bp_activity_order_by_date( $a, $b ) {
     314        return strcasecmp( $b['date_recorded'], $a['date_recorded'] ); 
     315}
     316
     317/**** DEPRECATED FUNCTIONS (DO NOT USE IN YOUR CODE) **************/
     318
     319/* DEPRECATED - use bp_activity_add() */
    182320function bp_activity_record( $item_id, $component_name, $component_action, $is_private, $secondary_item_id = false, $user_id = false, $secondary_user_id = false, $recorded_time = false ) {
    183321        global $bp, $wpdb;
     
    189327                $recorded_time = time();
    190328       
    191         $activity = new BP_Activity_Activity;
    192         $activity->item_id = $item_id;
    193         $activity->secondary_item_id = $secondary_item_id;
    194         $activity->user_id = $user_id;
    195         $activity->component_name = $component_name;
    196         $activity->component_action = $component_action;
    197         $activity->date_recorded = $recorded_time;
    198         $activity->is_private = $is_private;
    199 
    200         $loggedin_user_save = $activity->save();
    201        
    202         /* Save an activity entry for both logged in and secondary user. For example for a new friend connection
    203            you would want to show "X and Y are now friends" on both users activity stream */
     329        $args = compact( 'user_id', 'content', 'component_name', 'component_action', 'item_id', 'secondary_item_id', 'recorded_time' );
     330        bp_activity_add( $args );
     331       
    204332        if ( $secondary_user_id  ) {
    205                 $activity = new BP_Activity_Activity;
    206                 $activity->item_id = $item_id;
    207                 $activity->user_id = $secondary_user_id;
    208                 $activity->component_name = $component_name;
    209                 $activity->component_action = $component_action;
    210                 $activity->date_recorded = $recorded_time;
    211                 $activity->is_private = $is_private;
    212 
    213                 // We don't want to record this on the sitewide stream, otherwise we will get duplicates.
    214                 $activity->no_sitewide_cache = true;
    215 
    216                 $secondary_user_save = $activity->save();
     333                $hide_sitewide = true;
     334                $args = compact( 'user_id', 'content', 'component_name', 'component_action', 'item_id', 'secondary_item_id', 'recorded_time', 'hide_sitewide' );
     335                bp_activity_add( $args );
    217336        }
    218337       
     
    222341}
    223342
    224 function bp_activity_action_sitewide_feed() {
    225         global $bp, $wp_query;
    226 
    227         if ( $bp->current_component != $bp->activity->slug || $bp->current_action != 'feed' || $bp->displayed_user->id )
    228                 return false;
    229 
    230         $wp_query->is_404 = false;
    231         status_header( 200 );
    232 
    233         include_once( 'bp-activity/feeds/bp-activity-sitewide-feed.php' );
    234         die;
    235 }
    236 add_action( 'wp', 'bp_activity_action_sitewide_feed', 3 );
    237 
    238 function bp_activity_action_personal_feed() {
    239         global $bp, $wp_query; 
    240 
    241         if ( $bp->current_component != $bp->activity->slug || !$bp->displayed_user->id || $bp->current_action != 'feed' )
    242                 return false;
    243        
    244         $wp_query->is_404 = false;
    245         status_header( 200 );
    246 
    247         include_once( 'bp-activity/feeds/bp-activity-personal-feed.php' );
    248         die;
    249 }
    250 add_action( 'wp', 'bp_activity_action_personal_feed', 3 );
    251 
    252 function bp_activity_action_friends_feed() {
    253         global $bp, $wp_query;
    254 
    255         if ( $bp->current_component != $bp->activity->slug || !$bp->displayed_user->id || $bp->current_action != 'my-friends' || $bp->action_variables[0] != 'feed' )
    256                 return false;
    257 
    258         $wp_query->is_404 = false;     
    259         status_header( 200 );
    260 
    261         include_once( 'bp-activity/feeds/bp-activity-friends-feed.php' );
    262         die;   
    263 }
    264 add_action( 'wp', 'bp_activity_action_friends_feed', 3 );
    265 
    266 function bp_activity_get_last_updated() {
    267         return BP_Activity_Activity::get_last_updated();
    268 }
    269 
    270 function bp_activity_get_sitewide_activity( $max_items = 30, $pag_num = false, $pag_page = false ) {
    271         return BP_Activity_Activity::get_sitewide_activity( $max_items, $pag_num, $pag_page );
    272 }
    273 
    274 function bp_activity_get_user_activity( $user_id, $max_items = 30, $since = '-4 weeks', $pag_num = false, $pag_page = false ) {
    275         return BP_Activity_Activity::get_activity_for_user( $user_id, $max_items, $since, $pag_num, $pag_page );
    276 }
    277 
    278 function bp_activity_get_friends_activity( $user_id, $max_items = 30, $since = '-4 weeks', $max_items_per_friend = false, $pag_num = false, $pag_page = false ) {
    279         return BP_Activity_Activity::get_activity_for_friends( $user_id, $max_items, $since, $max_items_per_friend, $pag_num, $pag_page );
    280 }
    281 
     343/* DEPRECATED - use bp_activity_delete_by_item_id() */
    282344function bp_activity_delete( $item_id, $component_name, $component_action, $user_id, $secondary_item_id ) {     
    283         if ( !BP_Activity_Activity::delete( $item_id, $component_name, $component_action, $user_id, $secondary_item_id ) )
     345        if ( !bp_activity_delete_by_item_id( $user_id, $component_name, $component_action, $item_id, $secondary_item_id ) )
    284346                return false;
    285347               
     
    289351}
    290352
    291 function bp_activity_order_by_date( $a, $b ) {
    292         return strcasecmp( $b['date_recorded'], $a['date_recorded'] ); 
    293 }
    294 
    295 function bp_activity_remove_data( $user_id ) {
    296         // Clear the user's activity from the sitewide stream and clear their activity tables
    297         BP_Activity_Activity::delete_activity_for_user( $user_id );
    298        
    299         // Remove the deleted users activity tables
    300         BP_Activity_Activity::kill_tables_for_user( $user_id );
    301        
    302         do_action( 'bp_activity_remove_data', $user_id );
    303 }
    304 add_action( 'wpmu_delete_user', 'bp_activity_remove_data' );
    305 add_action( 'delete_user', 'bp_activity_remove_data' );
    306 
    307 
    308353?>
  • trunk/bp-activity/bp-activity-classes.php

    r1366 r1566  
    1010        var $component_action;
    1111        var $date_recorded;
    12         var $is_private = false;
    13         var $no_sitewide_cache = false;
    14        
    15         var $table_name;
    16         var $table_name_cached;
    17         var $for_secondary_user = false;
    18        
    19         function bp_activity_activity( $id = null, $populate = true ) {
     12        var $hide_sitewide = false;
     13       
     14        function bp_activity_activity( $args = false, $populate = false ) {
    2015                global $bp;
    2116               
    22                 if ( $id ) {
    23                         $this->id = $id;
     17                if ( $args ) {
     18                        extract( $args );
     19                       
     20                        $this->user_id = $user_id;
     21                        $this->component_name = $component_name;
     22                        $this->component_action = $component_action;
     23                        $this->item_id = $item_id;
     24                        $this->secondary_item_id = $secondary_item_id;
    2425                       
    2526                        if ( $populate )
     
    3132                global $wpdb, $bp;
    3233               
    33                 $activity = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$this->table_name} WHERE id = %d", $this->id ) );
    34 
     34                $activity = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE id = %d", $this->id ) );
     35               
     36                $this->id = $activity->id;
    3537                $this->item_id = $activity->item_id;
    3638                $this->secondary_item_id = $activity->secondary_item_id;
    3739                $this->user_id = $activity->user_id;
     40                $this->content = $activity->content;
     41                $this->primary_link = $activity->primary_link;
    3842                $this->component_name = $activity->component_name;
    3943                $this->component_action = $activity->component_action;
    4044                $this->date_recorded = $activity->date_recorded;
    41                 $this->is_private = $activity->is_private;
    42                 $this->no_sitewide_cache = $activity->no_sitewide_cache;
     45                $this->hide_sitewide = $activity->hide_sitewide;
    4346        }
    4447       
     
    4851                do_action( 'bp_activity_before_save', $this );
    4952
    50                 if ( !$this->item_id || !$this->user_id || $this->is_private || !$this->component_name )
    51                         return false;
    52                        
    53                 // Set the table names
    54                 $this->table_name = $bp->activity->table_name_user_activity;
    55                 $this->table_name_cached = $bp->activity->table_name_user_activity_cached;
    56 
    57                 if ( !$this->exists() ) {
    58                         // Insert the new activity into the activity table.
    59                         $activity = $wpdb->query( $wpdb->prepare( "INSERT INTO {$this->table_name} ( item_id, secondary_item_id, user_id, component_name, component_action, date_recorded, is_private, no_sitewide_cache ) VALUES ( %d, %d, %d, %s, %s, FROM_UNIXTIME(%d), %d, %d )", $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action, $this->date_recorded, $this->is_private, $this->no_sitewide_cache ) );
    60 
    61                         // Fetch the formatted activity content so we can add it to the cache.
     53                if ( !$this->user_id || !$this->component_name || !$this->component_action )
     54                        return false;
     55               
     56                /***
     57                 * Before v1.1 of BuddyPress, activity content was calculated at a later point. This is no longer the
     58                 * case, to to be backwards compatible we need to fetch content here to continue.
     59                 */
     60                if ( empty( $this->content ) || !$this->content ) {
    6261                        if ( function_exists( $bp->{$this->component_name}->format_activity_function ) ) {
    63                                 if ( !$activity_content = call_user_func( $bp->{$this->component_name}->format_activity_function, $this->item_id, $this->user_id, $this->component_action, $this->secondary_item_id, $this->for_secondary_user ) )
     62                                if ( !$fetched_content = call_user_func( $bp->{$this->component_name}->format_activity_function, $this->item_id, $this->user_id, $this->component_action, $this->secondary_item_id, $this->for_secondary_user ) )
    6463                                        return false;
    65                         }
    66                        
    67                         // Add the cached version of the activity to the cached activity table.
    68                         $activity_cached = $wpdb->query( $wpdb->prepare( "INSERT INTO {$this->table_name_cached} ( user_id, item_id, secondary_item_id, content, primary_link, component_name, component_action, date_cached, date_recorded, is_private ) VALUES ( %d, %d, %d, %s, %s, %s, %s, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d), %d )", $this->user_id, $this->item_id, $this->secondary_item_id, $activity_content['content'], $activity_content['primary_link'], $this->component_name, $this->component_action, time(), $this->date_recorded, $this->is_private ) );
    69                        
    70                         // Add the cached version of the activity to the sitewide activity table.
    71                         if ( !$this->no_sitewide_cache )
    72                                 $sitewide_cached = $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_sitewide} ( user_id, item_id, secondary_item_id, content, primary_link, component_name, component_action, date_cached, date_recorded ) VALUES ( %d, %d, %d, %s, %s, %s, %s, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d) )", $this->user_id, $this->item_id, $this->secondary_item_id, $activity_content['content'], $activity_content['primary_link'], $this->component_name, $this->component_action, time(), $this->date_recorded ) );
    73                        
    74                         if ( $activity && $activity_cached ) {
     64                               
     65                                $this->content = $fetched_content['content'];
     66                                $this->primary_link = $fetched_content['primary_link'];
     67                        }
     68                }
     69               
     70                if ( !$this->primary_link )
     71                        $this->primary_link = $bp->loggedin_user->domain;
     72               
     73                if ( $existing_activity_id = $this->exists() )
     74                        BP_Activity_Activity::delete_by_activity_id( $existing_activity_id );
     75               
     76                /* If we have an existing ID, update the activity item, otherwise insert it. */
     77                if ( $this->id ) {
     78                        if ( $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name} SET user_id = %d, component_name = %s, component_action = %s, content = %s, primary_link = %s, date_recorded = FROM_UNIXTIME(%d), item_id = %d, secondary_item_id = %d, hide_sitewide = %d WHERE id = %d", $this->user_id, $this->component_name, $this->component_action, $this->content, $this->primary_link, $this->date_recorded, $this->item_id, $this->secondary_item_id, $this->hide_sitewide, $this->id ) ) ) {
    7579                                do_action( 'bp_activity_after_save', $this );
    7680                                return true;
    7781                        }
    78                        
    79                         return false;
    80                 }
     82                } else {
     83                        if ( $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name} ( user_id, component_name, component_action, content, primary_link, date_recorded, item_id, secondary_item_id, hide_sitewide ) VALUES ( %d, %s, %s, %s, %s, FROM_UNIXTIME(%d), %d, %d, %d )", $this->user_id, $this->component_name, $this->component_action, $this->content, $this->primary_link, $this->date_recorded, $this->item_id, $this->secondary_item_id, $this->hide_sitewide ) ) ) {
     84                                do_action( 'bp_activity_after_save', $this );
     85                                return true;
     86                        }
     87                }
     88
     89                return false;
    8190        }
    8291       
    8392        function exists() {
    8493                global $wpdb, $bp;
    85                 return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$this->table_name} WHERE item_id = %d AND secondary_item_id = %d AND user_id = %d AND component_name = %s AND component_action = %s", $this->item_id, $this->secondary_item_id, $this->user_id, $this->component_name, $this->component_action ) );             
     94               
     95                /* If we have an item id, try and match on that, if not do a content match */
     96                if ( $this->item_id ) {
     97                        if ( $this->secondary_item_id )
     98                                $secondary_sql = $wpdb->prepare( " AND secondary_item_id = %d", $secondary_item_id );
     99                               
     100                        return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} WHERE user_id = %d AND item_id = %d{$secondary_sql} AND component_name = %s AND component_action = %s", $this->user_id, $this->item_id, $this->component_name, $this->component_action ) );         
     101                } else {
     102                        return $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$bp->activity->table_name} WHERE user_id = %d AND content = %s AND component_name = %s AND component_action = %s", $this->user_id, $this->content, $this->component_name, $this->component_action ) );                         
     103                }
    86104        }
    87105       
     
    93111                if ( !$user_id )
    94112                        return false;
    95                
    96                 if ( !$bp->activity )
    97                         bp_activity_setup_globals();
    98                
     113
    99114                if ( $secondary_item_id )
    100115                        $secondary_sql = $wpdb->prepare( "AND secondary_item_id = %d", $secondary_item_id );
    101116               
    102                 if ( $component_action ) {
     117                if ( $component_action )
    103118                        $component_action_sql = $wpdb->prepare( "AND component_action = %s AND user_id = %d", $component_action, $user_id );
    104                         $cached_component_action_sql = $wpdb->prepare( "AND component_action = %s", $component_action );
    105                 }
    106                                
    107                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_user_activity} WHERE item_id = %d {$secondary_sql} AND component_name = %s {$component_action_sql}", $item_id, $component_name ) );
    108                                
    109                 // Delete this entry from the user activity cache table and the sitewide cache table
    110                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_user_activity_cached} WHERE user_id = %d AND item_id = %d {$secondary_sql} AND component_name = %s {$cached_component_action_sql}", $user_id, $item_id, $component_name ) );
    111                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_sitewide} WHERE item_id = %d {$secondary_sql} AND component_name = %s {$component_action_sql}", $item_id, $component_name ) );
    112 
    113                 return true;
     119                               
     120                return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name} WHERE user_id = %d AND item_id = %d {$secondary_sql} AND component_name = %s {$cached_component_action_sql}", $user_id, $item_id, $component_name ) );
     121        }
     122       
     123        function delete_by_item_id( $user_id, $component_name, $component_action, $item_id, $secondary_item_id = false ) {
     124                return BP_Activity_Activity::delete( $item_id, $component_name, $component_action, $user_id, $secondary_item_id );
     125        }
     126
     127        function delete_by_activity_id( $activity_id ) {
     128                global $bp, $wpdb;
     129               
     130                return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name} WHERE id = %d", $activity_id ) );
     131        }
     132       
     133        function delete_by_content( $user_id, $content, $component_name, $component_action ) {
     134                global $bp, $wpdb;
     135               
     136                return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name} WHERE user_id = %d AND content = %s AND component_name = %s AND component_action = %s", $user_id, $content, $component_name, $component_action ) );               
     137        }
     138       
     139        function delete_for_user_by_component( $user_id, $component_name ) {
     140                global $bp, $wpdb;
     141               
     142                return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name} WHERE user_id = %d AND component_name = %s", $user_id, $component_name ) );               
     143        }
     144       
     145        function delete_for_user( $user_id ) {
     146                global $wpdb, $bp;
     147
     148                return $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name} WHERE user_id = %d", $user_id ) );
    114149        }
    115150       
     
    129164
    130165                if ( $limit && $page && $max )
    131                         $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name_user_activity_cached} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql ORDER BY date_recorded DESC $pag_sql", $user_id, $since ) );
     166                        $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql ORDER BY date_recorded DESC $pag_sql", $user_id, $since ) );
    132167                else
    133                         $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name_user_activity_cached} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql ORDER BY date_recorded DESC $pag_sql $max_sql", $user_id, $since ) );
    134        
    135                 $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->activity->table_name_user_activity_cached} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql ORDER BY date_recorded DESC $max_sql", $user_id, $since ) );
     168                        $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE user_id = %d AND date_recorded >= FROM_UNIXTIME(%d) $privacy_sql ORDER BY date_recorded DESC $pag_sql $max_sql", $user_id, $since ) );
     169       
     170                $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 ORDER BY date_recorded DESC $max_sql", $user_id, $since ) );
    136171
    137172                for ( $i = 0; $i < count( $activities ); $i++ ) {
     
    173208               
    174209                if ( $limit && $page && $max )
    175                         $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name_sitewide} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) ORDER BY date_recorded DESC $pag_sql", $since ) );
     210                        $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) ORDER BY date_recorded DESC $pag_sql", $since ) );
    176211                else
    177                         $activities = $wpdb->get_results( $wpdb->prepare( "SELECT DISTINCT user_id, content, primary_link, date_recorded, component_name, component_action FROM {$bp->activity->table_name_sitewide} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) ORDER BY date_recorded DESC $pag_sql $max_sql", $since ) );                       
    178                
    179                 $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT count(user_id) FROM {$bp->activity->table_name_sitewide} WHERE user_id IN ({$friend_ids}) AND date_recorded >= FROM_UNIXTIME(%d) ORDER BY date_recorded DESC $max_sql", $since ) );
     212                        $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) ORDER BY date_recorded DESC $pag_sql $max_sql", $since ) );                         
     213               
     214                $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) ORDER BY date_recorded DESC $max_sql", $since ) );
    180215               
    181216                return array( 'activities' => $activities, 'total' => (int)$total_activities );
     
    190225                if ( $max )
    191226                        $max_sql = $wpdb->prepare( "LIMIT %d", $max );
    192 
    193                 /* Remove entries that are older than 6 months */
    194                 $wpdb->query( $wpdb->prepare( "DELETE FROM " . $bp->activity->table_name_sitewide . " WHERE DATE_ADD(date_recorded, INTERVAL 6 MONTH) <= NOW()" ) );
    195227               
    196228                if ( $limit && $page && $max )
    197                         $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name_sitewide} ORDER BY date_recorded DESC $pag_sql" ) );
     229                        $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE hide_sitewide = 0 ORDER BY date_recorded DESC $pag_sql" ) );
    198230                else
    199                         $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name_sitewide} ORDER BY date_recorded DESC $pag_sql $max_sql" ) );
    200 
    201                 $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->activity->table_name_sitewide} ORDER BY date_recorded DESC $max_sql" ) );
     231                        $activities = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name} WHERE hide_sitewide = 0 ORDER BY date_recorded DESC $pag_sql $max_sql" ) );
     232
     233                $total_activities = $wpdb->get_var( $wpdb->prepare( "SELECT count(id) FROM {$bp->activity->table_name} WHERE hide_sitewide = 0 ORDER BY date_recorded DESC $max_sql" ) );
    202234               
    203235                for ( $i = 0; $i < count( $activities ); $i++ ) {
     
    228260        }
    229261       
    230         function cache_friends_activities( $activity_array ) {
    231                 global $wpdb, $bp;
    232                
    233                 /* Empty the cache */
    234                 $wpdb->query( "TRUNCATE TABLE {$bp->activity->table_name_loggedin_user_friends_cached}" );
    235                
    236                 for ( $i = 0; $i < count($activity_array); $i++ ) {
    237                         // Cache that sucka...
    238                         $cached = $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_loggedin_user_friends_cached} ( user_id, content, primary_link, component_name, component_action, date_cached, date_recorded ) VALUES ( %d, %s, %s, %s, %s, FROM_UNIXTIME(%d), %s )", $activity_array[$i]['user_id'], $activity_array[$i]['content'], $activity_array[$i]['primary_link'], $activity_array[$i]['component_name'], $activity_array[$i]['component_action'], time(), $activity_array[$i]['date_recorded'] ) );
    239                 }
    240                
    241                 update_usermeta( $bp->loggedin_user->id, 'bp_activity_friends_last_cached', time() );
    242         }
    243        
    244         function cache_activities( $activity_array, $user_id ) {
    245                 global $wpdb, $bp;
    246 
    247                 /* Delete cached items older than 30 days for the user */
    248                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_user_activity_cached} WHERE user_id = %d AND DATE_ADD(date_recorded, INTERVAL 30 DAY) <= NOW()", $user_id ) );
    249                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_sitewide} WHERE user_id = %d AND DATE_ADD(date_recorded, INTERVAL 30 DAY) <= NOW()", $user_id ) );
    250 
    251                 for ( $i = 0; $i < count($activity_array); $i++ ) {
    252                         if ( empty( $activity_array[$i]['content'] ) ) continue;
    253 
    254                         // Cache that sucka...
    255                         $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_user_activity_cached} ( user_id, content, item_id, secondary_item_id, primary_link, component_name, component_action, date_cached, date_recorded, is_private ) VALUES ( %d, %s, %d, %d, %s, %s, %s, FROM_UNIXTIME(%d), %s, %d )", $user_id, $activity_array[$i]['content'], $activity_array[$i]['item_id'], $activity_array[$i]['secondary_item_id'], $activity_array[$i]['primary_link'], $activity_array[$i]['component_name'], $activity_array[$i]['component_action'], time(), $activity_array[$i]['date_recorded'], $activity_array[$i]['is_private'] ) );
    256 
    257                         // Add to the sitewide activity stream
    258                         if ( !$activity_array[$i]['is_private'] && !$activity_array[$i]['no_sitewide_cache'] )
    259                                 $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_sitewide} ( user_id, content, item_id, secondary_item_id, primary_link, component_name, component_action, date_cached, date_recorded ) VALUES ( %d, %s, %d, %d, %s, %s, %s, FROM_UNIXTIME(%d), %s )", $user_id, $activity_array[$i]['content'], $activity_array[$i]['item_id'], $activity_array[$i]['secondary_item_id'], $activity_array[$i]['primary_link'], $activity_array[$i]['component_name'], $activity_array[$i]['component_action'], time(), $activity_array[$i]['date_recorded'] ) );
    260                 }
    261 
    262                 update_usermeta( $bp->displayed_user->id, 'bp_activity_last_cached', time() );
    263         }
    264 
    265         function delete_activity_for_user( $user_id ) {
    266                 global $wpdb, $bp;
    267 
    268                 /* Empty user's activities from the sitewide stream */
    269                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_sitewide} WHERE user_id = %d", $user_id ) );
    270 
    271                 /* Empty the user's activity items and cached activity items */
    272                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_user_activity} WHERE user_id = %d", $user_id ) );
    273                 $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_user_activity_cached} WHERE user_id = %d", $user_id ) );
    274                
    275                 return true;
    276         }
    277        
    278262        function get_last_updated() {
    279263                global $bp, $wpdb;
    280264               
    281                 return $wpdb->get_var( $wpdb->prepare( "SELECT date_recorded FROM " . $bp->activity->table_name_sitewide . " ORDER BY date_recorded ASC LIMIT 1" ) );
    282         }
    283        
    284         function kill_tables_for_user( $user_id ) {
    285                 global $bp, $wpdb;
    286 
    287                 if ( !$wpdb->get_var( "SHOW TABLES LIKE 'wp_user_{$user_id}_activity'" ) )
    288                         return false;
    289                
    290                 $wpdb->query( $wpdb->prepare( "DROP TABLE wp_user_{$user_id}_activity" ) );
    291                 $wpdb->query( $wpdb->prepare( "DROP TABLE wp_user_{$user_id}_activity_cached" ) );     
    292                 $wpdb->query( $wpdb->prepare( "DROP TABLE wp_user_{$user_id}_friends_activity_cached" ) );     
    293                
    294                 return true;
    295         }
    296        
    297         function convert_tables_for_user( $user_id ) {
    298                 global $bp, $wpdb;
    299                
    300                 if ( !$wpdb->get_var( "SHOW TABLES LIKE 'wp_user_{$user_id}_activity'" ) )
    301                         return false;
    302                
    303                 $activity_items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_user_{$user_id}_activity" ) );
    304                 $activity_cached_items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM wp_user_{$user_id}_activity_cached" ) );
    305                
    306                 if ( $activity_items ) {
    307                         foreach ( $activity_items as $activity_item ) {
    308                                 $wpdb->query( $wpdb->prepare(
    309                                         "INSERT INTO {$bp->activity->table_name_user_activity}
    310                                                 ( item_id, secondary_item_id, user_id, component_name, component_action, date_recorded, is_private, no_sitewide_cache )
    311                                         VALUES
    312                                                 ( %d, %d, %d, %s, %s, %s, %d, %d )",
    313                                         $activity_item->item_id, $activity_item->secondary_item_id, $user_id, $activity_item->component_name, $activity_item->component_action, $activity_item->date_recorded, $activity_item->is_private, $activity_item->no_sitewide_cache
    314                                 ) );
    315                         }
    316                 }
    317                
    318                 if ( $activity_cached_items ) {
    319                         foreach ( $activity_cached_items as $activity_cached_item ) {
    320                                 $wpdb->query( $wpdb->prepare(
    321                                         "INSERT INTO {$bp->activity->table_name_user_activity_cached}
    322                                                 ( content, primary_link, item_id, secondary_item_id, user_id, component_name, component_action, date_recorded, date_cached, is_private )
    323                                         VALUES
    324                                                 ( %s, %s, %d, %d, %d, %s, %s, %s, %s, %d )",
    325                                         $activity_cached_item->content, $activity_cached_item->primary_link, $activity_cached_item->item_id, $activity_cached_item->secondary_item_id, $user_id, $activity_cached_item->component_name, $activity_cached_item->component_action, $activity_cached_item->date_recorded, $activity_cached_item->date_cached, $activity_cached_item->is_private
    326                                 ) );
    327                         }
    328                 }
     265                return $wpdb->get_var( $wpdb->prepare( "SELECT date_recorded FROM {$bp->activity->table_name} ORDER BY date_recorded ASC LIMIT 1" ) );
    329266        }
    330267}
Note: See TracChangeset for help on using the changeset viewer.