Ticket #3794: 3794.03.patch
File 3794.03.patch, 18.3 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..741225d 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;472 // Save the favorite 473 if ( bp_activity_add_favorite( $activity_id, $user_id ) ) { 465 474 466 // Update the total number of users who have favorited this activity467 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );468 $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1;475 // Update the total number of users who have favorited this activity 476 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 477 $fav_count = ! empty( $fav_count ) ? (int) $fav_count + 1 : 1; 469 478 470 // Update user meta471 bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs);479 // Update activity meta counts for backcompat 480 bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ); 472 481 473 // Update activity meta counts 474 if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) { 482 wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' ); 475 483 476 484 // Execute additional code 477 485 do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id ); … … function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 494 502 * @since BuddyPress (1.2.0) 495 503 * 496 504 * @uses is_user_logged_in() 497 * @uses bp_get_user_meta() 505 * @uses bp_loggedin_user_id() to default to current user logged in 506 * @uses bp_activity_delete_by_item_id() to delete the favorite 498 507 * @uses bp_activity_get_meta() 499 508 * @uses bp_activity_update_meta() 500 * @uses bp_update_user_meta()501 509 * @uses do_action() To call the 'bp_activity_remove_user_favorite' hook. 502 510 * 503 511 * @param int $activity_id ID of the activity item being unfavorited. … … function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) { 516 524 $user_id = bp_loggedin_user_id(); 517 525 } 518 526 519 $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 520 $my_favs = array_flip( (array) $my_favs ); 527 // the favorite is an activity, so let's delete it 528 $removed = bp_activity_delete_by_item_id( array( 529 'item_id' => $activity_id, 530 'type' => 'activity_favorite', 531 'user_id' => $user_id, 532 ) ); 521 533 522 534 // Bail if the user has not previously favorited the item 523 if ( ! isset( $my_favs[ $activity_id ]) ) {535 if ( empty( $removed ) ) { 524 536 return false; 525 537 } 526 538 527 // Remove the fav from the user's favs 528 unset( $my_favs[$activity_id] ); 529 $my_favs = array_unique( array_flip( $my_favs ) ); 539 // Clean the user's favs cache 540 wp_cache_delete( 'bp_activity_user_' . $user_id . '_favorites', 'bp' ); 530 541 531 // Update the total number of users who have favorited this activity532 542 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 533 if ( ! empty( $fav_count ) ) {534 543 535 // Deduct from total favorites 536 if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) { 544 // Error getting favorite count 545 if ( empty( $fav_count ) ) { 546 return false; 547 } 548 549 // Deduct from total favorites 550 $count_updated = bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ); 537 551 538 // Update users favorites 539 if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) { 552 // Error updating favorite count 553 if ( empty( $count_updated ) ) { 554 return false; 555 } 540 556 541 542 557 // Execute additional code 558 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id ); 543 559 544 // Success545 return true; 560 return true; 561 } 546 562 547 // Error updating 548 } else { 549 return false; 550 } 563 /** 564 * Add an activity typed 'activity_favorite' for the favorited activity 565 * 566 * @since BuddyPress (2.2.0) 567 * @uses BP_Activity_Activity 568 * @uses bp_activity_get_permalink() 569 * @uses bp_core_get_userlink() 570 * @uses bp_activity_add() 571 * @return int|bool the activity id created or false if it fails 572 */ 573 function bp_activity_add_favorite( $activity_id = 0, $user_id = 0 ) { 574 if ( empty( $activity_id ) || empty( $user_id ) ) { 575 return false; 576 } 551 577 552 // Error updating favorite count 553 } else { 554 return false; 578 $bp = buddypress(); 579 580 // Parent activity exists ? 581 $activity = new BP_Activity_Activity( $activity_id ); 582 583 if ( empty( $activity->type ) || 'activity_favorite' == $activity->type ) { 584 return false; 585 } 586 587 $primary_link = bp_activity_get_permalink( $activity_id, $activity ); 588 $action = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $user_id ) ); 589 590 $args = array( 591 'user_id' => $user_id, 592 'component' => $bp->activity->id, 593 'type' => 'activity_favorite', 594 'action' => $action, 595 'content' => '', 596 'primary_link' => $primary_link, 597 'item_id' => $activity_id, 598 'secondary_item_id' => $activity->user_id, 599 'hide_sitewide' => $activity->hide_sitewide, 600 ); 601 602 return bp_activity_add( $args ); 603 } 604 605 /** 606 * Migrate user's favorite (previously in bp_favorite_activities user meta) to activity table 607 * 608 * @since BuddyPress (2.2.0) 609 * @uses bp_activity_add_favorite() to create the 'activity_favorite' activities 610 */ 611 function bp_activity_favorites_migrate() { 612 global $wpdb; 613 614 $user_favorites = $wpdb->get_results( "SELECT user_id, meta_value FROM {$wpdb->usermeta} WHERE meta_key = 'bp_favorite_activities'" ); 615 616 foreach( $user_favorites as $meta ) { 617 $meta_value = maybe_unserialize( $meta->meta_value ); 618 619 if ( empty( $meta_value ) ) { 620 continue; 555 621 } 556 622 557 // Error getting favorite count558 } else {559 return false;623 foreach( $meta_value as $favorite ) { 624 bp_activity_add_favorite( $favorite, $meta->user_id ); 625 } 560 626 } 561 627 } 562 628 … … function bp_activity_register_activity_actions() { 913 979 __( 'Activity Comments', 'buddypress' ) 914 980 ); 915 981 982 bp_activity_set_action( 983 $bp->activity->id, 984 'activity_favorite', 985 __( 'Favorited an update', 'buddypress' ), 986 'bp_activity_format_activity_action_activity_favorites' 987 ); 988 916 989 do_action( 'bp_activity_register_activity_actions' ); 917 990 918 991 // Backpat. Don't use this. … … function bp_activity_format_activity_action_activity_comment( $action, $activity 979 1052 return apply_filters( 'bp_activity_comment_action', $action, $activity ); 980 1053 } 981 1054 1055 /** 1056 * Format 'activity_favorite' activity actions. 1057 * 1058 * @since BuddyPress (2.2.0) 1059 * 1060 * @param string $action Static activity action. 1061 * @param object $activity Activity data object. 1062 * @return string 1063 */ 1064 function bp_activity_format_activity_action_activity_favorites( $action, $activity ) { 1065 $action = sprintf( _x( '%s favorited an update', 'favorites action string', 'buddypress' ), bp_core_get_userlink( $activity->user_id ) ); 1066 return apply_filters( 'bp_activity_favorite_action', $action, $activity ); 1067 } 1068 982 1069 /****************************************************************************** 983 1070 * Business functions are where all the magic happens in BuddyPress. They will 984 1071 * handle the actual saving or manipulation of information. Usually they will … … function bp_activity_delete_comment( $activity_id, $comment_id ) { 1661 1748 bp_activity_delete_children( $activity_id, $child->id ); 1662 1749 } 1663 1750 } 1664 1751 1665 1752 // Delete the comment itself 1666 1753 bp_activity_delete( array( 1667 1754 'secondary_item_id' => $comment_id, … … function bp_activity_get_permalink( $activity_id, $activity_obj = false ) { 1698 1785 $activity_obj = $activity_obj->current_comment; 1699 1786 } 1700 1787 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) {1788 if ( in_array( $activity_obj->type, array( 'new_blog_post', 'new_blog_comment', 'new_forum_topic', 'new_forum_post', 'activity_favorite' ) ) ) { 1702 1789 $link = $activity_obj->primary_link; 1703 1790 } else { 1704 1791 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