Skip to:
Content

BuddyPress.org

Ticket #5505: 5505.03.patch

File 5505.03.patch, 15.9 KB (added by imath, 11 years ago)
  • bp-activity/bp-activity-classes.php

    diff --git bp-activity/bp-activity-classes.php bp-activity/bp-activity-classes.php
    index 5be5de3..f1e6054 100644
    class BP_Activity_Activity { 
    11891189         * @since BuddyPress (1.5.0)
    11901190         *
    11911191         * @param array $filter_array {
    1192          *     Fields and values to filter by. Each can be either a single
    1193          *     string, a comma-separated list, or an array of values.
     1192         *     Fields and values to filter by.
    11941193         *     @type array|string|id $user_id User ID(s).
    11951194         *     @type array|string $object Corresponds to the 'component'
    11961195         *           column in the database.
    class BP_Activity_Activity { 
    12001199         *           column in the database.
    12011200         *     @type array|string|int $secondary_id Corresponds to the
    12021201         *           'secondary_item_id' column in the database.
     1202         *     @type int $offset Return only those items with an ID greater
     1203         *           than the offset value.
     1204         *     @type string $since Return only those items that have a
     1205         *           date_recorded value greater than a given MySQL-formatted
     1206         *           date.
    12031207         * }
    12041208         * @return string The filter clause, for use in a SQL query.
    12051209         */
    class BP_Activity_Activity { 
    12421246                        $filter_sql[] = "a.id >= {$sid_sql}";
    12431247                }
    12441248
     1249                if ( ! empty( $filter_array['since'] ) ) {
     1250                        // Validate that this is a proper Y-m-d H:i:s date
     1251                        // Trick: parse to UNIX date then translate back
     1252                        $translated_date = date( 'Y-m-d H:i:s', strtotime( $filter_array['since'] ) );
     1253                        if ( $translated_date === $filter_array['since'] ) {
     1254                                $filter_sql[] = "a.date_recorded > '{$translated_date}'";
     1255                        }
     1256                }
     1257
    12451258                if ( empty( $filter_sql ) )
    12461259                        return false;
    12471260
  • bp-activity/bp-activity-filters.php

    diff --git bp-activity/bp-activity-filters.php bp-activity/bp-activity-filters.php
    index 8f9cbff..9c6e5d9 100644
    add_filter( 'bp_core_get_js_dependencies', 'bp_activity_get_js_dependencies', 10 
    429429function bp_activity_newest_class( $classes = '' ) {
    430430        $bp = buddypress();
    431431
    432         if ( ! empty( $bp->activity->new_update_id ) && $bp->activity->new_update_id == bp_get_activity_id() ) {
     432        if ( ! empty( $bp->activity->last_recorded ) && $bp->activity->last_recorded == bp_get_activity_date_recorded() ) {
    433433                $classes .= ' new-update';
    434434        }
    435435
    function bp_activity_newest_class( $classes = '' ) { 
    438438}
    439439
    440440/**
     441 * Check if Activity Heartbeat feature i on to add a timestamp class.
     442 *
     443 * @since BuddyPress (2.0.0)
     444 *
     445 * @param string $classes
     446 * @return string $classes
     447 */
     448function bp_activity_timestamp_class( $classes = '' ) {
     449
     450        if ( ! bp_activity_do_heartbeat() ) {
     451                return $classes;
     452        }
     453
     454        $activity_date = bp_get_activity_date_recorded();
     455
     456        if ( empty( $activity_date ) ) {
     457                return $classes;
     458        }
     459       
     460        $classes .= ' date-recorded-' . strtotime( $activity_date );
     461
     462        return $classes;
     463}
     464add_filter( 'bp_get_activity_css_class', 'bp_activity_timestamp_class', 9, 1 );
     465
     466/**
    441467 * Use WordPress Heartbeat API to check for latest activity update.
    442468 *
    443469 * @since BuddyPress (2.0.0)
    444470 *
    445471 * @uses bp_activity_get_last_updated() to get the recorded date of the last activity
    446 
     472 *
    447473 * @param array $response
    448474 * @param array $data
    449475 * @return array $response
    function bp_activity_newest_class( $classes = '' ) { 
    451477function bp_activity_heartbeat_last_recorded( $response = array(), $data = array() ) {
    452478        $bp = buddypress();
    453479
    454         if ( empty( $data['bp_activity_last_id'] ) ) {
     480        if ( empty( $data['bp_activity_last_recorded'] ) ) {
    455481                return $response;
    456482        }
    457483
    function bp_activity_heartbeat_last_recorded( $response = array(), $data = array 
    459485        // filters), but force the offset to get only new items
    460486        $activity_latest_args = bp_parse_args(
    461487                bp_ajax_querystring( 'activity' ),
    462                 array( 'offset' => absint( $data['bp_activity_last_id'] ) + 1 ),
     488                array( 'since' => date( 'Y-m-d H:i:s', $data['bp_activity_last_recorded'] ) ),
    463489                'activity_latest_args'
    464490        );
    465491
    466492        $newest_activities = array();
    467         $last_activity_id = 0;
     493        $last_activity_recorded = 0;
    468494
    469495        // Temporarly add a just-posted class for new activity items
    470496        add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
    function bp_activity_heartbeat_last_recorded( $response = array(), $data = array 
    474500                while ( bp_activities() ) {
    475501                        bp_the_activity();
    476502
    477                         if ( $last_activity_id < bp_get_activity_id() ) {
    478                                 $last_activity_id = bp_get_activity_id();
     503                        $atime = strtotime( bp_get_activity_date_recorded() );
     504                        if ( $last_activity_recorded < $atime ) {
     505                                $last_activity_recorded = $atime;
    479506                        }
    480507
    481508                        bp_get_template_part( 'activity/entry' );
    482509                }
    483510        }
    484511
    485         $newest_activities['activities'] = ob_get_contents();
    486         $newest_activities['last_id']    = $last_activity_id;
     512        $newest_activities['activities']    = ob_get_contents();
     513        $newest_activities['last_recorded'] = $last_activity_recorded;
    487514        ob_end_clean();
    488515
    489516        // Remove the temporary filter
    490517        remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
    491518
    492         if ( ! empty( $newest_activities['last_id'] ) ) {
     519        if ( ! empty( $newest_activities['last_recorded'] ) ) {
    493520                $response['bp_activity_newest_activities'] = $newest_activities;
    494521        }
    495522
  • bp-activity/bp-activity-template.php

    diff --git bp-activity/bp-activity-template.php bp-activity/bp-activity-template.php
    index e6b57dd..8e28b6b 100644
    function bp_has_activities( $args = '' ) { 
    547547                'primary_id'        => $primary_id,  // object ID to filter on e.g. a group_id or forum_id or blog_id etc.
    548548                'secondary_id'      => false,        // secondary object ID to filter on e.g. a post_id
    549549                'offset'            => false,        // return only items >= this ID
     550                'since'             => false,        // return only items recorded since this Y-m-d H:i:s date
    550551
    551552                'meta_query'        => false,        // filter on activity meta. See WP_Meta_Query for format
    552553
    function bp_has_activities( $args = '' ) { 
    641642        // into bp-custom.php or your theme's functions.php
    642643        if ( isset( $_GET['afilter'] ) && apply_filters( 'bp_activity_enable_afilter_support', false ) )
    643644                $filter = array( 'object' => $_GET['afilter'] );
    644         else if ( ! empty( $user_id ) || ! empty( $object ) || ! empty( $action ) || ! empty( $primary_id ) || ! empty( $secondary_id ) || ! empty( $offset ) )
    645                 $filter = array( 'user_id' => $user_id, 'object' => $object, 'action' => $action, 'primary_id' => $primary_id, 'secondary_id' => $secondary_id, 'offset' => $offset );
     645        else if ( ! empty( $user_id ) || ! empty( $object ) || ! empty( $action ) || ! empty( $primary_id ) || ! empty( $secondary_id ) || ! empty( $offset ) || ! empty( $since ) )
     646                $filter = array( 'user_id' => $user_id, 'object' => $object, 'action' => $action, 'primary_id' => $primary_id, 'secondary_id' => $secondary_id, 'offset' => $offset, 'since' => $since );
    646647        else
    647648                $filter = false;
    648649
    function bp_send_public_message_link() { 
    28052806 */
    28062807function bp_activity_recurse_comments_activity_ids( $activity = array(), $activity_ids = array() ) {
    28072808        if ( is_array( $activity ) && ! empty( $activity['activities'] ) ) {
    2808                 $activity = $activity['activities'][0]; 
     2809                $activity = $activity['activities'][0];
    28092810        }
    28102811
    28112812        if ( ! empty( $activity->children ) ) {
  • bp-templates/bp-legacy/buddypress-functions.php

    diff --git bp-templates/bp-legacy/buddypress-functions.php bp-templates/bp-legacy/buddypress-functions.php
    index e1b3431..bca42bb 100644
    function bp_legacy_theme_post_update() { 
    706706        if ( empty( $activity_id ) )
    707707                exit( '-1<div id="message" class="error"><p>' . __( 'There was a problem posting your update, please try again.', 'buddypress' ) . '</p></div>' );
    708708
    709         $last_id = isset( $_POST['offset'] ) ? absint( $_POST['offset'] ) + 1 : 0;
    710         if ( $last_id ) {
    711                 $activity_args = array( 'offset' => $last_id );
    712                 $bp->activity->new_update_id = $activity_id;
     709        $last_recorded = isset( $_POST['since'] ) ? date( 'Y-m-d H:i:s', intval( $_POST['since'] ) ) : 0;
     710        if ( $last_recorded ) {
     711                $activity_args = array( 'since' => $last_recorded );
     712                $bp->activity->last_recorded = $last_recorded;
    713713                add_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
    714714        } else {
    715715                $activity_args = array( 'include' => $activity_id );
    function bp_legacy_theme_post_update() { 
    722722                }
    723723        }
    724724
    725         if ( ! empty( $last_id ) ) {
     725        if ( ! empty( $last_recorded ) ) {
    726726                remove_filter( 'bp_get_activity_css_class', 'bp_activity_newest_class', 10, 1 );
    727727        }
    728728
  • bp-templates/bp-legacy/js/buddypress.js

    diff --git bp-templates/bp-legacy/js/buddypress.js bp-templates/bp-legacy/js/buddypress.js
    index 610f704..7b62056 100644
    var bp_ajax_request = null; 
    66
    77// Global variables to temporarly store newest activities
    88var newest_activities = '';
    9 var activity_last_id  = 0;
     9var activity_last_recorded  = 0;
    1010
    1111jq(document).ready( function() {
    1212        /**** Page Load Actions *******************************************************/
    jq(document).ready( function() { 
    9191
    9292        /* New posts */
    9393        jq("#aw-whats-new-submit").on( 'click', function() {
    94                 var last_displayed_id = 0;
     94                var last_date_recorded = 0;
    9595                var button = jq(this);
    9696                var form = button.closest("form#whats-new-form");
    9797
    jq(document).ready( function() { 
    111111                var item_id = jq("#whats-new-post-in").val();
    112112                var content = jq("#whats-new").val();
    113113                var firstrow = jq( '#buddypress ul.activity-list li' ).first();
     114                var activity_row = firstrow;
    114115
    115                 if ( firstrow.hasClass( 'load-newest' ) ) {
    116                         last_displayed_id = firstrow.next().prop( 'id' ) ? firstrow.next().prop( 'id' ).replace( 'activity-','' ) : 0;
    117                 } else {
    118                         last_displayed_id = firstrow.prop( 'id' ) ? firstrow.prop( 'id' ).replace( 'activity-','' ) : 0;
     116                if ( activity_row.hasClass( 'load-newest' ) ) {
     117                        activity_row = firstrow.next();
     118                }
     119
     120                timestamp = activity_row.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
     121               
     122                if ( timestamp ) {
     123                        last_date_recorded = timestamp[1];
    119124                }
    120125
    121126                /* Set object for non-profile posts */
    jq(document).ready( function() { 
    130135                        'content': content,
    131136                        'object': object,
    132137                        'item_id': item_id,
    133                         'offset': last_displayed_id,
     138                        'since': last_date_recorded,
    134139                        '_bp_as_nonce': jq('#_bp_as_nonce').val() || ''
    135140                },
    136141                function(response) {
    jq(document).ready( function() { 
    157162
    158163                                jq("#activity-stream").prepend(response);
    159164
    160                                 if ( ! last_displayed_id )
     165                                if ( ! last_date_recorded )
    161166                                        jq("#activity-stream li:first").addClass('new-update just-posted');
    162167
    163168                                if ( 0 != jq("#latest-update").length ) {
    jq(document).ready( function() { 
    184189
    185190                                // reset vars to get newest activities
    186191                                newest_activities = '';
    187                                 activity_last_id  = 0;
     192                                activity_last_recorded  = 0;
    188193                        }
    189194
    190195                        jq("#whats-new-options").animate({
    jq(document).ready( function() { 
    305310                        var id        = li.attr('id').substr( 9, li.attr('id').length );
    306311                        var link_href = target.attr('href');
    307312                        var nonce     = link_href.split('_wpnonce=');
     313                        var timestamp = li.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
    308314
    309315                        nonce = nonce[1];
    310316
    jq(document).ready( function() { 
    325331                                        li.slideUp(300);
    326332
    327333                                        // reset vars to get newest activities
    328                                         if ( activity_last_id == id ) {
     334                                        if ( timestamp && activity_last_recorded == timestamp[1] ) {
    329335                                                newest_activities = '';
    330                                                 activity_last_id  = 0;
     336                                                activity_last_recorded  = 0;
    331337                                        }
    332338                                }
    333339                        });
    jq(document).ready( function() { 
    337343
    338344                // Spam activity stream items
    339345                if ( target.hasClass( 'spam-activity' ) ) {
    340                         var li = target.parents( 'div.activity ul li' );
     346                        var li        = target.parents( 'div.activity ul li' );
     347                        var timestamp = li.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
    341348                        target.addClass( 'loading' );
    342349
    343350                        jq.post( ajaxurl, {
    jq(document).ready( function() { 
    354361                                } else {
    355362                                        li.slideUp( 300 );
    356363                                        // reset vars to get newest activities
    357                                         if ( activity_last_id == id ) {
     364                                        if ( timestamp && activity_last_recorded == timestamp[1] ) {
    358365                                                newest_activities = '';
    359                                                 activity_last_id  = 0;
     366                                                activity_last_recorded  = 0;
    360367                                        }
    361368                                }
    362369                        });
    jq(document).ready( function() { 
    407414                        event.preventDefault();
    408415
    409416                        target.parent().hide();
     417
     418                        /**
     419                         * If a plugin is updating the recorded_date of an activity
     420                         * it will be loaded as a new one. We need to look in the
     421                         * stream and eventually remove similar ids to avoid "double".
     422                         */
     423                        activity_html = jq.parseHTML( newest_activities );
     424                       
     425                        jq.each( activity_html, function( i, el ){
     426                                if( 'LI' == el.nodeName && jq(el).hasClass( 'just-posted' ) ) {
     427                                        if( jq( '#' + jq(el).attr( 'id' ) ).length )
     428                                                jq( '#' + jq(el).attr( 'id' ) ).remove();
     429                                }
     430                        } );
     431
     432                        // Now the stream is cleaned, prepend newest
    410433                        jq( '#buddypress ul.activity-list' ).prepend( newest_activities );
    411434
    412435                        // reset the newest activities now they're displayed
    jq(document).ready( function() { 
    14921515
    14931516        // Set the last id to request after
    14941517        jq( document ).on( 'heartbeat-send.buddypress', function( e, data ) {
     1518               
     1519                firstrow = 0;
    14951520
    14961521                // First row is default latest activity id
    14971522                if ( jq( '#buddypress ul.activity-list li' ).first().prop( 'id' ) ) {
    1498                         firstrow = jq( '#buddypress ul.activity-list li' ).first().prop( 'id' ).replace( 'activity-','' );
    1499                 } else {
    1500                         firstrow = 0;
     1523                        // getting the timestamp
     1524                        timestamp = jq( '#buddypress ul.activity-list li' ).first().prop( 'class' ).match( /date-recorded-([0-9]+)/ );
     1525
     1526                        if ( timestamp ) {
     1527                                firstrow = timestamp[1];
     1528                        }
    15011529                }
    15021530
    1503                 if ( 0 == activity_last_id || Number( firstrow ) > activity_last_id )
    1504                         activity_last_id = Number( firstrow );
     1531                if ( 0 == activity_last_recorded || Number( firstrow ) > activity_last_recorded )
     1532                        activity_last_recorded = Number( firstrow );
    15051533
    1506                 data['bp_activity_last_id'] = activity_last_id;
     1534                data['bp_activity_last_recorded'] = activity_last_recorded;
    15071535        });
    15081536
    1509         // Increment newest_activities and activity_last_id if data has been returned
     1537        // Increment newest_activities and activity_last_recorded if data has been returned
    15101538        jq( document ).on( 'heartbeat-tick', function( e, data ) {
    15111539
    15121540                // Only proceed if we have newest activities
    jq(document).ready( function() { 
    15151543                }
    15161544
    15171545                newest_activities = data['bp_activity_newest_activities']['activities'] + newest_activities;
    1518                 activity_last_id  = Number( data['bp_activity_newest_activities']['last_id'] );
     1546                activity_last_recorded  = Number( data['bp_activity_newest_activities']['last_recorded'] );
    15191547
    15201548                if ( jq( '#buddypress ul.activity-list li' ).first().hasClass( 'load-newest' ) )
    15211549                        return;
  • tests/testcases/activity/class.BP_Activity_Activity.php

    diff --git tests/testcases/activity/class.BP_Activity_Activity.php tests/testcases/activity/class.BP_Activity_Activity.php
    index b4f61bc..117771e 100644
    class BP_Tests_Activity_Class extends BP_UnitTestCase { 
    282282                $ids = wp_list_pluck( $activity['activities'], 'id' );
    283283                $this->assertEquals( array( $a3, $a2 ), $ids );
    284284        }
     285
     286        /**
     287         * @group get
     288         */
     289        public function test_get_with_since() {
     290                $now = time();
     291                $a1 = $this->factory->activity->create( array(
     292                        'content' => 'Life Rules',
     293                        'recorded_time' => date( 'Y-m-d H:i:s', $now - 100 ),
     294                ) );
     295                $a2 = $this->factory->activity->create( array(
     296                        'content' => 'Life Drools',
     297                        'recorded_time' => date( 'Y-m-d H:i:s', $now - 50 ),
     298                ) );
     299                $a3 = $this->factory->activity->create( array(
     300                        'content' => 'Life Drools',
     301                        'recorded_time' => date( 'Y-m-d H:i:s', $now - 10 ),
     302                ) );
     303
     304                $activity = BP_Activity_Activity::get( array(
     305                        'filter' => array(
     306                                'since' => date( 'Y-m-d H:i:s', $now - 70 ),
     307                        ),
     308                ) );
     309                $ids = wp_list_pluck( $activity['activities'], 'id' );
     310                $this->assertEquals( array( $a3, $a2 ), $ids );
     311        }
     312
    285313        /**
    286314         * @group get_id
    287315         */