Ticket #3794: 3794.05.patch
File 3794.05.patch, 36.5 KB (added by , 9 years ago) |
---|
-
src/bp-activity/bp-activity-actions.php
diff --git src/bp-activity/bp-activity-actions.php src/bp-activity/bp-activity-actions.php index 7df351d..4252f49 100644
function bp_activity_action_post_comment() { 382 382 add_action( 'bp_actions', 'bp_activity_action_post_comment' ); 383 383 384 384 /** 385 * Mark activity as favorite.386 *387 * @since 1.2.0388 *389 * @return bool False on failure.390 */391 function bp_activity_action_mark_favorite() {392 393 if ( !is_user_logged_in() || !bp_is_activity_component() || !bp_is_current_action( 'favorite' ) )394 return false;395 396 // Check the nonce.397 check_admin_referer( 'mark_favorite' );398 399 if ( bp_activity_add_user_favorite( bp_action_variable( 0 ) ) )400 bp_core_add_message( __( 'Activity marked as favorite.', 'buddypress' ) );401 else402 bp_core_add_message( __( 'There was an error marking that activity as a favorite. Please try again.', 'buddypress' ), 'error' );403 404 bp_core_redirect( wp_get_referer() . '#activity-' . bp_action_variable( 0 ) );405 }406 add_action( 'bp_actions', 'bp_activity_action_mark_favorite' );407 408 /**409 * Remove activity from favorites.410 *411 * @since 1.2.0412 *413 * @return bool False on failure.414 */415 function bp_activity_action_remove_favorite() {416 417 if ( ! is_user_logged_in() || ! bp_is_activity_component() || ! bp_is_current_action( 'unfavorite' ) )418 return false;419 420 // Check the nonce.421 check_admin_referer( 'unmark_favorite' );422 423 if ( bp_activity_remove_user_favorite( bp_action_variable( 0 ) ) )424 bp_core_add_message( __( 'Activity removed as favorite.', 'buddypress' ) );425 else426 bp_core_add_message( __( 'There was an error removing that activity as a favorite. Please try again.', 'buddypress' ), 'error' );427 428 bp_core_redirect( wp_get_referer() . '#activity-' . bp_action_variable( 0 ) );429 }430 add_action( 'bp_actions', 'bp_activity_action_remove_favorite' );431 432 /**433 385 * Load the sitewide activity feed. 434 386 * 435 387 * @since 1.0.0 … … function bp_activity_action_mentions_feed() { 575 527 add_action( 'bp_actions', 'bp_activity_action_mentions_feed' ); 576 528 577 529 /** 578 * Load a user's favorites feed.579 *580 * @since 1.2.0581 *582 * @return bool False on failure.583 */584 function bp_activity_action_favorites_feed() {585 if ( ! bp_is_user_activity() || ! bp_is_current_action( 'favorites' ) || ! bp_is_action_variable( 'feed', 0 ) ) {586 return false;587 }588 589 // Get displayed user's favorite activity IDs.590 $favs = bp_activity_get_user_favorites( bp_displayed_user_id() );591 $fav_ids = implode( ',', (array) $favs );592 593 // Setup the feed.594 buddypress()->activity->feed = new BP_Activity_Feed( array(595 'id' => 'favorites',596 597 /* translators: User activity favorites RSS title - "[Site Name] | [User Display Name] | Favorites" */598 'title' => sprintf( __( '%1$s | %2$s | Favorites', 'buddypress' ), bp_get_site_name(), bp_get_displayed_user_fullname() ),599 600 'link' => bp_displayed_user_domain() . bp_get_activity_slug() . '/favorites/',601 'description' => sprintf( __( "Activity feed of %s's favorites.", 'buddypress' ), bp_get_displayed_user_fullname() ),602 'activity_args' => 'include=' . $fav_ids603 ) );604 }605 add_action( 'bp_actions', 'bp_activity_action_favorites_feed' );606 607 /**608 530 * Loads Akismet filtering for activity. 609 531 * 610 532 * @since 1.6.0 -
src/bp-activity/bp-activity-favorites.php
diff --git src/bp-activity/bp-activity-favorites.php src/bp-activity/bp-activity-favorites.php index e69de29..887b350 100644
1 <?php 2 /** 3 * Activity Favorites feature. 4 * 5 * @package BuddyPress 6 * @subpackage ActivityFavorites 7 * @since 1.2.0 8 */ 9 10 // Exit if accessed directly. 11 defined( 'ABSPATH' ) || exit; 12 13 /** Functions *****************************************************************/ 14 15 /** 16 * Private function to validate favorites. 17 * 18 * Before 2.7.0, all user favorites were in a serialized array in the same usermeta. 19 * This function makes sure to delete this meta, it checks the favorited activities 20 * still exist and eventually add metas. This function should be used only once per 21 * user. 22 * 23 * @since 2.7.0 24 * 25 * @param int $user_id The ID of the user we need to validate favorites for. 26 * @param array $activity_ids The list of favorites to validate. 27 * @return array The validated favorites. 28 */ 29 function _bp_activity_validate_favorites( $user_id, $activity_ids = array() ) { 30 if ( empty( $activity_ids ) || empty( $user_id ) ) { 31 return array(); 32 } 33 34 $activities = bp_activity_get( array( 'in'=> $activity_ids ) ); 35 bp_delete_user_meta( $user_id, 'bp_favorite_activities', $activity_ids ); 36 37 if ( empty( $activities['activities'] ) ) { 38 return array(); 39 } 40 41 $activity_ids = wp_list_pluck( $activities['activities'], 'id' ); 42 43 foreach ( $activity_ids as $id ) { 44 bp_add_user_meta( $user_id, 'bp_favorite_activities', $id ); 45 } 46 47 return $activity_ids; 48 } 49 50 /** 51 * Get a users favorite activity stream items. 52 * 53 * @since 1.2.0 54 * 55 * @param int $user_id ID of the user whose favorites are being queried. 56 * @return array IDs of the user's favorite activity items. 57 */ 58 function bp_activity_get_user_favorites( $user_id = 0, $nofilter = false ) { 59 60 // Fallback to logged in user if no user_id is passed. 61 if ( empty( $user_id ) ) { 62 $user_id = bp_displayed_user_id(); 63 } 64 65 // Get favorites for user. 66 $favs = bp_get_user_meta( $user_id, 'bp_favorite_activities' ); 67 68 if ( empty( $favs ) ) { 69 return array(); 70 } 71 72 $validate_favs = array(); 73 foreach ( $favs as $f => $fav ) { 74 if ( is_array( $fav ) ) { 75 $validate_favs = $fav; 76 unset( $favs[ $f ] ); 77 } 78 } 79 80 if ( ! empty( $validate_favs ) ) { 81 $validate_favs = _bp_activity_validate_favorites( $user_id, array_diff( $validate_favs, $favs ) ); 82 $favs = array_unique( array_merge( $favs, $validate_favs ) ); 83 } 84 85 // Activity ids are int. 86 $favs = array_map( 'intval', $favs ); 87 88 // Returns the unfiltered favorites. 89 if ( $nofilter ) { 90 return $favs; 91 } 92 93 /** 94 * Filters the favorited activity items for a specified user. 95 * 96 * @since 1.2.0 97 * 98 * @param array $favs Array of user's favorited activity items. 99 */ 100 return apply_filters( 'bp_activity_get_user_favorites', $favs ); 101 } 102 103 /** 104 * Add an activity stream item as a favorite for a user. 105 * 106 * @since 1.2.0 107 * 108 * @param int $activity_id ID of the activity item being favorited. 109 * @param int $user_id ID of the user favoriting the activity item. 110 * @return bool True on success, false on failure. 111 */ 112 function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) { 113 114 // Favorite activity stream items are for logged in users only. 115 if ( ! is_user_logged_in() ) { 116 return false; 117 } 118 119 // Fallback to logged in user if no user_id is passed. 120 if ( empty( $user_id ) ) { 121 $user_id = bp_loggedin_user_id(); 122 } 123 124 // Get all favorites 125 $my_favs = bp_activity_get_user_favorites( $user_id, true ); 126 $my_favs = array_flip( (array) $my_favs ); 127 128 // Bail if the user has already favorited this activity item. 129 if ( isset( $my_favs[ $activity_id ] ) ) { 130 return false; 131 } 132 133 // Add to user's favorites. 134 bp_add_user_meta( $user_id, 'bp_favorite_activities', $activity_id ); 135 136 // Update the total number of users who have favorited this activity. 137 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 138 $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1; 139 140 // Update activity meta counts. 141 if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) { 142 143 /** 144 * Fires if bp_activity_update_meta() for favorite_count is successful and before returning a true value for success. 145 * 146 * @since 1.2.1 147 * 148 * @param int $activity_id ID of the activity item being favorited. 149 * @param int $user_id ID of the user doing the favoriting. 150 */ 151 do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id ); 152 153 // Success. 154 return true; 155 156 // Saving meta was unsuccessful for an unknown reason. 157 } else { 158 159 /** 160 * Fires if bp_activity_update_meta() for favorite_count is unsuccessful and before returning a false value for failure. 161 * 162 * @since 1.5.0 163 * 164 * @param int $activity_id ID of the activity item being favorited. 165 * @param int $user_id ID of the user doing the favoriting. 166 */ 167 do_action( 'bp_activity_add_user_favorite_fail', $activity_id, $user_id ); 168 169 return false; 170 } 171 } 172 173 /** 174 * Remove an activity stream item as a favorite for a user. 175 * 176 * @since 1.2.0 177 * 178 * @param int $activity_id ID of the activity item being unfavorited. 179 * @param int $user_id ID of the user unfavoriting the activity item. 180 * @return bool True on success, false on failure. 181 */ 182 function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) { 183 184 // Favorite activity stream items are for logged in users only. 185 if ( ! is_user_logged_in() ) { 186 return false; 187 } 188 189 // Fallback to logged in user if no user_id is passed. 190 if ( empty( $user_id ) ) { 191 $user_id = bp_loggedin_user_id(); 192 } 193 194 $my_favs = bp_activity_get_user_favorites( $user_id, true ); 195 $my_favs = array_flip( (array) $my_favs ); 196 197 // Bail if the user has not previously favorited the item. 198 if ( ! isset( $my_favs[ $activity_id ] ) ) { 199 return false; 200 } 201 202 // Update the total number of users who have favorited this activity. 203 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' ); 204 if ( ! empty( $fav_count ) ) { 205 206 // Deduct from total favorites. 207 if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) { 208 209 // Update users favorites. 210 if ( bp_delete_user_meta( $user_id, 'bp_favorite_activities', $activity_id ) ) { 211 212 /** 213 * Fires if bp_update_user_meta() is successful and before returning a true value for success. 214 * 215 * @since 1.2.1 216 * 217 * @param int $activity_id ID of the activity item being unfavorited. 218 * @param int $user_id ID of the user doing the unfavoriting. 219 */ 220 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id ); 221 222 // Success. 223 return true; 224 225 // Error updating. 226 } else { 227 return false; 228 } 229 230 // Error updating favorite count. 231 } else { 232 return false; 233 } 234 235 // Error getting favorite count. 236 } else { 237 return false; 238 } 239 } 240 241 /** 242 * Retrieve the number of favorite activity stream items a user has. 243 * 244 * @since 1.2.0 245 * 246 * @param int $user_id ID of the user whose favorite count is being requested. 247 * @return int Total favorite count for the user. 248 */ 249 function bp_activity_total_favorites_for_user( $user_id = 0 ) { 250 $count = 0; 251 252 // Fallback on displayed user, and then logged in user. 253 if ( ! $user_id ) { 254 $user_id = bp_displayed_user_id(); 255 256 if ( ! $user_id ) { 257 $user_id = bp_loggedin_user_id(); 258 } 259 } 260 261 if ( ! $user_id ) { 262 return $count; 263 } 264 265 $favorites = bp_activity_get_user_favorites( $user_id, true ); 266 267 return count( $favorites ); 268 } 269 270 /** 271 * Delete favorites for all users for the requested activity ids. 272 * 273 * @since 2.7.0 274 * 275 * @param array $activity_ids The list of activities 276 */ 277 function bp_activity_delete_favorites( $activity_ids = array() ) { 278 if ( empty( $activity_ids ) ) { 279 return; 280 } 281 282 bp_delete_meta_by_key_value( 'user', 'bp_favorite_activities', $activity_ids ); 283 } 284 add_action( 'bp_activity_deleted_activities', 'bp_activity_delete_favorites', 10, 1 ); 285 286 /** Actions *******************************************************************/ 287 288 /** 289 * Mark activity as favorite. 290 * 291 * @since 1.2.0 292 * 293 * @return bool False on failure. 294 */ 295 function bp_activity_action_mark_favorite() { 296 297 if ( !is_user_logged_in() || !bp_is_activity_component() || !bp_is_current_action( 'favorite' ) ) 298 return false; 299 300 // Check the nonce. 301 check_admin_referer( 'mark_favorite' ); 302 303 if ( bp_activity_add_user_favorite( bp_action_variable( 0 ) ) ) 304 bp_core_add_message( __( 'Activity marked as favorite.', 'buddypress' ) ); 305 else 306 bp_core_add_message( __( 'There was an error marking that activity as a favorite. Please try again.', 'buddypress' ), 'error' ); 307 308 bp_core_redirect( wp_get_referer() . '#activity-' . bp_action_variable( 0 ) ); 309 } 310 add_action( 'bp_actions', 'bp_activity_action_mark_favorite' ); 311 312 /** 313 * Remove activity from favorites. 314 * 315 * @since 1.2.0 316 * 317 * @return bool False on failure. 318 */ 319 function bp_activity_action_remove_favorite() { 320 321 if ( ! is_user_logged_in() || ! bp_is_activity_component() || ! bp_is_current_action( 'unfavorite' ) ) 322 return false; 323 324 // Check the nonce. 325 check_admin_referer( 'unmark_favorite' ); 326 327 if ( bp_activity_remove_user_favorite( bp_action_variable( 0 ) ) ) 328 bp_core_add_message( __( 'Activity removed as favorite.', 'buddypress' ) ); 329 else 330 bp_core_add_message( __( 'There was an error removing that activity as a favorite. Please try again.', 'buddypress' ), 'error' ); 331 332 bp_core_redirect( wp_get_referer() . '#activity-' . bp_action_variable( 0 ) ); 333 } 334 add_action( 'bp_actions', 'bp_activity_action_remove_favorite' ); 335 336 /** 337 * Load a user's favorites feed. 338 * 339 * @since 1.2.0 340 * 341 * @return bool False on failure. 342 */ 343 function bp_activity_action_favorites_feed() { 344 if ( ! bp_is_user_activity() || ! bp_is_current_action( 'favorites' ) || ! bp_is_action_variable( 'feed', 0 ) ) { 345 return false; 346 } 347 348 // Get displayed user's favorite activity IDs. 349 $favs = bp_activity_get_user_favorites( bp_displayed_user_id() ); 350 $fav_ids = implode( ',', (array) $favs ); 351 352 // Setup the feed. 353 buddypress()->activity->feed = new BP_Activity_Feed( array( 354 'id' => 'favorites', 355 356 /* translators: User activity favorites RSS title - "[Site Name] | [User Display Name] | Favorites" */ 357 'title' => sprintf( __( '%1$s | %2$s | Favorites', 'buddypress' ), bp_get_site_name(), bp_get_displayed_user_fullname() ), 358 359 'link' => bp_displayed_user_domain() . bp_get_activity_slug() . '/favorites/', 360 'description' => sprintf( __( "Activity feed of %s's favorites.", 'buddypress' ), bp_get_displayed_user_fullname() ), 361 'activity_args' => 'include=' . $fav_ids 362 ) ); 363 } 364 add_action( 'bp_actions', 'bp_activity_action_favorites_feed' ); 365 366 /** Screens *******************************************************************/ 367 368 /** 369 * Load the 'Favorites' activity page. 370 * 371 * @since 1.2.0 372 * 373 */ 374 function bp_activity_screen_favorites() { 375 bp_update_is_item_admin( bp_current_user_can( 'bp_moderate' ), 'activity' ); 376 377 /** 378 * Fires right before the loading of the "Favorites" screen template file. 379 * 380 * @since 1.2.0 381 */ 382 do_action( 'bp_activity_screen_favorites' ); 383 384 /** 385 * Filters the template to load for the "Favorites" screen. 386 * 387 * @since 1.2.0 388 * 389 * @param string $template Path to the activity template to load. 390 */ 391 bp_core_load_template( apply_filters( 'bp_activity_template_favorite_activity', 'members/single/home' ) ); 392 } 393 394 /** Filters *******************************************************************/ 395 396 add_filter( 'bp_get_total_favorite_count_for_user', 'bp_core_number_format' ); 397 398 /** 399 * Set up activity arguments for use with the 'favorites' scope. 400 * 401 * @since 2.2.0 402 * 403 * @param array $retval Empty array by default. 404 * @param array $filter Current activity arguments. 405 * @return array $retval 406 */ 407 function bp_activity_filter_favorites_scope( $retval = array(), $filter = array() ) { 408 409 // Determine the user_id. 410 if ( ! empty( $filter['user_id'] ) ) { 411 $user_id = $filter['user_id']; 412 } else { 413 $user_id = bp_displayed_user_id() 414 ? bp_displayed_user_id() 415 : bp_loggedin_user_id(); 416 } 417 418 // Determine the favorites. 419 $favs = bp_activity_get_user_favorites( $user_id ); 420 if ( empty( $favs ) ) { 421 $favs = array( 0 ); 422 } 423 424 // Should we show all items regardless of sitewide visibility? 425 $show_hidden = array(); 426 if ( ! empty( $user_id ) && ( $user_id !== bp_loggedin_user_id() ) ) { 427 $show_hidden = array( 428 'column' => 'hide_sitewide', 429 'value' => 0 430 ); 431 } 432 433 $retval = array( 434 'relation' => 'AND', 435 array( 436 'column' => 'id', 437 'compare' => 'IN', 438 'value' => (array) $favs 439 ), 440 $show_hidden, 441 442 // Overrides. 443 'override' => array( 444 'display_comments' => true, 445 'filter' => array( 'user_id' => 0 ), 446 'show_hidden' => true 447 ), 448 ); 449 450 return $retval; 451 } 452 add_filter( 'bp_activity_set_favorites_scope_args', 'bp_activity_filter_favorites_scope', 10, 2 ); -
src/bp-activity/bp-activity-filters.php
diff --git src/bp-activity/bp-activity-filters.php src/bp-activity/bp-activity-filters.php index 104181a..ebfead4 100644
add_filter( 'bp_get_activity_parent_content', 'bp_create_excerpt' ); 106 106 add_filter( 'bp_get_activity_content_body', 'bp_activity_truncate_entry', 5 ); 107 107 add_filter( 'bp_get_activity_content', 'bp_activity_truncate_entry', 5 ); 108 108 109 add_filter( 'bp_get_total_favorite_count_for_user', 'bp_core_number_format' );110 109 add_filter( 'bp_get_total_mention_count_for_user', 'bp_core_number_format' ); 111 110 112 111 add_filter( 'bp_activity_get_embed_excerpt', 'bp_activity_embed_excerpt_onclick_location_filter', 9 ); … … function bp_activity_filter_just_me_scope( $retval = array(), $filter = array() 720 719 } 721 720 add_filter( 'bp_activity_set_just-me_scope_args', 'bp_activity_filter_just_me_scope', 10, 2 ); 722 721 723 /**724 * Set up activity arguments for use with the 'favorites' scope.725 *726 * @since 2.2.0727 *728 * @param array $retval Empty array by default.729 * @param array $filter Current activity arguments.730 * @return array $retval731 */732 function bp_activity_filter_favorites_scope( $retval = array(), $filter = array() ) {733 734 // Determine the user_id.735 if ( ! empty( $filter['user_id'] ) ) {736 $user_id = $filter['user_id'];737 } else {738 $user_id = bp_displayed_user_id()739 ? bp_displayed_user_id()740 : bp_loggedin_user_id();741 }742 743 // Determine the favorites.744 $favs = bp_activity_get_user_favorites( $user_id );745 if ( empty( $favs ) ) {746 $favs = array( 0 );747 }748 749 // Should we show all items regardless of sitewide visibility?750 $show_hidden = array();751 if ( ! empty( $user_id ) && ( $user_id !== bp_loggedin_user_id() ) ) {752 $show_hidden = array(753 'column' => 'hide_sitewide',754 'value' => 0755 );756 }757 758 $retval = array(759 'relation' => 'AND',760 array(761 'column' => 'id',762 'compare' => 'IN',763 'value' => (array) $favs764 ),765 $show_hidden,766 767 // Overrides.768 'override' => array(769 'display_comments' => true,770 'filter' => array( 'user_id' => 0 ),771 'show_hidden' => true772 ),773 );774 775 return $retval;776 }777 add_filter( 'bp_activity_set_favorites_scope_args', 'bp_activity_filter_favorites_scope', 10, 2 );778 779 722 780 723 /** 781 * Set up activity arguments for use with the ' favorites' scope.724 * Set up activity arguments for use with the 'mentions' scope. 782 725 * 783 726 * @since 2.2.0 784 727 * -
src/bp-activity/bp-activity-functions.php
diff --git src/bp-activity/bp-activity-functions.php src/bp-activity/bp-activity-functions.php index a764320..89db128 100644
function bp_activity_get_types() { 839 839 return apply_filters( 'bp_activity_get_types', $actions ); 840 840 } 841 841 842 /** Favorites ****************************************************************/843 844 /**845 * Get a users favorite activity stream items.846 *847 * @since 1.2.0848 *849 * @param int $user_id ID of the user whose favorites are being queried.850 * @return array IDs of the user's favorite activity items.851 */852 function bp_activity_get_user_favorites( $user_id = 0 ) {853 854 // Fallback to logged in user if no user_id is passed.855 if ( empty( $user_id ) ) {856 $user_id = bp_displayed_user_id();857 }858 859 // Get favorites for user.860 $favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );861 862 /**863 * Filters the favorited activity items for a specified user.864 *865 * @since 1.2.0866 *867 * @param array $favs Array of user's favorited activity items.868 */869 return apply_filters( 'bp_activity_get_user_favorites', $favs );870 }871 872 /**873 * Add an activity stream item as a favorite for a user.874 *875 * @since 1.2.0876 *877 * @param int $activity_id ID of the activity item being favorited.878 * @param int $user_id ID of the user favoriting the activity item.879 * @return bool True on success, false on failure.880 */881 function bp_activity_add_user_favorite( $activity_id, $user_id = 0 ) {882 883 // Favorite activity stream items are for logged in users only.884 if ( ! is_user_logged_in() ) {885 return false;886 }887 888 // Fallback to logged in user if no user_id is passed.889 if ( empty( $user_id ) ) {890 $user_id = bp_loggedin_user_id();891 }892 893 $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );894 if ( empty( $my_favs ) || ! is_array( $my_favs ) ) {895 $my_favs = array();896 }897 898 // Bail if the user has already favorited this activity item.899 if ( in_array( $activity_id, $my_favs ) ) {900 return false;901 }902 903 // Add to user's favorites.904 $my_favs[] = $activity_id;905 906 // Update the total number of users who have favorited this activity.907 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );908 $fav_count = !empty( $fav_count ) ? (int) $fav_count + 1 : 1;909 910 // Update user meta.911 bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs );912 913 // Update activity meta counts.914 if ( bp_activity_update_meta( $activity_id, 'favorite_count', $fav_count ) ) {915 916 /**917 * Fires if bp_activity_update_meta() for favorite_count is successful and before returning a true value for success.918 *919 * @since 1.2.1920 *921 * @param int $activity_id ID of the activity item being favorited.922 * @param int $user_id ID of the user doing the favoriting.923 */924 do_action( 'bp_activity_add_user_favorite', $activity_id, $user_id );925 926 // Success.927 return true;928 929 // Saving meta was unsuccessful for an unknown reason.930 } else {931 932 /**933 * Fires if bp_activity_update_meta() for favorite_count is unsuccessful and before returning a false value for failure.934 *935 * @since 1.5.0936 *937 * @param int $activity_id ID of the activity item being favorited.938 * @param int $user_id ID of the user doing the favoriting.939 */940 do_action( 'bp_activity_add_user_favorite_fail', $activity_id, $user_id );941 942 return false;943 }944 }945 946 /**947 * Remove an activity stream item as a favorite for a user.948 *949 * @since 1.2.0950 *951 * @param int $activity_id ID of the activity item being unfavorited.952 * @param int $user_id ID of the user unfavoriting the activity item.953 * @return bool True on success, false on failure.954 */955 function bp_activity_remove_user_favorite( $activity_id, $user_id = 0 ) {956 957 // Favorite activity stream items are for logged in users only.958 if ( ! is_user_logged_in() ) {959 return false;960 }961 962 // Fallback to logged in user if no user_id is passed.963 if ( empty( $user_id ) ) {964 $user_id = bp_loggedin_user_id();965 }966 967 $my_favs = bp_get_user_meta( $user_id, 'bp_favorite_activities', true );968 $my_favs = array_flip( (array) $my_favs );969 970 // Bail if the user has not previously favorited the item.971 if ( ! isset( $my_favs[ $activity_id ] ) ) {972 return false;973 }974 975 // Remove the fav from the user's favs.976 unset( $my_favs[$activity_id] );977 $my_favs = array_unique( array_flip( $my_favs ) );978 979 // Update the total number of users who have favorited this activity.980 $fav_count = bp_activity_get_meta( $activity_id, 'favorite_count' );981 if ( ! empty( $fav_count ) ) {982 983 // Deduct from total favorites.984 if ( bp_activity_update_meta( $activity_id, 'favorite_count', (int) $fav_count - 1 ) ) {985 986 // Update users favorites.987 if ( bp_update_user_meta( $user_id, 'bp_favorite_activities', $my_favs ) ) {988 989 /**990 * Fires if bp_update_user_meta() is successful and before returning a true value for success.991 *992 * @since 1.2.1993 *994 * @param int $activity_id ID of the activity item being unfavorited.995 * @param int $user_id ID of the user doing the unfavoriting.996 */997 do_action( 'bp_activity_remove_user_favorite', $activity_id, $user_id );998 999 // Success.1000 return true;1001 1002 // Error updating.1003 } else {1004 return false;1005 }1006 1007 // Error updating favorite count.1008 } else {1009 return false;1010 }1011 1012 // Error getting favorite count.1013 } else {1014 return false;1015 }1016 }1017 1018 842 /** 1019 843 * Check whether an activity item exists with a given content string. 1020 844 * … … function bp_activity_get_last_updated() { 1054 878 return apply_filters( 'bp_activity_get_last_updated', BP_Activity_Activity::get_last_updated() ); 1055 879 } 1056 880 1057 /**1058 * Retrieve the number of favorite activity stream items a user has.1059 *1060 * @since 1.2.01061 *1062 * @param int $user_id ID of the user whose favorite count is being requested.1063 * @return int Total favorite count for the user.1064 */1065 function bp_activity_total_favorites_for_user( $user_id = 0 ) {1066 1067 // Fallback on displayed user, and then logged in user.1068 if ( empty( $user_id ) ) {1069 $user_id = ( bp_displayed_user_id() ) ? bp_displayed_user_id() : bp_loggedin_user_id();1070 }1071 1072 return BP_Activity_Activity::total_favorite_count( $user_id );1073 }1074 1075 881 /** Meta *********************************************************************/ 1076 882 1077 883 /** -
src/bp-activity/bp-activity-screens.php
diff --git src/bp-activity/bp-activity-screens.php src/bp-activity/bp-activity-screens.php index a131548..4f5d4b3 100644
function bp_activity_screen_groups() { 131 131 } 132 132 133 133 /** 134 * Load the 'Favorites' activity page.135 *136 * @since 1.2.0137 *138 */139 function bp_activity_screen_favorites() {140 bp_update_is_item_admin( bp_current_user_can( 'bp_moderate' ), 'activity' );141 142 /**143 * Fires right before the loading of the "Favorites" screen template file.144 *145 * @since 1.2.0146 */147 do_action( 'bp_activity_screen_favorites' );148 149 /**150 * Filters the template to load for the "Favorites" screen.151 *152 * @since 1.2.0153 *154 * @param string $template Path to the activity template to load.155 */156 bp_core_load_template( apply_filters( 'bp_activity_template_favorite_activity', 'members/single/home' ) );157 }158 159 /**160 134 * Load the 'Mentions' activity page. 161 135 * 162 136 * @since 1.2.0 -
src/bp-activity/bp-activity-template.php
diff --git src/bp-activity/bp-activity-template.php src/bp-activity/bp-activity-template.php index c2d33b1..12af212 100644
function bp_activity_is_favorite() { 1726 1726 function bp_get_activity_is_favorite() { 1727 1727 global $activities_template; 1728 1728 1729 if ( ! bp_is_active( 'activity', 'favorites' ) ) { 1730 return false; 1731 } 1732 1729 1733 /** 1730 1734 * Filters whether the current activity item is in the current user's favorites. 1731 1735 * … … function bp_activity_can_comment_reply( $comment = false ) { 2917 2921 * @return bool True if comment can receive comments. 2918 2922 */ 2919 2923 function bp_activity_can_favorite() { 2924 /** 2925 * As this function is used in BP Legacy's templates 2926 * we'll use it to check if the feature is enable. 2927 */ 2928 $can_favorite = bp_is_active( 'activity', 'favorites' ); 2920 2929 2921 2930 /** 2922 2931 * Filters whether or not users can favorite activity items. 2923 2932 * 2924 2933 * @since 1.5.0 2925 2934 * 2926 * @param bool $ value Whether or not favoriting is enabled.2935 * @param bool $can_favorite Whether or not favoriting is enabled. 2927 2936 */ 2928 return apply_filters( 'bp_activity_can_favorite', true );2937 return apply_filters( 'bp_activity_can_favorite', $can_favorite ); 2929 2938 } 2930 2939 2931 2940 /** -
src/bp-activity/classes/class-bp-activity-activity.php
diff --git src/bp-activity/classes/class-bp-activity-activity.php src/bp-activity/classes/class-bp-activity-activity.php index c24a533..dea165a 100644
class BP_Activity_Activity { 1796 1796 * Get favorite count for a given user. 1797 1797 * 1798 1798 * @since 1.2.0 1799 * @deprecated 2.7.0 1799 1800 * 1800 1801 * @param int $user_id The ID of the user whose favorites you're counting. 1801 1802 * @return int $value A count of the user's favorites. 1802 1803 */ 1803 1804 public static function total_favorite_count( $user_id ) { 1805 _deprecated_function( __CLASS__ . '::' . __METHOD__, '2.6' ); 1804 1806 1805 // Get activities from user meta. 1806 $favorite_activity_entries = bp_get_user_meta( $user_id, 'bp_favorite_activities', true ); 1807 if ( ! empty( $favorite_activity_entries ) ) { 1808 return count( maybe_unserialize( $favorite_activity_entries ) ); 1807 if ( bp_is_active( 'activity', 'favorites' ) ) { 1808 return bp_activity_total_favorites_for_user( $user_id ); 1809 1809 } 1810 1811 // No favorites.1812 return 0;1813 1810 } 1814 1811 1815 1812 /** -
src/bp-activity/classes/class-bp-activity-component.php
diff --git src/bp-activity/classes/class-bp-activity-component.php src/bp-activity/classes/class-bp-activity-component.php index 4f85efb..3f79fba 100644
class BP_Activity_Component extends BP_Component { 32 32 array( 33 33 'adminbar_myaccount_order' => 10, 34 34 'search_query_arg' => 'activity_search', 35 'features' => array( 'embeds' )35 'features' => array( 'embeds', 'favorites' ) 36 36 ) 37 37 ); 38 38 } … … class BP_Activity_Component extends BP_Component { 78 78 $includes[] = 'embeds'; 79 79 } 80 80 81 // Favorites 82 if ( bp_is_active( $this->id, 'favorites' ) ) { 83 $includes[] = 'favorites'; 84 } 85 81 86 if ( is_admin() ) { 82 87 $includes[] = 'admin'; 83 88 } -
src/bp-activity/classes/class-bp-activity-template.php
diff --git src/bp-activity/classes/class-bp-activity-template.php src/bp-activity/classes/class-bp-activity-template.php index 864e551..a519fd2 100644
class BP_Activity_Template { 195 195 // Check if blog/forum replies are disabled. 196 196 $this->disable_blogforum_replies = (bool) bp_core_get_root_option( 'bp-disable-blogforum-comments' ); 197 197 198 // Get an array of the logged in user's favorite activities. 199 $this->my_favs = maybe_unserialize( bp_get_user_meta( bp_loggedin_user_id(), 'bp_favorite_activities', true ) ); 198 if ( bp_is_active( 'activity', 'favorites' ) ) { 199 // Get an array of the logged in user's favorite activities. 200 $this->my_favs = bp_activity_get_user_favorites( bp_loggedin_user_id() ); 201 } 200 202 201 203 // Fetch specific activity items based on ID's. 202 204 if ( !empty( $include ) ) { -
src/bp-core/bp-core-functions.php
diff --git src/bp-core/bp-core-functions.php src/bp-core/bp-core-functions.php index 7407add..41a02b8 100644
function bp_get_user_meta( $user_id, $key, $single = false ) { 1530 1530 } 1531 1531 1532 1532 /** 1533 * Add a piece of usermeta. 1534 * 1535 * This is a wrapper for add_user_meta() that allows for easy use of 1536 * bp_get_user_meta_key(), thereby increasing compatibility with non-standard 1537 * BP setups. 1538 * 1539 * @since 2.7.0 1540 * 1541 * @see add_user_meta() For complete details about parameters and return values. 1542 * 1543 * @param int $user_id The ID of the user whose meta you're setting. 1544 * @param string $key The meta key to set. 1545 * @param mixed $value Metadata value. 1546 * @param bool $unique Optional, default is false. Whether the same key should not be added. 1547 * @return int|false Meta ID on success, false on failure. 1548 */ 1549 function bp_add_user_meta( $user_id, $key, $value, $unique = false ) { 1550 return add_user_meta( $user_id, bp_get_user_meta_key( $key ), $value, $unique ); 1551 } 1552 1553 /** 1533 1554 * Update a piece of usermeta. 1534 1555 * 1535 1556 * This is a wrapper for update_user_meta() that allows for easy use of … … function bp_delete_user_meta( $user_id, $key, $value = '' ) { 1570 1591 return delete_user_meta( $user_id, bp_get_user_meta_key( $key ), $value ); 1571 1592 } 1572 1593 1594 /** 1595 * Get the meta_id(s) or the object_id(s) for the requested meta_key and meta_value. 1596 * 1597 * @since 2.7.0 1598 * 1599 * @param string $meta_type Required. Type of object metadata is for (e.g. user, group, blog, ...). 1600 * Default: 'user'. 1601 * @param string $key Required. The meta key to get. 1602 * @param mixed $meta_value Required. Metadata value. 1603 * @param string $id_type Optional. Whether to get 'meta' or 'object' ids 1604 * Default: 'meta'. 1605 * @return array The list of meta_ids or object_ids. 1606 */ 1607 function bp_get_meta_by_key_value( $meta_type = 'user', $key = '', $meta_value = '', $id_type = 'meta' ) { 1608 global $wpdb; 1609 1610 // Make sure everything is valid. 1611 if ( ! $meta_type || ! $key || ! $meta_value ) { 1612 return false; 1613 } 1614 1615 $table = _get_meta_table( $meta_type ); 1616 if ( ! $table ) { 1617 return false; 1618 } 1619 1620 $meta_key = $key; 1621 $id_column = 'meta_id'; 1622 if ( 'user' === $meta_type ) { 1623 $id_column = 'umeta_id'; 1624 1625 // Make sure multiple instances of BuddyPress can be used. 1626 $meta_key = bp_get_user_meta_key( $key ); 1627 } 1628 1629 if ( 'object' === $id_type ) { 1630 $id_column = sprintf( '%s_id', $meta_type ); 1631 } 1632 1633 $sql = array( 1634 'select' => "SELECT {$id_column} FROM {$table}", 1635 'where' => array( 1636 'meta_key' => $wpdb->prepare( 'meta_key = %s', $meta_key ), 1637 ), 1638 ); 1639 1640 if ( is_array( $meta_value ) ) { 1641 $in = '"' . implode( '","', array_map( 'esc_sql', $meta_value ) ) . '"'; 1642 $sql['where']['meta_value'] = "meta_value IN ({$in})"; 1643 } elseif ( is_numeric( $meta_value ) ) { 1644 $sql['where']['meta_value'] = $wpdb->prepare( 'meta_value = %d', $meta_key ); 1645 } else { 1646 $sql['where']['meta_value'] = $wpdb->prepare( 'meta_value = %s', $meta_key ); 1647 } 1648 1649 $sql['where'] = join( ' AND ', $sql['where'] ); 1650 1651 return $wpdb->get_col( join( ' WHERE ', $sql ) ); 1652 } 1653 1654 /** 1655 * Delete metadata for the requested meta_key and meta_value. 1656 * 1657 * @since 2.7.0 1658 * 1659 * @param string $meta_type Required. Type of object metadata is for (e.g. user, group, blog, ...). 1660 * Default: 'user'. 1661 * @param string $key Required. The meta key to get. 1662 * @param mixed $meta_value Required. Metadata value. 1663 * @return bool True if metadata were sucessfully deleted. False otherwise. 1664 */ 1665 function bp_delete_meta_by_key_value( $meta_type = 'user', $key = '', $meta_value = '' ) { 1666 $mids = bp_get_meta_by_key_value( $meta_type, $key, $meta_value ); 1667 1668 if ( empty( $mids ) ) { 1669 return false; 1670 } 1671 1672 foreach ( (array) $mids as $mid ) { 1673 delete_metadata_by_mid( $meta_type, $mid ); 1674 } 1675 1676 return true; 1677 } 1678 1573 1679 /** Embeds ********************************************************************/ 1574 1680 1575 1681 /** -
tests/phpunit/testcases/activity/functions.php
diff --git tests/phpunit/testcases/activity/functions.php tests/phpunit/testcases/activity/functions.php index 1132385..62685ee 100644
Bar!'; 1358 1358 } 1359 1359 1360 1360 /** 1361 * @group favorites 1362 * @ticket BP3794 1363 */ 1364 public function test_user_favorite_after_favorited_activity_deleted() { 1365 $u1 = $this->factory->user->create(); 1366 $a1 = $this->factory->activity->create(); 1367 $a2 = $this->factory->activity->create(); 1368 1369 // bp_activity_add_user_favorite() requires a logged-in user. 1370 $current_user = bp_loggedin_user_id(); 1371 $this->set_current_user( $u1 ); 1372 1373 // Favorite activities 1374 bp_activity_add_user_favorite( $a1, $u1 ); 1375 bp_activity_add_user_favorite( $a2, $u1 ); 1376 1377 // Delete one activity 1378 bp_activity_delete( array( 'id' => $a2 ) ); 1379 $favorites = bp_activity_get_user_favorites( $u1 ); 1380 $activities = bp_activity_get( array( 'in' => $favorites ) ); 1381 $a12 = wp_list_pluck( $activities['activities'], 'id' ); 1382 1383 $this->assertSame( $a12, $favorites ); 1384 1385 $this->set_current_user( $current_user ); 1386 } 1387 1388 /** 1389 * @group favorites 1390 * @ticket BP3794 1391 */ 1392 public function test_user_favorite_meta_not_validated() { 1393 $u1 = $this->factory->user->create(); 1394 $u2 = $this->factory->user->create(); 1395 $activities = $this->factory->activity->create_many( 5 ); 1396 1397 $c1 = bp_activity_new_comment( array( 1398 'activity_id' => $activities[1], 1399 'parent_id' => $activities[1], 1400 'content' => 'foo', 1401 'user_id' => $u1, 1402 ) ); 1403 1404 $c2 = bp_activity_new_comment( array( 1405 'activity_id' => $activities[2], 1406 'parent_id' => $activities[2], 1407 'content' => 'foo', 1408 'user_id' => $u2, 1409 ) ); 1410 1411 $old_meta = bp_update_user_meta( $u1, 'bp_favorite_activities', array( 1412 $activities[0], 1413 $activities[1], 1414 $activities[2], 1415 ) ); 1416 1417 $current_user = get_current_user_id(); 1418 $this->set_current_user( $u1 ); 1419 1420 // Favorite activities 1421 bp_activity_add_user_favorite( $activities[0], $u1 ); 1422 bp_activity_add_user_favorite( $c2, $u1 ); 1423 1424 $this->set_current_user( $u2 ); 1425 bp_activity_add_user_favorite( $activities[4], $u2 ); 1426 bp_activity_add_user_favorite( $c1, $u2 ); 1427 1428 bp_activity_delete( array( 'id' => $activities[1] ) ); 1429 bp_activity_delete( array( 'id' => $activities[2] ) ); 1430 1431 $f1 = bp_activity_get_user_favorites( $u1 ); 1432 $this->assertSame( array( $activities[0] ), $f1 ); 1433 1434 $f2 = bp_activity_get_user_favorites( $u2 ); 1435 $this->assertSame( array( $activities[4] ), $f2 ); 1436 1437 $this->set_current_user( $current_user ); 1438 } 1439 1440 /** 1361 1441 * @group bp_activity_post_update 1362 1442 */ 1363 1443 public function test_bp_activity_post_update_empty_content() {