PluginProbe
bbPress / 2.0.2
bbPress v2.0.2
trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 2.2.2 All 71 releases
bbpress / bbp-includes / bbp-reply-functions.php

bbp-reply-functions.php in bbPress 2.0.2, at bbp-includes/bbp-reply-functions.php

1,473 lines 49.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Reply Functions
5 *
6 * @package bbPress
7 * @subpackage Functions
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /** Insert ********************************************************************/
14
15 /**
16 * A wrapper for wp_insert_post() that also includes the necessary meta values
17 * for the reply to function properly.
18 *
19 * @since bbPress (r3349)
20 *
21 * @uses wp_parse_args()
22 * @uses bbp_get_reply_post_type()
23 * @uses wp_insert_post()
24 * @uses update_post_meta()
25 *
26 * @param array $reply_data Forum post data
27 * @param arrap $reply_meta Forum meta data
28 */
29 function bbp_insert_reply( $reply_data = array(), $reply_meta = array() ) {
30
31 // Forum
32 $default_reply = array(
33 'post_parent' => 0, // topic ID
34 'post_status' => bbp_get_public_status_id(),
35 'post_type' => bbp_get_reply_post_type(),
36 'post_author' => 0,
37 'post_password' => '',
38 'post_content' => '',
39 'post_title' => '',
40 'menu_order' => 0,
41 );
42
43 // Parse args
44 $reply_data = wp_parse_args( $reply_data, $default_reply );
45
46 // Insert reply
47 $reply_id = wp_insert_post( $reply_data );
48
49 // Bail if no reply was added
50 if ( empty( $reply_id ) )
51 return false;
52
53 // Forum meta
54 $default_meta = array(
55 'author_ip' => bbp_current_author_ip(),
56 'forum_id' => 0,
57 'topic_id' => 0,
58 );
59
60 // Parse args
61 $reply_meta = wp_parse_args( $reply_meta, $default_meta );
62
63 // Insert reply meta
64 foreach ( $reply_meta as $meta_key => $meta_value )
65 update_post_meta( $reply_id, '_bbp_' . $meta_key, $meta_value );
66
67 // Update the topic
68 $topic_id = bbp_get_reply_topic_id( $reply_id );
69 if ( !empty( $topic_id ) )
70 bbp_update_topic( $topic_id );
71
72 // Return new reply ID
73 return $reply_id;
74 }
75
76 /** Post Form Handlers ********************************************************/
77
78 /**
79 * Handles the front end reply submission
80 *
81 * @since bbPress (r2574)
82 *
83 * @uses bbp_add_error() To add an error message
84 * @uses check_admin_referer() To verify the nonce and check the referer
85 * @uses bbp_is_anonymous() To check if an anonymous post is being made
86 * @uses current_user_can() To check if the current user can publish replies
87 * @uses bbp_get_current_user_id() To get the current user id
88 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
89 * @uses bbp_set_current_anonymous_user_data() To set the anonymous user
90 * cookies
91 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
92 * @uses remove_filter() To remove 'wp_filter_kses' filters if needed
93 * @uses esc_attr() For sanitization
94 * @uses bbp_check_for_flood() To check for flooding
95 * @uses bbp_check_for_duplicate() To check for duplicates
96 * @uses apply_filters() Calls 'bbp_new_reply_pre_title' with the title
97 * @uses apply_filters() Calls 'bbp_new_reply_pre_content' with the content
98 * @uses bbp_get_reply_post_type() To get the reply post type
99 * @uses wp_set_post_terms() To set the topic tags
100 * @uses wp_insert_post() To insert the reply
101 * @uses do_action() Calls 'bbp_new_reply' with the reply id, topic id, forum
102 * id, anonymous data and reply author
103 * @uses bbp_get_reply_url() To get the paginated url to the reply
104 * @uses wp_safe_redirect() To redirect to the reply url
105 * @uses bbPress::errors::get_error_message() To get the {@link WP_Error} error
106 * message
107 */
108 function bbp_new_reply_handler() {
109
110 // Bail if not a POST action
111 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
112 return;
113
114 // Bail if action is not bbp-new-reply
115 if ( empty( $_POST['action'] ) || ( 'bbp-new-reply' !== $_POST['action'] ) )
116 return;
117
118 // Nonce check
119 check_admin_referer( 'bbp-new-reply' );
120
121 // Define local variable(s)
122 $topic_id = $forum_id = $reply_author = $anonymous_data = 0;
123 $reply_title = $reply_content = $terms = '';
124
125 /** Reply Author **********************************************************/
126
127 // User is anonymous
128 if ( bbp_is_anonymous() ) {
129
130 // Filter anonymous data
131 $anonymous_data = bbp_filter_anonymous_post_data();
132
133 // Anonymous data checks out, so set cookies, etc...
134 if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
135 bbp_set_current_anonymous_user_data( $anonymous_data );
136 }
137
138 // User is logged in
139 } else {
140
141 // User cannot create replies
142 if ( !current_user_can( 'publish_replies' ) ) {
143 bbp_add_error( 'bbp_reply_permissions', __( '<strong>ERROR</strong>: You do not have permission to reply.', 'bbpress' ) );
144 }
145
146 // Reply author is current user
147 $reply_author = bbp_get_current_user_id();
148
149 }
150
151 /** Topic ID **************************************************************/
152
153 // Handle Topic ID to append reply to
154 if ( isset( $_POST['bbp_topic_id'] ) && ( !$topic_id = (int) $_POST['bbp_topic_id'] ) )
155 bbp_add_error( 'bbp_reply_topic_id', __( '<strong>ERROR</strong>: Topic ID is missing.', 'bbpress' ) );
156
157 /** Forum ID **************************************************************/
158
159 // Handle Forum ID to adjust counts of
160 if ( isset( $_POST['bbp_forum_id'] ) && ( !$forum_id = (int) $_POST['bbp_forum_id'] ) )
161 bbp_add_error( 'bbp_reply_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
162
163 /** Unfiltered HTML *******************************************************/
164
165 // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
166 if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $topic_id ) == $_POST['_bbp_unfiltered_html_reply'] ) {
167 remove_filter( 'bbp_new_reply_pre_title', 'wp_filter_kses' );
168 remove_filter( 'bbp_new_reply_pre_content', 'wp_filter_kses' );
169 }
170
171 /** Reply Title ***********************************************************/
172
173 if ( !empty( $_POST['bbp_reply_title'] ) )
174 $reply_title = esc_attr( strip_tags( $_POST['bbp_reply_title'] ) );
175
176 // Filter and sanitize
177 $reply_title = apply_filters( 'bbp_new_reply_pre_title', $reply_title );
178
179 // No reply title
180 if ( empty( $reply_title ) )
181 bbp_add_error( 'bbp_reply_title', __( '<strong>ERROR</strong>: Your reply needs a title.', 'bbpress' ) );
182
183 /** Reply Content *********************************************************/
184
185 if ( !empty( $_POST['bbp_reply_content'] ) )
186 $reply_content = $_POST['bbp_reply_content'];
187
188 // Filter and sanitize
189 $reply_content = apply_filters( 'bbp_new_reply_pre_content', $reply_content );
190
191 // No reply content
192 if ( empty( $reply_content ) )
193 bbp_add_error( 'bbp_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress' ) );
194
195 /** Reply Flooding ********************************************************/
196
197 if ( !bbp_check_for_flood( $anonymous_data, $reply_author ) )
198 bbp_add_error( 'bbp_reply_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
199
200 /** Reply Duplicate *******************************************************/
201
202 if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_reply_post_type(), 'post_author' => $reply_author, 'post_content' => $reply_content, 'post_parent' => $topic_id, 'anonymous_data' => $anonymous_data ) ) )
203 bbp_add_error( 'bbp_reply_duplicate', __( '<strong>ERROR</strong>: Duplicate reply detected; it looks as though you&#8217;ve already said that!', 'bbpress' ) );
204
205 /** Reply Blacklist *******************************************************/
206
207 if ( !bbp_check_for_blacklist( $anonymous_data, $reply_author, $reply_title, $reply_content ) )
208 bbp_add_error( 'bbp_reply_blacklist', __( '<strong>ERROR</strong>: Your reply cannot be created at this time.', 'bbpress' ) );
209
210 /** Topic Tags ************************************************************/
211
212 if ( !empty( $_POST['bbp_topic_tags'] ) )
213 $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
214
215 /** Additional Actions (Before Save) **************************************/
216
217 do_action( 'bbp_new_reply_pre_extras' );
218
219 /** No Errors *************************************************************/
220
221 // Handle insertion into posts table
222 if ( !bbp_has_errors() ) {
223
224 /** Create new reply **************************************************/
225
226 // Add the content of the form to $post as an array
227 $reply_data = array(
228 'post_author' => $reply_author,
229 'post_title' => $reply_title,
230 'post_content' => $reply_content,
231 'post_parent' => $topic_id,
232 'post_status' => bbp_get_public_status_id(),
233 'post_type' => bbp_get_reply_post_type()
234 );
235
236 // Just in time manipulation of reply data before being created
237 $reply_data = apply_filters( 'bbp_new_reply_pre_insert', $reply_data );
238
239 // Insert reply
240 $reply_id = wp_insert_post( $reply_data );
241
242 /** No Errors *********************************************************/
243
244 // Check for missing reply_id or error
245 if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) {
246
247 /** Topic Tags ****************************************************/
248
249 // Just in time manipulation of reply terms before being edited
250 $terms = apply_filters( 'bbp_new_reply_pre_set_terms', $terms, $topic_id, $reply_id );
251
252 // Insert terms
253 $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false );
254
255 // Term error
256 if ( is_wp_error( $terms ) ) {
257 bbp_add_error( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was a problem adding the tags to the topic.', 'bbpress' ) );
258 }
259
260 /** Trash Check ***************************************************/
261
262 // If this reply starts as trash, add it to pre_trashed_replies
263 // for the topic, so it is properly restored.
264 if ( bbp_is_topic_trash( $topic_id ) || ( $reply_data['post_status'] == bbp_get_trash_status_id() ) ) {
265
266 // Trash the reply
267 wp_trash_post( $reply_id );
268
269 // Get pre_trashed_replies for topic
270 $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
271
272 // Add this reply to the end of the existing replies
273 $pre_trashed_replies[] = $reply_id;
274
275 // Update the pre_trashed_reply post meta
276 update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
277 }
278
279 /** Spam Check ****************************************************/
280
281 // If reply or topic are spam, officially spam this reply
282 if ( bbp_is_topic_spam( $topic_id ) || ( $reply_data['post_status'] == bbp_get_spam_status_id() ) )
283 add_post_meta( $reply_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
284
285 /** Update counts, etc... *****************************************/
286
287 do_action( 'bbp_new_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply_author );
288
289 /** Redirect ******************************************************/
290
291 // Redirect to
292 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
293
294 // Get the reply URL
295 $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
296
297 // Allow to be filtered
298 $reply_url = apply_filters( 'bbp_new_reply_redirect_to', $reply_url, $redirect_to );
299
300 /** Successful Save ***********************************************/
301
302 // Redirect back to new reply
303 wp_safe_redirect( $reply_url );
304
305 // For good measure
306 exit();
307
308 /** Errors ************************************************************/
309
310 } else {
311 $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : '';
312 bbp_add_error( 'bbp_reply_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress' ) );
313 }
314 }
315 }
316
317 /**
318 * Handles the front end edit reply submission
319 *
320 * @uses bbp_add_error() To add an error message
321 * @uses bbp_get_reply() To get the reply
322 * @uses check_admin_referer() To verify the nonce and check the referer
323 * @uses bbp_is_reply_anonymous() To check if the reply was by an anonymous user
324 * @uses current_user_can() To check if the current user can edit that reply
325 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
326 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
327 * @uses remove_filter() To remove 'wp_filter_kses' filters if needed
328 * @uses esc_attr() For sanitization
329 * @uses apply_filters() Calls 'bbp_edit_reply_pre_title' with the title and
330 * reply id
331 * @uses apply_filters() Calls 'bbp_edit_reply_pre_content' with the content
332 * reply id
333 * @uses wp_set_post_terms() To set the topic tags
334 * @uses bbp_has_errors() To get the {@link WP_Error} errors
335 * @uses wp_save_post_revision() To save a reply revision
336 * @uses bbp_update_topic_revision_log() To update the reply revision log
337 * @uses wp_update_post() To update the reply
338 * @uses bbp_get_reply_topic_id() To get the reply topic id
339 * @uses bbp_get_topic_forum_id() To get the topic forum id
340 * @uses do_action() Calls 'bbp_edit_reply' with the reply id, topic id, forum
341 * id, anonymous data, reply author and bool true (for edit)
342 * @uses bbp_get_reply_url() To get the paginated url to the reply
343 * @uses wp_safe_redirect() To redirect to the reply url
344 * @uses bbPress::errors::get_error_message() To get the {@link WP_Error} error
345 * message
346 */
347 function bbp_edit_reply_handler() {
348
349 // Bail if not a POST action
350 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
351 return;
352
353 // Bail if action is not bbp-edit-reply
354 if ( empty( $_POST['action'] ) || ( 'bbp-edit-reply' !== $_POST['action'] ) )
355 return;
356
357 // Define local variable(s)
358 $reply = $reply_id = $topic_id = $forum_id = $anonymous_data = 0;
359 $reply_title = $reply_content = $reply_edit_reason = $terms = '';
360
361 /** Reply *****************************************************************/
362
363 // Reply id was not passed
364 if ( empty( $_POST['bbp_reply_id'] ) ) {
365 bbp_add_error( 'bbp_edit_reply_id', __( '<strong>ERROR</strong>: Reply ID not found.', 'bbpress' ) );
366
367 // Reply id was passed
368 } elseif ( is_numeric( $_POST['bbp_reply_id'] ) ) {
369 $reply_id = (int) $_POST['bbp_reply_id'];
370 $reply = bbp_get_reply( $reply_id );
371 }
372
373 // Reply does not exist
374 if ( empty( $reply ) ) {
375 bbp_add_error( 'bbp_edit_reply_not_found', __( '<strong>ERROR</strong>: The reply you want to edit was not found.', 'bbpress' ) );
376
377 // Reply exists
378 } else {
379
380 // Nonce check
381 check_admin_referer( 'bbp-edit-reply_' . $reply_id );
382
383 // Check users ability to create new reply
384 if ( !bbp_is_reply_anonymous( $reply_id ) ) {
385
386 // User cannot edit this reply
387 if ( !current_user_can( 'edit_reply', $reply_id ) ) {
388 bbp_add_error( 'bbp_edit_reply_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that reply.', 'bbpress' ) );
389 }
390
391 // It is an anonymous post
392 } else {
393
394 // Filter anonymous data
395 $anonymous_data = bbp_filter_anonymous_post_data( array(), true );
396 }
397 }
398
399 // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
400 if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_reply'] ) && wp_create_nonce( 'bbp-unfiltered-html-reply_' . $reply_id ) == $_POST['_bbp_unfiltered_html_reply'] ) {
401 remove_filter( 'bbp_edit_reply_pre_title', 'wp_filter_kses' );
402 remove_filter( 'bbp_edit_reply_pre_content', 'wp_filter_kses' );
403 }
404
405 /** Reply Topic ***********************************************************/
406
407 $topic_id = bbp_get_reply_topic_id( $reply_id );
408
409 /** Topic Forum ***********************************************************/
410
411 $forum_id = bbp_get_topic_forum_id( $topic_id );
412
413 // Forum exists
414 if ( !empty( $forum_id ) && ( $forum_id !== bbp_get_reply_forum_id( $reply_id ) ) ) {
415
416 // Forum is a category
417 if ( bbp_is_forum_category( $forum_id ) )
418 bbp_add_error( 'bbp_edit_reply_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics or replies can be created in it.', 'bbpress' ) );
419
420 // Forum is closed and user cannot access
421 if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
422 bbp_add_error( 'bbp_edit_reply_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics and replies.', 'bbpress' ) );
423
424 // Forum is private and user cannot access
425 if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
426 bbp_add_error( 'bbp_edit_reply_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
427
428 // Forum is hidden and user cannot access
429 if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
430 bbp_add_error( 'bbp_edit_reply_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new replies in it.', 'bbpress' ) );
431 }
432
433 /** Reply Title ***********************************************************/
434
435 if ( !empty( $_POST['bbp_reply_title'] ) )
436 $reply_title = esc_attr( strip_tags( $_POST['bbp_reply_title'] ) );
437
438 // Filter and sanitize
439 $reply_title = apply_filters( 'bbp_edit_reply_pre_title', $reply_title, $reply_id );
440
441 /** Reply Content *********************************************************/
442
443 if ( !empty( $_POST['bbp_reply_content'] ) )
444 $reply_content = $_POST['bbp_reply_content'];
445
446 // Filter and sanitize
447 $reply_content = apply_filters( 'bbp_edit_reply_pre_content', $reply_content, $reply_id );
448
449 // No reply content
450 if ( empty( $reply_content ) )
451 bbp_add_error( 'bbp_edit_reply_content', __( '<strong>ERROR</strong>: Your reply cannot be empty.', 'bbpress' ) );
452
453 /** Reply Blacklist *******************************************************/
454
455 if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_reply_author_id( $reply_id ), $reply_title, $reply_content ) )
456 bbp_add_error( 'bbp_reply_blacklist', __( '<strong>ERROR</strong>: Your reply cannot be edited at this time.', 'bbpress' ) );
457
458 /** Topic Tags ************************************************************/
459
460 if ( !empty( $_POST['bbp_topic_tags'] ) )
461 $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
462
463 /** Additional Actions (Before Save) **************************************/
464
465 do_action( 'bbp_edit_reply_pre_extras', $reply_id );
466
467 /** No Errors *************************************************************/
468
469 // Handle insertion into posts table
470 if ( !bbp_has_errors() ) {
471
472 // Add the content of the form to $post as an array
473 $reply_data = array(
474 'ID' => $reply_id,
475 'post_title' => $reply_title,
476 'post_content' => $reply_content
477 );
478
479 // Just in time manipulation of reply data before being edited
480 $reply_data = apply_filters( 'bbp_edit_reply_pre_insert', $reply_data );
481
482 // Insert reply
483 $reply_id = wp_update_post( $reply_data );
484
485 /** Topic Tags ****************************************************/
486
487 // Just in time manipulation of reply terms before being edited
488 $terms = apply_filters( 'bbp_edit_reply_pre_set_terms', $terms, $topic_id, $reply_id );
489
490 // Insert terms
491 $terms = wp_set_post_terms( $topic_id, $terms, bbp_get_topic_tag_tax_id(), false );
492
493 // Term error
494 if ( is_wp_error( $terms ) ) {
495 bbp_add_error( 'bbp_reply_tags', __( '<strong>ERROR</strong>: There was a problem adding the tags to the topic.', 'bbpress' ) );
496 }
497
498 /** Revisions *********************************************************/
499
500 // Revision Reason
501 if ( !empty( $_POST['bbp_reply_edit_reason'] ) )
502 $reply_edit_reason = esc_attr( strip_tags( $_POST['bbp_reply_edit_reason'] ) );
503
504 // Update revision log
505 if ( !empty( $_POST['bbp_log_reply_edit'] ) && ( 1 == $_POST['bbp_log_reply_edit'] ) && ( $revision_id = wp_save_post_revision( $reply_id ) ) ) {
506 bbp_update_reply_revision_log( array(
507 'reply_id' => $reply_id,
508 'revision_id' => $revision_id,
509 'author_id' => bbp_get_current_user_id(),
510 'reason' => $reply_edit_reason
511 ) );
512 }
513
514 /** No Errors *********************************************************/
515
516 if ( !empty( $reply_id ) && !is_wp_error( $reply_id ) ) {
517
518 // Update counts, etc...
519 do_action( 'bbp_edit_reply', $reply_id, $topic_id, $forum_id, $anonymous_data, $reply->post_author , true /* Is edit */ );
520
521 /** Additional Actions (After Save) *******************************/
522
523 do_action( 'bbp_edit_reply_post_extras', $reply_id );
524
525 /** Redirect ******************************************************/
526
527 // Redirect to
528 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
529
530 // Get the reply URL
531 $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
532
533 // Allow to be filtered
534 $reply_url = apply_filters( 'bbp_edit_reply_redirect_to', $reply_url, $redirect_to );
535
536 /** Successful Edit ***********************************************/
537
538 // Redirect back to new reply
539 wp_safe_redirect( $reply_url );
540
541 // For good measure
542 exit();
543
544 /** Errors ************************************************************/
545
546 } else {
547 $append_error = ( is_wp_error( $reply_id ) && $reply_id->get_error_message() ) ? $reply_id->get_error_message() . ' ' : '';
548 bbp_add_error( 'bbp_reply_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your reply:' . $append_error . 'Please try again.', 'bbpress' ) );
549 }
550 }
551 }
552
553 /**
554 * Handle all the extra meta stuff from posting a new reply or editing a reply
555 *
556 * @param int $reply_id Optional. Reply id
557 * @param int $topic_id Optional. Topic id
558 * @param int $forum_id Optional. Forum id
559 * @param bool|array $anonymous_data Optional. If it is an array, it is
560 * extracted and anonymous user info is saved
561 * @param int $author_id Author id
562 * @param bool $is_edit Optional. Is the post being edited? Defaults to false.
563 * @uses bbp_get_reply_id() To get the reply id
564 * @uses bbp_get_topic_id() To get the topic id
565 * @uses bbp_get_forum_id() To get the forum id
566 * @uses bbp_get_current_user_id() To get the current user id
567 * @uses bbp_get_reply_topic_id() To get the reply topic id
568 * @uses bbp_get_topic_forum_id() To get the topic forum id
569 * @uses update_post_meta() To update the reply metas
570 * @uses set_transient() To update the flood check transient for the ip
571 * @uses update_user_meta() To update the last posted meta for the user
572 * @uses bbp_is_subscriptions_active() To check if the subscriptions feature is
573 * activated or not
574 * @uses bbp_is_user_subscribed() To check if the user is subscribed
575 * @uses bbp_remove_user_subscription() To remove the user's subscription
576 * @uses bbp_add_user_subscription() To add the user's subscription
577 * @uses bbp_update_reply_forum_id() To update the reply forum id
578 * @uses bbp_update_reply_topic_id() To update the reply topic id
579 * @uses bbp_update_reply_walker() To update the reply's ancestors' counts
580 */
581 function bbp_update_reply( $reply_id = 0, $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) {
582
583 // Validate the ID's passed from 'bbp_new_reply' action
584 $reply_id = bbp_get_reply_id( $reply_id );
585 $topic_id = bbp_get_topic_id( $topic_id );
586 $forum_id = bbp_get_forum_id( $forum_id );
587
588 // Check author_id
589 if ( empty( $author_id ) )
590 $author_id = bbp_get_current_user_id();
591
592 // Check topic_id
593 if ( empty( $topic_id ) )
594 $topic_id = bbp_get_reply_topic_id( $reply_id );
595
596 // Check forum_id
597 if ( !empty( $topic_id ) && empty( $forum_id ) )
598 $forum_id = bbp_get_topic_forum_id( $topic_id );
599
600 // If anonymous post, store name, email, website and ip in post_meta.
601 // It expects anonymous_data to be sanitized.
602 // Check bbp_filter_anonymous_post_data() for sanitization.
603 if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
604 extract( $anonymous_data );
605
606 update_post_meta( $reply_id, '_bbp_anonymous_name', $bbp_anonymous_name, false );
607 update_post_meta( $reply_id, '_bbp_anonymous_email', $bbp_anonymous_email, false );
608
609 // Set transient for throttle check (only on new, not edit)
610 if ( empty( $is_edit ) ) {
611 set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time() );
612 }
613
614 // Website is optional
615 if ( !empty( $bbp_anonymous_website ) ) {
616 update_post_meta( $reply_id, '_bbp_anonymous_website', $bbp_anonymous_website, false );
617 }
618
619 } else {
620 if ( empty( $is_edit ) && !current_user_can( 'throttle' ) ) {
621 update_user_meta( $author_id, '_bbp_last_posted', time() );
622 }
623 }
624
625 // Handle Subscription Checkbox
626 if ( bbp_is_subscriptions_active() && !empty( $author_id ) ) {
627 $subscribed = bbp_is_user_subscribed( $author_id, $topic_id );
628 $subscheck = ( !empty( $_POST['bbp_topic_subscription'] ) && ( 'bbp_subscribe' == $_POST['bbp_topic_subscription'] ) ) ? true : false;
629
630 // Subscribed and unsubscribing
631 if ( true == $subscribed && false == $subscheck ) {
632 bbp_remove_user_subscription( $author_id, $topic_id );
633
634 // Subscribing
635 } elseif ( false == $subscribed && true == $subscheck ) {
636 bbp_add_user_subscription( $author_id, $topic_id );
637 }
638 }
639
640 // Reply meta relating to reply position in tree
641 bbp_update_reply_forum_id( $reply_id, $forum_id );
642 bbp_update_reply_topic_id( $reply_id, $topic_id );
643
644 // Update associated topic values if this is a new reply
645 if ( empty( $is_edit ) ) {
646
647 // Update poster IP if not editing
648 update_post_meta( $reply_id, '_bbp_author_ip', bbp_current_author_ip(), false );
649
650 // Last active time
651 $last_active_time = current_time( 'mysql' );
652
653 // Walk up ancestors and do the dirty work
654 bbp_update_reply_walker( $reply_id, $last_active_time, $forum_id, $topic_id, false );
655 }
656 }
657
658 /**
659 * Walk up the ancestor tree from the current reply, and update all the counts
660 *
661 * @since bbPress (r2884)
662 *
663 * @param int $reply_id Optional. Reply id
664 * @param string $last_active_time Optional. Last active time
665 * @param int $forum_id Optional. Forum id
666 * @param int $topic_id Optional. Topic id
667 * @param bool $refresh If set to true, unsets all the previous parameters.
668 * Defaults to true
669 * @uses bbp_get_reply_id() To get the reply id
670 * @uses bbp_get_reply_topic_id() To get the reply topic id
671 * @uses bbp_get_reply_forum_id() To get the reply forum id
672 * @uses get_post_ancestors() To get the ancestors of the reply
673 * @uses bbp_is_reply() To check if the ancestor is a reply
674 * @uses bbp_is_topic() To check if the ancestor is a topic
675 * @uses bbp_update_topic_last_reply_id() To update the topic last reply id
676 * @uses bbp_update_topic_last_active_id() To update the topic last active id
677 * @uses bbp_get_topic_last_active_id() To get the topic last active id
678 * @uses get_post_field() To get the post date of the last active id
679 * @uses bbp_update_topic_last_active_time() To update the last active topic meta
680 * @uses bbp_update_topic_voice_count() To update the topic voice count
681 * @uses bbp_update_topic_reply_count() To update the topic reply count
682 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
683 * count
684 * @uses bbp_is_forum() To check if the ancestor is a forum
685 * @uses bbp_update_forum_last_topic_id() To update the last topic id forum meta
686 * @uses bbp_update_forum_last_reply_id() To update the last reply id forum meta
687 * @uses bbp_update_forum_last_active_id() To update the forum last active id
688 * @uses bbp_get_forum_last_active_id() To get the forum last active id
689 * @uses bbp_update_forum_last_active_time() To update the forum last active time
690 * @uses bbp_update_forum_reply_count() To update the forum reply count
691 */
692 function bbp_update_reply_walker( $reply_id, $last_active_time = '', $forum_id = 0, $topic_id = 0, $refresh = true ) {
693
694 // Verify the reply ID
695 $reply_id = bbp_get_reply_id( $reply_id );
696
697 // Reply was passed
698 if ( !empty( $reply_id ) ) {
699
700 // Get the topic ID if none was passed
701 if ( empty( $topic_id ) ) {
702 $topic_id = bbp_get_reply_topic_id( $reply_id );
703 }
704
705 // Get the forum ID if none was passed
706 if ( empty( $forum_id ) ) {
707 $forum_id = bbp_get_reply_forum_id( $reply_id );
708 }
709 }
710
711 // Set the active_id based on topic_id/reply_id
712 $active_id = empty( $reply_id ) ? $topic_id : $reply_id;
713
714 // Setup ancestors array to walk up
715 $ancestors = array_values( array_unique( array_merge( array( $topic_id, $forum_id ), get_post_ancestors( $topic_id ) ) ) );
716
717 // If we want a full refresh, unset any of the possibly passed variables
718 if ( true == $refresh )
719 $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
720
721 // Walk up ancestors
722 foreach ( $ancestors as $ancestor ) {
723
724 // Reply meta relating to most recent reply
725 if ( bbp_is_reply( $ancestor ) ) {
726 // @todo - hierarchical replies
727
728 // Topic meta relating to most recent reply
729 } elseif ( bbp_is_topic( $ancestor ) ) {
730
731 // Last reply and active ID's
732 bbp_update_topic_last_reply_id ( $ancestor, $reply_id );
733 bbp_update_topic_last_active_id( $ancestor, $active_id );
734
735 // Get the last active time if none was passed
736 $topic_last_active_time = $last_active_time;
737 if ( empty( $last_active_time ) ) {
738 $topic_last_active_time = get_post_field( 'post_date', bbp_get_topic_last_active_id( $ancestor ) );
739 }
740
741 // Only update if reply is published
742 if ( bbp_is_reply_published( $reply_id ) ) {
743 bbp_update_topic_last_active_time( $ancestor, $topic_last_active_time );
744 }
745
746 // Counts
747 bbp_update_topic_voice_count ( $ancestor );
748 bbp_update_topic_reply_count ( $ancestor );
749 bbp_update_topic_reply_count_hidden( $ancestor );
750
751 // Forum meta relating to most recent topic
752 } elseif ( bbp_is_forum( $ancestor ) ) {
753
754 // Last topic and reply ID's
755 bbp_update_forum_last_topic_id( $ancestor, $topic_id );
756 bbp_update_forum_last_reply_id( $ancestor, $reply_id );
757
758 // Last Active
759 bbp_update_forum_last_active_id( $ancestor, $active_id );
760
761 // Get the last active time if none was passed
762 $forum_last_active_time = $last_active_time;
763 if ( empty( $last_active_time ) ) {
764 $forum_last_active_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $ancestor ) );
765 }
766
767 // Only update if reply is published
768 if ( bbp_is_reply_published( $reply_id ) ) {
769 bbp_update_forum_last_active_time( $ancestor, $forum_last_active_time );
770 }
771
772 // Counts
773 bbp_update_forum_reply_count( $ancestor );
774 }
775 }
776 }
777
778 /** Reply Updaters ************************************************************/
779
780 /**
781 * Update the reply with its forum id it is in
782 *
783 * @since bbPress (r2855)
784 *
785 * @param int $reply_id Optional. Reply id to update
786 * @param int $forum_id Optional. Forum id
787 * @uses bbp_get_reply_id() To get the reply id
788 * @uses bbp_get_forum_id() To get the forum id
789 * @uses get_post_ancestors() To get the reply's forum
790 * @uses get_post_field() To get the post type of the post
791 * @uses update_post_meta() To update the reply forum id meta
792 * @uses apply_filters() Calls 'bbp_update_reply_forum_id' with the forum id
793 * and reply id
794 * @return bool Reply's forum id
795 */
796 function bbp_update_reply_forum_id( $reply_id = 0, $forum_id = 0 ) {
797
798 // Validation
799 $reply_id = bbp_get_reply_id( $reply_id );
800 $forum_id = bbp_get_forum_id( $forum_id );
801
802 // If no forum_id was passed, walk up ancestors and look for forum type
803 if ( empty( $forum_id ) ) {
804
805 // Get ancestors
806 $ancestors = get_post_ancestors( $reply_id );
807
808 // Loop through ancestors
809 foreach ( $ancestors as $ancestor ) {
810
811 // Get first parent that is a forum
812 if ( get_post_field( 'post_type', $ancestor ) == bbp_get_forum_post_type() ) {
813 $forum_id = $ancestor;
814
815 // Found a forum, so exit the loop and continue
816 continue;
817 }
818 }
819 }
820
821 // Update the forum ID
822 bbp_update_forum_id( $reply_id, $forum_id );
823
824 return apply_filters( 'bbp_update_reply_forum_id', (int) $forum_id, $reply_id );
825 }
826
827 /**
828 * Update the reply with its topic id it is in
829 *
830 * @since bbPress (r2855)
831 *
832 * @param int $reply_id Optional. Reply id to update
833 * @param int $topic_id Optional. Topic id
834 * @uses bbp_get_reply_id() To get the reply id
835 * @uses bbp_get_topic_id() To get the topic id
836 * @uses get_post_ancestors() To get the reply's topic
837 * @uses get_post_field() To get the post type of the post
838 * @uses update_post_meta() To update the reply topic id meta
839 * @uses apply_filters() Calls 'bbp_update_reply_topic_id' with the topic id
840 * and reply id
841 * @return bool Reply's topic id
842 */
843 function bbp_update_reply_topic_id( $reply_id = 0, $topic_id = 0 ) {
844
845 // Validation
846 $reply_id = bbp_get_reply_id( $reply_id );
847 $topic_id = bbp_get_topic_id( $topic_id );
848
849 // If no topic_id was passed, walk up ancestors and look for topic type
850 if ( empty( $topic_id ) ) {
851
852 // Get ancestors
853 $ancestors = get_post_ancestors( $reply_id );
854
855 // Loop through ancestors
856 foreach ( $ancestors as $ancestor ) {
857
858 // Get first parent that is a forum
859 if ( get_post_field( 'post_type', $ancestor ) == bbp_get_topic_post_type() ) {
860 $topic_id = $ancestor;
861
862 // Found a forum, so exit the loop and continue
863 continue;
864 }
865 }
866 }
867
868 // Update the topic ID
869 bbp_update_topic_id( $reply_id, $topic_id );
870
871 return apply_filters( 'bbp_update_reply_topic_id', (int) $topic_id, $reply_id );
872 }
873
874 /**
875 * Update the revision log of the reply
876 *
877 * @since bbPress (r2782)
878 *
879 * @param mixed $args Supports these args:
880 * - reply_id: reply id
881 * - author_id: Author id
882 * - reason: Reason for editing
883 * - revision_id: Revision id
884 * @uses bbp_get_reply_id() To get the reply id
885 * @uses bbp_get_user_id() To get the user id
886 * @uses bbp_format_revision_reason() To format the reason
887 * @uses bbp_get_reply_raw_revision_log() To get the raw reply revision log
888 * @uses update_post_meta() To update the reply revision log meta
889 * @return mixed False on failure, true on success
890 */
891 function bbp_update_reply_revision_log( $args = '' ) {
892 $defaults = array (
893 'reason' => '',
894 'reply_id' => 0,
895 'author_id' => 0,
896 'revision_id' => 0
897 );
898
899 $r = wp_parse_args( $args, $defaults );
900 extract( $r );
901
902 // Populate the variables
903 $reason = bbp_format_revision_reason( $reason );
904 $reply_id = bbp_get_reply_id( $reply_id );
905 $author_id = bbp_get_user_id ( $author_id, false, true );
906 $revision_id = (int) $revision_id;
907
908 // Get the logs and append the new one to those
909 $revision_log = bbp_get_reply_raw_revision_log( $reply_id );
910 $revision_log[$revision_id] = array( 'author' => $author_id, 'reason' => $reason );
911
912 // Finally, update
913 update_post_meta( $reply_id, '_bbp_revision_log', $revision_log );
914
915 return apply_filters( 'bbp_update_reply_revision_log', $revision_log, $reply_id );
916 }
917
918 /** Reply Actions *************************************************************/
919
920 /**
921 * Handles the front end spamming/unspamming and trashing/untrashing/deleting of
922 * replies
923 *
924 * @since bbPress (r2740)
925 *
926 * @uses bbp_get_reply() To get the reply
927 * @uses current_user_can() To check if the user is capable of editing or
928 * deleting the reply
929 * @uses check_ajax_referer() To verify the nonce and check the referer
930 * @uses bbp_get_reply_post_type() To get the reply post type
931 * @uses bbp_is_reply_spam() To check if the reply is marked as spam
932 * @uses bbp_spam_reply() To make the reply as spam
933 * @uses bbp_unspam_reply() To unmark the reply as spam
934 * @uses wp_trash_post() To trash the reply
935 * @uses wp_untrash_post() To untrash the reply
936 * @uses wp_delete_post() To delete the reply
937 * @uses do_action() Calls 'bbp_toggle_reply_handler' with success, post data
938 * and action
939 * @uses bbp_get_reply_url() To get the reply url
940 * @uses add_query_arg() To add custom args to the reply url
941 * @uses wp_redirect() To redirect to the reply
942 * @uses bbPress::errors:add() To log the error messages
943 */
944 function bbp_toggle_reply_handler() {
945
946 // Bail if not a GET action
947 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
948 return;
949
950 // Bail if required GET actions aren't passed
951 if ( empty( $_GET['reply_id'] ) || empty( $_GET['action'] ) )
952 return;
953
954 // Setup possible get actions
955 $possible_actions = array(
956 'bbp_toggle_reply_spam',
957 'bbp_toggle_reply_trash'
958 );
959
960 // Bail if actions aren't meant for this function
961 if ( !in_array( $_GET['action'], $possible_actions ) )
962 return;
963
964 $failure = ''; // Empty failure string
965 $view_all = false; // Assume not viewing all
966 $action = $_GET['action']; // What action is taking place?
967 $reply_id = (int) $_GET['reply_id']; // What's the reply id?
968 $success = false; // Flag
969 $post_data = array( 'ID' => $reply_id ); // Prelim array
970
971 // Make sure reply exists
972 $reply = bbp_get_reply( $reply_id );
973 if ( empty( $reply ) )
974 return;
975
976 // What is the user doing here?
977 if ( !current_user_can( 'edit_reply', $reply->ID ) || ( 'bbp_toggle_reply_trash' == $action && !current_user_can( 'delete_reply', $reply->ID ) ) ) {
978 bbp_add_error( 'bbp_toggle_reply_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that!', 'bbpress' ) );
979 return;
980 }
981
982 // What action are we trying to perform?
983 switch ( $action ) {
984
985 // Toggle spam
986 case 'bbp_toggle_reply_spam' :
987 check_ajax_referer( 'spam-reply_' . $reply_id );
988
989 $is_spam = bbp_is_reply_spam( $reply_id );
990 $success = $is_spam ? bbp_unspam_reply( $reply_id ) : bbp_spam_reply( $reply_id );
991 $failure = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the reply as spam!', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the reply as spam!', 'bbpress' );
992 $view_all = !$is_spam;
993
994 break;
995
996 // Toggle trash
997 case 'bbp_toggle_reply_trash' :
998
999 $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
1000
1001 if ( empty( $sub_action ) )
1002 break;
1003
1004 switch ( $sub_action ) {
1005 case 'trash':
1006 check_ajax_referer( 'trash-' . bbp_get_reply_post_type() . '_' . $reply_id );
1007
1008 $view_all = true;
1009 $success = wp_trash_post( $reply_id );
1010 $failure = __( '<strong>ERROR</strong>: There was a problem trashing the reply!', 'bbpress' );
1011
1012 break;
1013
1014 case 'untrash':
1015 check_ajax_referer( 'untrash-' . bbp_get_reply_post_type() . '_' . $reply_id );
1016
1017 $success = wp_untrash_post( $reply_id );
1018 $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the reply!', 'bbpress' );
1019
1020 break;
1021
1022 case 'delete':
1023 check_ajax_referer( 'delete-' . bbp_get_reply_post_type() . '_' . $reply_id );
1024
1025 $success = wp_delete_post( $reply_id );
1026 $failure = __( '<strong>ERROR</strong>: There was a problem deleting the reply!', 'bbpress' );
1027
1028 break;
1029 }
1030
1031 break;
1032 }
1033
1034 // Do additional reply toggle actions
1035 do_action( 'bbp_toggle_reply_handler', $success, $post_data, $action );
1036
1037 // No errors
1038 if ( ( false != $success ) && !is_wp_error( $success ) ) {
1039
1040 /** Redirect **********************************************************/
1041
1042 // Redirect to
1043 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
1044
1045 // Get the reply URL
1046 $reply_url = bbp_get_reply_url( $reply_id, $redirect_to );
1047
1048 // Add view all if needed
1049 if ( !empty( $view_all ) )
1050 $reply_url = bbp_add_view_all( $reply_url, true );
1051
1052 // Redirect back to reply
1053 wp_redirect( $reply_url );
1054
1055 // For good measure
1056 exit();
1057
1058 // Handle errors
1059 } else {
1060 bbp_add_error( 'bbp_toggle_reply', $failure );
1061 }
1062 }
1063
1064 /** Reply Actions *************************************************************/
1065
1066 /**
1067 * Marks a reply as spam
1068 *
1069 * @since bbPress (r2740)
1070 *
1071 * @param int $reply_id Reply id
1072 * @uses wp_get_single_post() To get the reply
1073 * @uses do_action() Calls 'bbp_spam_reply' with the reply ID
1074 * @uses add_post_meta() To add the previous status to a meta
1075 * @uses wp_insert_post() To insert the updated post
1076 * @uses do_action() Calls 'bbp_spammed_reply' with the reply ID
1077 * @return mixed False or {@link WP_Error} on failure, reply id on success
1078 */
1079 function bbp_spam_reply( $reply_id = 0 ) {
1080
1081 // Get reply
1082 if ( !$reply = wp_get_single_post( $reply_id, ARRAY_A ) )
1083 return $reply;
1084
1085 // Bail if already spam
1086 if ( bbp_get_spam_status_id() == $reply['post_status'] )
1087 return false;
1088
1089 // Execute pre spam code
1090 do_action( 'bbp_spam_reply', $reply_id );
1091
1092 // Add the original post status as post meta for future restoration
1093 add_post_meta( $reply_id, '_bbp_spam_meta_status', $reply['post_status'] );
1094
1095 // Set post status to spam
1096 $reply['post_status'] = bbp_get_spam_status_id();
1097
1098 // No revisions
1099 remove_action( 'pre_post_update', 'wp_save_post_revision' );
1100
1101 // Update the reply
1102 $reply_id = wp_insert_post( $reply );
1103
1104 // Execute post spam code
1105 do_action( 'bbp_spammed_reply', $reply_id );
1106
1107 // Return reply_id
1108 return $reply_id;
1109 }
1110
1111 /**
1112 * Unspams a reply
1113 *
1114 * @since bbPress (r2740)
1115 *
1116 * @param int $reply_id Reply id
1117 * @uses wp_get_single_post() To get the reply
1118 * @uses do_action() Calls 'bbp_unspam_reply' with the reply ID
1119 * @uses get_post_meta() To get the previous status meta
1120 * @uses delete_post_meta() To delete the previous status meta
1121 * @uses wp_insert_post() To insert the updated post
1122 * @uses do_action() Calls 'bbp_unspammed_reply' with the reply ID
1123 * @return mixed False or {@link WP_Error} on failure, reply id on success
1124 */
1125 function bbp_unspam_reply( $reply_id = 0 ) {
1126
1127 // Get reply
1128 if ( !$reply = wp_get_single_post( $reply_id, ARRAY_A ) )
1129 return $reply;
1130
1131 // Bail if already not spam
1132 if ( bbp_get_spam_status_id() != $reply['post_status'] )
1133 return false;
1134
1135 // Execute pre unspam code
1136 do_action( 'bbp_unspam_reply', $reply_id );
1137
1138 // Get pre spam status
1139 $reply['post_status'] = get_post_meta( $reply_id, '_bbp_spam_meta_status', true );
1140
1141 // Delete pre spam meta
1142 delete_post_meta( $reply_id, '_bbp_spam_meta_status' );
1143
1144 // No revisions
1145 remove_action( 'pre_post_update', 'wp_save_post_revision' );
1146
1147 // Update the reply
1148 $reply_id = wp_insert_post( $reply );
1149
1150 // Execute post unspam code
1151 do_action( 'bbp_unspammed_reply', $reply_id );
1152
1153 // Return reply_id
1154 return $reply_id;
1155 }
1156
1157 /** Before Delete/Trash/Untrash ***********************************************/
1158
1159 /**
1160 * Called before deleting a reply
1161 *
1162 * @uses bbp_get_reply_id() To get the reply id
1163 * @uses bbp_is_reply() To check if the passed id is a reply
1164 * @uses do_action() Calls 'bbp_delete_reply' with the reply id
1165 */
1166 function bbp_delete_reply( $reply_id = 0 ) {
1167 $reply_id = bbp_get_reply_id( $reply_id );
1168
1169 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
1170 return false;
1171
1172 do_action( 'bbp_delete_reply', $reply_id );
1173 }
1174
1175 /**
1176 * Called before trashing a reply
1177 *
1178 * @uses bbp_get_reply_id() To get the reply id
1179 * @uses bbp_is_reply() To check if the passed id is a reply
1180 * @uses do_action() Calls 'bbp_trash_reply' with the reply id
1181 */
1182 function bbp_trash_reply( $reply_id = 0 ) {
1183 $reply_id = bbp_get_reply_id( $reply_id );
1184
1185 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
1186 return false;
1187
1188 do_action( 'bbp_trash_reply', $reply_id );
1189 }
1190
1191 /**
1192 * Called before untrashing (restoring) a reply
1193 *
1194 * @uses bbp_get_reply_id() To get the reply id
1195 * @uses bbp_is_reply() To check if the passed id is a reply
1196 * @uses do_action() Calls 'bbp_unstrash_reply' with the reply id
1197 */
1198 function bbp_untrash_reply( $reply_id = 0 ) {
1199 $reply_id = bbp_get_reply_id( $reply_id );
1200
1201 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
1202 return false;
1203
1204 do_action( 'bbp_untrash_reply', $reply_id );
1205 }
1206
1207 /** After Delete/Trash/Untrash ************************************************/
1208
1209 /**
1210 * Called after deleting a reply
1211 *
1212 * @uses bbp_get_reply_id() To get the reply id
1213 * @uses bbp_is_reply() To check if the passed id is a reply
1214 * @uses do_action() Calls 'bbp_deleted_reply' with the reply id
1215 */
1216 function bbp_deleted_reply( $reply_id = 0 ) {
1217 $reply_id = bbp_get_reply_id( $reply_id );
1218
1219 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
1220 return false;
1221
1222 do_action( 'bbp_deleted_reply', $reply_id );
1223 }
1224
1225 /**
1226 * Called after trashing a reply
1227 *
1228 * @uses bbp_get_reply_id() To get the reply id
1229 * @uses bbp_is_reply() To check if the passed id is a reply
1230 * @uses do_action() Calls 'bbp_trashed_reply' with the reply id
1231 */
1232 function bbp_trashed_reply( $reply_id = 0 ) {
1233 $reply_id = bbp_get_reply_id( $reply_id );
1234
1235 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
1236 return false;
1237
1238 do_action( 'bbp_trashed_reply', $reply_id );
1239 }
1240
1241 /**
1242 * Called after untrashing (restoring) a reply
1243 *
1244 * @uses bbp_get_reply_id() To get the reply id
1245 * @uses bbp_is_reply() To check if the passed id is a reply
1246 * @uses do_action() Calls 'bbp_untrashed_reply' with the reply id
1247 */
1248 function bbp_untrashed_reply( $reply_id = 0 ) {
1249 $reply_id = bbp_get_reply_id( $reply_id );
1250
1251 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
1252 return false;
1253
1254 do_action( 'bbp_untrashed_reply', $reply_id );
1255 }
1256
1257 /** Settings ******************************************************************/
1258
1259 /**
1260 * Return the replies per page setting
1261 *
1262 * @since bbPress (r3540)
1263 *
1264 * @uses get_option() To get the setting
1265 * @uses apply_filters() To allow the return value to be manipulated
1266 * @return int
1267 */
1268 function bbp_get_replies_per_page() {
1269
1270 // The default per setting
1271 $default = 15;
1272
1273 // Get database option and cast as integer
1274 $per = $retval = (int) get_option( '_bbp_replies_per_page', $default );
1275
1276 // If return val is empty, set it to default
1277 if ( empty( $retval ) )
1278 $retval = $default;
1279
1280 // Filter and return
1281 return (int) apply_filters( 'bbp_get_replies_per_page', $retval, $per );
1282 }
1283
1284 /**
1285 * Return the replies per RSS page setting
1286 *
1287 * @since bbPress (r3540)
1288 *
1289 * @uses get_option() To get the setting
1290 * @uses apply_filters() To allow the return value to be manipulated
1291 * @return int
1292 */
1293 function bbp_get_replies_per_rss_page() {
1294
1295 // The default per setting
1296 $default = 25;
1297
1298 // Get database option and cast as integer
1299 $per = $retval = (int) get_option( '_bbp_replies_per_rss_page', $default );
1300
1301 // If return val is empty, set it to default
1302 if ( empty( $retval ) )
1303 $retval = $default;
1304
1305 // Filter and return
1306 return (int) apply_filters( 'bbp_get_replies_per_rss_page', $retval, $per );
1307 }
1308
1309 /** Feeds *********************************************************************/
1310
1311 /**
1312 * Output an RSS2 feed of replies, based on the query passed.
1313 *
1314 * @since bbPress (r3171)
1315 *
1316 * @uses bbp_version()
1317 * @uses bbp_is_single_topic()
1318 * @uses bbp_user_can_view_forum()
1319 * @uses bbp_get_topic_forum_id()
1320 * @uses bbp_show_load_topic()
1321 * @uses bbp_topic_permalink()
1322 * @uses bbp_topic_title()
1323 * @uses bbp_get_topic_reply_count()
1324 * @uses bbp_topic_content()
1325 * @uses bbp_has_replies()
1326 * @uses bbp_replies()
1327 * @uses bbp_the_reply()
1328 * @uses bbp_reply_url()
1329 * @uses bbp_reply_title()
1330 * @uses bbp_reply_content()
1331 * @uses get_wp_title_rss()
1332 * @uses get_option()
1333 * @uses bloginfo_rss
1334 * @uses self_link()
1335 * @uses the_author()
1336 * @uses get_post_time()
1337 * @uses rss_enclosure()
1338 * @uses do_action()
1339 * @uses apply_filters()
1340 *
1341 * @param array $replies_query
1342 */
1343 function bbp_display_replies_feed_rss2( $replies_query = array() ) {
1344
1345 // User cannot access forum this topic is in
1346 if ( bbp_is_single_topic() && !bbp_user_can_view_forum( array( 'forum_id' => bbp_get_topic_forum_id() ) ) )
1347 return;
1348
1349 // Adjust the title based on context
1350 if ( bbp_is_single_topic() && bbp_user_can_view_forum( array( 'forum_id' => bbp_get_topic_forum_id() ) ) )
1351 $title = apply_filters( 'wp_title_rss', get_wp_title_rss( ' &#187; ' ) );
1352 elseif ( !bbp_show_lead_topic() )
1353 $title = ' &#187; ' . __( 'All Posts', 'bbpress' );
1354 else
1355 $title = ' &#187; ' . __( 'All Replies', 'bbpress' );
1356
1357 // Display the feed
1358 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
1359 header( 'Status: 200 OK' );
1360 echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>
1361
1362 <rss version="2.0"
1363 xmlns:content="http://purl.org/rss/1.0/modules/content/"
1364 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
1365 xmlns:dc="http://purl.org/dc/elements/1.1/"
1366 xmlns:atom="http://www.w3.org/2005/Atom"
1367
1368 <?php do_action( 'bbp_feed' ); ?>
1369 >
1370
1371 <channel>
1372 <title><?php bloginfo_rss('name'); echo $title; ?></title>
1373 <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
1374 <link><?php self_link(); ?></link>
1375 <description><?php //?></description>
1376 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s O', '', false ); ?></pubDate>
1377 <generator>http://bbpress.org/?v=<?php bbp_version(); ?></generator>
1378 <language><?php echo get_option( 'rss_language' ); ?></language>
1379
1380 <?php do_action( 'bbp_feed_head' ); ?>
1381
1382 <?php if ( bbp_is_single_topic() ) : ?>
1383 <?php if ( bbp_user_can_view_forum( array( 'forum_id' => bbp_get_topic_forum_id() ) ) ) : ?>
1384 <?php if ( bbp_show_lead_topic() ) : ?>
1385
1386 <item>
1387 <guid><?php bbp_topic_permalink(); ?></guid>
1388 <title><![CDATA[<?php bbp_topic_title(); ?>]]></title>
1389 <link><?php bbp_topic_permalink(); ?></link>
1390 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
1391 <dc:creator><?php the_author(); ?></dc:creator>
1392
1393 <description>
1394 <![CDATA[
1395 <p><?php printf( __( 'Replies: %s', 'bbpress' ), bbp_get_topic_reply_count() ); ?></p>
1396 <?php bbp_topic_content(); ?>
1397 ]]>
1398 </description>
1399
1400 <?php rss_enclosure(); ?>
1401
1402 <?php do_action( 'bbp_feed_item' ); ?>
1403
1404 </item>
1405
1406 <?php endif; ?>
1407 <?php endif; ?>
1408 <?php endif; ?>
1409
1410 <?php if ( bbp_has_replies( $replies_query ) ) : ?>
1411 <?php while ( bbp_replies() ) : bbp_the_reply(); ?>
1412
1413 <item>
1414 <guid><?php bbp_reply_url(); ?></guid>
1415 <title><![CDATA[<?php bbp_reply_title(); ?>]]></title>
1416 <link><?php bbp_reply_url(); ?></link>
1417 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s +0000', get_post_time( 'Y-m-d H:i:s', true ), false ); ?></pubDate>
1418 <dc:creator><?php the_author() ?></dc:creator>
1419
1420 <description>
1421 <![CDATA[
1422 <?php bbp_reply_content(); ?>
1423 ]]>
1424 </description>
1425
1426 <?php rss_enclosure(); ?>
1427
1428 <?php do_action( 'bbp_feed_item' ); ?>
1429
1430 </item>
1431
1432 <?php endwhile; ?>
1433 <?php endif; ?>
1434
1435 <?php do_action( 'bbp_feed_footer' ); ?>
1436
1437 </channel>
1438 </rss>
1439
1440 <?php
1441
1442 // We're done here
1443 exit();
1444 }
1445
1446 /** Permissions ***************************************************************/
1447
1448 /**
1449 * Redirect if unathorized user is attempting to edit a reply
1450 *
1451 * @since bbPress (r3605)
1452 *
1453 * @uses bbp_is_reply_edit()
1454 * @uses current_user_can()
1455 * @uses bbp_get_topic_id()
1456 * @uses wp_safe_redirect()
1457 * @uses bbp_get_topic_permalink()
1458 */
1459 function bbp_check_reply_edit() {
1460
1461 // Bail if not editing a topic
1462 if ( !bbp_is_reply_edit() )
1463 return;
1464
1465 // User cannot edit topic, so redirect back to reply
1466 if ( !current_user_can( 'edit_reply', bbp_get_reply_id() ) ) {
1467 wp_safe_redirect( bbp_get_reply_url() );
1468 exit();
1469 }
1470 }
1471
1472 ?>
1473