Skip to:
Content

BuddyPress.org

Changeset 7334


Ignore:
Timestamp:
08/02/2013 06:29:06 PM (7 years ago)
Author:
boonebgorges
Message:

Introduces bp_*_user_last_activity() functions

User last activity data is currently stored as usermeta, with BuddyPress
making multiple calls directly to the usermeta table to get that data. These
new functions provide a central, standardized way to update and fetch
last_activity information. This makes it easier for plugins to update user
activity programatically in a future-compatible way.

Having this set of functions will facilitate any changes associated with #5128,
whose goal is to move last_activity data out of usermeta altogether.

Location:
trunk
Files:
6 edited

Legend:

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

    r7311 r7334  
    778778        $this->avatar_thumb = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'thumb', 'alt' => sprintf( __( 'Avatar of %s', 'buddypress' ), $this->fullname ) ) );
    779779        $this->avatar_mini  = bp_core_fetch_avatar( array( 'item_id' => $this->id, 'type' => 'thumb', 'alt' => sprintf( __( 'Avatar of %s', 'buddypress' ), $this->fullname ), 'width' => 30, 'height' => 30 ) );
    780         $this->last_active  = bp_core_get_last_activity( bp_get_user_meta( $this->id, 'last_activity', true ), __( 'active %s', 'buddypress' ) );
     780        $this->last_active  = bp_core_get_last_activity( bp_get_user_last_activity( $this->id ), __( 'active %s', 'buddypress' ) );
    781781    }
    782782
  • trunk/bp-core/bp-core-functions.php

    r7260 r7334  
    835835        return false;
    836836
    837     $activity = bp_get_user_meta( $user_id, 'last_activity', true );
     837    $activity = bp_get_user_last_activity( $user_id );
    838838
    839839    if ( !is_numeric( $activity ) )
     
    848848    }
    849849
    850     if ( empty( $activity ) || strtotime( $current_time ) >= strtotime( '+5 minutes', $activity ) )
    851         bp_update_user_meta( $user_id, 'last_activity', $current_time );
     850    if ( empty( $activity ) || strtotime( $current_time ) >= strtotime( '+5 minutes', $activity ) ) {
     851        bp_update_user_last_activity( $user_id, $current_time );
     852    }
    852853}
    853854add_action( 'wp_head', 'bp_core_record_activity' );
  • trunk/bp-members/bp-members-functions.php

    r7228 r7334  
    806806    // Return the inverse of active
    807807    return !bp_is_user_active( $user_id );
     808}
     809
     810/**
     811 * Update a user's last activity
     812 *
     813 * @since BuddyPress (1.9)
     814 * @param int $user_id ID of the user being updated
     815 * @param string $time Time of last activity, in 'Y-m-d H:i:s' format
     816 * @return bool True on success
     817 */
     818function bp_update_user_last_activity( $user_id = 0, $time = '' ) {
     819    // Fall back on current user
     820    if ( empty( $user_id ) ) {
     821        $user_id = bp_loggedin_user_id();
     822    }
     823
     824    // Bail if the user id is 0, as there's nothing to update
     825    if ( empty( $user_id ) ) {
     826        return false;
     827    }
     828
     829    // Fall back on current time
     830    if ( empty( $time ) ) {
     831        $time = bp_core_current_time();
     832    }
     833
     834    return bp_update_user_meta( $user_id, 'last_activity', $time );
     835}
     836
     837/**
     838 * Get the last activity for a given user
     839 *
     840 * @param int $user_id The ID of the user
     841 * @return string Time of last activity, in 'Y-m-d H:i:s' format, or an empty
     842 *   string if none is found
     843 */
     844function bp_get_user_last_activity( $user_id = 0 ) {
     845    // Fall back on current user
     846    if ( empty( $user_id ) ) {
     847        $user_id = bp_loggedin_user_id();
     848    }
     849
     850    $activity = bp_get_user_meta( $user_id, 'last_activity', true );
     851
     852    return apply_filters( 'bp_get_user_last_activity', $activity, $user_id );
    808853}
    809854
     
    15141559    add_action( 'login_head', 'wp_shake_js', 12 );
    15151560}
    1516 add_action( 'login_form_bp-spam', 'bp_live_spammer_login_error' ); 
     1561add_action( 'login_form_bp-spam', 'bp_live_spammer_login_error' );
  • trunk/bp-members/bp-members-template.php

    r7328 r7334  
    869869            $user_id = bp_displayed_user_id();
    870870
    871         $last_activity = bp_core_get_last_activity( bp_get_user_meta( $user_id, 'last_activity', true ), __('active %s', 'buddypress') );
     871        $last_activity = bp_core_get_last_activity( bp_get_user_last_activity( $user_id ), __('active %s', 'buddypress') );
    872872
    873873        return apply_filters( 'bp_get_last_activity', $last_activity );
  • trunk/bp-messages/bp-messages-classes.php

    r7311 r7334  
    479479        $wpdb->query( $wpdb->prepare( "UPDATE {$bp->messages->table_name_notices} SET is_active = 0 WHERE id != %d", $id ) );
    480480
    481         bp_update_user_meta( bp_loggedin_user_id(), 'last_activity', bp_core_current_time() );
     481        bp_update_user_last_activity( bp_loggedin_user_id(), bp_core_current_time() );
    482482
    483483        do_action_ref_array( 'messages_notice_after_save', array( &$this ) );
  • trunk/tests/includes/testcase.php

    r7237 r7334  
    214214        $user_id = $this->factory->user->create( $args );
    215215
    216         update_user_meta( $user_id, 'last_activity', $last_activity );
     216        bp_update_user_last_activity( $user_id, $last_activity );
    217217
    218218        if ( bp_is_active( 'xprofile' ) ) {
Note: See TracChangeset for help on using the changeset viewer.