Ticket #5128: 5128.patch
| File 5128.patch, 19.4 KB (added by , 12 years ago) |
|---|
-
bp-activity/bp-activity-classes.php
diff --git bp-activity/bp-activity-classes.php bp-activity/bp-activity-classes.php index 016c8dd..304fc15 100644
class BP_Activity_Activity { 355 355 $where_conditions[] = "a.type != 'activity_comment'"; 356 356 } 357 357 358 // Exclude 'last_activity' items unless the 'action' filter has 359 // been explicitly set 360 if ( empty( $filter['object'] ) ) { 361 $where_conditions[] = "a.type != 'last_activity'"; 362 } 363 358 364 // Filter the where conditions 359 365 $where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql ); 360 366 … … class BP_Activity_Activity { 978 984 global $wpdb; 979 985 980 986 // split items at the comma 981 $items_dirty = explode( ',', $items ); 987 if ( ! is_array( $items ) ) { 988 $items = explode( ',', $items ); 989 } 982 990 983 991 // array of prepared integers or quoted strings 984 992 $items_prepared = array(); 985 993 986 994 // clean up and format each item 987 foreach ( $items _dirtyas $item ) {995 foreach ( $items as $item ) { 988 996 // clean up the string 989 997 $item = trim( $item ); 990 998 // pass everything through prepare for security and to safely quote strings … … class BP_Activity_Activity { 1036 1044 1037 1045 if ( !empty( $filter_array['action'] ) ) { 1038 1046 $action_sql = BP_Activity_Activity::get_in_operator_sql( 'a.type', $filter_array['action'] ); 1039 if ( ! empty( $action_sql ) )1047 if ( ! empty( $action_sql ) ) 1040 1048 $filter_sql[] = $action_sql; 1041 1049 } 1042 1050 -
bp-core/admin/bp-core-schema.php
diff --git bp-core/admin/bp-core-schema.php bp-core/admin/bp-core-schema.php index 6001979..a082d87 100644
function bp_core_install( $active_components = false ) { 23 23 if ( empty( $active_components ) ) 24 24 $active_components = apply_filters( 'bp_active_components', bp_get_option( 'bp-active-components' ) ); 25 25 26 // Activity Streams 27 // Install tables even when inactive, to store last_activity data 28 bp_core_install_activity_streams(); 29 26 30 // Notifications 27 31 if ( !empty( $active_components['notifications'] ) ) 28 32 bp_core_install_notifications(); 29 33 30 // Activity Streams31 if ( !empty( $active_components['activity'] ) )32 bp_core_install_activity_streams();33 34 34 // Friend Connections 35 35 if ( !empty( $active_components['friends'] ) ) 36 36 bp_core_install_friends(); -
bp-core/bp-core-classes.php
diff --git bp-core/bp-core-classes.php bp-core/bp-core-classes.php index ff8744a..9b2cf5a 100644
class BP_User_Query { 245 245 // number of minutes used as an interval 246 246 case 'online' : 247 247 $this->uid_name = 'user_id'; 248 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$ wpdb->usermeta} u";249 $sql['where'][] = $wpdb->prepare( "u. meta_key = %s", bp_get_user_meta_key( 'last_activity' ));250 $sql['where'][] = $wpdb->prepare( "u. meta_value>= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters( 'bp_user_query_online_interval', 15 ) );251 $sql['orderby'] = "ORDER BY u. meta_value";248 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$bp->members->table_name_last_activity} u"; 249 $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id ); 250 $sql['where'][] = $wpdb->prepare( "u.date_recorded >= DATE_SUB( UTC_TIMESTAMP(), INTERVAL %d MINUTE )", apply_filters( 'bp_user_query_online_interval', 15 ) ); 251 $sql['orderby'] = "ORDER BY u.date_recorded"; 252 252 $sql['order'] = "DESC"; 253 253 254 254 break; … … class BP_User_Query { 259 259 case 'newest' : 260 260 case 'random' : 261 261 $this->uid_name = 'user_id'; 262 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$ wpdb->usermeta} u";263 $sql['where'][] = $wpdb->prepare( "u. meta_key = %s", bp_get_user_meta_key( 'last_activity' ));262 $sql['select'] = "SELECT u.{$this->uid_name} as id FROM {$bp->members->table_name_last_activity} u"; 263 $sql['where'][] = $wpdb->prepare( "u.component = %s AND u.type = 'last_activity'", buddypress()->members->id ); 264 264 265 265 if ( 'newest' == $type ) { 266 266 $sql['orderby'] = "ORDER BY u.user_id"; … … class BP_User_Query { 268 268 } else if ( 'random' == $type ) { 269 269 $sql['orderby'] = "ORDER BY rand()"; 270 270 } else { 271 $sql['orderby'] = "ORDER BY u. meta_value";271 $sql['orderby'] = "ORDER BY u.date_recorded"; 272 272 $sql['order'] = "DESC"; 273 273 } 274 274 … … class BP_User_Query { 540 540 // Turn user ID's into a query-usable, comma separated value 541 541 $user_ids_sql = implode( ',', wp_parse_id_list( $this->user_ids ) ); 542 542 543 $bp = buddypress(); 544 543 545 /** 544 546 * Use this action to independently populate your own custom extras. 545 547 * … … class BP_User_Query { 555 557 */ 556 558 do_action_ref_array( 'bp_user_query_populate_extras', array( $this, $user_ids_sql ) ); 557 559 560 // Fetch last_active data from the activity table 561 $last_activities = BP_Core_User::get_last_activity( $this->user_ids ); 562 563 if ( ! empty( $last_activities ) ) { 564 foreach ( $last_activities as $la_user => $la_value ) { 565 if ( isset( $this->results[ $la_user ] ) ) { 566 $this->results[ $la_user ]->last_activity = $la_value['date_recorded']; 567 } 568 } 569 } 570 558 571 // Fetch usermeta data 559 572 // We want the three following pieces of info from usermeta: 560 573 // - friend count 561 // - last activity562 574 // - latest update 563 575 $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' ); 564 $last_activity_key = bp_get_user_meta_key( 'last_activity' );565 576 $bp_latest_update_key = bp_get_user_meta_key( 'bp_latest_update' ); 566 577 567 578 // total_friend_count must be set for each user, even if its … … class BP_User_Query { 571 582 } 572 583 573 584 // Create, prepare, and run the seperate usermeta query 574 $user_metas = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s,%s ,%s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $last_activity_key, $bp_latest_update_key ) );585 $user_metas = $wpdb->get_results( $wpdb->prepare( "SELECT user_id, meta_key, meta_value FROM {$wpdb->usermeta} WHERE meta_key IN (%s,%s) AND user_id IN ({$user_ids_sql})", $total_friend_count_key, $bp_latest_update_key ) ); 575 586 576 587 // The $members_template global expects the index key to be different 577 588 // from the meta_key in some cases, so we rejig things here. … … class BP_User_Query { 581 592 $key = 'total_friend_count'; 582 593 break; 583 594 584 case $last_activity_key :585 $key = 'last_activity';586 break;587 588 595 case $bp_latest_update_key : 589 596 $key = 'latest_update'; 590 597 break; … … class BP_Core_User { 1299 1306 1300 1307 return $user; 1301 1308 } 1309 1310 /** 1311 * Get last activity data for a user or set of users. 1312 * 1313 * @param int|array User IDs or multiple user IDs. 1314 * @return array 1315 */ 1316 public static function get_last_activity( $user_id ) { 1317 global $wpdb; 1318 1319 if ( is_array( $user_id ) ) { 1320 $user_ids = wp_parse_id_list( $user_id ); 1321 } else { 1322 $user_ids = array( absint( $user_id ) ); 1323 } 1324 1325 if ( empty( $user_ids ) ) { 1326 return false; 1327 } 1328 1329 $bp = buddypress(); 1330 1331 $user_ids_sql = implode( ',', $user_ids ); 1332 $user_count = count( $user_ids ); 1333 1334 $last_activities = $wpdb->get_results( $wpdb->prepare( "SELECT id, user_id, date_recorded FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity' AND user_id IN ({$user_ids_sql}) LIMIT {$user_count}", $bp->members->id ) ); 1335 1336 // Re-key 1337 $retval = array(); 1338 foreach ( $last_activities as $last_activity ) { 1339 $retval[ $last_activity->user_id ] = array( 1340 'user_id' => $last_activity->user_id, 1341 'date_recorded' => $last_activity->date_recorded, 1342 'activity_id' => $last_activity->id, 1343 ); 1344 } 1345 1346 return $retval; 1347 } 1348 1349 /** 1350 * Set a user's last_activity value. 1351 * 1352 * Will create a new entry if it does not exist. Otherwise updates the 1353 * existing entry. 1354 * 1355 * @since 2.0 1356 * 1357 * @param int $user_id ID of the user whose last_activity you are updating. 1358 * @param string $time MySQL-formatted time string. 1359 * @return bool True on success, false on failure. 1360 */ 1361 public static function update_last_activity( $user_id, $time ) { 1362 global $wpdb; 1363 1364 $table_name = buddypress()->members->table_name_last_activity; 1365 1366 $existing = self::get_last_activity( $user_id ); 1367 1368 if ( ! empty( $existing ) ) { 1369 $data = array( 1370 'date_recorded' => $time, 1371 ); 1372 1373 $data_format = array( 1374 '%s', 1375 ); 1376 1377 $where = array( 1378 ); 1379 1380 $where_format = array( 1381 '%d', 1382 ); 1383 1384 $updated = $wpdb->update( 1385 $table_name, 1386 1387 // Data to update 1388 array( 1389 'date_recorded' => $time, 1390 ), 1391 1392 // WHERE 1393 array( 1394 'id' => $existing[ $user_id ]['activity_id'], 1395 ), 1396 1397 // Data sanitization format 1398 array( 1399 '%s', 1400 ), 1401 1402 // WHERE sanitization format 1403 array( 1404 '%d', 1405 ) 1406 ); 1407 } else { 1408 $updated = $wpdb->insert( 1409 $table_name, 1410 1411 // Data 1412 array( 1413 'user_id' => $user_id, 1414 'component' => buddypress()->members->id, 1415 'type' => 'last_activity', 1416 'date_recorded' => $time, 1417 ), 1418 1419 // Data sanitization format 1420 array( 1421 '%d', 1422 '%s', 1423 '%s', 1424 '%s', 1425 ) 1426 ); 1427 } 1428 1429 return $updated; 1430 } 1431 1432 /** 1433 * Delete a user's last_activity value. 1434 * 1435 * @since 2.0 1436 * 1437 * @param int $user_id 1438 * @return bool True on success, false on failure or if no last_activity 1439 * is found for the user. 1440 */ 1441 public static function delete_last_activity( $user_id ) { 1442 global $wpdb; 1443 1444 $existing = self::get_last_activity( $user_id ); 1445 1446 if ( empty( $existing ) ) { 1447 return false; 1448 } 1449 1450 $deleted = $wpdb->delete( 1451 buddypress()->members->table_name_last_activity, 1452 1453 // WHERE 1454 array( 1455 'id' => $existing[ $user_id ]['activity_id'], 1456 ), 1457 1458 // WHERE sanitization format 1459 array( 1460 '%s', 1461 ) 1462 ); 1463 1464 return $deleted; 1465 } 1302 1466 } 1303 1467 1304 1468 -
bp-core/bp-core-functions.php
diff --git bp-core/bp-core-functions.php bp-core/bp-core-functions.php index 0e95ce7..120875e 100644
function bp_core_get_last_activity( $last_activity_date, $string ) { 1038 1038 * 1039 1039 * Plugin authors should use BP's _user_meta() functions, which bakes in 1040 1040 * bp_get_user_meta_key(): 1041 * $ last_active = bp_get_user_meta( $user_id, 'last_activity', true );1041 * $friend_count = bp_get_user_meta( $user_id, 'total_friend_count', true ); 1042 1042 * If you must use WP's _user_meta() functions directly for some reason, you 1043 1043 * should use this function to determine the $key parameter, eg 1044 * $ last_active = get_user_meta( $user_id, bp_get_user_meta_key( 'last_activity' ), true );1044 * $friend_count = get_user_meta( $user_id, bp_get_user_meta_key( 'total_friend_count' ), true ); 1045 1045 * If using the WP functions, do not not hardcode your meta keys. 1046 1046 * 1047 1047 * @since BuddyPress (1.5.0) -
bp-core/bp-core-update.php
diff --git bp-core/bp-core-update.php bp-core/bp-core-update.php index dd353bb..c626979 100644
function bp_version_updater() { 225 225 if ( $raw_db_version < 7553 ) { 226 226 bp_update_to_1_9(); 227 227 } 228 229 // 2.0 230 if ( $raw_db_version < 7767 ) { 231 bp_update_to_2_0(); 232 } 228 233 } 229 234 230 235 /** All done! *************************************************************/ … … function bp_update_to_1_9() { 301 306 } 302 307 303 308 /** 309 * 2.0 update routine. 310 * 311 * - Ensure that the activity tables are installed, for last_activity storage. 312 * - Migrate last_activity data from usermeta to activity table 313 */ 314 function bp_update_to_2_0() { 315 global $wpdb; 316 317 // Install activity tables 318 bp_core_install_activity_streams(); 319 320 $bp = buddypress(); 321 322 // Migrate data 323 // The "NOT IN" clause prevents duplicates 324 $sql = "INSERT INTO {$bp->members->table_name_last_activity} (`user_id`, `component`, `type`, `date_recorded` ) ( 325 SELECT user_id, '{$bp->members->id}' as component, 'last_activity' as type, meta_value AS date_recorded 326 FROM {$wpdb->usermeta} 327 WHERE 328 meta_key = 'last_activity' 329 AND 330 user_id NOT IN ( 331 SELECT user_id 332 FROM {$bp->members->table_name_last_activity} 333 WHERE component = '{$bp->members->id}' AND type = 'last_activity' 334 ) 335 );"; 336 337 $wpdb->query( $sql ); 338 } 339 340 /** 304 341 * Redirect user to BP's What's New page on first page load after activation. 305 342 * 306 343 * @since BuddyPress (1.7.0) -
bp-friends/bp-friends-classes.php
diff --git bp-friends/bp-friends-classes.php bp-friends/bp-friends-classes.php index 0ea096e..cb4afac 100644
class BP_Friends_Friendship { 364 364 public static function get_bulk_last_active( $user_ids ) { 365 365 global $wpdb; 366 366 367 $user_ids = implode( ',', wp_parse_id_list( $user_ids ) ); 367 $last_activities = BP_Core_User::get_last_activity( $user_ids ); 368 369 // Sort and structure as expected in legacy function 370 usort( $last_activities, create_function( '$a, $b', ' 371 if ( $a["date_recorded"] == $b["date_recorded"] ) { 372 return 0; 373 } 374 375 return ( strtotime( $a["date_recorded"] ) < strtotime( $b["date_recorded"] ) ) ? 1 : -1; 376 ' ) ); 377 378 $retval = array(); 379 foreach ( $last_activities as $last_activity ) { 380 $u = new stdClass; 381 $u->last_activity = $last_activity['date_recorded']; 382 $u->user_id = $last_activity['user_id']; 383 384 $retval[] = $u; 385 } 368 386 369 return $ wpdb->get_results( $wpdb->prepare( "SELECT meta_value as last_activity, user_id FROM {$wpdb->usermeta} WHERE meta_key = %s AND user_id IN ( {$user_ids} ) ORDER BY meta_value DESC", bp_get_user_meta_key( 'last_activity' ) ) );387 return $retval; 370 388 } 371 389 372 390 /** -
bp-loader.php
diff --git bp-loader.php bp-loader.php index f17ebbb..0d3e1bb 100644
class BuddyPress { 304 304 /** Versions **************************************************/ 305 305 306 306 $this->version = '2.0-alpha-7752'; 307 $this->db_version = 7 553;307 $this->db_version = 7767; 308 308 309 309 /** Loading ***************************************************/ 310 310 -
bp-members/bp-members-functions.php
diff --git bp-members/bp-members-functions.php bp-members/bp-members-functions.php index d7744fa..e15444a 100644
function bp_core_get_active_member_count() { 510 510 global $wpdb; 511 511 512 512 if ( !$count = get_transient( 'bp_active_member_count' ) ) { 513 $bp = buddypress(); 514 513 515 // Avoid a costly join by splitting the lookup 514 516 if ( is_multisite() ) { 515 517 $sql = "SELECT ID FROM {$wpdb->users} WHERE (user_status != 0 OR deleted != 0 OR user_status != 0)"; … … function bp_core_get_active_member_count() { 519 521 520 522 $exclude_users = $wpdb->get_col( $sql ); 521 523 $exclude_users_sql = !empty( $exclude_users ) ? "AND user_id NOT IN (" . implode( ',', wp_parse_id_list( $exclude_users ) ) . ")" : ''; 522 $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(user_id) FROM {$ wpdb->usermeta} WHERE meta_key = %s {$exclude_users_sql}", bp_get_user_meta_key( 'last_activity' )) );524 $count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(user_id) FROM {$bp->members->table_name_last_activity} WHERE component = %s AND type = 'last_activity' {$exclude_users_sql}", $bp->members->id ) ); 523 525 524 526 set_transient( 'bp_active_member_count', $count ); 525 527 } … … function bp_update_user_last_activity( $user_id = 0, $time = '' ) { 840 842 $time = bp_core_current_time(); 841 843 } 842 844 843 return bp_update_user_meta( $user_id, 'last_activity', $time );845 return BP_Core_User::update_last_activity( $user_id, $time ); 844 846 } 845 847 846 848 /** … … function bp_update_user_last_activity( $user_id = 0, $time = '' ) { 851 853 * string if none is found 852 854 */ 853 855 function bp_get_user_last_activity( $user_id = 0 ) { 854 // Fall back on current user 855 if ( empty( $user_id ) ) { 856 $user_id = bp_loggedin_user_id(); 857 } 856 $activity = ''; 858 857 859 $activity = bp_get_user_meta( $user_id, 'last_activity', true ); 858 $last_activity = BP_Core_User::get_last_activity( $user_id ); 859 if ( ! empty( $last_activity[ $user_id ] ) ) { 860 $activity = $last_activity[ $user_id ]['date_recorded']; 861 } 860 862 861 863 return apply_filters( 'bp_get_user_last_activity', $activity, $user_id ); 862 864 } … … add_filter( 'authenticate', 'bp_core_boot_spammer', 30 ); 996 998 */ 997 999 function bp_core_remove_data( $user_id ) { 998 1000 999 // Remove usermeta1000 bp_delete_user_meta( $user_id, 'last_activity');1001 // Remove last_activity data 1002 BP_Core_User::delete_last_activity( $user_id ); 1001 1003 1002 1004 // Flush the cache to remove the user from all cached objects 1003 1005 wp_cache_flush(); -
bp-members/bp-members-loader.php
diff --git bp-members/bp-members-loader.php bp-members/bp-members-loader.php index c654240..d88380b 100644
class BP_Members_Component extends BP_Component { 72 72 'slug' => BP_MEMBERS_SLUG, 73 73 'root_slug' => isset( $bp->pages->members->slug ) ? $bp->pages->members->slug : BP_MEMBERS_SLUG, 74 74 'has_directory' => true, 75 'global_tables' => array( 76 'table_name_last_activity' => bp_core_get_table_prefix() . 'bp_activity', 77 ), 75 78 'search_string' => __( 'Search Members...', 'buddypress' ), 76 79 ) ); 77 80 -
tests/includes/testcase.php
diff --git tests/includes/testcase.php tests/includes/testcase.php index 3424efa..425f633 100644
class BP_UnitTestCase extends WP_UnitTestCase { 212 212 function create_user( $args = array() ) { 213 213 $r = wp_parse_args( $args, array( 214 214 'role' => 'subscriber', 215 'last_activity' => bp_core_current_time() - 60*60*24*365,215 'last_activity' => date( 'Y-m-d h:i:s', strtotime( bp_core_current_time() ) - 60*60*24*365 ), 216 216 ) ); 217 217 218 218 $last_activity = $r['last_activity']; -
tests/testcases/core/class-bp-core-user.php
diff --git tests/testcases/core/class-bp-core-user.php tests/testcases/core/class-bp-core-user.php index 61ca70b..d7b4545 100644
class BP_Tests_BP_Core_User_TestCases extends BP_UnitTestCase { 119 119 $this->assertEquals( array( $u1, $u3 ), $user_ids ); 120 120 } 121 121 122 /** 123 * @group last_activity 124 */ 125 public function test_get_last_activity() { 126 $u = $this->create_user(); 127 $time = bp_core_current_time(); 128 129 BP_Core_User::update_last_activity( $u, $time ); 130 131 $a = BP_Core_User::get_last_activity( $u ); 132 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 133 134 $this->assertEquals( $time, $found ); 135 } 122 136 137 /** 138 * @group last_activity 139 */ 140 public function test_update_last_activity() { 141 $u = $this->create_user(); 142 $time = bp_core_current_time(); 143 $time2 = '1968-12-25 01:23:45'; 144 145 BP_Core_User::update_last_activity( $u, $time ); 146 $a = BP_Core_User::get_last_activity( $u ); 147 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 148 $this->assertEquals( $time, $found ); 149 150 BP_Core_User::update_last_activity( $u, $time2 ); 151 $a = BP_Core_User::get_last_activity( $u ); 152 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 153 $this->assertEquals( $time2, $found ); 154 } 123 155 156 /** 157 * @group last_activity 158 */ 159 public function test_delete_last_activity() { 160 $u = $this->create_user(); 161 $time = bp_core_current_time(); 162 163 BP_Core_User::update_last_activity( $u, $time ); 164 $a = BP_Core_User::get_last_activity( $u ); 165 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 166 $this->assertEquals( $time, $found ); 167 168 BP_Core_User::delete_last_activity( $u ); 169 $a = BP_Core_User::get_last_activity( $u ); 170 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 171 $this->assertEquals( '', $found ); 172 } 124 173 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)