PluginProbe
Forumax – AI Powered Advanced Community Forum Plugin / 1.0.8
Forumax – AI Powered Advanced Community Forum Plugin v1.0.8
2.4.4 2.4.3 2.4.2 2.4.1 2.4.0 trunk 1.0.8 1.1.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 2.0.0 2.1.0 All 29 releases
bbp-core / includes / features / bbp-private-replies.php

bbp-private-replies.php in Forumax – AI Powered Advanced Community Forum Plugin 1.0.8, at includes/features/bbp-private-replies.php

327 lines 9.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace features;
3
4 class BBP_Private_Replies {
5
6 /**
7 * The capability required to view private posts.
8 *
9 * @since 1.3.3
10 *
11 * @var string $capability
12 */
13 public $capability = 'moderate';
14
15 /*
16 --------------------------------------------*
17 * Constructor
18 *--------------------------------------------*/
19
20 /**
21 * Initializes the plugin by setting filters, and administration functions.
22 */
23 function __construct() {
24
25 // Allow others to change the capability required to view private posts.
26 add_action( 'plugins_loaded', [ $this, 'filter_capability' ] );
27
28 // show the "Private Reply?" checkbox
29 add_action( 'bbp_theme_before_reply_form_submit_wrapper', [ $this, 'checkbox' ] );
30
31 // save the private reply state
32 add_action( 'bbp_new_reply', [ $this, 'update_reply' ], 0, 6 );
33 add_action( 'bbp_edit_reply', [ $this, 'update_reply' ], 0, 6 );
34
35 // hide reply content
36 add_filter( 'bbp_get_reply_excerpt', [ $this, 'hide_reply' ], 999, 2 );
37 add_filter( 'bbp_get_reply_content', [ $this, 'hide_reply' ], 999, 2 );
38 add_filter( 'the_content', [ $this, 'hide_reply' ], 999 );
39 add_filter( 'the_excerpt', [ $this, 'hide_reply' ], 999 );
40
41 // prevent private replies from being sent in email subscriptions
42 add_filter( 'bbp_subscription_mail_message', [ $this, 'prevent_subscription_email' ], 999999, 3 );
43
44 // add a class name indicating the read status
45 add_filter( 'post_class', [ $this, 'reply_post_class' ] );
46
47 } // end constructor
48
49 /**
50 * Called during the plugins_loaded action to filter the capability
51 * required to view private replies.
52 *
53 * @since 1.3.3
54 *
55 * @return void
56 */
57 public function filter_capability() {
58 $this->capability = apply_filters( 'bbp_private_replies_capability', $this->capability );
59 }
60
61 /**
62 * Retrieves the no reply address.
63 *
64 * @since 1.3.3
65 *
66 * @return string
67 */
68 public function get_no_reply() {
69 return apply_filters( 'bbp_private_replies_no_reply_address', bbp_get_do_not_reply_address() );
70 }
71
72 /**
73 * Outputs the "Set as private reply" checkbox
74 *
75 * @since 1.0
76 *
77 * @return void
78 */
79 public function checkbox() {
80
81 ?>
82 <p>
83
84 <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(); ?>" />
85
86 <?php if ( bbp_is_reply_edit() && ( get_the_author_meta( 'ID' ) != bbp_get_current_user_id() ) ) : ?>
87
88 <label for="bbp_private_reply"><?php esc_html_e( "Set author's post as private.", 'ama-core' ); ?></label>
89
90 <?php else : ?>
91
92 <label for="bbp_private_reply"><?php esc_html_e( 'Set as private reply', 'ama-core' ); ?></label>
93
94 <?php endif; ?>
95
96 </p>
97 <?php
98
99 }
100
101
102 /**
103 * Stores the private state on reply creation and edit
104 *
105 * @since 1.0
106 *
107 * @param $reply_id int The ID of the reply
108 * @param $topic_id int The ID of the topic the reply belongs to
109 * @param $forum_id int The ID of the forum the topic belongs to
110 * @param $anonymous_data bool Are we posting as an anonymous user?
111 * @param $author_id int The ID of user creating the reply, or the ID of the replie's author during edit
112 * @param $is_edit bool Are we editing a reply?
113 *
114 * @return void
115 */
116 public function update_reply( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) {
117
118 if ( isset( $_POST['bbp_private_reply'] ) ) {
119 update_post_meta( $reply_id, '_bbp_reply_is_private', '1' );
120 } else {
121 delete_post_meta( $reply_id, '_bbp_reply_is_private' );
122 }
123
124 }
125
126
127 /**
128 * Determines if a reply is marked as private
129 *
130 * @since 1.0
131 *
132 * @param $reply_id int The ID of the reply
133 *
134 * @return bool
135 */
136 public function is_private( $reply_id = 0 ) {
137
138 $retval = false;
139
140 // Checking a specific reply id
141 if ( ! empty( $reply_id ) ) {
142 $reply = bbp_get_reply( $reply_id );
143 $reply_id = ! empty( $reply ) ? $reply->ID : 0;
144
145 // Using the global reply id
146 } elseif ( bbp_get_reply_id() ) {
147 $reply_id = bbp_get_reply_id();
148
149 // Use the current post id
150 } elseif ( ! bbp_get_reply_id() ) {
151 $reply_id = get_the_ID();
152 }
153
154 if ( ! empty( $reply_id ) ) {
155 $retval = get_post_meta( $reply_id, '_bbp_reply_is_private', true );
156 }
157
158 return (bool) apply_filters( 'bbp_reply_is_private', (bool) $retval, $reply_id );
159 }
160
161
162 /**
163 * Hides the reply content for users that do not have permission to view it
164 *
165 * @since 1.0
166 *
167 * @param $content string The content of the reply
168 * @param $reply_id int The ID of the reply
169 *
170 * @return string
171 */
172 public function hide_reply( $content = '', $reply_id = 0 ) {
173
174 if ( empty( $reply_id ) ) {
175 $reply_id = bbp_get_reply_id( $reply_id );
176 }
177
178 if ( $this->is_private( $reply_id ) ) {
179
180 $can_view = false;
181 $current_user = is_user_logged_in() ? wp_get_current_user() : false;
182 $topic_author = bbp_get_topic_author_id();
183 $reply_author = bbp_get_reply_author_id( $reply_id );
184
185 if ( ! empty( $current_user ) && $topic_author === $current_user->ID && user_can( $reply_author, $this->capability ) ) {
186 // Let the thread author view replies if the reply author is from a moderator
187 $can_view = true;
188 }
189
190 if ( ! empty( $current_user ) && $reply_author === $current_user->ID ) {
191 // Let the reply author view their own reply
192 $can_view = true;
193 }
194
195 if ( current_user_can( $this->capability ) ) {
196 // Let moderators view all replies
197 $can_view = true;
198 }
199
200 if ( ! $can_view ) {
201 $content = '<div class="alert alert-danger">' . esc_html__( 'This reply has been marked as private.', 'ama-core' ) . '</div>';
202 } else {
203 $content = '<div class="alert alert-info">' . esc_html__( 'This is a private reply.', 'ama-core' ) . '</div>' . $content;
204 }
205 }
206
207 return $content;
208 }
209
210
211 /**
212 * Prevents a New Reply notification from being sent if the user doesn't have permission to view it
213 *
214 * @since 1.0
215 *
216 * @param $message string The email message
217 * @param $reply_id int The ID of the reply
218 * @param $topic_id int The ID of the reply's topic
219 *
220 * @return mixed
221 */
222 public function prevent_subscription_email( $message, $reply_id, $topic_id ) {
223
224 if ( $this->is_private( $reply_id ) ) {
225 $this->subscription_email( $message, $reply_id, $topic_id );
226 return false;
227 }
228
229 return $message; // message unchanged
230 }
231
232
233 /**
234 * Sends the new reply notification email to moderators on private replies
235 *
236 * @since 1.2
237 *
238 * @param $message string The email message
239 * @param $reply_id int The ID of the reply
240 * @param $topic_id int The ID of the reply's topic
241 *
242 * @return void
243 */
244 public function subscription_email( $message, $reply_id, $topic_id ) {
245
246 if ( ! $this->is_private( $reply_id ) ) {
247
248 return false; // reply isn't private so do nothing
249
250 }
251
252 $topic_author = bbp_get_topic_author_id( $topic_id );
253 $reply_author = bbp_get_reply_author_id( $reply_id );
254 $reply_author_name = bbp_get_reply_author_display_name( $reply_id );
255
256 // Strip tags from text and setup mail data
257 $topic_title = strip_tags( bbp_get_topic_title( $topic_id ) );
258 $reply_content = strip_tags( bbp_get_reply_content( $reply_id ) );
259 $reply_url = bbp_get_reply_url( $reply_id );
260 $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
261
262 $subject = apply_filters( 'bbp_subscription_mail_title', '[' . $blog_name . '] ' . $topic_title, $reply_id, $topic_id );
263
264 // Array to hold BCC's
265 $headers = [];
266
267 // Setup the From header
268 $headers[] = 'From: ' . get_bloginfo( 'name' ) . ' <' . $this->get_no_reply() . '>';
269
270 // Get topic subscribers and bail if empty
271 $user_ids = bbp_get_topic_subscribers( $topic_id, true );
272 if ( empty( $user_ids ) ) {
273 return false;
274 }
275
276 // Loop through users
277 foreach ( (array) $user_ids as $user_id ) {
278
279 // Don't send notifications to the person who made the post
280 if ( ! empty( $reply_author ) && (int) $user_id === (int) $reply_author ) {
281 continue;
282 }
283
284 $should_notify_op = user_can( $reply_author, $this->capability ) && (int) $topic_author === (int) $user_id;
285
286 if ( user_can( $user_id, $this->capability ) || $should_notify_op ) {
287
288 // Get email address of subscribed user
289 $headers[] = 'Bcc: ' . get_userdata( $user_id )->user_email;
290
291 }
292 }
293
294 wp_mail( $this->get_no_reply(), $subject, $message, $headers );
295 }
296
297
298 /**
299 * Adds a new class to replies that are marked as private
300 *
301 * @since 1.0
302 *
303 * @param $classes array An array of current class names
304 *
305 * @return bool
306 */
307 public function reply_post_class( $classes ) {
308 $reply_id = bbp_get_reply_id();
309
310 // only apply the class to replies
311 if ( bbp_get_reply_post_type() != get_post_type( $reply_id ) ) {
312 return $classes;
313 }
314
315 if ( $this->is_private( $reply_id ) ) {
316 $classes[] = 'bbp-private-reply';
317 }
318
319 return $classes;
320 }
321
322 } // end class
323
324 // instantiate our plugin's class.
325 $GLOBALS['bbp_private_replies'] = new BBP_Private_Replies();
326 ?>
327