/bp-forums/bp-forums-screens.php

  1. <?php 
  2. /** 
  3. * BuddyPress Forums Screen Functions. 
  4. * 
  5. * @package BuddyPress 
  6. * @subpackage ForumsScreens 
  7. * @since 1.5.0 
  8. */ 
  9.  
  10. // Exit if accessed directly. 
  11. defined( 'ABSPATH' ) || exit; 
  12.  
  13. /** 
  14. * Load the Forums directory. 
  15. * 
  16. * @since 1.1.0 
  17. */ 
  18. function bp_forums_directory_forums_setup() { 
  19.  
  20. // Get BuddyPress once. 
  21. $bp = buddypress(); 
  22.  
  23. if ( bp_is_forums_component() && ( !bp_current_action() || ( 'tag' == bp_current_action() && bp_action_variables() ) ) && !bp_current_item() ) { 
  24. if ( !bp_forums_has_directory() ) 
  25. return false; 
  26.  
  27. if ( !bp_forums_is_installed_correctly() ) { 
  28. bp_core_add_message( __( 'The forums component has not been set up yet.', 'buddypress' ), 'error' ); 
  29. bp_core_redirect( bp_get_root_domain() ); 
  30.  
  31. bp_update_is_directory( true, 'forums' ); 
  32.  
  33. /** 
  34. * Fires early in the initialization of bbPress-based areas of BuddyPress. 
  35. * 
  36. * @since 1.1.0 
  37. */ 
  38. do_action( 'bbpress_init' ); 
  39.  
  40. // Check to see if the user has posted a new topic from the forums page. 
  41. if ( isset( $_POST['submit_topic'] ) && bp_is_active( 'forums' ) ) { 
  42. check_admin_referer( 'bp_forums_new_topic' ); 
  43.  
  44. $bp->groups->current_group = groups_get_group( array( 'group_id' => $_POST['topic_group_id'] ) ); 
  45. if ( !empty( $bp->groups->current_group->id ) ) { 
  46. // Auto join this user if they are not yet a member of this group. 
  47. if ( !bp_current_user_can( 'bp_moderate' ) && 'public' == $bp->groups->current_group->status && !groups_is_user_member( bp_loggedin_user_id(), $bp->groups->current_group->id ) ) 
  48. groups_join_group( $bp->groups->current_group->id ); 
  49.  
  50. $error_message = ''; 
  51.  
  52. $forum_id = groups_get_groupmeta( $bp->groups->current_group->id, 'forum_id' ); 
  53. if ( !empty( $forum_id ) ) { 
  54. if ( empty( $_POST['topic_title'] ) ) 
  55. $error_message = __( 'Please provide a title for your forum topic.', 'buddypress' ); 
  56. else if ( empty( $_POST['topic_text'] ) ) 
  57. $error_message = __( 'Forum posts cannot be empty. Please enter some text.', 'buddypress' ); 
  58.  
  59. if ( $error_message ) { 
  60. bp_core_add_message( $error_message, 'error' ); 
  61. $redirect = bp_get_group_permalink( $bp->groups->current_group ) . 'forum'; 
  62. } else { 
  63. if ( !$topic = groups_new_group_forum_topic( $_POST['topic_title'], $_POST['topic_text'], $_POST['topic_tags'], $forum_id ) ) { 
  64. bp_core_add_message( __( 'There was an error when creating the topic', 'buddypress'), 'error' ); 
  65. $redirect = bp_get_group_permalink( $bp->groups->current_group ) . 'forum'; 
  66. } else { 
  67. bp_core_add_message( __( 'The topic was created successfully', 'buddypress') ); 
  68. $redirect = bp_get_group_permalink( $bp->groups->current_group ) . 'forum/topic/' . $topic->topic_slug . '/'; 
  69.  
  70. bp_core_redirect( $redirect ); 
  71.  
  72. } else { 
  73. bp_core_add_message( __( 'Please pick the group forum where you would like to post this topic.', 'buddypress' ), 'error' ); 
  74. bp_core_redirect( add_query_arg( 'new', '', bp_get_forums_directory_permalink() ) ); 
  75.  
  76. } else { 
  77. bp_core_add_message( __( 'Please pick the group forum where you would like to post this topic.', 'buddypress' ), 'error' ); 
  78. bp_core_redirect( add_query_arg( 'new', '', bp_get_forums_directory_permalink() ) ); 
  79.  
  80. /** 
  81. * Fires right before the loading of the forums directory screen template file. 
  82. * 
  83. * @since 1.1.0 
  84. */ 
  85. do_action( 'bp_forums_directory_forums_setup' ); 
  86.  
  87. /** 
  88. * Filters the template to load for the forums directory screen. 
  89. * 
  90. * @since 1.1.0 
  91. * 
  92. * @param string $template Path to the forums template to load. 
  93. */ 
  94. bp_core_load_template( apply_filters( 'bp_forums_template_directory_forums_setup', 'forums/index' ) ); 
  95. add_action( 'bp_screens', 'bp_forums_directory_forums_setup', 2 ); 
  96.  
  97. /** 
  98. * Load the Topics Started screen. 
  99. * 
  100. * @since 1.5.0 
  101. */ 
  102. function bp_member_forums_screen_topics() { 
  103.  
  104. /** 
  105. * Fires right before the loading of the forums topics started screen template file. 
  106. * 
  107. * @since 1.5.0 
  108. */ 
  109. do_action( 'bp_member_forums_screen_topics' ); 
  110.  
  111. /** 
  112. * Filters the template to load for the forums topics started screen. 
  113. * 
  114. * @since 1.5.0 
  115. * 
  116. * @param string $template Path to the forums topics started template to load. 
  117. */ 
  118. bp_core_load_template( apply_filters( 'bp_member_forums_screen_topics', 'members/single/home' ) ); 
  119.  
  120. /** 
  121. * Load the Replied To screen. 
  122. * 
  123. * @since 1.5.0 
  124. */ 
  125. function bp_member_forums_screen_replies() { 
  126.  
  127. /** 
  128. * Fires right before the loading of the forums replied to screen template file. 
  129. * 
  130. * @since 1.5.0 
  131. */ 
  132. do_action( 'bp_member_forums_screen_replies' ); 
  133.  
  134. /** 
  135. * Filters the template to load for the forums replied to screen. 
  136. * 
  137. * @since 1.5.0 
  138. * 
  139. * @param string $template Path to the forums replied to template to load. 
  140. */ 
  141. bp_core_load_template( apply_filters( 'bp_member_forums_screen_replies', 'members/single/home' ) ); 
  142.  
  143. /** 
  144. * Load the template content for a user's Favorites forum tab. 
  145. * 
  146. * Note that this feature is not fully implemented at the moment. 
  147. * 
  148. * @since 1.5.0 
  149. */ 
  150. function bp_member_forums_screen_favorites() { 
  151.  
  152. /** 
  153. * Fires right before the loading of the forums favorites screen template file. 
  154. * 
  155. * @since 1.5.0 
  156. */ 
  157. do_action( 'bp_member_forums_screen_favorites' ); 
  158.  
  159. /** 
  160. * Filters the template to load for the forums favorites screen. 
  161. * 
  162. * @since 1.5.0 
  163. * 
  164. * @param string $template Path to the forums favorites template to load. 
  165. */ 
  166. bp_core_load_template( apply_filters( 'bp_member_forums_screen_favorites', 'members/single/home' ) ); 
  167.  
  168. /** 
  169. * Load a single forum page. 
  170. * 
  171. * @since 1.5.0 
  172. */ 
  173. function bp_forums_screen_single_forum() { 
  174.  
  175. if ( !bp_is_forums_component() || !bp_is_current_action( 'forum' ) || !bp_action_variable( 0 ) ) 
  176. return false; 
  177.  
  178. /** 
  179. * Fires right before the loading of the forums single forum screen template file. 
  180. * 
  181. * @since 1.5.0 
  182. */ 
  183. do_action( 'bp_forums_screen_single_forum' ); 
  184.  
  185. /** 
  186. * Filters the template to load for the forums single forum screen. 
  187. * 
  188. * @since 1.5.0 
  189. * 
  190. * @param string $template Path to the forums single forum template to load. 
  191. */ 
  192. bp_core_load_template( apply_filters( 'bp_forums_screen_single_forum', 'forums/single/forum' ) ); 
  193. add_action( 'bp_screens', 'bp_forums_screen_single_forum' ); 
  194.  
  195. /** 
  196. * Load a single forum topic page. 
  197. * 
  198. * @since 1.5.0 
  199. */ 
  200. function bp_forums_screen_single_topic() { 
  201.  
  202. if ( !bp_is_forums_component() || !bp_is_current_action( 'topic' ) || !bp_action_variable( 0 ) ) 
  203. return false; 
  204.  
  205. /** 
  206. * Fires right before the loading of the forums single topic screen template file. 
  207. * 
  208. * @since 1.5.0 
  209. */ 
  210. do_action( 'bp_forums_screen_single_topic' ); 
  211.  
  212. /** 
  213. * Filters the template to load for the forums single topic screen. 
  214. * 
  215. * @since 1.5.0 
  216. * 
  217. * @param string $template Path to the forums single topic template to load. 
  218. */ 
  219. bp_core_load_template( apply_filters( 'bp_forums_screen_single_topic', 'forums/single/topic' ) ); 
  220. add_action( 'bp_screens', 'bp_forums_screen_single_topic' ); 
  221.  
  222.  
  223. /** Theme Compatibility *******************************************************/ 
  224.  
  225. /** 
  226. * The main theme compat class for legacy BuddyPress forums. 
  227. * 
  228. * This class sets up the necessary theme compatibility actions to safely output 
  229. * old forum template parts to the_title and the_content areas of a theme. 
  230. * 
  231. * @since 1.7.0 
  232. */ 
  233. class BP_Forum_Legacy_Theme_Compat { 
  234.  
  235. /** 
  236. * Set up theme compatibility for the legacy forums component. 
  237. * 
  238. * @since 1.7.0 
  239. */ 
  240. public function __construct() { 
  241. add_action( 'bp_setup_theme_compat', array( $this, 'is_legacy_forum' ) ); 
  242.  
  243. /** 
  244. * Are we looking at something that needs old forum theme compatibility? 
  245. * 
  246. * @since 1.7.0 
  247. */ 
  248. public function is_legacy_forum() { 
  249.  
  250. // Bail if not looking at a group. 
  251. if ( ! bp_is_forums_component() ) 
  252. return; 
  253.  
  254. // Forum Directory. 
  255. if ( ( ! bp_current_action() || ( 'tag' == bp_current_action() && bp_action_variables() ) ) && ! bp_current_item() ) { 
  256.  
  257. if ( ! bp_forums_has_directory() ) 
  258. return false; 
  259.  
  260. if ( ! bp_forums_is_installed_correctly() ) { 
  261. bp_core_add_message( __( 'The forums component has not been set up yet.', 'buddypress' ), 'error' ); 
  262. bp_core_redirect( bp_get_root_domain() ); 
  263.  
  264. bp_update_is_directory( true, 'forums' ); 
  265.  
  266. do_action( 'bp_forums_directory_forums_setup' ); 
  267.  
  268. add_action( 'bp_template_include_reset_dummy_post_data', array( $this, 'directory_dummy_post' ) ); 
  269. add_filter( 'bp_replace_the_content', array( $this, 'directory_content' ) ); 
  270.  
  271.  
  272.  
  273. /** Directory *************************************************************/ 
  274.  
  275. /** 
  276. * Update the global $post with directory data. 
  277. * 
  278. * @since 1.7.0 
  279. */ 
  280. public function directory_dummy_post() { 
  281.  
  282. // Title based on ability to create groups. 
  283. if ( is_user_logged_in() ) { 
  284. $title = __( 'Forums', 'buddypress' ) . ' <a class="button show-hide-new bp-title-button" href="#new-topic" id="new-topic-button">' . __( 'New Topic', 'buddypress' ) . '</a>'; 
  285. } else { 
  286. $title = __( 'Forums', 'buddypress' ); 
  287.  
  288. bp_theme_compat_reset_post( array( 
  289. 'ID' => 0,  
  290. 'post_title' => $title,  
  291. 'post_author' => 0,  
  292. 'post_date' => 0,  
  293. 'post_content' => '',  
  294. 'post_type' => 'page',  
  295. 'post_status' => 'publish',  
  296. 'is_page' => true,  
  297. 'comment_status' => 'closed' 
  298. ) ); 
  299.  
  300. /** 
  301. * Filter the_content with the old forum index template part. 
  302. * 
  303. * @since 1.7.0 
  304. */ 
  305. public function directory_content() { 
  306. return bp_buffer_template_part( 'forums/index', null, false ); 
  307. new BP_Forum_Legacy_Theme_Compat(); 
.