Ticket #3794: 3794.02.patch
File 3794.02.patch, 18.1 KB (added by , 10 years ago) |
---|
-
src/bp-activity/bp-activity-admin.php
diff --git src/bp-activity/bp-activity-admin.php src/bp-activity/bp-activity-admin.php index 5547557..8fcae95 100644
class BP_Activity_List_Table extends WP_List_Table { 1535 1535 1536 1536 if ( $this->disable_blogforum_comments ) { 1537 1537 switch ( $item['type'] ) { 1538 case 'new_blog_post' : 1539 case 'new_blog_comment' : 1540 case 'new_forum_topic' : 1541 case 'new_forum_post' : 1538 case 'new_blog_post' : 1539 case 'new_blog_comment' : 1540 case 'new_forum_topic' : 1541 case 'new_forum_post' : 1542 case 'activity_favorite' : 1542 1543 $can_comment = false; 1543 1544 break; 1544 1545 } … … class BP_Activity_List_Table extends WP_List_Table { 1573 1574 } 1574 1575 } 1575 1576 1576 return apply_filters( 'bp_activity_list_table_can_comment', $can_comment );1577 return apply_filters( 'bp_activity_list_table_can_comment', $can_comment, $item['type'] ); 1577 1578 } 1578 1579 1579 1580 /** -
src/bp-activity/bp-activity-classes.php
diff --git src/bp-activity/bp-activity-classes.php src/bp-activity/bp-activity-classes.php index ad6f9f0..51f0290 100644
class BP_Activity_Activity { 403 403 $excluded_types[] = 'last_activity'; 404 404 } 405 405 406 if ( empty( $filter['action'] ) || 'activity_favorite' != $filter['action'] ) { 407 $excluded_types[] = 'activity_favorite'; 408 } 409 406 410 // Exclude 'new_member' items if xprofile component is not active 407 411 if ( ! bp_is_active( 'xprofile' ) ) { 408 412 $excluded_types[] = 'new_member'; … … class BP_Activity_Activity { 890 894 else 891 895 return false; 892 896 893 // Fetch the activity IDs so we can delete any c ommentsfor this activity item897 // Fetch the activity IDs so we can delete any children for this activity item 894 898 $activity_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$where_sql}" ); 895 899 896 900 if ( ! $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$where_sql}" ) ) { 897 901 return false; 898 902 } 899 903 900 // Handle accompanying activity c ommentsand meta deletion904 // Handle accompanying activity children and meta deletion 901 905 if ( $activity_ids ) { 902 906 $activity_ids_comma = implode( ',', wp_parse_id_list( $activity_ids ) ); 903 $activity_c omments_where_sql = "WHERE type = 'activity_comment'AND item_id IN ({$activity_ids_comma})";907 $activity_children_where_sql = "WHERE type IN ( 'activity_comment', 'activity_favorite' ) AND item_id IN ({$activity_ids_comma})"; 904 908 905 // Fetch the activity c ommentIDs for our deleted activity items906 $activity_c omment_ids = $wpdb->get_col( "SELECT id FROM {$bp->activity->table_name} {$activity_comments_where_sql}" );909 // Fetch the activity child IDs for our deleted activity items 910 $activity_children = $wpdb->get_results( "SELECT id, user_id, type FROM {$bp->activity->table_name} {$activity_children_where_sql}" ); 907 911 908 // We have activity comments! 909 if ( ! empty( $activity_comment_ids ) ) { 910 // Delete activity comments 911 $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_comments_where_sql}" ); 912 // We have activity children! 913 if ( ! empty( $activity_children ) ) { 914 $activity_child_ids = array(); 915 916 // Delete activity children 917 $wpdb->query( "DELETE FROM {$bp->activity->table_name} {$activity_children_where_sql}" ); 918 919 foreach ( $activity_children as $child ) { 920 $activity_child_ids[] = $child->id; 921 922 if ( 'activity_favorite' == $child->type ) { 923 wp_cache_delete( 'bp_activity_user_' . $child->user_id . '_favorites', 'bp' ); 924 } 925 } 912 926 913 // Merge activity IDs with activity c ommentIDs914 $activity_ids = array_merge( $activity_ids, $activity_c omment_ids );927 // Merge activity IDs with activity child IDs 928 $activity_ids = array_merge( $activity_ids, $activity_child_ids ); 915 929 } 916 930 917 931 // Delete all activity meta entries for activity items and activity comments … … class BP_Activity_Activity { 1362 1376 * @return int A count of the user's favorites. 1363 1377 */ 1364 1378 public static function total_favorite_count( $user_id ) { 1365 if ( !$favorite_activity_entries = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ) ) 1379 $favorite_activity_entries = bp_activity_get_user_favorites( $user_id ); 1380 1381 if ( empty( $favorite_activity_entries ) ) { 1366 1382 return 0; 1383 } 1367 1384 1368 return count( maybe_unserialize( $favorite_activity_entries ));1385 return count( $favorite_activity_entries ); 1369 1386 } 1370 1387 1371 1388 /** 1389 * Get user_ids who favorited activities. 1390 * 1391 * @since BuddyPress (2.2.0) 1392 * 1393 * @param int $user_id 1394 * @param int $activity_id 1395 * @return array $favorites The favorited activities. 1396 */ 1397 public static function get_favorites_by_user_id( $user_id = 0, $activity_id = 0 ) { 1398 global $wpdb; 1399 $bp = buddypress(); 1400 1401 $favorites = array(); 1402 1403 if ( empty( $user_id ) ) { 1404 return $favorites; 1405 } 1406 1407 $where = array( 1408 "type = 'activity_favorite'", 1409 $wpdb->prepare( "user_id = %d", $user_id ) 1410 ); 1411 1412 if ( ! empty( $activity_id ) ) { 1413 $where[] = $wpdb->prepare( "item_id = %d", $activity_id ); 1414 } 1415 1416 $where = 'WHERE ' . implode( ' AND ', $where ); 1417 $favorites = $wpdb->get_col( "SELECT item_id FROM {$bp->activity->table_name} {$where}" ); 1418 1419 return array_map( 'intval', $favorites ); 1420 } 1421 1422 /** 1372 1423 * Check whether an activity item exists with a given string content. 1373 1424 * 1374 1425 * @param string $content The content to filter by. -
src/bp-activity/bp-activity-functions.php
diff --git src/bp-activity/bp-activity-functions.php src/bp-activity/bp-activity-functions.php index 94b5de1..ab3fd4b 100644
function bp_activity_get_types() { 385 385 $action = array_values( (array) $action ); 386 386 387 387 for ( $i = 0, $i_count = count( $action ); $i < $i_count; $i++ ) { 388 // Remove favorite type 389 if ( 'activity_favorite' == $action[$i]['key'] ) { 390 continue; 391 } 392 388 393 $actions[ $action[$i]['key'] ] = $action[$i]['value']; 389 394 } 390 395 } … … function bp_activity_get_types() { 402 407 * 403 408 * @since BuddyPress (1.2.0) 404 409 * 405 * @uses bp_get_user_meta()410 * @uses BP_Activity_Activity::get_favorites_by_user_id() to get user's favorites 406 411 * @uses apply_filters() To call the 'bp_activity_get_user_favorites' hook. 407 412 * 408 413 * @param int $user_id ID of the user whose favorites are being queried. … … function bp_activity_get_user_favorites( $user_id = 0 ) { 416 421 } 417 422 418 423 // Get favorites for user 419 $favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 424 $favs = wp_cache_get( 'bp_activity_user_' . $user_id . '_favorites', 'bp' ); 425 426 if ( empty( $favs ) ) { 427 $favs = BP_Activity_Activity::get_favorites_by_user_id( $user_id ); 428 wp_cache_set( 'bp_activity_user_' . $user_id . '_favorites', $favs, 'bp' ); 429 } 420 430 421 431 return apply_filters( 'bp_activity_get_user_favorites', $favs ); 422 432 } … … function bp_activity_get_user_favorites( $user_id = 0 ) { 427 437 * @since BuddyPress (1.2.0) 428 438 * 429 439 * @uses is_user_logged_in() 430 * @uses bp_get_user_meta() 440 * @uses bp_loggedin_user_id() to default to current user logged in 441 * @uses BP_Activity_Activity::get_favorites_by_user_id() to check if the activity to favorite is not already favorited 431 442 * @uses bp_activity_get_meta() 432 * @uses bp_update_user_meta()433 443 * @uses bp_activity_update_meta() 444 * @uses bp_activity_add_favorite() to create the 'activity_favorite' activity 434 445 * @uses do_action() To call the 'bp_activity_add_user_favorite' hook. 435 446 * @uses do_action() To call the 'bp_activity_add_user_favorite_fail' hook. 436 447 * … … function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 450 461 $user_id = bp_loggedin_user_id(); 451 462 } 452 463 453 $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 454 if ( empty( $my_favs ) || ! is_array( $my_favs ) ) { 455 $my_favs = array(); 456 } 464 // Check if this activity item is already in user's favorites 465 $is_fav = BP_Activity_Activity::get_favorites_by_user_id( $user_id, $activity_id ); 457 466 458 467 // Bail if the user has already favorited this activity item 459 if ( in_array( $activity_id, $my_favs) ) {468 if ( ! empty( $is_fav ) ) { 460 469 return false; 461 470 } 462 471 463 // Add to user's favorites464 $my_favs[] = $activity_id;465 466 472 // Update the total number of users who have favorited this activity 467 473 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 468 $fav_count = ! empty( $fav_count ) ? (int) $fav_count + 1 : 1;474 $fav_count = ! empty( $fav_count ) ? (int) $fav_count + 1 : 1; 469 475 470 // Update user meta471 bp_ update_user_meta( $user_id, 'bp_favorite_activities', $my_favs);476 // Update activity meta counts for backcompat 477 bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ); 472 478 473 // Update activity meta counts 474 if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) { 479 // Save the favorite 480 if ( bp_activity_add_favorite( $activity_id, $user_id ) ) { 481 wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' ); 475 482 476 483 // Execute additional code 477 484 do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id ); … … function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 494 501 * @since BuddyPress (1.2.0) 495 502 * 496 503 * @uses is_user_logged_in() 497 * @uses bp_get_user_meta() 504 * @uses bp_loggedin_user_id() to default to current user logged in 505 * @uses bp_activity_delete_by_item_id() to delete the favorite 498 506 * @uses bp_activity_get_meta() 499 507 * @uses bp_activity_update_meta() 500 * @uses bp_update_user_meta()501 508 * @uses do_action() To call the 'bp_activity_remove_user_favorite' hook. 502 509 * 503 510 * @param int $activity_id ID of the activity item being unfavorited. … … function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) { 516 523 $user_id = bp_loggedin_user_id(); 517 524 } 518 525 519 $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 520 $my_favs = array_flip( (array) $my_favs ); 526 // the favorite is an activity, so let's delete it 527 $removed = bp_activity_delete_by_item_id( array( 528 'item_id' => $activity_id, 529 'type' => 'activity_favorite', 530 'user_id' => $user_id, 531 ) ); 521 532 522 533 // Bail if the user has not previously favorited the item 523 if ( ! isset( $my_favs[ $activity_id ]) ) {534 if ( empty( $removed ) ) { 524 535 return false; 525 536 } 526 537 527 // Remove the fav from the user's favs 528 unset( $my_favs[$activity_id] ); 529 $my_favs = array_unique( array_flip( $my_favs ) ); 538 // Clean the user's favs cache 539 wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' ); 530 540 531 // Update the total number of users who have favorited this activity532 541 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 533 if ( ! empty( $fav_count ) ) {534 542 535 // Deduct from total favorites 536 if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) { 543 // Error getting favorite count 544 if ( empty( $fav_count ) ) { 545 return false; 546 } 537 547 538 // Update usersfavorites539 if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {548 // Deduct from total favorites 549 $count_updated = bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ); 540 550 541 // Execute additional code 542 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id ); 551 // Error updating favorite count 552 if ( empty( $count_updated ) ) { 553 return false; 554 } 543 555 544 // Success 545 return true;556 // Execute additional code 557 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id ); 546 558 547 // Error updating 548 } else { 549 return false; 550 } 559 return true; 560 } 551 561 552 // Error updating favorite count 553 } else { 554 return false; 562 /** 563 * Add an activity typed 'activity_favorite' for the favorited activity 564 * 565 * @since BuddyPress (2.2.0) 566 * @uses BP_Activity_Activity 567 * @uses bp_activity_get_permalink() 568 * @uses bp_core_get_userlink() 569 * @uses bp_activity_add() 570 * @return int|bool the activity id created or false if it fails 571 */ 572 function bp_activity_add_favorite( $activity_id = 0, $user_id = 0 ) { 573 if ( empty( $activity_id ) || empty( $user_id ) ) { 574 return false; 575 } 576 577 $bp = buddypress(); 578 579 // Parent activity exists ? 580 $activity = new BP_Activity_Activity( $activity_id ); 581 582 if ( empty( $activity->type ) || 'activity_favorite' == $activity->type ) { 583 return false; 584 } 585 586 $primary_link = bp_activity_get_permalink( $activity_id, $activity ); 587 $action = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $user_id ) ); 588 589 $args = array( 590 'user_id' => $user_id, 591 'component' => $bp->activity->id, 592 'type' => 'activity_favorite', 593 'action' => $action, 594 'content' => '', 595 'primary_link' => $primary_link, 596 'item_id' => $activity_id, 597 'secondary_item_id' => $activity->user_id, 598 ); 599 600 return bp_activity_add( $args ); 601 } 602 603 /** 604 * Migrate user's favorite (previously in bp_favorite_activities user meta) to activity table 605 * 606 * @since BuddyPress (2.2.0) 607 * @uses bp_activity_add_favorite() to create the 'activity_favorite' activities 608 */ 609 function bp_activity_favorites_migrate() { 610 global $wpdb; 611 612 $user_favorites = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'bp_favorite_activities'" ); 613 614 foreach( $user_favorites as $meta ) { 615 $meta_value = maybe_unserialize( $meta->meta_value ); 616 617 if ( empty( $meta_value ) ) { 618 continue; 555 619 } 556 620 557 // Error getting favorite count558 } else {559 return false;621 foreach( $meta_value as $favorite ) { 622 bp_activity_add_favorite( $favorite, $meta->user_id ); 623 } 560 624 } 561 625 } 562 626 … … function bp_activity_register_activity_actions() { 913 977 __( 'Activity Comments', 'buddypress' ) 914 978 ); 915 979 980 bp_activity_set_action( 981 $bp->activity->id, 982 'activity_favorite', 983 __( 'Favorited an update', 'buddypress' ), 984 'bp_activity_format_activity_action_activity_favorites' 985 ); 986 916 987 do_action( 'bp_activity_register_activity_actions' ); 917 988 918 989 // Backpat. Don't use this. … … function bp_activity_format_activity_action_activity_comment( $action, $activity 979 1050 return apply_filters( 'bp_activity_comment_action', $action, $activity ); 980 1051 } 981 1052 1053 /** 1054 * Format 'activity_favorite' activity actions. 1055 * 1056 * @since BuddyPress (2.2.0) 1057 * 1058 * @param string $action Static activity action. 1059 * @param object $activity Activity data object. 1060 * @return string 1061 */ 1062 function bp_activity_format_activity_action_activity_favorites( $action, $activity ) { 1063 $action = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) ); 1064 return apply_filters( 'bp_activity_favorite_action', $action, $activity ); 1065 } 1066 982 1067 /****************************************************************************** 983 1068 * Business functions are where all the magic happens in BuddyPress. They will 984 1069 * handle the actual saving or manipulation of information. Usually they will … … function bp_activity_delete_comment( $activity_id, $comment_id ) { 1661 1746 bp_activity_delete_children( $activity_id, $child->id ); 1662 1747 } 1663 1748 } 1664 1749 1665 1750 // Delete the comment itself 1666 1751 bp_activity_delete( array( 1667 1752 'secondary_item_id' => $comment_id, … … function bp_activity_get_permalink( $activity_id, $activity_obj = false ) { 1698 1783 $activity_obj = $activity_obj->current_comment; 1699 1784 } 1700 1785 1701 if ( 'new_blog_post' == $activity_obj->type || 'new_blog_comment' == $activity_obj->type || 'new_forum_topic' == $activity_obj->type || 'new_forum_post' == $activity_obj->type) {1786 if ( in_array( $activity_obj->type, array( 'new_blog_post', 'new_blog_comment', 'new_forum_topic', 'new_forum_post', 'activity_favorite' ) ) ) { 1702 1787 $link = $activity_obj->primary_link; 1703 1788 } else { 1704 1789 if ( 'activity_comment' == $activity_obj->type ) { -
src/bp-activity/bp-activity-template.php
diff --git src/bp-activity/bp-activity-template.php src/bp-activity/bp-activity-template.php index d65eac3..7e40afc 100644
class BP_Activity_Template { 195 195 $this->disable_blogforum_replies = isset( $bp->site_options['bp-disable-blogforum-comments'] ) ? $bp->site_options['bp-disable-blogforum-comments'] : false; 196 196 197 197 // Get an array of the logged in user's favorite activities 198 $this->my_favs = maybe_unserialize( bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true) );198 $this->my_favs = bp_activity_get_user_favorites( bp_loggedin_user_id() ); 199 199 200 200 // Fetch specific activity items based on ID's 201 201 if ( !empty( $include ) ) { … … function bp_activity_can_comment_reply( $comment = '' ) { 2865 2865 * @return bool True if comment can receive comments. 2866 2866 */ 2867 2867 function bp_activity_can_favorite() { 2868 global $activities_template; 2869 2870 $can_favorite = true; 2871 2872 // Don't favorite a favorite 2873 if ( ! empty( $activities_template->activity->type ) && 'activity_favorite' == $activities_template->activity->type ) { 2874 $can_favorite = false; 2875 } 2876 2868 2877 return apply_filters( 'bp_activity_can_favorite', true ); 2869 2878 } 2870 2879 -
src/bp-core/bp-core-update.php
diff --git src/bp-core/bp-core-update.php src/bp-core/bp-core-update.php index 6b27154..4641747 100644
function bp_version_updater() { 241 241 if ( $raw_db_version < 8311 ) { 242 242 bp_update_to_2_0_1(); 243 243 } 244 245 // 2.2 246 if ( $raw_db_version < 8312 ) { 247 bp_update_to_2_2(); 248 } 244 249 } 245 250 246 251 /** All done! *************************************************************/ … … function bp_update_to_2_0_1() { 390 395 } 391 396 392 397 /** 398 * 2.2 database upgrade routine 399 * 400 * @since BuddyPress (2.2.0) 401 * 402 * @return void 403 */ 404 function bp_update_to_2_2() { 405 406 /** Migrate favorites data ****************************************************/ 407 408 if ( bp_is_active( 'activity' ) ) { 409 bp_activity_favorites_migrate(); 410 } 411 } 412 413 /** 393 414 * Redirect user to BP's What's New page on first page load after activation. 394 415 * 395 416 * @since BuddyPress (1.7.0) -
src/bp-loader.php
diff --git src/bp-loader.php src/bp-loader.php index 41c13e0..0838e94 100644
class BuddyPress { 301 301 /** Versions **********************************************************/ 302 302 303 303 $this->version = '2.2-alpha'; 304 $this->db_version = 831 1;304 $this->db_version = 8312; 305 305 306 306 /** Loading ***********************************************************/ 307 307