Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
02/21/2014 02:49:33 PM (10 years ago)
Author:
boonebgorges
Message:

Automatically check for newly created items when viewing the Activity Stream.

Leveraging WP's Heartbeat API, this new functionality performs periodic checks
to see whether new activity items matching the current filter have been posted
since the page was originally loaded. If new items are found, a Load Newest
link is inserted into the top of the stream, which will insert the new items.

A filter is included, bp_activity_heartbeat_pulse, that allows site owners to
set a specific interval for these checks. The default value is 15 seconds, or
whatever your global Heartbeat interval is.

To disable the feature, there is a new setting "Automatically check for new
activity items when viewing the activity stream".

Fixes #5328

Props imath, boonebgorges

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/bp-activity/bp-activity-filters.php

    r7890 r7952  
    397397    return apply_filters( 'bp_activity_truncate_entry', $excerpt, $text, $append_text );
    398398}
     399
     400/**
     401 * Include extra javascript dependencies for activity component.
     402 *
     403 * @since BuddyPress (2.0.0)
     404 *
     405 * @uses bp_activity_do_heartbeat() to check if heartbeat is required.
     406 *
     407 * @param array $js_handles The original dependencies.
     408 * @return array $js_handles The new dependencies.
     409 */
     410function bp_activity_get_js_dependencies( $js_handles = array() ) {
     411    if ( bp_activity_do_heartbeat() ) {
     412        $js_handles[] = 'heartbeat';
     413    }
     414
     415    return $js_handles;
     416}
     417add_filter( 'bp_core_get_js_dependencies', 'bp_activity_get_js_dependencies', 10, 1 );
     418
     419/**
     420 * Add a just-posted classes to the most recent activity item.
     421 *
     422 * We use these classes to avoid pagination issues when items are loaded
     423 * dynamically into the activity stream.
     424 *
     425 * @since BuddyPress (2.0.0)
     426 *
     427 * @param string $classes
     428 * @return string $classes
     429 */
     430function bp_activity_newest_class( $classes = '' ) {
     431    $bp = buddypress();
     432
     433    if ( ! empty( $bp->activity->new_update_id ) && $bp->activity->new_update_id == bp_get_activity_id() ) {
     434        $classes .= ' new-update';
     435    }
     436
     437    $classes .= ' just-posted';
     438    return $classes;
     439}
     440
     441/**
     442 * Use WordPress Heartbeat API to check for latest activity update.
     443 *
     444 * @since BuddyPress (2.0.0)
     445 *
     446 * @uses bp_activity_get_last_updated() to get the recorded date of the last activity
     447
     448 * @param array $response
     449 * @param array $data
     450 * @return array $response
     451 */
     452function bp_activity_heartbeat_last_recorded( $response = array(), $data = array() ) {
     453    $bp = buddypress();
     454
     455    if ( empty( $data['bp_activity_last_id'] ) ) {
     456        return $response;
     457    }
     458
     459    // Use the querystring argument stored in the cookie (to preserve
     460    // filters), but force the offset to get only new items
     461    $activity_latest_args = bp_parse_args(
     462        bp_ajax_querystring( 'activity' ),
     463        array( 'offset' => absint( $data['bp_activity_last_id'] ) + 1 ),
     464        'activity_latest_args'
     465    );
     466
     467    $newest_activities = array();
     468    $last_activity_id  = 0;
     469
     470    // Temporarly add a just-posted class for new activity items
     471    add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
     472
     473    ob_start();
     474    if ( bp_has_activities( $activity_latest_args ) ) {
     475        while ( bp_activities() ) {
     476            bp_the_activity();
     477
     478            if ( $last_activity_id < bp_get_activity_id() ) {
     479                $last_activity_id = bp_get_activity_id();
     480            }
     481
     482            bp_get_template_part( 'activity/entry' );
     483        }
     484    }
     485
     486    $newest_activities['activities'] = ob_get_contents();
     487    $newest_activities['last_id']    = $last_activity_id;
     488    ob_end_clean();
     489
     490    // Remove the temporary filter
     491    remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
     492
     493    if ( ! empty( $newest_activities['last_id'] ) ) {
     494        $response['bp_activity_newest_activities'] = $newest_activities;
     495    }
     496
     497    return $response;
     498}
     499add_filter( 'heartbeat_received', 'bp_activity_heartbeat_last_recorded', 10, 2 );
     500add_filter( 'heartbeat_nopriv_received', 'bp_activity_heartbeat_last_recorded', 10, 2 );
     501
     502/**
     503 * Set the strings for WP HeartBeat API where needed.
     504 *
     505 * @since BuddyPress (2.0.0)
     506 *
     507 * @param array $strings Localized strings.
     508 * @return array $strings
     509 */
     510function bp_activity_heartbeat_strings( $strings = array() ) {
     511
     512    if ( ! bp_activity_do_heartbeat() ) {
     513        return $strings;
     514    }
     515
     516    $global_pulse = 0;
     517
     518    // Check whether the global heartbeat settings already exist.
     519    $heartbeat_settings = apply_filters( 'heartbeat_settings', array() );
     520    if ( ! empty( $heartbeat_settings['interval'] ) ) {
     521        // 'Fast' is 5
     522        $global_pulse = is_numeric( $heartbeat_settings['interval'] ) ? absint( $heartbeat_settings['interval'] ) : 5;
     523    }
     524
     525    // Filter here to specify a BP-specific pulse frequency
     526    $bp_activity_pulse = apply_filters( 'bp_activity_heartbeat_pulse', 15 );
     527
     528    /**
     529     * Use the global pulse value unless:
     530     * a. the BP-specific value has been specifically filtered, or
     531     * b. it doesn't exist (ie, BP will be the only one using the heartbeat,
     532     *    so we're responsible for enabling it)
     533     */
     534    if ( has_filter( 'bp_activity_heartbeat_pulse' ) || empty( $global_pulse ) ) {
     535        $pulse = $bp_activity_pulse;
     536    } else {
     537        $pulse = $global_pulse;
     538    }
     539
     540    $strings = array_merge( $strings, array(
     541        'newest' => __( 'Load Newest', 'buddypress' ),
     542        'pulse'  => absint( $pulse ),
     543    ) );
     544
     545    return $strings;
     546}
     547add_filter( 'bp_core_get_js_strings', 'bp_activity_heartbeat_strings', 10, 1 );
Note: See TracChangeset for help on using the changeset viewer.