Changeset 7860
- Timestamp:
- 02/12/2014 09:27:20 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/bp-activity/bp-activity-classes.php
r7854 r7860 362 362 } 363 363 364 // Exclude 'last_activity' items unless the 'action' filter has 365 // been explicitly set 366 if ( empty( $filter['object'] ) ) { 367 $where_conditions[] = "a.type != 'last_activity'"; 368 } 369 364 370 // Filter the where conditions 365 371 $where_conditions = apply_filters( 'bp_activity_get_where_conditions', $where_conditions, $r, $select_sql, $from_sql, $join_sql ); … … 1063 1069 1064 1070 // split items at the comma 1065 $items_dirty = explode( ',', $items ); 1071 if ( ! is_array( $items ) ) { 1072 $items = explode( ',', $items ); 1073 } 1066 1074 1067 1075 // array of prepared integers or quoted strings … … 1069 1077 1070 1078 // clean up and format each item 1071 foreach ( $items _dirtyas $item ) {1079 foreach ( $items as $item ) { 1072 1080 // clean up the string 1073 1081 $item = trim( $item ); … … 1121 1129 if ( !empty( $filter_array['action'] ) ) { 1122 1130 $action_sql = BP_Activity_Activity::get_in_operator_sql( 'a.type', $filter_array['action'] ); 1123 if ( ! empty( $action_sql ) )1131 if ( ! empty( $action_sql ) ) 1124 1132 $filter_sql[] = $action_sql; 1125 1133 } -
trunk/bp-core/admin/bp-core-schema.php
r7521 r7860 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 30 // Activity Streams31 if ( !empty( $active_components['activity'] ) )32 bp_core_install_activity_streams();33 33 34 34 // Friend Connections -
trunk/bp-core/bp-core-classes.php
r7812 r7860 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 … … 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 ) { … … 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 } … … 538 538 // Turn user ID's into a query-usable, comma separated value 539 539 $user_ids_sql = implode( ',', wp_parse_id_list( $this->user_ids ) ); 540 541 $bp = buddypress(); 540 542 541 543 /** … … 554 556 do_action_ref_array( 'bp_user_query_populate_extras', array( $this, $user_ids_sql ) ); 555 557 558 // Fetch last_active data from the activity table 559 $last_activities = BP_Core_User::get_last_activity( $this->user_ids ); 560 561 if ( ! empty( $last_activities ) ) { 562 foreach ( $last_activities as $la_user => $la_value ) { 563 if ( isset( $this->results[ $la_user ] ) ) { 564 $this->results[ $la_user ]->last_activity = $la_value['date_recorded']; 565 } 566 } 567 } 568 556 569 // Fetch usermeta data 557 570 // We want the three following pieces of info from usermeta: 558 571 // - friend count 559 // - last activity560 572 // - latest update 561 573 $total_friend_count_key = bp_get_user_meta_key( 'total_friend_count' ); 562 $last_activity_key = bp_get_user_meta_key( 'last_activity' );563 574 $bp_latest_update_key = bp_get_user_meta_key( 'bp_latest_update' ); 564 575 … … 570 581 571 582 // Create, prepare, and run the seperate usermeta query 572 $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 ) );583 $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 ) ); 573 584 574 585 // The $members_template global expects the index key to be different … … 578 589 case $total_friend_count_key : 579 590 $key = 'total_friend_count'; 580 break;581 582 case $last_activity_key :583 $key = 'last_activity';584 591 break; 585 592 … … 1297 1304 1298 1305 return $user; 1306 } 1307 1308 /** 1309 * Get last activity data for a user or set of users. 1310 * 1311 * @param int|array User IDs or multiple user IDs. 1312 * @return array 1313 */ 1314 public static function get_last_activity( $user_id ) { 1315 global $wpdb; 1316 1317 if ( is_array( $user_id ) ) { 1318 $user_ids = wp_parse_id_list( $user_id ); 1319 } else { 1320 $user_ids = array( absint( $user_id ) ); 1321 } 1322 1323 if ( empty( $user_ids ) ) { 1324 return false; 1325 } 1326 1327 $bp = buddypress(); 1328 1329 $user_ids_sql = implode( ',', $user_ids ); 1330 $user_count = count( $user_ids ); 1331 1332 $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 ) ); 1333 1334 // Re-key 1335 $retval = array(); 1336 foreach ( $last_activities as $last_activity ) { 1337 $retval[ $last_activity->user_id ] = array( 1338 'user_id' => $last_activity->user_id, 1339 'date_recorded' => $last_activity->date_recorded, 1340 'activity_id' => $last_activity->id, 1341 ); 1342 } 1343 1344 return $retval; 1345 } 1346 1347 /** 1348 * Set a user's last_activity value. 1349 * 1350 * Will create a new entry if it does not exist. Otherwise updates the 1351 * existing entry. 1352 * 1353 * @since 2.0 1354 * 1355 * @param int $user_id ID of the user whose last_activity you are updating. 1356 * @param string $time MySQL-formatted time string. 1357 * @return bool True on success, false on failure. 1358 */ 1359 public static function update_last_activity( $user_id, $time ) { 1360 global $wpdb; 1361 1362 $table_name = buddypress()->members->table_name_last_activity; 1363 1364 $existing = self::get_last_activity( $user_id ); 1365 1366 if ( ! empty( $existing ) ) { 1367 $data = array( 1368 'date_recorded' => $time, 1369 ); 1370 1371 $data_format = array( 1372 '%s', 1373 ); 1374 1375 $where = array( 1376 ); 1377 1378 $where_format = array( 1379 '%d', 1380 ); 1381 1382 $updated = $wpdb->update( 1383 $table_name, 1384 1385 // Data to update 1386 array( 1387 'date_recorded' => $time, 1388 ), 1389 1390 // WHERE 1391 array( 1392 'id' => $existing[ $user_id ]['activity_id'], 1393 ), 1394 1395 // Data sanitization format 1396 array( 1397 '%s', 1398 ), 1399 1400 // WHERE sanitization format 1401 array( 1402 '%d', 1403 ) 1404 ); 1405 } else { 1406 $updated = $wpdb->insert( 1407 $table_name, 1408 1409 // Data 1410 array( 1411 'user_id' => $user_id, 1412 'component' => buddypress()->members->id, 1413 'type' => 'last_activity', 1414 'date_recorded' => $time, 1415 ), 1416 1417 // Data sanitization format 1418 array( 1419 '%d', 1420 '%s', 1421 '%s', 1422 '%s', 1423 ) 1424 ); 1425 } 1426 1427 return $updated; 1428 } 1429 1430 /** 1431 * Delete a user's last_activity value. 1432 * 1433 * @since 2.0 1434 * 1435 * @param int $user_id 1436 * @return bool True on success, false on failure or if no last_activity 1437 * is found for the user. 1438 */ 1439 public static function delete_last_activity( $user_id ) { 1440 global $wpdb; 1441 1442 $existing = self::get_last_activity( $user_id ); 1443 1444 if ( empty( $existing ) ) { 1445 return false; 1446 } 1447 1448 $deleted = $wpdb->delete( 1449 buddypress()->members->table_name_last_activity, 1450 1451 // WHERE 1452 array( 1453 'id' => $existing[ $user_id ]['activity_id'], 1454 ), 1455 1456 // WHERE sanitization format 1457 array( 1458 '%s', 1459 ) 1460 ); 1461 1462 return $deleted; 1299 1463 } 1300 1464 } -
trunk/bp-core/bp-core-functions.php
r7809 r7860 1054 1054 * Plugin authors should use BP's _user_meta() functions, which bakes in 1055 1055 * bp_get_user_meta_key(): 1056 * $ last_active = bp_get_user_meta( $user_id, 'last_activity', true );1056 * $friend_count = bp_get_user_meta( $user_id, 'total_friend_count', true ); 1057 1057 * If you must use WP's _user_meta() functions directly for some reason, you 1058 1058 * should use this function to determine the $key parameter, eg 1059 * $ last_active = get_user_meta( $user_id, bp_get_user_meta_key( 'last_activity' ), true );1059 * $friend_count = get_user_meta( $user_id, bp_get_user_meta_key( 'total_friend_count' ), true ); 1060 1060 * If using the WP functions, do not not hardcode your meta keys. 1061 1061 * -
trunk/bp-core/bp-core-update.php
r7798 r7860 230 230 if ( $raw_db_version < 7731 ) { 231 231 bp_update_to_1_9_2(); 232 } 233 234 // 2.0 235 if ( $raw_db_version < 7859 ) { 236 bp_update_to_2_0(); 232 237 } 233 238 } … … 329 334 330 335 /** 336 * 2.0 update routine. 337 * 338 * - Ensure that the activity tables are installed, for last_activity storage. 339 * - Migrate last_activity data from usermeta to activity table 340 */ 341 function bp_update_to_2_0() { 342 global $wpdb; 343 344 // Install activity tables 345 bp_core_install_activity_streams(); 346 347 $bp = buddypress(); 348 349 // Migrate data 350 // The "NOT IN" clause prevents duplicates 351 $sql = "INSERT INTO {$bp->members->table_name_last_activity} (`user_id`, `component`, `type`, `date_recorded` ) ( 352 SELECT user_id, '{$bp->members->id}' as component, 'last_activity' as type, meta_value AS date_recorded 353 FROM {$wpdb->usermeta} 354 WHERE 355 meta_key = 'last_activity' 356 AND 357 user_id NOT IN ( 358 SELECT user_id 359 FROM {$bp->members->table_name_last_activity} 360 WHERE component = '{$bp->members->id}' AND type = 'last_activity' 361 ) 362 );"; 363 364 $wpdb->query( $sql ); 365 } 366 367 /** 331 368 * Redirect user to BP's What's New page on first page load after activation. 332 369 * -
trunk/bp-friends/bp-friends-classes.php
r7556 r7860 365 365 global $wpdb; 366 366 367 $user_ids = implode( ',', wp_parse_id_list( $user_ids ) ); 368 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' ) ) ); 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 } 386 387 return $retval; 370 388 } 371 389 -
trunk/bp-loader.php
r7854 r7860 305 305 306 306 $this->version = '2.0-alpha-7752'; 307 $this->db_version = 7 731;307 $this->db_version = 7859; 308 308 309 309 /** Loading ***************************************************/ -
trunk/bp-members/bp-members-functions.php
r7764 r7860 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() ) { … … 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 ); … … 818 820 819 821 /** 820 * Update a user's last activity 821 * 822 * @since BuddyPress (1.9) 823 * @param int $user_id ID of the user being updated 824 * @param string $time Time of last activity, in 'Y-m-d H:i:s' format 825 * @return bool True on success 822 * Update a user's last activity. 823 * 824 * @since BuddyPress (1.9.0) 825 * 826 * @param int $user_id ID of the user being updated. 827 * @param string $time Time of last activity, in 'Y-m-d H:i:s' format. 828 * @return bool True on success, false on failure. 826 829 */ 827 830 function bp_update_user_last_activity( $user_id = 0, $time = '' ) { … … 841 844 } 842 845 843 return bp_update_user_meta( $user_id, 'last_activity', $time ); 844 } 845 846 /** 847 * Get the last activity for a given user 848 * 849 * @param int $user_id The ID of the user 846 // As of BuddyPress 2.0, last_activity is no longer stored in usermeta. 847 // However, we mirror it there for backward compatibility. Do not use! 848 // Remove our warning and re-add. 849 remove_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 ); 850 update_user_meta( $user_id, 'last_activity', $time ); 851 add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 ); 852 853 return BP_Core_User::update_last_activity( $user_id, $time ); 854 } 855 856 /** 857 * Backward compatibility for 'last_activity' usermeta fetching. 858 * 859 * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For 860 * backward compatibility, we continue to mirror the data there. This function 861 * serves two purposes: it warns plugin authors of the change, and it returns 862 * the data from the proper location. 863 * 864 * @since BuddyPress (2.0.0) 865 * 866 * @access private For internal use only. 867 * 868 * @param null $retval 869 * @param int $object_id ID of the user. 870 * @param string $meta_key Meta key being fetched. 871 */ 872 function _bp_get_user_meta_last_activity_warning( $retval, $object_id, $meta_key ) { 873 static $warned; 874 875 if ( 'last_activity' === $meta_key ) { 876 // Don't send the warning more than once per pageload 877 if ( empty( $warned ) ) { 878 _doing_it_wrong( 'get_user_meta( $user_id, \'last_activity\' )', __( 'User last_activity data is no longer stored in usermeta. Use bp_get_user_last_activity() instead.', 'buddypress' ), '2.0.0' ); 879 $warned = 1; 880 } 881 882 return bp_get_user_last_activity( $object_id ); 883 } 884 885 return $retval; 886 } 887 add_filter( 'get_user_metadata', '_bp_get_user_meta_last_activity_warning', 10, 3 ); 888 889 /** 890 * Backward compatibility for 'last_activity' usermeta setting. 891 * 892 * In BuddyPress 2.0, user last_activity data was moved out of usermeta. For 893 * backward compatibility, we continue to mirror the data there. This function 894 * serves two purposes: it warns plugin authors of the change, and it updates 895 * the data in the proper location. 896 * 897 * @since BuddyPress (2.0.0) 898 * 899 * @access private For internal use only. 900 * 901 * @param int $meta_id ID of the just-set usermeta row. 902 * @param int $object_id ID of the user. 903 * @param string $meta_key Meta key being fetched. 904 * @param string $meta_value Active time. 905 */ 906 function _bp_update_user_meta_last_activity_warning( $meta_id, $object_id, $meta_key, $meta_value ) { 907 if ( 'last_activity' === $meta_key ) { 908 _doing_it_wrong( 'update_user_meta( $user_id, \'last_activity\' )', __( 'User last_activity data is no longer stored in usermeta. Use bp_update_user_last_activity() instead.', 'buddypress' ), '2.0.0' ); 909 bp_update_user_last_activity( $object_id, $meta_value ); 910 } 911 } 912 add_filter( 'update_user_metadata', '_bp_update_user_meta_last_activity_warning', 10, 4 ); 913 914 /** 915 * Get the last activity for a given user. 916 * 917 * @param int $user_id The ID of the user. 850 918 * @return string Time of last activity, in 'Y-m-d H:i:s' format, or an empty 851 * string if none is found919 * string if none is found. 852 920 */ 853 921 function bp_get_user_last_activity( $user_id = 0 ) { 854 // Fall back on current user855 if ( empty( $user_id ) ) { 856 $user_id = bp_loggedin_user_id();857 }858 859 $activity = bp_get_user_meta( $user_id, 'last_activity', true );922 $activity = ''; 923 924 $last_activity = BP_Core_User::get_last_activity( $user_id ); 925 if ( ! empty( $last_activity[ $user_id ] ) ) { 926 $activity = $last_activity[ $user_id ]['date_recorded']; 927 } 860 928 861 929 return apply_filters( 'bp_get_user_last_activity', $activity, $user_id ); … … 997 1065 function bp_core_remove_data( $user_id ) { 998 1066 999 // Remove usermeta1000 bp_delete_user_meta( $user_id, 'last_activity');1067 // Remove last_activity data 1068 BP_Core_User::delete_last_activity( $user_id ); 1001 1069 1002 1070 // Flush the cache to remove the user from all cached objects -
trunk/bp-members/bp-members-loader.php
r7764 r7860 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 ) ); -
trunk/tests/includes/testcase.php
r7818 r7860 219 219 $r = wp_parse_args( $args, array( 220 220 'role' => 'subscriber', 221 'last_activity' => bp_core_current_time() - 60*60*24*365,221 'last_activity' => date( 'Y-m-d h:i:s', strtotime( bp_core_current_time() ) - 60*60*24*365 ), 222 222 ) ); 223 223 -
trunk/tests/testcases/core/class-bp-core-user.php
r7425 r7860 3 3 /** 4 4 * @group core 5 * @group BP_Core_User 5 6 */ 6 7 class BP_Tests_BP_Core_User_TestCases extends BP_UnitTestCase { … … 120 121 } 121 122 123 /** 124 * @group last_activity 125 */ 126 public function test_get_last_activity() { 127 $u = $this->create_user(); 128 $time = bp_core_current_time(); 122 129 130 BP_Core_User::update_last_activity( $u, $time ); 123 131 132 $a = BP_Core_User::get_last_activity( $u ); 133 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 134 135 $this->assertEquals( $time, $found ); 136 } 137 138 /** 139 * @group last_activity 140 */ 141 public function test_update_last_activity() { 142 $u = $this->create_user(); 143 $time = bp_core_current_time(); 144 $time2 = '1968-12-25 01:23:45'; 145 146 BP_Core_User::update_last_activity( $u, $time ); 147 $a = BP_Core_User::get_last_activity( $u ); 148 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 149 $this->assertEquals( $time, $found ); 150 151 BP_Core_User::update_last_activity( $u, $time2 ); 152 $a = BP_Core_User::get_last_activity( $u ); 153 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 154 $this->assertEquals( $time2, $found ); 155 } 156 157 /** 158 * @group last_activity 159 */ 160 public function test_delete_last_activity() { 161 $u = $this->create_user(); 162 $time = bp_core_current_time(); 163 164 BP_Core_User::update_last_activity( $u, $time ); 165 $a = BP_Core_User::get_last_activity( $u ); 166 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 167 $this->assertEquals( $time, $found ); 168 169 BP_Core_User::delete_last_activity( $u ); 170 $a = BP_Core_User::get_last_activity( $u ); 171 $found = isset( $a[ $u ]['date_recorded'] ) ? $a[ $u ]['date_recorded'] : ''; 172 $this->assertEquals( '', $found ); 173 } 124 174 }
Note: See TracChangeset
for help on using the changeset viewer.