/includes/admin.php

  1. <?php 
  2.  
  3. // Exit if accessed directly 
  4. if ( ! defined( 'ABSPATH' ) ) exit; 
  5.  
  6. if ( ! class_exists( 'BP_bbP_ST_Admin' ) ) : 
  7. /** 
  8. * Loads Buddy-bbPress Support Topic plugin admin area 
  9. * 
  10. * @package Buddy-bbPress Support Topic 
  11. * @subpackage Administration 
  12. * 
  13. * @since 2.0 
  14. */ 
  15. class BP_bbP_ST_Admin { 
  16.  
  17. /** 
  18. * The admin loader 
  19. * 
  20. * @since 2.0 
  21. * 
  22. * @uses BP_bbP_ST_Admin::setup_actions() to add some key hooks 
  23. * @uses BP_bbP_ST_Admin::maybe_activate() to eventually load the welcome screen 
  24. */ 
  25. public function __construct() { 
  26. $this->setup_actions(); 
  27. $this->maybe_activate(); 
  28.  
  29. /** 
  30. * Setup the admin hooks, actions and filters 
  31. * 
  32. * @since 2.0 
  33. * @access private 
  34. * 
  35. * @uses bbp_is_deactivation() to prevent interfering with bbPress deactivation process 
  36. * @uses bbp_is_activation() to prevent interfering with bbPress activation process 
  37. * @uses add_action() To add various actions 
  38. * @uses add_filter() To add various filters 
  39. * @uses bpbbpst_is_bbp_required_version_ok() to check if bbPress required version is ok 
  40. */ 
  41. private function setup_actions() { 
  42.  
  43. if ( bbp_is_deactivation() || bbp_is_activation() ) { 
  44. return; 
  45.  
  46. // Welcome Screen 
  47. add_action( 'bbp_admin_menu', array( $this, 'welcome_screen_register' ) ); 
  48. add_action( 'bbp_admin_head', array( $this, 'welcome_screen_css' ) ); 
  49. add_filter( 'plugin_action_links', array( $this, 'modify_plugin_action_links' ), 10, 2 ); 
  50.  
  51. // Bail if bbPress required version doesn't match our need 
  52. if ( ! bpbbpst_is_bbp_required_version_ok() ) { 
  53. add_action( 'bbp_admin_notices', array( $this, 'admin_notices' ) ); 
  54. return; 
  55.  
  56. // forums metabox 
  57. add_action( 'bbp_forum_attributes_metabox', array( $this, 'forum_meta_box_register' ), 10 ); 
  58. add_action( 'bbp_forum_attributes_metabox_save', array( $this, 'forum_meta_box_save' ), 10, 1 ); 
  59.  
  60. // Action to edit users query to only get forum moderators ( Keymasters+moderators ) 
  61. add_action( 'pre_get_users', array( $this, 'filter_user_query' ), 10, 1 ); 
  62.  
  63. // enqueues a js script to hide show recipients 
  64. add_action( 'load-post.php', array( $this, 'enqueue_forum_js' ) ); 
  65. add_action( 'load-post-new.php', array( $this, 'enqueue_forum_js' ) ); 
  66.  
  67. // topics metabox 
  68. add_action( 'bbp_topic_metabox', array( $this, 'topic_meta_box' ), 10, 1 ); 
  69. add_action( 'bbp_topic_attributes_metabox_save', array( $this, 'topic_meta_box_save' ), 10, 2 ); 
  70.  
  71. // moving a topic from the admin 
  72. add_action( 'save_post', array( $this, 'topic_moved' ), 9, 2 ); 
  73.  
  74. // topics list columns 
  75. add_filter( 'bbp_admin_topics_column_headers', array( $this, 'topics_admin_column' ), 10, 1 ); 
  76. add_action( 'bbp_admin_topics_column_data', array( $this, 'topics_column_data' ), 10, 2 ); 
  77.  
  78. // topics list filter by support status 
  79. add_action( 'restrict_manage_posts', array( $this, 'topics_admin_support_filter' ), 11 ); 
  80. add_filter( 'bbp_request', array( $this, 'topics_admin_support_request' ), 11, 1 ); 
  81.  
  82. // topics bulk edit 
  83. add_action( 'bulk_edit_custom_box', array( $this, 'bulk_topics_support' ), 10, 2 ); 
  84. add_action( 'load-edit.php', array( $this, 'bulk_update_support' ) ); 
  85.  
  86. // Dashboard right now bbPress widget 
  87. add_action( 'bbp_dashboard_widget_right_now_table_end', array( $this, 'dashboard_widget' ) ); 
  88.  
  89. // At a glance Dashboard widget 
  90. add_action( 'bbp_dashboard_at_a_glance', array( $this, 'dashboard_at_a_glance' ), 10, 2 ); 
  91.  
  92. /** 
  93. * Display a notice to give the admin some feedback 
  94. * 
  95. * @since 2.0 
  96. * 
  97. * @uses bpbbpst_get_plugin_version() to get plugin's version 
  98. * @uses bpbbpst_bbp_required_version() to get bbPress required version 
  99. * @return string HTML output 
  100. */ 
  101. public function admin_notices() { 
  102. ?> 
  103. <div id="message" class="error fade"> 
  104. <p><?php printf( esc_html__( 'Version %s of Buddy-bbPress Support Topic requires version %s of bbPress to be activated. Please upgrade bbPress.', 'buddy-bbpress-support-topic' ), bpbbpst_get_plugin_version(), bpbbpst_bbp_required_version() ); ?></p> 
  105. </div> 
  106. <?php 
  107.  
  108. /** 
  109. * Registers a new metabox in Forum's edit form (admin) 
  110. * 
  111. * @since 2.0 
  112. * 
  113. * @global $post_ID 
  114. * @uses bbp_is_forum_category() to check if the forum is a category 
  115. * @uses add_meta_box() to add the metabox to forum edit screen 
  116. * @uses bbp_get_forum_post_type() to get forum post type 
  117. */ 
  118. public function forum_meta_box_register() { 
  119. global $post_ID; 
  120.  
  121. if ( bbp_is_forum_category( $post_ID ) ) { 
  122. return; 
  123.  
  124. add_meta_box ( 
  125. 'bpbbpst_forum_settings',  
  126. __( 'Support settings', 'buddy-bbpress-support-topic' ),  
  127. array( &$this, 'forum_meta_box_display' ),  
  128. bbp_get_forum_post_type(),  
  129. 'normal',  
  130. 'low' 
  131. ); 
  132.  
  133.  
  134. /** 
  135. * Displays the content for the metabox 
  136. * 
  137. * @since 2.0 
  138. * 
  139. * @param object $forum the forum object 
  140. * @uses bpbbpst_get_forum_support_setting() to get forum support setting 
  141. * @uses bpbbpst_display_forum_setting_options() to list the available support settings 
  142. * @uses bpbbpst_checklist_moderators() to list the bbPress keymasters and moderators 
  143. * @uses do_action_ref_array() call 'bpbbpst_forum_support_options' to add your custom forum support settings 
  144. */ 
  145. public function forum_meta_box_display( $forum = false ) { 
  146. if ( empty( $forum->ID ) ) { 
  147. return; 
  148.  
  149. $support_feature = bpbbpst_get_forum_support_setting( $forum->ID ); 
  150.  
  151. $mailing_list_style = ''; 
  152. if ( 3 === (int) $support_feature ) { 
  153. $mailing_list_style = 'style="display:none"'; 
  154.  
  155. $support_only_style = 'style="display:none"'; 
  156.  
  157. if ( 2 === (int) $support_feature ) { 
  158. $support_only_style = ''; 
  159.  
  160. bpbbpst_display_forum_setting_options( $support_feature ); 
  161. ?> 
  162. <div class="bpbbpst-mailing-list" <?php echo $mailing_list_style;?>> 
  163. <h4><?php _e( 'Who should receive an email notification when a new support topic is posted ?', 'buddy-bbpress-support-topic' );?></h4> 
  164.  
  165. <?php bpbbpst_checklist_moderators( $forum->ID );?> 
  166. </div> 
  167.  
  168. <?php do_action_ref_array( 'bpbbpst_forum_support_options', array( $forum->ID, $mailing_list_style ) ); ?> 
  169.  
  170. <div class="bpbbpst-support-guides" <?php echo $support_only_style;?>> 
  171. <h4><?php _e( 'New Topic form extras', 'buddy-bbpress-support-topic' );?></h4> 
  172. <label class="screen-reader-text" for="support-topic-intro"><?php esc_html_e( 'New Topic Guide', 'buddy-bbpress-support-topic' ); ?></label> 
  173. <textarea rows="3" cols="40" name="_bpbbpst_support_topic[intro]" id="support-topic-intro" style="width:100%"><?php echo bpbbpst_get_forum_support_topic_intro( $forum->ID );?></textarea> 
  174. <p class="description"><?php printf( esc_html__( 'Use this field to insert some instructions above the new topic form. Allowed tags are: %s', 'buddy-bbpress-support-topic' ), join( ', ', array_keys( (array) wp_kses_allowed_html( 'forum' ) ) ) ); ?></p> 
  175.  
  176. <label class="screen-reader-text" for="support-topic-tpl"><?php esc_html_e( 'New Topic Template', 'buddy-bbpress-support-topic' ); ?></label> 
  177. <textarea rows="3" cols="40" name="_bpbbpst_support_topic[tpl]" id="support-topic-tpl" style="width:100%"><?php echo bpbbpst_get_forum_support_topic_template( $forum->ID );?></textarea> 
  178. <p class="description"><?php esc_html_e( 'The text added within this field will be used as a template for the content of new topics.', 'buddy-bbpress-support-topic' ); ?></p> 
  179. </div> 
  180. <?php 
  181.  
  182. do_action_ref_array( 'bpbbpst_forum_support_options_after_guides', array( $forum->ID, $support_only_style ) ); 
  183.  
  184. /** 
  185. * Saves the forum metabox datas 
  186. * 
  187. * @since 2.0 
  188. * 
  189. * @param integer $forum_id the forum id 
  190. * @uses bbp_is_forum_category() to check if forum is a category 
  191. * @uses update_post_meta() to save the forum support setting 
  192. * @uses delete_post_meta() to eventually delete a setting if needed 
  193. * @uses do_action() call 'bpbbpst_forum_settings_updated' to save your custom forum support settings 
  194. * @return integer the forum id 
  195. */ 
  196. public function forum_meta_box_save( $forum_id = 0 ) { 
  197. $support_feature = false; 
  198. $is_forum_category = bbp_is_forum_category( $forum_id ); 
  199.  
  200. if ( ! empty( $_POST['_bpbbpst_forum_settings'] ) ) { 
  201. $support_feature = absint( $_POST['_bpbbpst_forum_settings'] ); 
  202.  
  203. // Forum is not a category, save the support metas 
  204. if ( ! empty( $support_feature ) && ! $is_forum_category ) { 
  205. update_post_meta( $forum_id, '_bpbbpst_forum_settings', $support_feature ); 
  206.  
  207. if ( 3 === (int) $support_feature ) { 
  208. delete_post_meta( $forum_id, '_bpbbpst_support_recipients' ); 
  209. delete_post_meta( $forum_id, '_bpbbpst_support_topic_intro' ); 
  210. delete_post_meta( $forum_id, '_bpbbpst_support_topic_tpl' ); 
  211. } else { 
  212. $recipients = ! empty( $_POST['_bpbbpst_support_recipients'] ) ? array_map( 'intval', $_POST['_bpbbpst_support_recipients'] ) : false ; 
  213.  
  214. if ( ! empty( $recipients ) && is_array( $recipients ) && count( $recipients ) > 0 ) { 
  215. update_post_meta( $forum_id, '_bpbbpst_support_recipients', $recipients ); 
  216. } else { 
  217. delete_post_meta( $forum_id, '_bpbbpst_support_recipients' ); 
  218.  
  219. if ( 2 === (int) $support_feature ) { 
  220. if ( ! empty( $_POST['_bpbbpst_support_topic']['intro'] ) ) { 
  221. update_post_meta( $forum_id, '_bpbbpst_support_topic_intro', wp_unslash( $_POST['_bpbbpst_support_topic']['intro'] ) ); 
  222. } else { 
  223. delete_post_meta( $forum_id, '_bpbbpst_support_topic_intro' ); 
  224.  
  225. if ( ! empty( $_POST['_bpbbpst_support_topic']['tpl'] ) ) { 
  226. update_post_meta( $forum_id, '_bpbbpst_support_topic_tpl', wp_unslash( $_POST['_bpbbpst_support_topic']['tpl'] ) ); 
  227. } else { 
  228. delete_post_meta( $forum_id, '_bpbbpst_support_topic_tpl' ); 
  229. } else if ( ! empty( $_POST['_bpbbpst_support_topic'] ) ) { 
  230. delete_post_meta( $forum_id, '_bpbbpst_support_topic_intro' ); 
  231. delete_post_meta( $forum_id, '_bpbbpst_support_topic_tpl' ); 
  232.  
  233. do_action( 'bpbbpst_forum_settings_updated', $forum_id, $support_feature ); 
  234.  
  235. // Check for support metas to eventually remove them 
  236. } else if ( $is_forum_category ) { 
  237. $support_feature = get_post_meta( $forum_id, '_bpbbpst_forum_settings', true ); 
  238.  
  239. if ( ! empty( $support_feature ) ) { 
  240. delete_post_meta( $forum_id, '_bpbbpst_forum_settings' ); 
  241. delete_post_meta( $forum_id, '_bpbbpst_support_recipients' ); 
  242. delete_post_meta( $forum_id, '_bpbbpst_support_topic_intro' ); 
  243. delete_post_meta( $forum_id, '_bpbbpst_support_topic_tpl' ); 
  244.  
  245. return $forum_id; 
  246.  
  247. /** 
  248. * Adds a js to WordPress scripts queue 
  249. * 
  250. * @since 2.0 
  251. * 
  252. * @uses get_current_screen() to be sure we are in forum post screen 
  253. * @uses bbp_get_forum_post_type() to get the forum post type 
  254. * @uses wp_enqueue_script() to add the js to WordPress queue 
  255. * @uses bpbbpst_get_plugin_url() to build the path to plugin's js folder 
  256. * @uses bpbbpst_get_plugin_version() to get plugin's version 
  257. */ 
  258. public function enqueue_forum_js() { 
  259.  
  260. if ( ! isset( get_current_screen()->post_type ) || ( bbp_get_forum_post_type() != get_current_screen()->post_type ) ) { 
  261. return; 
  262.  
  263. wp_enqueue_script( 'bpbbpst-forum-js', bpbbpst_get_plugin_url( 'js' ) . 'bpbbpst-forum.js', array( 'jquery' ), bpbbpst_get_plugin_version(), true ); 
  264.  
  265. /** 
  266. * Hooks pre_get_users to build a cutom meta_query to list forum moderators 
  267. * 
  268. * First checks for who arguments to be sure we're requesting forum moderators 
  269. * 
  270. * @since 2.0 
  271. * 
  272. * @global object $wpdb (the database class) 
  273. * @global integer the current blog id 
  274. * @param object $query the user query arguments 
  275. * @uses bbp_get_keymaster_role() to get keymaster role 
  276. * @uses bbp_get_moderator_role() to get moderator role 
  277. */ 
  278. public function filter_user_query( $query = false ) { 
  279. global $wpdb; 
  280.  
  281. if ( empty( $query->query_vars['who'] ) || 'bpbbpst_moderators' != $query->query_vars['who'] ) { 
  282. return; 
  283.  
  284. $unset = array_fill_keys( array( 
  285. 'who',  
  286. 'blog_id', // to avoid the extra meta query in multisite 
  287. 'meta_key', // to make sure no primary meta query is set 
  288. 'meta_value', // to make sure no primary meta query is set 
  289. 'meta_compare' // to make sure no primary meta query is set 
  290. ), false ); 
  291.  
  292. // Unset the query vars before adding our meta query one 
  293. $query->query_vars = array_diff_key( $query->query_vars, $unset ); 
  294.  
  295. // Set current blog 
  296. $blog_id = get_current_blog_id(); 
  297.  
  298. // Set meta key 
  299. $meta_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; 
  300.  
  301. // Set meta query 
  302. $query->query_vars['meta_query'] = array( 
  303. 'relation' => 'OR',  
  304. array( 
  305. 'key' => $meta_key,  
  306. 'value' => bbp_get_keymaster_role(),  
  307. 'compare' => 'LIKE' 
  308. ),  
  309. array( 
  310. 'key' => $meta_key,  
  311. 'value' => bbp_get_moderator_role(),  
  312. 'compare' => 'LIKE' 
  313. ),  
  314. ); 
  315.  
  316. /** 
  317. * Adds a selectbox to update the support status to topic attributes metabox 
  318. * 
  319. * @since 2.0 
  320. * 
  321. * @param integer $topic_id the topic id 
  322. * @uses bbp_get_topic_forum_id() to get the parent forum id 
  323. * @uses bpbbpst_get_forum_support_setting() to get the support setting of the parent forum 
  324. * @uses get_post_meta() to get the previuosly stored topic support status 
  325. * @uses bpbbpst_get_selectbox() to build the support status selectbox 
  326. */ 
  327. public function topic_meta_box( $topic_id = 0 ) { 
  328. // Since 2.0, we first need to check parent forum has support for support :) 
  329. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  330.  
  331. if ( empty( $forum_id ) ) { 
  332. return false; 
  333.  
  334. if ( 3 == bpbbpst_get_forum_support_setting( $forum_id ) ) { 
  335. return false; 
  336.  
  337. $support_status = get_post_meta( $topic_id, '_bpbbpst_support_topic', true ); 
  338.  
  339. if ( empty( $support_status ) ) { 
  340. $support_status = 0; 
  341. ?> 
  342. <p> 
  343. <strong class="label"><?php _e( 'Support:', 'buddy-bbpress-support-topic' ); ?></strong> 
  344. <label class="screen-reader-text" for="parent_id"><?php _e( 'Support', 'buddy-bbpress-support-topic' ); ?></label> 
  345. <?php echo bpbbpst_get_selectbox( $support_status, $topic_id);?> 
  346. </p> 
  347. <?php 
  348.  
  349. /** 
  350. * Saves support status for the topic (admin) 
  351. * 
  352. * @since 2.0 
  353. * 
  354. * @param integer $topic_id the topic id 
  355. * @param integer $forum_id the parent forum id 
  356. * @uses wp_verify_nonce() for security reason 
  357. * @uses delete_post_meta() to eventually delete the support status 
  358. * @uses update_post_meta() to save the support status 
  359. * @uses do_action() call 'bpbbpst_topic_meta_box_save' to perform custom actions for the topic support 
  360. * @return integer the topic id 
  361. */ 
  362. public function topic_meta_box_save( $topic_id = 0, $forum_id = 0 ) { 
  363.  
  364. if ( ! isset( $_POST['_support_status'] ) || $_POST['_support_status'] === false ) { 
  365. return $topic_id; 
  366.  
  367. $new_status = intval( $_POST['_support_status'] ); 
  368.  
  369. if ( $new_status !== false && ! empty( $_POST['_wpnonce_bpbbpst_support_status'] ) && wp_verify_nonce( $_POST['_wpnonce_bpbbpst_support_status'], 'bpbbpst_support_status') ) { 
  370.  
  371. if ( empty( $new_status ) ) { 
  372. delete_post_meta( $topic_id, '_bpbbpst_support_topic' ); 
  373. } else { 
  374. update_post_meta( $topic_id, '_bpbbpst_support_topic', $new_status ); 
  375.  
  376. do_action( 'bpbbpst_topic_meta_box_save', $new_status ); 
  377.  
  378.  
  379. return $topic_id; 
  380.  
  381. /** 
  382. * Handles the support status in case a topic moved to another forum (admin) 
  383. * 
  384. * In case a topic moves to another forum, we need to check the new parent forum 
  385. * support setting to eventually delete the support status or create it. 
  386. * 
  387. * @since 2.0 
  388. * 
  389. * @param integer $topic_id the topic id 
  390. * @param object $topic the topic object 
  391. * @uses get_current_screen() to make sure we're editing a topic from admin 
  392. * @uses bbp_get_topic_post_type() to get topic post type 
  393. * @uses bbp_is_post_request() to make sure we're playing with a post request 
  394. * @uses wp_verify_nonce() for security reasons 
  395. * @uses current_user_can() to check for current user's capability 
  396. * @uses bpbbpst_handle_moving_topic() to handle topic move 
  397. */ 
  398. public function topic_moved( $topic_id = 0, $topic = false ) { 
  399. if ( ! isset( get_current_screen()->post_type ) || ( bbp_get_topic_post_type() != get_current_screen()->post_type ) ) { 
  400. return $topic_id; 
  401.  
  402. // Bail if doing an autosave 
  403. if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
  404. return $topic_id; 
  405.  
  406. // Bail if not a post request 
  407. if ( ! bbp_is_post_request() ) { 
  408. return $topic_id; 
  409.  
  410. // Nonce check 
  411. if ( empty( $_POST['bbp_topic_metabox'] ) || ! wp_verify_nonce( $_POST['bbp_topic_metabox'], 'bbp_topic_metabox_save' ) ) { 
  412. return $topic_id; 
  413.  
  414. // Bail if current user cannot edit this topic 
  415. if ( !current_user_can( 'edit_topic', $topic_id ) ) { 
  416. return $topic_id; 
  417.  
  418. // Get the forum ID 
  419. $forum_id = ! empty( $_POST['parent_id'] ) ? (int) $_POST['parent_id'] : 0; 
  420.  
  421. if ( empty( $forum_id ) ) { 
  422. return $topic_id; 
  423.  
  424. if ( $the_topic = wp_is_post_revision( $topic_id ) ) { 
  425. $topic_id = $the_topic; 
  426.  
  427. bpbbpst_handle_moving_topic( $topic_id, $forum_id ); 
  428.  
  429. /** 
  430. * Registers a new column to topics admin list to show support status 
  431. * 
  432. * @since 2.0 
  433. * 
  434. * @param array $columns the registered columns 
  435. * @return array the columns with the support one 
  436. */ 
  437. public function topics_admin_column( $columns = array() ) { 
  438. $columns['buddy_bbp_st_support'] = __( 'Support', 'buddy-bbpress-support-topic' ); 
  439.  
  440. return $columns; 
  441.  
  442. /** 
  443. * Displays the support status of each topic row 
  444. * 
  445. * @since 2.0 
  446. * 
  447. * @param string $column the column id 
  448. * @param integer $topic_id the topic id 
  449. * @uses bpbbpst_add_support_mention() to output the topic support status 
  450. */ 
  451. public function topics_column_data( $column = '', $topic_id = 0 ) { 
  452. if ( $column == 'buddy_bbp_st_support' && ! empty( $topic_id ) ) { 
  453. bpbbpst_add_support_mention( $topic_id ); 
  454.  
  455. /** 
  456. * Adds a selectbox to allow filtering topics by status (admin) 
  457. * 
  458. * @since 2.0 
  459. * 
  460. * @uses get_current_screen() to be sure we're on topic admin list 
  461. * @uses bbp_get_topic_post_type() to get topic post type 
  462. * @uses bpbbpst_get_selectbox() to output the support status selectbox 
  463. */ 
  464. public function topics_admin_support_filter() { 
  465. if ( get_current_screen()->post_type == bbp_get_topic_post_type() ) { 
  466.  
  467. $selected = empty( $_GET['_support_status'] ) ? -1 : intval( $_GET['_support_status'] ); 
  468. //displays the selectbox to filter by support status 
  469. echo bpbbpst_get_selectbox( $selected , 'adminlist' ); 
  470.  
  471. /** 
  472. * Filters bbPress query to include a support status meta query 
  473. * 
  474. * @since 2.0 
  475. * 
  476. * @param array $query_vars the bbPress query vars 
  477. * @uses is_admin() to check we're in WordPress backend 
  478. * @return array the query vars with a support meta query 
  479. */ 
  480. public function topics_admin_support_request( $query_vars = array() ) { 
  481. if ( ! is_admin() ) { 
  482. return $query_vars; 
  483.  
  484. if ( empty( $_GET['_support_status'] ) ) { 
  485. return $query_vars; 
  486.  
  487. $support_status = intval( $_GET['_support_status'] ); 
  488.  
  489. if ( ! empty( $query_vars['meta_key'] ) ) { 
  490.  
  491. if ( $support_status == -1 ) { 
  492. return $query_vars; 
  493.  
  494. unset( $query_vars['meta_value'], $query_vars['meta_key'] ); 
  495.  
  496. $query_vars['meta_query'] = array( 
  497. array( 
  498. 'key' => '_bpbbpst_support_topic',  
  499. 'value' => $support_status,  
  500. 'compare' => '=' 
  501. ),  
  502. array( 
  503. 'key' => '_bbp_forum_id',  
  504. 'value' => intval( $_GET['bbp_forum_id'] ),  
  505. 'compare' => '=' 
  506. ); 
  507.  
  508. } else { 
  509.  
  510. if ( $support_status == -1 ) { 
  511. return $query_vars; 
  512.  
  513. $query_vars['meta_key'] = '_bpbbpst_support_topic'; 
  514. $query_vars['meta_value'] = $support_status; 
  515.  
  516.  
  517. return $query_vars; 
  518.  
  519. /** 
  520. * Adds an inline edit part to topics row to allow support bulk edit 
  521. * 
  522. * @since 2.0 
  523. * 
  524. * @param string $column_name the colum name id 
  525. * @param string $post_type the post type id 
  526. * @uses bpbbpst_get_support_status() to get available support statuses 
  527. * @return string html output 
  528. */ 
  529. public function bulk_topics_support( $column_name = '', $post_type = '' ) { 
  530. if ( $column_name != 'buddy_bbp_st_support' ) { 
  531. return; 
  532.  
  533. $all_status = bpbbpst_get_support_status(); 
  534.  
  535. if ( empty( $all_status ) || ! is_array( $all_status ) ) { 
  536. return; 
  537. ?> 
  538. <fieldset class="inline-edit-col-right"> 
  539. <div class="inline-edit-col"> 
  540. <div class="inline-edit-group"> 
  541. <label class="alignleft"> 
  542. <span class="title"><?php _e( 'Support' ); ?></span> 
  543. <select name="_support_status"> 
  544. <?php foreach ( $all_status as $status ) : ?> 
  545. <option value="<?php echo $status['value'];?>"><?php echo $status['sb-caption']; ?></option> 
  546. <?php endforeach ; ?> 
  547. </select> 
  548. </label> 
  549. </div> 
  550. </div> 
  551. </fieldset> 
  552. <?php 
  553.  
  554. /** 
  555. * Bulk update support statuses for selected topics 
  556. * 
  557. * @since 2.0 
  558. * 
  559. * @uses wp_parse_id_list() to sanitize list of topic ids 
  560. * @uses bbp_get_topic_forum_id() to get the forum parent id 
  561. * @uses bpbbpst_get_forum_support_setting() to get forum parent support setting 
  562. * @uses update_post_meta() to update the support statuses for selected topic ids 
  563. * @return boolean true 
  564. */ 
  565. public function bulk_update_support() { 
  566.  
  567. if ( ! isset( $_GET['bulk_edit'] ) ) { 
  568. return; 
  569.  
  570. if ( ! isset( $_GET['post_type'] ) || $_GET['post_type'] != bbp_get_topic_post_type() ) { 
  571. return; 
  572.  
  573. if ( ! isset( $_GET['_support_status'] ) ) { 
  574. return; 
  575.  
  576. if ( ! isset( $_GET['post'] ) ) { 
  577. return; 
  578.  
  579. $topic_ids = wp_parse_id_list( $_GET['post'] ); 
  580.  
  581. $support_status = intval( $_GET['_support_status'] ); 
  582.  
  583. foreach ( $topic_ids as $topic_id ) { 
  584. // we need to check the topic belongs to a support featured forum 
  585. $forum_id = bbp_get_topic_forum_id( $topic_id ); 
  586.  
  587. if ( empty( $forum_id ) || ( 3 == bpbbpst_get_forum_support_setting( $forum_id ) && 0 != $support_status ) ) { 
  588. continue; 
  589.  
  590. if ( 2 == bpbbpst_get_forum_support_setting( $forum_id ) && 0 == $support_status ) { 
  591. continue; 
  592.  
  593. update_post_meta( $topic_id, '_bpbbpst_support_topic', $support_status ); 
  594.  
  595. return true; 
  596.  
  597. /** 
  598. * Extends bbPress right now Dashboard widget to display support statistics 
  599. * bbPress Right now Dashboard widget seems to disappear in 2.6 
  600. * 
  601. * @since 2.0 
  602. * 
  603. * @uses bpbbpst_support_statistics() to build the support statistics 
  604. * @uses current_user_can() to check for current user's capability 
  605. * @uses add_query_arg() to build links to topic admin list filtered by support status 
  606. * @uses bbp_get_topic_post_type() to get the topic post type 
  607. * @uses get_admin_url() to get the admin url 
  608. * @return string html output 
  609. */ 
  610. public function dashboard_widget() { 
  611. $support_statistics = bpbbpst_support_statistics(); 
  612.  
  613. if ( empty( $support_statistics['total_support'] ) ) { 
  614. return false; 
  615.  
  616. $status_stats = $support_statistics['allstatus']; 
  617.  
  618. if ( ! is_array( $status_stats ) || count( $status_stats ) < 1 ) { 
  619. return false; 
  620.  
  621. ?> 
  622. <div class="table table_content" style="margin-top:40px"> 
  623. <p class="sub"><?php _e( 'Support topics', 'bbpress' ); ?></p> 
  624. <table> 
  625. <tr class="first"> 
  626.  
  627. <td class="first b b-topics"><span class="total-count"><?php echo $support_statistics['percent']; ?></span></td> 
  628. <td class="t topics"><?php _e( 'Resolved so far', 'buddy-bbpress-support-topic' ); ?></td> 
  629.  
  630. </tr> 
  631.  
  632. <?php foreach ( $status_stats as $key => $stat ) : ?> 
  633.  
  634. <tr class="first"> 
  635.  
  636. <?php 
  637. $num = $stat['stat']; 
  638. $text = $stat['label']; 
  639. $class = $stat['admin_class']; 
  640.  
  641. if ( current_user_can( 'publish_topics' ) ) { 
  642. $link = add_query_arg( array( 'post_type' => bbp_get_topic_post_type(), '_support_status' => $key ), get_admin_url( null, 'edit.php' ) ); 
  643. $num = '<a href="' . $link . '" class="' . $class . '">' . $num . '</a>'; 
  644. $text = '<a href="' . $link . '" class="' . $class . '">' . $text . '</a>'; 
  645. ?> 
  646.  
  647. <td class="first b b-topic_tags"><?php echo $num; ?></td> 
  648. <td class="t topic_tags"><?php echo $text; ?></td> 
  649.  
  650. </tr> 
  651.  
  652. <?php endforeach ; ?> 
  653.  
  654. </table> 
  655.  
  656. </div> 
  657. <?php 
  658.  
  659. /** 
  660. * Register custom elements in the at a glance Dashboard Widget 
  661. * This is appearing in bbPress 2.6 
  662. * 
  663. * @since 2.0 
  664. * 
  665. * @param array $elements list of shortcut links 
  666. * @param array $stats bbPress stats 
  667. * @return array same elements with plugin's ones if needed 
  668. */ 
  669. public function dashboard_at_a_glance( $elements = array(), $stats = array() ) { 
  670. if ( empty( $elements ) || empty( $stats['topic_count'] ) ) { 
  671. return $elements; 
  672.  
  673. $support_statistics = bpbbpst_support_statistics(); 
  674.  
  675. if ( empty( $support_statistics['total_support'] ) ) { 
  676. return $elements; 
  677.  
  678. $status_stats = $support_statistics['allstatus']; 
  679.  
  680. if ( ! is_array( $status_stats ) || count( $status_stats ) < 1 ) { 
  681. return $elements; 
  682.  
  683. foreach ( (array) $status_stats as $key => $stat ) { 
  684. $link = add_query_arg( array( 'post_type' => bbp_get_topic_post_type(), '_support_status' => $key ), get_admin_url( null, 'edit.php' ) ); 
  685. $text = sprintf( '%d %s', $stat['stat'], $stat['label'] ); 
  686. $class = array( $stat['admin_class'] ); 
  687.  
  688. if ( ! empty( $stat['dashicon']['class'] ) ) { 
  689. $class[] = $stat['dashicon']['class']; 
  690.  
  691. $elements[] = '<a href="' . esc_url( $link ) . '" class="' . join( ' ', $class ) . '">' . esc_html( $text ) . '</a>'; 
  692.  
  693. return $elements; 
  694.  
  695. /** 
  696. * Check for welcome screen transient to eventually redirect admin to welcome screen 
  697. * 
  698. * @since 2.0 
  699. * 
  700. * @uses get_transient() to get the transient created on plugin's activation 
  701. * @uses delete_transient() to remove it 
  702. * @uses is_network_admin() to check for network admin area 
  703. * @uses wp_safe_redirect() to redirect admin to welcome screen 
  704. * @uses add_query_arg() to build the link to the welcome screen 
  705. * @uses admin_url() to get admin url 
  706. */ 
  707. public function maybe_activate() { 
  708.  
  709. if ( ! get_transient( '_bpbbst_welcome_screen' ) ) { 
  710. return; 
  711.  
  712. // Delete the redirect transient 
  713. delete_transient( '_bpbbst_welcome_screen' ); 
  714.  
  715. // Bail if activating from network, or bulk 
  716. if ( is_network_admin() || isset( $_GET['activate-multi'] ) ) { 
  717. return; 
  718.  
  719. wp_safe_redirect( add_query_arg( array( 'page' => 'bpbbst-about' ), admin_url( 'index.php' ) ) ); 
  720.  
  721. /** 
  722. * Adds a submenu to dashboard page to register the welcome screen 
  723. * 
  724. * @since 2.0 
  725. * 
  726. * @uses add_dashboard_page() to build the dashboard submenu 
  727. * @uses get_option() to get db version 
  728. * @uses bpbbpst_get_plugin_version() to get plugin's version 
  729. * @uses update_option() to eventually update db version 
  730. * @uses do_action() call 'bpbbpst_upgrade' to perform custom actions once the plugin has been upgraded 
  731. */ 
  732. public function welcome_screen_register() { 
  733.  
  734. $this->about_page = add_dashboard_page( 
  735. __( 'Welcome to Buddy-bbPress Support Topic', 'buddy-bbpress-support-topic' ),  
  736. __( 'Welcome to Buddy-bbPress Support Topic', 'buddy-bbpress-support-topic' ),  
  737. 'manage_options',  
  738. 'bpbbst-about',  
  739. array( &$this, 'welcome_screen_display' ) 
  740. ); 
  741.  
  742. // do not upgrade version if required is not ok 
  743. if ( ! bpbbpst_is_bbp_required_version_ok() ) { 
  744. return; 
  745.  
  746. $db_version = get_option( 'bp-bbp-st-version' ); 
  747. $plugin_version = bpbbpst_get_plugin_version(); 
  748.  
  749. if ( empty( $db_version ) || $plugin_version != $db_version ) { 
  750. update_option( 'bp-bbp-st-version', $plugin_version ); 
  751.  
  752. do_action( 'bpbbpst_upgrade', $plugin_version, $db_version ); 
  753.  
  754. /** 
  755. * Displays the welcome screen of the plugin 
  756. * 
  757. * @since 2.0 
  758. * 
  759. * @uses bpbbpst_get_plugin_version() to get plugin's version 
  760. * @uses bpbbpst_get_plugin_url() to get plugin's url 
  761. * @uses bpbbpst_bbp_required_version() to get bbPress required version 
  762. * @uses esc_url() to sanitize urls 
  763. * @uses admin_url() to build the admin url of welcome screen 
  764. * @uses add_query_arg() to add arguments to the admin url 
  765. * @return string html 
  766. */ 
  767. public function welcome_screen_display() { 
  768. $display_version = bpbbpst_get_plugin_version(); 
  769. $plugin_url = bpbbpst_get_plugin_url(); 
  770. $bbpress_required = sprintf( '<strong>bbPress %s</strong>', bpbbpst_bbp_required_version() ); 
  771. ?> 
  772. <div class="wrap about-wrap"> 
  773. <h1><?php printf( __( 'Buddy-bbPress Support Topic %s', 'buddy-bbpress-support-topic' ), $display_version ); ?></h1> 
  774.  
  775. <?php if ( ! bpbbpst_is_bbp_required_version_ok() ) :?> 
  776. <div class="about-text"><?php printf( esc_html__( 'Ouch! Buddy-bbPress Support Topic %s requires at least %s, see below why it might be a great idea to upgrade bbPress ;)', 'buddy-bbpress-support-topic' ), $display_version, $bbpress_required ); ?></div> 
  777. <?php else : ?> 
  778. <div class="about-text"><?php printf( esc_html__( 'Thank you for using the latest version of Buddy-bbPress Support Topic (%s). Support only forums got some nice improvements!', 'buddy-bbpress-support-topic' ), $display_version ); ?></div> 
  779. <?php endif; ?> 
  780.  
  781. <div class="bpbbpst-badge"> 
  782. <div class="badge"></div> 
  783. <div class="badge-text"><?php esc_html_e( 'Support', 'buddy-bbpress-support-topic' ); ?></div> 
  784. </div> 
  785.  
  786. <h2 class="nav-tab-wrapper wp-clearfix"> 
  787. <a class="nav-tab nav-tab-active" href="<?php echo esc_url( admin_url( add_query_arg( array( 'page' => 'bpbbst-about' ), 'index.php' ) ) ); ?>"> 
  788. <?php esc_html_e( 'What's New', 'buddy-bbpress-support-topic' ); ?> 
  789. </a> 
  790. </h2> 
  791.  
  792. <div class="headline-feature feature-section one-col"> 
  793. <h2><?php esc_html_e( 'Guidelines & support topic templates for your support only forums.', 'buddy-bbpress-support-topic' ); ?></h2> 
  794.  
  795. <div class="featured-image"> 
  796. <img src="https://cldup.com/0iNnnWZw5d.png" alt="<?php esc_attr_e( 'Guidelines and Support topic template', 'buddy-bbpress-support-topic' ); ?>"> 
  797. </div> 
  798.  
  799. <p><?php esc_html_e( 'Providing the right answer often requires to have all needed informations about an issue. Support only forums can now use some guidelines and/or a support topic template to help the user describe the issue the best way.', 'buddy-bbpress-support-topic' ); ?></p> 
  800. </div> 
  801.  
  802. <div class="feature-section two-col"> 
  803. <div class="col"> 
  804. <h3><?php esc_html_e( 'Guidelines', 'buddy-bbpress-support-topic' ); ?></h3> 
  805.  
  806. <p><?php esc_html_e( 'They are displayed before the topic form to let the user know about the specific rules to observe for the support forum he will post his topic into.', 'buddy-bbpress-support-topic' ); ?></p> 
  807.  
  808. <h3><?php esc_html_e( 'Support topic templates', 'buddy-bbpress-support-topic' ); ?></h3> 
  809.  
  810. <p><?php esc_html_e( 'Just like guidelines, each forum can include a specific topic template to display a checklist or some specific advices into the content area of the topic form.', 'buddy-bbpress-support-topic' ); ?></p> 
  811. </div> 
  812. <div class="col"> 
  813. <div class="media-container"> 
  814. <img src="https://cldup.com/3SOgj-v0ny.png" alt="<?php esc_attr_e( 'Guidelines and Support topic template inside the topic form', 'buddy-bbpress-support-topic' ); ?>"> 
  815. </div> 
  816. </div> 
  817. </div> 
  818.  
  819. <div class="clear"></div> 
  820.  
  821. <hr> 
  822.  
  823. <div class="feature-section two-col"> 
  824. <div class="col"> 
  825. <div class="media-container"> 
  826. <img src="https://cldup.com/3t2XlLbz1b.png" alt="<?php esc_attr_e( 'reply checkbox', 'buddy-bbpress-support-topic' ); ?>"> 
  827. </div> 
  828. </div> 
  829. <div class="col"> 
  830. <h3><?php esc_html_e( 'Mark this topic as resolved!', 'buddy-bbpress-support-topic' ); ?></h3> 
  831.  
  832. <p><?php esc_html_e( 'The reply to the topic you are about to post is fixing the issue? Great job, you can now directly mark the support topic as resolved by activating the corresponding checkbox.', 'buddy-bbpress-support-topic' ); ?></p> 
  833. </div> 
  834. </div> 
  835.  
  836. <div class="clear"></div> 
  837.  
  838. <hr> 
  839.  
  840. <h3 class="wp-people-group"><?php printf( esc_html__( 'Many thanks to %s contributors', 'buddy-bbpress-support-topic' ), $display_version ); ?></h3> 
  841.  
  842. <p class="wp-credits-list"> 
  843. <a href="https://profiles.wordpress.org/danieliser">danieliser</a>,  
  844. <a href="https://profiles.wordpress.org/imath">imath</a>. 
  845. </p> 
  846. <?php 
  847.  
  848. /** 
  849. * Outputs some css rules if on welcome screen or on dashboard (at a glance widget) 
  850. * 
  851. * @since 2.0 
  852. * 
  853. * @uses remove_submenu_page() to remove the page from dashoboard menu 
  854. * @uses bpbbpst_get_plugin_url() to get the plugin url 
  855. * @uses get_current_screen() to check current page is the welcome screen 
  856. * @uses bpbbpst_get_support_status() to get all available support status 
  857. * @uses sanitize_html_class() to sanitize the clases 
  858. * @uses sanitize_text_field() to sanitize the content 
  859. * @return string css rules 
  860. */ 
  861. public function welcome_screen_css() { 
  862. // Avoid a notice in network administration 
  863. if ( is_network_admin() ) { 
  864. return; 
  865.  
  866. remove_submenu_page( 'index.php', 'bpbbst-about'); 
  867.  
  868. if ( $this->about_page == get_current_screen()->id ) { 
  869. ?> 
  870. <style type="text/css" media="screen"> 
  871. /*<![CDATA[*/ 
  872.  
  873. .about-wrap .bpbbpst-badge .badge { 
  874. font: normal 150px/1 'dashicons' !important; 
  875. /* Better Font Rendering =========== */ 
  876. -webkit-font-smoothing: antialiased; 
  877. -moz-osx-font-smoothing: grayscale; 
  878.  
  879. color: #333; 
  880. display: inline-block; 
  881. content:''; 
  882.  
  883. .about-wrap .bpbbpst-badge .badge:before{ 
  884. content: "\f488"; 
  885.  
  886. .bpbbpst-badge { 
  887. background-color: #FFF; 
  888. width: 150px; 
  889. -webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, .2); 
  890. box-shadow: 0 1px 3px rgba(0, 0, 0, .2); 
  891.  
  892. .bpbbpst-badge .badge-text { 
  893. color:#333; 
  894. font-size: 14px; 
  895. text-align: center; 
  896. font-weight: 600; 
  897. margin: 5px 0 0; 
  898. height: 30px; 
  899. text-rendering: optimizeLegibility; 
  900.  
  901. .about-wrap .bpbbpst-badge { 
  902. position: absolute; 
  903. top: 0; 
  904. right: 0; 
  905. body.rtl .about-wrap .bpbbpst-badge { 
  906. right: auto; 
  907. left: 0; 
  908.  
  909. /*]]>*/ 
  910. </style> 
  911. <?php 
  912. } else if ( 'dashboard' == get_current_screen()->id ) { 
  913. ?> 
  914. <style type="text/css" media="screen"> 
  915. /*<![CDATA[*/ 
  916.  
  917. <?php foreach ( (array) bpbbpst_get_support_status() as $status ) : ?> 
  918.  
  919. <?php if ( empty( $status['dashicon']['class'] ) || empty( $status['dashicon']['content'] ) ) : continue ; endif ;?> 
  920.  
  921. #dashboard_right_now a.<?php echo sanitize_html_class( $status['dashicon']['class'] ) ;?>:before { 
  922. content: <?php echo sanitize_text_field( $status['dashicon']['content'] ) ;?>; 
  923.  
  924. <?php endforeach ;?> 
  925.  
  926. /*]]>*/ 
  927. </style> 
  928. <?php 
  929.  
  930. /** 
  931. * Adds a custom link to the welcome screen in plugin's list for our row 
  932. * 
  933. * @since 2.0 
  934. * 
  935. * @param array $links the plugin links 
  936. * @param string $file the plugin in row 
  937. * @uses plugin_basename() to get plugin's basename 
  938. * @uses bbpress() to get bbPress main instance 
  939. * @uses add_query_arg() to build the url to our welcome screen 
  940. * @uses admin_url() to get admin url to dashoboard 
  941. * @uses esc_html__() to sanitize translated string 
  942. * @return array the plugin links 
  943. */ 
  944. public function modify_plugin_action_links( $links = array(), $file = '' ) { 
  945. // Do not touch to links in network admin 
  946. if ( is_network_admin() ) { 
  947. return $links; 
  948.  
  949. // Return normal links if not BuddyPress 
  950. if ( plugin_basename( bbpress()->extend->bpbbpst->globals->file ) != $file ) { 
  951. return $links; 
  952.  
  953. // Add a few links to the existing links array 
  954. return array_merge( $links, array( 
  955. 'about' => '<a href="' . add_query_arg( array( 'page' => 'bpbbst-about' ), admin_url( 'index.php' ) ) . '">' . esc_html__( 'About', 'buddy-bbpress-support-topic' ) . '</a>' 
  956. ) ); 
  957.  
  958.  
  959.  
  960. endif; // class_exists check 
  961.  
  962. /** 
  963. * Setup Buddy-bbPress Support Topic Admin 
  964. * 
  965. * @since 2.0 
  966. * 
  967. * @uses bbpress() to get main bbPress instance 
  968. * @uses BP_bbP_ST_Admin() to start the admin part of the plugin 
  969. */ 
  970. function bpbbpst_admin() { 
  971. bbpress()->extend->bpbbpst->admin = new BP_bbP_ST_Admin(); 
.