BP_XProfile_Component
Creates our XProfile component.
Description
Source
File: bp-xprofile/classes/class-bp-xprofile-component.php
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 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 | class BP_XProfile_Component extends BP_Component { /** * Profile field types. * * @since BuddyPress 1.5.0 * @var array */ public $field_types ; /** * The acceptable visibility levels for xprofile fields. * * @see bp_xprofile_get_visibility_levels() * * @since BuddyPress 1.6.0 * @var array */ public $visibility_levels = array (); /** * Start the xprofile component creation process. * * @since BuddyPress 1.5.0 */ public function __construct() { parent::start( 'xprofile' , __( 'Profile Fields' , 'buddyboss' ), buddypress()->plugin_dir, array ( 'adminbar_myaccount_order' => 20 ) ); $this ->setup_hooks(); } /** * Include files. * * @since BuddyPress 1.5.0 * * @param array $includes Array of files to include. */ public function includes( $includes = array () ) { $includes = array ( 'cssjs' , 'cache' , 'caps' , 'filters' , 'template' , 'functions' , 'repeaters' , ); // Conditional includes. if ( bp_is_active( 'activity' ) ) { $includes [] = 'activity' ; } if ( bp_is_active( 'notifications' ) ) { $includes [] = 'notifications' ; } if ( bp_is_active( 'settings' ) ) { $includes [] = 'settings' ; } if ( is_admin() ) { $includes [] = 'admin' ; } 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 ; } // Bail if not on a user page. if ( ! bp_is_user() ) { return ; } // User nav. if ( bp_is_profile_component() ) { require $this ->path . 'bp-xprofile/screens/public.php' ; // Action - Delete avatar. if ( is_user_logged_in()&& bp_is_user_change_avatar() && bp_is_action_variable( 'delete-avatar' , 0 ) ) { require $this ->path . 'bp-xprofile/actions/delete-avatar.php' ; } // Sub-nav items. if ( is_user_logged_in() && in_array( bp_current_action(), array ( 'edit' , 'change-avatar' , 'change-cover-image' ), true ) ) { require $this ->path . 'bp-xprofile/screens/' . bp_current_action() . '.php' ; } } // Settings. if ( is_user_logged_in() && bp_is_user_settings_profile() ) { require $this ->path . 'bp-xprofile/screens/settings-profile.php' ; } } /** * Setup globals. * * The BP_XPROFILE_SLUG constant is deprecated, and only used here for * backwards compatibility. * * @since BuddyPress 1.5.0 * * @param array $args Array of globals to set up. */ public function setup_globals( $args = array () ) { $bp = buddypress(); // Define a slug, if necessary. if ( !defined( 'BP_XPROFILE_SLUG' ) ) { define( 'BP_XPROFILE_SLUG' , 'profile' ); } // Assign the base group and fullname field names to constants // to use in SQL statements. // Defined conditionally to accommodate unit tests. if ( ! defined( 'BP_XPROFILE_BASE_GROUP_NAME' ) ) { define( 'BP_XPROFILE_BASE_GROUP_NAME' , stripslashes ( bp_core_get_root_option( 'avatar_default' ) ) ); } if ( ! defined( 'BP_XPROFILE_FULLNAME_FIELD_NAME' ) ) { define( 'BP_XPROFILE_FULLNAME_FIELD_NAME' , stripslashes ( bp_core_get_root_option( 'bp-xprofile-fullname-field-name' ) ) ); } /** * Filters the supported field type IDs. * * @since BuddyPress 1.1.0 * * @param array $value Array of IDs for the supported field types. */ $this ->field_types = apply_filters( 'xprofile_field_types' , array_keys ( bp_xprofile_get_field_types() ) ); // 'option' is a special case. It is not a top-level field, so // does not have an associated BP_XProfile_Field_Type class, // but it must be whitelisted. $this ->field_types[] = 'option' ; // Register the visibility levels. See bp_xprofile_get_visibility_levels() to filter. $this ->visibility_levels = array ( 'public' => array ( 'id' => 'public' , 'label' => __( 'Public' , 'buddyboss' ) ), 'adminsonly' => array ( 'id' => 'adminsonly' , 'label' => __( 'Only Me' , 'buddyboss' ) ), 'loggedin' => array ( 'id' => 'loggedin' , 'label' => __( 'All Members' , 'buddyboss' ) ) ); if ( bp_is_active( 'friends' ) ) { $this ->visibility_levels[ 'friends' ] = array ( 'id' => 'friends' , 'label' => __( 'My Connections' , 'buddyboss' ) ); } // Tables. $global_tables = array ( 'table_name_data' => $bp ->table_prefix . 'bp_xprofile_data' , 'table_name_groups' => $bp ->table_prefix . 'bp_xprofile_groups' , 'table_name_fields' => $bp ->table_prefix . 'bp_xprofile_fields' , 'table_name_meta' => $bp ->table_prefix . 'bp_xprofile_meta' , ); $meta_tables = array ( 'xprofile_group' => $bp ->table_prefix . 'bp_xprofile_meta' , 'xprofile_field' => $bp ->table_prefix . 'bp_xprofile_meta' , 'xprofile_data' => $bp ->table_prefix . 'bp_xprofile_meta' , ); $globals = array ( 'slug' => BP_XPROFILE_SLUG, 'has_directory' => false, 'notification_callback' => 'xprofile_format_notifications' , 'global_tables' => $global_tables , 'meta_tables' => $meta_tables , ); parent::setup_globals( $globals ); } /** * Set up navigation. * * @since BuddyPress 1.5.0 * * @global BuddyPress $bp The one true BuddyPress instance * * @param array $main_nav Array of main nav items to set up. * @param array $sub_nav Array of sub nav items to set up. */ 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_profile_slug(); $profile_link = trailingslashit( $user_domain . $slug ); // Add 'Profile' to the main navigation. $main_nav = array ( 'name' => __( 'Profile' , 'buddyboss' ), 'slug' => $slug , 'position' => 20, 'screen_function' => 'xprofile_screen_display_profile' , 'default_subnav_slug' => 'public' , 'item_css_id' => $this ->id ); // Add the subnav items to the profile. $sub_nav [] = array ( 'name' => __( 'View' , 'buddyboss' ), 'slug' => 'public' , 'parent_url' => $profile_link , 'parent_slug' => $slug , 'screen_function' => 'xprofile_screen_display_profile' , 'position' => 10 ); // Edit Profile. $sub_nav [] = array ( 'name' => __( 'Edit' , 'buddyboss' ), 'slug' => 'edit' , 'parent_url' => $profile_link , 'parent_slug' => $slug , 'screen_function' => 'xprofile_screen_edit_profile' , 'position' => 20, 'user_has_access' => $access ); // Change Avatar. if ( buddypress()->avatar->show_avatars ) { $sub_nav [] = array ( 'name' => __( 'Profile Photo' , 'buddyboss' ), 'slug' => 'change-avatar' , 'parent_url' => $profile_link , 'parent_slug' => $slug , 'screen_function' => 'xprofile_screen_change_avatar' , 'position' => 30, 'user_has_access' => $access ); } // Change cover photo. if ( bp_displayed_user_use_cover_image_header() ) { $sub_nav [] = array ( 'name' => __( 'Cover Photo' , 'buddyboss' ), 'slug' => 'change-cover-image' , 'parent_url' => $profile_link , 'parent_slug' => $slug , 'screen_function' => 'xprofile_screen_change_cover_image' , 'position' => 40, 'user_has_access' => $access ); } // The Settings > Profile nav item can only be set up after // the Settings component has run its own nav routine. add_action( 'bp_settings_setup_nav' , array ( $this , 'setup_settings_nav' ) ); parent::setup_nav( $main_nav , $sub_nav ); } /** * Set up the Settings > Profile nav item. * * Loaded in a separate method because the Settings component may not * be loaded in time for BP_XProfile_Component::setup_nav(). * * @since BuddyPress 2.1.0 */ public function setup_settings_nav() { if ( ! bp_is_active( 'settings' ) ) { return ; } // 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 ; } // Get the settings slug. $settings_slug = bp_get_settings_slug(); bp_core_new_subnav_item( array ( 'name' => __( 'Privacy' , 'buddyboss' ), 'slug' => 'profile' , 'parent_url' => trailingslashit( $user_domain . $settings_slug ), 'parent_slug' => $settings_slug , 'screen_function' => 'bp_xprofile_screen_settings' , 'position' => 30, 'user_has_access' => bp_core_can_edit_settings() ), 'members' ); } /** * Set up the Admin Bar. * * @since BuddyPress 1.5.0 * * @param array $wp_admin_nav Admin Bar items. */ public function setup_admin_bar( $wp_admin_nav = array () ) { // Menus for logged in user. if ( is_user_logged_in() ) { // Profile link. $profile_link = trailingslashit( bp_loggedin_user_domain() . bp_get_profile_slug() ); // Add the "Profile" sub menu. $wp_admin_nav [] = array ( 'parent' => buddypress()->my_account_menu_id, 'id' => 'my-account-' . $this ->id, 'title' => __( 'Profile' , 'buddyboss' ), 'href' => $profile_link ); // View Profile. $wp_admin_nav [] = array ( 'parent' => 'my-account-' . $this ->id, 'id' => 'my-account-' . $this ->id . '-public' , 'title' => __( 'View' , 'buddyboss' ), 'href' => $profile_link , 'position' => 10 ); // Edit Profile. $wp_admin_nav [] = array ( 'parent' => 'my-account-' . $this ->id, 'id' => 'my-account-' . $this ->id . '-edit' , 'title' => __( 'Edit' , 'buddyboss' ), 'href' => trailingslashit( $profile_link . 'edit' ), 'position' => 20 ); // Edit Avatar. if ( buddypress()->avatar->show_avatars ) { $wp_admin_nav [] = array ( 'parent' => 'my-account-' . $this ->id, 'id' => 'my-account-' . $this ->id . '-change-avatar' , 'title' => __( 'Profile Photo' , 'buddyboss' ), 'href' => trailingslashit( $profile_link . 'change-avatar' ), 'position' => 30 ); } if ( bp_displayed_user_use_cover_image_header() ) { $wp_admin_nav [] = array ( 'parent' => 'my-account-' . $this ->id, 'id' => 'my-account-' . $this ->id . '-change-cover-image' , 'title' => __( 'Cover Photo' , 'buddyboss' ), 'href' => trailingslashit( $profile_link . 'change-cover-image' ), 'position' => 40 ); } } parent::setup_admin_bar( $wp_admin_nav ); } /** * Add custom hooks. * * @since BuddyPress 2.0.0 */ public function setup_hooks() { add_filter( 'bp_settings_admin_nav' , array ( $this , 'setup_settings_admin_nav' ), 2 ); } /** * Sets up the title for pages and <title>. * * @since BuddyPress 1.5.0 */ public function setup_title() { if ( bp_is_profile_component() ) { $bp = buddypress(); if ( bp_is_my_profile() ) { $bp ->bp_options_title = __( 'My Profile' , '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_xprofile' , 'bp_xprofile_data' , 'bp_xprofile_fields' , 'bp_xprofile_groups' , 'xprofile_meta' ) ); parent::setup_cache_groups(); } /** * Adds "Settings > Profile" subnav item under the "Settings" adminbar menu. * * @since BuddyPress 2.0.0 * * @param array $wp_admin_nav The settings adminbar nav array. * @return array */ public function setup_settings_admin_nav( $wp_admin_nav ) { // Setup the logged in user variables. $settings_link = trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() ); // Add the "Profile" subnav item. $wp_admin_nav [] = array ( 'parent' => 'my-account-' . buddypress()->settings->id, 'id' => 'my-account-' . buddypress()->settings->id . '-profile' , 'title' => __( 'Privacy' , 'buddyboss' ), 'href' => trailingslashit( $settings_link . 'profile' ), 'position' => 30 ); return $wp_admin_nav ; } } |
Changelog
Version | Description |
---|---|
BuddyPress 1.5.0 | Introduced. |
Methods
- __construct — Start the xprofile component creation process.
- includes — Include files.
- late_includes — Late includes method.
- rest_api_init — Init the BuddyBoss REST API.
- setup_admin_bar — Set up the Admin Bar.
- setup_cache_groups — Setup cache groups.
- setup_globals — Setup globals.
- setup_hooks — Add custom hooks.
- setup_nav — Set up navigation.
- setup_settings_admin_nav — Adds "Settings > Profile" subnav item under the "Settings" adminbar menu.
- setup_settings_nav — Set up the Settings > Profile nav item.
- setup_title — Sets 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.