/bp-core/classes/class-bp-core.php

  1. <?php 
  2. /** 
  3. * BuddyPress Core Loader. 
  4. * 
  5. * Core contains the commonly used functions, classes, and APIs. 
  6. * 
  7. * @package BuddyPress 
  8. * @subpackage Core 
  9. * @since 1.5.0 
  10. */ 
  11.  
  12. // Exit if accessed directly. 
  13. defined( 'ABSPATH' ) || exit; 
  14.  
  15. /** 
  16. * Creates the Core component. 
  17. * 
  18. * @since 1.5.0 
  19. */ 
  20. class BP_Core extends BP_Component { 
  21.  
  22. /** 
  23. * Start the members component creation process. 
  24. * 
  25. * @since 1.5.0 
  26. * 
  27. */ 
  28. public function __construct() { 
  29. parent::start( 
  30. 'core',  
  31. __( 'BuddyPress Core', 'buddypress' ),  
  32. buddypress()->plugin_dir 
  33. ); 
  34.  
  35. $this->bootstrap(); 
  36.  
  37. /** 
  38. * Populate the global data needed before BuddyPress can continue. 
  39. * 
  40. * This involves figuring out the currently required, activated, deactivated,  
  41. * and optional components. 
  42. * 
  43. * @since 1.5.0 
  44. */ 
  45. private function bootstrap() { 
  46. $bp = buddypress(); 
  47.  
  48. /** 
  49. * Fires before the loading of individual components and after BuddyPress Core. 
  50. * 
  51. * Allows plugins to run code ahead of the other components. 
  52. * 
  53. * @since 1.2.0 
  54. */ 
  55. do_action( 'bp_core_loaded' ); 
  56.  
  57. /** Components ******************************************************* 
  58. */ 
  59.  
  60. /** 
  61. * Filters the included and optional components. 
  62. * 
  63. * @since 1.5.0 
  64. * 
  65. * @param array $value Array of included and optional components. 
  66. */ 
  67. $bp->optional_components = apply_filters( 'bp_optional_components', array( 'activity', 'blogs', 'forums', 'friends', 'groups', 'messages', 'notifications', 'settings', 'xprofile' ) ); 
  68.  
  69. /** 
  70. * Filters the required components. 
  71. * 
  72. * @since 1.5.0 
  73. * 
  74. * @param array $value Array of required components. 
  75. */ 
  76. $bp->required_components = apply_filters( 'bp_required_components', array( 'members' ) ); 
  77.  
  78. // Get a list of activated components. 
  79. if ( $active_components = bp_get_option( 'bp-active-components' ) ) { 
  80.  
  81. /** This filter is documented in bp-core/admin/bp-core-admin-components.php */ 
  82. $bp->active_components = apply_filters( 'bp_active_components', $active_components ); 
  83.  
  84. /** 
  85. * Filters the deactivated components. 
  86. * 
  87. * @since 1.0.0 
  88. * 
  89. * @param array $value Array of deactivated components. 
  90. */ 
  91. $bp->deactivated_components = apply_filters( 'bp_deactivated_components', array_values( array_diff( array_values( array_merge( $bp->optional_components, $bp->required_components ) ), array_keys( $bp->active_components ) ) ) ); 
  92.  
  93. // Pre 1.5 Backwards compatibility. 
  94. } elseif ( $deactivated_components = bp_get_option( 'bp-deactivated-components' ) ) { 
  95.  
  96. // Trim off namespace and filename. 
  97. foreach ( array_keys( (array) $deactivated_components ) as $component ) { 
  98. $trimmed[] = str_replace( '.php', '', str_replace( 'bp-', '', $component ) ); 
  99.  
  100. /** This filter is documented in bp-core/bp-core-loader.php */ 
  101. $bp->deactivated_components = apply_filters( 'bp_deactivated_components', $trimmed ); 
  102.  
  103. // Setup the active components. 
  104. $active_components = array_fill_keys( array_diff( array_values( array_merge( $bp->optional_components, $bp->required_components ) ), array_values( $bp->deactivated_components ) ), '1' ); 
  105.  
  106. /** This filter is documented in bp-core/admin/bp-core-admin-components.php */ 
  107. $bp->active_components = apply_filters( 'bp_active_components', $bp->active_components ); 
  108.  
  109. // Default to all components active. 
  110. } else { 
  111.  
  112. // Set globals. 
  113. $bp->deactivated_components = array(); 
  114.  
  115. // Setup the active components. 
  116. $active_components = array_fill_keys( array_values( array_merge( $bp->optional_components, $bp->required_components ) ), '1' ); 
  117.  
  118. /** This filter is documented in bp-core/admin/bp-core-admin-components.php */ 
  119. $bp->active_components = apply_filters( 'bp_active_components', $bp->active_components ); 
  120.  
  121. // Loop through optional components. 
  122. foreach( $bp->optional_components as $component ) { 
  123. if ( bp_is_active( $component ) && file_exists( $bp->plugin_dir . '/bp-' . $component . '/bp-' . $component . '-loader.php' ) ) { 
  124. include( $bp->plugin_dir . '/bp-' . $component . '/bp-' . $component . '-loader.php' ); 
  125.  
  126. // Loop through required components. 
  127. foreach( $bp->required_components as $component ) { 
  128. if ( file_exists( $bp->plugin_dir . '/bp-' . $component . '/bp-' . $component . '-loader.php' ) ) { 
  129. include( $bp->plugin_dir . '/bp-' . $component . '/bp-' . $component . '-loader.php' ); 
  130.  
  131. // Add Core to required components. 
  132. $bp->required_components[] = 'core'; 
  133.  
  134. /** 
  135. * Fires after the loading of individual components. 
  136. * 
  137. * @since 2.0.0 
  138. */ 
  139. do_action( 'bp_core_components_included' ); 
  140.  
  141. /** 
  142. * Include bp-core files. 
  143. * 
  144. * @since 1.6.0 
  145. * 
  146. * @see BP_Component::includes() for description of parameters. 
  147. * 
  148. * @param array $includes See {@link BP_Component::includes()}. 
  149. */ 
  150. public function includes( $includes = array() ) { 
  151.  
  152. if ( ! is_admin() ) { 
  153. return; 
  154.  
  155. $includes = array( 
  156. 'admin' 
  157. ); 
  158.  
  159. parent::includes( $includes ); 
  160.  
  161. /** 
  162. * Set up bp-core global settings. 
  163. * 
  164. * Sets up a majority of the BuddyPress globals that require a minimal 
  165. * amount of processing, meaning they cannot be set in the BuddyPress class. 
  166. * 
  167. * @since 1.5.0 
  168. * 
  169. * @see BP_Component::setup_globals() for description of parameters. 
  170. * 
  171. * @param array $args See {@link BP_Component::setup_globals()}. 
  172. */ 
  173. public function setup_globals( $args = array() ) { 
  174. $bp = buddypress(); 
  175.  
  176. /** Database ********************************************************* 
  177. */ 
  178.  
  179. // Get the base database prefix. 
  180. if ( empty( $bp->table_prefix ) ) { 
  181. $bp->table_prefix = bp_core_get_table_prefix(); 
  182.  
  183. // The domain for the root of the site where the main blog resides. 
  184. if ( empty( $bp->root_domain ) ) { 
  185. $bp->root_domain = bp_core_get_root_domain(); 
  186.  
  187. // Fetches all of the core BuddyPress settings in one fell swoop. 
  188. if ( empty( $bp->site_options ) ) { 
  189. $bp->site_options = bp_core_get_root_options(); 
  190.  
  191. // The names of the core WordPress pages used to display BuddyPress content. 
  192. if ( empty( $bp->pages ) ) { 
  193. $bp->pages = bp_core_get_directory_pages(); 
  194.  
  195. /** Basic current user data ****************************************** 
  196. */ 
  197.  
  198. // Logged in user is the 'current_user'. 
  199. $current_user = wp_get_current_user(); 
  200.  
  201. // The user ID of the user who is currently logged in. 
  202. $bp->loggedin_user = new stdClass; 
  203. $bp->loggedin_user->id = isset( $current_user->ID ) ? $current_user->ID : 0; 
  204.  
  205. /** Avatars ********************************************************** 
  206. */ 
  207.  
  208. // Fetches the default Gravatar image to use if the user/group/blog has no avatar or gravatar. 
  209. $bp->grav_default = new stdClass; 
  210.  
  211. /** 
  212. * Filters the default user Gravatar. 
  213. * 
  214. * @since 1.1.0 
  215. * 
  216. * @param string $value Default user Gravatar. 
  217. */ 
  218. $bp->grav_default->user = apply_filters( 'bp_user_gravatar_default', $bp->site_options['avatar_default'] ); 
  219.  
  220. /** 
  221. * Filters the default group Gravatar. 
  222. * 
  223. * @since 1.1.0 
  224. * 
  225. * @param string $value Default group Gravatar. 
  226. */ 
  227. $bp->grav_default->group = apply_filters( 'bp_group_gravatar_default', $bp->grav_default->user ); 
  228.  
  229. /** 
  230. * Filters the default blog Gravatar. 
  231. * 
  232. * @since 1.1.0 
  233. * 
  234. * @param string $value Default blog Gravatar. 
  235. */ 
  236. $bp->grav_default->blog = apply_filters( 'bp_blog_gravatar_default', $bp->grav_default->user ); 
  237.  
  238. // Notifications table. Included here for legacy purposes. Use 
  239. // bp-notifications instead. 
  240. $bp->core->table_name_notifications = $bp->table_prefix . 'bp_notifications'; 
  241.  
  242. // Backward compatibility for plugins modifying the legacy bp_nav and bp_options_nav global properties. 
  243. $bp->bp_nav = new BP_Core_BP_Nav_BackCompat(); 
  244. $bp->bp_options_nav = new BP_Core_BP_Options_Nav_BackCompat(); 
  245.  
  246. /** 
  247. * Used to determine if user has admin rights on current content. If the 
  248. * logged in user is viewing their own profile and wants to delete 
  249. * something, is_item_admin is used. This is a generic variable so it 
  250. * can be used by other components. It can also be modified, so when 
  251. * viewing a group 'is_item_admin' would be 'true' if they are a group 
  252. * admin, and 'false' if they are not. 
  253. */ 
  254. bp_update_is_item_admin( bp_user_has_access(), 'core' ); 
  255.  
  256. // Is the logged in user is a mod for the current item? 
  257. bp_update_is_item_mod( false, 'core' ); 
  258.  
  259. /** 
  260. * Fires at the end of the setup of bp-core globals setting. 
  261. * 
  262. * @since 1.1.0 
  263. */ 
  264. do_action( 'bp_core_setup_globals' ); 
  265.  
  266. /** 
  267. * Setup cache groups 
  268. * 
  269. * @since 2.2.0 
  270. */ 
  271. public function setup_cache_groups() { 
  272.  
  273. // Global groups. 
  274. wp_cache_add_global_groups( array( 
  275. 'bp' 
  276. ) ); 
  277.  
  278. parent::setup_cache_groups(); 
  279.  
  280. /** 
  281. * Set up post types. 
  282. * 
  283. * @since BuddyPress (2.4.0) 
  284. */ 
  285. public function register_post_types() { 
  286.  
  287. // Emails 
  288. if ( bp_is_root_blog() && ! is_network_admin() ) { 
  289. register_post_type( 
  290. bp_get_email_post_type(),  
  291. apply_filters( 'bp_register_email_post_type', array( 
  292. 'description' => _x( 'BuddyPress emails', 'email post type description', 'buddypress' ),  
  293. 'labels' => bp_get_email_post_type_labels(),  
  294. 'menu_icon' => 'dashicons-email',  
  295. 'public' => false,  
  296. 'publicly_queryable' => bp_current_user_can( 'bp_moderate' ),  
  297. 'query_var' => false,  
  298. 'rewrite' => false,  
  299. 'show_in_admin_bar' => false,  
  300. 'show_ui' => bp_current_user_can( 'bp_moderate' ),  
  301. 'supports' => bp_get_email_post_type_supports(),  
  302. ) ) 
  303. ); 
  304.  
  305. parent::register_post_types(); 
.