bp_ap_rtmedia_update_group_medias_count

Update media count user meta each time a user visit the profile page.

Description

bp_ap_rtmedia_update_group_medias_count(); 

Usage

  1. if ( !function_exists( 'bp_ap_rtmedia_update_group_medias_count' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress-activity-privacy/includes/bp-activity-privacy-integrations.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = bp_ap_rtmedia_update_group_medias_count(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/bp-activity-privacy-integrations.php  
  1. function bp_ap_rtmedia_update_group_medias_count() { 
  2. global $bp, $wpdb; 
  3.  
  4. $is_super_admin = is_super_admin(); 
  5. $bp_group_id = bp_get_group_id(); 
  6. $bp_loggedin_user_id = bp_loggedin_user_id(); 
  7. //if ($bp_displayed_user_id == $bp_loggedin_user_id) 
  8. // return; 
  9.  
  10. global $rtmedia, $rtmedia_query; 
  11.  
  12. $allowed_media_types = array(); 
  13. foreach ( $rtmedia->allowed_types as $value ) { 
  14. $allowed_media_types[ ] = $value['name']; 
  15. $allowed_media_types = implode("', '", $allowed_media_types); 
  16. $allowed_media_types = "'".$allowed_media_types."'"; 
  17.  
  18. $table_name = $rtmedia_query->model->table_name; 
  19.  
  20. $r = $wpdb->get_results( $wpdb->prepare( "SELECT activity_id, media_type  
  21. from {$table_name}  
  22. where media_type IN ({$allowed_media_types})  
  23. and context = 'group'  
  24. and context_id = %d  
  25. and blog_id = %d", $bp_group_id, get_current_blog_id() ) ); 
  26.  
  27. $removed_media_count = array(); 
  28. foreach ( $r as $my_r ) { 
  29. $activities = bp_activity_get_specific( array( 'activity_ids' => $my_r->activity_id ) ); 
  30. $activity = $activities["activities"][0]; 
  31.  
  32. $remove_from_stream = bp_visibility_is_activity_invisible( $activity, $bp_loggedin_user_id, $is_super_admin, $bp_displayed_user_id ); 
  33.  
  34. if ($remove_from_stream) { 
  35. $removed_media_count[$my_r->media_type]++; 
  36.  
  37.  
  38. $rtMediaNav = new RTMediaNav(); 
  39. $rtMediaNav->refresh_counts( $bp_group_id, array( "context" => 'group', 'context_id' => $bp_group_id ) ); 
  40. $media_count = $rtMediaNav->get_counts( $bp_group_id, array( "context" => 'group', 'context_id' => $bp_group_id ) ); 
  41.  
  42. $count = array(); 
  43. $count[0] = new stdClass(); 
  44. if ( !empty($media_count) ) { 
  45. // merga and sum all media by privacy 
  46. foreach ($media_count as $media) { 
  47. foreach ($media as $key => $value) { 
  48. if ( !isset($count[0]->{$key}) )  
  49. $count[0]->{$key} = 0; 
  50.  
  51. $count[0]->{$key} += $value; 
  52. $media_count = $count; 
  53.  
  54. $total = 0; 
  55. if ( !empty($removed_media_count) && !empty($media_count) ) { 
  56. foreach ($removed_media_count as $key => $value) { 
  57. $media_count[0]->{$key} -= $value; 
  58. $total += $media_count[0]->{$key}; 
  59.  
  60. $slug = apply_filters('rtmedia_media_tab_slug', RTMEDIA_MEDIA_SLUG ); 
  61. $nav = reset($bp->bp_options_nav); 
  62. $kkey = key($bp->bp_options_nav); 
  63. foreach ($nav as $key => $value) { 
  64. if ($value['slug'] == $slug) { 
  65. $bp->bp_options_nav[$kkey][$key]['name'] = RTMEDIA_MEDIA_LABEL . '<span>' . $total . '</span>'; 
  66. break; 
  67.  
  68.  
  69. // update metadata 
  70. groups_update_groupmeta ( $bp_group_id, 'rtmedia_counts_' . get_current_blog_id(), $media_count ); 
  71.