BuddyPress_Support_Topic

Loads BuddyPress Specific parts.

Defined (1)

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

/includes/buddypress.php  
  1. class BuddyPress_Support_Topic extends BP_Group_Extension { 
  2.  
  3. /** 
  4. * The constructor 
  5. * @since 2.0 
  6. * @uses BuddyPress_Support_Topic::enable_edit_item() to check if a forum is set for the group 
  7. * @uses BuddyPress_Support_Topic::setup_actions() to register some key actions and filters 
  8. * @uses BP_Group_Extension::init() 
  9. */ 
  10. public function __construct() { 
  11.  
  12. $args = array( 
  13. 'slug' => 'forum-support',  
  14. 'name' => 'Forum Support',  
  15. 'enable_nav_item' => false,  
  16. 'screens' => array( 
  17. 'create' => array( 'enabled' => false ),  
  18. 'edit' => array( 'enabled' => $this->enable_edit_item() ),  
  19. 'admin' => array( 'enabled' => false ),  
  20. ); 
  21.  
  22. parent::init( $args ); 
  23.  
  24. $this->setup_actions(); 
  25.  
  26.  
  27. /** 
  28. * Checks a forum is activated for the current group 
  29. * @since 2.0 
  30. * @uses bp_is_group() to check we're in a single group 
  31. * @uses bp_get_new_group_id() to get the created group id 
  32. * @uses bp_get_current_group_id() to get current group id 
  33. * @uses bp_get_new_group_enable_forum() to check forum is enabled 
  34. * @uses groups_get_groupmeta() to get the forum settings for this group 
  35. * @return boolean true|false 
  36. */ 
  37. public function enable_edit_item() { 
  38. // Bail if not viewing a single group 
  39. if ( ! bp_is_group() ) { 
  40. return; 
  41.  
  42. $group_id = bp_get_new_group_id(); 
  43.  
  44. if ( empty( $group_id ) ) { 
  45. $group_id = bp_get_current_group_id(); 
  46.  
  47. // Are forums enabled for this group? 
  48. $checked = bp_get_new_group_enable_forum() || groups_get_groupmeta( $group_id, 'forum_id' ); 
  49.  
  50. return (bool) $checked; 
  51.  
  52. /** 
  53. * Sets some key actions and filters 
  54. * @since 2.0 
  55. * @uses add_action() to hook our action to specific points 
  56. * @uses add_filter() to eventually modify some key parts 
  57. */ 
  58. public function setup_actions() { 
  59. // Enqueues a js script to hide/show the recipients 
  60. add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_forum_js' ) ); 
  61.  
  62. // Topic title in a BuddyPress group 
  63. add_filter( 'bbp_get_topic_title', array( $this, 'filter_topic_title' ), 10, 2 ); 
  64.  
  65. // removes above filter as soon as we can ! 
  66. add_filter( 'bbp_get_template_part', array( $this, 'remove_topic_title_filer' ), 100, 3 ); 
  67.  
  68. // adds the list of 'BuddyPress group mods' recipients to forum admin ui 
  69. add_action( 'bpbbpst_forum_support_options', array( $this, 'admin_group_mods_list' ), 10, 2 ); 
  70.  
  71. // saves the group recipients from forum admin 
  72. add_action( 'bpbbpst_forum_settings_updated', array( $this, 'admin_group_mods_list_save' ), 10, 2 ); 
  73.  
  74. // merges bp group specific recipients with bbPress ones 
  75. add_filter( 'bpbbpst_list_recipients', array( $this, 'merge_bp_recipients' ), 10, 3 ); 
  76.  
  77. /** 
  78. * Adds a javascript to WordPress scripts queue 
  79. * @since 2.0 
  80. * @uses bp_is_group_admin_screen() to make sure we're in plugin's group admin tab 
  81. * @uses wp_enqueue_script() to add the script to WordPress queue 
  82. * @uses bpbbpst_get_plugin_url() to get the plugin's url 
  83. * @uses bpbbpst_get_plugin_version() to get plugin's version 
  84. */ 
  85. public function enqueue_forum_js() { 
  86. if ( bp_is_group_admin_screen( 'forum-support' ) ) { 
  87. wp_enqueue_script( 'bpbbpst-forum-js', bpbbpst_get_plugin_url( 'js' ) . 'bpbbpst-forum.js', array( 'jquery' ), bpbbpst_get_plugin_version() ); 
  88.  
  89. /** 
  90. * Adds the support status to topic title when on single topic 
  91. * @since 2.0 
  92. * @param string $topic_title the topic title 
  93. * @uses bp_is_group_forum_topic() to check we're viewing a topic 
  94. * @uses bpbbpst_add_support_mention() to build the right support status 
  95. * @return string the topic title 
  96. */ 
  97. public function filter_topic_title( $topic_title = '', $topic_id = 0 ) { 
  98. // Avoid the prefix to be displayed in <title> 
  99. if ( ! did_action( 'bp_head' ) ) { 
  100. return $topic_title; 
  101.  
  102. if ( bp_is_group_forum_topic() ) { 
  103. return bpbbpst_add_support_mention( $topic_id, false ) . $topic_title ; 
  104. } else { 
  105. return $topic_title; 
  106.  
  107. /** 
  108. * Removes the above filter as soon as we can 
  109. * This prevents the filter to spread in places we don't want to. 
  110. * @since 2.0 
  111. * @param array $templates the list of templates 
  112. * @param string $slug 
  113. * @param string $name 
  114. * @uses remove_filter() to remove our filter on the topic title 
  115. * @return array the list of templates 
  116. */ 
  117. public function remove_topic_title_filer( $templates = array(), $slug = '', $name = '' ) { 
  118.  
  119. if ( in_array( $name, array( 'single-topic', 'topic' ) ) ) { 
  120. remove_filter( 'bbp_get_topic_title', array( $this, 'filter_topic_title' ), 10, 2 ); 
  121.  
  122. return $templates; 
  123.  
  124. /** 
  125. * Displays the content of plugin's group admin tab 
  126. * @since 2.0 
  127. * @param integer $group_id the group id 
  128. * @uses bbp_get_group_forum_ids() to get the forum id attached to this group 
  129. * @uses bpbbpst_get_forum_support_setting() to get the forum support setting 
  130. * @uses bpbbpst_display_forum_setting_options() to display the forum support available options 
  131. * @uses groups_get_groupmeta() to get a specific group setting 
  132. * @uses BuddyPress_Support_Topic::group_list_moderators() to build the list of admins and mods for the group 
  133. * @uses bpbbpst_array_checked() to eventually add a checked attribute to user's checkbox 
  134. * @uses bp_core_get_user_displayname() to get user's display name 
  135. * @return string html output 
  136. */ 
  137. public function edit_screen( $group_id = 0 ) { 
  138.  
  139. if ( empty( $group_id ) ) { 
  140. return false; 
  141.  
  142. $forum_ids = bbp_get_group_forum_ids( $group_id ); 
  143.  
  144. // Get the first forum ID 
  145. if ( ! empty( $forum_ids ) ) { 
  146. $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids; 
  147. } else { 
  148. return false; 
  149.  
  150. $support_feature = bpbbpst_get_forum_support_setting( $forum_id ); 
  151.  
  152. $style = ( $support_feature == 3 ) ? 'style="display:none"' : ''; 
  153.  
  154. bpbbpst_display_forum_setting_options( $support_feature ); 
  155.  
  156. $recipients = groups_get_groupmeta( $group_id, '_bpbbpst_support_bp_recipients' ); 
  157.  
  158. if ( empty( $recipients ) ) { 
  159. $recipients = array(); 
  160.  
  161. $group_forum_moderators = $this->group_list_moderators( $group_id ); 
  162. ?> 
  163. <div class="bpbbpst-mailing-list" <?php echo $style;?>> 
  164. <h4><?php _e( 'Who should receive an email notification when a new support topic is posted ?', 'buddy-bbpress-support-topic' );?></h4> 
  165. <input type="hidden" name="_bpbbpst_support_groups[]" value="<?php echo $group_id;?>"/> 
  166. <ul class="bp-moderators-list"> 
  167. <?php foreach ( $group_forum_moderators as $moderator ) : ?> 
  168. <li> 
  169. <input type="checkbox" value="<?php echo $moderator->user_id;?>" name="_bpbbpst_support_bp_recipients[<?php echo $group_id;?>][]" <?php bpbbpst_array_checked( $recipients, $moderator->user_id );?>> 
  170. <?php echo bp_core_get_user_displayname( $moderator->user_id ) ;?> (<?php echo $moderator->role ;?>) 
  171. </li> 
  172. <?php endforeach;?> 
  173. </ul> 
  174. </div> 
  175. <?php 
  176.  
  177. /** 
  178. * Saves the group settings 
  179. * @since 2.0 
  180. * @param integer $group_id the group id 
  181. * @uses bbp_get_group_forum_ids() to get group's forum ids 
  182. * @uses delete_post_meta() to eventually remove a forum setting 
  183. * @uses update_post_meta() to save the forum setting 
  184. * @uses BuddyPress_Support_Topic::admin_group_mods_list_save() to eventually save the recipients for this group 
  185. * @uses bp_core_add_message() to add a user's notice to inform him of success / fail 
  186. * @uses bp_core_redirect() to redirect user and avoid double post 
  187. * @uses bp_get_group_permalink() to get current group permalink 
  188. * @uses buddypress() to get BuddyPress main instance 
  189. */ 
  190. public function edit_screen_save( $group_id = 0 ) { 
  191. $success = false; 
  192.  
  193. $forum_ids = bbp_get_group_forum_ids( $group_id ); 
  194.  
  195. // Get the first forum ID 
  196. if ( ! empty( $forum_ids ) ) { 
  197. $forum_id = (int) is_array( $forum_ids ) ? $forum_ids[0] : $forum_ids; 
  198.  
  199. $support_feature = ! empty( $_POST['_bpbbpst_forum_settings'] ) ? intval( $_POST['_bpbbpst_forum_settings'] ) : 1; 
  200.  
  201. if ( 3 == $support_feature ) { 
  202. delete_post_meta( $forum_id, '_bpbbpst_support_recipients' ); 
  203.  
  204. $success = update_post_meta( $forum_id, '_bpbbpst_forum_settings', $support_feature ); 
  205.  
  206. // let's use admin function for recipients 
  207. $this->admin_group_mods_list_save( $forum_id , $support_feature ); 
  208.  
  209. if ( ! $success ) { 
  210. bp_core_add_message( __( 'There was an error saving, please try again', 'buddy-bbpress-support-topic' ), 'error' ); 
  211. } else { 
  212. bp_core_add_message( __( 'Settings saved successfully', 'buddy-bbpress-support-topic' ) ); 
  213.  
  214. bp_core_redirect( bp_get_group_permalink( buddypress()->groups->current_group ) . 'admin/' . $this->slug ); 
  215.  
  216. /** 
  217. * Builds a list of Moderators for the forum of the group (Admin + Mods) 
  218. * @since 2.0 
  219. * @param integer $forum_id the forum id 
  220. * @param string $style hide or show the recipients 
  221. * @uses bbp_get_group_forum_ids() to get group's forum ids 
  222. * @uses groups_get_groups() to build a detailled list of groups using the forum 
  223. * @uses esc_url() to sanitize url 
  224. * @uses bp_get_group_permalink() to build the group permalink 
  225. * @uses esc_html() to sanitize the group name 
  226. * @uses groups_get_groupmeta() to get the previously saved recipients 
  227. * @uses BuddyPress_Support_Topic::group_list_moderators() to get Group Admins and Mods 
  228. * @uses bpbbpst_array_checked() to eventually add a checked attribute to user's checkbox 
  229. * @uses bp_core_get_user_displayname() to get user's display name 
  230. * @return string html output 
  231. */ 
  232. public function admin_group_mods_list( $forum_id = 0, $style = '' ) { 
  233.  
  234. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  235.  
  236. if ( empty( $group_ids ) || ! is_array( $group_ids ) ) { 
  237. return false; 
  238.  
  239. /** a forum can be in several groups... But strangely the forum meta seems to only keep the latest group.. 
  240. it might evolved so let's keep the following this way, we'll be ready */ 
  241. $groups = groups_get_groups( array( 'include' => implode( ', ', $group_ids ) ) ); 
  242.  
  243. if ( empty( $groups ) ) { 
  244. return false; 
  245. ?> 
  246. <ul class="bpbbpst-mailing-list" <?php echo $style;?>> 
  247.  
  248. <?php foreach ( $groups['groups'] as $group ) : ?> 
  249.  
  250. <li> 
  251. <h4><?php printf( __( 'In Group: %s', 'buddy-bbpress-support-topic' ), '<a href="'. esc_url( bp_get_group_permalink( $group ) ) .'">'. esc_html( $group->name ) .'</a>' );?></h4> 
  252. <input type="hidden" name="_bpbbpst_support_groups[]" value="<?php echo $group->id;?>"/> 
  253. <ul class="bp-moderators-list"> 
  254.  
  255. <?php 
  256.  
  257. $recipients = $group_moderators = array(); 
  258. $recipients = groups_get_groupmeta( $group->id, '_bpbbpst_support_bp_recipients' ); 
  259. $group_moderators = $this->group_list_moderators( $group->id ); 
  260.  
  261. foreach ( $group_moderators as $moderator ) : ?> 
  262. <li> 
  263. <input type="checkbox" value="<?php echo $moderator->user_id;?>" name="_bpbbpst_support_bp_recipients[<?php echo $group->id;?>][]" <?php bpbbpst_array_checked( $recipients, $moderator->user_id );?>> 
  264. <?php echo bp_core_get_user_displayname( $moderator->user_id ) ;?> (<?php echo $moderator->role ;?>) 
  265. </li> 
  266. <?php endforeach ; ?> 
  267. </ul> 
  268.  
  269. </li> 
  270.  
  271. <?php endforeach;?> 
  272.  
  273. </ul> 
  274. <?php 
  275.  
  276. /** 
  277. * Saves the recipients list for the forum of the group 
  278. * @since 2.0 
  279. * @param integer $forum_id the forum id 
  280. * @param integer $support_feature the forum support setting 
  281. * @uses groups_delete_groupmeta() to eventually remove a group setting 
  282. * @uses groups_update_groupmeta() to save a group setting 
  283. */ 
  284. public function admin_group_mods_list_save( $forum_id = 0, $support_feature = 0 ) { 
  285.  
  286. if ( empty( $_POST['_bpbbpst_support_groups'] ) || ! is_array( $_POST['_bpbbpst_support_groups'] ) || empty( $forum_id ) ) { 
  287. return; 
  288.  
  289. $group_ids = array_map( 'intval', $_POST['_bpbbpst_support_groups'] ); 
  290.  
  291. if ( $support_feature == 3 ) { 
  292. foreach ( $group_ids as $group_id ) { 
  293. groups_delete_groupmeta( $group_id, '_bpbbpst_support_bp_recipients' ); 
  294.  
  295. } else { 
  296. $bp_recipients = ! empty( $_POST['_bpbbpst_support_bp_recipients'] ) ? $_POST['_bpbbpst_support_bp_recipients'] : false ; 
  297.  
  298. if ( ! empty( $bp_recipients ) && is_array( $bp_recipients ) && count( $bp_recipients ) > 0 ) { 
  299. foreach ( $group_ids as $group_id ) { 
  300. if ( ! empty( $bp_recipients[$group_id] ) && is_array( $bp_recipients[$group_id] ) && count( $bp_recipients[$group_id] ) > 0 ) { 
  301. groups_update_groupmeta( $group_id, '_bpbbpst_support_bp_recipients', array_map( 'intval', $bp_recipients[$group_id] ) ); 
  302. } else { 
  303. groups_delete_groupmeta( $group_id, '_bpbbpst_support_bp_recipients' ); 
  304.  
  305. } else { 
  306. foreach ( $group_ids as $group_id ) { 
  307. groups_delete_groupmeta( $group_id, '_bpbbpst_support_bp_recipients' ); 
  308.  
  309. /** 
  310. * In case of a notification, eventually adds group's forum recipients to bbPress ones 
  311. * @since 2.0 
  312. * @param array $recipients the list of bbPress moderators 
  313. * @param string $context are we notifying users ? 
  314. * @param integer $forum_id the forum id 
  315. * @uses bbp_get_group_forum_ids() to get group's forum ids 
  316. * @uses groups_get_groupmeta() to get the previously saved recipients 
  317. * @return array the list of forum moderators 
  318. */ 
  319. public function merge_bp_recipients( $recipients = array(), $context = 'admin', $forum_id = 0 ) { 
  320.  
  321. if ( $context == 'notify' && ! empty( $forum_id ) ) { 
  322. $group_ids = bbp_get_forum_group_ids( $forum_id ); 
  323.  
  324. if ( empty( $group_ids ) || ! is_array( $group_ids ) ) { 
  325. return $recipients; 
  326.  
  327. foreach ( $group_ids as $group_id ) { 
  328. $bp_recipients = groups_get_groupmeta( $group_id, '_bpbbpst_support_bp_recipients' ); 
  329.  
  330. if ( ! empty( $bp_recipients ) ) { 
  331. // strips common values 
  332. $bp_recipients = array_diff( $bp_recipients, $recipients ); 
  333. // merge unique 
  334. $recipients = array_merge( $recipients, $bp_recipients ); 
  335.  
  336.  
  337.  
  338. return $recipients; 
  339.  
  340. /** 
  341. * Builds the list of group mods and admins 
  342. * @since 2.0 
  343. * @param integer $group_id the group id 
  344. * @uses groups_get_group_admins() to get group admins 
  345. * @uses groups_get_group_mods() to get group mods 
  346. * @return array the list of group's forum admins and mods 
  347. */ 
  348. public static function group_list_moderators( $group_id = 0 ) { 
  349.  
  350. if ( empty( $group_id ) ) { 
  351. return false; 
  352.  
  353. $group_admins = array( 
  354. array( 
  355. 'users' => groups_get_group_admins( $group_id ),  
  356. 'role' => __( 'Group Admin', 'buddy-bbpress-support-topic' ) 
  357. ); 
  358.  
  359. $group_mods = array( 
  360. array( 
  361. 'users' => groups_get_group_mods( $group_id ),  
  362. 'role' => __( 'Group Mod', 'buddy-bbpress-support-topic' ) 
  363. ); 
  364.  
  365. $group_admins = array_map( 'bpbbpst_role_group_forum_map', $group_admins ); 
  366. $group_mods = array_map( 'bpbbpst_role_group_forum_map', $group_mods ); 
  367.  
  368. $group_forum_moderators = array_merge( $group_admins[0], $group_mods[0] ); 
  369.  
  370. return $group_forum_moderators; 
  371.  
  372. /** 
  373. * We do not use widgets 
  374. * @since 2.0 
  375. * @return boolean false 
  376. */ 
  377. public function widget_display() { return false; } 
  378.