Skip to:
Content

BuddyPress.org

Changeset 1566 for trunk/bp-activity.php


Ignore:
Timestamp:
06/23/2009 12:57:13 AM (15 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.

File:
1 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?>
Note: See TracChangeset for help on using the changeset viewer.