BP_Friends_Component
Defines the BuddyBoss Connections Component.
Description
Source
File: bp-friends/classes/class-bp-friends-component.php
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 | class BP_Friends_Component extends BP_Component { /** * Start the friends component creation process. * * @since BuddyPress 1.5.0 */ public function __construct() { parent::start( 'friends' , __( 'Connections' , 'buddyboss' ), buddypress()->plugin_dir, array ( 'adminbar_myaccount_order' => 60 ) ); } /** * Include bp-friends files. * * @since BuddyPress 1.5.0 * * @see BP_Component::includes() for description of parameters. * * @param array $includes See {@link BP_Component::includes()}. */ public function includes( $includes = array () ) { $includes = array ( 'cache' , 'filters' , 'template' , 'functions' , 'widgets' , ); // Conditional includes. if ( bp_is_active( 'activity' ) ) { $includes [] = 'activity' ; } if ( bp_is_active( 'notifications' ) ) { $includes [] = 'notifications' ; } parent::includes( $includes ); } /** * Late includes method. * * Only load up certain code when on specific pages. * * @since BuddyPress 3.0.0 */ public function late_includes() { // Bail if PHPUnit is running. if ( defined( 'BP_TESTS_DIR' ) ) { return ; } // Connections. if ( bp_is_user_friends() ) { // Authenticated actions. if ( is_user_logged_in() && in_array( bp_current_action(), array ( 'add-friend' , 'remove-friend' ), true ) ) { require $this ->path . 'bp-friends/actions/' . bp_current_action() . '.php' ; } // User nav. require $this ->path . 'bp-friends/screens/my-friends.php' ; if ( is_user_logged_in() && bp_is_user_friend_requests() ) { require $this ->path . 'bp-friends/screens/requests.php' ; } if ( is_user_logged_in() ) { require $this ->path . 'bp-friends/screens/mutual-friends.php' ; } } } /** * Set up bp-friends global settings. * * The BP_FRIENDS_SLUG constant is deprecated, and only used here for * backwards compatibility. * * @since BuddyPress 1.5.0 * * @see BP_Component::setup_globals() for description of parameters. * * @param array $args See {@link BP_Component::setup_globals()}. */ public function setup_globals( $args = array () ) { $bp = buddypress(); // Deprecated. Do not use. // Defined conditionally to support unit tests. if ( ! defined( 'BP_FRIENDS_DB_VERSION' ) ) { define( 'BP_FRIENDS_DB_VERSION' , '1800' ); } // Define a slug, if necessary. if ( ! defined( 'BP_FRIENDS_SLUG' ) ) { define( 'BP_FRIENDS_SLUG' , $this ->id ); } // Global tables for the friends component. $global_tables = array ( 'table_name' => $bp ->table_prefix . 'bp_friends' , 'table_name_meta' => $bp ->table_prefix . 'bp_friends_meta' , ); // All globals for the friends component. // Note that global_tables is included in this array. $args = array ( 'slug' => BP_FRIENDS_SLUG, 'has_directory' => false, 'search_string' => __( 'Search Connections…' , 'buddyboss' ), 'notification_callback' => 'friends_format_notifications' , 'global_tables' => $global_tables ); parent::setup_globals( $args ); } /** * Set up component navigation. * * @since BuddyPress 1.5.0 * * @see BP_Component::setup_nav() for a description of arguments. * * @param array $main_nav Optional. See BP_Component::setup_nav() for * description. * @param array $sub_nav Optional. See BP_Component::setup_nav() for * description. */ public function setup_nav( $main_nav = array (), $sub_nav = array () ) { // Determine user to use. if ( bp_displayed_user_domain() ) { $user_domain = bp_displayed_user_domain(); } elseif ( bp_loggedin_user_domain() ) { $user_domain = bp_loggedin_user_domain(); } else { return ; } $access = bp_core_can_edit_settings(); $slug = bp_get_friends_slug(); $friends_link = trailingslashit( $user_domain . $slug ); // Add 'Connections' to the main navigation. $count = friends_get_total_friend_count(); $class = ( 0 === $count ) ? 'no-count' : 'count' ; $main_nav_name = sprintf( /* translators: %s: Connection count for the current user */ __( 'Connections %s' , 'buddyboss' ), sprintf( '<span class="%s">%s</span>' , esc_attr( $class ), bp_core_number_format( $count ) ) ); $main_nav = array ( 'name' => $main_nav_name , 'slug' => $slug , 'position' => 60, 'screen_function' => 'friends_screen_my_friends' , 'default_subnav_slug' => 'my-friends' , 'item_css_id' => $this ->id ); // Add the subnav items to the friends nav item. $sub_nav [] = array ( 'name' => ! bp_is_my_profile() ? __( 'All Connections' , 'buddyboss' ) : __( 'My Connections' , 'buddyboss' ), 'slug' => 'my-friends' , 'parent_url' => $friends_link , 'parent_slug' => $slug , 'screen_function' => 'friends_screen_my_friends' , 'position' => 10, 'item_css_id' => 'friends-my-friends' ); if ( bp_get_mutual_friendships() ) { $sub_nav [] = array ( 'name' => __( 'Mutual Connections' , 'buddyboss' ), 'slug' => 'mutual' , 'parent_url' => $friends_link , 'parent_slug' => $slug , 'screen_function' => 'friends_screen_mutual_friends' , 'position' => 20, 'user_has_access' => ! bp_is_my_profile() && bp_loggedin_user_id() ); } $sub_nav [] = array ( 'name' => __( 'Requests' , 'buddyboss' ), 'slug' => 'requests' , 'parent_url' => $friends_link , 'parent_slug' => $slug , 'screen_function' => 'friends_screen_requests' , 'position' => 30, 'user_has_access' => $access ); parent::setup_nav( $main_nav , $sub_nav ); } /** * Set up bp-friends integration with the WordPress admin bar. * * @since BuddyPress 1.5.0 * * @see BP_Component::setup_admin_bar() for a description of arguments. * * @param array $wp_admin_nav See BP_Component::setup_admin_bar() * for description. */ public function setup_admin_bar( $wp_admin_nav = array () ) { // Menus for logged in user. if ( is_user_logged_in() ) { // Setup the logged in user variables. $friends_link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() ); // Pending friend requests. $count = count ( friends_get_friendship_request_user_ids( bp_loggedin_user_id() ) ); if ( ! empty ( $count ) ) { $title = sprintf( /* translators: %s: Pending friend request count for the current user */ __( 'Connections %s' , 'buddyboss' ), '<span class="count">' . bp_core_number_format( $count ) . '</span>' ); $pending = sprintf( /* translators: %s: Pending friend request count for the current user */ __( 'Pending Requests %s' , 'buddyboss' ), '<span class="count">' . bp_core_number_format( $count ) . '</span>' ); } else { $title = __( 'Connections' , 'buddyboss' ); $pending = __( 'No Pending Requests' , 'buddyboss' ); } // Add the "My Account" sub menus. $wp_admin_nav [] = array ( 'parent' => buddypress()->my_account_menu_id, 'id' => 'my-account-' . $this ->id, 'title' => $title , 'href' => $friends_link ); // My Connections. $wp_admin_nav [] = array ( 'parent' => 'my-account-' . $this ->id, 'id' => 'my-account-' . $this ->id . '-friendships' , 'title' => __( 'My Connections' , 'buddyboss' ), 'href' => $friends_link , 'position' => 10 ); // Requests. $wp_admin_nav [] = array ( 'parent' => 'my-account-' . $this ->id, 'id' => 'my-account-' . $this ->id . '-requests' , 'title' => $pending , 'href' => trailingslashit( $friends_link . 'requests' ), 'position' => 20 ); } parent::setup_admin_bar( $wp_admin_nav ); } /** * Set up the title for pages and <title>. * * @since BuddyPress 1.5.0 */ public function setup_title() { // Adjust title. if ( bp_is_friends_component() ) { $bp = buddypress(); if ( bp_is_my_profile() ) { $bp ->bp_options_title = __( 'Connections' , 'buddyboss' ); } else { $bp ->bp_options_avatar = bp_core_fetch_avatar( array ( 'item_id' => bp_displayed_user_id(), 'type' => 'thumb' , 'alt' => sprintf( __( 'Profile photo of %s' , 'buddyboss' ), bp_get_displayed_user_fullname() ) ) ); $bp ->bp_options_title = bp_get_displayed_user_fullname(); } } parent::setup_title(); } /** * Setup cache groups. * * @since BuddyPress 2.2.0 */ public function setup_cache_groups() { // Global groups. wp_cache_add_global_groups( array ( 'bp_friends_requests' , 'bp_friends_friendships' , // Individual friendship objects are cached here by ID. 'bp_friends_friendships_for_user' // All friendship IDs for a single user. ) ); parent::setup_cache_groups(); } } |
Changelog
Version | Description |
---|---|
BuddyPress 1.5.0 | Introduced. |
Methods
- __construct — Start the friends component creation process.
- includes — Include bp-friends files.
- late_includes — Late includes method.
- rest_api_init — Init the BuddyBoss REST API.
- setup_admin_bar — Set up bp-friends integration with the WordPress admin bar.
- setup_cache_groups — Setup cache groups.
- setup_globals — Set up bp-friends global settings.
- setup_nav — Set up component navigation.
- setup_title — Set up the title for pages and .
Questions?
We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.