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