BP_Friends_Component

Defines the BuddyPress Friends Component.

Defined (1)

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

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