Skip to:
Content

BuddyPress.org

Ticket #6234: 6234.patch

File 6234.patch, 7.9 KB (added by imath, 5 years ago)
  • src/bp-activity/bp-activity-filters.php

    diff --git src/bp-activity/bp-activity-filters.php src/bp-activity/bp-activity-filters.php
    index 82f992a..4b799a1 100644
    function bp_activity_heartbeat_last_recorded( $response = array(), $data = array 
    532532                return $response;
    533533        }
    534534
    535         // Use the querystring argument stored in the cookie (to preserve
    536         // filters), but force the offset to get only new items
    537         $activity_latest_args = bp_parse_args(
    538                 bp_ajax_querystring( 'activity' ),
    539                 array( 'since' => date( 'Y-m-d H:i:s', $data['bp_activity_last_recorded'] ) ),
    540                 'activity_latest_args'
    541         );
     535        $bp = buddypress();
     536        $reset_post = $_POST;
     537
     538        $activity_latest_args = array( 'since' => date( 'Y-m-d H:i:s', $data['bp_activity_last_recorded'] ) );
    542539
    543540        if ( ! empty( $data['bp_activity_last_recorded_search_terms'] ) && empty( $activity_latest_args['search_terms'] ) ) {
    544541                $activity_latest_args['search_terms'] = addslashes( $data['bp_activity_last_recorded_search_terms'] );
    545542        }
    546543
     544        $bp->activity->heartbeat_query = apply_filters( 'bp_after_activity_latest_args_parse_args', $activity_latest_args );
     545
    547546        $newest_activities = array();
    548         $last_activity_recorded = 0;
     547        $bp->activity->last_recorded = 0;
     548
     549        /**
     550         * Temporary hookd to add a just-posted class for new activity items
     551         * and include heartbeat args to querystring
     552         */
     553        add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class',                       10, 1 );
     554        add_filter( 'bp_ajax_querystring',       'bp_activity_heartbeat_filter_ajax_querystring', 999, 1 );
     555        add_action( 'bp_after_activity_entry',   'bp_activity_heartbeat_get_last_recorded'               );
    549556
    550         // Temporarily add a just-posted class for new activity items
    551         add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
     557        // We need to simulate a posted page to avoid the ul/form elements of activity loop to be output
     558        $_POST['page'] = 1;
    552559
    553560        ob_start();
    554         if ( bp_has_activities( $activity_latest_args ) ) {
    555                 while ( bp_activities() ) {
    556                         bp_the_activity();
    557561
    558                         $atime = strtotime( bp_get_activity_date_recorded() );
    559                         if ( $last_activity_recorded < $atime ) {
    560                                 $last_activity_recorded = $atime;
    561                         }
    562 
    563                         bp_get_template_part( 'activity/entry' );
    564                 }
    565         }
     562        // Use the activity-loop to include hardcoded parameters
     563        bp_get_template_part( 'activity/activity-loop' );
    566564
    567565        $newest_activities['activities']    = ob_get_contents();
    568         $newest_activities['last_recorded'] = $last_activity_recorded;
     566        $newest_activities['last_recorded'] = $bp->activity->last_recorded;
     567
    569568        ob_end_clean();
    570569
    571         // Remove the temporary filter
    572         remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
     570        // Remove the temporary hooks
     571        remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class',                  10, 1 );
     572        remove_filter( 'bp_ajax_querystring',       'bp_activity_heartbeat_filter_querystring', 999, 1 );
     573        remove_action( 'bp_after_activity_entry',   'bp_activity_heartbeat_get_last_recorded'          );
     574
     575        // Reset $_POST
     576        $_POST = $reset_post;
     577
     578        // Reset globals
     579        $bp->activity->heartbeat_query = array();
     580        $bp->activity->last_recorded = 0;
    573581
    574582        if ( ! empty( $newest_activities['last_recorded'] ) ) {
    575583                $response['bp_activity_newest_activities'] = $newest_activities;
    add_filter( 'heartbeat_received', 'bp_activity_heartbeat_last_recorded', 10, 2 ) 
    581589add_filter( 'heartbeat_nopriv_received', 'bp_activity_heartbeat_last_recorded', 10, 2 );
    582590
    583591/**
     592 * Filter the querystring to include heartbeat activities args.
     593 *
     594 * @since BuddyPress (2.3.0)
     595 *
     596 * @see https://buddypress.trac.wordpress.org/ticket/6234
     597 *
     598 * @param string $querystring
     599 * @return string edited or unchanged querystring
     600 */
     601function bp_activity_heartbeat_filter_ajax_querystring( $querystring = '' ) {
     602        $bp = buddypress();
     603
     604        // Init new to current
     605        $new_query_string = $querystring;
     606
     607        // Make sure we will manipulate an array
     608        $query_args = wp_parse_args( $querystring, array() );
     609
     610        if ( ! empty( $bp->activity->heartbeat_query ) ) {
     611                $query_args = array_merge( $query_args, $bp->activity->heartbeat_query );
     612
     613                // Build the new querystring
     614                $new_query_string = http_build_query( $query_args );
     615        }
     616
     617        return apply_filters( 'bp_activity_heartbeat_filter_ajax_querystring', $new_query_string, $querystring );
     618}
     619
     620/**
     621 * Get the activity last recorded time displayed in the activity loop template.
     622 *
     623 * @since BuddyPress (2.3.0)
     624 *
     625 * @see https://buddypress.trac.wordpress.org/ticket/6234
     626 */
     627function bp_activity_heartbeat_get_last_recorded() {
     628        $bp = buddypress();
     629
     630        if ( ! isset( $bp->activity->last_recorded ) ) {
     631                $bp->activity->last_recorded = 0;
     632        }
     633
     634        $atime = strtotime( bp_get_activity_date_recorded() );
     635        if ( $bp->activity->last_recorded < $atime ) {
     636                $bp->activity->last_recorded = $atime;
     637        }
     638}
     639
     640/**
    584641 * Set the strings for WP HeartBeat API where needed.
    585642 *
    586643 * @since BuddyPress (2.0.0)
  • src/bp-templates/bp-legacy/buddypress-functions.php

    diff --git src/bp-templates/bp-legacy/buddypress-functions.php src/bp-templates/bp-legacy/buddypress-functions.php
    index 7b7f1b9..6a3dcb8 100644
    function bp_legacy_theme_activity_template_loader() { 
    804804 */
    805805function bp_legacy_theme_post_update() {
    806806        $bp = buddypress();
     807        $reset_post = $_POST;
    807808
    808809        // Bail if not a POST action
    809810        if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
    function bp_legacy_theme_post_update() { 
    834835                exit( '-1<div id="message" class="error bp-ajax-message"><p>' . __( 'There was a problem posting your update. Please try again.', 'buddypress' ) . '</p></div>' );
    835836
    836837        $last_recorded = ! empty( $_POST['since'] ) ? date( 'Y-m-d H:i:s', intval( $_POST['since'] ) ) : 0;
     838
     839        // Check for "heartbeat" activities
    837840        if ( $last_recorded ) {
    838                 $activity_args = array( 'since' => $last_recorded );
     841                $bp->activity->heartbeat_query = apply_filters( 'bp_after_activity_latest_args_parse_args', array( 'since' => $last_recorded ) );
    839842                $bp->activity->last_recorded = $last_recorded;
    840                 add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
    841         } else {
    842                 $activity_args = array( 'include' => $activity_id );
    843         }
    844843
    845         if ( bp_has_activities ( $activity_args ) ) {
    846                 while ( bp_activities() ) {
    847                         bp_the_activity();
    848                         bp_get_template_part( 'activity/entry' );
    849                 }
    850         }
     844                /**
     845                 * Temporary hookd to add a just-posted class for new activity items
     846                 * and include heartbeat args to querystring
     847                 */
     848                add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class',                       10, 1 );
     849                add_filter( 'bp_ajax_querystring',       'bp_activity_heartbeat_filter_ajax_querystring', 999, 1 );
     850                add_action( 'bp_after_activity_entry',   'bp_activity_heartbeat_get_last_recorded'               );
    851851
    852         if ( ! empty( $last_recorded ) ) {
    853                 remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
    854         }
     852                // We need to simulate a posted page to avoid the ul/form elements of activity loop to be output
     853                $_POST['page'] = 1;
     854
     855                // Use the activity-loop to include hardcoded parameters
     856                bp_get_template_part( 'activity/activity-loop' );
    855857
     858                // Remove the temporary hooks
     859                remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class',                  10, 1 );
     860                remove_filter( 'bp_ajax_querystring',       'bp_activity_heartbeat_filter_querystring', 999, 1 );
     861                remove_action( 'bp_after_activity_entry',   'bp_activity_heartbeat_get_last_recorded'          );
     862
     863                // Reset $_POST
     864                $_POST = $reset_post;
     865
     866                /**
     867                 * Reset globals except the last recorded as it's needed later to apply a specific css class
     868                 * @see bp_activity_newest_class()
     869                 */
     870                $bp->activity->heartbeat_query = array();
     871
     872        // Simply include the just posted activity
     873        } else {
     874                if ( bp_has_activities ( array( 'include' => $activity_id ) ) ) {
     875                        while ( bp_activities() ) {
     876                                bp_the_activity();
     877                                bp_get_template_part( 'activity/entry' );
     878                        }
     879                }
     880        }
    856881        exit;
    857882}
    858883