bp_ap_rtmedia_update_member_medias_count

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

Description

bp_ap_rtmedia_update_member_medias_count(); 

Usage

  1. if ( !function_exists( 'bp_ap_rtmedia_update_member_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_member_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_member_medias_count() { 
  2. global $bp, $wpdb; 
  3.  
  4. $is_super_admin = is_super_admin(); 
  5. $bp_displayed_user_id = bp_displayed_user_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; 
  11.  
  12. $media_model = new RTMediaModel(); 
  13.  
  14.  
  15. $allowed_media_types = array(); 
  16. foreach ( $rtmedia->allowed_types as $value ) { 
  17. $allowed_media_types[ ] = $value['name']; 
  18. $allowed_media_types = implode("', '", $allowed_media_types); 
  19. $allowed_media_types = "'".$allowed_media_types."'"; 
  20.  
  21.  
  22. $table_name = $media_model->table_name; 
  23.  
  24. $r = $wpdb->get_results( $wpdb->prepare( "SELECT activity_id, media_type  
  25. from {$table_name}  
  26. where media_type IN ({$allowed_media_types})  
  27. and context = 'profile' 
  28. and media_author = %d  
  29. and blog_id = %d", $bp_displayed_user_id, get_current_blog_id() ) ); 
  30.  
  31.  
  32. $removed_media_count = array(); 
  33. foreach ( $r as $my_r ) { 
  34. $activities = bp_activity_get_specific( array( 'activity_ids' => $my_r->activity_id ) ); 
  35. $activity = $activities["activities"][0]; 
  36.  
  37. $remove_from_stream = bp_visibility_is_activity_invisible( $activity, $bp_loggedin_user_id, $is_super_admin, $bp_displayed_user_id ); 
  38.  
  39. if ($remove_from_stream) { 
  40. if(!isset($removed_media_count[$my_r->media_type])) 
  41. $removed_media_count[$my_r->media_type] = 0; 
  42.  
  43. $removed_media_count[$my_r->media_type]++; 
  44.  
  45.  
  46. $rtMediaNav = new RTMediaNav(); 
  47. $rtMediaNav->refresh_counts( $bp_displayed_user_id, array( "context" => 'profile', 'media_author' => $bp_displayed_user_id ) ); 
  48. $media_count = $rtMediaNav->get_counts( $bp_displayed_user_id ); 
  49.  
  50. $count = array(); 
  51. $count[0] = new stdClass(); 
  52. if ( !empty($media_count) ) { 
  53. // merga and sum all media by privacy 
  54. foreach ($media_count as $media) { 
  55. foreach ($media as $key => $value) { 
  56. if ( !isset($count[0]->{$key}) )  
  57. $count[0]->{$key} = 0; 
  58.  
  59. $count[0]->{$key} += $value; 
  60. $media_count = $count; 
  61.  
  62. $total = null; 
  63. if ( !empty($removed_media_count) && !empty($media_count) ) { 
  64. $total = 0; 
  65. foreach ($removed_media_count as $key => $value) { 
  66. $media_count[0]->{$key} -= $value; 
  67. $total += $media_count[0]->{$key}; 
  68.  
  69. if ( isset($total) ) { 
  70. $slug = apply_filters('rtmedia_media_tab_slug', RTMEDIA_MEDIA_SLUG ); 
  71.  
  72. foreach ($bp->bp_nav as $key => $value) { 
  73. if ($value['slug'] == $slug) { 
  74. $bp->bp_nav[$key]['name'] = RTMEDIA_MEDIA_LABEL . '<span>' . $total . '</span>'; 
  75. break; 
  76.  
  77. // update metadata 
  78. update_user_meta ( $bp_displayed_user_id, 'rtmedia_counts_' . get_current_blog_id(), $media_count );