BP_Messages_Component

Implementation of BP_Component for the Messages component.

Defined (1)

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

/bp-messages/classes/class-bp-messages-component.php  
  1. class BP_Messages_Component extends BP_Component { 
  2.  
  3. /** 
  4. * If this is true, the Message autocomplete will return friends only, unless 
  5. * this is set to false, in which any matching users will be returned. 
  6. * @since 1.5.0 
  7. * @var bool 
  8. */ 
  9. public $autocomplete_all; 
  10.  
  11. /** 
  12. * Start the messages component creation process. 
  13. * @since 1.5.0 
  14. */ 
  15. public function __construct() { 
  16. parent::start( 
  17. 'messages',  
  18. __( 'Private Messages', 'buddypress' ),  
  19. buddypress()->plugin_dir,  
  20. array( 
  21. 'adminbar_myaccount_order' => 50,  
  22. 'features' => array( 'star' ) 
  23. ); 
  24.  
  25. /** 
  26. * Include files. 
  27. * @since 1.5.0 
  28. * @param array $includes See {BP_Component::includes()} for details. 
  29. */ 
  30. public function includes( $includes = array() ) { 
  31.  
  32. // Files to include. 
  33. $includes = array( 
  34. 'cssjs',  
  35. 'cache',  
  36. 'actions',  
  37. 'screens',  
  38. 'filters',  
  39. 'template',  
  40. 'functions',  
  41. 'widgets',  
  42. ); 
  43.  
  44. // Conditional includes. 
  45. if ( bp_is_active( 'notifications' ) ) { 
  46. $includes[] = 'notifications'; 
  47. if ( bp_is_active( $this->id, 'star' ) ) { 
  48. $includes[] = 'star'; 
  49.  
  50. parent::includes( $includes ); 
  51.  
  52. /** 
  53. * Set up globals for the Messages component. 
  54. * The BP_MESSAGES_SLUG constant is deprecated, and only used here for 
  55. * backwards compatibility. 
  56. * @since 1.5.0 
  57. * @param array $args Not used. 
  58. */ 
  59. public function setup_globals( $args = array() ) { 
  60. $bp = buddypress(); 
  61.  
  62. // Define a slug, if necessary. 
  63. if ( ! defined( 'BP_MESSAGES_SLUG' ) ) { 
  64. define( 'BP_MESSAGES_SLUG', $this->id ); 
  65.  
  66. // Global tables for messaging component. 
  67. $global_tables = array( 
  68. 'table_name_notices' => $bp->table_prefix . 'bp_messages_notices',  
  69. 'table_name_messages' => $bp->table_prefix . 'bp_messages_messages',  
  70. 'table_name_recipients' => $bp->table_prefix . 'bp_messages_recipients',  
  71. 'table_name_meta' => $bp->table_prefix . 'bp_messages_meta',  
  72. ); 
  73.  
  74. // Metadata tables for messaging component. 
  75. $meta_tables = array( 
  76. 'message' => $bp->table_prefix . 'bp_messages_meta',  
  77. ); 
  78.  
  79. $this->autocomplete_all = defined( 'BP_MESSAGES_AUTOCOMPLETE_ALL' ); 
  80.  
  81. // All globals for messaging component. 
  82. // Note that global_tables is included in this array. 
  83. parent::setup_globals( array( 
  84. 'slug' => BP_MESSAGES_SLUG,  
  85. 'has_directory' => false,  
  86. 'notification_callback' => 'messages_format_notifications',  
  87. 'search_string' => __( 'Search Messages...', 'buddypress' ),  
  88. 'global_tables' => $global_tables,  
  89. 'meta_tables' => $meta_tables 
  90. ) ); 
  91.  
  92. /** 
  93. * Set up navigation for user pages. 
  94. * @param array $main_nav See {BP_Component::setup_nav()} for details. 
  95. * @param array $sub_nav See {BP_Component::setup_nav()} for details. 
  96. */ 
  97. public function setup_nav( $main_nav = array(), $sub_nav = array() ) { 
  98.  
  99. // Determine user to use. 
  100. if ( bp_displayed_user_domain() ) { 
  101. $user_domain = bp_displayed_user_domain(); 
  102. } elseif ( bp_loggedin_user_domain() ) { 
  103. $user_domain = bp_loggedin_user_domain(); 
  104. } else { 
  105. return; 
  106.  
  107. $access = bp_core_can_edit_settings(); 
  108. $slug = bp_get_messages_slug(); 
  109. $messages_link = trailingslashit( $user_domain . $slug ); 
  110.  
  111. // Only grab count if we're on a user page and current user has access. 
  112. if ( bp_is_user() && bp_user_has_access() ) { 
  113. $count = bp_get_total_unread_messages_count(); 
  114. $class = ( 0 === $count ) ? 'no-count' : 'count'; 
  115. $nav_name = sprintf( 
  116. /** translators: %s: Unread message count for the current user */ 
  117. __( 'Messages %s', 'buddypress' ),  
  118. sprintf( 
  119. '<span class="%s">%s</span>',  
  120. esc_attr( $class ),  
  121. bp_core_number_format( $count ) 
  122. ); 
  123. } else { 
  124. $nav_name = __( 'Messages', 'buddypress' ); 
  125.  
  126. // Add 'Messages' to the main navigation. 
  127. $main_nav = array( 
  128. 'name' => $nav_name,  
  129. 'slug' => $slug,  
  130. 'position' => 50,  
  131. 'show_for_displayed_user' => $access,  
  132. 'screen_function' => 'messages_screen_inbox',  
  133. 'default_subnav_slug' => 'inbox',  
  134. 'item_css_id' => $this->id 
  135. ); 
  136.  
  137. // Add the subnav items to the profile. 
  138. $sub_nav[] = array( 
  139. 'name' => __( 'Inbox', 'buddypress' ),  
  140. 'slug' => 'inbox',  
  141. 'parent_url' => $messages_link,  
  142. 'parent_slug' => $slug,  
  143. 'screen_function' => 'messages_screen_inbox',  
  144. 'position' => 10,  
  145. 'user_has_access' => $access 
  146. ); 
  147.  
  148. if ( bp_is_active( $this->id, 'star' ) ) { 
  149. $sub_nav[] = array( 
  150. 'name' => __( 'Starred', 'buddypress' ),  
  151. 'slug' => bp_get_messages_starred_slug(),  
  152. 'parent_url' => $messages_link,  
  153. 'parent_slug' => $slug,  
  154. 'screen_function' => 'bp_messages_star_screen',  
  155. 'position' => 11,  
  156. 'user_has_access' => $access 
  157. ); 
  158.  
  159. $sub_nav[] = array( 
  160. 'name' => __( 'Sent', 'buddypress' ),  
  161. 'slug' => 'sentbox',  
  162. 'parent_url' => $messages_link,  
  163. 'parent_slug' => $slug,  
  164. 'screen_function' => 'messages_screen_sentbox',  
  165. 'position' => 20,  
  166. 'user_has_access' => $access 
  167. ); 
  168.  
  169. $sub_nav[] = array( 
  170. 'name' => __( 'Compose', 'buddypress' ),  
  171. 'slug' => 'compose',  
  172. 'parent_url' => $messages_link,  
  173. 'parent_slug' => $slug,  
  174. 'screen_function' => 'messages_screen_compose',  
  175. 'position' => 30,  
  176. 'user_has_access' => $access 
  177. ); 
  178.  
  179. if ( bp_current_user_can( 'bp_moderate' ) ) { 
  180. $sub_nav[] = array( 
  181. 'name' => __( 'Notices', 'buddypress' ),  
  182. 'slug' => 'notices',  
  183. 'parent_url' => $messages_link,  
  184. 'parent_slug' => $slug,  
  185. 'screen_function' => 'messages_screen_notices',  
  186. 'position' => 90,  
  187. 'user_has_access' => true 
  188. ); 
  189.  
  190. parent::setup_nav( $main_nav, $sub_nav ); 
  191.  
  192. /** 
  193. * Set up the Toolbar. 
  194. * @param array $wp_admin_nav See {BP_Component::setup_admin_bar()} for details. 
  195. */ 
  196. public function setup_admin_bar( $wp_admin_nav = array() ) { 
  197.  
  198. // Menus for logged in user. 
  199. if ( is_user_logged_in() ) { 
  200.  
  201. // Setup the logged in user variables. 
  202. $messages_link = trailingslashit( bp_loggedin_user_domain() . bp_get_messages_slug() ); 
  203.  
  204. // Unread message count. 
  205. $count = messages_get_unread_count(); 
  206. if ( !empty( $count ) ) { 
  207. $title = sprintf( 
  208. /** translators: %s: Unread message count for the current user */ 
  209. __( 'Messages %s', 'buddypress' ),  
  210. '<span class="count">' . bp_core_number_format( $count ) . '</span>' 
  211. ); 
  212. $inbox = sprintf( 
  213. /** translators: %s: Unread message count for the current user */ 
  214. __( 'Inbox %s', 'buddypress' ),  
  215. '<span class="count">' . bp_core_number_format( $count ) . '</span>' 
  216. ); 
  217. } else { 
  218. $title = __( 'Messages', 'buddypress' ); 
  219. $inbox = __( 'Inbox', 'buddypress' ); 
  220.  
  221. // Add main Messages menu. 
  222. $wp_admin_nav[] = array( 
  223. 'parent' => buddypress()->my_account_menu_id,  
  224. 'id' => 'my-account-' . $this->id,  
  225. 'title' => $title,  
  226. 'href' => $messages_link 
  227. ); 
  228.  
  229. // Inbox. 
  230. $wp_admin_nav[] = array( 
  231. 'parent' => 'my-account-' . $this->id,  
  232. 'id' => 'my-account-' . $this->id . '-inbox',  
  233. 'title' => $inbox,  
  234. 'href' => $messages_link,  
  235. 'position' => 10 
  236. ); 
  237.  
  238. // Starred. 
  239. if ( bp_is_active( $this->id, 'star' ) ) { 
  240. $wp_admin_nav[] = array( 
  241. 'parent' => 'my-account-' . $this->id,  
  242. 'id' => 'my-account-' . $this->id . '-starred',  
  243. 'title' => __( 'Starred', 'buddypress' ),  
  244. 'href' => trailingslashit( $messages_link . bp_get_messages_starred_slug() ),  
  245. 'position' => 11 
  246. ); 
  247.  
  248. // Sent Messages. 
  249. $wp_admin_nav[] = array( 
  250. 'parent' => 'my-account-' . $this->id,  
  251. 'id' => 'my-account-' . $this->id . '-sentbox',  
  252. 'title' => __( 'Sent', 'buddypress' ),  
  253. 'href' => trailingslashit( $messages_link . 'sentbox' ),  
  254. 'position' => 20 
  255. ); 
  256.  
  257. // Compose Message. 
  258. $wp_admin_nav[] = array( 
  259. 'parent' => 'my-account-' . $this->id,  
  260. 'id' => 'my-account-' . $this->id . '-compose',  
  261. 'title' => __( 'Compose', 'buddypress' ),  
  262. 'href' => trailingslashit( $messages_link . 'compose' ),  
  263. 'position' => 30 
  264. ); 
  265.  
  266. // Site Wide Notices. 
  267. if ( bp_current_user_can( 'bp_moderate' ) ) { 
  268. $wp_admin_nav[] = array( 
  269. 'parent' => 'my-account-' . $this->id,  
  270. 'id' => 'my-account-' . $this->id . '-notices',  
  271. 'title' => __( 'All Member Notices', 'buddypress' ),  
  272. 'href' => trailingslashit( $messages_link . 'notices' ),  
  273. 'position' => 90 
  274. ); 
  275.  
  276. parent::setup_admin_bar( $wp_admin_nav ); 
  277.  
  278. /** 
  279. * Set up the title for pages and <title>. 
  280. */ 
  281. public function setup_title() { 
  282.  
  283. if ( bp_is_messages_component() ) { 
  284. $bp = buddypress(); 
  285.  
  286. if ( bp_is_my_profile() ) { 
  287. $bp->bp_options_title = __( 'My Messages', 'buddypress' ); 
  288. } else { 
  289. $bp->bp_options_avatar = bp_core_fetch_avatar( array( 
  290. 'item_id' => bp_displayed_user_id(),  
  291. 'type' => 'thumb',  
  292. 'alt' => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() ) 
  293. ) ); 
  294. $bp->bp_options_title = bp_get_displayed_user_fullname(); 
  295.  
  296. parent::setup_title(); 
  297.  
  298. /** 
  299. * Setup cache groups 
  300. * @since 2.2.0 
  301. */ 
  302. public function setup_cache_groups() { 
  303.  
  304. // Global groups. 
  305. wp_cache_add_global_groups( array( 
  306. 'bp_messages',  
  307. 'bp_messages_threads',  
  308. 'bp_messages_unread_count',  
  309. 'message_meta' 
  310. ) ); 
  311.  
  312. parent::setup_cache_groups();