BP_XProfile_Component

Creates our XProfile component.

Defined (1)

The class is defined in the following location(s).

/bp-xprofile/classes/class-bp-xprofile-component.php  
  1. class BP_XProfile_Component extends BP_Component { 
  2.  
  3. /** 
  4. * Profile field types. 
  5. * @since 1.5.0 
  6. * @var array 
  7. */ 
  8. public $field_types; 
  9.  
  10. /** 
  11. * The acceptable visibility levels for xprofile fields. 
  12. * @see bp_xprofile_get_visibility_levels() 
  13. * @since 1.6.0 
  14. * @var array 
  15. */ 
  16. public $visibility_levels = array(); 
  17.  
  18. /** 
  19. * Start the xprofile component creation process. 
  20. * @since 1.5.0 
  21. */ 
  22. public function __construct() { 
  23. parent::start( 
  24. 'xprofile',  
  25. _x( 'Extended Profiles', 'Component page <title>', 'buddypress' ),  
  26. buddypress()->plugin_dir,  
  27. array( 
  28. 'adminbar_myaccount_order' => 20 
  29. ); 
  30.  
  31. $this->setup_hooks(); 
  32.  
  33. /** 
  34. * Include files. 
  35. * @since 1.5.0 
  36. * @param array $includes Array of files to include. 
  37. */ 
  38. public function includes( $includes = array() ) { 
  39. $includes = array( 
  40. 'cssjs',  
  41. 'cache',  
  42. 'actions',  
  43. 'activity',  
  44. 'screens',  
  45. 'caps',  
  46. 'filters',  
  47. 'settings',  
  48. 'template',  
  49. 'functions',  
  50. 'notifications',  
  51. ); 
  52.  
  53. if ( is_admin() ) { 
  54. $includes[] = 'admin'; 
  55.  
  56. parent::includes( $includes ); 
  57.  
  58. /** 
  59. * Setup globals. 
  60. * The BP_XPROFILE_SLUG constant is deprecated, and only used here for 
  61. * backwards compatibility. 
  62. * @since 1.5.0 
  63. * @param array $args Array of globals to set up. 
  64. */ 
  65. public function setup_globals( $args = array() ) { 
  66. $bp = buddypress(); 
  67.  
  68. // Define a slug, if necessary. 
  69. if ( !defined( 'BP_XPROFILE_SLUG' ) ) { 
  70. define( 'BP_XPROFILE_SLUG', 'profile' ); 
  71.  
  72. // Assign the base group and fullname field names to constants 
  73. // to use in SQL statements. 
  74. // Defined conditionally to accommodate unit tests. 
  75. if ( ! defined( 'BP_XPROFILE_BASE_GROUP_NAME' ) ) { 
  76. define( 'BP_XPROFILE_BASE_GROUP_NAME', stripslashes( bp_core_get_root_option( 'avatar_default' ) ) ); 
  77.  
  78. if ( ! defined( 'BP_XPROFILE_FULLNAME_FIELD_NAME' ) ) { 
  79. define( 'BP_XPROFILE_FULLNAME_FIELD_NAME', stripslashes( bp_core_get_root_option( 'bp-xprofile-fullname-field-name' ) ) ); 
  80.  
  81. /** 
  82. * Filters the supported field type IDs. 
  83. * @since 1.1.0 
  84. * @param array $value Array of IDs for the supported field types. 
  85. */ 
  86. $this->field_types = apply_filters( 'xprofile_field_types', array_keys( bp_xprofile_get_field_types() ) ); 
  87.  
  88. // 'option' is a special case. It is not a top-level field, so 
  89. // does not have an associated BP_XProfile_Field_Type class,  
  90. // but it must be whitelisted. 
  91. $this->field_types[] = 'option'; 
  92.  
  93. // Register the visibility levels. See bp_xprofile_get_visibility_levels() to filter. 
  94. $this->visibility_levels = array( 
  95. 'public' => array( 
  96. 'id' => 'public',  
  97. 'label' => _x( 'Everyone', 'Visibility level setting', 'buddypress' ) 
  98. ),  
  99. 'adminsonly' => array( 
  100. 'id' => 'adminsonly',  
  101. 'label' => _x( 'Only Me', 'Visibility level setting', 'buddypress' ) 
  102. ),  
  103. 'loggedin' => array( 
  104. 'id' => 'loggedin',  
  105. 'label' => _x( 'All Members', 'Visibility level setting', 'buddypress' ) 
  106. ); 
  107.  
  108. if ( bp_is_active( 'friends' ) ) { 
  109. $this->visibility_levels['friends'] = array( 
  110. 'id' => 'friends',  
  111. 'label' => _x( 'My Friends', 'Visibility level setting', 'buddypress' ) 
  112. ); 
  113.  
  114. // Tables. 
  115. $global_tables = array( 
  116. 'table_name_data' => $bp->table_prefix . 'bp_xprofile_data',  
  117. 'table_name_groups' => $bp->table_prefix . 'bp_xprofile_groups',  
  118. 'table_name_fields' => $bp->table_prefix . 'bp_xprofile_fields',  
  119. 'table_name_meta' => $bp->table_prefix . 'bp_xprofile_meta',  
  120. ); 
  121.  
  122. $meta_tables = array( 
  123. 'xprofile_group' => $bp->table_prefix . 'bp_xprofile_meta',  
  124. 'xprofile_field' => $bp->table_prefix . 'bp_xprofile_meta',  
  125. 'xprofile_data' => $bp->table_prefix . 'bp_xprofile_meta',  
  126. ); 
  127.  
  128. $globals = array( 
  129. 'slug' => BP_XPROFILE_SLUG,  
  130. 'has_directory' => false,  
  131. 'notification_callback' => 'xprofile_format_notifications',  
  132. 'global_tables' => $global_tables,  
  133. 'meta_tables' => $meta_tables,  
  134. ); 
  135.  
  136. parent::setup_globals( $globals ); 
  137.  
  138. /** 
  139. * Set up navigation. 
  140. * @since 1.5.0 
  141. * @global BuddyPress $bp The one true BuddyPress instance 
  142. * @param array $main_nav Array of main nav items to set up. 
  143. * @param array $sub_nav Array of sub nav items to set up. 
  144. */ 
  145. public function setup_nav( $main_nav = array(), $sub_nav = array() ) { 
  146.  
  147. // Determine user to use. 
  148. if ( bp_displayed_user_domain() ) { 
  149. $user_domain = bp_displayed_user_domain(); 
  150. } elseif ( bp_loggedin_user_domain() ) { 
  151. $user_domain = bp_loggedin_user_domain(); 
  152. } else { 
  153. return; 
  154.  
  155. $access = bp_core_can_edit_settings(); 
  156. $slug = bp_get_profile_slug(); 
  157. $profile_link = trailingslashit( $user_domain . $slug ); 
  158.  
  159. // Add 'Profile' to the main navigation. 
  160. $main_nav = array( 
  161. 'name' => _x( 'Profile', 'Profile header menu', 'buddypress' ),  
  162. 'slug' => $slug,  
  163. 'position' => 20,  
  164. 'screen_function' => 'xprofile_screen_display_profile',  
  165. 'default_subnav_slug' => 'public',  
  166. 'item_css_id' => $this->id 
  167. ); 
  168.  
  169. // Add the subnav items to the profile. 
  170. $sub_nav[] = array( 
  171. 'name' => _x( 'View', 'Profile header sub menu', 'buddypress' ),  
  172. 'slug' => 'public',  
  173. 'parent_url' => $profile_link,  
  174. 'parent_slug' => $slug,  
  175. 'screen_function' => 'xprofile_screen_display_profile',  
  176. 'position' => 10 
  177. ); 
  178.  
  179. // Edit Profile. 
  180. $sub_nav[] = array( 
  181. 'name' => _x( 'Edit', 'Profile header sub menu', 'buddypress' ),  
  182. 'slug' => 'edit',  
  183. 'parent_url' => $profile_link,  
  184. 'parent_slug' => $slug,  
  185. 'screen_function' => 'xprofile_screen_edit_profile',  
  186. 'position' => 20,  
  187. 'user_has_access' => $access 
  188. ); 
  189.  
  190. // Change Avatar. 
  191. if ( buddypress()->avatar->show_avatars ) { 
  192. $sub_nav[] = array( 
  193. 'name' => _x( 'Change Profile Photo', 'Profile header sub menu', 'buddypress' ),  
  194. 'slug' => 'change-avatar',  
  195. 'parent_url' => $profile_link,  
  196. 'parent_slug' => $slug,  
  197. 'screen_function' => 'xprofile_screen_change_avatar',  
  198. 'position' => 30,  
  199. 'user_has_access' => $access 
  200. ); 
  201.  
  202. // Change Cover image. 
  203. if ( bp_displayed_user_use_cover_image_header() ) { 
  204. $sub_nav[] = array( 
  205. 'name' => _x( 'Change Cover Image', 'Profile header sub menu', 'buddypress' ),  
  206. 'slug' => 'change-cover-image',  
  207. 'parent_url' => $profile_link,  
  208. 'parent_slug' => $slug,  
  209. 'screen_function' => 'xprofile_screen_change_cover_image',  
  210. 'position' => 40,  
  211. 'user_has_access' => $access 
  212. ); 
  213.  
  214. // The Settings > Profile nav item can only be set up after 
  215. // the Settings component has run its own nav routine. 
  216. add_action( 'bp_settings_setup_nav', array( $this, 'setup_settings_nav' ) ); 
  217.  
  218. parent::setup_nav( $main_nav, $sub_nav ); 
  219.  
  220. /** 
  221. * Set up the Settings > Profile nav item. 
  222. * Loaded in a separate method because the Settings component may not 
  223. * be loaded in time for BP_XProfile_Component::setup_nav(). 
  224. * @since 2.1.0 
  225. */ 
  226. public function setup_settings_nav() { 
  227. if ( ! bp_is_active( 'settings' ) ) { 
  228. return; 
  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. // Get the settings slug. 
  239. $settings_slug = bp_get_settings_slug(); 
  240.  
  241. bp_core_new_subnav_item( array( 
  242. 'name' => _x( 'Profile Visibility', 'Profile settings sub nav', 'buddypress' ),  
  243. 'slug' => 'profile',  
  244. 'parent_url' => trailingslashit( $user_domain . $settings_slug ),  
  245. 'parent_slug' => $settings_slug,  
  246. 'screen_function' => 'bp_xprofile_screen_settings',  
  247. 'position' => 30,  
  248. 'user_has_access' => bp_core_can_edit_settings() 
  249. ), 'members' ); 
  250.  
  251. /** 
  252. * Set up the Admin Bar. 
  253. * @since 1.5.0 
  254. * @param array $wp_admin_nav Admin Bar items. 
  255. */ 
  256. public function setup_admin_bar( $wp_admin_nav = array() ) { 
  257.  
  258. // Menus for logged in user. 
  259. if ( is_user_logged_in() ) { 
  260.  
  261. // Profile link. 
  262. $profile_link = trailingslashit( bp_loggedin_user_domain() . bp_get_profile_slug() ); 
  263.  
  264. // Add the "Profile" sub menu. 
  265. $wp_admin_nav[] = array( 
  266. 'parent' => buddypress()->my_account_menu_id,  
  267. 'id' => 'my-account-' . $this->id,  
  268. 'title' => _x( 'Profile', 'My Account Profile', 'buddypress' ),  
  269. 'href' => $profile_link 
  270. ); 
  271.  
  272. // View Profile. 
  273. $wp_admin_nav[] = array( 
  274. 'parent' => 'my-account-' . $this->id,  
  275. 'id' => 'my-account-' . $this->id . '-public',  
  276. 'title' => _x( 'View', 'My Account Profile sub nav', 'buddypress' ),  
  277. 'href' => $profile_link,  
  278. 'position' => 10 
  279. ); 
  280.  
  281. // Edit Profile. 
  282. $wp_admin_nav[] = array( 
  283. 'parent' => 'my-account-' . $this->id,  
  284. 'id' => 'my-account-' . $this->id . '-edit',  
  285. 'title' => _x( 'Edit', 'My Account Profile sub nav', 'buddypress' ),  
  286. 'href' => trailingslashit( $profile_link . 'edit' ),  
  287. 'position' => 20 
  288. ); 
  289.  
  290. // Edit Avatar. 
  291. if ( buddypress()->avatar->show_avatars ) { 
  292. $wp_admin_nav[] = array( 
  293. 'parent' => 'my-account-' . $this->id,  
  294. 'id' => 'my-account-' . $this->id . '-change-avatar',  
  295. 'title' => _x( 'Change Profile Photo', 'My Account Profile sub nav', 'buddypress' ),  
  296. 'href' => trailingslashit( $profile_link . 'change-avatar' ),  
  297. 'position' => 30 
  298. ); 
  299.  
  300. if ( bp_displayed_user_use_cover_image_header() ) { 
  301. $wp_admin_nav[] = array( 
  302. 'parent' => 'my-account-' . $this->id,  
  303. 'id' => 'my-account-' . $this->id . '-change-cover-image',  
  304. 'title' => _x( 'Change Cover Image', 'My Account Profile sub nav', 'buddypress' ),  
  305. 'href' => trailingslashit( $profile_link . 'change-cover-image' ),  
  306. 'position' => 40 
  307. ); 
  308.  
  309. parent::setup_admin_bar( $wp_admin_nav ); 
  310.  
  311. /** 
  312. * Add custom hooks. 
  313. * @since 2.0.0 
  314. */ 
  315. public function setup_hooks() { 
  316. add_filter( 'bp_settings_admin_nav', array( $this, 'setup_settings_admin_nav' ), 2 ); 
  317.  
  318. /** 
  319. * Sets up the title for pages and <title>. 
  320. * @since 1.5.0 
  321. */ 
  322. public function setup_title() { 
  323.  
  324. if ( bp_is_profile_component() ) { 
  325. $bp = buddypress(); 
  326.  
  327. if ( bp_is_my_profile() ) { 
  328. $bp->bp_options_title = _x( 'My Profile', 'Page title', 'buddypress' ); 
  329. } else { 
  330. $bp->bp_options_avatar = bp_core_fetch_avatar( array( 
  331. 'item_id' => bp_displayed_user_id(),  
  332. 'type' => 'thumb',  
  333. 'alt' => sprintf( _x( 'Profile picture of %s', 'Avatar alt', 'buddypress' ), bp_get_displayed_user_fullname() ) 
  334. ) ); 
  335. $bp->bp_options_title = bp_get_displayed_user_fullname(); 
  336.  
  337. parent::setup_title(); 
  338.  
  339. /** 
  340. * Setup cache groups. 
  341. * @since 2.2.0 
  342. */ 
  343. public function setup_cache_groups() { 
  344.  
  345. // Global groups. 
  346. wp_cache_add_global_groups( array( 
  347. 'bp_xprofile',  
  348. 'bp_xprofile_data',  
  349. 'bp_xprofile_fields',  
  350. 'bp_xprofile_groups',  
  351. 'xprofile_meta' 
  352. ) ); 
  353.  
  354. parent::setup_cache_groups(); 
  355.  
  356. /** 
  357. * Adds "Settings > Profile" subnav item under the "Settings" adminbar menu. 
  358. * @since 2.0.0 
  359. * @param array $wp_admin_nav The settings adminbar nav array. 
  360. * @return array 
  361. */ 
  362. public function setup_settings_admin_nav( $wp_admin_nav ) { 
  363.  
  364. // Setup the logged in user variables. 
  365. $settings_link = trailingslashit( bp_loggedin_user_domain() . bp_get_settings_slug() ); 
  366.  
  367. // Add the "Profile" subnav item. 
  368. $wp_admin_nav[] = array( 
  369. 'parent' => 'my-account-' . buddypress()->settings->id,  
  370. 'id' => 'my-account-' . buddypress()->settings->id . '-profile',  
  371. 'title' => _x( 'Profile', 'My Account Settings sub nav', 'buddypress' ),  
  372. 'href' => trailingslashit( $settings_link . 'profile' ) 
  373. ); 
  374.  
  375. return $wp_admin_nav;