Skip to:
Content

BuddyPress.org

Ticket #5409: 5409.diff

File 5409.diff, 6.5 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 5d5a870..6d70a9d 100644
    class BP_Activity_Activity { 
    11561156         *
    11571157         * @since BuddyPress (1.2)
    11581158         *
     1159         * @param array $args
    11591160         * @return string ISO timestamp.
    11601161         */
    1161         public static function get_last_updated() {
     1162        public static function get_last_updated( $args = array() ) {
    11621163                global $bp, $wpdb;
    11631164
    1164                 return $wpdb->get_var( "SELECT date_recorded FROM {$bp->activity->table_name} ORDER BY date_recorded DESC LIMIT 1" );
     1165                $r = bp_parse_args( $args,
     1166                        array(
     1167                                'field_name'       => 'date_recorded',
     1168                                'search_terms'     => false,
     1169                                'filter'           => false,      // See self::get_filter_sql()
     1170                                'display_comments' => 'threaded', // or stream or false
     1171                                'show_hidden'      => false,      // Show items marked hide_sitewide
     1172                        ),
     1173                        'activity_last_updated'
     1174                );
     1175                extract( $r );
     1176
     1177                if ( 'id' != $field_name ) {
     1178                        return $wpdb->get_var( "SELECT date_recorded FROM {$bp->activity->table_name} ORDER BY date_recorded DESC LIMIT 1" );
     1179                }
     1180
     1181                $select_sql = "SELECT a.id FROM {$bp->activity->table_name} a";
     1182                $order_sql  = "ORDER BY a.id DESC";
     1183                $limit_sql  = "LIMIT 1";
     1184                $exclude_types = array();
     1185                $index_hint_sql = '';
     1186
     1187                $where_conditions['spam_sql'] = 'a.is_spam = 0';
     1188
     1189                // Searching
     1190                if ( $search_terms ) {
     1191                        $search_terms = esc_sql( $search_terms );
     1192                        $where_conditions['search_sql'] = "a.content LIKE '%%" . esc_sql( like_escape( $search_terms ) ) . "%%'";
     1193                }
     1194                // Filtering
     1195                if ( $filter && $filter_sql = BP_Activity_Activity::get_filter_sql( $filter ) ) {
     1196                        $where_conditions['filter_sql'] = $filter_sql;
     1197                } else {
     1198                        $index_hint_sql = "USE INDEX (type)";
     1199                }
     1200
     1201                if ( ! $show_hidden )
     1202                        $where_conditions['hidden_sql'] = "a.hide_sitewide = 0";
     1203
     1204                if ( empty( $filter_array['action'] ) )
     1205                        $exclude_types[] = 'last_activity';
     1206
     1207                if ( false === $display_comments || 'threaded' === $display_comments )
     1208                        $exclude_types[] = 'activity_comment';
     1209
     1210                if ( ! empty( $exclude_types ) && $excluded_type = BP_Activity_Activity::get_in_operator_sql( 'a.type', $exclude_types ) ) {
     1211                        $excluded_type = str_replace( 'a.type IN', 'a.type NOT IN', $excluded_type );
     1212                        $where_conditions[] = $excluded_type;
     1213                }       
     1214
     1215                // Filter the where conditions
     1216                $where_conditions = apply_filters( 'bp_activity_get_last_updated_recorded_where', $where_conditions, $r );
     1217
     1218                // Join the where conditions together
     1219                $where_sql = 'WHERE ' . join( ' AND ', $where_conditions );
     1220
     1221                $last_updated = $wpdb->get_var( apply_filters( 'bp_activity_get_last_updated_sql', "{$select_sql} {$index_hint_sql} {$where_sql} {$order_sql} {$limit_sql}", $select_sql, $index_hint_sql, $where_sql, $order_sql ) );
     1222
     1223                return absint( $last_updated );
    11651224        }
    11661225
    11671226        /**
  • bp-activity/bp-activity-functions.php

    diff --git bp-activity/bp-activity-functions.php bp-activity/bp-activity-functions.php
    index 12016cd..aaa2986 100644
    function bp_activity_check_exists_by_content( $content ) { 
    507507 *
    508508 * @uses BP_Activity_Activity::get_last_updated() {@link BP_Activity_Activity}
    509509 * @uses apply_filters() To call the 'bp_activity_get_last_updated' hook.
    510  *
    511  * @return string Date last updated.
     510 * @param array args
     511 * @return string|int Date last updated | Activity id of the last updated depending on args.
    512512 */
    513 function bp_activity_get_last_updated() {
    514         return apply_filters( 'bp_activity_get_last_updated', BP_Activity_Activity::get_last_updated() );
     513function bp_activity_get_last_updated( $args = array() ) {
     514        // date_recorded is default value and is used by BP_Activity_Feed
     515        $field_name = 'date_recorded';
     516
     517        if ( ! empty( $args['field_name'] ) ) {
     518                $field_name = 'id';
     519        }
     520
     521        $last_updated = wp_cache_get( 'bp_activity_last_updated_' . $field_name , 'bp' );
     522
     523        if ( empty( $last_updated ) || ( is_array( $last_updated ) && $args != $last_updated['args'] ) ) {
     524                $last_updated = BP_Activity_Activity::get_last_updated( $args );
     525
     526                if( 'id' == $field_name ) {
     527                        $last_updated = array( 'id' => $last_updated, 'args' => $args );
     528                }
     529
     530                wp_cache_set( 'bp_activity_last_updated_' . $field_name, $last_updated, 'bp' );
     531        } else {
     532                return array_merge( $last_updated , array( ' cached' ) );
     533        }
     534
     535        return apply_filters_ref_array( 'bp_activity_get_last_updated', array( &$last_updated,  &$args ) );
    515536}
    516537
    517538/**
    function bp_activity_add( $args = '' ) { 
    11041125                BP_Activity_Activity::rebuild_activity_comment_tree( $activity->item_id );
    11051126
    11061127        wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
     1128        wp_cache_delete( 'bp_activity_last_updated_date_recorded', 'bp' );
     1129        wp_cache_delete( 'bp_activity_last_updated_id', 'bp' );
    11071130        do_action( 'bp_activity_add', $params );
    11081131
    11091132        return $activity->id;
    function bp_activity_delete( $args = '' ) { 
    13551378        do_action( 'bp_activity_deleted_activities', $activity_ids_deleted );
    13561379
    13571380        wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
     1381        wp_cache_delete( 'bp_activity_last_updated_date_recorded', 'bp' );
     1382       
     1383        if ( $last_updated_id == wp_cache_get( 'bp_activity_last_updated_id' , 'bp' ) && in_array( $last_updated_id['id'], $activity_ids_deleted ) ) {
     1384                wp_cache_delete( 'bp_activity_last_updated_id', 'bp' );
     1385        }
    13581386
    13591387        return true;
    13601388}
    function bp_activity_mark_as_spam( &$activity, $source = 'by_a_person' ) { 
    16571685        // Clear the activity stream first page cache
    16581686        wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
    16591687
     1688        // Clear the last updated date_recorded cache
     1689        wp_cache_delete( 'bp_activity_last_updated_date_recorded', 'bp' );
     1690       
     1691        // Clear the last updated id if the spammed activity is the one that is cached
     1692        if ( $last_updated_id == wp_cache_get( 'bp_activity_last_updated_id' , 'bp' ) && $last_updated_id['id'] == $activity->id ) {
     1693                wp_cache_delete( 'bp_activity_last_updated_id', 'bp' );
     1694        }
     1695
    16601696        // Clear the activity comment cache for this activity item
    16611697        wp_cache_delete( 'bp_activity_comments_' . $activity->id, 'bp' );
    16621698
    function bp_activity_mark_as_ham( &$activity, $source = 'by_a_person' ) { 
    16971733        // Clear the activity stream first page cache
    16981734        wp_cache_delete( 'bp_activity_sitewide_front', 'bp' );
    16991735
     1736        // Clear the last updated date_recorded cache
     1737        wp_cache_delete( 'bp_activity_last_updated_date_recorded', 'bp' );
     1738       
     1739        // Clear the last updated id cache
     1740        wp_cache_delete( 'bp_activity_last_updated_id', 'bp' );
     1741
    17001742        // Clear the activity comment cache for this activity item
    17011743        wp_cache_delete( 'bp_activity_comments_' . $activity->id, 'bp' );
    17021744