BBP_Private_Replies

Plugin Name: bbPress - Private Replies Plugin URL: http://pippinsplugins.com/bbpress-private-replies Description: Allows users to set replies as private so that only the original poster and admins can see it Version: 1.2 Author: Pippin Williamson and Remi Corson Author URI: http://pippinsplugins.com Contributors: mordauk, corsonr Text Domain: bbp_private_replies Domain Path: languages.

Defined (1)

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

/bbp-private-replies.php  
  1. class BBP_Private_Replies { 
  2.  
  3. /**--------------------------------------------* 
  4. * Constructor 
  5. *--------------------------------------------*/ 
  6.  
  7. /** 
  8. * Initializes the plugin by setting localization, filters, and administration functions. 
  9. */ 
  10. function __construct() { 
  11.  
  12. // load the plugin translation files 
  13. add_action( 'init', array( $this, 'textdomain' ) ); 
  14.  
  15. // show the "Private Reply?" checkbox 
  16. add_action( 'bbp_theme_before_reply_form_submit_wrapper', array( $this, 'checkbox' ) ); 
  17.  
  18. // save the private reply state 
  19. add_action( 'bbp_new_reply', array( $this, 'update_reply' ), 0, 6 ); 
  20. add_action( 'bbp_edit_reply', array( $this, 'update_reply' ), 0, 6 ); 
  21.  
  22. // hide reply content 
  23. add_filter( 'bbp_get_reply_excerpt', array( $this, 'hide_reply' ), 999, 2 ); 
  24. add_filter( 'bbp_get_reply_content', array( $this, 'hide_reply' ), 999, 2 ); 
  25. add_filter( 'the_content', array( $this, 'hide_reply' ), 999 ); 
  26. add_filter( 'the_excerpt', array( $this, 'hide_reply' ), 999 ); 
  27.  
  28. // prevent private replies from being sent in email subscriptions 
  29. add_filter( 'bbp_subscription_mail_message', array( $this, 'prevent_subscription_email' ), 10, 3 ); 
  30.  
  31. // add a class name indicating the read status 
  32. add_filter( 'post_class', array( $this, 'reply_post_class' ) ); 
  33.  
  34. // register css files 
  35. add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) ); 
  36.  
  37. } // end constructor 
  38.  
  39.  
  40. /** 
  41. * Load the plugin's text domain 
  42. * @since 1.0 
  43. * @return void 
  44. */ 
  45. public function textdomain() { 
  46. load_plugin_textdomain( 'bbp_private_replies', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); 
  47.  
  48.  
  49. /** 
  50. * Outputs the "Set as private reply" checkbox 
  51. * @since 1.0 
  52. * @return void 
  53. */ 
  54. public function checkbox() { 
  55.  
  56. ?> 
  57. <p> 
  58.  
  59. <input name="bbp_private_reply" id="bbp_private_reply" type="checkbox"<?php checked( '1', $this->is_private( bbp_get_reply_id() ) ); ?> value="1" tabindex="<?php bbp_tab_index(); ?>" /> 
  60.  
  61. <?php if ( bbp_is_reply_edit() && ( get_the_author_meta( 'ID' ) != bbp_get_current_user_id() ) ) : ?> 
  62.  
  63. <label for="bbp_private_reply"><?php _e( 'Set author\'s post as private.', 'bbp_private_replies' ); ?></label> 
  64.  
  65. <?php else : ?> 
  66.  
  67. <label for="bbp_private_reply"><?php _e( 'Set as private reply', 'bbp_private_replies' ); ?></label> 
  68.  
  69. <?php endif; ?> 
  70.  
  71. </p> 
  72. <?php 
  73.  
  74.  
  75.  
  76. /** 
  77. * Stores the private state on reply creation and edit 
  78. * @since 1.0 
  79. * @param $reply_id int The ID of the reply 
  80. * @param $topic_id int The ID of the topic the reply belongs to 
  81. * @param $forum_id int The ID of the forum the topic belongs to 
  82. * @param $anonymous_data bool Are we posting as an anonymous user? 
  83. * @param $author_id int The ID of user creating the reply, or the ID of the replie's author during edit 
  84. * @param $is_edit bool Are we editing a reply? 
  85. * @return void 
  86. */ 
  87. public function update_reply( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) { 
  88.  
  89. if( isset( $_POST['bbp_private_reply'] ) ) 
  90. update_post_meta( $reply_id, '_bbp_reply_is_private', '1' ); 
  91. else 
  92. delete_post_meta( $reply_id, '_bbp_reply_is_private' ); 
  93.  
  94.  
  95.  
  96. /** 
  97. * Determines if a reply is marked as private 
  98. * @since 1.0 
  99. * @param $reply_id int The ID of the reply 
  100. * @return bool 
  101. */ 
  102. public function is_private( $reply_id = 0 ) { 
  103.  
  104. $retval = false; 
  105.  
  106. // Checking a specific reply id 
  107. if ( !empty( $reply_id ) ) { 
  108. $reply = bbp_get_reply( $reply_id ); 
  109. $reply_id = !empty( $reply ) ? $reply->ID : 0; 
  110.  
  111. // Using the global reply id 
  112. } elseif ( bbp_get_reply_id() ) { 
  113. $reply_id = bbp_get_reply_id(); 
  114.  
  115. // Use the current post id 
  116. } elseif ( !bbp_get_reply_id() ) { 
  117. $reply_id = get_the_ID(); 
  118.  
  119. if ( ! empty( $reply_id ) ) { 
  120. $retval = get_post_meta( $reply_id, '_bbp_reply_is_private', true ); 
  121.  
  122. return (bool) apply_filters( 'bbp_reply_is_private', (bool) $retval, $reply_id ); 
  123.  
  124.  
  125. /** 
  126. * Hides the reply content for users that do not have permission to view it 
  127. * @since 1.0 
  128. * @param $content string The content of the reply 
  129. * @param $reply_id int The ID of the reply 
  130. * @return string 
  131. */ 
  132. public function hide_reply( $content = '', $reply_id = 0 ) { 
  133.  
  134. if( empty( $reply_id ) ) 
  135. $reply_id = bbp_get_reply_id( $reply_id ); 
  136.  
  137. if( $this->is_private( $reply_id ) ) { 
  138.  
  139. $can_view = false; 
  140. $current_user = is_user_logged_in() ? wp_get_current_user() : false; 
  141. $topic_author = bbp_get_topic_author_id(); 
  142. $reply_author = bbp_get_reply_author_id( $reply_id ); 
  143.  
  144. if( $topic_author === $current_user->ID && user_can( $reply_author, 'moderate' ) ) { 
  145. // Let the thread author view replies if the reply author is from a moderator 
  146. $can_view = true; 
  147.  
  148. if( $reply_author === $current_user->ID ) { 
  149. // Let the reply author view their own reply 
  150. $can_view = true; 
  151.  
  152. if( current_user_can( 'moderate' ) ) { 
  153. // Let moderators view all replies 
  154. $can_view = true; 
  155.  
  156. if( ! $can_view ) { 
  157. $content = __( 'This reply has been marked as private.', 'bbp_private_replies' ); 
  158.  
  159. return $content; 
  160.  
  161.  
  162. /** 
  163. * Prevents a New Reply notification from being sent if the user doesn't have permission to view it 
  164. * @since 1.0 
  165. * @param $message string The email message 
  166. * @param $reply_id int The ID of the reply 
  167. * @param $topic_id int The ID of the reply's topic 
  168. * @return mixed 
  169. */ 
  170. public function prevent_subscription_email( $message, $reply_id, $topic_id ) { 
  171.  
  172. if( $this->is_private( $reply_id ) ) { 
  173. $this->subscription_email( $message, $reply_id, $topic_id ); 
  174. return false; 
  175.  
  176. return $message; // message unchanged 
  177.  
  178.  
  179. /** 
  180. * Sends the new reply notification email to moderators on private replies 
  181. * @since 1.2 
  182. * @param $message string The email message 
  183. * @param $reply_id int The ID of the reply 
  184. * @param $topic_id int The ID of the reply's topic 
  185. * @return void 
  186. */ 
  187. public function subscription_email( $message, $reply_id, $topic_id ) { 
  188.  
  189. if( ! $this->is_private( $reply_id ) ) { 
  190.  
  191. return false; // reply isn't private so do nothing 
  192.  
  193.  
  194. $topic_author = bbp_get_topic_author_id( $topic_id ); 
  195. $reply_author = bbp_get_reply_author_id( $reply_id ); 
  196. $reply_author_name = bbp_get_reply_author_display_name( $reply_id ); 
  197.  
  198. // Strip tags from text and setup mail data 
  199. $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) ); 
  200. $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) ); 
  201. $reply_url = bbp_get_reply_url( $reply_id ); 
  202. $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 
  203. $do_not_reply = '<noreply@' . ltrim( get_home_url(), '^(http|https)://' ) . '>'; 
  204.  
  205. $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id ); 
  206.  
  207. // Array to hold BCC's 
  208. $headers = array(); 
  209.  
  210. // Setup the From header 
  211. $headers[] = 'From: ' . get_bloginfo( 'name' ) . ' ' . $do_not_reply; 
  212.  
  213. // Get topic subscribers and bail if empty 
  214. $user_ids = bbp_get_topic_subscribers( $topic_id, true ); 
  215. if ( empty( $user_ids ) ) { 
  216. return false; 
  217.  
  218. // Loop through users 
  219. foreach ( (array) $user_ids as $user_id ) { 
  220.  
  221. // Don't send notifications to the person who made the post 
  222. if ( ! empty( $reply_author ) && (int) $user_id === (int) $reply_author ) { 
  223. continue; 
  224.  
  225. if( user_can( $user_id, 'moderate' ) || (int) $topic_author === (int) $user_id ) { 
  226.  
  227. // Get email address of subscribed user 
  228. $headers[] = 'Bcc: ' . get_userdata( $user_id )->user_email; 
  229.  
  230.  
  231. wp_mail( $do_not_reply, $subject, $message, $headers ); 
  232.  
  233.  
  234. /** 
  235. * Adds a new class to replies that are marked as private 
  236. * @since 1.0 
  237. * @param $classes array An array of current class names 
  238. * @return bool 
  239. */ 
  240. public function reply_post_class( $classes ) { 
  241.  
  242. $reply_id = bbp_get_reply_id(); 
  243.  
  244. // only apply the class to replies 
  245. if( bbp_get_reply_post_type() != get_post_type( $reply_id ) ) 
  246. return $classes; 
  247.  
  248. if( $this->is_private( $reply_id ) ) 
  249. $classes[] = 'bbp-private-reply'; 
  250.  
  251. return $classes; 
  252.  
  253. /** 
  254. * Load the plugin's CSS files 
  255. * @since 1.0 
  256. * @return void 
  257. */ 
  258. public function register_plugin_styles() { 
  259. $css_path = plugin_dir_path( __FILE__ ) . 'css/frond-end.css'; 
  260. wp_enqueue_style( 'bbp_private_replies_style', plugin_dir_url( __FILE__ ) . 'css/frond-end.css', filemtime( $css_path ) ); 
  261.  
  262. } // end class