BP_Members_Component

Defines the BuddyPress Members Component.

Defined (1)

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

/bp-members/classes/class-bp-members-component.php  
  1. class BP_Members_Component extends BP_Component { 
  2.  
  3. /** 
  4. * Member types. 
  5. * @see bp_register_member_type() 
  6. * @since 2.2.0 
  7. * @var array 
  8. */ 
  9. public $types = array(); 
  10.  
  11. /** 
  12. * Start the members component creation process. 
  13. * @since 1.5.0 
  14. */ 
  15. public function __construct() { 
  16. parent::start( 
  17. 'members',  
  18. __( 'Members', 'buddypress' ),  
  19. buddypress()->plugin_dir,  
  20. array( 
  21. 'adminbar_myaccount_order' => 20,  
  22. 'search_query_arg' => 'members_search',  
  23. ); 
  24.  
  25. /** 
  26. * Include bp-members files. 
  27. * @since 1.5.0 
  28. * @see BP_Component::includes() for description of parameters. 
  29. * @param array $includes See {@link BP_Component::includes()}. 
  30. */ 
  31. public function includes( $includes = array() ) { 
  32.  
  33. // Always include these files. 
  34. $includes = array( 
  35. 'actions',  
  36. 'filters',  
  37. 'screens',  
  38. 'template',  
  39. 'adminbar',  
  40. 'functions',  
  41. 'widgets',  
  42. 'cache',  
  43. ); 
  44.  
  45. if ( bp_is_active( 'activity' ) ) { 
  46. $includes[] = 'activity'; 
  47.  
  48. // Include these only if in admin. 
  49. if ( is_admin() ) { 
  50. $includes[] = 'admin'; 
  51.  
  52. parent::includes( $includes ); 
  53.  
  54. /** 
  55. * Set up bp-members global settings. 
  56. * The BP_MEMBERS_SLUG constant is deprecated, and only used here for 
  57. * backwards compatibility. 
  58. * @since 1.5.0 
  59. * @see BP_Component::setup_globals() for description of parameters. 
  60. * @param array $args See {@link BP_Component::setup_globals()}. 
  61. */ 
  62. public function setup_globals( $args = array() ) { 
  63. global $wpdb; 
  64.  
  65. $bp = buddypress(); 
  66.  
  67. /** Component Globals ************************************************ 
  68. */ 
  69.  
  70. // Define a slug, as a fallback for backpat. 
  71. if ( !defined( 'BP_MEMBERS_SLUG' ) ) { 
  72. define( 'BP_MEMBERS_SLUG', $this->id ); 
  73.  
  74. // Fetch the default directory title. 
  75. $default_directory_titles = bp_core_get_directory_page_default_titles(); 
  76. $default_directory_title = $default_directory_titles[$this->id]; 
  77.  
  78. // Override any passed args. 
  79. $args = array( 
  80. 'slug' => BP_MEMBERS_SLUG,  
  81. 'root_slug' => isset( $bp->pages->members->slug ) ? $bp->pages->members->slug : BP_MEMBERS_SLUG,  
  82. 'has_directory' => true,  
  83. 'directory_title' => isset( $bp->pages->members->title ) ? $bp->pages->members->title : $default_directory_title,  
  84. 'search_string' => __( 'Search Members...', 'buddypress' ),  
  85. 'global_tables' => array( 
  86. 'table_name_last_activity' => bp_core_get_table_prefix() . 'bp_activity',  
  87. 'table_name_signups' => $wpdb->base_prefix . 'signups', // Signups is a global WordPress table. 
  88. ); 
  89.  
  90. parent::setup_globals( $args ); 
  91.  
  92. /** Logged in user *************************************************** 
  93. */ 
  94.  
  95. // The core userdata of the user who is currently logged in. 
  96. $bp->loggedin_user->userdata = bp_core_get_core_userdata( bp_loggedin_user_id() ); 
  97.  
  98. // Fetch the full name for the logged in user. 
  99. $bp->loggedin_user->fullname = isset( $bp->loggedin_user->userdata->display_name ) ? $bp->loggedin_user->userdata->display_name : ''; 
  100.  
  101. // Hits the DB on single WP installs so get this separately. 
  102. $bp->loggedin_user->is_super_admin = $bp->loggedin_user->is_site_admin = is_super_admin( bp_loggedin_user_id() ); 
  103.  
  104. // The domain for the user currently logged in. eg: http://example.com/members/andy. 
  105. $bp->loggedin_user->domain = bp_core_get_user_domain( bp_loggedin_user_id() ); 
  106.  
  107. /** Displayed user *************************************************** 
  108. */ 
  109.  
  110. // The core userdata of the user who is currently being displayed. 
  111. $bp->displayed_user->userdata = bp_core_get_core_userdata( bp_displayed_user_id() ); 
  112.  
  113. // Fetch the full name displayed user. 
  114. $bp->displayed_user->fullname = isset( $bp->displayed_user->userdata->display_name ) ? $bp->displayed_user->userdata->display_name : ''; 
  115.  
  116. // The domain for the user currently being displayed. 
  117. $bp->displayed_user->domain = bp_core_get_user_domain( bp_displayed_user_id() ); 
  118.  
  119. // Initialize the nav for the members component. 
  120. $this->nav = new BP_Core_Nav(); 
  121.  
  122. // If A user is displayed, check if there is a front template 
  123. if ( bp_get_displayed_user() ) { 
  124. $bp->displayed_user->front_template = bp_displayed_user_get_front_template(); 
  125.  
  126. /** Signup *********************************************************** 
  127. */ 
  128.  
  129. $bp->signup = new stdClass; 
  130.  
  131. /** Profiles Fallback ************************************************ 
  132. */ 
  133.  
  134. if ( ! bp_is_active( 'xprofile' ) ) { 
  135. $bp->profile = new stdClass; 
  136. $bp->profile->slug = 'profile'; 
  137. $bp->profile->id = 'profile'; 
  138.  
  139. /** 
  140. * Set up canonical stack for this component. 
  141. * @since 2.1.0 
  142. */ 
  143. public function setup_canonical_stack() { 
  144. $bp = buddypress(); 
  145.  
  146. /** Default Profile Component **************************************** 
  147. */ 
  148. if ( bp_displayed_user_has_front_template() ) { 
  149. $bp->default_component = 'front'; 
  150. } elseif ( defined( 'BP_DEFAULT_COMPONENT' ) && BP_DEFAULT_COMPONENT ) { 
  151. $bp->default_component = BP_DEFAULT_COMPONENT; 
  152. } elseif ( bp_is_active( 'activity' ) && isset( $bp->pages->activity ) ) { 
  153. $bp->default_component = bp_get_activity_slug(); 
  154. } else { 
  155. $bp->default_component = ( 'xprofile' === $bp->profile->id ) ? 'profile' : $bp->profile->id; 
  156.  
  157. /** Canonical Component Stack **************************************** 
  158. */ 
  159.  
  160. if ( bp_displayed_user_id() ) { 
  161. $bp->canonical_stack['base_url'] = bp_displayed_user_domain(); 
  162.  
  163. if ( bp_current_component() ) { 
  164. $bp->canonical_stack['component'] = bp_current_component(); 
  165.  
  166. if ( bp_current_action() ) { 
  167. $bp->canonical_stack['action'] = bp_current_action(); 
  168.  
  169. if ( !empty( $bp->action_variables ) ) { 
  170. $bp->canonical_stack['action_variables'] = bp_action_variables(); 
  171.  
  172. // Looking at the single member root/home, so assume the default. 
  173. if ( ! bp_current_component() ) { 
  174. $bp->current_component = $bp->default_component; 
  175.  
  176. // The canonical URL will not contain the default component. 
  177. } elseif ( bp_is_current_component( $bp->default_component ) && ! bp_current_action() ) { 
  178. unset( $bp->canonical_stack['component'] ); 
  179.  
  180. // If we're on a spammer's profile page, only users with the 'bp_moderate' cap 
  181. // can view subpages on the spammer's profile. 
  182. // 
  183. // users without the cap trying to access a spammer's subnav page will get 
  184. // redirected to the root of the spammer's profile page. this occurs by 
  185. // by removing the component in the canonical stack. 
  186. if ( bp_is_user_spammer( bp_displayed_user_id() ) && ! bp_current_user_can( 'bp_moderate' ) ) { 
  187. unset( $bp->canonical_stack['component'] ); 
  188.  
  189. /** 
  190. * Set up fall-back component navigation if XProfile is inactive. 
  191. * @since 1.5.0 
  192. * @see BP_Component::setup_nav() for a description of arguments. 
  193. * @param array $main_nav Optional. See BP_Component::setup_nav() for 
  194. * description. 
  195. * @param array $sub_nav Optional. See BP_Component::setup_nav() for 
  196. * description. 
  197. */ 
  198. public function setup_nav( $main_nav = array(), $sub_nav = array() ) { 
  199.  
  200. // Don't set up navigation if there's no member. 
  201. if ( ! is_user_logged_in() && ! bp_is_user() ) { 
  202. return; 
  203.  
  204. $is_xprofile_active = bp_is_active( 'xprofile' ); 
  205.  
  206. // Bail if XProfile component is active and there's no custom front page for the user. 
  207. if ( ! bp_displayed_user_has_front_template() && $is_xprofile_active ) { 
  208. return; 
  209.  
  210. // Determine user to use. 
  211. if ( bp_displayed_user_domain() ) { 
  212. $user_domain = bp_displayed_user_domain(); 
  213. } elseif ( bp_loggedin_user_domain() ) { 
  214. $user_domain = bp_loggedin_user_domain(); 
  215. } else { 
  216. return; 
  217.  
  218. // Set slug to profile in case the xProfile component is not active 
  219. $slug = bp_get_profile_slug(); 
  220.  
  221. // Defaults to empty navs 
  222. $this->main_nav = array(); 
  223. $this->sub_nav = array(); 
  224.  
  225. if ( ! $is_xprofile_active ) { 
  226. $this->main_nav = array( 
  227. 'name' => _x( 'Profile', 'Member profile main navigation', 'buddypress' ),  
  228. 'slug' => $slug,  
  229. 'position' => 20,  
  230. 'screen_function' => 'bp_members_screen_display_profile',  
  231. 'default_subnav_slug' => 'public',  
  232. 'item_css_id' => buddypress()->profile->id 
  233. ); 
  234.  
  235. /** 
  236. * Setup the subnav items for the member profile. 
  237. * This is required in case there's a custom front or in case the xprofile component 
  238. * is not active. 
  239. */ 
  240. $this->sub_nav = array( 
  241. 'name' => _x( 'View', 'Member profile view', 'buddypress' ),  
  242. 'slug' => 'public',  
  243. 'parent_url' => trailingslashit( $user_domain . $slug ),  
  244. 'parent_slug' => $slug,  
  245. 'screen_function' => 'bp_members_screen_display_profile',  
  246. 'position' => 10 
  247. ); 
  248.  
  249. /** 
  250. * If there's a front template the members component nav 
  251. * will be there to display the user's front page. 
  252. */ 
  253. if ( bp_displayed_user_has_front_template() ) { 
  254. $main_nav = array( 
  255. 'name' => _x( 'Home', 'Member Home page', 'buddypress' ),  
  256. 'slug' => 'front',  
  257. 'position' => 5,  
  258. 'screen_function' => 'bp_members_screen_display_profile',  
  259. 'default_subnav_slug' => 'public',  
  260. ); 
  261.  
  262. // We need a dummy subnav for the front page to load. 
  263. $front_subnav = $this->sub_nav; 
  264. $front_subnav['parent_slug'] = 'front'; 
  265.  
  266. // In case the subnav is displayed in the front template 
  267. $front_subnav['parent_url'] = trailingslashit( $user_domain . 'front' ); 
  268.  
  269. // Set the subnav 
  270. $sub_nav[] = $front_subnav; 
  271.  
  272. /** 
  273. * If the profile component is not active, we need to create a new 
  274. * nav to display the WordPress profile. 
  275. */ 
  276. if ( ! $is_xprofile_active ) { 
  277. add_action( 'bp_members_setup_nav', array( $this, 'setup_profile_nav' ) ); 
  278.  
  279. /** 
  280. * If there's no front template and xProfile is not active, the members 
  281. * component nav will be there to display the WordPress profile 
  282. */ 
  283. } else { 
  284. $main_nav = $this->main_nav; 
  285. $sub_nav[] = $this->sub_nav; 
  286.  
  287.  
  288. parent::setup_nav( $main_nav, $sub_nav ); 
  289.  
  290. /** 
  291. * Set up a profile nav in case the xProfile 
  292. * component is not active and a front template is 
  293. * used. 
  294. * @since 2.6.0 
  295. */ 
  296. public function setup_profile_nav() { 
  297. if ( empty( $this->main_nav ) || empty( $this->sub_nav ) ) { 
  298. return; 
  299.  
  300. // Add the main nav 
  301. bp_core_new_nav_item( $this->main_nav, 'members' ); 
  302.  
  303. // Add the sub nav item. 
  304. bp_core_new_subnav_item( $this->sub_nav, 'members' ); 
  305.  
  306. /** 
  307. * Set up the title for pages and <title>. 
  308. * @since 1.5.0 
  309. */ 
  310. public function setup_title() { 
  311. $bp = buddypress(); 
  312.  
  313. if ( bp_is_my_profile() ) { 
  314. $bp->bp_options_title = __( 'You', 'buddypress' ); 
  315. } elseif ( bp_is_user() ) { 
  316. $bp->bp_options_title = bp_get_displayed_user_fullname(); 
  317. $bp->bp_options_avatar = bp_core_fetch_avatar( array( 
  318. 'item_id' => bp_displayed_user_id(),  
  319. 'type' => 'thumb',  
  320. 'alt' => sprintf( __( 'Profile picture of %s', 'buddypress' ), $bp->bp_options_title ) 
  321. ) ); 
  322.  
  323. parent::setup_title(); 
  324.  
  325. /** 
  326. * Setup cache groups. 
  327. * @since 2.2.0 
  328. */ 
  329. public function setup_cache_groups() { 
  330.  
  331. // Global groups. 
  332. wp_cache_add_global_groups( array( 
  333. 'bp_last_activity',  
  334. 'bp_member_type' 
  335. ) ); 
  336.  
  337. parent::setup_cache_groups();