BP_Notifications_Component

Extends the component class to set up the Notifications component.

Defined (1)

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

/bp-notifications/classes/class-bp-notifications-component.php  
  1. class BP_Notifications_Component extends BP_Component { 
  2.  
  3. /** 
  4. * Start the notifications component creation process. 
  5. * @since 1.9.0 
  6. */ 
  7. public function __construct() { 
  8. parent::start( 
  9. 'notifications',  
  10. _x( 'Notifications', 'Page <title>', 'buddypress' ),  
  11. buddypress()->plugin_dir,  
  12. array( 
  13. 'adminbar_myaccount_order' => 30 
  14. ); 
  15.  
  16. /** 
  17. * Include notifications component files. 
  18. * @since 1.9.0 
  19. * @see BP_Component::includes() for a description of arguments. 
  20. * @param array $includes See BP_Component::includes() for a description. 
  21. */ 
  22. public function includes( $includes = array() ) { 
  23. $includes = array( 
  24. 'actions',  
  25. 'screens',  
  26. 'adminbar',  
  27. 'template',  
  28. 'functions',  
  29. 'cache',  
  30. ); 
  31.  
  32. parent::includes( $includes ); 
  33.  
  34. /** 
  35. * Set up component global data. 
  36. * @since 1.9.0 
  37. * @see BP_Component::setup_globals() for a description of arguments. 
  38. * @param array $args See BP_Component::setup_globals() for a description. 
  39. */ 
  40. public function setup_globals( $args = array() ) { 
  41. $bp = buddypress(); 
  42.  
  43. // Define a slug, if necessary. 
  44. if ( ! defined( 'BP_NOTIFICATIONS_SLUG' ) ) { 
  45. define( 'BP_NOTIFICATIONS_SLUG', $this->id ); 
  46.  
  47. // Global tables for the notifications component. 
  48. $global_tables = array( 
  49. 'table_name' => $bp->table_prefix . 'bp_notifications',  
  50. 'table_name_meta' => $bp->table_prefix . 'bp_notifications_meta',  
  51. ); 
  52.  
  53. // Metadata tables for notifications component. 
  54. $meta_tables = array( 
  55. 'notification' => $bp->table_prefix . 'bp_notifications_meta',  
  56. ); 
  57.  
  58. // All globals for the notifications component. 
  59. // Note that global_tables is included in this array. 
  60. $args = array( 
  61. 'slug' => BP_NOTIFICATIONS_SLUG,  
  62. 'has_directory' => false,  
  63. 'search_string' => __( 'Search Notifications...', 'buddypress' ),  
  64. 'global_tables' => $global_tables,  
  65. 'meta_tables' => $meta_tables 
  66. ); 
  67.  
  68. parent::setup_globals( $args ); 
  69.  
  70. /** 
  71. * Set up component navigation. 
  72. * @since 1.9.0 
  73. * @see BP_Component::setup_nav() for a description of arguments. 
  74. * @param array $main_nav Optional. See BP_Component::setup_nav() for 
  75. * description. 
  76. * @param array $sub_nav Optional. See BP_Component::setup_nav() for 
  77. * description. 
  78. */ 
  79. public function setup_nav( $main_nav = array(), $sub_nav = array() ) { 
  80.  
  81. // Determine user to use. 
  82. if ( bp_displayed_user_domain() ) { 
  83. $user_domain = bp_displayed_user_domain(); 
  84. } elseif ( bp_loggedin_user_domain() ) { 
  85. $user_domain = bp_loggedin_user_domain(); 
  86. } else { 
  87. return; 
  88.  
  89. $access = bp_core_can_edit_settings(); 
  90. $slug = bp_get_notifications_slug(); 
  91. $notifications_link = trailingslashit( $user_domain . $slug ); 
  92.  
  93. // Only grab count if we're on a user page and current user has access. 
  94. if ( bp_is_user() && bp_user_has_access() ) { 
  95. $count = bp_notifications_get_unread_notification_count( bp_displayed_user_id() ); 
  96. $class = ( 0 === $count ) ? 'no-count' : 'count'; 
  97. $nav_name = sprintf( 
  98. /** translators: %s: Unread notification count for the current user */ 
  99. _x( 'Notifications %s', 'Profile screen nav', 'buddypress' ),  
  100. sprintf( 
  101. '<span class="%s">%s</span>',  
  102. esc_attr( $class ),  
  103. bp_core_number_format( $count ) 
  104. ); 
  105. } else { 
  106. $nav_name = _x( 'Notifications', 'Profile screen nav', 'buddypress' ); 
  107.  
  108. // Add 'Notifications' to the main navigation. 
  109. $main_nav = array( 
  110. 'name' => $nav_name,  
  111. 'slug' => $slug,  
  112. 'position' => 30,  
  113. 'show_for_displayed_user' => $access,  
  114. 'screen_function' => 'bp_notifications_screen_unread',  
  115. 'default_subnav_slug' => 'unread',  
  116. 'item_css_id' => $this->id,  
  117. ); 
  118.  
  119. // Add the subnav items to the notifications nav item. 
  120. $sub_nav[] = array( 
  121. 'name' => _x( 'Unread', 'Notification screen nav', 'buddypress' ),  
  122. 'slug' => 'unread',  
  123. 'parent_url' => $notifications_link,  
  124. 'parent_slug' => $slug,  
  125. 'screen_function' => 'bp_notifications_screen_unread',  
  126. 'position' => 10,  
  127. 'item_css_id' => 'notifications-my-notifications',  
  128. 'user_has_access' => $access,  
  129. ); 
  130.  
  131. $sub_nav[] = array( 
  132. 'name' => _x( 'Read', 'Notification screen nav', 'buddypress' ),  
  133. 'slug' => 'read',  
  134. 'parent_url' => $notifications_link,  
  135. 'parent_slug' => $slug,  
  136. 'screen_function' => 'bp_notifications_screen_read',  
  137. 'position' => 20,  
  138. 'user_has_access' => $access,  
  139. ); 
  140.  
  141. parent::setup_nav( $main_nav, $sub_nav ); 
  142.  
  143. /** 
  144. * Set up the component entries in the WordPress Admin Bar. 
  145. * @since 1.9.0 
  146. * @see BP_Component::setup_nav() for a description of the $wp_admin_nav 
  147. * parameter array. 
  148. * @param array $wp_admin_nav See BP_Component::setup_admin_bar() for a 
  149. * description. 
  150. */ 
  151. public function setup_admin_bar( $wp_admin_nav = array() ) { 
  152.  
  153. // Menus for logged in user. 
  154. if ( is_user_logged_in() ) { 
  155.  
  156. // Setup the logged in user variables. 
  157. $notifications_link = trailingslashit( bp_loggedin_user_domain() . bp_get_notifications_slug() ); 
  158.  
  159. // Pending notification requests. 
  160. $count = bp_notifications_get_unread_notification_count( bp_loggedin_user_id() ); 
  161. if ( ! empty( $count ) ) { 
  162. $title = sprintf( 
  163. /** translators: %s: Unread notification count for the current user */ 
  164. _x( 'Notifications %s', 'My Account Notification pending', 'buddypress' ),  
  165. '<span class="count">' . bp_core_number_format( $count ) . '</span>' 
  166. ); 
  167. $unread = sprintf( 
  168. /** translators: %s: Unread notification count for the current user */ 
  169. _x( 'Unread %s', 'My Account Notification pending', 'buddypress' ),  
  170. '<span class="count">' . bp_core_number_format( $count ) . '</span>' 
  171. ); 
  172. } else { 
  173. $title = _x( 'Notifications', 'My Account Notification', 'buddypress' ); 
  174. $unread = _x( 'Unread', 'My Account Notification sub nav', 'buddypress' ); 
  175.  
  176. // Add the "My Account" sub menus. 
  177. $wp_admin_nav[] = array( 
  178. 'parent' => buddypress()->my_account_menu_id,  
  179. 'id' => 'my-account-' . $this->id,  
  180. 'title' => $title,  
  181. 'href' => $notifications_link 
  182. ); 
  183.  
  184. // Unread. 
  185. $wp_admin_nav[] = array( 
  186. 'parent' => 'my-account-' . $this->id,  
  187. 'id' => 'my-account-' . $this->id . '-unread',  
  188. 'title' => $unread,  
  189. 'href' => $notifications_link,  
  190. 'position' => 10 
  191. ); 
  192.  
  193. // Read. 
  194. $wp_admin_nav[] = array( 
  195. 'parent' => 'my-account-' . $this->id,  
  196. 'id' => 'my-account-' . $this->id . '-read',  
  197. 'title' => _x( 'Read', 'My Account Notification sub nav', 'buddypress' ),  
  198. 'href' => trailingslashit( $notifications_link . 'read' ),  
  199. 'position' => 20 
  200. ); 
  201.  
  202. parent::setup_admin_bar( $wp_admin_nav ); 
  203.  
  204. /** 
  205. * Set up the title for pages and <title>. 
  206. * @since 1.9.0 
  207. */ 
  208. public function setup_title() { 
  209.  
  210. // Adjust title. 
  211. if ( bp_is_notifications_component() ) { 
  212. $bp = buddypress(); 
  213.  
  214. if ( bp_is_my_profile() ) { 
  215. $bp->bp_options_title = __( 'Notifications', 'buddypress' ); 
  216. } else { 
  217. $bp->bp_options_avatar = bp_core_fetch_avatar( array( 
  218. 'item_id' => bp_displayed_user_id(),  
  219. 'type' => 'thumb',  
  220. 'alt' => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() ) 
  221. ) ); 
  222. $bp->bp_options_title = bp_get_displayed_user_fullname(); 
  223.  
  224. parent::setup_title(); 
  225.  
  226. /** 
  227. * Setup cache groups. 
  228. * @since 2.2.0 
  229. */ 
  230. public function setup_cache_groups() { 
  231.  
  232. // Global groups. 
  233. wp_cache_add_global_groups( array( 
  234. 'bp_notifications',  
  235. 'notification_meta' 
  236. ) ); 
  237.  
  238. parent::setup_cache_groups();