Advertisement
Guest User

Untitled

a guest
Jun 14th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.54 KB | None | 0 0
  1. /* Buddypress Notifications in menu item */
  2. if (!function_exists('kleo_add_notifications_nav_item')) {
  3. function kleo_add_notifications_nav_item($menu_items)
  4. {
  5. $menu_items[] = array(
  6. 'name' => esc_html__('Live Notifications', 'buddyapp'),
  7. 'slug' => 'notifications',
  8. 'link' => '#',
  9. );
  10.  
  11. return $menu_items;
  12. }
  13. add_filter('kleo_nav_menu_items', 'kleo_add_notifications_nav_item');
  14. }
  15.  
  16. /* Localize refresh interval to JavaScript app.js */
  17. if (!function_exists('kleo_bp_notif_refresh_int')) {
  18. function kleo_bp_notif_refresh_int($data)
  19. {
  20. $data['bpAjaxRefresh'] = sq_option('bp_notif_interval', 30000);
  21.  
  22. return $data;
  23. }
  24. add_filter('kleo_localize_app', 'kleo_bp_notif_refresh_int');
  25. }
  26.  
  27. if (!function_exists(kleo_setup_notifications_nav)) {
  28. function kleo_setup_notifications_nav($menu_item)
  29. {
  30. $menu_item->classes[] = 'has-submenu kleo-notifications';
  31. if (!is_user_logged_in()) {
  32. $menu_item->_invalid = true;
  33. } else {
  34. add_filter('walker_nav_menu_start_el_notifications', 'kleo_menu_notifications', 10, 4);
  35. }
  36.  
  37. return $menu_item;
  38. }
  39. add_filter('kleo_setup_nav_item_notifications', 'kleo_setup_notifications_nav');
  40. }
  41.  
  42.  
  43. if (!function_exists(kleo_menu_notifications)) {
  44. function kleo_menu_notifications($item_output = '', $item = null, $depth = 1, $args = null)
  45. {
  46.  
  47. if (!isset($item)) {
  48. $item = new stdClass();
  49. $item->title = esc_html__('Notifications', 'buddyapp');
  50. $item->attr_title = esc_html__('Notifications', 'buddyapp');
  51. $item->icon = 'notification-line';
  52. }
  53. if (!isset($args)) {
  54. $args = new stdClass();
  55. $args->theme_location = 'top-left';
  56. }
  57. $output = '';
  58. $url = bp_loggedin_user_domain() . BP_NOTIFICATIONS_SLUG;
  59. $notifications = bp_notifications_get_notifications_for_user(bp_loggedin_user_id(), 'object');
  60. $count = !empty($notifications) ? count($notifications) : 0;
  61.  
  62. if ($count > 0) {
  63. $alert = 'new-alert';
  64. $status = ' has-notif';
  65. } else {
  66. $alert = 'no-alert';
  67. $status = '';
  68. }
  69. $attr_title = strip_tags($item->attr_title);
  70.  
  71. $title_icon = kleo_get_menu_icon($item->icon, $depth, $args, 'notification-line');
  72.  
  73. $title_icon = '<i class="icon-' . $title_icon . '"></i> ';
  74.  
  75. $output .= '<a href="' . $url . '" title="' . $attr_title . '">'
  76. . $title_icon
  77. . ' <b class="bubble ' . $alert . '">' . $count . '</b>'
  78. . '<span>' . $item->title . '</span>'
  79. . '</a>'
  80. . '<em class="menu-arrow"></em>';
  81.  
  82. $output .= '<ul class="submenu' . $status . '">';
  83.  
  84. if (!empty($notifications)) {
  85. foreach ((array)$notifications as $notification) {
  86. $output .= '<li class="kleo-submenu-item" id="kleo-notification-' . $notification->id . '">';
  87. $output .= '<a href="' . $notification->href . '">' . $notification->content . '</a>';
  88. $output .= '</li>';
  89. }
  90. } else {
  91. $output .= '<li class="kleo-submenu-item"><span>' . esc_html__('No new notifications', 'buddyapp') . '</span></li>';
  92. }
  93.  
  94. if (!empty($notifications)) {
  95. $style = '';
  96. } else {
  97. $style = ' style="display: none;"';
  98. }
  99. $output .= '<li class="footer-item"' . $style . '><a class="btn btn-link mark-as-read" href="#">' . esc_html__('Mark all as read', 'buddyapp') . '</a></li>';
  100.  
  101. $output .= '</ul>';
  102.  
  103. return $output;
  104. }
  105. }
  106.  
  107. /* Mark notifications as read by AJAX */
  108. if (!function_exists(kleo_bp_notification_mark_read)) {
  109. function kleo_bp_notification_mark_read()
  110. {
  111. $response = array();
  112.  
  113. if (BP_Notifications_Notification::mark_all_for_user(bp_loggedin_user_id())) {
  114. $response['status'] = 'success';
  115. } else {
  116. $response['status'] = 'failure';
  117. }
  118.  
  119. $notifications = bp_notifications_get_notifications_for_user(bp_loggedin_user_id(), 'object');
  120. $count = !empty($notifications) ? count($notifications) : 0;
  121. $response['count'] = $count;
  122. $response['empty'] = '<li class="kleo-submenu-item">' . esc_html__('No new notifications', 'buddyapp') . '</li>';
  123.  
  124. echo json_encode($response);
  125. exit;
  126. }
  127. add_action('wp_ajax_kleo_bp_notification_mark_read', 'kleo_bp_notification_mark_read');
  128. }
  129.  
  130.  
  131. if (!function_exists(kleo_bp_notifications_refresh)) {
  132.  
  133. function kleo_bp_notifications_refresh($response)
  134. {
  135.  
  136. if (!isset($_GET['current_notifications'])) {
  137. $response['statusNotif'] = 'failure';
  138. return $response;
  139. }
  140.  
  141. $old_count = (int)$_GET['current_notifications'];
  142.  
  143. $notifications = bp_notifications_get_notifications_for_user(bp_loggedin_user_id(), 'object');
  144. $count = !empty($notifications) ? count($notifications) : 0;
  145.  
  146. if ($count == $old_count) {
  147. $response['statusNotif'] = 'no_change';
  148. return $response;
  149. }
  150.  
  151. $output = '';
  152.  
  153. if (!empty($notifications)) {
  154. foreach ((array)$notifications as $notification) {
  155. $output .= '<li class="kleo-submenu-item" id="kleo-notification-' . $notification->id . '">';
  156. $output .= '<a href="' . $notification->href . '">' . $notification->content . '</a>';
  157. $output .= '</li>';
  158. }
  159. } else {
  160. $output .= '<li class="kleo-submenu-item">' . esc_html__('No new notifications', 'buddyapp') . '</li>';
  161. }
  162. $response['dataNotif'] = $output;
  163. $response['countNotif'] = $count;
  164. $response['statusNotif'] = 'success';
  165.  
  166. return $response;
  167. }
  168. add_filter('kleo_bp_ajax_call', 'kleo_bp_notifications_refresh');
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement