Skip to:
Content

BuddyPress.org

Changeset 3753


Ignore:
Timestamp:
01/20/2011 05:44:22 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Audit bp-activity.php - Sweep through existing functions and document them. Add missing error checks. Cast arguments correctly.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity.php

    r3751 r3753  
    11<?php
     2
     3/**
     4 * BuddyPress Activity Streams
     5 *
     6 * An activity stream component, for users, groups, and blog tracking.
     7 *
     8 * @package BuddyPress
     9 * @subpackage Activity Core
     10 */
    211
    312class BP_Activity_Component extends BP_Component {
     
    514    /**
    615     * Start the activity component creation process
     16     *
     17     * @since BuddyPress {unknown}
    718     */
    819    function BP_Activity_Component() {
     
    1324     * Setup globals
    1425     *
     26     * The BP_ACTIVITY_SLUG constant is deprecated, and only used here for
     27     * backwards compatibility.
     28     *
     29     * @since BuddyPress {unknown}
    1530     * @global obj $bp
    1631     */
     
    3752     */
    3853    function _includes() {
    39         require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-actions.php'      );
    40         require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-filters.php'      );
    41         require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-screens.php'      );
    42         require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-classes.php'      );
    43         require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-functions.php'    );
    44         require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-template.php' );
     54        require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-actions.php'   );
     55        require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-filters.php'   );
     56        require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-screens.php'   );
     57        require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-classes.php'   );
     58        require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-template.php'  );
     59        require_once( BP_PLUGIN_DIR . '/bp-activity/bp-activity-functions.php' );
    4560    }
    4661
     
    248263/** Actions *******************************************************************/
    249264
     265/**
     266 * Sets the current action for a given activity stream location
     267 *
     268 * @global obj $bp
     269 * @param str $component_id
     270 * @param str $key
     271 * @param str $value
     272 * @return bool False on error, True on success
     273 */
    250274function bp_activity_set_action( $component_id, $key, $value ) {
    251275    global $bp;
    252276
     277    // Return false if any of the above values are not set
    253278    if ( empty( $component_id ) || empty( $key ) || empty( $value ) )
    254279        return false;
    255280
     281    // Set activity action
    256282    $bp->activity->actions->{$component_id}->{$key} = apply_filters( 'bp_activity_set_action', array(
    257283        'key'   => $key,
    258284        'value' => $value
    259285    ), $component_id, $key, $value );
    260 }
    261 
     286
     287    return true;
     288}
     289
     290/**
     291 * Retreives the current action from a component and key
     292 *
     293 * @global obj $bp
     294 * @param str $component_id
     295 * @param str $key
     296 * @return mixed False on error, action on success
     297 */
    262298function bp_activity_get_action( $component_id, $key ) {
    263299    global $bp;
    264300
     301    // Return false if any of the above values are not set
    265302    if ( empty( $component_id ) || empty( $key ) )
    266303        return false;
     
    271308/** Favorites *****************************************************************/
    272309
    273 function bp_activity_get_user_favorites( $user_id ) {
    274     $my_favs = maybe_unserialize( get_user_meta( $user_id, 'bp_favorite_activities', true ) );
    275     $existing_favs = bp_activity_get_specific( array( 'activity_ids' => $my_favs ) );
     310/**
     311 * Get a users favorite activity stream items
     312 *
     313 * @global obj $bp
     314 * @param int $user_id
     315 * @return array Array of users favorite activity stream ID's
     316 */
     317function bp_activity_get_user_favorites( $user_id = 0 ) {
     318    global $bp;
     319
     320    // Fallback to logged in user if no user_id is passed
     321    if ( empty( $user_id ) )
     322        $user_id = $bp->displayed_user->id;
     323
     324    // Get favorites for user
     325    $favs          = get_user_meta( $user_id, 'bp_favorite_activities', true );
     326    $existing_favs = bp_activity_get_specific( array( 'activity_ids' => $favs ) );
    276327
    277328    foreach( (array)$existing_favs['activities'] as $fav )
     
    284335}
    285336
    286 function bp_activity_add_user_favorite( $activity_id, $user_id = false ) {
    287     global $bp;
    288 
    289     if ( !$user_id )
     337/**
     338 * Add an activity stream item as a favorite for a user
     339 *
     340 * @global obj $bp
     341 * @param int $activity_id
     342 * @param int $user_id
     343 * @return bool
     344 */
     345function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) {
     346    global $bp;
     347
     348    // Favorite activity stream items are for logged in users only
     349    if ( !is_user_logged_in() )
     350        return false;
     351
     352    // Fallback to logged in user if no user_id is passed
     353    if ( empty( $user_id ) )
    290354        $user_id = $bp->loggedin_user->id;
    291355
    292356    // Update the user's personal favorites
    293     $my_favs = maybe_unserialize( get_user_meta( $bp->loggedin_user->id, 'bp_favorite_activities', true ) );
     357    $my_favs   = get_user_meta( $bp->loggedin_user->id, 'bp_favorite_activities', true );
    294358    $my_favs[] = $activity_id;
    295359
    296360    // Update the total number of users who have favorited this activity
    297361    $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    298 
    299     if ( !empty( $fav_count ) )
    300         $fav_count = (int)$fav_count + 1;
    301     else
    302         $fav_count = 1;
    303 
     362    $fav_count = !empty( $fav_count ) ? (int)$fav_count + 1 : 1;
     363
     364    // Update user meta
    304365    update_user_meta( $bp->loggedin_user->id, 'bp_favorite_activities', $my_favs );
    305     bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count );
    306 
    307     do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id );
    308 
    309     return true;
    310 }
    311 
    312 function bp_activity_remove_user_favorite( $activity_id, $user_id = false ) {
    313     global $bp;
    314 
    315     if ( !$user_id )
     366
     367    // Update activity meta counts
     368    if ( true === bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {
     369
     370        // Execute additional code
     371        do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id );
     372
     373        // Success
     374        return true;
     375
     376    // Saving meta was unsuccessful for an unknown reason
     377    } else {
     378        // Execute additional code
     379        do_action( 'bp_activity_add_user_favorite_fail', $activity_id, $user_id );
     380
     381        return false;
     382    }
     383}
     384
     385function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) {
     386    global $bp;
     387
     388    // Favorite activity stream items are for logged in users only
     389    if ( !is_user_logged_in() )
     390        return false;
     391
     392    // Fallback to logged in user if no user_id is passed
     393    if ( empty( $user_id ) )
    316394        $user_id = $bp->loggedin_user->id;
    317395
    318396    // Remove the fav from the user's favs
    319     $my_favs = maybe_unserialize( get_user_meta( $user_id, 'bp_favorite_activities', true ) );
     397    $my_favs = get_user_meta( $user_id, 'bp_favorite_activities', true );
    320398    $my_favs = array_flip( (array) $my_favs );
    321399    unset( $my_favs[$activity_id] );
     
    323401
    324402    // Update the total number of users who have favorited this activity
    325     $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );
    326 
    327     if ( !empty( $fav_count ) ) {
    328         $fav_count = (int)$fav_count - 1;
    329         bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count );
    330     }
    331 
    332     update_user_meta( $user_id, 'bp_favorite_activities', $my_favs );
    333 
    334     do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
    335 
    336     return true;
    337 }
    338 
     403    if ( $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ) ) {
     404
     405        // Deduct from total favorites
     406        if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int)$fav_count - 1 ) ) {
     407
     408            // Update users favorites
     409            if ( update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {
     410
     411                // Execute additional code
     412                do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );
     413
     414                // Success
     415                return true;
     416
     417            // Error updating
     418            } else {
     419                return false;
     420            }
     421
     422        // Error updating favorite count
     423        } else {
     424            return false;
     425        }
     426
     427    // Error getting favorite count
     428    } else {
     429        return false;
     430    }
     431}
     432
     433/**
     434 * Check if activity exists by scanning content
     435 *
     436 * @param str $content
     437 * @return bool
     438 */
    339439function bp_activity_check_exists_by_content( $content ) {
    340440    return apply_filters( 'bp_activity_check_exists_by_content', BP_Activity_Activity::check_exists_by_content( $content ) );
    341441}
    342442
     443/**
     444 * Retreive the last time activity was updated
     445 *
     446 * @return str
     447 */
    343448function bp_activity_get_last_updated() {
    344449    return apply_filters( 'bp_activity_get_last_updated', BP_Activity_Activity::get_last_updated() );
    345450}
    346451
    347 function bp_activity_total_favorites_for_user( $user_id = false ) {
    348     global $bp;
    349 
    350     if ( !$user_id )
     452/**
     453 * Retreive the number of favorite activity stream items a user has
     454 *
     455 * @global obj $bp
     456 * @param int $user_id
     457 * @return int
     458 */
     459function bp_activity_total_favorites_for_user( $user_id = 0 ) {
     460    global $bp;
     461
     462    // Fallback on displayed user, and then logged in user
     463    if ( empty( $user_id ) )
    351464        $user_id = ( $bp->displayed_user->id ) ? $bp->displayed_user->id : $bp->loggedin_user->id;
    352465
     
    356469/** Meta **********************************************************************/
    357470
    358 
    359 function bp_activity_delete_meta( $activity_id, $meta_key = false, $meta_value = false ) {
     471/**
     472 * Delete a meta entry from the DB for an activity stream item
     473 *
     474 * @global DB $wpdb
     475 * @global obj $bp
     476 * @param int $activity_id
     477 * @param str $meta_key
     478 * @param str $meta_value
     479 * @return bool
     480 */
     481function bp_activity_delete_meta( $activity_id, $meta_key = '', $meta_value = '' ) {
    360482    global $wpdb, $bp;
    361483
     484    // Return false if any of the above values are not set
    362485    if ( !is_numeric( $activity_id ) )
    363486        return false;
    364487
     488    // Sanitize key
    365489    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    366490
     
    368492        $meta_value = serialize( $meta_value );
    369493
     494    // Trim off whitespace
    370495    $meta_value = trim( $meta_value );
    371496
    372     if ( !$meta_key )
    373         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );
     497    // Delete all for activity_id
     498    if ( empty( $meta_key ) )
     499        $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );
     500
     501    // Delete only when all match
    374502    else if ( $meta_value )
    375         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s AND meta_value = %s", $activity_id, $meta_key, $meta_value ) );
     503        $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s AND meta_value = %s", $activity_id, $meta_key, $meta_value ) );
     504
     505    // Delete only when activity_id and meta_key match
    376506    else
    377         $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) );
    378 
     507        $retval = $wpdb->query( $wpdb->prepare( "DELETE FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) );
     508
     509    // Delete cache entry
    379510    wp_cache_delete( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, 'bp' );
    380511
    381     return true;
    382 }
    383 
    384 function bp_activity_get_meta( $activity_id, $meta_key = '' ) {
     512    // Success
     513    if ( !is_wp_error( $retval ) )
     514        return true;
     515
     516    // Fail
     517    else
     518        return false;
     519}
     520
     521/**
     522 * Get activity meta
     523 *
     524 * @global DB $wpdb
     525 * @global obj $bp
     526 * @param int $activity_id
     527 * @param str $meta_key
     528 * @return bool
     529 */
     530function bp_activity_get_meta( $activity_id = 0, $meta_key = '' ) {
    385531    global $wpdb, $bp;
    386532
    387     $activity_id = (int)$activity_id;
    388 
    389     if ( !$activity_id )
    390         return false;
    391 
    392     if ( !empty($meta_key) ) {
     533    // Make sure activity_id is valid
     534    if ( empty( $activity_id ) || !is_numeric( $activity_id ) )
     535        return false;
     536
     537    // We have a key to look for
     538    if ( !empty( $meta_key ) ) {
     539
     540        // Sanitize key
    393541        $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    394542
     543        // Check cache
    395544        if ( !$metas = wp_cache_get( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, 'bp' ) ) {
     545
     546            // No cache so hit the DB
    396547            $metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) );
     548
     549            // Set cache
    397550            wp_cache_set( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, $metas, 'bp' );
    398551        }
    399     } else
     552
     553    // No key so get all for activity_id
     554    } else {
    400555        $metas = $wpdb->get_col( $wpdb->prepare( "SELECT meta_value FROM {$bp->activity->table_name_meta} WHERE activity_id = %d", $activity_id ) );
    401 
    402     if ( empty($metas) )
    403         return false;
    404 
     556    }
     557
     558    // No result so return false
     559    if ( empty( $metas ) )
     560        return false;
     561
     562    // Maybe, just maybe... unserialize
    405563    $metas = array_map( 'maybe_unserialize', (array)$metas );
    406564
    407     if ( 1 == count($metas) )
    408         return $metas[0];
    409     else
    410         return $metas;
    411 }
    412 
     565    // Return first item in array if only 1, else return all metas found
     566    $retval = ( 1 == count( $metas ) ? $metas[0] : $metas );
     567
     568    // Filter result before returning
     569    return apply_filters( 'bp_activity_get_meta', $retval, $activity_id, $meta_key );
     570}
     571
     572/**
     573 * Update activity meta
     574 *
     575 * @global DB $wpdb
     576 * @global obj $bp
     577 * @param int $activity_id
     578 * @param str $meta_key
     579 * @param str $meta_value
     580 * @return bool
     581 */
    413582function bp_activity_update_meta( $activity_id, $meta_key, $meta_value ) {
    414583    global $wpdb, $bp;
    415584
     585    // Make sure activity_id is valid
    416586    if ( !is_numeric( $activity_id ) )
    417587        return false;
    418588
     589    // Sanitize key
    419590    $meta_key = preg_replace( '|[^a-z0-9_]|i', '', $meta_key );
    420591
     592    // Sanitize value
    421593    if ( is_string( $meta_value ) )
    422594        $meta_value = stripslashes( $wpdb->escape( $meta_value ) );
    423595
     596    // Maybe, just maybe... serialize
    424597    $meta_value = maybe_serialize( $meta_value );
    425598
     599    // If value is empty, delete the meta key
    426600    if ( empty( $meta_value ) )
    427601        return bp_activity_delete_meta( $activity_id, $meta_key );
    428602
     603    // See if meta key exists for activity_id
    429604    $cur = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$bp->activity->table_name_meta} WHERE activity_id = %d AND meta_key = %s", $activity_id, $meta_key ) );
    430605
    431     if ( !$cur )
     606    // Meta key does not exist so INSERT
     607    if ( empty( $cur ) )
    432608        $wpdb->query( $wpdb->prepare( "INSERT INTO {$bp->activity->table_name_meta} ( activity_id, meta_key, meta_value ) VALUES ( %d, %s, %s )", $activity_id, $meta_key, $meta_value ) );
     609
     610    // Meta key exists, so UPDATE
    433611    else if ( $cur->meta_value != $meta_value )
    434612        $wpdb->query( $wpdb->prepare( "UPDATE {$bp->activity->table_name_meta} SET meta_value = %s WHERE activity_id = %d AND meta_key = %s", $meta_value, $activity_id, $meta_key ) );
     613
     614    // Weirdness, so return false
    435615    else
    436616        return false;
    437617
     618    // Set cache
    438619    wp_cache_set( 'bp_activity_meta_' . $meta_key . '_' . $activity_id, $meta_value, 'bp' );
    439620
     621    // Victory is ours!
    440622    return true;
    441623}
     
    443625/** Clean up ******************************************************************/
    444626
    445 function bp_activity_remove_data( $user_id ) {
     627/**
     628 * Completely remove
     629 * @param int $user_id
     630 */
     631function bp_activity_remove_all_user_data( $user_id = 0 ) {
     632
     633    // Do not delete user data unless a logged in user says so
     634    if ( empty( $user_id ) || !is_user_logged_in() )
     635        return false;
     636
    446637    // Clear the user's activity from the sitewide stream and clear their activity tables
    447638    bp_activity_delete( array( 'user_id' => $user_id ) );
     
    451642    delete_user_meta( $user_id, 'bp_favorite_activities' );
    452643
    453     do_action( 'bp_activity_remove_data', $user_id );
    454 }
    455 add_action( 'wpmu_delete_user',  'bp_activity_remove_data' );
    456 add_action( 'delete_user',       'bp_activity_remove_data' );
    457 add_action( 'bp_make_spam_user', 'bp_activity_remove_data' );
     644    // Execute additional code
     645    do_action( 'bp_activity_remove_data', $user_id ); // Deprecated! Do not use!
     646
     647    // Use this going forward
     648    do_action( 'bp_activity_remove_all_user_data', $user_id );
     649}
     650add_action( 'wpmu_delete_user',  'bp_activity_remove_all_user_data' );
     651add_action( 'delete_user',       'bp_activity_remove_all_user_data' );
     652add_action( 'bp_make_spam_user', 'bp_activity_remove_all_user_data' );
    458653
    459654/**
    460655 * Register the activity stream actions for updates
    461656 *
    462  * @global array $bp
     657 * @global obj $bp
    463658 */
    464659function updates_register_activity_actions() {
Note: See TracChangeset for help on using the changeset viewer.