/includes/functions.php

  1. <?php 
  2.  
  3. // Exit if accessed directly 
  4. if ( ! defined( 'ABSPATH' ) ) exit; 
  5.  
  6. /** 
  7. * Gets the plugin's version 
  8. * 
  9. * @since 2.0 
  10. * 
  11. * @uses bbpress() the main bbPress instance 
  12. * @return string the plugin version 
  13. */ 
  14. function bpbbpst_get_plugin_version() { 
  15. return bbpress()->extend->bpbbpst->globals->version; 
  16.  
  17. /** 
  18. * Gets the plugin's url 
  19. * 
  20. * @since 2.0 
  21. * 
  22. * @uses bbpress() the main bbPress instance 
  23. * @return string the plugin url 
  24. */ 
  25. function bpbbpst_get_plugin_url( $subfolder = '' ) { 
  26. return trailingslashit( bbpress()->extend->bpbbpst->globals->plugin_url . $subfolder ); 
  27.  
  28. /** 
  29. * Gets the plugin's path 
  30. * 
  31. * @since 2.0 
  32. * 
  33. * @uses bbpress() the main bbPress instance 
  34. * @return string the plugin path 
  35. */ 
  36. function bpbbpst_get_plugin_dir( $subfolder = '' ) { 
  37. return trailingslashit( bbpress()->extend->bpbbpst->globals->plugin_dir . $subfolder ); 
  38.  
  39. /** 
  40. * Gets the plugin's includes url 
  41. * 
  42. * @since 2.0 
  43. * 
  44. * @uses bbpress() the main bbPress instance 
  45. * @return string the plugin includes url 
  46. */ 
  47. function bpbbpst_get_includes_url( $subfolder = '' ) { 
  48. return trailingslashit( bbpress()->extend->bpbbpst->globals->includes_url . $subfolder ); 
  49.  
  50. /** 
  51. * Gets the plugin's includes path 
  52. * 
  53. * @since 2.0 
  54. * 
  55. * @uses bbpress() the main bbPress instance 
  56. * @return string the plugin includes path 
  57. */ 
  58. function bpbbpst_get_includes_dir( $subfolder = '' ) { 
  59. return trailingslashit( bbpress()->extend->bpbbpst->globals->includes_dir . $subfolder ); 
  60.  
  61. /** 
  62. * Gets the registered support statuses 
  63. * 
  64. * @since 2.0 
  65. * 
  66. * @uses bbpress() the main bbPress instance 
  67. * @uses apply_filters() to let plugins or themes modify the value 
  68. * @return array the support statuses 
  69. */ 
  70. function bpbbpst_get_support_status() { 
  71. $bpbbpst = bbpress()->extend->bpbbpst; 
  72.  
  73. return apply_filters( 'bpbbpst_get_support_status', bbpress()->extend->bpbbpst->support_status ); 
  74.  
  75. /** 
  76. * Returns bbPress required version. 
  77. * 
  78. * @since 2.0 
  79. * 
  80. * @uses bbpress() the main bbPress instance 
  81. * @return bool true if required version is ok, false otherwise 
  82. */ 
  83. function bpbbpst_bbp_required_version() { 
  84. return bbpress()->extend->bpbbpst->globals->required_bbp_version; 
  85.  
  86. /** 
  87. * Informs if required version of bbPress is ok. 
  88. * 
  89. * @since 2.0 
  90. * 
  91. * @uses bbpress() the main bbPress instance 
  92. * @return bool true if required version is ok, false otherwise 
  93. */ 
  94. function bpbbpst_is_bbp_required_version_ok() { 
  95. return (bool) bbpress()->extend->bpbbpst->globals->site_bbp_version_ok; 
  96.  
  97. /** 
  98. * We're in a dynamic sidebar 
  99. * 
  100. * @since 2.0 
  101. * 
  102. * @uses bbpress() the main bbPress instance 
  103. */ 
  104. function bpbbpst_set_sidebar_true() { 
  105. bbpress()->extend->bpbbpst->globals->is_sidebar = true; 
  106.  
  107. /** 
  108. * We're no more in a dynamic sidebar 
  109. * 
  110. * @since 2.0 
  111. * 
  112. * @uses bbpress() the main bbPress instance 
  113. */ 
  114. function bpbbpst_set_sidebar_false() { 
  115. bbpress()->extend->bpbbpst->globals->is_sidebar = false; 
  116.  
  117. /** 
  118. * Are we in a dynamic sidebar ? 
  119. * 
  120. * @since 2.0 
  121. * 
  122. * @uses bbpress() the main bbPress instance 
  123. * @return bool true if inside a dynamic sidebar, false otherwise 
  124. */ 
  125. function bpbbpst_is_sidebar() { 
  126. return (bool) bbpress()->extend->bpbbpst->globals->is_sidebar; 
  127.  
  128. /** 
  129. * Gets the forum setting for support feature 
  130. * 
  131. * @since 2.0 
  132. * 
  133. * @param integer $forum_id the forum id 
  134. * @uses get_post_meta() to get the forum support setting 
  135. * @uses apply_filters() to let plugins or themes modify the value 
  136. * @return integer the forum support setting 
  137. */ 
  138. function bpbbpst_get_forum_support_setting( $forum_id = 0 ) { 
  139. if ( empty( $forum_id ) ) { 
  140. return false; 
  141.  
  142. $forum_support_setting = get_post_meta( $forum_id, '_bpbbpst_forum_settings', true ); 
  143.  
  144. if ( empty( $forum_support_setting ) ) { 
  145. $forum_support_setting = 1; 
  146.  
  147. return apply_filters( 'bpbbpst_get_forum_support_setting', intval( $forum_support_setting ) ); 
  148.  
  149. /** 
  150. * Get the New Topic form intro for support only forums 
  151. * 
  152. * @since 2.1.0 
  153. * 
  154. * @param integer $forum_id the forum id 
  155. * @return string the support forum intro 
  156. */ 
  157. function bpbbpst_get_forum_support_topic_intro( $forum_id = 0 ) { 
  158. if ( empty( $forum_id ) ) { 
  159. $forum_id = bbp_get_forum_id(); 
  160.  
  161. if ( ! $forum_id ) { 
  162. return ''; 
  163.  
  164. $support_intro = get_post_meta( $forum_id, '_bpbbpst_support_topic_intro', true ); 
  165.  
  166. if ( ! empty( $support_intro ) ) { 
  167. $support_intro = wp_kses( $support_intro, wp_kses_allowed_html( 'forum' ) ); 
  168.  
  169. return apply_filters( 'bpbbpst_get_forum_support_topic_intro', $support_intro ); 
  170.  
  171. /** 
  172. * Get the New Topic form intro for support only forums 
  173. * 
  174. * @since 2.1.0 
  175. * 
  176. * @param integer $forum_id the forum id 
  177. * @return string the support forum intro 
  178. */ 
  179. function bpbbpst_get_forum_support_topic_template( $forum_id = 0 ) { 
  180. if ( empty( $forum_id ) ) { 
  181. $forum_id = bbp_get_forum_id(); 
  182.  
  183. if ( ! $forum_id ) { 
  184. return ''; 
  185.  
  186. $support_guide_tpl = get_post_meta( $forum_id, '_bpbbpst_support_topic_tpl', true ); 
  187.  
  188. if ( ! empty( $support_guide_tpl ) ) { 
  189. $support_guide_tpl = esc_html( $support_guide_tpl ); 
  190.  
  191. return apply_filters( 'bpbbpst_get_forum_support_topic_template', $support_guide_tpl ); 
  192.  
  193. /** 
  194. * Outputs a field to specify the topic is a support one 
  195. * 
  196. * First checks for parent forum support setting 
  197. * 
  198. * @since 2.0 
  199. * 
  200. * @uses bbp_get_forum_id() to get the parent forum id 
  201. * @uses bbp_get_topic_id() to get the topic id 
  202. * @uses bbp_get_topic_forum_id() to have a fallback to get parent forum id thanks to topic id 
  203. * @uses bpbbpst_get_forum_support_setting() to get the parent forum setting for support feature 
  204. * @uses bbp_is_topic_edit() to check if the topic is being edited from front end 
  205. * @uses get_post_meta() to get topic's support option 
  206. * @uses do_action() to let plugins or themes run some actions from this point 
  207. * @uses wp_nonce_field() for security reasons 
  208. * @return string the html output 
  209. */ 
  210. function bpbbpst_maybe_output_support_field() { 
  211. $checked = $output = false; 
  212. $forum_id = bbp_get_forum_id(); 
  213. $topic_id = bbp_get_topic_id(); 
  214.  
  215. if ( empty( $forum_id ) ) { 
  216. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  217.  
  218. $parent_forum_support_feature = bpbbpst_get_forum_support_setting( $forum_id ); 
  219.  
  220. switch( $parent_forum_support_feature ) { 
  221.  
  222. case 2: 
  223. $output = '<input type="hidden" value="support" name="_bp_bbp_st_is_support" id="bp_bbp_st_is_support_hidden">'; 
  224. break; 
  225.  
  226. case 3: 
  227. $output = false; 
  228. break; 
  229.  
  230. case 1: 
  231. default: 
  232. if ( bbp_is_topic_edit() ) { 
  233.  
  234. $support_status = get_post_meta( $topic_id, '_bpbbpst_support_topic', true ); 
  235.  
  236. if ( ! empty( $support_status ) ) { 
  237. $checked = true; 
  238.  
  239. $output = '<label for="bp_bbp_st_is_support"><input type="checkbox" value="support" name="_bp_bbp_st_is_support" id="bp_bbp_st_is_support" '. checked( true, $checked, false ).'> '. __('This is a support topic', 'buddy-bbpress-support-topic') . '</label>' ; 
  240. break; 
  241.  
  242. if ( empty( $output ) ) { 
  243. return false; 
  244. ?> 
  245. <p> 
  246. <?php echo $output;?> 
  247.  
  248. <?php do_action( 'bpbbpst_output_support_extra_field', $parent_forum_support_feature );?> 
  249.  
  250. <?php wp_nonce_field( 'bpbbpst_support_define', '_wpnonce_bpbbpst_support_define' ); ?> 
  251. </p> 
  252. <?php 
  253.  
  254. /** 
  255. * Hooks bbp_new_topic_post_extras to safely insert the support request 
  256. * 
  257. * @since 2.0 
  258. * 
  259. * @param integer $topic_id the id of the topic 
  260. * @uses wp_verify_nonce() to check nonce 
  261. * @uses update_post_meta() to set the support status to support request 
  262. * @uses esc_url_raw() to escape the referer before inserting it in db 
  263. * @uses do_action() to let plugins or themes run some actions from this point 
  264. */ 
  265. function bpbbpst_save_support_type( $topic_id = 0 ) { 
  266. // if safe then store 
  267. if ( ! empty( $_POST['_bp_bbp_st_is_support'] ) && wp_verify_nonce( $_POST['_wpnonce_bpbbpst_support_define'], 'bpbbpst_support_define' ) ) { 
  268. // no need to sanitize value as i arbitrary set the support topic option to 1 
  269. update_post_meta( $topic_id, '_bpbbpst_support_topic', 1 ); 
  270.  
  271. if ( ! empty( $_POST['_bp_bbp_st_referer'] ) ) { 
  272. update_post_meta( $topic_id, '_bpbbpst_support_referer', esc_url_raw( $_POST['_bp_bbp_st_referer'] ) ); 
  273.  
  274. do_action( 'bpbbpst_support_type_saved' ); 
  275.  
  276. /** 
  277. * Hooks bbp_edit_topic_post_extras to update the support status when topic is edited 
  278. * 
  279. * @since 2.0 
  280. * 
  281. * @param integer $topic_id the id of the topic 
  282. * @uses wp_verify_nonce() to check nonce 
  283. * @uses get_post_meta() to get the stored status if it exists 
  284. * @uses update_post_meta() to edit the support status to the previous status, defaults to support request 
  285. * @uses delete_post_meta() to remove the support status 
  286. * @uses do_action() to let plugins or themes run some actions from this point 
  287. */ 
  288. function bpbbpst_edit_support_type( $topic_id = 0 ) { 
  289.  
  290. if ( empty( $_POST['_wpnonce_bpbbpst_support_define'] ) || ! wp_verify_nonce( $_POST['_wpnonce_bpbbpst_support_define'], 'bpbbpst_support_define' ) ) { 
  291. return; 
  292.  
  293. if ( ! empty( $_POST['_bp_bbp_st_is_support'] ) ) { 
  294. $support = get_post_meta( $topic_id, '_bpbbpst_support_topic', true ); 
  295.  
  296. $support = empty( $support ) ? 1 : $support; 
  297. update_post_meta( $topic_id, '_bpbbpst_support_topic', $support ); 
  298. } else { 
  299. $support = get_post_meta( $topic_id, '_bpbbpst_support_topic', true ); 
  300.  
  301. if ( ! empty( $support ) ) { 
  302. delete_post_meta( $topic_id, '_bpbbpst_support_topic' ); 
  303.  
  304. do_action( 'bpbbpst_support_type_edited'); 
  305.  
  306. /** 
  307. * Handles topic moves from a forum to another 
  308. * 
  309. * @since 2.0 
  310. * 
  311. * @param integer $topic_id the topic id 
  312. * @param integer $forum_id the new forum id 
  313. * @uses bbp_get_topic_forum_id() to get old forum id 
  314. * @uses bpbbpst_get_forum_support_setting() to get old and new forum support setting 
  315. * @uses delete_post_meta() to eventually remove a support status 
  316. * @uses get_post_meta() to get a previously saved support status 
  317. * @uses update_post_meta() to save a default support status if needed. 
  318. */ 
  319. function bpbbpst_handle_moving_topic( $topic_id = 0, $forum_id = 0 ) { 
  320. if ( empty( $topic_id ) || empty( $forum_id ) ) { 
  321. return; 
  322.  
  323. $old_forum_id = bbp_get_topic_forum_id( $topic_id ); 
  324.  
  325. //if old is new, then do nothing ! 
  326. if ( $old_forum_id == $forum_id ) { 
  327. return; 
  328.  
  329. $old_forum_support_feature = bpbbpst_get_forum_support_setting( $old_forum_id ); 
  330. $new_forum_support_feature = bpbbpst_get_forum_support_setting( $forum_id ); 
  331.  
  332. //if old has same support feature than new, then do nothing 
  333. if ( $old_forum_support_feature == $new_forum_support_feature ) { 
  334. return; 
  335.  
  336. // at this point it means old had a support feature and new one no 
  337. if ( $new_forum_support_feature == 3 ) { 
  338. // delete_post_meta will be handled by bpbbpst_bbpress_edit_support_type 
  339. if ( isset( $_POST['_bp_bbp_st_is_support'] ) ) { 
  340. unset( $_POST['_bp_bbp_st_is_support'] ); 
  341.  
  342. if ( isset( $_POST['_support_status'] ) ) { 
  343. unset( $_POST['_support_status'] ); 
  344.  
  345. delete_post_meta( $topic_id, '_bpbbpst_support_topic' ); 
  346.  
  347. } else { 
  348. $meta = get_post_meta( $topic_id, '_bpbbpst_support_topic', true ); 
  349.  
  350. if ( $old_forum_support_feature == 3 ) { 
  351. // in this case nonce is not set 
  352. if ( $new_forum_support_feature == 2 ) { 
  353. $meta = 1; 
  354.  
  355. update_post_meta( $topic_id, '_bpbbpst_support_topic', $meta ); 
  356. } else { 
  357. if ( empty( $_POST['_bp_bbp_st_is_support'] ) && $new_forum_support_feature == 2 ) { 
  358. $_POST['_bp_bbp_st_is_support'] = 'support'; 
  359.  
  360. if ( ! empty( $_POST['_bp_bbp_st_is_support'] ) && empty( $meta ) && $new_forum_support_feature == 1 ) { 
  361. unset( $_POST['_bp_bbp_st_is_support'] ); 
  362.  
  363. if ( empty( $_POST['_support_status'] ) && empty( $meta ) && $new_forum_support_feature == 2 ) { 
  364. $_POST['_support_status'] = 1; 
  365.  
  366.  
  367. /** 
  368. * Hooks wp_enqueue_scripts to load needed js or css 
  369. * 
  370. * It only loads the scripts or stylesheets only on the template 
  371. * where we need it (not on all page of the blog!) 
  372. * 
  373. * @since 2.0 
  374. * 
  375. * @uses bbp_is_single_topic() to check if topic is on single template 
  376. * @uses bp_is_group_forum_topic() in case BuddyPress is activated 
  377. * @uses wp_enqueue_script() to enqueue the script and its dependencies 
  378. * @uses bpbbpst_get_plugin_url() to get plugin's url 
  379. * @uses bpbbpst_get_plugin_version() to get plugin's version 
  380. * @uses wp_localize_script() to ensure translation of messages 
  381. */ 
  382. function bpbbpst_enqueue_scripts() { 
  383.  
  384. /** 
  385. With BuddyPress activated, bbp_is_single_topic() is becoming true too late :( 
  386. so we need to check for bp_is_group_forum_topic(), if this function exists ;) 
  387. */ 
  388. $bbpress_load_scripts = false; 
  389.  
  390. if ( bbp_is_single_topic() ) { 
  391. $bbpress_load_scripts = true; 
  392. } else if ( function_exists( 'bp_is_group_forum_topic' ) && bp_is_group_forum_topic() ) { 
  393. $bbpress_load_scripts = true; 
  394.  
  395. if ( $bbpress_load_scripts ) { 
  396. wp_enqueue_script( 'bpbbpst-topic-js', bpbbpst_get_plugin_url( 'js' ) . 'bpbbpst-topic.js', array( 'jquery' ), bpbbpst_get_plugin_version(), true ); 
  397. wp_localize_script( 'bpbbpst-topic-js', 'bpbbpstbbp_vars', array( 
  398. 'securitycheck' => __( 'Security check failed', 'buddy-bbpress-support-topic' ),  
  399. 'loading' => __( 'loading', 'buddy-bbpress-support-topic' ) 
  400. ) ); 
  401.  
  402. /** 
  403. * Handles Ajax calls and change the support status 
  404. * 
  405. * @since 2.0 
  406. * 
  407. * @uses wp_verify_nonce() to check nonce 
  408. * @uses delete_post_meta() to delete the support status 
  409. * @uses intval() to sanitize input 
  410. * @uses update_post_meta() to edit the support status 
  411. * @uses die() to make sure the script is exited 
  412. */ 
  413. function bpbbpst_change_support_status() { 
  414.  
  415. if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) { 
  416. return; 
  417.  
  418. if ( ! wp_verify_nonce( $_POST['_wpnonce_bpbbpst_support_status'], 'bpbbpst_support_status' ) ) { 
  419. echo -1; 
  420. die(); 
  421.  
  422. if ( ! empty( $_POST['topic_id'] ) ) { 
  423.  
  424. if ( empty( $_POST['support_status'] ) ) { 
  425. delete_post_meta( $_POST['topic_id'], '_bpbbpst_support_topic' ); 
  426. } else { 
  427. update_post_meta( $_POST['topic_id'], '_bpbbpst_support_topic', intval( $_POST['support_status'] ) ); 
  428. echo 1; 
  429. } else { 
  430. echo 0; 
  431. die(); 
  432.  
  433. /** 
  434. * Hooks bbp_get_topic_admin_links to eventually add the selectbox of available support status 
  435. * 
  436. * @since 2.0 
  437. * 
  438. * @param string $input the html containing bbPress admin links 
  439. * @param array $args the array containing eventual args 
  440. * @uses bbp_is_single_topic() to return input if not on single topic 
  441. * @uses bbp_get_topic_id() to get the topic id 
  442. * @uses bbp_parse_args() to merge user defined arguments into defaults array 
  443. * @uses bbp_get_topic_forum_id() to get parent forum id 
  444. * @uses bpbbpst_get_forum_support_setting() to check for parent forum support setting 
  445. * @uses current_user_can() to check user capacity to edit the topic 
  446. * @uses get_post_meta() to get the stored status if it exists 
  447. * @uses bpbbpst_get_selectbox() to get the selectbox of available status 
  448. * @uses apply_filters() to call the 'bpbbpst_support_admin_links' hook 
  449. * @return string $input or the input with the selectbox 
  450. */ 
  451. function bpbbpst_support_admin_links( $input = '', $args = array() ) { 
  452.  
  453. if ( ! bbp_is_single_topic() ) { 
  454. return $input; 
  455.  
  456. $defaults = array ( 
  457. 'id' => bbp_get_topic_id(),  
  458. 'before' => '<span class="bbp-admin-links">',  
  459. 'after' => '</span>',  
  460. 'sep' => ' | ',  
  461. 'links' => array() 
  462. ); 
  463.  
  464. $r = bbp_parse_args( $args, $defaults, 'get_topic_admin_links' ); 
  465.  
  466. // Since 2.0, we first need to check parent forum has support for support :) 
  467. $forum_id = bbp_get_topic_forum_id( $r['id'] ); 
  468.  
  469. if ( empty( $forum_id ) ) { 
  470. return $input; 
  471.  
  472. if ( 3 == bpbbpst_get_forum_support_setting( $forum_id ) ) { 
  473. return $input; 
  474.  
  475. // now let's check the post meta ! 
  476. $support_status = get_post_meta( $r['id'], '_bpbbpst_support_topic', true ); 
  477.  
  478. if ( current_user_can( 'edit_topic', $r['id'] ) && ! empty( $support_status ) ) { 
  479.  
  480. $support_selectbox = bpbbpst_get_selectbox( $support_status, $r['id'] ) . $r['sep'] ; 
  481.  
  482. $new_span = str_replace( $r['before'], $r['before'] . $support_selectbox, $input ); 
  483.  
  484. return apply_filters( 'bpbbpst_support_admin_links', $new_span, $args ); 
  485.  
  486. } else { 
  487. return $input; 
  488.  
  489.  
  490. /** 
  491. * Gets the selected status from the list of available ones 
  492. * 
  493. * @since 2.0 
  494. * 
  495. * @param integer $selected the value of the support status 
  496. * @uses bpbbpst_get_support_status() to get the registered statuses 
  497. * @return array the selected status with arguments 
  498. */ 
  499. function bpbbpst_get_selected_support_status( $selected = 0 ) { 
  500.  
  501. $selected_status = array(); 
  502.  
  503. $all_status = bpbbpst_get_support_status(); 
  504.  
  505. foreach ( $all_status as $key => $status ) { 
  506.  
  507. if ( $status['value'] == $selected ) { 
  508. $selected_status = $all_status[$key]; 
  509. $selected_status = array_merge( $selected_status, array( 'class' => $key ) ); 
  510.  
  511. return $selected_status; 
  512.  
  513. /** 
  514. * Hooks bbp_theme_before_topic_title to add the support mention before the topic title 
  515. * 
  516. * @since 2.0 
  517. * 
  518. * @param integer $topic_id the topic id 
  519. * @param boolean $echo true to display, false to return 
  520. * @uses bbp_get_topic_id() to get the topic id if not set 
  521. * @uses bbp_get_topic_forum_id() to get parent forum id 
  522. * @uses bpbbpst_get_forum_support_setting() to check for parent forum support setting 
  523. * @uses get_post_meta() to get the support status 
  524. * @uses bpbbpst_get_selected_support_status() to get arguments for the selected status 
  525. * @uses sanitize_html_class() to sanitize html class 
  526. * @return string the html output containing the support status 
  527. */ 
  528. function bpbbpst_add_support_mention( $topic_id = 0, $echo = true ) { 
  529. $class = false; 
  530. $output = ''; 
  531.  
  532. if ( empty( $topic_id ) ) { 
  533. $topic_id = bbp_get_topic_id(); 
  534.  
  535. // Since 2.0, we first need to check parent forum has support for support :) 
  536. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  537.  
  538. if ( empty( $forum_id ) ) { 
  539. return; 
  540.  
  541. /** 
  542. * See bpbbpst_display_forum_setting_options() for a description 
  543. * of the different forum types 
  544. */ 
  545. $forum_status = bpbbpst_get_forum_support_setting( $forum_id ); 
  546.  
  547. if ( 3 == $forum_status ) { 
  548. return; 
  549.  
  550. $support_status = get_post_meta( $topic_id, '_bpbbpst_support_topic', true ); 
  551. $status = ''; 
  552.  
  553. if ( empty( $support_status ) ) { 
  554.  
  555. if ( ! empty( $echo ) ) { 
  556. do_action( 'bpbbpst_add_support_mention_action', $forum_id, $forum_status, $topic_id ); 
  557. } else { 
  558. $output = apply_filters( 'bpbbpst_add_support_mention_filter', '', $forum_id, $forum_status, $topic_id ); 
  559.  
  560. return $output; 
  561.  
  562. /** new since 2.0 */ 
  563. $status = bpbbpst_get_selected_support_status( $support_status ); 
  564.  
  565. if ( empty( $status ) || ! is_array( $status ) ) { 
  566. return; 
  567.  
  568. // Build the output 
  569. $output = apply_filters( 
  570. 'bpbbpst_add_support_mention',  
  571. '<span class="bbp-st-topic-support '. sanitize_html_class( $status['class'] ).'">'. $status['prefix-title'] .'</span>',  
  572. $forum_id,  
  573. $forum_status,  
  574. $topic_id 
  575. ); 
  576.  
  577. if ( ! empty( $echo ) ) { 
  578. echo $output; 
  579. } else { 
  580. return $output; 
  581.  
  582. /** 
  583. * Removes the filter to the_title to avoid the support mention to be added in the_content 
  584. * 
  585. * @since 2.0 
  586. * 
  587. * @param array $templates the hierarchy of bbPress templates 
  588. * @param string $slug the slug template part 
  589. * @param string $name the optionnal template part 
  590. * @return array $templates unchanged 
  591. */ 
  592. function bpbbpst_topic_is_single( $templates = array(), $slug = '', $name = '' ) { 
  593.  
  594. if ( in_array( $name, array( 'single-topic', 'topic' ) ) ) { 
  595. remove_filter( 'the_title', 'bpbbpst_change_topic_title', 99 ); 
  596.  
  597. return $templates; 
  598.  
  599. /** 
  600. * Change the title of the topic to add support mention 
  601. * 
  602. * @since 2.0 
  603. * 
  604. * @param string $title the title of the topic 
  605. * @param integer $id the id of the topic 
  606. * @uses bpbbpst_is_sidebar() to be sure we're not in the sidebar 
  607. * @uses bbp_is_topic() to check for topic post type 
  608. * @uses bbp_is_topic_edit() to check if topic is being edited 
  609. * @uses bbp_is_single_topic() to check if topic is on single template 
  610. * @uses bpbbpst_add_support_mention() to return the mention and the title 
  611. * @return string $title the title with the eventual support mention 
  612. */ 
  613. function bpbbpst_change_topic_title( $title = '', $id = 0 ) { 
  614. /** 
  615. * get_the_title() is where is located the filter 'the_title', the one the plugin is 
  616. * using to prefix topic title. The problem is that there's no context to this filter 
  617. * so that we are sure get_the_title() is called to actually display the topic title just 
  618. * before the topic content. 
  619. * 
  620. * get_the_title() or the_title() can be used in header, widgets, breadcrumbs, meta tags... So there will 
  621. * allways be a place where the plugin shouldn't filter the title. If you're a plugin/theme developer 
  622. * and you want to get rid of this prefix, you simply need to add these lines before and after your use of 
  623. * get_the_title() : 
  624. * 
  625. * add_filter( 'bpbbpst_donot_change_topic_title', '__return_true' ); 
  626. * 
  627. * get_the_title() 
  628. * 
  629. * remove_filter( 'bpbbpst_donot_change_topic_title', '__return_true' ); 
  630. * 
  631. * If you want to completely get rid of it, just use the first line in the above example 
  632. * 
  633. * The prefix is only appended when on the topic single and edit bbPress templates. If on this template,  
  634. * a sidebar widget displaying the recent topics for instance could have the prefix appended, but not on 
  635. * all other pages of the site. So to be consistent, i chose to disable the prefix filter in dynamic 
  636. * sidebars. 
  637. * 
  638. * @param bool true to avoid the prefix to be appended to the_title, false otherwise 
  639. */ 
  640. $do_not_prefix = apply_filters( 'bpbbpst_donot_change_topic_title', bpbbpst_is_sidebar() ); 
  641.  
  642. if ( ! empty( $do_not_prefix ) ) { 
  643. return $title; 
  644.  
  645. if ( ! bbp_is_topic( $id ) ) { 
  646. return $title; 
  647.  
  648. if ( bbp_is_topic_edit() || bbp_is_single_topic() ) { 
  649. return bpbbpst_add_support_mention( $id, false ) . $title; 
  650. } else { 
  651. return $title; 
  652.  
  653.  
  654. /** 
  655. * Hooks bbp_head to launch a filter to the_title to change the title 
  656. * 
  657. * it does it quite late to avoid filtering the get_the_title() WordPress 
  658. * function that may be used by other plugin hooking wp_head such as jetpack. 
  659. * 
  660. * @since 2.0 
  661. * 
  662. * @uses add_filter() to modify the_title 
  663. */ 
  664. function bpbbpst_filter_topic_title() { 
  665. add_filter( 'the_title', 'bpbbpst_change_topic_title', 99, 2 ); 
  666.  
  667. /** 
  668. * Outputs the selectbox of available support status 
  669. * 
  670. * @since 2.0 
  671. * 
  672. * @param integer $support_status the current support status 
  673. * @param integer $topic_id the id of the topic 
  674. * @uses bpbbpst_get_support_status() to get the registered support statuses 
  675. * @uses selected() to activate the current option 
  676. * @uses wp_nonce_field() to create the security token 
  677. * @uses apply_filters() to call the 'bpbbpst_get_selectbox' hook 
  678. * @return $output 
  679. */ 
  680. function bpbbpst_get_selectbox( $support_status = 1, $topic_id = 0 ) { 
  681.  
  682. if ( empty( $topic_id ) ) { 
  683. return; 
  684.  
  685. $all_status = bpbbpst_get_support_status(); 
  686.  
  687. if ( empty( $all_status ) || ! is_array( $all_status ) ) { 
  688. return; 
  689.  
  690. $output = '<span class="support-select-box">'; 
  691. $output .= '<select class="support-select-status" name="_support_status" data-topicsupport="'.$topic_id.'">'; 
  692.  
  693. if ( $topic_id == 'adminlist' ) { 
  694. $output .= '<option value="-1">' . __( 'All support status', 'buddy-bbpress-support-topic' ) .'</option>'; 
  695.  
  696. foreach ( $all_status as $status ) { 
  697.  
  698. if ( $topic_id == 'adminlist' && $status['value'] == 0 ) { 
  699. continue; 
  700.  
  701. $output .= '<option value="'. $status['value'] .'" '; 
  702. $output .= selected( $support_status, $status['value'], false ); 
  703. $output .= '>'. $status['sb-caption'] .'</option>'; 
  704.  
  705. $output .= '</select>'; 
  706.  
  707. // nonce field 
  708. if ( $topic_id != 'adminlist' ) { 
  709. $output .= wp_nonce_field( 'bpbbpst_support_status', '_wpnonce_bpbbpst_support_status', true, false ); 
  710.  
  711. $output .= '</span>'; 
  712.  
  713. return apply_filters( 'bpbbpst_get_selectbox', $output, $support_status, $topic_id ); 
  714.  
  715.  
  716. /** 
  717. * Eventually unsets the not a support questions for support only forums 
  718. * 
  719. * @since 2.0 
  720. * 
  721. * @param array $all_status the registered support statuses 
  722. * @uses is_admin() to check for WordPress Backend 
  723. * @uses get_current_screen() to check for the topic admin area 
  724. * @uses bbp_get_topic_post_type() to get the topic post type 
  725. * @uses bbp_get_topic_forum_id() to get parent forum id 
  726. * @uses bpbbpst_get_forum_support_setting() to get parent forum support setting 
  727. * @return array the statuses without the not a support question if needed 
  728. */ 
  729. function bpbbpst_neutralize_not_support( $all_status = array() ) { 
  730.  
  731. $topic_id = bbp_get_topic_id(); 
  732.  
  733. if ( is_admin() && empty( $topic_id ) && isset( get_current_screen()->post_type ) && get_current_screen()->post_type == bbp_get_topic_post_type() && get_current_screen()->base == 'post' ) { 
  734. $topic_id = get_the_ID(); 
  735.  
  736. if ( empty( $topic_id ) ) { 
  737. return $all_status; 
  738.  
  739. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  740.  
  741. if ( empty( $forum_id ) ) { 
  742. return $all_status; 
  743.  
  744. if ( 2 == bpbbpst_get_forum_support_setting( $forum_id ) && ! empty( $all_status['topic-not-support'] ) ) { 
  745. unset( $all_status['topic-not-support'] ); 
  746.  
  747. return $all_status; 
  748.  
  749. /** 
  750. * Check for a selected value in an array 
  751. * 
  752. * @since 2.0 
  753. * 
  754. * @param array $current the complete array 
  755. * @param mixed $tocheck the value to check in the array 
  756. * @param boolean $echo wether to display or return 
  757. * @uses checked() to build the checked attribute 
  758. * @return string html checked attribute 
  759. */ 
  760. function bpbbpst_array_checked( $current = array(), $tocheck = false , $echo = true ) { 
  761. $checked = false; 
  762.  
  763. if ( empty( $current ) || empty( $tocheck ) ) { 
  764. return false; 
  765.  
  766. if ( ! is_array( $current ) ) { 
  767. $current = explode( ', ', $current ); 
  768.  
  769. if ( is_array( $current ) ) { 
  770. $checked = checked( true, in_array( $tocheck, $current ), false ); 
  771. } else { 
  772. $checked = checked( $current, $tocheck, false ); 
  773.  
  774. if ( empty( $echo ) ) { 
  775. return $checked; 
  776. } else { 
  777. echo $checked; 
  778.  
  779. /** 
  780. * Maps buddypress group mods to add their role in array 
  781. * 
  782. * @since 2.0 
  783. * 
  784. * @param array $users the group users (mods or admins) 
  785. * @return array the users with their group "role" 
  786. */ 
  787. function bpbbpst_role_group_forum_map( $users = array() ) { 
  788.  
  789. $role = $users['role']; 
  790.  
  791. foreach ( $users['users'] as $key => $user ) { 
  792. if ( ! empty( $user->user_id ) ) { 
  793. $user->role = $role; 
  794. $users['users'][$key] = $user; 
  795.  
  796. return $users['users']; 
  797.  
  798. /** 
  799. * List all bbPress moderators that are subscribed to new support topics 
  800. * 
  801. * @since 2.0 
  802. * 
  803. * @param integer $forum_id the forum id 
  804. * @param string $context are we using form admin or inside our notify function ? 
  805. * @uses get_post_meta() to get array of subscribed moderators 
  806. * @return array of user ids 
  807. */ 
  808. function bpbbpst_list_recipients( $forum_id = 0, $context = 'admin' ) { 
  809.  
  810. if ( empty( $forum_id ) ) { 
  811. return false; 
  812.  
  813. $recipients = get_post_meta( $forum_id, '_bpbbpst_support_recipients', true ); 
  814.  
  815. if ( empty( $recipients ) ) { 
  816. $recipients = array(); 
  817.  
  818. return apply_filters( 'bpbbpst_list_recipients', $recipients, $context, $forum_id ); 
  819.  
  820. /** 
  821. * Displays the available 3 options for forum support settings 
  822. * 
  823. * @since 2.0 
  824. * 
  825. * @param integer $support_feature the previously saved option if any 
  826. * @uses apply_filters() call 'bpbbpst_forum_support_settings' to add/edit/remove forum support settings 
  827. * @uses checked() to eventually add a checked attribute to field 
  828. * @uses esc_attr() to santize attributes 
  829. * @uses esc_html() to sanitize output 
  830. * @return string html list of options 
  831. */ 
  832. function bpbbpst_display_forum_setting_options( $support_feature = 1 ) { 
  833. $forum_support_settings = apply_filters( 'bpbbpst_forum_support_settings', array( 
  834. 'bpbbpst_forum_settings_yes' => array( 
  835. 'value' => 1,  
  836. 'label' => __( 'Leave users mark topics of this forum as support ones', 'buddy-bbpress-support-topic' ) 
  837. ),  
  838. 'bpbbpst_forum_settings_only' => array( 
  839. 'value' => 2,  
  840. 'label' => __( 'This forum is dedicated to support, all topics will be marked as support ones.', 'buddy-bbpress-support-topic' ) 
  841. ),  
  842. 'bpbbpst_forum_settings_no' => array( 
  843. 'value' => 3,  
  844. 'label' => __( 'This forum does not accept support topics', 'buddy-bbpress-support-topic' ) 
  845. ),  
  846. ) ); 
  847.  
  848. if ( empty( $forum_support_settings ) ) { 
  849. return; 
  850. ?> 
  851. <ul class="forum-support-settings"> 
  852.  
  853. <?php foreach ( $forum_support_settings as $key_setting => $setting ) : ?> 
  854.  
  855. <?php if ( empty( $key_setting ) || empty( $setting['value'] ) || empty( $setting['label'] ) ) : continue ; endif ;?> 
  856.  
  857. <li> 
  858. <input type="radio" name="_bpbbpst_forum_settings" id="<?php echo esc_attr( $key_setting );?>" value="<?php echo esc_attr( $setting['value'] );?>" <?php checked( intval( $setting['value'] ), $support_feature );?>/> 
  859. <?php echo esc_html( $setting['label'] ); ?> 
  860. </li> 
  861.  
  862. <?php endforeach ;?> 
  863. </ul> 
  864. <?php 
  865.  
  866. /** 
  867. * Displays a list of checkbox for moderators notifications 
  868. * 
  869. * Admin can choose the moderators that will receive a notification 
  870. * in case a new support topic has been posted. 
  871. * 
  872. * @since 2.0 
  873. * 
  874. * @param integer $forum_id the forum id 
  875. * @uses get_users() to list all moderators 
  876. * @uses bpbbpst_list_recipients() to get the support recipients for the specified forum 
  877. * @uses bpbbpst_array_checked() to activate the checboxes regarding the recipients previously saved 
  878. * @uses bbp_get_dynamic_role_name() to get the role name of listed users 
  879. * @uses bbp_get_user_role() to get the forum role for listed users 
  880. * @return string html the list of checkboxes 
  881. */ 
  882. function bpbbpst_checklist_moderators( $forum_id = false ) { 
  883.  
  884. $user_query = get_users( array( 'who' => 'bpbbpst_moderators' ) ); 
  885.  
  886. if ( ! is_array( $user_query ) || count( $user_query ) < 1 ) { 
  887. return false; 
  888.  
  889. $recipients = bpbbpst_list_recipients( $forum_id ); 
  890. ?> 
  891. <ul class="bbp-moderators-list"> 
  892. <?php foreach ( $user_query as $user ) :?> 
  893. <li> 
  894. <input type="checkbox" value="<?php echo $user->data->ID;?>" name="_bpbbpst_support_recipients[]" <?php bpbbpst_array_checked( $recipients, $user->data->ID );?>> 
  895. <?php echo $user->data->display_name ;?> (<?php echo bbp_get_dynamic_role_name( bbp_get_user_role( $user->data->ID ) );?>) 
  896. </li> 
  897. <?php endforeach;?> 
  898. </ul> 
  899. <?php 
  900.  
  901. /** 
  902. * Returns an array of statistics element about support topics 
  903. * 
  904. * @since 2.0 
  905. * 
  906. * @param array $args user can customize the query 
  907. * @uses bbp_get_topic_post_type() to get the topic post type identifier 
  908. * @uses bbp_parse_args() to merge args with defaults 
  909. * @uses WP_Query to make the request and get the support topics 
  910. * @uses bpbbpst_get_support_status() to get the registered support statuses 
  911. * @uses sanitize_html_class() to sanitize the class 
  912. * @uses get_post_meta() to get the support status of the topic 
  913. * @uses wp_reset_postdata() to restore the $post global to the current post in the main query 
  914. * @uses apply_filters() to let plugins or themes modify the value 
  915. * @return array $support_stats the different statistics element 
  916. */ 
  917. function bpbbpst_support_statistics( $args = '' ) { 
  918.  
  919. $defaults = array( 
  920. 'post_type' => bbp_get_topic_post_type(),  
  921. 'posts_per_page' => -1,  
  922. 'meta_query' => array( 
  923. array( 
  924. 'key' => '_bpbbpst_support_topic',  
  925. 'value' => 1,  
  926. 'type' => 'numeric',  
  927. 'compare' => '>=' 
  928. ); 
  929.  
  930. $r = bbp_parse_args( $args, $defaults, 'support_statistics' ); 
  931.  
  932. $support_query = new WP_Query( $r ); 
  933. $total_support = $support_query->found_posts; 
  934.  
  935. $all_status = bpbbpst_get_support_status(); 
  936. unset( $all_status['topic-not-support'] ); 
  937.  
  938. $support_stat = array(); 
  939.  
  940. foreach ( $all_status as $key => $status ) { 
  941. $support_stat[ $status['value'] ] = array( 
  942. 'stat' => 0,  
  943. 'label' => $status['sb-caption'],  
  944. 'admin_class' => $status['admin_class'],  
  945. 'front_class' => sanitize_html_class( $key ) 
  946. ); 
  947.  
  948. if ( ! empty( $status['dashicon'] ) ) { 
  949. $support_stat[ $status['value'] ]['dashicon'] = $status['dashicon']; 
  950.  
  951. if ( $support_query->have_posts() ) : 
  952.  
  953. while ( $support_query->have_posts() ) : $support_query->the_post(); 
  954.  
  955. $db_status = get_post_meta( $support_query->post->ID, '_bpbbpst_support_topic', true ); 
  956.  
  957. // We need to check parent forum support setting to avoid inconsistent stats 
  958. $forum_id = $support_query->post->post_parent; 
  959. $forum_support_status = false; 
  960.  
  961. if ( ! empty( $forum_id ) ) { 
  962. $forum_support_status = get_post_meta( $forum_id, '_bpbbpst_forum_settings', true ); 
  963.  
  964. if ( ! in_array( $forum_support_status, array( 1, 2 ) ) ) { 
  965. // Let's avoid the display of the topic in the WP_List_Table 
  966. delete_post_meta( $support_query->post->ID, '_bpbbpst_support_topic' ); 
  967. continue; 
  968.  
  969. $support_stat[$db_status]['stat'] += 1; 
  970.  
  971. endwhile; 
  972.  
  973. wp_reset_postdata(); 
  974.  
  975. endif; 
  976.  
  977. $goon = false; 
  978.  
  979. foreach ( $support_stat as $notempty ) { 
  980. if ( $notempty['stat'] > 0 ) { 
  981. $goon = true; 
  982.  
  983. if ( ! empty( $goon ) ) { 
  984.  
  985. $percent_support = number_format( ( $support_stat[2]['stat'] / $total_support ) * 100, 2 ) . '%'; 
  986.  
  987. $support_stats['allstatus'] = $support_stat; 
  988. $support_stats['total_support'] = $total_support; 
  989. $support_stats['percent'] = $percent_support; 
  990.  
  991. return apply_filters( 'bpbbpst_support_statistics', $support_stats, $args ); 
  992.  
  993. } else { 
  994. return false; 
  995.  
  996.  
  997. /** 
  998. * Hooks bbp_new_topic to eventually send a notification to moderators 
  999. * 
  1000. * @since 2.0 
  1001. * 
  1002. * @param integer $topic_id the topic id just created 
  1003. * @param integer $forum_id the forum id the topic is attached to 
  1004. * @param array $anonymous_data 
  1005. * @param integer $topic_author the topic author id 
  1006. * @uses bpbbpst_get_forum_support_setting() to get support settings for the forum the topic is attached to 
  1007. * @uses bpbbpst_notify_moderators() to build and send the notifications to moderators. 
  1008. */ 
  1009. function bpbbpst_new_support_topic_notify( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) { 
  1010.  
  1011. if ( empty( $topic_id ) ) { 
  1012. return; 
  1013.  
  1014. if ( empty( $forum_id ) ) { 
  1015. return; 
  1016.  
  1017. $forum_support_feature = bpbbpst_get_forum_support_setting( $forum_id ); 
  1018.  
  1019. if ( $forum_support_feature != 3 && ! empty( $_POST['_bp_bbp_st_is_support'] ) ) { 
  1020. bpbbpst_notify_moderators( $topic_id, $forum_id, $anonymous_data, $topic_author ); 
  1021.  
  1022. /** 
  1023. * Sends a notification to bbPress moderators and eventually BP Groups one 
  1024. * 
  1025. * Inpired by : bbPress bbp_notify_subscribers() function 
  1026. * 
  1027. * @since 2.0 
  1028. * 
  1029. * @param integer $topic_id the topic id 
  1030. * @param integer $forum_id the forum id 
  1031. * @param boolean $anonymous_data 
  1032. * @param integer $topic_author 
  1033. * @uses bbp_get_topic_id() to validate the topic id 
  1034. * @uses bbp_get_forum_id() to validate the forum id 
  1035. * @uses bpbbpst_list_recipients() to get the moderators ids (bbPress & current BuddyPress group ones) 
  1036. * @uses bbp_is_topic_published() to make sure the topic is published 
  1037. * @uses bbp_get_topic_author_display_name() to get authors display name 
  1038. * @uses remove_all_filters() to remove the filters applied to topic content and title 
  1039. * @uses bbp_get_topic_title() to get the topic title 
  1040. * @uses bbp_get_topic_content() to get the topic content 
  1041. * @uses bbp_get_topic_permalink() to get the link to the topic 
  1042. * @uses wp_specialchars_decode() to get the decoded string without html entities 
  1043. * @uses get_option() to get blogname 
  1044. * @uses bbp_get_forum_title() to get forum title 
  1045. * @uses apply_filters() to let plugins or themes modify the value 
  1046. * @uses get_userdata() to get the user's data (email...) 
  1047. * @uses wp_mail() to send the mail 
  1048. * @uses do_action() to let plugins or themes run some actions from this point 
  1049. * @return boolean true 
  1050. */ 
  1051. function bpbbpst_notify_moderators( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $topic_author = 0 ) { 
  1052.  
  1053. /** Validation ************************************************************/ 
  1054. $topic_id = bbp_get_topic_id( $topic_id ); 
  1055. $forum_id = bbp_get_forum_id( $forum_id ); 
  1056.  
  1057. $recipients = bpbbpst_list_recipients( $forum_id, 'notify' ); 
  1058.  
  1059. if ( empty( $recipients ) || ! is_array( $recipients ) || count( $recipients ) < 1 ) { 
  1060. return; 
  1061.  
  1062. /** Topic *****************************************************************/ 
  1063.  
  1064. // Bail if topic is not published 
  1065. if ( ! bbp_is_topic_published( $topic_id ) ) { 
  1066. return false; 
  1067.  
  1068. // Poster name 
  1069. $topic_author_name = bbp_get_topic_author_display_name( $topic_id ); 
  1070.  
  1071. // Remove filters from reply content and topic title to prevent content 
  1072. // from being encoded with HTML entities, wrapped in paragraph tags, etc... 
  1073. remove_all_filters( 'bbp_get_topic_content' ); 
  1074. remove_all_filters( 'bbp_get_topic_title' ); 
  1075.  
  1076. // Strip tags from text 
  1077. $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); 
  1078. $topic_content = strip_tags( bbp_get_topic_content( $topic_id ) ); 
  1079. $topic_url = bbp_get_topic_permalink( $topic_id ); 
  1080. $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 
  1081. $forum_name = wp_specialchars_decode( bbp_get_forum_title( $forum_id ), ENT_QUOTES ); 
  1082.  
  1083. // Loop through users 
  1084. foreach ( (array) $recipients as $user_id ) { 
  1085.  
  1086. /** Don't send notifications to the moderator if he is the one who made the post */ 
  1087. if ( ! empty( $topic_author ) && (int) $user_id == (int) $topic_author ) { 
  1088. continue; 
  1089.  
  1090. // For plugins to filter messages per reply/topic/user 
  1091. $message = sprintf( __( '%1$s wrote a new support topic: %2$s 
  1092.  
  1093. %3$s 
  1094.  
  1095. Topic Link: %4$s 
  1096.  
  1097. ----------- 
  1098.  
  1099. You are receiving this email because a forum admin made you subscribe to new support topic of %5$s forum. 
  1100.  
  1101. Please ask the admin to unsubscribe from these emails.', 'buddy-bbpress-support-topic' ),  
  1102.  
  1103. $topic_author_name,  
  1104. $topic_title,  
  1105. $topic_content,  
  1106. $topic_url,  
  1107. $forum_name 
  1108. ); 
  1109.  
  1110. $message = apply_filters( 'bpbbpst_notify_moderators_mail_message', $message, $topic_id, $forum_id, $user_id ); 
  1111.  
  1112. if ( empty( $message ) ) { 
  1113. continue; 
  1114.  
  1115. // For plugins to filter titles per reply/topic/user 
  1116. $subject = apply_filters( 'bpbbpst_notify_moderators_mail_title', '[' . $blog_name . '] New support topic on : ' . $forum_name, $topic_id, $forum_id, $user_id ); 
  1117.  
  1118. if ( empty( $subject ) ) { 
  1119. continue; 
  1120.  
  1121. // Custom headers 
  1122. $headers = apply_filters( 'bpbbpst_notify_moderators_mail_headers', array() ); 
  1123.  
  1124. // Get user data of this user 
  1125. $user = get_userdata( $user_id ); 
  1126.  
  1127. // Send notification email 
  1128. wp_mail( $user->user_email, $subject, $message, $headers ); 
  1129.  
  1130. do_action( 'bpbbpst_notify_moderators', $topic_id, $forum_id, $recipients ); 
  1131.  
  1132. return true; 
  1133.  
  1134. /** 
  1135. * Builds a selectbox with forum set as support only ones 
  1136. * 
  1137. * @since 2.0 
  1138. * 
  1139. * @param integer $selected the forum id previously selected 
  1140. * @param string $field_id id of the selectbox 
  1141. * @param string $field_name name of the selectbox 
  1142. * @uses bbp_get_forum_post_type() to get forum post type 
  1143. * @uses WP_Query to build a loop 
  1144. * @uses esc_attr() to sanitize value of field 
  1145. * @uses bbp_get_forum_id() to validate forum id 
  1146. * @uses selected() to add a selected attribute to option if needed 
  1147. * @uses bbp_forum_title() to get the forum title 
  1148. * @uses wp_reset_postdata() to reset the post datas 
  1149. * @return string html output 
  1150. */ 
  1151. function bpbbpst_get_support_only_forums( $selected = 0, $field_id = '_support_only_forum', $field_name = '_support_only_forums' ) { 
  1152.  
  1153. $query_args = array( 
  1154. 'post_type' => bbp_get_forum_post_type(),  
  1155. 'posts_per_page' => -1,  
  1156. 'meta_query' => array( 
  1157. array( 
  1158. 'key' => '_bpbbpst_forum_settings',  
  1159. 'value' => 2,  
  1160. 'type' => 'numeric',  
  1161. 'compare' => '=' 
  1162. ); 
  1163.  
  1164. $support_query = new WP_Query( $query_args ); 
  1165.  
  1166. if ( $support_query->have_posts() ) :?> 
  1167.  
  1168. <select class="widefat" id="<?php echo $field_id; ?>" name="<?php echo $field_name; ?>"> 
  1169.  
  1170. <?php while ( $support_query->have_posts() ) : $support_query->the_post() ; ?> 
  1171.  
  1172. <option value="<?php echo esc_attr( bbp_get_forum_id( $support_query->post->ID ) ) ?>" <?php selected( $support_query->post->ID, $selected ) ?>><?php bbp_forum_title( $support_query->post->ID ) ?></option> 
  1173.  
  1174. <?php endwhile ; ?> 
  1175.  
  1176. </select> 
  1177.  
  1178. <?php wp_reset_postdata(); 
  1179.  
  1180. else : 
  1181. esc_html_e( 'No support only forums were found', 'buddy-bbpress-support-topic' ); 
  1182.  
  1183. endif; 
  1184.  
  1185. /** 
  1186. * Outputs an hidden field with the referer url 
  1187. * 
  1188. * @since 2.0 
  1189. * 
  1190. * @param integer $support_type the forum support setting 
  1191. * @uses esc_url_raw() to sanitize url 
  1192. * @uses wp_get_referer() to get the referer url 
  1193. * @return string html output 
  1194. */ 
  1195. function bpbbpst_referer_extra_field( $support_type = 0 ) { 
  1196. if ( ! empty( $support_type ) && $support_type != 3 && ! empty( $_REQUEST['bpbbpst-referer'] ) ) { 
  1197. $referer = esc_url_raw( wp_get_referer() ); 
  1198. ?> 
  1199. <input type="hidden" name="_bp_bbp_st_referer" value="<?php echo $referer;?>"/> 
  1200. <?php 
  1201.  
  1202. /** 
  1203. * Displays the referer above the support topic content 
  1204. * 
  1205. * @since 2.0 
  1206. * 
  1207. * @uses get_post_meta() to get the saved referer 
  1208. * @uses bbp_get_topic_id() to get the topic id 
  1209. * @uses current_user_can() to check for current user's capability 
  1210. * @uses bbp_get_reply_id() to get reply id 
  1211. * @uses esc_url() to sanitize the referer 
  1212. * @return string html output 
  1213. */ 
  1214. function bpbbpst_display_referer_to_moderators() { 
  1215. $meta = get_post_meta( bbp_get_topic_id(), '_bpbbpst_support_referer', true ); 
  1216.  
  1217. if ( ! empty( $meta ) && current_user_can( 'moderate' ) && bbp_get_reply_id() == bbp_get_topic_id() ) { 
  1218. echo '<pre>' . __( 'Referer', 'buddy-bbpress-support-topic' ) . ' :<br/>'. esc_url( $meta ).'</pre>'; 
  1219.  
  1220. /** 
  1221. * Activate the "Subscribe to replies" checkbox if the user asked for support using the new support widget 
  1222. * 
  1223. * @since 2.1.0 
  1224. * 
  1225. * @uses bbp_get_forum_id() to get the parent forum id 
  1226. * @uses bpbbpst_get_forum_support_setting() to get the parent forum setting for support feature 
  1227. * @return string html output 
  1228. */ 
  1229. function bpbbpst_referer_topic_subscribed( $output = '', $checked ) { 
  1230. if ( ! empty( $checked ) ) { 
  1231. return $output; 
  1232.  
  1233. $forum_id = bbp_get_forum_id(); 
  1234.  
  1235. if ( empty( $forum_id ) ) { 
  1236. return $output; 
  1237.  
  1238. $support_type = bpbbpst_get_forum_support_setting( $forum_id ); 
  1239.  
  1240. if ( ! empty( $support_type ) && $support_type != 3 && ! empty( $_REQUEST['bpbbpst-referer'] ) ) { 
  1241. $checked = checked( true, true, false ); 
  1242.  
  1243. return apply_filters( 'bpbbpst_referer_filter_topic_subscribed', $checked, $forum_id, $support_type ); 
  1244.  
  1245. /** 
  1246. * Displays template notices when needed 
  1247. * 
  1248. * @since 2.1.0 
  1249. * 
  1250. * @return string html output 
  1251. */ 
  1252. function bpbbpst_template_notices() { 
  1253. $notices = array(); 
  1254.  
  1255. if ( bbp_is_single_forum() && ! bbp_is_topic_edit() ) { 
  1256. $forum_id = bbp_get_forum_id(); 
  1257.  
  1258. if ( 2 === (int) bpbbpst_get_forum_support_setting( $forum_id ) ) { 
  1259. $intro = bpbbpst_get_forum_support_topic_intro( $forum_id ); 
  1260.  
  1261. if ( ! empty( $intro ) ) { 
  1262. $notices[] = sprintf( '<div class="bbp-template-notice topic-intro"><p>%s</p></div>', nl2br( $intro ) ); 
  1263.  
  1264. if ( ! empty( $notices ) ) { 
  1265. echo join( "\n", $notices ); 
  1266.  
  1267. /** 
  1268. * Displays a topic template inside the new topic editor if needed 
  1269. * 
  1270. * @since 2.1.0 
  1271. * 
  1272. * @return string html output 
  1273. */ 
  1274. function bpbbpst_support_topic_template( $content = '' ) { 
  1275. if ( ! empty( $content ) || ! bbp_is_single_forum() || bbp_is_topic_edit() ) { 
  1276. return $content; 
  1277.  
  1278. $forum_id = bbp_get_forum_id(); 
  1279.  
  1280. if ( 2 === (int) bpbbpst_get_forum_support_setting( $forum_id ) ) { 
  1281. $template = bpbbpst_get_forum_support_topic_template( $forum_id ); 
  1282.  
  1283. if ( ! empty( $template ) ) { 
  1284. $content = $template; 
  1285.  
  1286. return apply_filters( 'bpbbpst_support_topic_template', $content, $forum_id ); 
  1287.  
  1288. /** 
  1289. * Displays a checkbox on the Reply form to mark to support topic as resolved 
  1290. * 
  1291. * @since 2.1.0 
  1292. * 
  1293. * @return string html output 
  1294. */ 
  1295. function bpbbpst_after_reply_form_subscription() { 
  1296. $topic_id = bbp_get_topic_id(); 
  1297. if ( empty( $topic_id ) ) { 
  1298. return; 
  1299.  
  1300. $forum_id = bbp_get_forum_id(); 
  1301. if ( empty( $forum_id ) ) { 
  1302. $forum_id = get_post_field( 'post_parent', $topic_id ); 
  1303.  
  1304. // Not a support only forum, do not output 
  1305. if ( 2 !== (int) bpbbpst_get_forum_support_setting( $forum_id ) ) { 
  1306. return; 
  1307.  
  1308. // Topic resolved, do not output 
  1309. if ( 2 === (int) get_post_meta( $topic_id, '_bpbbpst_support_topic', true ) ) { 
  1310. return; 
  1311.  
  1312. // Output the Checkbox 
  1313. ?> 
  1314. <input name="_bpbbpst_support_topic_cb" id="bpbbpst_support_topic_cb" type="checkbox" value="1"/> 
  1315. <label for="bpbbpst_support_topic_cb"><?php esc_html_e( 'Mark this support topic as resolved', 'buddy-bbpress-support-topic' ); ?></label> 
  1316. <?php 
  1317.  
  1318. /** 
  1319. * Mark the support topic as resolved if a reply did resolve it. 
  1320. * 
  1321. * @since 2.1.0 
  1322. * 
  1323. * @param integer $reply_id The just instered reply ID 
  1324. */ 
  1325. function bpbbpst_reply_save_support_type( $reply_id = 0 ) { 
  1326. // Check we have required data 
  1327. if ( empty( $_POST['_bpbbpst_support_topic_cb'] ) || empty( $reply_id ) ) { 
  1328. return; 
  1329.  
  1330. $reply = bbp_get_reply( $reply_id ); 
  1331.  
  1332. // Reply is awaiting moderation, do nothing 
  1333. if ( bbp_get_pending_status_id() === $reply->post_status ) { 
  1334. return; 
  1335.  
  1336. $topic_id = $reply->post_parent; 
  1337.  
  1338. // Post parent is not set, try to use post meta as fallback 
  1339. if ( ! $topic_id ) { 
  1340. $topic_id = bbp_get_reply_topic_id( $reply_id ); 
  1341.  
  1342. // Finally mark the support topic as resolved 
  1343. if ( ! empty( $topic_id ) ) { 
  1344. update_post_meta( $topic_id, '_bpbbpst_support_topic', 2 ); 
.