PW_BBP_Admin_Notes

PW_BBP_Admin_Notes Class.

Defined (1)

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

/bbp-admin-notes.php  
  1. final class PW_BBP_Admin_Notes { 
  2. /** 
  3. * Holds the instance 
  4. * Ensures that only one instance of bbPress Admin Notes exists in memory at any one 
  5. * time and it also prevents needing to define globals all over the place. 
  6. * @var object 
  7. * @static 
  8. * @since 1.0 
  9. */ 
  10. private static $instance; 
  11.  
  12. /** 
  13. * Get the instance and store the class inside it. This plugin utilises 
  14. * the PHP singleton design pattern. 
  15. * @since 1.0 
  16. * @static 
  17. * @staticvar array $instance 
  18. * @access public 
  19. * @uses PW_BBP_Admin_Notes::actions() 
  20. * @uses PW_BBP_Admin_Notes::filters() 
  21. * @return object self::$instance Instance 
  22. */ 
  23. public static function instance() { 
  24. if ( ! isset( self::$instance ) ) { 
  25. self::$instance = new PW_BBP_Admin_Notes; 
  26. self::$instance->actions(); 
  27. self::$instance->filters(); 
  28.  
  29. return self::$instance; 
  30.  
  31. /** 
  32. * Dummy constructor 
  33. * @since 1.0 
  34. * @access protected 
  35. * @return void 
  36. */ 
  37. private function __construct() { /** nothing here */ } 
  38.  
  39.  
  40. /** 
  41. * Add all actions we need 
  42. * @since 1.0 
  43. * @access private 
  44. * @return void 
  45. */ 
  46. private function actions() { 
  47. // Load plugin textdomain 
  48. add_action( 'init', array( $this, 'load_textdomain' ) ); 
  49.  
  50. // Enable comments on the reply post type 
  51. add_action( 'init', array( $this, 'add_comment_support' ) ); 
  52.  
  53. // Save the new notes 
  54. add_action( 'init', array( $this, 'save_note' ) ); 
  55.  
  56. // Removes the "Discussion" box 
  57. add_action( 'add_meta_boxes', array( $this, 'remove_comments_status_box' ) ); 
  58.  
  59. // Load the notes CSS 
  60. add_action( 'wp_enqueue_scripts', array( $this, 'notes_css' ) ); 
  61.  
  62. // Append the notes to the bottom of replies 
  63. add_action( 'bbp_theme_after_reply_content', array( $this, 'reply_notes' ) ); 
  64.  
  65. // Output the "Add Notes" form 
  66. add_action( 'bbp_theme_after_topicy_content', array( $this, 'add_note_form' ) ); 
  67. add_action( 'bbp_theme_after_reply_content', array( $this, 'add_note_form' ) ); 
  68.  
  69. // Output our custom JS for the "Add Notes" form 
  70. add_action( 'wp_footer', array( $this, 'notes_js' ) ); 
  71.  
  72.  
  73. /** 
  74. * Add all filters we need 
  75. * @since 1.0 
  76. * @access private 
  77. * @return void 
  78. */ 
  79. private function filters() { 
  80. // Add our custom admin links 
  81. add_filter( 'bbp_get_topic_admin_links', array( $this, 'add_topic_note_link' ), 10, 2 ); 
  82. add_filter( 'bbp_get_reply_admin_links', array( $this, 'add_reply_note_link' ), 10, 2 ); 
  83. add_filter( 'comments_clauses', array( $this, 'hidenotes' ), 10, 2 ); 
  84. add_filter( 'comment_feed_where', array( $this, 'hide_notes_from_feeds' ), 10, 2 ); 
  85.  
  86.  
  87. /** 
  88. * Loads the plugin textdomain 
  89. * @since 1.0 
  90. * @access public 
  91. * @return bool 
  92. */ 
  93. public function load_textdomain() { 
  94. // Set filter for plugin's languages directory 
  95. $lang_dir = dirname( plugin_basename( __FILE__ ) ) . '/languages/'; 
  96. $lang_dir = apply_filters( 'bbp_admin_notes_languages', $lang_dir ); 
  97.  
  98. // Traditional WordPress plugin locale filter 
  99. $locale = apply_filters( 'plugin_locale', get_locale(), 'bbp-admin-notes' ); 
  100. $mofile = sprintf( '%1$s-%2$s.mo', 'bbp-admin-notes', $locale ); 
  101.  
  102. // Setup paths to current locale file 
  103. $mofile_local = $lang_dir . $mofile; 
  104. $mofile_global = WP_LANG_DIR . '/bbp-admin-notes/' . $mofile; 
  105.  
  106. if ( file_exists( $mofile_global ) ) { 
  107. // Look in global /wp-content/languages/bbp-admin-notes folder 
  108. load_textdomain( 'bbp-admin-notes', $mofile_global ); 
  109. } elseif ( file_exists( $mofile_local ) ) { 
  110. // Look in local /wp-content/plugins/bbp-admin-notes/languages/ folder 
  111. load_textdomain( 'bbp-admin-notes', $mofile_local ); 
  112. } else { 
  113. // Load the default language files 
  114. load_plugin_textdomain( 'bbp-admin-notes', false, $lang_dir ); 
  115.  
  116. /** 
  117. * Enable comment support 
  118. * This is just for showing the Comments meta box in edit.php 
  119. * @since v1.0.3 
  120. * @access public 
  121. * @return void 
  122. */ 
  123. public function add_comment_support() { 
  124. add_post_type_support( bbp_get_topic_post_type(), 'comments' ) ; 
  125. add_post_type_support( bbp_get_reply_post_type(), 'comments' ) ; 
  126.  
  127. /** 
  128. * Remove "Discussion" meta box 
  129. * @since v1.0.3 
  130. * @access public 
  131. * @return void 
  132. */ 
  133. public function remove_comments_status_box() { 
  134. remove_meta_box( 'commentstatusdiv', bbp_get_topic_post_type(), 'normal' ); 
  135. remove_meta_box( 'commentstatusdiv', bbp_get_reply_post_type(), 'normal' ); 
  136.  
  137. /** 
  138. * Add the admin links to topics and replies 
  139. * This is kind of a hacky way of doing this for now 
  140. * Once http://bbpress.trac.wordpress.org/ticket/2090 gets implemented in some form, I will refactor this 
  141. * @since 1.0 
  142. * @access public 
  143. * @param $links string The HTML string of all existing admin links 
  144. * @param $args array All arguments passed from bbPress 
  145. * @return string 
  146. */ 
  147. public function add_topic_note_link( $links = '', $args = array() ) { 
  148. if ( ! current_user_can( 'moderate', bbp_get_forum_id() ) ) 
  149. return; 
  150.  
  151. $topic_id = bbp_get_topic_id(); 
  152.  
  153. $links .= $args['before']; 
  154. $links .= '<a href="#" class="bbp-add-note bbp-add-note-' . $topic_id . '" data-id="' . $topic_id . '">' . __( 'Add Note', 'bbp-admin-notes' ) . '</a>'; 
  155. $links .= '<a href="#" class="bbp-add-note bbp-add-note-' . $topic_id . '" style="display:none;" data-id="' . $topic_id . '">' . __( 'Hide Note', 'bbp-admin-notes' ) . '</a>'; 
  156. $links .= $args['sep'] . ' ' . $args['after']; 
  157. return $links; 
  158.  
  159. /** 
  160. * Add the admin links to topics and replies 
  161. * This is kind of a hacky way of doing this for now 
  162. * Once http://bbpress.trac.wordpress.org/ticket/2090 gets implemented in some form, I will refactor this 
  163. * @since 1.0 
  164. * @access public 
  165. * @param $links string The HTML string of all existing admin links 
  166. * @param $args array All arguments passed from bbPress 
  167. * @return string 
  168. */ 
  169. public function add_reply_note_link( $links = '', $args = array() ) { 
  170. if ( ! current_user_can( 'moderate', bbp_get_forum_id() ) ) 
  171. return; 
  172.  
  173. $reply_id = bbp_get_reply_id(); 
  174.  
  175. $links .= $args['before']; 
  176. $links .= '<a href="#" class="bbp-add-note bbp-add-note-' . $reply_id . '" data-id="' . $reply_id . '">' . __( 'Add Note', 'bbp-admin-notes' ) . '</a>'; 
  177. $links .= '<a href="#" class="bbp-add-note bbp-add-note-' . $reply_id . '" style="display:none;" data-id="' . $reply_id . '">' . __( 'Hide Note', 'bbp-admin-notes' ) . '</a>'; 
  178. $links .= $args['sep'] . ' ' . $args['after']; 
  179. return $links; 
  180.  
  181.  
  182. /** 
  183. * Shows the notes at the bottom of each reply 
  184. * @since 1.0 
  185. * @access public 
  186. * @return void 
  187. */ 
  188. public function reply_notes() { 
  189. $reply_id = bbp_get_reply_id(); 
  190. $topic_id = bbp_get_topic_id(); 
  191.  
  192. if ( ! current_user_can( 'moderate', bbp_get_forum_id() ) ) 
  193. return; 
  194.  
  195. $notes = $this->have_notes( $reply_id ); 
  196.  
  197. if ( empty( $notes ) ) 
  198. return; 
  199.  
  200. ?> 
  201. <strong><?php _e( 'Moderator Notes:', 'bbp-admin-notes' ); ?></strong> 
  202. <ul id="bbp-reply-<?php bbp_reply_id(); ?>-notes" class="bbp-reply-notes"> 
  203. <?php foreach( $notes as $note ) : ?> 
  204. <li class="bbp-reply-note" id="bbp-reply-note-<?php echo $note->comment_ID; ?>"> 
  205. <span class="bbp-note-author"><strong><?php echo $note->comment_author; ?></strong>: </span> 
  206. <span class="bbp-note-time"><?php echo date_i18n( get_option( 'date_format' ), strtotime( $note->comment_date ) ); ?> –</span> 
  207. <span class="bbp-note-content"><?php echo $note->comment_content; ?></span> 
  208. </li> 
  209. <?php endforeach; ?> 
  210. </ul> 
  211. <?php 
  212.  
  213. /** 
  214. * Attach the Add Note form to the bottom of replies 
  215. * @since v1.0 
  216. * @access public 
  217. * @return void 
  218. */ 
  219. public function add_note_form() { 
  220. $reply_id = bbp_get_reply_id(); 
  221. $topic_id = bbp_get_topic_id(); 
  222.  
  223. ?> 
  224. <form id="bbp-add-note-form-<?php bbp_reply_id(); ?>" class="bbp-add-note-form" method="post" action="<?php echo get_permalink( $topic_id ); ?>#post-<?php bbp_reply_id(); ?>" style="display:none"> 
  225. <div> 
  226. <label for="bbp-reply-note-<?php bbp_reply_id(); ?>"><?php _e( 'Add New Note to this Reply', 'bbp-admin-notes' ); ?></label><br/> 
  227. <textarea name="bbp-reply-note" id="bbp-reply-note-<?php bbp_reply_id(); ?>" cols="50" rows="10"></textarea> 
  228. </div> 
  229. <div> 
  230. <input type="hidden" name="bbp-reply-id" value="<?php bbp_reply_id(); ?>" /> 
  231. <input type="hidden" name="bbp-topic-id" value="<?php bbp_topic_id(); ?>" /> 
  232. <input type="submit" name="bbp-add-note" value="<?php _e( 'Add Note', 'bbp-admin-notes' ); ?>" /> 
  233. </div> 
  234. </form> 
  235. <?php 
  236.  
  237. /** 
  238. * Does the reply have any notes? 
  239. * @since 1.0 
  240. * @access private 
  241. * @param int $object_id Reply or Topic ID 
  242. * @return array $notes The notes attached to the reply 
  243. */ 
  244. private function have_notes( $object_id = 0 ) { 
  245. $notes = $this->get_notes( $object_id ); 
  246.  
  247. return ! empty( $notes ) ? $notes : array(); 
  248.  
  249. /** 
  250. * Retrieve the notes for the specified reply 
  251. * @since 1.0 
  252. * @access private 
  253. * @param int $reply_id Reply ID 
  254. * @return mixed Array of notes if the reply has notes, false if not 
  255. */ 
  256. private function get_notes( $reply_id = 0 ) { 
  257. $notes = get_comments( array( 
  258. 'post_id' => $reply_id,  
  259. 'order' => 'ASC' 
  260. ) ); 
  261.  
  262. if( ! empty( $notes ) ) 
  263. return $notes; 
  264. return false; 
  265.  
  266. /** 
  267. * Store a new note 
  268. * @since 1.0 
  269. * @access public 
  270. * @return int The ID of the new note 
  271. */ 
  272. public function save_note() { 
  273.  
  274. if ( ! current_user_can( 'moderate' ) ) 
  275. return; 
  276.  
  277. if ( empty( $_POST['bbp-add-note'] ) ) 
  278. return; 
  279.  
  280. if ( empty( $_POST['bbp-reply-id'] ) ) 
  281. return; 
  282.  
  283. $reply_id = absint( $_POST['bbp-reply-id'] ); 
  284.  
  285. $reply_note = wp_kses( $_POST['bbp-reply-note'], array() ); 
  286.  
  287. $user = get_userdata( get_current_user_id() ); 
  288.  
  289. $note_id = wp_insert_comment( wp_filter_comment( array( 
  290. 'comment_post_ID' => $reply_id,  
  291. 'comment_author' => $user->user_login,  
  292. 'comment_author_url' => $user->user_url,  
  293. 'comment_author_IP' => bbp_current_author_ip(),  
  294. 'comment_author_email' => $user->user_email,  
  295. 'comment_content' => $reply_note,  
  296. 'user_id' => $user->ID,  
  297. 'comment_agent' => bbp_current_author_ua(),  
  298. 'comment_date' => current_time( 'mysql' ),  
  299. 'comment_date_gmt' => current_time( 'mysql', 1 ),  
  300. 'comment_approved' => 1,  
  301. 'comment_parent' => 0,  
  302. 'comment_type' => 'bbp_note',  
  303.  
  304. ) ) ); 
  305.  
  306. /** Email all moderators who are suscribed to this topic */ 
  307. $user_ids = bbp_get_topic_subscribers( $_POST['bbp-topic-id'] ); 
  308.  
  309. foreach ( (array) $user_ids as $user_id ) { 
  310. if ( ( (int) $user_id == (int) $user->ID ) ) 
  311. continue; 
  312.  
  313. if ( ! user_can( $user_id, 'moderate' ) ) 
  314. continue; 
  315.  
  316. $link = bbp_get_reply_url( $_POST['bbp-reply-id'] ); 
  317.  
  318. $message = sprintf( __( '%1$s added a new note on: 
  319.  
  320. %2$s 
  321. ----------- 
  322.  
  323. View note: %3$s 
  324.  
  325. You are receiving this email because you subscribed to a forum topic. 
  326.  
  327. Login and visit the topic to unsubscribe from these emails.', 'bbp-admin-notes' ),  
  328.  
  329. get_the_author_meta( 'display_name', $user->ID ),  
  330. $reply_note,  
  331. $link 
  332. ); 
  333.  
  334. // For plugins to filter titles per reply/topic/user 
  335. $subject = '[' . wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ) . '] ' . strip_tags( bbp_get_topic_title( $_POST['bbp-topic-id'] ) ) . ' (' . __( 'New Admin Note', 'bbp-admin-notes' ) . ')'; 
  336.  
  337. $headers = apply_filters( 'bbp_subscription_mail_headers', array() ); 
  338.  
  339. // Get user data of this user 
  340. $user_data = get_userdata( $user_id ); 
  341.  
  342. // Send notification email 
  343. wp_mail( $user_data->user_email, $subject, $message, $headers ); 
  344.  
  345. return $note_id; 
  346.  
  347. /** 
  348. * Exclude notes (comments) on bbPress post types from showing in Recent 
  349. * Comments widgets and other locations 
  350. * @since 1.1 
  351. * @param array $clauses Comment clauses for comment query 
  352. * @param obj $wp_comment_query WordPress Comment Query Object 
  353. * @return array $clauses Updated comment clauses 
  354. */ 
  355. function hidenotes( $clauses, $wp_comment_query ) { 
  356. global $wpdb; 
  357. $clauses['where'] .= ' AND comment_type != "bbp_note"'; 
  358. return $clauses; 
  359.  
  360. /** 
  361. * Exclude notes (comments) on bbPress post types from showing in comment feeds 
  362. * @since 1.1 
  363. * @param array $where 
  364. * @param obj $wp_comment_query WordPress Comment Query Object 
  365. * @return array $where 
  366. */ 
  367. function hide_notes_from_feeds( $where, $wp_comment_query ) { 
  368. global $wpdb; 
  369.  
  370. $where .= $wpdb->prepare( " AND comment_type != %s", 'bbp_note' ); 
  371. return $where; 
  372.  
  373. /** 
  374. * Output the JS to show the Add Note form 
  375. * @since 1.0 
  376. * @access public 
  377. * @return void 
  378. */ 
  379.  
  380. public function notes_js() { 
  381.  
  382. if( ! bbp_is_single_reply() && ! bbp_is_single_topic() ) 
  383. return; 
  384.  
  385. ?> 
  386. <script type="text/javascript"> 
  387. jQuery(document).ready(function($) { 
  388. $('.bbp-add-note').on('click', function(e) { 
  389. e.preventDefault(); 
  390. var id = $(this).data('id'); 
  391. $('#bbp-add-note-form-' + id).slideToggle(); 
  392. $('.bbp-add-note-' + id).toggle(); 
  393. }); 
  394. }); 
  395. </script> 
  396. <?php 
  397.  
  398.  
  399. /** 
  400. * Load the CSS for the notes 
  401. * @since v1.0 
  402. * @return void 
  403. */ 
  404.  
  405. public function notes_css() { 
  406. if ( ! bbp_is_single_reply() && ! bbp_is_single_topic() ) 
  407. return; 
  408.  
  409. wp_enqueue_style( 'bbp-admin-notes', plugins_url( 'bbp-admin-notes.css', __FILE__ ) );