/bp-forums/bp-forums-loader.php

  1. <?php 
  2. /** 
  3. * BuddyPress Forums Loader. 
  4. * 
  5. * A discussion forums component. Comes bundled with bbPress stand-alone. 
  6. * 
  7. * Note: The bp-forums component has been retired. Use the bbPress WordPress 
  8. * plugin instead. 
  9. * 
  10. * @package BuddyPress 
  11. * @subpackage ForumsLoader 
  12. * @since 1.5.0 
  13. */ 
  14.  
  15. // Exit if accessed directly. 
  16. defined( 'ABSPATH' ) || exit; 
  17.  
  18. /** 
  19. * Sets up the forums component. 
  20. * 
  21. * @since 1.5.0 
  22. */ 
  23. class BP_Forums_Component extends BP_Component { 
  24.  
  25. /** 
  26. * Start the forums component creation process. 
  27. * 
  28. * @since 1.5.0 
  29. */ 
  30. public function __construct() { 
  31. parent::start( 
  32. 'forums',  
  33. __( 'Discussion Forums', 'buddypress' ),  
  34. buddypress()->plugin_dir,  
  35. array( 
  36. 'adminbar_myaccount_order' => 80 
  37. ); 
  38.  
  39. /** 
  40. * Set up bp-forums global settings. 
  41. * 
  42. * The BP_FORUMS_SLUG constant is deprecated, and only used here for 
  43. * backwards compatibility. 
  44. * 
  45. * @since 1.5.0 
  46. * 
  47. * @see BP_Component::setup_globals() for description of parameters. 
  48. * 
  49. * @param array $args See {@link BP_Component::setup_globals()}. 
  50. */ 
  51. public function setup_globals( $args = array() ) { 
  52. $bp = buddypress(); 
  53.  
  54. // Define the parent forum ID. 
  55. if ( ! defined( 'BP_FORUMS_PARENT_FORUM_ID' ) ) { 
  56. define( 'BP_FORUMS_PARENT_FORUM_ID', 1 ); 
  57.  
  58. // Define a slug, if necessary. 
  59. if ( ! defined( 'BP_FORUMS_SLUG' ) ) { 
  60. define( 'BP_FORUMS_SLUG', $this->id ); 
  61.  
  62. // The location of the bbPress stand-alone config file. 
  63. $bbconfig = bp_core_get_root_option( 'bb-config-location' ); 
  64. if ( '' !== $bbconfig ) { 
  65. $this->bbconfig = $bbconfig; 
  66.  
  67. // All globals for messaging component. 
  68. // Note that global_tables is included in this array. 
  69. $globals = array( 
  70. 'slug' => BP_FORUMS_SLUG,  
  71. 'root_slug' => isset( $bp->pages->forums->slug ) ? $bp->pages->forums->slug : BP_FORUMS_SLUG,  
  72. 'has_directory' => true,  
  73. 'notification_callback' => 'messages_format_notifications',  
  74. 'search_string' => __( 'Search Forums...', 'buddypress' ),  
  75. ); 
  76.  
  77. parent::setup_globals( $globals ); 
  78.  
  79. /** 
  80. * Include bp-forums files. 
  81. * 
  82. * @since 1.5.0 
  83. * 
  84. * @see BP_Component::includes() for description of parameters. 
  85. * 
  86. * @param array $includes See {@link BP_Component::includes()}. 
  87. */ 
  88. public function includes( $includes = array() ) { 
  89.  
  90. // Files to include. 
  91. $includes = array( 
  92. 'actions',  
  93. 'screens',  
  94. 'classes',  
  95. 'filters',  
  96. 'template',  
  97. 'functions',  
  98. ); 
  99.  
  100. // bbPress stand-alone. 
  101. if ( ! defined( 'BB_PATH' ) ) { 
  102. $includes[] = 'bbpress-sa'; 
  103.  
  104. // Admin-specific code. 
  105. if ( is_admin() ) { 
  106. $includes[] = 'deprecated/1.6'; 
  107. $includes[] = 'deprecated/1.7'; 
  108.  
  109. parent::includes( $includes ); 
  110.  
  111. /** 
  112. * Set up component navigation. 
  113. * 
  114. * @since 1.5.0 
  115. * 
  116. * @see BP_Component::setup_nav() for a description of arguments. 
  117. * 
  118. * @param array $main_nav Optional. See BP_Component::setup_nav() for 
  119. * description. 
  120. * @param array $sub_nav Optional. See BP_Component::setup_nav() for 
  121. * description. 
  122. */ 
  123. public function setup_nav( $main_nav = array(), $sub_nav = array() ) { 
  124.  
  125. // Stop if forums haven't been set up yet. 
  126. if ( ! bp_forums_is_installed_correctly() ) { 
  127. return; 
  128.  
  129. // Stop if there is no user displayed or logged in. 
  130. if ( ! is_user_logged_in() && ! bp_displayed_user_id() ) { 
  131. return; 
  132.  
  133. // Determine user to use. 
  134. if ( bp_displayed_user_domain() ) { 
  135. $user_domain = bp_displayed_user_domain(); 
  136. } elseif ( bp_loggedin_user_domain() ) { 
  137. $user_domain = bp_loggedin_user_domain(); 
  138. } else { 
  139. return; 
  140.  
  141. // User link. 
  142. $slug = bp_get_forums_slug(); 
  143. $forums_link = trailingslashit( $user_domain . $slug ); 
  144.  
  145. // Add 'Forums' to the main navigation. 
  146. $main_nav = array( 
  147. 'name' => __( 'Forums', 'buddypress' ),  
  148. 'slug' => $slug,  
  149. 'position' => 80,  
  150. 'screen_function' => 'bp_member_forums_screen_topics',  
  151. 'default_subnav_slug' => 'topics',  
  152. 'item_css_id' => $this->id 
  153. ); 
  154.  
  155. // Topics started. 
  156. $sub_nav[] = array( 
  157. 'name' => __( 'Topics Started', 'buddypress' ),  
  158. 'slug' => 'topics',  
  159. 'parent_url' => $forums_link,  
  160. 'parent_slug' => $slug,  
  161. 'screen_function' => 'bp_member_forums_screen_topics',  
  162. 'position' => 20,  
  163. 'item_css_id' => 'topics' 
  164. ); 
  165.  
  166. // Topics replied to. 
  167. $sub_nav[] = array( 
  168. 'name' => __( 'Replied To', 'buddypress' ),  
  169. 'slug' => 'replies',  
  170. 'parent_url' => $forums_link,  
  171. 'parent_slug' => $slug,  
  172. 'screen_function' => 'bp_member_forums_screen_replies',  
  173. 'position' => 40,  
  174. 'item_css_id' => 'replies' 
  175. ); 
  176.  
  177. parent::setup_nav( $main_nav, $sub_nav ); 
  178.  
  179. /** 
  180. * Set up bp-forums integration with the WordPress admin bar. 
  181. * 
  182. * @since 1.5.0 
  183. * 
  184. * @see BP_Component::setup_admin_bar() for a description of arguments. 
  185. * 
  186. * @param array $wp_admin_nav See BP_Component::setup_admin_bar() 
  187. * for description. 
  188. */ 
  189. public function setup_admin_bar( $wp_admin_nav = array() ) { 
  190.  
  191. // Menus for logged in user. 
  192. if ( is_user_logged_in() ) { 
  193.  
  194. // Setup the logged in user variables. 
  195. $forums_link = trailingslashit( bp_loggedin_user_domain() . bp_get_forums_slug() ); 
  196.  
  197. // Add the "My Account" sub menus. 
  198. $wp_admin_nav[] = array( 
  199. 'parent' => buddypress()->my_account_menu_id,  
  200. 'id' => 'my-account-' . $this->id,  
  201. 'title' => __( 'Forums', 'buddypress' ),  
  202. 'href' => $forums_link 
  203. ); 
  204.  
  205. // Topics. 
  206. $wp_admin_nav[] = array( 
  207. 'parent' => 'my-account-' . $this->id,  
  208. 'id' => 'my-account-' . $this->id . '-topics-started',  
  209. 'title' => __( 'Topics Started', 'buddypress' ),  
  210. 'href' => $forums_link 
  211. ); 
  212.  
  213. // Replies. 
  214. $wp_admin_nav[] = array( 
  215. 'parent' => 'my-account-' . $this->id,  
  216. 'id' => 'my-account-' . $this->id . '-replies',  
  217. 'title' => __( 'Replies', 'buddypress' ),  
  218. 'href' => trailingslashit( $forums_link . 'replies' ) 
  219. ); 
  220.  
  221. // Favorites. 
  222. $wp_admin_nav[] = array( 
  223. 'parent' => 'my-account-' . $this->id,  
  224. 'id' => 'my-account-' . $this->id . '-favorite-topics',  
  225. 'title' => __( 'Favorite Topics', 'buddypress' ),  
  226. 'href' => trailingslashit( $forums_link . 'favorites' ) 
  227. ); 
  228.  
  229. parent::setup_admin_bar( $wp_admin_nav ); 
  230.  
  231. /** 
  232. * Set up the title for pages and the <title> element. 
  233. * 
  234. * @since 1.5.0 
  235. */ 
  236. public function setup_title() { 
  237.  
  238. // Adjust title based on view. 
  239. if ( bp_is_forums_component() ) { 
  240. $bp = buddypress(); 
  241.  
  242. if ( bp_is_my_profile() ) { 
  243. $bp->bp_options_title = __( 'Forums', 'buddypress' ); 
  244. } else { 
  245. $bp->bp_options_avatar = bp_core_fetch_avatar( array( 
  246. 'item_id' => bp_displayed_user_id(),  
  247. 'type' => 'thumb',  
  248. 'alt' => sprintf( __( 'Profile picture of %s', 'buddypress' ), bp_get_displayed_user_fullname() ) 
  249. ) ); 
  250. $bp->bp_options_title = bp_get_displayed_user_fullname(); 
  251.  
  252. parent::setup_title(); 
  253.  
  254. /** 
  255. * Disable cache groups due to a caching discrepancy in BackPress. 
  256. * 
  257. * @since 2.7.0 
  258. * @see https://buddypress.trac.wordpress.org/ticket/4759 
  259. */ 
  260. public function setup_cache_groups() { 
  261. wp_cache_add_non_persistent_groups( array( 
  262. // Posts. 
  263. 'bb_forums',  
  264. 'bb_query',  
  265. 'bb_cache_posts_post_ids',  
  266.  
  267. // Topics 
  268. 'bb_post' 
  269. ) ); 
  270.  
  271. /** 
  272. * Set up the bp-forums component. 
  273. * 
  274. * @since 1.6.0 
  275. */ 
  276. function bp_setup_forums() { 
  277. buddypress()->forums = new BP_Forums_Component(); 
  278. add_action( 'bp_setup_components', 'bp_setup_forums', 6 ); 
.