Skip to:
Content

BuddyPress.org

Changeset 7846


Ignore:
Timestamp:
02/11/2014 03:19:01 PM (11 years ago)
Author:
boonebgorges
Message:

Add 'active_format' parameter to bp_get_member_last_active()

This new param allows theme authors to specify whether they'd like the current
user's last activity string in the format "Active 5 minutes ago" vs the simpler
"5 minutes ago".

Fixes #5387

Props lenasterg

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-members/bp-members-template.php

    r7756 r7846  
    587587    add_filter( 'bp_get_member_name', 'esc_html'       );
    588588
    589 function bp_member_last_active() {
    590     echo bp_get_member_last_active();
    591 }
    592     function bp_get_member_last_active() {
    593         global $members_template;
    594 
    595         if ( isset( $members_template->member->last_activity ) )
    596             $last_activity = bp_core_get_last_activity( $members_template->member->last_activity, __( 'active %s', 'buddypress' ) );
    597         else
     589/**
     590 * Output the current member's last active time.
     591 *
     592 * @param array $args See {@link bp_get_member_last_active()}.
     593 */
     594function bp_member_last_active( $args = array() ) {
     595    echo bp_get_member_last_active( $args );
     596}
     597    /**
     598     * Return the current member's last active time.
     599     *
     600     * @param array $args {
     601     *     Array of optional arguments.
     602     *     @type bool $active_format If true, formatted "Active 5 minutes
     603     *           ago". If false, formatted "5 minutes ago". Default: true.
     604     * }
     605     * @return string
     606     */
     607    function bp_get_member_last_active( $args = array() ) {
     608        global $members_template;
     609
     610        $r = wp_parse_args( $args, array(
     611            'active_format' => true,
     612        ) );
     613
     614        if ( isset( $members_template->member->last_activity ) ) {
     615            if ( ! empty( $r['active_format'] ) ) {
     616                $last_activity = bp_core_get_last_activity( $members_template->member->last_activity, __( 'active %s', 'buddypress' ) );
     617            } else {
     618                $last_activity = bp_core_time_since( $members_template->member->last_activity );
     619            }
     620        } else {
    598621            $last_activity = __( 'Never active', 'buddypress' );
     622        }
    599623
    600624        return apply_filters( 'bp_member_last_active', $last_activity );
  • trunk/tests/testcases/members/template.php

    r7664 r7846  
    9898    }
    9999
     100    /**
     101     * @group bp_get_member_last_active
     102     */
     103    public function test_bp_get_member_last_active_default_params() {
     104        // Fake the global
     105        global $members_template;
     106
     107        $time = date( 'Y-m-d h:i:s', time() - 24*60*60 );
     108        $members_template = new stdClass;
     109        $members_template->member = new stdClass;
     110        $members_template->member->last_activity = $time;
     111
     112        $this->assertEquals( bp_core_get_last_activity( $time, __( 'active %s', 'buddypress' ) ), bp_get_member_last_active() );
     113    }
     114
     115    /**
     116     * @group bp_get_member_last_active
     117     */
     118    public function test_bp_get_member_last_active_active_format_true() {
     119        // Fake the global
     120        global $members_template;
     121
     122        $time = date( 'Y-m-d h:i:s', time() - 24*60*60 );
     123        $members_template = new stdClass;
     124        $members_template->member = new stdClass;
     125        $members_template->member->last_activity = $time;
     126
     127        $this->assertEquals( bp_core_get_last_activity( $time, __( 'active %s', 'buddypress' ) ), bp_get_member_last_active( array( 'active_format' => true, ) ) );
     128    }
     129
     130    /**
     131     * @group bp_get_member_last_active
     132     */
     133    public function test_bp_get_member_last_active_active_format_false() {
     134        // Fake the global
     135        global $members_template;
     136
     137        $time = date( 'Y-m-d h:i:s', time() - 24*60*60 );
     138        $members_template = new stdClass;
     139        $members_template->member = new stdClass;
     140        $members_template->member->last_activity = $time;
     141
     142        $this->assertEquals( bp_core_time_since( $time ), bp_get_member_last_active( array( 'active_format' => false, ) ) );
     143    }
    100144}
Note: See TracChangeset for help on using the changeset viewer.