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

  1. <?php 
  2. /** 
  3. * BuddyPress Friends Streams Loader. 
  4. * 
  5. * The friends component is for users to create relationships with each other. 
  6. * 
  7. * @package BuddyPress 
  8. * @subpackage Friends 
  9. * @since 1.5.0 
  10. */ 
  11.  
  12. // Exit if accessed directly. 
  13. defined( 'ABSPATH' ) || exit; 
  14.  
  15. /** 
  16. * Defines the BuddyPress Friends Component. 
  17. * 
  18. * @since 1.5.0 
  19. */ 
  20. class BP_Friends_Component extends BP_Component { 
  21.  
  22. /** 
  23. * Start the friends component creation process. 
  24. * 
  25. * @since 1.5.0 
  26. */ 
  27. public function __construct() { 
  28. parent::start( 
  29. 'friends',  
  30. _x( 'Friend Connections', 'Friends screen page <title>', 'buddypress' ),  
  31. buddypress()->plugin_dir,  
  32. array( 
  33. 'adminbar_myaccount_order' => 60 
  34. ); 
  35.  
  36. /** 
  37. * Include bp-friends files. 
  38. * 
  39. * @since 1.5.0 
  40. * 
  41. * @see BP_Component::includes() for description of parameters. 
  42. * 
  43. * @param array $includes See {@link BP_Component::includes()}. 
  44. */ 
  45. public function includes( $includes = array() ) { 
  46. $includes = array( 
  47. 'cache',  
  48. 'actions',  
  49. 'screens',  
  50. 'filters',  
  51. 'activity',  
  52. 'template',  
  53. 'functions',  
  54. 'widgets',  
  55. ); 
  56.  
  57. // Conditional includes. 
  58. if ( bp_is_active( 'notifications' ) ) { 
  59. $includes[] = 'notifications'; 
  60.  
  61. parent::includes( $includes ); 
  62.  
  63. /** 
  64. * Set up bp-friends global settings. 
  65. * 
  66. * The BP_FRIENDS_SLUG constant is deprecated, and only used here for 
  67. * backwards compatibility. 
  68. * 
  69. * @since 1.5.0 
  70. * 
  71. * @see BP_Component::setup_globals() for description of parameters. 
  72. * 
  73. * @param array $args See {@link BP_Component::setup_globals()}. 
  74. */ 
  75. public function setup_globals( $args = array() ) { 
  76. $bp = buddypress(); 
  77.  
  78. // Deprecated. Do not use. 
  79. // Defined conditionally to support unit tests. 
  80. if ( ! defined( 'BP_FRIENDS_DB_VERSION' ) ) { 
  81. define( 'BP_FRIENDS_DB_VERSION', '1800' ); 
  82.  
  83. // Define a slug, if necessary. 
  84. if ( ! defined( 'BP_FRIENDS_SLUG' ) ) { 
  85. define( 'BP_FRIENDS_SLUG', $this->id ); 
  86.  
  87. // Global tables for the friends component. 
  88. $global_tables = array( 
  89. 'table_name' => $bp->table_prefix . 'bp_friends',  
  90. 'table_name_meta' => $bp->table_prefix . 'bp_friends_meta',  
  91. ); 
  92.  
  93. // All globals for the friends component. 
  94. // Note that global_tables is included in this array. 
  95. $args = array( 
  96. 'slug' => BP_FRIENDS_SLUG,  
  97. 'has_directory' => false,  
  98. 'search_string' => __( 'Search Friends...', 'buddypress' ),  
  99. 'notification_callback' => 'friends_format_notifications',  
  100. 'global_tables' => $global_tables 
  101. ); 
  102.  
  103. parent::setup_globals( $args ); 
  104.  
  105. /** 
  106. * Set up component navigation. 
  107. * 
  108. * @since 1.5.0 
  109. * 
  110. * @see BP_Component::setup_nav() for a description of arguments. 
  111. * 
  112. * @param array $main_nav Optional. See BP_Component::setup_nav() for 
  113. * description. 
  114. * @param array $sub_nav Optional. See BP_Component::setup_nav() for 
  115. * description. 
  116. */ 
  117. public function setup_nav( $main_nav = array(), $sub_nav = array() ) { 
  118.  
  119. // Determine user to use. 
  120. if ( bp_displayed_user_domain() ) { 
  121. $user_domain = bp_displayed_user_domain(); 
  122. } elseif ( bp_loggedin_user_domain() ) { 
  123. $user_domain = bp_loggedin_user_domain(); 
  124. } else { 
  125. return; 
  126.  
  127. $access = bp_core_can_edit_settings(); 
  128. $slug = bp_get_friends_slug(); 
  129. $friends_link = trailingslashit( $user_domain . $slug ); 
  130.  
  131. // Add 'Friends' to the main navigation. 
  132. $count = friends_get_total_friend_count(); 
  133. $class = ( 0 === $count ) ? 'no-count' : 'count'; 
  134.  
  135. $main_nav_name = sprintf( 
  136. /** translators: %s: Friend count for the current user */ 
  137. __( 'Friends %s', 'buddypress' ),  
  138. sprintf( 
  139. '<span class="%s">%s</span>',  
  140. esc_attr( $class ),  
  141. bp_core_number_format( $count ) 
  142. ); 
  143.  
  144. $main_nav = array( 
  145. 'name' => $main_nav_name,  
  146. 'slug' => $slug,  
  147. 'position' => 60,  
  148. 'screen_function' => 'friends_screen_my_friends',  
  149. 'default_subnav_slug' => 'my-friends',  
  150. 'item_css_id' => $this->id 
  151. ); 
  152.  
  153. // Add the subnav items to the friends nav item. 
  154. $sub_nav[] = array( 
  155. 'name' => _x( 'Friendships', 'Friends screen sub nav', 'buddypress' ),  
  156. 'slug' => 'my-friends',  
  157. 'parent_url' => $friends_link,  
  158. 'parent_slug' => $slug,  
  159. 'screen_function' => 'friends_screen_my_friends',  
  160. 'position' => 10,  
  161. 'item_css_id' => 'friends-my-friends' 
  162. ); 
  163.  
  164. $sub_nav[] = array( 
  165. 'name' => _x( 'Requests', 'Friends screen sub nav', 'buddypress' ),  
  166. 'slug' => 'requests',  
  167. 'parent_url' => $friends_link,  
  168. 'parent_slug' => $slug,  
  169. 'screen_function' => 'friends_screen_requests',  
  170. 'position' => 20,  
  171. 'user_has_access' => $access 
  172. ); 
  173.  
  174. parent::setup_nav( $main_nav, $sub_nav ); 
  175.  
  176. /** 
  177. * Set up bp-friends integration with the WordPress admin bar. 
  178. * 
  179. * @since 1.5.0 
  180. * 
  181. * @see BP_Component::setup_admin_bar() for a description of arguments. 
  182. * 
  183. * @param array $wp_admin_nav See BP_Component::setup_admin_bar() 
  184. * for description. 
  185. */ 
  186. public function setup_admin_bar( $wp_admin_nav = array() ) { 
  187.  
  188. // Menus for logged in user. 
  189. if ( is_user_logged_in() ) { 
  190.  
  191. // Setup the logged in user variables. 
  192. $friends_link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() ); 
  193.  
  194. // Pending friend requests. 
  195. $count = count( friends_get_friendship_request_user_ids( bp_loggedin_user_id() ) ); 
  196. if ( !empty( $count ) ) { 
  197. $title = sprintf( 
  198. /** translators: %s: Pending friend request count for the current user */ 
  199. _x( 'Friends %s', 'My Account Friends menu', 'buddypress' ),  
  200. '<span class="count">' . bp_core_number_format( $count ) . '</span>' 
  201. ); 
  202. $pending = sprintf( 
  203. /** translators: %s: Pending friend request count for the current user */ 
  204. _x( 'Pending Requests %s', 'My Account Friends menu sub nav', 'buddypress' ),  
  205. '<span class="count">' . bp_core_number_format( $count ) . '</span>' 
  206. ); 
  207. } else { 
  208. $title = _x( 'Friends', 'My Account Friends menu', 'buddypress' ); 
  209. $pending = _x( 'No Pending Requests', 'My Account Friends menu sub nav', 'buddypress' ); 
  210.  
  211. // Add the "My Account" sub menus. 
  212. $wp_admin_nav[] = array( 
  213. 'parent' => buddypress()->my_account_menu_id,  
  214. 'id' => 'my-account-' . $this->id,  
  215. 'title' => $title,  
  216. 'href' => $friends_link 
  217. ); 
  218.  
  219. // My Friends. 
  220. $wp_admin_nav[] = array( 
  221. 'parent' => 'my-account-' . $this->id,  
  222. 'id' => 'my-account-' . $this->id . '-friendships',  
  223. 'title' => _x( 'Friendships', 'My Account Friends menu sub nav', 'buddypress' ),  
  224. 'href' => $friends_link,  
  225. 'position' => 10 
  226. ); 
  227.  
  228. // Requests. 
  229. $wp_admin_nav[] = array( 
  230. 'parent' => 'my-account-' . $this->id,  
  231. 'id' => 'my-account-' . $this->id . '-requests',  
  232. 'title' => $pending,  
  233. 'href' => trailingslashit( $friends_link . 'requests' ),  
  234. 'position' => 20 
  235. ); 
  236.  
  237. parent::setup_admin_bar( $wp_admin_nav ); 
  238.  
  239. /** 
  240. * Set up the title for pages and <title>. 
  241. * 
  242. * @since 1.5.0 
  243. */ 
  244. public function setup_title() { 
  245.  
  246. // Adjust title. 
  247. if ( bp_is_friends_component() ) { 
  248. $bp = buddypress(); 
  249.  
  250. if ( bp_is_my_profile() ) { 
  251. $bp->bp_options_title = __( 'Friendships', 'buddypress' ); 
  252. } else { 
  253. $bp->bp_options_avatar = bp_core_fetch_avatar( array( 
  254. 'item_id' => bp_displayed_user_id(),  
  255. 'type' => 'thumb',  
  256. 'alt' => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() ) 
  257. ) ); 
  258. $bp->bp_options_title = bp_get_displayed_user_fullname(); 
  259.  
  260. parent::setup_title(); 
  261.  
  262. /** 
  263. * Setup cache groups. 
  264. * 
  265. * @since 2.2.0 
  266. */ 
  267. public function setup_cache_groups() { 
  268.  
  269. // Global groups. 
  270. wp_cache_add_global_groups( array( 
  271. 'bp_friends_requests',  
  272. 'bp_friends_friendships', // Individual friendship objects are cached here by ID. 
  273. 'bp_friends_friendships_for_user' // All friendship IDs for a single user. 
  274. ) ); 
  275.  
  276. parent::setup_cache_groups(); 
.