[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/src/bp-xprofile/classes/ -> class-bp-xprofile-component.php (source)

   1  <?php
   2  /**
   3   * BuddyPress XProfile Loader.
   4   *
   5   * An extended profile component for users. This allows site admins to create
   6   * groups of fields for users to enter information about themselves.
   7   *
   8   * @package BuddyPress
   9   * @subpackage XProfileLoader
  10   * @since 1.5.0
  11   */
  12  
  13  // Exit if accessed directly.
  14  defined( 'ABSPATH' ) || exit;
  15  
  16  /**
  17   * Creates our XProfile component.
  18   *
  19   * @since 1.5.0
  20   */
  21  class BP_XProfile_Component extends BP_Component {
  22  
  23      /**
  24       * Profile field types.
  25       *
  26       * @since 1.5.0
  27       * @var array
  28       */
  29      public $field_types;
  30  
  31      /**
  32       * The acceptable visibility levels for xprofile fields.
  33       *
  34       * @see bp_xprofile_get_visibility_levels()
  35       *
  36       * @since 1.6.0
  37       * @var array
  38       */
  39      public $visibility_levels = array();
  40  
  41      /**
  42       * Start the xprofile component creation process.
  43       *
  44       * @since 1.5.0
  45       */
  46  	public function __construct() {
  47          parent::start(
  48              'xprofile',
  49              _x( 'Extended Profiles', 'Component page <title>', 'buddypress' ),
  50              buddypress()->plugin_dir,
  51              array(
  52                  'adminbar_myaccount_order' => 20
  53              )
  54          );
  55  
  56          $this->setup_hooks();
  57      }
  58  
  59      /**
  60       * Include files.
  61       *
  62       * @since 1.5.0
  63       *
  64       * @param array $includes Array of files to include.
  65       */
  66  	public function includes( $includes = array() ) {
  67          $includes = array(
  68              'cssjs',
  69              'cache',
  70              'caps',
  71              'filters',
  72              'template',
  73              'functions',
  74          );
  75  
  76          // Conditional includes.
  77          if ( bp_is_active( 'activity' ) ) {
  78              $includes[] = 'activity';
  79          }
  80          if ( bp_is_active( 'notifications' ) ) {
  81              $includes[] = 'notifications';
  82          }
  83          if ( bp_is_active( 'settings' ) ) {
  84              $includes[] = 'settings';
  85          }
  86          if ( is_admin() ) {
  87              $includes[] = 'admin';
  88          }
  89  
  90          parent::includes( $includes );
  91      }
  92  
  93      /**
  94       * Late includes method.
  95       *
  96       * Only load up certain code when on specific pages.
  97       *
  98       * @since 3.0.0
  99       */
 100  	public function late_includes() {
 101          // Bail if PHPUnit is running.
 102          if ( defined( 'BP_TESTS_DIR' ) ) {
 103              return;
 104          }
 105  
 106          // Bail if not on a user page.
 107          if ( ! bp_is_user() ) {
 108              return;
 109          }
 110  
 111          // User nav.
 112          if ( bp_is_profile_component() ) {
 113              require $this->path . 'bp-xprofile/screens/public.php';
 114  
 115              // Action - Delete avatar.
 116              if ( is_user_logged_in()&& bp_is_user_change_avatar() && bp_is_action_variable( 'delete-avatar', 0 ) ) {
 117                  require $this->path . 'bp-xprofile/actions/delete-avatar.php';
 118              }
 119  
 120              // Sub-nav items.
 121              if ( is_user_logged_in() &&
 122                  in_array( bp_current_action(), array( 'edit', 'change-avatar', 'change-cover-image' ), true )
 123              ) {
 124                  require $this->path . 'bp-xprofile/screens/' . bp_current_action() . '.php';
 125              }
 126          }
 127  
 128          // Settings.
 129          if ( is_user_logged_in() && bp_is_user_settings_profile() ) {
 130              require $this->path . 'bp-xprofile/screens/settings-profile.php';
 131          }
 132      }
 133  
 134      /**
 135       * Setup globals.
 136       *
 137       * The BP_XPROFILE_SLUG constant is deprecated, and only used here for
 138       * backwards compatibility.
 139       *
 140       * @since 1.5.0
 141       *
 142       * @param array $args Array of globals to set up.
 143       */
 144  	public function setup_globals( $args = array() ) {
 145          $bp = buddypress();
 146  
 147          // Define a slug, if necessary.
 148          if ( !defined( 'BP_XPROFILE_SLUG' ) ) {
 149              define( 'BP_XPROFILE_SLUG', 'profile' );
 150          }
 151  
 152          // Assign the base group and fullname field names to constants
 153          // to use in SQL statements.
 154          // Defined conditionally to accommodate unit tests.
 155          if ( ! defined( 'BP_XPROFILE_BASE_GROUP_NAME' ) ) {
 156              define( 'BP_XPROFILE_BASE_GROUP_NAME', stripslashes( bp_core_get_root_option( 'avatar_default' ) ) );
 157          }
 158  
 159          if ( ! defined( 'BP_XPROFILE_FULLNAME_FIELD_NAME' ) ) {
 160              define( 'BP_XPROFILE_FULLNAME_FIELD_NAME', stripslashes( bp_core_get_root_option( 'bp-xprofile-fullname-field-name' ) ) );
 161          }
 162  
 163          /**
 164           * Filters the supported field type IDs.
 165           *
 166           * @since 1.1.0
 167           *
 168           * @param array $value Array of IDs for the supported field types.
 169           */
 170          $this->field_types = apply_filters( 'xprofile_field_types', array_keys( bp_xprofile_get_field_types() ) );
 171  
 172          // 'option' is a special case. It is not a top-level field, so
 173          // does not have an associated BP_XProfile_Field_Type class,
 174          // but it must be whitelisted.
 175          $this->field_types[] = 'option';
 176  
 177          // Register the visibility levels. See bp_xprofile_get_visibility_levels() to filter.
 178          $this->visibility_levels = array(
 179              'public' => array(
 180                  'id'      => 'public',
 181                  'label' => _x( 'Everyone', 'Visibility level setting', 'buddypress' )
 182              ),
 183              'adminsonly' => array(
 184                  'id'      => 'adminsonly',
 185                  'label' => _x( 'Only Me', 'Visibility level setting', 'buddypress' )
 186              ),
 187              'loggedin' => array(
 188                  'id'      => 'loggedin',
 189                  'label' => _x( 'All Members', 'Visibility level setting', 'buddypress' )
 190              )
 191          );
 192  
 193          if ( bp_is_active( 'friends' ) ) {
 194              $this->visibility_levels['friends'] = array(
 195                  'id'    => 'friends',
 196                  'label'    => _x( 'My Friends', 'Visibility level setting', 'buddypress' )
 197              );
 198          }
 199  
 200          // Tables.
 201          $global_tables = array(
 202              'table_name_data'   => $bp->table_prefix . 'bp_xprofile_data',
 203              'table_name_groups' => $bp->table_prefix . 'bp_xprofile_groups',
 204              'table_name_fields' => $bp->table_prefix . 'bp_xprofile_fields',
 205              'table_name_meta'   => $bp->table_prefix . 'bp_xprofile_meta',
 206          );
 207  
 208          $meta_tables = array(
 209              'xprofile_group' => $bp->table_prefix . 'bp_xprofile_meta',
 210              'xprofile_field' => $bp->table_prefix . 'bp_xprofile_meta',
 211              'xprofile_data'  => $bp->table_prefix . 'bp_xprofile_meta',
 212          );
 213  
 214          $globals = array(
 215              'slug'                  => BP_XPROFILE_SLUG,
 216              'has_directory'         => false,
 217              'notification_callback' => 'xprofile_format_notifications',
 218              'global_tables'         => $global_tables,
 219              'meta_tables'           => $meta_tables,
 220          );
 221  
 222          parent::setup_globals( $globals );
 223      }
 224  
 225      /**
 226       * Set up navigation.
 227       *
 228       * @since 1.5.0
 229       *
 230       * @global BuddyPress $bp The one true BuddyPress instance
 231       *
 232       * @param array $main_nav Array of main nav items to set up.
 233       * @param array $sub_nav  Array of sub nav items to set up.
 234       */
 235  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 236  
 237          // Determine user to use.
 238          if ( bp_displayed_user_domain() ) {
 239              $user_domain = bp_displayed_user_domain();
 240          } elseif ( bp_loggedin_user_domain() ) {
 241              $user_domain = bp_loggedin_user_domain();
 242          } else {
 243              return;
 244          }
 245  
 246          $access       = bp_core_can_edit_settings();
 247          $slug         = bp_get_profile_slug();
 248          $profile_link = trailingslashit( $user_domain . $slug );
 249  
 250          // Add 'Profile' to the main navigation.
 251          $main_nav = array(
 252              'name'                => _x( 'Profile', 'Profile header menu', 'buddypress' ),
 253              'slug'                => $slug,
 254              'position'            => 20,
 255              'screen_function'     => 'xprofile_screen_display_profile',
 256              'default_subnav_slug' => 'public',
 257              'item_css_id'         => $this->id
 258          );
 259  
 260          // Add the subnav items to the profile.
 261          $sub_nav[] = array(
 262              'name'            => _x( 'View', 'Profile header sub menu', 'buddypress' ),
 263              'slug'            => 'public',
 264              'parent_url'      => $profile_link,
 265              'parent_slug'     => $slug,
 266              'screen_function' => 'xprofile_screen_display_profile',
 267              'position'        => 10
 268          );
 269  
 270          // Edit Profile.
 271          $sub_nav[] = array(
 272              'name'            => _x( 'Edit','Profile header sub menu', 'buddypress' ),
 273              'slug'            => 'edit',
 274              'parent_url'      => $profile_link,
 275              'parent_slug'     => $slug,
 276              'screen_function' => 'xprofile_screen_edit_profile',
 277              'position'        => 20,
 278              'user_has_access' => $access
 279          );
 280  
 281          // Change Avatar.
 282          if ( buddypress()->avatar->show_avatars ) {
 283              $sub_nav[] = array(
 284                  'name'            => _x( 'Change Profile Photo', 'Profile header sub menu', 'buddypress' ),
 285                  'slug'            => 'change-avatar',
 286                  'parent_url'      => $profile_link,
 287                  'parent_slug'     => $slug,
 288                  'screen_function' => 'xprofile_screen_change_avatar',
 289                  'position'        => 30,
 290                  'user_has_access' => $access
 291              );
 292          }
 293  
 294          // Change Cover image.
 295          if ( bp_displayed_user_use_cover_image_header() ) {
 296              $sub_nav[] = array(
 297                  'name'            => _x( 'Change Cover Image', 'Profile header sub menu', 'buddypress' ),
 298                  'slug'            => 'change-cover-image',
 299                  'parent_url'      => $profile_link,
 300                  'parent_slug'     => $slug,
 301                  'screen_function' => 'xprofile_screen_change_cover_image',
 302                  'position'        => 40,
 303                  'user_has_access' => $access
 304              );
 305          }
 306  
 307          // The Settings > Profile nav item can only be set up after
 308          // the Settings component has run its own nav routine.
 309          add_action( 'bp_settings_setup_nav', array( $this, 'setup_settings_nav' ) );
 310  
 311          parent::setup_nav( $main_nav, $sub_nav );
 312      }
 313  
 314      /**
 315       * Set up the Settings > Profile nav item.
 316       *
 317       * Loaded in a separate method because the Settings component may not
 318       * be loaded in time for BP_XProfile_Component::setup_nav().
 319       *
 320       * @since 2.1.0
 321       */
 322  	public function setup_settings_nav() {
 323          if ( ! bp_is_active( 'settings' ) ) {
 324              return;
 325          }
 326  
 327          // Determine user to use.
 328          if ( bp_displayed_user_domain() ) {
 329              $user_domain = bp_displayed_user_domain();
 330          } elseif ( bp_loggedin_user_domain() ) {
 331              $user_domain = bp_loggedin_user_domain();
 332          } else {
 333              return;
 334          }
 335  
 336          // Get the settings slug.
 337          $settings_slug = bp_get_settings_slug();
 338  
 339          bp_core_new_subnav_item( array(
 340              'name'            => _x( 'Profile Visibility', 'Profile settings sub nav', 'buddypress' ),
 341              'slug'            => 'profile',
 342              'parent_url'      => trailingslashit( $user_domain . $settings_slug ),
 343              'parent_slug'     => $settings_slug,
 344              'screen_function' => 'bp_xprofile_screen_settings',
 345              'position'        => 30,
 346              'user_has_access' => bp_core_can_edit_settings()
 347          ), 'members' );
 348      }
 349  
 350      /**
 351       * Set up the Admin Bar.
 352       *
 353       * @since 1.5.0
 354       *
 355       * @param array $wp_admin_nav Admin Bar items.
 356       */
 357  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 358  
 359          // Menus for logged in user.
 360          if ( is_user_logged_in() ) {
 361  
 362              // Profile link.
 363              $profile_link = trailingslashit( bp_loggedin_user_domain() . bp_get_profile_slug() );
 364  
 365              // Add the "Profile" sub menu.
 366              $wp_admin_nav[] = array(
 367                  'parent' => buddypress()->my_account_menu_id,
 368                  'id'     => 'my-account-' . $this->id,
 369                  'title'  => _x( 'Profile', 'My Account Profile', 'buddypress' ),
 370                  'href'   => $profile_link
 371              );
 372  
 373              // View Profile.
 374              $wp_admin_nav[] = array(
 375                  'parent'   => 'my-account-' . $this->id,
 376                  'id'       => 'my-account-' . $this->id . '-public',
 377                  'title'    => _x( 'View', 'My Account Profile sub nav', 'buddypress' ),
 378                  'href'     => $profile_link,
 379                  'position' => 10
 380              );
 381  
 382              // Edit Profile.
 383              $wp_admin_nav[] = array(
 384                  'parent'   => 'my-account-' . $this->id,
 385                  'id'       => 'my-account-' . $this->id . '-edit',
 386                  'title'    => _x( 'Edit', 'My Account Profile sub nav', 'buddypress' ),
 387                  'href'     => trailingslashit( $profile_link . 'edit' ),
 388                  'position' => 20
 389              );
 390  
 391              // Edit Avatar.
 392              if ( buddypress()->avatar->show_avatars ) {
 393                  $wp_admin_nav[] = array(
 394                      'parent'   => 'my-account-' . $this->id,
 395                      'id'       => 'my-account-' . $this->id . '-change-avatar',
 396                      'title'    => _x( 'Change Profile Photo', 'My Account Profile sub nav', 'buddypress' ),
 397                      'href'     => trailingslashit( $profile_link . 'change-avatar' ),
 398                      'position' => 30
 399                  );
 400              }
 401  
 402              if ( bp_displayed_user_use_cover_image_header() ) {
 403                  $wp_admin_nav[] = array(
 404                      'parent'   => 'my-account-' . $this->id,
 405                      'id'       => 'my-account-' . $this->id . '-change-cover-image',
 406                      'title'    => _x( 'Change Cover Image', 'My Account Profile sub nav', 'buddypress' ),
 407                      'href'     => trailingslashit( $profile_link . 'change-cover-image' ),
 408                      'position' => 40
 409                  );
 410              }
 411          }
 412  
 413          parent::setup_admin_bar( $wp_admin_nav );
 414      }
 415  
 416      /**
 417       * Add custom hooks.
 418       *
 419       * @since 2.0.0
 420       */
 421  	public function setup_hooks() {
 422          add_filter( 'bp_settings_admin_nav', array( $this, 'setup_settings_admin_nav' ), 2 );
 423      }
 424  
 425      /**
 426       * Sets up the title for pages and <title>.
 427       *
 428       * @since 1.5.0
 429       */
 430  	public function setup_title() {
 431  
 432          if ( bp_is_profile_component() ) {
 433              $bp = buddypress();
 434  
 435              if ( bp_is_my_profile() ) {
 436                  $bp->bp_options_title = _x( 'My Profile', 'Page title', 'buddypress' );
 437              } else {
 438                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 439                      'item_id' => bp_displayed_user_id(),
 440                      'type'    => 'thumb',
 441                      'alt'      => sprintf( _x( 'Profile picture of %s', 'Avatar alt', 'buddypress' ), bp_get_displayed_user_fullname() )
 442                  ) );
 443                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 444              }
 445          }
 446  
 447          parent::setup_title();
 448      }
 449  
 450      /**
 451       * Setup cache groups.
 452       *
 453       * @since 2.2.0
 454       */
 455  	public function setup_cache_groups() {
 456  
 457          // Global groups.
 458          wp_cache_add_global_groups( array(
 459              'bp_xprofile',
 460              'bp_xprofile_data',
 461              'bp_xprofile_fields',
 462              'bp_xprofile_groups',
 463              'xprofile_meta'
 464          ) );
 465  
 466          parent::setup_cache_groups();
 467      }
 468  
 469      /**
 470       * Adds "Settings > Profile" subnav item under the "Settings" adminbar menu.
 471       *
 472       * @since 2.0.0
 473       *
 474       * @param array $wp_admin_nav The settings adminbar nav array.
 475       * @return array
 476       */
 477  	public function setup_settings_admin_nav( $wp_admin_nav ) {
 478  
 479          // Setup the logged in user variables.
 480          $settings_link = trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() );
 481  
 482          // Add the "Profile" subnav item.
 483          $wp_admin_nav[] = array(
 484              'parent' => 'my-account-' . buddypress()->settings->id,
 485              'id'     => 'my-account-' . buddypress()->settings->id . '-profile',
 486              'title'  => _x( 'Profile', 'My Account Settings sub nav', 'buddypress' ),
 487              'href'   => trailingslashit( $settings_link . 'profile' )
 488          );
 489  
 490          return $wp_admin_nav;
 491      }
 492  
 493      /**
 494       * Init the BP REST API.
 495       *
 496       * @since 5.0.0
 497       *
 498       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 499       *                           description.
 500       */
 501  	public function rest_api_init( $controllers = array() ) {
 502          parent::rest_api_init( array(
 503              'BP_REST_XProfile_Fields_Endpoint',
 504              'BP_REST_XProfile_Field_Groups_Endpoint',
 505              'BP_REST_XProfile_Data_Endpoint',
 506              'BP_REST_Attachments_Member_Avatar_Endpoint',
 507          ) );
 508      }
 509  }


Generated: Mon Nov 18 01:01:37 2019 Cross-referenced by PHPXref 0.7.1