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