[ 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              // Sub-nav items.
 116              if ( is_user_logged_in() && 'edit' === bp_current_action() ) {
 117                  require $this->path . 'bp-xprofile/screens/edit.php';
 118              }
 119          }
 120  
 121          // Settings.
 122          if ( is_user_logged_in() && bp_is_user_settings_profile() ) {
 123              require $this->path . 'bp-xprofile/screens/settings-profile.php';
 124          }
 125      }
 126  
 127      /**
 128       * Setup globals.
 129       *
 130       * The BP_XPROFILE_SLUG constant is deprecated, and only used here for
 131       * backwards compatibility.
 132       *
 133       * @since 1.5.0
 134       *
 135       * @param array $args Array of globals to set up.
 136       */
 137  	public function setup_globals( $args = array() ) {
 138          $bp = buddypress();
 139  
 140          // Define a slug, if necessary.
 141          if ( !defined( 'BP_XPROFILE_SLUG' ) ) {
 142              define( 'BP_XPROFILE_SLUG', 'profile' );
 143          }
 144  
 145          // Assign the base group and fullname field names to constants
 146          // to use in SQL statements.
 147          // Defined conditionally to accommodate unit tests.
 148          if ( ! defined( 'BP_XPROFILE_BASE_GROUP_NAME' ) ) {
 149              define( 'BP_XPROFILE_BASE_GROUP_NAME', stripslashes( bp_core_get_root_option( 'avatar_default' ) ) );
 150          }
 151  
 152          if ( ! defined( 'BP_XPROFILE_FULLNAME_FIELD_NAME' ) ) {
 153              define( 'BP_XPROFILE_FULLNAME_FIELD_NAME', stripslashes( bp_core_get_root_option( 'bp-xprofile-fullname-field-name' ) ) );
 154          }
 155  
 156          /**
 157           * Filters the supported field type IDs.
 158           *
 159           * @since 1.1.0
 160           *
 161           * @param array $value Array of IDs for the supported field types.
 162           */
 163          $this->field_types = apply_filters( 'xprofile_field_types', array_keys( bp_xprofile_get_field_types() ) );
 164  
 165          // 'option' is a special case. It is not a top-level field, so
 166          // does not have an associated BP_XProfile_Field_Type class,
 167          // but it must be whitelisted.
 168          $this->field_types[] = 'option';
 169  
 170          // Register the visibility levels. See bp_xprofile_get_visibility_levels() to filter.
 171          $this->visibility_levels = array(
 172              'public' => array(
 173                  'id'      => 'public',
 174                  'label' => _x( 'Everyone', 'Visibility level setting', 'buddypress' )
 175              ),
 176              'adminsonly' => array(
 177                  'id'      => 'adminsonly',
 178                  'label' => _x( 'Only Me', 'Visibility level setting', 'buddypress' )
 179              ),
 180              'loggedin' => array(
 181                  'id'      => 'loggedin',
 182                  'label' => _x( 'All Members', 'Visibility level setting', 'buddypress' )
 183              )
 184          );
 185  
 186          if ( bp_is_active( 'friends' ) ) {
 187              $this->visibility_levels['friends'] = array(
 188                  'id'    => 'friends',
 189                  'label'    => _x( 'My Friends', 'Visibility level setting', 'buddypress' )
 190              );
 191          }
 192  
 193          // Tables.
 194          $global_tables = array(
 195              'table_name_data'   => $bp->table_prefix . 'bp_xprofile_data',
 196              'table_name_groups' => $bp->table_prefix . 'bp_xprofile_groups',
 197              'table_name_fields' => $bp->table_prefix . 'bp_xprofile_fields',
 198              'table_name_meta'   => $bp->table_prefix . 'bp_xprofile_meta',
 199          );
 200  
 201          $meta_tables = array(
 202              'xprofile_group' => $bp->table_prefix . 'bp_xprofile_meta',
 203              'xprofile_field' => $bp->table_prefix . 'bp_xprofile_meta',
 204              'xprofile_data'  => $bp->table_prefix . 'bp_xprofile_meta',
 205          );
 206  
 207          $globals = array(
 208              'slug'                  => BP_XPROFILE_SLUG,
 209              'has_directory'         => false,
 210              'notification_callback' => 'xprofile_format_notifications',
 211              'global_tables'         => $global_tables,
 212              'meta_tables'           => $meta_tables,
 213          );
 214  
 215          parent::setup_globals( $globals );
 216      }
 217  
 218      /**
 219       * Set up navigation.
 220       *
 221       * @since 1.5.0
 222       *
 223       * @global BuddyPress $bp The one true BuddyPress instance
 224       *
 225       * @param array $main_nav Array of main nav items to set up.
 226       * @param array $sub_nav  Array of sub nav items to set up.
 227       */
 228  	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
 229  
 230          // Determine user to use.
 231          if ( bp_displayed_user_domain() ) {
 232              $user_domain = bp_displayed_user_domain();
 233          } elseif ( bp_loggedin_user_domain() ) {
 234              $user_domain = bp_loggedin_user_domain();
 235          } else {
 236              return;
 237          }
 238  
 239          $access       = bp_core_can_edit_settings();
 240          $slug         = bp_get_profile_slug();
 241          $profile_link = trailingslashit( $user_domain . $slug );
 242  
 243          // Add 'Profile' to the main navigation.
 244          $main_nav = array(
 245              'name'                => _x( 'Profile', 'Profile header menu', 'buddypress' ),
 246              'slug'                => $slug,
 247              'position'            => 20,
 248              'screen_function'     => 'xprofile_screen_display_profile',
 249              'default_subnav_slug' => 'public',
 250              'item_css_id'         => $this->id
 251          );
 252  
 253          // Add the subnav items to the profile.
 254          $sub_nav[] = array(
 255              'name'            => _x( 'View', 'Profile header sub menu', 'buddypress' ),
 256              'slug'            => 'public',
 257              'parent_url'      => $profile_link,
 258              'parent_slug'     => $slug,
 259              'screen_function' => 'xprofile_screen_display_profile',
 260              'position'        => 10
 261          );
 262  
 263          // Edit Profile.
 264          $sub_nav[] = array(
 265              'name'            => _x( 'Edit','Profile header sub menu', 'buddypress' ),
 266              'slug'            => 'edit',
 267              'parent_url'      => $profile_link,
 268              'parent_slug'     => $slug,
 269              'screen_function' => 'xprofile_screen_edit_profile',
 270              'position'        => 20,
 271              'user_has_access' => $access
 272          );
 273  
 274          // The Settings > Profile nav item can only be set up after
 275          // the Settings component has run its own nav routine.
 276          add_action( 'bp_settings_setup_nav', array( $this, 'setup_settings_nav' ) );
 277  
 278          parent::setup_nav( $main_nav, $sub_nav );
 279      }
 280  
 281      /**
 282       * Set up the Settings > Profile nav item.
 283       *
 284       * Loaded in a separate method because the Settings component may not
 285       * be loaded in time for BP_XProfile_Component::setup_nav().
 286       *
 287       * @since 2.1.0
 288       */
 289  	public function setup_settings_nav() {
 290          if ( ! bp_is_active( 'settings' ) ) {
 291              return;
 292          }
 293  
 294          // Determine user to use.
 295          if ( bp_displayed_user_domain() ) {
 296              $user_domain = bp_displayed_user_domain();
 297          } elseif ( bp_loggedin_user_domain() ) {
 298              $user_domain = bp_loggedin_user_domain();
 299          } else {
 300              return;
 301          }
 302  
 303          // Get the settings slug.
 304          $settings_slug = bp_get_settings_slug();
 305  
 306          bp_core_new_subnav_item( array(
 307              'name'            => _x( 'Profile Visibility', 'Profile settings sub nav', 'buddypress' ),
 308              'slug'            => 'profile',
 309              'parent_url'      => trailingslashit( $user_domain . $settings_slug ),
 310              'parent_slug'     => $settings_slug,
 311              'screen_function' => 'bp_xprofile_screen_settings',
 312              'position'        => 30,
 313              'user_has_access' => bp_core_can_edit_settings()
 314          ), 'members' );
 315      }
 316  
 317      /**
 318       * Set up the Admin Bar.
 319       *
 320       * @since 1.5.0
 321       *
 322       * @param array $wp_admin_nav Admin Bar items.
 323       */
 324  	public function setup_admin_bar( $wp_admin_nav = array() ) {
 325  
 326          // Menus for logged in user.
 327          if ( is_user_logged_in() ) {
 328  
 329              // Profile link.
 330              $profile_link = trailingslashit( bp_loggedin_user_domain() . bp_get_profile_slug() );
 331  
 332              // Add the "Profile" sub menu.
 333              $wp_admin_nav[] = array(
 334                  'parent' => buddypress()->my_account_menu_id,
 335                  'id'     => 'my-account-' . $this->id,
 336                  'title'  => _x( 'Profile', 'My Account Profile', 'buddypress' ),
 337                  'href'   => $profile_link
 338              );
 339  
 340              // View Profile.
 341              $wp_admin_nav[] = array(
 342                  'parent'   => 'my-account-' . $this->id,
 343                  'id'       => 'my-account-' . $this->id . '-public',
 344                  'title'    => _x( 'View', 'My Account Profile sub nav', 'buddypress' ),
 345                  'href'     => $profile_link,
 346                  'position' => 10
 347              );
 348  
 349              // Edit Profile.
 350              $wp_admin_nav[] = array(
 351                  'parent'   => 'my-account-' . $this->id,
 352                  'id'       => 'my-account-' . $this->id . '-edit',
 353                  'title'    => _x( 'Edit', 'My Account Profile sub nav', 'buddypress' ),
 354                  'href'     => trailingslashit( $profile_link . 'edit' ),
 355                  'position' => 20
 356              );
 357          }
 358  
 359          parent::setup_admin_bar( $wp_admin_nav );
 360      }
 361  
 362      /**
 363       * Add custom hooks.
 364       *
 365       * @since 2.0.0
 366       */
 367  	public function setup_hooks() {
 368          add_filter( 'bp_settings_admin_nav', array( $this, 'setup_settings_admin_nav' ), 2 );
 369      }
 370  
 371      /**
 372       * Sets up the title for pages and <title>.
 373       *
 374       * @since 1.5.0
 375       */
 376  	public function setup_title() {
 377  
 378          if ( bp_is_profile_component() ) {
 379              $bp = buddypress();
 380  
 381              if ( bp_is_my_profile() ) {
 382                  $bp->bp_options_title = _x( 'My Profile', 'Page title', 'buddypress' );
 383              } else {
 384                  $bp->bp_options_avatar = bp_core_fetch_avatar( array(
 385                      'item_id' => bp_displayed_user_id(),
 386                      'type'    => 'thumb',
 387  
 388                      /* translators: %s: member name */
 389                      'alt'      => sprintf( _x( 'Profile picture of %s', 'Avatar alt', 'buddypress' ), bp_get_displayed_user_fullname() )
 390                  ) );
 391                  $bp->bp_options_title = bp_get_displayed_user_fullname();
 392              }
 393          }
 394  
 395          parent::setup_title();
 396      }
 397  
 398      /**
 399       * Setup cache groups.
 400       *
 401       * @since 2.2.0
 402       */
 403  	public function setup_cache_groups() {
 404  
 405          // Global groups.
 406          wp_cache_add_global_groups( array(
 407              'bp_xprofile',
 408              'bp_xprofile_data',
 409              'bp_xprofile_fields',
 410              'bp_xprofile_groups',
 411              'xprofile_meta'
 412          ) );
 413  
 414          parent::setup_cache_groups();
 415      }
 416  
 417      /**
 418       * Adds "Settings > Profile" subnav item under the "Settings" adminbar menu.
 419       *
 420       * @since 2.0.0
 421       *
 422       * @param array $wp_admin_nav The settings adminbar nav array.
 423       * @return array
 424       */
 425  	public function setup_settings_admin_nav( $wp_admin_nav ) {
 426  
 427          // Setup the logged in user variables.
 428          $settings_link = trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() );
 429  
 430          // Add the "Profile" subnav item.
 431          $wp_admin_nav[] = array(
 432              'parent' => 'my-account-' . buddypress()->settings->id,
 433              'id'     => 'my-account-' . buddypress()->settings->id . '-profile',
 434              'title'  => _x( 'Profile', 'My Account Settings sub nav', 'buddypress' ),
 435              'href'   => trailingslashit( $settings_link . 'profile' )
 436          );
 437  
 438          return $wp_admin_nav;
 439      }
 440  
 441      /**
 442       * Init the BP REST API.
 443       *
 444       * @since 5.0.0
 445       *
 446       * @param array $controllers Optional. See BP_Component::rest_api_init() for
 447       *                           description.
 448       */
 449  	public function rest_api_init( $controllers = array() ) {
 450          parent::rest_api_init( array(
 451              'BP_REST_XProfile_Fields_Endpoint',
 452              'BP_REST_XProfile_Field_Groups_Endpoint',
 453              'BP_REST_XProfile_Data_Endpoint',
 454          ) );
 455      }
 456  }


Generated: Sun Apr 5 01:01:30 2020 Cross-referenced by PHPXref 0.7.1