/functions.php

  1. <?php 
  2.  
  3. /** 
  4. * Get all new notifications after a given time for the current user 
  5. * 
  6. * @global type $wpdb 
  7. * 
  8. * @param type $user_id 
  9. * @param type $last_notified 
  10. * 
  11. * @return type 
  12. */ 
  13.  
  14. function bpln_get_new_notifications( $user_id, $last_notified ) { 
  15.  
  16. global $wpdb; 
  17.  
  18. $bp = buddypress(); 
  19.  
  20. $table = $bp->notifications->table_name; 
  21.  
  22. $registered_components = bp_notifications_get_registered_components(); 
  23.  
  24.  
  25. $components_list = array(); 
  26.  
  27. foreach ( $registered_components as $component ) { 
  28. $components_list[] = $wpdb->prepare( '%s', $component ); 
  29.  
  30. $components_list = implode( ', ', $components_list ); 
  31.  
  32.  
  33. $query = "SELECT * FROM {$table} WHERE user_id = %d AND component_name IN ({$components_list}) AND id > %d AND is_new = %d "; 
  34.  
  35. $query = $wpdb->prepare( $query, $user_id, $last_notified, 1 ); 
  36.  
  37. return $wpdb->get_results( $query ); 
  38.  
  39. /** 
  40. * Get the last notification id for the user 
  41. * 
  42. * @global type $wpdb 
  43. * 
  44. * @param type $user_id 
  45. * 
  46. * @return type 
  47. */ 
  48. function bpln_get_latest_notification_id( $user_id = false ) { 
  49.  
  50. if ( ! $user_id ) { 
  51. $user_id = get_current_user_id(); 
  52.  
  53. global $wpdb; 
  54.  
  55. $bp = buddypress(); 
  56.  
  57. $table = $bp->notifications->table_name; 
  58.  
  59. $registered_components = bp_notifications_get_registered_components(); 
  60.  
  61.  
  62. $components_list = array(); 
  63.  
  64. foreach ( $registered_components as $component ) { 
  65. $components_list[] = $wpdb->prepare( '%s', $component ); 
  66.  
  67. $components_list = implode( ', ', $components_list ); 
  68.  
  69.  
  70. $query = "SELECT MAX(id) FROM {$table} WHERE user_id = %d AND component_name IN ({$components_list}) AND is_new = %d "; 
  71.  
  72. $query = $wpdb->prepare( $query, $user_id, 1 ); 
  73.  
  74. return (int) $wpdb->get_var( $query ); 
  75.  
  76.  
  77. /** 
  78. * Get a list of processed messages 
  79. * 
  80. */ 
  81. function bpln_get_notification_messages( $notifications ) { 
  82.  
  83. $messages = array(); 
  84.  
  85. if ( empty( $notifications ) ) { 
  86. return $messages; 
  87.  
  88. $total_notifications = count( $notifications ); 
  89.  
  90. for ( $i = 0; $i < $total_notifications; $i ++ ) { 
  91.  
  92. $notification = $notifications[ $i ]; 
  93.  
  94. $messages[] = bpln_get_the_notification_description( $notification ); 
  95.  
  96.  
  97.  
  98. return $messages; 
  99.  
  100. /** 
  101. * A copy of bp_get_the_notification_description to server our purpose of parsing notification to extract the message 
  102. * 
  103. * @see bp_get_the_notification_description 
  104. * 
  105. * @param type $notification 
  106. * 
  107. * @return type 
  108. */ 
  109.  
  110. function bpln_get_the_notification_description( $notification ) { 
  111.  
  112. $bp = buddypress(); 
  113.  
  114. // Callback function exists 
  115. if ( isset( $bp->{$notification->component_name}->notification_callback ) && is_callable( $bp->{$notification->component_name}->notification_callback ) ) { 
  116. $description = call_user_func( $bp->{$notification->component_name}->notification_callback, $notification->component_action, $notification->item_id, $notification->secondary_item_id, 1 ); 
  117.  
  118. // @deprecated format_notification_function - 1.5 
  119. } elseif ( isset( $bp->{$notification->component_name}->format_notification_function ) && function_exists( $bp->{$notification->component_name}->format_notification_function ) ) { 
  120. $description = call_user_func( $bp->{$notification->component_name}->format_notification_function, $notification->component_action, $notification->item_id, $notification->secondary_item_id, 1 ); 
  121.  
  122. // Allow non BuddyPress components to hook in 
  123. } else { 
  124.  
  125. /** This filter is documented in bp-notifications/bp-notifications-functions.php */ 
  126. $description = apply_filters_ref_array( 'bp_notifications_get_notifications_for_user', array( 
  127. $notification->component_action,  
  128. $notification->item_id,  
  129. $notification->secondary_item_id,  
  130. ) ); 
  131.  
  132. /** 
  133. * Filters the full-text description for a specific notification. 
  134. * 
  135. * @since BuddyPress (1.9.0) 
  136. * 
  137. * @param string $description Full-text description for a specific notification. 
  138. */ 
  139. return apply_filters( 'bp_get_the_notification_description', $description ); 
  140.  
  141. function bpln_disable_in_dashboard() { 
  142.  
  143. return apply_filters( 'bpln_disable_in_dashboard', false );//use this hook to disable notification in the backend 
.