/bp-messages/classes/class-bp-messages-component.php

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