Skip to:
Content

BuddyPress.org

Ticket #5505: 5505.02.diff

File 5505.02.diff, 15.0 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 a210786..f18b6b9 100644
    class BP_Activity_Activity { 
    11771177         * @since BuddyPress (1.5.0)
    11781178         *
    11791179         * @param array $filter_array {
    1180          *     Fields and values to filter by. Each can be either a single
    1181          *     string, a comma-separated list, or an array of values.
     1180         *     Fields and values to filter by.
    11821181         *     @type array|string|id $user_id User ID(s).
    11831182         *     @type array|string $object Corresponds to the 'component'
    11841183         *           column in the database.
    class BP_Activity_Activity { 
    11881187         *           column in the database.
    11891188         *     @type array|string|int $secondary_id Corresponds to the
    11901189         *           'secondary_item_id' column in the database.
     1190         *     @type int $offset Return only those items with an ID greater
     1191         *           than the offset value.
     1192         *     @type string $since Return only those items that have a
     1193         *           date_recorded value greater than a given MySQL-formatted
     1194         *           date.
    11911195         * }
    11921196         * @return string The filter clause, for use in a SQL query.
    11931197         */
    class BP_Activity_Activity { 
    12301234                        $filter_sql[] = "a.id >= {$sid_sql}";
    12311235                }
    12321236
     1237                if ( ! empty( $filter_array['since'] ) ) {
     1238                        // Validate that this is a proper Y-m-d H:i:s date
     1239                        // Trick: parse to UNIX date then translate back
     1240                        $translated_date = date( 'Y-m-d H:i:s', strtotime( $filter_array['since'] ) );
     1241                        if ( $translated_date === $filter_array['since'] ) {
     1242                                $filter_sql[] = "a.date_recorded > '{$translated_date}'";
     1243                        }
     1244                }
     1245
    12331246                if ( empty( $filter_sql ) )
    12341247                        return false;
    12351248
  • 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..3fc5d6c 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() { 
    113113                var firstrow = jq( '#buddypress ul.activity-list li' ).first();
    114114
    115115                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                        firstrow = firstrow.next();
     117                }
     118
     119                timestamp = firstrow.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
     120               
     121                if ( timestamp ) {
     122                        last_date_recorded = timestamp[1];
    119123                }
    120124
    121125                /* Set object for non-profile posts */
    jq(document).ready( function() { 
    130134                        'content': content,
    131135                        'object': object,
    132136                        'item_id': item_id,
    133                         'offset': last_displayed_id,
     137                        'since': last_date_recorded,
    134138                        '_bp_as_nonce': jq('#_bp_as_nonce').val() || ''
    135139                },
    136140                function(response) {
    jq(document).ready( function() { 
    157161
    158162                                jq("#activity-stream").prepend(response);
    159163
    160                                 if ( ! last_displayed_id )
     164                                if ( ! last_date_recorded )
    161165                                        jq("#activity-stream li:first").addClass('new-update just-posted');
    162166
    163167                                if ( 0 != jq("#latest-update").length ) {
    jq(document).ready( function() { 
    184188
    185189                                // reset vars to get newest activities
    186190                                newest_activities = '';
    187                                 activity_last_id  = 0;
     191                                activity_last_recorded  = 0;
    188192                        }
    189193
    190194                        jq("#whats-new-options").animate({
    jq(document).ready( function() { 
    305309                        var id        = li.attr('id').substr( 9, li.attr('id').length );
    306310                        var link_href = target.attr('href');
    307311                        var nonce     = link_href.split('_wpnonce=');
     312                        var timestamp = li.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
    308313
    309314                        nonce = nonce[1];
    310315
    jq(document).ready( function() { 
    325330                                        li.slideUp(300);
    326331
    327332                                        // reset vars to get newest activities
    328                                         if ( activity_last_id == id ) {
     333                                        if ( timestamp && activity_last_recorded == timestamp[1] ) {
    329334                                                newest_activities = '';
    330                                                 activity_last_id  = 0;
     335                                                activity_last_recorded  = 0;
    331336                                        }
    332337                                }
    333338                        });
    jq(document).ready( function() { 
    337342
    338343                // Spam activity stream items
    339344                if ( target.hasClass( 'spam-activity' ) ) {
    340                         var li = target.parents( 'div.activity ul li' );
     345                        var li        = target.parents( 'div.activity ul li' );
     346                        var timestamp = li.prop( 'class' ).match( /date-recorded-([0-9]+)/ );
    341347                        target.addClass( 'loading' );
    342348
    343349                        jq.post( ajaxurl, {
    jq(document).ready( function() { 
    354360                                } else {
    355361                                        li.slideUp( 300 );
    356362                                        // reset vars to get newest activities
    357                                         if ( activity_last_id == id ) {
     363                                        if ( timestamp && activity_last_recorded == timestamp[1] ) {
    358364                                                newest_activities = '';
    359                                                 activity_last_id  = 0;
     365                                                activity_last_recorded  = 0;
    360366                                        }
    361367                                }
    362368                        });
    jq(document).ready( function() { 
    14921498
    14931499        // Set the last id to request after
    14941500        jq( document ).on( 'heartbeat-send.buddypress', function( e, data ) {
     1501               
     1502                firstrow = 0;
    14951503
    14961504                // First row is default latest activity id
    14971505                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;
     1506                        // getting the timestamp
     1507                        timestamp = jq( '#buddypress ul.activity-list li' ).first().prop( 'class' ).match( /date-recorded-([0-9]+)/ );
     1508
     1509                        if ( timestamp ) {
     1510                                firstrow = timestamp[1];
     1511                        }
    15011512                }
    15021513
    1503                 if ( 0 == activity_last_id || Number( firstrow ) > activity_last_id )
    1504                         activity_last_id = Number( firstrow );
     1514                if ( 0 == activity_last_recorded || Number( firstrow ) > activity_last_recorded )
     1515                        activity_last_recorded = Number( firstrow );
    15051516
    1506                 data['bp_activity_last_id'] = activity_last_id;
     1517                data['bp_activity_last_recorded'] = activity_last_recorded;
    15071518        });
    15081519
    1509         // Increment newest_activities and activity_last_id if data has been returned
     1520        // Increment newest_activities and activity_last_recorded if data has been returned
    15101521        jq( document ).on( 'heartbeat-tick', function( e, data ) {
    15111522
    15121523                // Only proceed if we have newest activities
    jq(document).ready( function() { 
    15151526                }
    15161527
    15171528                newest_activities = data['bp_activity_newest_activities']['activities'] + newest_activities;
    1518                 activity_last_id  = Number( data['bp_activity_newest_activities']['last_id'] );
     1529                activity_last_recorded  = Number( data['bp_activity_newest_activities']['last_recorded'] );
    15191530
    15201531                if ( jq( '#buddypress ul.activity-list li' ).first().hasClass( 'load-newest' ) )
    15211532                        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 4b0744a..f8eaa8b 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         */