PluginProbe
bbPress / 2.0-rc-4
bbPress v2.0-rc-4
2.6.17 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 All 72 releases
bbpress / bbp-includes / bbp-reply-functions.php

bbp-reply-functions.php in bbPress 2.0-rc-4, at bbp-includes/bbp-reply-functions.php

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