PluginProbe
bbPress / 2.0-rc-5
bbPress v2.0-rc-5
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-topic-functions.php

bbp-topic-functions.php in bbPress 2.0-rc-5, at bbp-includes/bbp-topic-functions.php

2,984 lines 104.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Topic 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 topic to function properly.
18 *
19 * @since bbPress (r3349)
20 *
21 * @uses wp_parse_args()
22 * @uses bbp_get_topic_post_type()
23 * @uses wp_insert_post()
24 * @uses update_post_meta()
25 *
26 * @param array $topic_data Forum post data
27 * @param arrap $topic_meta Forum meta data
28 */
29 function bbp_insert_topic( $topic_data = array(), $topic_meta = array() ) {
30
31 // Forum
32 $default_topic = array(
33 'post_parent' => 0, // forum ID
34 'post_status' => bbp_get_public_status_id(),
35 'post_type' => bbp_get_topic_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 $topic_data = wp_parse_args( $topic_data, $default_topic );
45
46 // Insert topic
47 $topic_id = wp_insert_post( $topic_data );
48
49 // Bail if no topic was added
50 if ( empty( $topic_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' => $topic_id,
58 'voice_count' => 1,
59 'reply_count' => 0,
60 'reply_count_hidden' => 0,
61 'last_reply_id' => 0,
62 'last_active_id' => $topic_id,
63 'last_active_time' => get_post_field( 'post_date', $topic_id, 'db' ),
64 );
65
66 // Parse args
67 $topic_meta = wp_parse_args( $topic_meta, $default_meta );
68
69 // Insert topic meta
70 foreach ( $topic_meta as $meta_key => $meta_value )
71 update_post_meta( $topic_id, '_bbp_' . $meta_key, $meta_value );
72
73 // Update the forum
74 $forum_id = bbp_get_topic_forum_id( $topic_id );
75 if ( !empty( $forum_id ) )
76 bbp_update_forum( array( 'forum_id' => $forum_id ) );
77
78 // Return new topic ID
79 return $topic_id;
80 }
81
82 /** Post Form Handlers ********************************************************/
83
84 /**
85 * Handles the front end topic submission
86 *
87 * @uses bbPress:errors::add() To log various error messages
88 * @uses check_admin_referer() To verify the nonce and check the referer
89 * @uses bbp_is_anonymous() To check if an anonymous post is being made
90 * @uses current_user_can() To check if the current user can publish topic
91 * @uses bbp_get_current_user_id() To get the current user id
92 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
93 * @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies
94 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
95 * @uses esc_attr() For sanitization
96 * @uses bbp_is_forum_category() To check if the forum is a category
97 * @uses bbp_is_forum_closed() To check if the forum is closed
98 * @uses bbp_is_forum_private() To check if the forum is private
99 * @uses bbp_check_for_flood() To check for flooding
100 * @uses bbp_check_for_duplicate() To check for duplicates
101 * @uses bbp_get_topic_post_type() To get the topic post type
102 * @uses remove_filter() To remove 'wp_filter_kses' filters if needed
103 * @uses apply_filters() Calls 'bbp_new_topic_pre_title' with the content
104 * @uses apply_filters() Calls 'bbp_new_topic_pre_content' with the content
105 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
106 * @uses wp_insert_post() To insert the topic
107 * @uses do_action() Calls 'bbp_new_topic' with the topic id, forum id,
108 * anonymous data and reply author
109 * @uses bbp_stick_topic() To stick or super stick the topic
110 * @uses bbp_unstick_topic() To unstick the topic
111 * @uses bbp_get_topic_permalink() To get the topic permalink
112 * @uses wp_safe_redirect() To redirect to the topic link
113 * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
114 * messages
115 */
116 function bbp_new_topic_handler() {
117
118 // Bail if not a POST action
119 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
120 return;
121
122 // Bail if action is not bbp-new-topic
123 if ( empty( $_POST['action'] ) || ( 'bbp-new-topic' !== $_POST['action'] ) )
124 return;
125
126 // Nonce check
127 check_admin_referer( 'bbp-new-topic' );
128
129 // Define local variable(s)
130 $view_all = false;
131 $forum_id = $topic_author = $anonymous_data = 0;
132 $topic_title = $topic_content = '';
133 $terms = array( bbp_get_topic_tag_tax_id() => array() );
134
135 /** Topic Author **********************************************************/
136
137 // User is anonymous
138 if ( bbp_is_anonymous() ) {
139
140 // Filter anonymous data
141 $anonymous_data = bbp_filter_anonymous_post_data();
142
143 // Anonymous data checks out, so set cookies, etc...
144 if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
145 bbp_set_current_anonymous_user_data( $anonymous_data );
146 }
147
148 // User is logged in
149 } else {
150
151 // User cannot create topics
152 if ( !current_user_can( 'publish_topics' ) ) {
153 bbp_add_error( 'bbp_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new topics.', 'bbpress' ) );
154 }
155
156 // Topic author is current user
157 $topic_author = bbp_get_current_user_id();
158
159 }
160
161 // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
162 if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && wp_create_nonce( 'bbp-unfiltered-html-topic_new' ) == $_POST['_bbp_unfiltered_html_topic'] ) {
163 remove_filter( 'bbp_new_topic_pre_title', 'wp_filter_kses' );
164 remove_filter( 'bbp_new_topic_pre_content', 'wp_filter_kses' );
165 }
166
167 /** Topic Title ***********************************************************/
168
169 if ( !empty( $_POST['bbp_topic_title'] ) )
170 $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
171
172 // Filter and sanitize
173 $topic_title = apply_filters( 'bbp_new_topic_pre_title', $topic_title );
174
175 // No topic title
176 if ( empty( $topic_title ) )
177 bbp_add_error( 'bbp_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
178
179 /** Topic Content *********************************************************/
180
181 if ( !empty( $_POST['bbp_topic_content'] ) )
182 $topic_content = $_POST['bbp_topic_content'];
183
184 // Filter and sanitize
185 $topic_content = apply_filters( 'bbp_new_topic_pre_content', $topic_content );
186
187 // No topic content
188 if ( empty( $topic_content ) )
189 bbp_add_error( 'bbp_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
190
191 /** Topic Forum ***********************************************************/
192
193 // Forum id was not passed
194 if ( empty( $_POST['bbp_forum_id'] ) )
195 bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
196
197 // Forum id was passed
198 elseif ( is_numeric( $_POST['bbp_forum_id'] ) )
199 $forum_id = (int) $_POST['bbp_forum_id'];
200
201 // Forum exists
202 if ( !empty( $forum_id ) ) {
203
204 // Forum is a category
205 if ( bbp_is_forum_category( $forum_id ) )
206 bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in this forum.', 'bbpress' ) );
207
208 // Forum is closed and user cannot access
209 if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
210 bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
211
212 // Forum is private and user cannot access
213 if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
214 bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
215
216 // Forum is hidden and user cannot access
217 if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
218 bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
219 }
220
221 /** Topic Flooding ********************************************************/
222
223 if ( !bbp_check_for_flood( $anonymous_data, $topic_author ) )
224 bbp_add_error( 'bbp_topic_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
225
226 /** Topic Duplicate *******************************************************/
227
228 if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_topic_post_type(), 'post_author' => $topic_author, 'post_content' => $topic_content, 'anonymous_data' => $anonymous_data ) ) )
229 bbp_add_error( 'bbp_topic_duplicate', __( '<strong>ERROR</strong>: Duplicate topic detected; it looks as though you&#8217;ve already said that!', 'bbpress' ) );
230
231 /** topic Blacklist *******************************************************/
232
233 if ( !bbp_check_for_blacklist( $anonymous_data, $topic_author, $topic_title, $topic_content ) )
234 bbp_add_error( 'bbp_topic_blacklist', __( '<strong>ERROR</strong>: Your topic cannot be created at this time.', 'bbpress' ) );
235
236 /** Topic Tags ************************************************************/
237
238 if ( !empty( $_POST['bbp_topic_tags'] ) ) {
239
240 // Escape tag input
241 $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
242
243 // Explode by comma
244 if ( strstr( $terms, ',' ) )
245 $terms = explode( ',', $terms );
246
247 // Add topic tag ID as main key
248 $terms = array( bbp_get_topic_tag_tax_id() => $terms );
249 }
250
251 /** Additional Actions (Before Save) **************************************/
252
253 do_action( 'bbp_new_topic_pre_extras' );
254
255 /** No Errors *************************************************************/
256
257 if ( !bbp_has_errors() ) {
258
259 /** Create new topic **************************************************/
260
261 // Add the content of the form to $post as an array
262 $topic_data = array(
263 'post_author' => $topic_author,
264 'post_title' => $topic_title,
265 'post_content' => $topic_content,
266 'post_parent' => $forum_id,
267 'tax_input' => $terms,
268 'post_status' => bbp_get_public_status_id(),
269 'post_type' => bbp_get_topic_post_type()
270 );
271
272 // Just in time manipulation of topic data before being created
273 $topic_data = apply_filters( 'bbp_new_topic_pre_insert', $topic_data );
274
275 // Insert topic
276 $topic_id = wp_insert_post( $topic_data );
277
278 /** No Errors *********************************************************/
279
280 if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
281
282 /** Stickies ******************************************************/
283
284 if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
285
286 // What's the haps?
287 switch ( $_POST['bbp_stick_topic'] ) {
288
289 // Sticky in this forum
290 case 'stick' :
291 bbp_stick_topic( $topic_id );
292 break;
293
294 // Super sticky in all forums
295 case 'super' :
296 bbp_stick_topic( $topic_id, true );
297 break;
298
299 // We can avoid this as it is a new topic
300 case 'unstick' :
301 default :
302 break;
303 }
304 }
305
306 /** Trash Check ***************************************************/
307
308 // If the forum is trash, or the topic_status is switched to
309 // trash, trash it properly
310 if ( ( get_post_field( 'post_status', $forum_id ) == bbp_get_trash_status_id() ) || ( $topic_data['post_status'] == bbp_get_trash_status_id() ) ) {
311
312 // Trash the reply
313 wp_trash_post( $topic_id );
314
315 // Force view=all
316 $view_all = true;
317 }
318
319 /** Spam Check ****************************************************/
320
321 // If reply or topic are spam, officially spam this reply
322 if ( $topic_data['post_status'] == bbp_get_spam_status_id() ) {
323 add_post_meta( $topic_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
324
325 // Force view=all
326 $view_all = true;
327 }
328
329 /** Update counts, etc... *****************************************/
330
331 do_action( 'bbp_new_topic', $topic_id, $forum_id, $anonymous_data, $topic_author );
332
333 /** Redirect ******************************************************/
334
335 // Redirect to
336 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
337
338 // Get the topic URL
339 $redirect_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
340
341 // Add view all?
342 if ( bbp_get_view_all() || !empty( $view_all ) ) {
343
344 // User can moderate, so redirect to topic with view all set
345 if ( current_user_can( 'moderate' ) ) {
346 $redirect_url = bbp_add_view_all( $redirect_url );
347
348 // User cannot moderate, so redirect to forum
349 } else {
350 $redirect_url = bbp_get_forum_permalink( $forum_id );
351 }
352 }
353
354 // Allow to be filtered
355 $redirect_url = apply_filters( 'bbp_new_topic_redirect_to', $redirect_url, $redirect_to );
356
357 /** Successful Save ***********************************************/
358
359 // Redirect back to new topic
360 wp_safe_redirect( $redirect_url );
361
362 // For good measure
363 exit();
364
365 // Errors
366 } else {
367 $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
368 bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error, 'bbpress' ) );
369 }
370 }
371 }
372
373 /**
374 * Handles the front end edit topic submission
375 *
376 * @uses bbPress:errors::add() To log various error messages
377 * @uses bbp_get_topic() To get the topic
378 * @uses check_admin_referer() To verify the nonce and check the referer
379 * @uses bbp_is_topic_anonymous() To check if topic is by an anonymous user
380 * @uses current_user_can() To check if the current user can edit the topic
381 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
382 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
383 * @uses esc_attr() For sanitization
384 * @uses bbp_is_forum_category() To check if the forum is a category
385 * @uses bbp_is_forum_closed() To check if the forum is closed
386 * @uses bbp_is_forum_private() To check if the forum is private
387 * @uses remove_filter() To remove 'wp_filter_kses' filters if needed
388 * @uses apply_filters() Calls 'bbp_edit_topic_pre_title' with the title and
389 * topic id
390 * @uses apply_filters() Calls 'bbp_edit_topic_pre_content' with the content
391 * and topic id
392 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
393 * @uses wp_save_post_revision() To save a topic revision
394 * @uses bbp_update_topic_revision_log() To update the topic revision log
395 * @uses bbp_stick_topic() To stick or super stick the topic
396 * @uses bbp_unstick_topic() To unstick the topic
397 * @uses wp_update_post() To update the topic
398 * @uses do_action() Calls 'bbp_edit_topic' with the topic id, forum id,
399 * anonymous data and reply author
400 * @uses bbp_move_topic_handler() To handle movement of a topic from one forum
401 * to another
402 * @uses bbp_get_topic_permalink() To get the topic permalink
403 * @uses wp_safe_redirect() To redirect to the topic link
404 * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
405 * messages
406 */
407 function bbp_edit_topic_handler() {
408
409 // Bail if not a POST action
410 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
411 return;
412
413 // Bail if action is not bbp-edit-topic
414 if ( empty( $_POST['action'] ) || ( 'bbp-edit-topic' !== $_POST['action'] ) )
415 return;
416
417 // Define local variable(s)
418 $topic = $topic_id = $forum_id = $anonymous_data = 0;
419 $topic_title = $topic_content = $topic_edit_reason = '';
420 $terms = array( bbp_get_topic_tag_tax_id() => array() );
421
422 /** Topic *****************************************************************/
423
424 // Topic id was not passed
425 if ( empty( $_POST['bbp_topic_id'] ) ) {
426 bbp_add_error( 'bbp_edit_topic_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
427
428 // Topic id was passed
429 } elseif ( is_numeric( $_POST['bbp_topic_id'] ) ) {
430 $topic_id = (int) $_POST['bbp_topic_id'];
431 $topic = bbp_get_topic( $topic_id );
432 }
433
434 // Topic does not exist
435 if ( empty( $topic ) ) {
436 bbp_add_error( 'bbp_edit_topic_not_found', __( '<strong>ERROR</strong>: The topic you want to edit was not found.', 'bbpress' ) );
437
438 // Topic exists
439 } else {
440
441 // Nonce check
442 check_admin_referer( 'bbp-edit-topic_' . $topic_id );
443
444 // Check users ability to create new topic
445 if ( !bbp_is_topic_anonymous( $topic_id ) ) {
446
447 // User cannot edit this topic
448 if ( !current_user_can( 'edit_topic', $topic_id ) ) {
449 bbp_add_error( 'bbp_edit_topic_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that topic.', 'bbpress' ) );
450 }
451
452 // It is an anonymous post
453 } else {
454
455 // Filter anonymous data
456 $anonymous_data = bbp_filter_anonymous_post_data( array(), true );
457 }
458 }
459
460 // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
461 if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_topic'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-topic_' . $topic_id ) == $_POST['_bbp_unfiltered_html_topic'] ) ) {
462 remove_filter( 'bbp_edit_topic_pre_title', 'wp_filter_kses' );
463 remove_filter( 'bbp_edit_topic_pre_content', 'wp_filter_kses' );
464 }
465
466 /** Topic Forum ***********************************************************/
467
468 // Forum id was not passed
469 if ( empty( $_POST['bbp_forum_id'] ) ) {
470 bbp_add_error( 'bbp_topic_forum_id', __( '<strong>ERROR</strong>: Forum ID is missing.', 'bbpress' ) );
471
472 // Forum id was passed
473 } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
474 $forum_id = (int) $_POST['bbp_forum_id'];
475 }
476
477 // Current forum this topic is in
478 $current_forum_id = bbp_get_topic_forum_id( $topic_id );
479
480 // Forum exists
481 if ( !empty( $forum_id ) && ( $forum_id !== $current_forum_id ) ) {
482
483 // Forum is a category
484 if ( bbp_is_forum_category( $forum_id ) )
485 bbp_add_error( 'bbp_edit_topic_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No topics can be created in it.', 'bbpress' ) );
486
487 // Forum is closed and user cannot access
488 if ( bbp_is_forum_closed( $forum_id ) && !current_user_can( 'edit_forum', $forum_id ) )
489 bbp_add_error( 'bbp_edit_topic_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new topics.', 'bbpress' ) );
490
491 // Forum is private and user cannot access
492 if ( bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
493 bbp_add_error( 'bbp_edit_topic_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
494
495 // Forum is hidden and user cannot access
496 if ( bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
497 bbp_add_error( 'bbp_edit_topic_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new topics in it.', 'bbpress' ) );
498 }
499
500 /** Topic Title ***********************************************************/
501
502 if ( !empty( $_POST['bbp_topic_title'] ) )
503 $topic_title = esc_attr( strip_tags( $_POST['bbp_topic_title'] ) );
504
505 // Filter and sanitize
506 $topic_title = apply_filters( 'bbp_edit_topic_pre_title', $topic_title, $topic_id );
507
508 // No topic title
509 if ( empty( $topic_title ) )
510 bbp_add_error( 'bbp_edit_topic_title', __( '<strong>ERROR</strong>: Your topic needs a title.', 'bbpress' ) );
511
512 /** Topic Content *********************************************************/
513
514 if ( !empty( $_POST['bbp_topic_content'] ) )
515 $topic_content = $_POST['bbp_topic_content'];
516
517 // Filter and sanitize
518 $topic_content = apply_filters( 'bbp_edit_topic_pre_content', $topic_content, $topic_id );
519
520 // No topic content
521 if ( empty( $topic_content ) )
522 bbp_add_error( 'bbp_edit_topic_content', __( '<strong>ERROR</strong>: Your topic cannot be empty.', 'bbpress' ) );
523
524 /** topic Blacklist *******************************************************/
525
526 if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_topic_author_id( $topic_id ), $topic_title, $topic_content ) )
527 bbp_add_error( 'bbp_topic_blacklist', __( '<strong>ERROR</strong>: Your topic cannot be edited at this time.', 'bbpress' ) );
528
529 /** Topic Tags ************************************************************/
530
531 // Tags
532 if ( !empty( $_POST['bbp_topic_tags'] ) ) {
533
534 // Escape tag input
535 $terms = esc_attr( strip_tags( $_POST['bbp_topic_tags'] ) );
536
537 // Explode by comma
538 if ( strstr( $terms, ',' ) )
539 $terms = explode( ',', $terms );
540
541 // Add topic tag ID as main key
542 $terms = array( bbp_get_topic_tag_tax_id() => $terms );
543 }
544
545 /** Additional Actions (Before Save) **************************************/
546
547 do_action( 'bbp_edit_topic_pre_extras', $topic_id );
548
549 /** No Errors *************************************************************/
550
551 if ( !bbp_has_errors() ) {
552
553 /** Stickies **********************************************************/
554
555 if ( !empty( $_POST['bbp_stick_topic'] ) && in_array( $_POST['bbp_stick_topic'], array( 'stick', 'super', 'unstick' ) ) ) {
556
557 // What's the dilly?
558 switch ( $_POST['bbp_stick_topic'] ) {
559
560 // Sticky in forum
561 case 'stick' :
562 bbp_stick_topic( $topic_id );
563 break;
564
565 // Sticky in all forums
566 case 'super' :
567 bbp_stick_topic( $topic_id, true );
568 break;
569
570 // Normal
571 case 'unstick' :
572 default :
573 bbp_unstick_topic( $topic_id );
574 break;
575 }
576 }
577
578 /** Update the topic **************************************************/
579
580 // Add the content of the form to $post as an array
581 $topic_data = array(
582 'ID' => $topic_id,
583 'post_title' => $topic_title,
584 'post_content' => $topic_content,
585 'post_parent' => $forum_id,
586 'tax_input' => $terms,
587 );
588
589 // Just in time manipulation of topic data before being edited
590 $topic_data = apply_filters( 'bbp_edit_topic_pre_insert', $topic_data );
591
592 // Insert topic
593 $topic_id = wp_update_post( $topic_data );
594
595 /** Revisions *********************************************************/
596
597 // Revision Reason
598 if ( !empty( $_POST['bbp_topic_edit_reason'] ) )
599 $topic_edit_reason = esc_attr( strip_tags( $_POST['bbp_topic_edit_reason'] ) );
600
601 // Update revision log
602 if ( !empty( $_POST['bbp_log_topic_edit'] ) && ( 1 == $_POST['bbp_log_topic_edit'] ) && ( $revision_id = wp_save_post_revision( $topic_id ) ) ) {
603 bbp_update_topic_revision_log( array(
604 'topic_id' => $topic_id,
605 'revision_id' => $revision_id,
606 'author_id' => bbp_get_current_user_id(),
607 'reason' => $topic_edit_reason
608 ) );
609 }
610
611 /** No Errors *********************************************************/
612
613 if ( !empty( $topic_id ) && !is_wp_error( $topic_id ) ) {
614
615 // Update counts, etc...
616 do_action( 'bbp_edit_topic', $topic_id, $forum_id, $anonymous_data, $topic->post_author , true /* Is edit */ );
617
618 // If the new forum id is not equal to the old forum id, run the
619 // bbp_move_topic action and pass the topic's forum id as the
620 // first arg and topic id as the second to update counts.
621 if ( $forum_id != $topic->post_parent )
622 bbp_move_topic_handler( $topic_id, $topic->post_parent, $forum_id );
623
624 /** Additional Actions (After Save) *******************************/
625
626 do_action( 'bbp_edit_topic_post_extras', $topic_id );
627
628 /** Redirect ******************************************************/
629
630 // Redirect to
631 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
632
633 // View all?
634 $view_all = bbp_get_view_all();
635
636 // Get the topic URL
637 $topic_url = bbp_get_topic_permalink( $topic_id, $redirect_to );
638
639 // Add view all?
640 if ( !empty( $view_all ) )
641 $topic_url = bbp_add_view_all( $topic_url );
642
643 // Allow to be filtered
644 $topic_url = apply_filters( 'bbp_edit_topic_redirect_to', $topic_url, $view_all, $redirect_to );
645
646 /** Successful Edit ***********************************************/
647
648 // Redirect back to new topic
649 wp_safe_redirect( $topic_url );
650
651 // For good measure
652 exit();
653
654 /** Errors ************************************************************/
655
656 } else {
657 $append_error = ( is_wp_error( $topic_id ) && $topic_id->get_error_message() ) ? $topic_id->get_error_message() . ' ' : '';
658 bbp_add_error( 'bbp_topic_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your topic:' . $append_error . 'Please try again.', 'bbpress' ) );
659 }
660 }
661 }
662
663 /**
664 * Handle all the extra meta stuff from posting a new topic
665 *
666 * @param int $topic_id Optional. Topic id
667 * @param int $forum_id Optional. Forum id
668 * @param bool|array $anonymous_data Optional. If it is an array, it is
669 * extracted and anonymous user info is saved
670 * @param int $author_id Author id
671 * @param bool $is_edit Optional. Is the post being edited? Defaults to false.
672 * @uses bbp_get_topic_id() To get the topic id
673 * @uses bbp_get_forum_id() To get the forum id
674 * @uses bbp_get_current_user_id() To get the current user id
675 * @yses bbp_get_topic_forum_id() To get the topic forum id
676 * @uses update_post_meta() To update the topic metas
677 * @uses set_transient() To update the flood check transient for the ip
678 * @uses update_user_meta() To update the last posted meta for the user
679 * @uses bbp_is_subscriptions_active() To check if the subscriptions feature is
680 * activated or not
681 * @uses bbp_is_user_subscribed() To check if the user is subscribed
682 * @uses bbp_remove_user_subscription() To remove the user's subscription
683 * @uses bbp_add_user_subscription() To add the user's subscription
684 * @uses bbp_update_topic_forum_id() To update the topic's forum id
685 * @uses bbp_update_topic_topic_id() To update the topic's topic id
686 * @uses bbp_update_topic_last_reply_id() To update the last reply id topic meta
687 * @uses bbp_update_topic_last_active_id() To update the topic last active id
688 * @uses bbp_update_topic_last_active_time() To update the last active topic meta
689 * @uses bbp_update_topic_reply_count() To update the topic reply count
690 * @uses bbp_update_topic_reply_count_hidden() To udpate the topic hidden reply count
691 * @uses bbp_update_topic_voice_count() To update the topic voice count
692 * @uses bbp_update_topic_walker() To udpate the topic's ancestors
693 */
694 function bbp_update_topic( $topic_id = 0, $forum_id = 0, $anonymous_data = false, $author_id = 0, $is_edit = false ) {
695
696 // Validate the ID's passed from 'bbp_new_topic' action
697 $topic_id = bbp_get_topic_id( $topic_id );
698 $forum_id = bbp_get_forum_id( $forum_id );
699
700 // Check author_id
701 if ( empty( $author_id ) )
702 $author_id = bbp_get_current_user_id();
703
704 // Check forum_id
705 if ( empty( $forum_id ) )
706 $forum_id = bbp_get_topic_forum_id( $topic_id );
707
708 // If anonymous post, store name, email, website and ip in post_meta.
709 // It expects anonymous_data to be sanitized.
710 // Check bbp_filter_anonymous_post_data() for sanitization.
711 if ( !empty( $anonymous_data ) && is_array( $anonymous_data ) ) {
712 extract( $anonymous_data );
713
714 update_post_meta( $topic_id, '_bbp_anonymous_name', $bbp_anonymous_name, false );
715 update_post_meta( $topic_id, '_bbp_anonymous_email', $bbp_anonymous_email, false );
716
717 // Set transient for throttle check (only on new, not edit)
718 if ( empty( $is_edit ) ) {
719 set_transient( '_bbp_' . bbp_current_author_ip() . '_last_posted', time() );
720 }
721
722 // Website is optional
723 if ( !empty( $bbp_anonymous_website ) ) {
724 update_post_meta( $topic_id, '_bbp_anonymous_website', $bbp_anonymous_website, false );
725 }
726 } else {
727 if ( empty( $is_edit ) && !current_user_can( 'throttle' ) ) {
728 update_user_meta( $author_id, '_bbp_last_posted', time() );
729 }
730 }
731
732 // Handle Subscription Checkbox
733 if ( bbp_is_subscriptions_active() && !empty( $author_id ) ) {
734 $subscribed = bbp_is_user_subscribed( $author_id, $topic_id );
735 $subscheck = ( !empty( $_POST['bbp_topic_subscription'] ) && ( 'bbp_subscribe' == $_POST['bbp_topic_subscription'] ) ) ? true : false;
736
737 // Subscribed and unsubscribing
738 if ( true == $subscribed && false == $subscheck ) {
739 bbp_remove_user_subscription( $author_id, $topic_id );
740
741 // Subscribing
742 } elseif ( false == $subscribed && true == $subscheck ) {
743 bbp_add_user_subscription( $author_id, $topic_id );
744 }
745 }
746
747 // Forum topic meta
748 bbp_update_topic_forum_id( $topic_id, $forum_id );
749 bbp_update_topic_topic_id( $topic_id, $topic_id );
750
751 // Update associated topic values if this is a new topic
752 if ( empty( $is_edit ) ) {
753
754 // Update poster IP if not editing
755 update_post_meta( $topic_id, '_bbp_author_ip', bbp_current_author_ip(), false );
756
757 // Last active time
758 $last_active = current_time( 'mysql' );
759
760 // Reply topic meta
761 bbp_update_topic_last_reply_id ( $topic_id, 0 );
762 bbp_update_topic_last_active_id ( $topic_id, $topic_id );
763 bbp_update_topic_last_active_time ( $topic_id, $last_active );
764 bbp_update_topic_reply_count ( $topic_id, 0 );
765 bbp_update_topic_reply_count_hidden ( $topic_id, 0 );
766 bbp_update_topic_voice_count ( $topic_id );
767
768 // Walk up ancestors and do the dirty work
769 bbp_update_topic_walker( $topic_id, $last_active, $forum_id, 0, false );
770 }
771 }
772
773 /**
774 * Walks up the post_parent tree from the current topic_id, and updates the
775 * counts of forums above it. This calls a few internal functions that all run
776 * manual queries against the database to get their results. As such, this
777 * function can be costly to run but is necessary to keep everything accurate.
778 *
779 * @since bbPress (r2800)
780 * @param int $topic_id Topic id
781 * @param string $last_active_time Optional. Last active time
782 * @param int $forum_id Optional. Forum id
783 * @param int $reply_id Optional. Reply id
784 * @param bool $refresh Reset all the previous parameters? Defaults to true.
785 * @uses bbp_get_topic_id() To get the topic id
786 * @uses bbp_get_topic_forum_id() To get the topic forum id
787 * @uses get_post_ancestors() To get the topic's ancestors
788 * @uses bbp_is_forum() To check if the ancestor is a forum
789 * @uses bbp_update_forum() To update the forum
790 */
791 function bbp_update_topic_walker( $topic_id, $last_active_time = '', $forum_id = 0, $reply_id = 0, $refresh = true ) {
792
793 // Validate topic_id
794 $topic_id = bbp_get_topic_id( $topic_id );
795
796 // Topic was passed
797 if ( !empty( $topic_id ) ) {
798
799 // Get the forum ID if none was passed
800 if ( empty( $forum_id ) ) {
801 $forum_id = bbp_get_topic_forum_id( $topic_id );
802 }
803
804 // Set the active_id based on topic_id/reply_id
805 $active_id = empty( $reply_id ) ? $topic_id : $reply_id;
806 }
807
808 // Get topic ancestors
809 $ancestors = array_values( array_unique( array_merge( array( $forum_id ), get_post_ancestors( $topic_id ) ) ) );
810
811 // Topic status
812 $topic_status = get_post_status( $topic_id );
813
814 // If we want a full refresh, unset any of the possibly passed variables
815 if ( true == $refresh ) {
816 $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
817 $topic_status = bbp_get_public_status_id();
818 }
819
820 // Loop through ancestors
821 foreach ( $ancestors as $ancestor ) {
822
823 // If ancestor is a forum, update counts
824 if ( bbp_is_forum( $ancestor ) ) {
825
826 // Update the forum
827 bbp_update_forum( array(
828 'forum_id' => $ancestor,
829 'last_topic_id' => $topic_id,
830 'last_reply_id' => $reply_id,
831 'last_active_id' => $active_id,
832 'last_active_time' => 0,
833 'last_active_status' => $topic_status
834 ) );
835 }
836 }
837 }
838
839 /**
840 * Handle the moving of a topic from one forum to another. This includes walking
841 * up the old and new branches and updating the counts.
842 *
843 * @param int $topic_id Topic id
844 * @param int $old_forum_id Old forum id
845 * @param int $new_forum_id New forum id
846 * @uses bbp_get_topic_id() To get the topic id
847 * @uses bbp_get_forum_id() To get the forum id
848 * @uses bbp_get_stickies() To get the old forums sticky topics
849 * @uses delete_post_meta() To delete the forum sticky meta
850 * @uses update_post_meta() To update the old forum sticky meta
851 * @uses bbp_stick_topic() To stick the topic in the new forum
852 * @uses bbp_get_reply_post_type() To get the reply post type
853 * @uses bbp_get_all_child_ids() To get the public child ids
854 * @uses bbp_update_reply_forum_id() To update the reply forum id
855 * @uses bbp_update_topic_forum_id() To update the topic forum id
856 * @uses get_post_ancestors() To get the topic's ancestors
857 * @uses bbp_is_forum() To check if the ancestor is a forum
858 * @uses bbp_update_forum() To update the forum
859 */
860 function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) {
861
862 // Validate parameters
863 $topic_id = bbp_get_topic_id( $topic_id );
864 $old_forum_id = bbp_get_forum_id( $old_forum_id );
865 $new_forum_id = bbp_get_forum_id( $new_forum_id );
866
867 /** Stickies **************************************************************/
868
869 // Get forum stickies
870 $old_stickies = bbp_get_stickies( $old_forum_id );
871
872 // Only proceed if stickies are found
873 if ( !empty( $old_stickies ) ) {
874
875 // Define local variables
876 $updated_stickies = array();
877
878 // Loop through stickies of forum
879 foreach ( $old_stickies as $sticky_topic_id ) {
880
881 // Add non-matches to the updated array
882 if ( $topic_id != $sticky_topic_id ) {
883 $updated_stickies[$k] = $v;
884 }
885 }
886
887 // No more stickies so delete the beta
888 if ( empty( $updated_stickies ) ) {
889 delete_post_meta ( $old_forum_id, '_bbp_sticky_topics' );
890
891 // Still stickies so update the meta
892 } else {
893 update_post_meta( $old_forum_id, '_bbp_sticky_topics', $updated_stickies );
894 }
895
896 // Topic was sticky, so restick in new forum
897 bbp_stick_topic( $topic_id );
898 }
899
900 /** Topic Replies *********************************************************/
901
902 // Get the topics replies
903 $replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() );
904
905 // Update the forum_id of all replies in the topic
906 foreach ( $replies as $reply_id )
907 bbp_update_reply_forum_id( $reply_id, $new_forum_id );
908
909 // Forum topic meta
910 bbp_update_topic_forum_id( $topic_id, $new_forum_id );
911
912 /** Old forum_id **********************************************************/
913
914 // Get topic ancestors
915 $ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), get_post_ancestors( $old_forum_id ) ) ) );
916
917 // Loop through ancestors
918 foreach ( $ancestors as $ancestor ) {
919
920 // If ancestor is a forum, update counts
921 if ( bbp_is_forum( $ancestor ) ) {
922
923 // Update the forum
924 bbp_update_forum( array(
925 'forum_id' => $ancestor,
926 ) );
927 }
928 }
929
930 /** New forum_id **********************************************************/
931
932 // Make sure we're not walking twice
933 if ( !in_array( $new_forum_id, $ancestors ) ) {
934
935 // Get topic ancestors
936 $ancestors = array_values( array_unique( array_merge( array( $new_forum_id ), get_post_ancestors( $new_forum_id ) ) ) );
937
938 // Loop through ancestors
939 foreach ( $ancestors as $ancestor ) {
940
941 // If ancestor is a forum, update counts
942 if ( bbp_is_forum( $ancestor ) ) {
943
944 // Update the forum
945 bbp_update_forum( array(
946 'forum_id' => $ancestor,
947 ) );
948 }
949 }
950 }
951 }
952
953 /**
954 * Merge topic handler
955 *
956 * Handles the front end merge topic submission
957 *
958 * @since bbPress (r2756)
959 *
960 * @uses bbPress:errors::add() To log various error messages
961 * @uses bbp_get_topic() To get the topics
962 * @uses check_admin_referer() To verify the nonce and check the referer
963 * @uses current_user_can() To check if the current user can edit the topics
964 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
965 * @uses do_action() Calls 'bbp_merge_topic' with the destination and source
966 * topic ids
967 * @uses bbp_get_topic_subscribers() To get the source topic subscribers
968 * @uses bbp_add_user_subscription() To add the user subscription
969 * @uses bbp_remove_user_subscription() To remove the user subscription
970 * @uses bbp_get_topic_favoriters() To get the source topic favoriters
971 * @uses bbp_add_user_favorite() To add the user favorite
972 * @uses bbp_remove_user_favorite() To remove the user favorite
973 * @uses wp_get_post_terms() To get the source topic tags
974 * @uses wp_set_post_terms() To set the topic tags
975 * @uses wp_delete_object_term_relationships() To delete the topic tags
976 * @uses bbp_open_topic() To open the topic
977 * @uses bbp_unstick_topic() To unstick the topic
978 * @uses bbp_get_reply_post_type() To get the reply post type
979 * @uses get_posts() To get the replies
980 * @uses wp_update_post() To update the topic
981 * @uses bbp_update_reply_topic_id() To update the reply topic id
982 * @uses bbp_get_topic_forum_id() To get the topic forum id
983 * @uses bbp_update_reply_forum_id() To update the reply forum id
984 * @uses do_action() Calls 'bbp_merged_topic_reply' with the reply id and
985 * destination topic id
986 * @uses do_action() Calls 'bbp_merged_topic' with the destination and source
987 * topic ids and source topic's forum id
988 * @uses bbp_get_topic_permalink() To get the topic permalink
989 * @uses wp_redirect() To redirect to the topic link
990 */
991 function bbp_merge_topic_handler() {
992
993 // Bail if not a POST action
994 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
995 return;
996
997 // Bail if action is not bbp-merge-topic
998 if ( empty( $_POST['action'] ) || ( 'bbp-merge-topic' !== $_POST['action'] ) )
999 return;
1000
1001 // Define local variable(s)
1002 $source_topic_id = $destination_topic_id = 0;
1003 $source_topic = $destination_topic = 0;
1004 $subscribers = $favoriters = $replies = array();
1005
1006 /** Source Topic **********************************************************/
1007
1008 // Topic id
1009 if ( empty( $_POST['bbp_topic_id'] ) )
1010 bbp_add_error( 'bbp_merge_topic_source_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
1011 else
1012 $source_topic_id = (int) $_POST['bbp_topic_id'];
1013
1014 // Nonce check
1015 check_admin_referer( 'bbp-merge-topic_' . $source_topic_id );
1016
1017 // Source topic not found
1018 if ( !$source_topic = bbp_get_topic( $source_topic_id ) )
1019 bbp_add_error( 'bbp_merge_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to merge was not found.', 'bbpress' ) );
1020
1021 // Cannot edit source topic
1022 if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
1023 bbp_add_error( 'bbp_merge_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
1024
1025 /** Destination Topic *****************************************************/
1026
1027 // Topic id
1028 if ( empty( $_POST['bbp_destination_topic'] ) )
1029 bbp_add_error( 'bbp_merge_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found.', 'bbpress' ) );
1030 else
1031 $destination_topic_id = (int) $_POST['bbp_destination_topic'];
1032
1033 // Destination topic not found
1034 if ( !$destination_topic = bbp_get_topic( $destination_topic_id ) )
1035 bbp_add_error( 'bbp_merge_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to merge to was not found.', 'bbpress' ) );
1036
1037 // Cannot edit destination topic
1038 if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
1039 bbp_add_error( 'bbp_merge_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic.', 'bbpress' ) );
1040
1041 /** No Errors *************************************************************/
1042
1043 if ( !bbp_has_errors() ) {
1044
1045 // Update counts, etc...
1046 do_action( 'bbp_merge_topic', $destination_topic->ID, $source_topic->ID );
1047
1048 /** Date Check ********************************************************/
1049
1050 // Check if the destination topic is older than the source topic
1051 if ( strtotime( $source_topic->post_date ) < strtotime( $destination_topic->post_date ) ) {
1052
1053 // Set destination topic post_date to 1 second before source topic
1054 $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $source_topic->post_date ) - 1 );
1055
1056 $postarr = array(
1057 'ID' => $destination_topic_id,
1058 'post_date' => $destination_post_date,
1059 'post_date_gmt' => get_gmt_from_date( $destination_post_date )
1060 );
1061
1062 // Update destination topic
1063 wp_update_post( $postarr );
1064 }
1065
1066 /** Subscriptions *****************************************************/
1067
1068 // Get subscribers from source topic
1069 $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
1070
1071 // Remove the topic from everybody's subscriptions
1072 if ( !empty( $subscribers ) ) {
1073
1074 // Loop through each user
1075 foreach ( (array) $subscribers as $subscriber ) {
1076
1077 // Shift the subscriber if told to
1078 if ( !empty( $_POST['bbp_topic_subscribers'] ) && ( 1 == $_POST['bbp_topic_subscribers'] ) && bbp_is_subscriptions_active() )
1079 bbp_add_user_subscription( $subscriber, $destination_topic->ID );
1080
1081 // Remove old subscription
1082 bbp_remove_user_subscription( $subscriber, $source_topic->ID );
1083 }
1084 }
1085
1086 /** Favorites *********************************************************/
1087
1088 // Get favoriters from source topic
1089 $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
1090
1091 // Remove the topic from everybody's favorites
1092 if ( !empty( $favoriters ) ) {
1093
1094 // Loop through each user
1095 foreach ( (array) $favoriters as $favoriter ) {
1096
1097 // Shift the favoriter if told to
1098 if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] )
1099 bbp_add_user_favorite( $favoriter, $destination_topic->ID );
1100
1101 // Remove old favorite
1102 bbp_remove_user_favorite( $favoriter, $source_topic->ID );
1103 }
1104 }
1105
1106 /** Tags **************************************************************/
1107
1108 // Get the source topic tags
1109 $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
1110
1111 // Tags to possibly merge
1112 if ( !empty( $source_topic_tags ) && !is_wp_error( $source_topic_tags ) ) {
1113
1114 // Shift the tags if told to
1115 if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) )
1116 wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
1117
1118 // Delete the tags from the source topic
1119 wp_delete_object_term_relationships( $source_topic->ID, bbp_get_topic_tag_tax_id() );
1120 }
1121
1122 /** Source Topic ******************************************************/
1123
1124 // Status
1125 bbp_open_topic( $source_topic->ID );
1126
1127 // Sticky
1128 bbp_unstick_topic( $source_topic->ID );
1129
1130 // Get the replies of the source topic
1131 $replies = (array) get_posts( array(
1132 'post_parent' => $source_topic->ID,
1133 'post_type' => bbp_get_reply_post_type(),
1134 'posts_per_page' => -1,
1135 'order' => 'ASC'
1136 ) );
1137
1138 // Prepend the source topic to its replies array for processing
1139 array_unshift( $replies, $source_topic );
1140
1141 if ( !empty( $replies ) ) {
1142
1143 /** Merge Replies *************************************************/
1144
1145 // Change the post_parent of each reply to the destination topic id
1146 foreach ( $replies as $reply ) {
1147 $postarr = array(
1148 'ID' => $reply->ID,
1149 'post_title' => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
1150 'post_name' => false,
1151 'post_type' => bbp_get_reply_post_type(),
1152 'post_parent' => $destination_topic->ID,
1153 'guid' => ''
1154 );
1155
1156 wp_update_post( $postarr );
1157
1158 // Adjust reply meta values
1159 bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID );
1160 bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
1161
1162 // Do additional actions per merged reply
1163 do_action( 'bbp_merged_topic_reply', $reply->ID, $destination_topic->ID );
1164 }
1165 }
1166
1167 /** Successful Merge **************************************************/
1168
1169 // Update topic's last meta data
1170 bbp_update_topic_last_reply_id ( $destination_topic->ID );
1171 bbp_update_topic_last_active_id ( $destination_topic->ID );
1172 bbp_update_topic_last_active_time( $destination_topic->ID );
1173
1174 // Send the post parent of the source topic as it has been shifted
1175 // (possibly to a new forum) so we need to update the counts of the
1176 // old forum as well as the new one
1177 do_action( 'bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent );
1178
1179 // Redirect back to new topic
1180 wp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
1181
1182 // For good measure
1183 exit();
1184 }
1185 }
1186
1187 /**
1188 * Fix counts on topic merge
1189 *
1190 * When a topic is merged, update the counts of source and destination topic
1191 * and their forums.
1192 *
1193 * @since bbPress (r2756)
1194 *
1195 * @param int $destination_topic_id Destination topic id
1196 * @param int $source_topic_id Source topic id
1197 * @param int $source_topic_forum Source topic's forum id
1198 * @uses bbp_update_forum_topic_count() To update the forum topic counts
1199 * @uses bbp_update_forum_reply_count() To update the forum reply counts
1200 * @uses bbp_update_topic_reply_count() To update the topic reply counts
1201 * @uses bbp_update_topic_voice_count() To update the topic voice counts
1202 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
1203 * count
1204 * @uses do_action() Calls 'bbp_merge_topic_count' with the destination topic
1205 * id, source topic id & source topic forum id
1206 */
1207 function bbp_merge_topic_count( $destination_topic_id, $source_topic_id, $source_topic_forum_id ) {
1208
1209 /** Source Topic **********************************************************/
1210
1211 // Forum Topic Counts
1212 bbp_update_forum_topic_count( $source_topic_forum_id );
1213
1214 // Forum Reply Counts
1215 bbp_update_forum_reply_count( $source_topic_forum_id );
1216
1217 /** Destination Topic *****************************************************/
1218
1219 // Topic Reply Counts
1220 bbp_update_topic_reply_count( $destination_topic_id );
1221
1222 // Topic Hidden Reply Counts
1223 bbp_update_topic_reply_count_hidden( $destination_topic_id );
1224
1225 // Topic Voice Counts
1226 bbp_update_topic_voice_count( $destination_topic_id );
1227
1228 do_action( 'bbp_merge_topic_count', $destination_topic_id, $source_topic_id, $source_topic_forum_id );
1229 }
1230
1231 /**
1232 * Split topic handler
1233 *
1234 * Handles the front end split topic submission
1235 *
1236 * @since bbPress (r2756)
1237 *
1238 * @uses bbPress:errors::add() To log various error messages
1239 * @uses bbp_get_reply() To get the reply
1240 * @uses bbp_get_topic() To get the topics
1241 * @uses check_admin_referer() To verify the nonce and check the referer
1242 * @uses current_user_can() To check if the current user can edit the topics
1243 * @uses bbp_get_topic_post_type() To get the topic post type
1244 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
1245 * @uses do_action() Calls 'bbp_pre_split_topic' with the from reply id, source
1246 * and destination topic ids
1247 * @uses bbp_get_topic_subscribers() To get the source topic subscribers
1248 * @uses bbp_add_user_subscription() To add the user subscription
1249 * @uses bbp_get_topic_favoriters() To get the source topic favoriters
1250 * @uses bbp_add_user_favorite() To add the user favorite
1251 * @uses wp_get_post_terms() To get the source topic tags
1252 * @uses wp_set_post_terms() To set the topic tags
1253 * @uses bbp_get_reply_post_type() To get the reply post type
1254 * @uses wpdb::prepare() To prepare our sql query
1255 * @uses wpdb::get_results() To execute the sql query and get results
1256 * @uses wp_update_post() To update the replies
1257 * @uses bbp_update_reply_topic_id() To update the reply topic id
1258 * @uses bbp_get_topic_forum_id() To get the topic forum id
1259 * @uses bbp_update_reply_forum_id() To update the reply forum id
1260 * @uses do_action() Calls 'bbp_split_topic_reply' with the reply id and
1261 * destination topic id
1262 * @uses bbp_update_topic_last_reply_id() To update the topic last reply id
1263 * @uses bbp_update_topic_last_active_time() To update the topic last active meta
1264 * @uses do_action() Calls 'bbp_post_split_topic' with the destination and
1265 * source topic ids and source topic's forum id
1266 * @uses bbp_get_topic_permalink() To get the topic permalink
1267 * @uses wp_redirect() To redirect to the topic link
1268 */
1269 function bbp_split_topic_handler() {
1270
1271 // Bail if not a POST action
1272 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1273 return;
1274
1275 // Bail if action is not 'bbp-split-topic'
1276 if ( empty( $_POST['action'] ) || ( 'bbp-split-topic' !== $_POST['action'] ) )
1277 return;
1278
1279 global $wpdb;
1280
1281 // Prevent debug notices
1282 $from_reply_id = $destination_topic_id = 0;
1283 $destination_topic_title = '';
1284 $destination_topic = $from_reply = $source_topic = '';
1285 $split_option = false;
1286
1287 /** Split Reply ***********************************************************/
1288
1289 if ( empty( $_POST['bbp_reply_id'] ) )
1290 bbp_add_error( 'bbp_split_topic_reply_id', __( '<strong>ERROR</strong>: Reply ID to split the topic from not found!', 'bbpress' ) );
1291 else
1292 $from_reply_id = (int) $_POST['bbp_reply_id'];
1293
1294 $from_reply = bbp_get_reply( $from_reply_id );
1295
1296 // Reply exists
1297 if ( empty( $from_reply ) )
1298 bbp_add_error( 'bbp_split_topic_r_not_found', __( '<strong>ERROR</strong>: The reply you want to split from was not found.', 'bbpress' ) );
1299
1300 /** Topic to Split ********************************************************/
1301
1302 // Get the topic being split
1303 $source_topic = bbp_get_topic( $from_reply->post_parent );
1304
1305 // No topic
1306 if ( empty( $source_topic ) )
1307 bbp_add_error( 'bbp_split_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to split was not found.', 'bbpress' ) );
1308
1309 // Nonce check
1310 check_admin_referer( 'bbp-split-topic_' . $source_topic->ID );
1311
1312 // Use cannot edit topic
1313 if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
1314 bbp_add_error( 'bbp_split_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
1315
1316 /** How to Split **********************************************************/
1317
1318 if ( !empty( $_POST['bbp_topic_split_option'] ) )
1319 $split_option = (string) trim( $_POST['bbp_topic_split_option'] );
1320
1321 // Invalid split option
1322 if ( empty( $split_option ) || !in_array( $split_option, array( 'existing', 'reply' ) ) ) {
1323 bbp_add_error( 'bbp_split_topic_option', __( '<strong>ERROR</strong>: You need to choose a valid split option.', 'bbpress' ) );
1324
1325 // Valid Split Option
1326 } else {
1327
1328 // What kind of split
1329 switch ( $split_option ) {
1330
1331 // Into an existing topic
1332 case 'existing' :
1333
1334 // Get destination topic id
1335 if ( empty( $_POST['bbp_destination_topic'] ) )
1336 bbp_add_error( 'bbp_split_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found!', 'bbpress' ) );
1337 else
1338 $destination_topic_id = (int) $_POST['bbp_destination_topic'];
1339
1340 // Get the destination topic
1341 $destination_topic = bbp_get_topic( $destination_topic_id );
1342
1343 // No destination topic
1344 if ( empty( $destination_topic ) )
1345 bbp_add_error( 'bbp_split_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to split to was not found!', 'bbpress' ) );
1346
1347 // User cannot edit the destination topic
1348 if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
1349 bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', 'bbpress' ) );
1350
1351 break;
1352
1353 // Split at reply into a new topic
1354 case 'reply' :
1355 default :
1356
1357 // User needs to be able to publish topics
1358 if ( current_user_can( 'publish_topics' ) ) {
1359
1360 // Use the new title that was passed
1361 if ( !empty( $_POST['bbp_topic_split_destination_title'] ) ) {
1362 $destination_topic_title = esc_attr( strip_tags( $_POST['bbp_topic_split_destination_title'] ) );
1363
1364 // Use the source topic title
1365 } else {
1366 $destination_topic_title = $source_topic->post_title;
1367 }
1368
1369 // Setup the updated topic parameters
1370 $postarr = array(
1371 'ID' => $from_reply->ID,
1372 'post_title' => $destination_topic_title,
1373 'post_name' => false,
1374 'post_type' => bbp_get_topic_post_type(),
1375 'post_parent' => $source_topic->post_parent,
1376 'guid' => ''
1377 );
1378
1379 // Update the topic
1380 $destination_topic_id = wp_update_post( $postarr );
1381 $destination_topic = bbp_get_topic( $destination_topic_id );
1382
1383 // Make sure the new topic knows its a topic
1384 bbp_update_topic_topic_id( $from_reply->ID );
1385
1386 // Shouldn't happen
1387 if ( false == $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) {
1388 bbp_add_error( 'bbp_split_topic_destination_reply', __( '<strong>ERROR</strong>: There was a problem converting the reply into the topic. Please try again.', 'bbpress' ) );
1389 }
1390
1391 // User cannot publish posts
1392 } else {
1393 bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to create new topics. The reply could not be converted into a topic.', 'bbpress' ) );
1394 }
1395
1396 break;
1397 }
1398 }
1399
1400 /** No Errors - Do the Spit ***********************************************/
1401
1402 if ( !bbp_has_errors() ) {
1403
1404 // Update counts, etc...
1405 do_action( 'bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
1406
1407 /** Subscriptions *****************************************************/
1408
1409 // Copy the subscribers
1410 if ( !empty( $_POST['bbp_topic_subscribers'] ) && 1 == $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active() ) {
1411
1412 // Get the subscribers
1413 $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
1414
1415 if ( !empty( $subscribers ) ) {
1416
1417 // Add subscribers to new topic
1418 foreach ( (array) $subscribers as $subscriber ) {
1419 bbp_add_user_subscription( $subscriber, $destination_topic->ID );
1420 }
1421 }
1422 }
1423
1424 /** Favorites *********************************************************/
1425
1426 // Copy the favoriters if told to
1427 if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] ) {
1428
1429 // Get the favoriters
1430 $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
1431
1432 if ( !empty( $favoriters ) ) {
1433
1434 // Add the favoriters to new topic
1435 foreach ( (array) $favoriters as $favoriter ) {
1436 bbp_add_user_favorite( $favoriter, $destination_topic->ID );
1437 }
1438 }
1439 }
1440
1441 /** Tags **************************************************************/
1442
1443 // Copy the tags if told to
1444 if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) ) {
1445
1446 // Get the source topic tags
1447 $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
1448
1449 if ( !empty( $source_topic_tags ) ) {
1450 wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
1451 }
1452 }
1453
1454 /** Split Replies *************************************************/
1455
1456 // get_posts() is not used because it doesn't allow us to use '>='
1457 // comparision without a filter.
1458 $replies = (array) $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE {$wpdb->posts}.post_date >= %s AND {$wpdb->posts}.post_parent = %d AND {$wpdb->posts}.post_type = %s ORDER BY {$wpdb->posts}.post_date ASC", $from_reply->post_date, $source_topic->ID, bbp_get_reply_post_type() ) );
1459
1460 // Make sure there are replies to loop through
1461 if ( !empty( $replies ) && !is_wp_error( $replies ) ) {
1462
1463 // Change the post_parent of each reply to the destination topic id
1464 foreach ( $replies as $reply ) {
1465
1466 // New reply data
1467 $postarr = array(
1468 'ID' => $reply->ID,
1469 'post_title' => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
1470 'post_name' => false, // will be automatically generated
1471 'post_parent' => $destination_topic->ID,
1472 'guid' => ''
1473 );
1474
1475 // Update the reply
1476 wp_update_post( $postarr );
1477
1478 // Adjust reply meta values
1479 bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID );
1480 bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
1481
1482 // Do additional actions per split reply
1483 do_action( 'bbp_split_topic_reply', $reply->ID, $destination_topic->ID );
1484 }
1485 }
1486
1487 // It is a new topic and we need to set some default metas to make
1488 // the topic display in bbp_has_topics() list
1489 if ( 'reply' == $split_option ) {
1490 $last_reply_id = ( empty( $reply ) || empty( $reply->ID ) ) ? 0 : $reply->ID;
1491 $freshness = ( empty( $reply ) || empty( $reply->post_date ) ) ? '' : $reply->post_date;
1492
1493 bbp_update_topic_last_reply_id ( $destination_topic->ID, $last_reply_id );
1494 bbp_update_topic_last_active_time( $destination_topic->ID, $freshness );
1495 }
1496
1497 /** Successful Split **************************************************/
1498
1499 // Update counts, etc...
1500 do_action( 'bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
1501
1502 // Redirect back to the topic
1503 wp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
1504
1505 // For good measure
1506 exit();
1507 }
1508 }
1509
1510 /**
1511 * Fix counts on topic split
1512 *
1513 * When a topic is split, update the counts of source and destination topic
1514 * and their forums.
1515 *
1516 * @since bbPress (r2756)
1517 *
1518 * @param int $from_reply_id From reply id
1519 * @param int $source_topic_id Source topic id
1520 * @param int $destination_topic_id Destination topic id
1521 * @uses bbp_update_forum_topic_count() To update the forum topic counts
1522 * @uses bbp_update_forum_reply_count() To update the forum reply counts
1523 * @uses bbp_update_topic_reply_count() To update the topic reply counts
1524 * @uses bbp_update_topic_voice_count() To update the topic voice counts
1525 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
1526 * count
1527 * @uses do_action() Calls 'bbp_split_topic_count' with the from reply id,
1528 * source topic id & destination topic id
1529 */
1530 function bbp_split_topic_count( $from_reply_id, $source_topic_id, $destination_topic_id ) {
1531
1532 // Forum Topic Counts
1533 bbp_update_forum_topic_count( $destination_topic_id );
1534
1535 // Forum Reply Counts
1536 bbp_update_forum_reply_count( $destination_topic_id );
1537
1538 // Topic Reply Counts
1539 bbp_update_topic_reply_count( $source_topic_id );
1540 bbp_update_topic_reply_count( $destination_topic_id );
1541
1542 // Topic Hidden Reply Counts
1543 bbp_update_topic_reply_count_hidden( $source_topic_id );
1544 bbp_update_topic_reply_count_hidden( $destination_topic_id );
1545
1546 // Topic Voice Counts
1547 bbp_update_topic_voice_count( $source_topic_id );
1548 bbp_update_topic_voice_count( $destination_topic_id );
1549
1550 do_action( 'bbp_split_topic_count', $from_reply_id, $source_topic_id, $destination_topic_id );
1551 }
1552
1553 /**
1554 * Handles the front end tag management (renaming, merging, destroying)
1555 *
1556 * @since bbPress (r2768)
1557 *
1558 * @uses check_admin_referer() To verify the nonce and check the referer
1559 * @uses current_user_can() To check if the current user can edit/delete tags
1560 * @uses bbPress::errors::add() To log the error messages
1561 * @uses wp_update_term() To update the topic tag
1562 * @uses get_term_link() To get the topic tag url
1563 * @uses term_exists() To check if the topic tag already exists
1564 * @uses wp_insert_term() To insert a topic tag
1565 * @uses wp_delete_term() To delete the topic tag
1566 * @uses home_url() To get the blog's home page url
1567 * @uses do_action() Calls actions based on the actions with associated args
1568 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
1569 * @uses wp_redirect() To redirect to the url
1570 */
1571 function bbp_manage_topic_tag_handler() {
1572
1573 // Bail if not a POST action
1574 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1575 return;
1576
1577 // Bail if required POST actions aren't passed
1578 if ( empty( $_POST['tag-id'] ) || empty( $_POST['action'] ) )
1579 return;
1580
1581 // Setup possible get actions
1582 $possible_actions = array(
1583 'bbp-update-topic-tag',
1584 'bbp-merge-topic-tag',
1585 'bbp-delete-topic-tag'
1586 );
1587
1588 // Bail if actions aren't meant for this function
1589 if ( !in_array( $_POST['action'], $possible_actions ) )
1590 return;
1591
1592 // Setup vars
1593 $action = $_POST['action'];
1594 $tag_id = (int) $_POST['tag-id'];
1595 $tag = get_term( $tag_id, bbp_get_topic_tag_tax_id() );
1596
1597 // Tag does not exist
1598 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1599 bbp_add_error( 'bbp_manage_topic_invalid_tag', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while getting the tag: %s', 'bbpress' ), $tag->get_error_message() ) );
1600 return;
1601 }
1602
1603 // What action are we trying to perform?
1604 switch ( $action ) {
1605
1606 // Update tag
1607 case 'bbp-update-topic-tag' :
1608
1609 // Nonce check
1610 check_admin_referer( 'update-tag_' . $tag_id );
1611
1612 // Can user edit topic tags?
1613 if ( !current_user_can( 'edit_topic_tags' ) ) {
1614 bbp_add_error( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
1615 return;
1616 }
1617
1618 // No tag name was provided
1619 if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
1620 bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
1621 return;
1622 }
1623
1624 // Attempt to update the tag
1625 $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : '';
1626 $tag = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) );
1627
1628 // Cannot update tag
1629 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1630 bbp_add_error( 'bbp_manage_topic_tag_update_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while updating the tag: %s', 'bbpress' ), $tag->get_error_message() ) );
1631 return;
1632 }
1633
1634 // Redirect
1635 $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() );
1636
1637 // Update counts, etc...
1638 do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug );
1639
1640 break;
1641
1642 // Merge two tags
1643 case 'bbp-merge-topic-tag' :
1644
1645 // Nonce check
1646 check_admin_referer( 'merge-tag_' . $tag_id );
1647
1648 // Can user edit topic tags?
1649 if ( !current_user_can( 'edit_topic_tags' ) ) {
1650 bbp_add_error( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
1651 return;
1652 }
1653
1654 // No tag name was provided
1655 if ( empty( $_POST['tag-existing-name'] ) || !$name = $_POST['tag-existing-name'] ) {
1656 bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
1657 return;
1658 }
1659
1660 // If term does not exist, create it
1661 if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) )
1662 $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() );
1663
1664 // Problem inserting the new term
1665 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1666 bbp_add_error( 'bbp_manage_topic_tag_merge_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while merging the tags: %s', 'bbpress' ), $tag->get_error_message() ) );
1667 return;
1668 }
1669
1670 // Merging in to...
1671 $to_tag = $tag['term_id'];
1672
1673 // Attempting to merge a tag into itself
1674 if ( $tag_id == $to_tag ) {
1675 bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'bbpress' ) );
1676 return;
1677 }
1678
1679 // Delete the old term
1680 $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) );
1681
1682 // Error merging the terms
1683 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1684 bbp_add_error( 'bbp_manage_topic_tag_merge_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while merging the tags: %s', 'bbpress' ), $tag->get_error_message() ) );
1685 return;
1686 }
1687
1688 // Redirect
1689 $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() );
1690
1691 // Update counts, etc...
1692 do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag );
1693
1694 break;
1695
1696 // Delete tag
1697 case 'bbp-delete-topic-tag' :
1698
1699 // Nonce check
1700 check_admin_referer( 'delete-tag_' . $tag_id );
1701
1702 // Can user delete topic tags?
1703 if ( !current_user_can( 'delete_topic_tags' ) ) {
1704 bbp_add_error( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the topic tags.', 'bbpress' ) );
1705 return;
1706 }
1707
1708 // Attempt to delete term
1709 $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() );
1710
1711 // Error deleting term
1712 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1713 bbp_add_error( 'bbp_manage_topic_tag_delete_error', sprintf( __( '<strong>ERROR</strong>: The following problem(s) have been found while deleting the tag: %s', 'bbpress' ), $tag->get_error_message() ) );
1714 return;
1715 }
1716
1717 // We don't have any other place to go other than home! Or we may die because of the 404 disease
1718 $redirect = home_url();
1719
1720 // Update counts, etc...
1721 do_action( 'bbp_delete_topic_tag', $tag_id, $tag );
1722
1723 break;
1724 }
1725
1726 /** Successful Moderation *************************************************/
1727
1728 // Redirect back
1729 $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url();
1730 wp_safe_redirect( $redirect );
1731
1732 // For good measure
1733 exit();
1734 }
1735
1736 /** Stickies ******************************************************************/
1737
1738 /**
1739 * Return sticky topics of a forum
1740 *
1741 * @since bbPress (r2592)
1742 *
1743 * @param int $forum_id Optional. If not passed, super stickies are returned.
1744 * @uses bbp_get_super_stickies() To get the super stickies
1745 * @uses get_post_meta() To get the forum stickies
1746 * @uses apply_filters() Calls 'bbp_get_stickies' with the stickies and forum id
1747 * @return array IDs of sticky topics of a forum or super stickies
1748 */
1749 function bbp_get_stickies( $forum_id = 0 ) {
1750 $stickies = empty( $forum_id ) ? bbp_get_super_stickies() : get_post_meta( $forum_id, '_bbp_sticky_topics', true );
1751 $stickies = ( empty( $stickies ) || !is_array( $stickies ) ) ? array() : $stickies;
1752
1753 return apply_filters( 'bbp_get_stickies', $stickies, (int) $forum_id );
1754 }
1755
1756 /**
1757 * Return topics stuck to front page of the forums
1758 *
1759 * @since bbPress (r2592)
1760 *
1761 * @uses get_option() To get super sticky topics
1762 * @uses apply_filters() Calls 'bbp_get_super_stickies' with the stickies
1763 * @return array IDs of super sticky topics
1764 */
1765 function bbp_get_super_stickies() {
1766 $stickies = get_option( '_bbp_super_sticky_topics', array() );
1767 $stickies = ( empty( $stickies ) || !is_array( $stickies ) ) ? array() : $stickies;
1768
1769 return apply_filters( 'bbp_get_super_stickies', $stickies );
1770 }
1771
1772 /** Topics Actions ************************************************************/
1773
1774 /**
1775 * Handles the front end opening/closing, spamming/unspamming,
1776 * sticking/unsticking and trashing/untrashing/deleting of topics
1777 *
1778 * @since bbPress (r2727)
1779 *
1780 * @uses bbp_get_topic() To get the topic
1781 * @uses current_user_can() To check if the user is capable of editing or
1782 * deleting the topic
1783 * @uses bbp_get_topic_post_type() To get the topic post type
1784 * @uses check_ajax_referer() To verify the nonce and check the referer
1785 * @uses bbp_is_topic_open() To check if the topic is open
1786 * @uses bbp_close_topic() To close the topic
1787 * @uses bbp_open_topic() To open the topic
1788 * @uses bbp_is_topic_sticky() To check if the topic is a sticky
1789 * @uses bbp_unstick_topic() To unstick the topic
1790 * @uses bbp_stick_topic() To stick the topic
1791 * @uses bbp_is_topic_spam() To check if the topic is marked as spam
1792 * @uses bbp_spam_topic() To make the topic as spam
1793 * @uses bbp_unspam_topic() To unmark the topic as spam
1794 * @uses wp_trash_post() To trash the topic
1795 * @uses wp_untrash_post() To untrash the topic
1796 * @uses wp_delete_post() To delete the topic
1797 * @uses do_action() Calls 'bbp_toggle_topic_handler' with success, post data
1798 * and action
1799 * @uses bbp_get_forum_permalink() To get the forum link
1800 * @uses bbp_get_topic_permalink() To get the topic link
1801 * @uses add_query_arg() To add args to the url
1802 * @uses wp_redirect() To redirect to the topic
1803 * @uses bbPress::errors:add() To log the error messages
1804 */
1805 function bbp_toggle_topic_handler() {
1806
1807 // Bail if not a GET action
1808 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1809 return;
1810
1811 // Bail if required GET actions aren't passed
1812 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
1813 return;
1814
1815 // Setup possible get actions
1816 $possible_actions = array(
1817 'bbp_toggle_topic_close',
1818 'bbp_toggle_topic_stick',
1819 'bbp_toggle_topic_spam',
1820 'bbp_toggle_topic_trash'
1821 );
1822
1823 // Bail if actions aren't meant for this function
1824 if ( !in_array( $_GET['action'], $possible_actions ) )
1825 return;
1826
1827 $view_all = false; // Assume not viewing all
1828 $action = $_GET['action']; // What action is taking place?
1829 $topic_id = (int) $_GET['topic_id']; // What's the topic id?
1830 $success = false; // Flag
1831 $post_data = array( 'ID' => $topic_id ); // Prelim array
1832
1833 // Make sure topic exists
1834 if ( !$topic = bbp_get_topic( $topic_id ) )
1835 return;
1836
1837 // What is the user doing here?
1838 if ( !current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' == $action && !current_user_can( 'delete_topic', $topic->ID ) ) ) {
1839 bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that.', 'bbpress' ) );
1840 return;
1841 }
1842
1843 // What action are we trying to perform?
1844 switch ( $action ) {
1845
1846 // Toggle open/close
1847 case 'bbp_toggle_topic_close' :
1848 check_ajax_referer( 'close-topic_' . $topic_id );
1849
1850 $is_open = bbp_is_topic_open( $topic_id );
1851 $success = $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id );
1852 $failure = $is_open ? __( '<strong>ERROR</strong>: There was a problem closing the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem opening the topic.', 'bbpress' );
1853
1854 break;
1855
1856 // Toggle sticky/super-sticky/unstick
1857 case 'bbp_toggle_topic_stick' :
1858 check_ajax_referer( 'stick-topic_' . $topic_id );
1859
1860 $is_sticky = bbp_is_topic_sticky( $topic_id );
1861 $is_super = ( empty( $is_sticky ) && !empty( $_GET['super'] ) && 1 == (int) $_GET['super'] ) ? true : false;
1862 $success = $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );
1863 $failure = $is_sticky ? __( '<strong>ERROR</strong>: There was a problem unsticking the topic.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem sticking the topic.', 'bbpress' );
1864
1865 break;
1866
1867 // Toggle spam
1868 case 'bbp_toggle_topic_spam' :
1869 check_ajax_referer( 'spam-topic_' . $topic_id );
1870
1871 $is_spam = bbp_is_topic_spam( $topic_id );
1872 $success = $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id );
1873 $failure = $is_spam ? __( '<strong>ERROR</strong>: There was a problem unmarking the topic as spam.', 'bbpress' ) : __( '<strong>ERROR</strong>: There was a problem marking the topic as spam.', 'bbpress' );
1874 $view_all = !$is_spam;
1875
1876 break;
1877
1878 // Toggle trash
1879 case 'bbp_toggle_topic_trash' :
1880
1881 $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
1882
1883 if ( empty( $sub_action ) )
1884 break;
1885
1886 switch ( $sub_action ) {
1887 case 'trash':
1888 check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id );
1889
1890 $view_all = true;
1891 $success = wp_trash_post( $topic_id );
1892 $failure = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
1893
1894 break;
1895
1896 case 'untrash':
1897 check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id );
1898
1899 $success = wp_untrash_post( $topic_id );
1900 $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
1901
1902 break;
1903
1904 case 'delete':
1905 check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id );
1906
1907 $success = wp_delete_post( $topic_id );
1908 $failure = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
1909
1910 break;
1911 }
1912
1913 break;
1914 }
1915
1916 // Do additional topic toggle actions
1917 do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action );
1918
1919 // No errors
1920 if ( false != $success && !is_wp_error( $success ) ) {
1921
1922 // Redirect back to the topic's forum
1923 if ( isset( $sub_action ) && ( 'delete' == $sub_action ) ) {
1924 $redirect = bbp_get_forum_permalink( $success->post_parent );
1925
1926 // Redirect back to the topic
1927 } else {
1928
1929 // Get the redirect detination
1930 $permalink = bbp_get_topic_permalink( $topic_id );
1931 $redirect = bbp_add_view_all( $permalink, $view_all );
1932 }
1933
1934 wp_redirect( $redirect );
1935
1936 // For good measure
1937 exit();
1938
1939 // Handle errors
1940 } else {
1941 bbp_add_error( 'bbp_toggle_topic', $failure );
1942 }
1943 }
1944
1945 /** Favorites & Subscriptions *************************************************/
1946
1947 /**
1948 * Remove a deleted topic from all users' favorites
1949 *
1950 * @since bbPress (r2652)
1951 *
1952 * @param int $topic_id Topic ID to remove
1953 * @uses bbp_get_topic_favoriters() To get the topic's favoriters
1954 * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
1955 */
1956 function bbp_remove_topic_from_all_favorites( $topic_id = 0 ) {
1957 $topic_id = bbp_get_topic_id( $topic_id );
1958
1959 // Bail if no topic
1960 if ( empty( $topic_id ) )
1961 return;
1962
1963 // Get users
1964 $users = (array) bbp_get_topic_favoriters( $topic_id );
1965
1966 // Users exist
1967 if ( !empty( $users ) ) {
1968
1969 // Loop through users
1970 foreach ( $users as $user ) {
1971
1972 // Remove each user
1973 bbp_remove_user_favorite( $user, $topic_id );
1974 }
1975 }
1976 }
1977
1978 /**
1979 * Remove a deleted topic from all users' subscriptions
1980 *
1981 * @since bbPress (r2652)
1982 *
1983 * @param int $topic_id Topic ID to remove
1984 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
1985 * @uses bbp_get_topic_subscribers() To get the topic subscribers
1986 * @uses bbp_remove_user_subscription() To remove the user subscription
1987 */
1988 function bbp_remove_topic_from_all_subscriptions( $topic_id = 0 ) {
1989
1990 // Subscriptions are not active
1991 if ( !bbp_is_subscriptions_active() )
1992 return;
1993
1994 $topic_id = bbp_get_topic_id( $topic_id );
1995
1996 // Bail if no topic
1997 if ( empty( $topic_id ) )
1998 return;
1999
2000 // Get users
2001 $users = (array) bbp_get_topic_subscribers( $topic_id );
2002
2003 // Users exist
2004 if ( !empty( $users ) ) {
2005
2006 // Loop through users
2007 foreach ( $users as $user ) {
2008
2009 // Remove each user
2010 bbp_remove_user_subscription( $user, $topic_id );
2011 }
2012 }
2013 }
2014
2015 /** Topic Updaters ************************************************************/
2016
2017 /**
2018 * Update the topic's forum id
2019 *
2020 * @since bbPress (r2855)
2021 *
2022 * @param int $topic_id Optional. Topic id to update
2023 * @param int $forum_id Optional. Forum id
2024 * @uses bbp_is_reply() TO check if the passed topic id is a reply
2025 * @uses bbp_get_reply_topic_id() To get the reply topic id
2026 * @uses bbp_get_topic_id() To get the topic id
2027 * @uses get_post_field() To get the post parent of the topic id
2028 * @uses bbp_get_forum_id() To get the forum id
2029 * @uses update_post_meta() To update the topic forum id meta
2030 * @uses apply_filters() Calls 'bbp_update_topic_forum_id' with the forum id
2031 * and topic id
2032 * @return int Forum id
2033 */
2034 function bbp_update_topic_forum_id( $topic_id = 0, $forum_id = 0 ) {
2035
2036 // If it's a reply, then get the parent (topic id)
2037 if ( bbp_is_reply( $topic_id ) )
2038 $topic_id = bbp_get_reply_topic_id( $topic_id );
2039 else
2040 $topic_id = bbp_get_topic_id( $topic_id );
2041
2042 if ( empty( $forum_id ) )
2043 $forum_id = get_post_field( 'post_parent', $topic_id );
2044
2045 update_post_meta( $topic_id, '_bbp_forum_id', (int) $forum_id );
2046
2047 return apply_filters( 'bbp_update_topic_forum_id', (int) $forum_id, $topic_id );
2048 }
2049
2050 /**
2051 * Update the topic's topic id
2052 *
2053 * @since bbPress (r2954)
2054 *
2055 * @param int $topic_id Optional. Topic id to update
2056 * @uses bbp_get_topic_id() To get the topic id
2057 * @uses update_post_meta() To update the topic's topic id meta
2058 * @uses apply_filters() Calls 'bbp_update_topic_topic_id' with the topic id
2059 * @return int Topic id
2060 */
2061 function bbp_update_topic_topic_id( $topic_id = 0 ) {
2062 $topic_id = bbp_get_topic_id( $topic_id );
2063
2064 update_post_meta( $topic_id, '_bbp_topic_id', (int) $topic_id );
2065
2066 return apply_filters( 'bbp_update_topic_topic_id', (int) $topic_id );
2067 }
2068
2069 /**
2070 * Adjust the total reply count of a topic
2071 *
2072 * @since bbPress (r2467)
2073 *
2074 * @param int $topic_id Optional. Topic id to update
2075 * @param int $reply_count Optional. Set the reply count manually.
2076 * @uses bbp_is_reply() To check if the passed topic id is a reply
2077 * @uses bbp_get_reply_topic_id() To get the reply topic id
2078 * @uses bbp_get_topic_id() To get the topic id
2079 * @uses bbp_get_reply_post_type() To get the reply post type
2080 * @uses bbp_get_public_child_count() To get the reply count
2081 * @uses update_post_meta() To update the topic reply count meta
2082 * @uses apply_filters() Calls 'bbp_update_topic_reply_count' with the reply
2083 * count and topic id
2084 * @return int Topic reply count
2085 */
2086 function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = 0 ) {
2087
2088 // If it's a reply, then get the parent (topic id)
2089 if ( bbp_is_reply( $topic_id ) )
2090 $topic_id = bbp_get_reply_topic_id( $topic_id );
2091 else
2092 $topic_id = bbp_get_topic_id( $topic_id );
2093
2094 // Get replies of topic if not passed
2095 if ( empty( $reply_count ) )
2096 $reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
2097
2098 update_post_meta( $topic_id, '_bbp_reply_count', (int) $reply_count );
2099
2100 return apply_filters( 'bbp_update_topic_reply_count', (int) $reply_count, $topic_id );
2101 }
2102
2103 /**
2104 * Adjust the total hidden reply count of a topic (hidden includes trashed and spammed replies)
2105 *
2106 * @since bbPress (r2740)
2107 *
2108 * @param int $topic_id Optional. Topic id to update
2109 * @param int $reply_count Optional. Set the reply count manually
2110 * @uses bbp_is_reply() To check if the passed topic id is a reply
2111 * @uses bbp_get_reply_topic_id() To get the reply topic id
2112 * @uses bbp_get_topic_id() To get the topic id
2113 * @uses bbp_get_reply_post_type() To get the reply post type
2114 * @uses wpdb::prepare() To prepare our sql query
2115 * @uses wpdb::get_var() To execute our query and get the var back
2116 * @uses update_post_meta() To update the topic hidden reply count meta
2117 * @uses apply_filters() Calls 'bbp_update_topic_reply_count_hidden' with the
2118 * hidden reply count and topic id
2119 * @return int Topic hidden reply count
2120 */
2121 function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
2122 global $wpdb;
2123
2124 // If it's a reply, then get the parent (topic id)
2125 if ( bbp_is_reply( $topic_id ) )
2126 $topic_id = bbp_get_reply_topic_id( $topic_id );
2127 else
2128 $topic_id = bbp_get_topic_id( $topic_id );
2129
2130 // Get replies of topic
2131 if ( empty( $reply_count ) )
2132 $reply_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( bbp_get_trash_status_id(), bbp_get_spam_status_id() ) ) . "') AND post_type = '%s';", $topic_id, bbp_get_reply_post_type() ) );
2133
2134 update_post_meta( $topic_id, '_bbp_reply_count_hidden', (int) $reply_count );
2135
2136 return apply_filters( 'bbp_update_topic_reply_count_hidden', (int) $reply_count, $topic_id );
2137 }
2138
2139 /**
2140 * Update the topic with the last active post ID
2141 *
2142 * @since bbPress (r2888)
2143 *
2144 * @param int $topic_id Optional. Topic id to update
2145 * @param int $active_id Optional. active id
2146 * @uses bbp_is_reply() To check if the passed topic id is a reply
2147 * @uses bbp_get_reply_topic_id() To get the reply topic id
2148 * @uses bbp_get_topic_id() To get the topic id
2149 * @uses bbp_get_reply_post_type() To get the reply post type
2150 * @uses bbp_get_public_child_last_id() To get the last public reply id
2151 * @uses bbp_get_active_id() To get the active id
2152 * @uses update_post_meta() To update the topic last active id meta
2153 * @uses apply_filters() Calls 'bbp_update_topic_last_active_id' with the active
2154 * id and topic id
2155 * @return int Active id
2156 */
2157 function bbp_update_topic_last_active_id( $topic_id = 0, $active_id = 0 ) {
2158
2159 // If it's a reply, then get the parent (topic id)
2160 if ( bbp_is_reply( $topic_id ) )
2161 $topic_id = bbp_get_reply_topic_id( $topic_id );
2162 else
2163 $topic_id = bbp_get_topic_id( $topic_id );
2164
2165 if ( empty( $active_id ) )
2166 $active_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
2167
2168 // Adjust last_id's based on last_reply post_type
2169 if ( empty( $active_id ) || !bbp_is_reply( $active_id ) )
2170 $active_id = $topic_id;
2171
2172 // Update only if published
2173 if ( bbp_get_public_status_id() == get_post_status( $active_id ) )
2174 update_post_meta( $topic_id, '_bbp_last_active_id', (int) $active_id );
2175
2176 return apply_filters( 'bbp_update_topic_last_active_id', (int) $active_id, $topic_id );
2177 }
2178
2179 /**
2180 * Update the topics last active date/time (aka freshness)
2181 *
2182 * @since bbPress (r2680)
2183 *
2184 * @param int $topic_id Optional. Topic id
2185 * @param string $new_time Optional. New time in mysql format
2186 * @uses bbp_get_topic_id() To get the topic id
2187 * @uses bbp_get_reply_topic_id() To get the reply topic id
2188 * @uses current_time() To get the current time
2189 * @uses update_post_meta() To update the topic last active meta
2190 * @return bool True on success, false on failure
2191 */
2192 function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) {
2193
2194 // If it's a reply, then get the parent (topic id)
2195 if ( bbp_is_reply( $topic_id ) )
2196 $topic_id = bbp_get_reply_topic_id( $topic_id );
2197 else
2198 $topic_id = bbp_get_topic_id( $topic_id );
2199
2200 // Check time and use current if empty
2201 if ( empty( $new_time ) )
2202 $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) );
2203
2204 // Update only if published
2205 if ( !empty( $new_time ) )
2206 update_post_meta( $topic_id, '_bbp_last_active_time', $new_time );
2207
2208 return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id );
2209 }
2210
2211 /**
2212 * Update the topic with the most recent reply ID
2213 *
2214 * @since bbPress (r2625)
2215 *
2216 * @param int $topic_id Optional. Topic id to update
2217 * @param int $reply_id Optional. Reply id
2218 * @uses bbp_is_reply() To check if the passed topic id is a reply
2219 * @uses bbp_get_reply_id() To get the reply id
2220 * @uses bbp_get_reply_topic_id() To get the reply topic id
2221 * @uses bbp_get_topic_id() To get the topic id
2222 * @uses bbp_get_reply_post_type() To get the reply post type
2223 * @uses bbp_get_public_child_last_id() To get the last public reply id
2224 * @uses update_post_meta() To update the topic last reply id meta
2225 * @uses apply_filters() Calls 'bbp_update_topic_last_reply_id' with the reply
2226 * id and topic id
2227 * @return int Reply id
2228 */
2229 function bbp_update_topic_last_reply_id( $topic_id = 0, $reply_id = 0 ) {
2230
2231 // If it's a reply, then get the parent (topic id)
2232 if ( empty( $reply_id ) && bbp_is_reply( $topic_id ) ) {
2233 $reply_id = bbp_get_reply_id( $topic_id );
2234 $topic_id = bbp_get_reply_topic_id( $reply_id );
2235 } else {
2236 $reply_id = bbp_get_reply_id( $reply_id );
2237 $topic_id = bbp_get_topic_id( $topic_id );
2238 }
2239
2240 if ( empty( $reply_id ) )
2241 $reply_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
2242
2243 // Adjust last_id's based on last_reply post_type
2244 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
2245 $reply_id = 0;
2246
2247 // Update if reply is published
2248 if ( bbp_is_reply_published( $reply_id ) )
2249 update_post_meta( $topic_id, '_bbp_last_reply_id', (int) $reply_id );
2250
2251 return apply_filters( 'bbp_update_topic_last_reply_id', (int) $reply_id, $topic_id );
2252 }
2253
2254 /**
2255 * Adjust the total voice count of a topic
2256 *
2257 * @since bbPress (r2567)
2258 *
2259 * @param int $topic_id Optional. Topic id to update
2260 * @uses bbp_is_reply() To check if the passed topic id is a reply
2261 * @uses bbp_get_reply_topic_id() To get the reply topic id
2262 * @uses bbp_get_topic_id() To get the topic id
2263 * @uses bbp_get_reply_topic_id() To get the reply topic id
2264 * @uses bbp_get_reply_post_type() To get the reply post type
2265 * @uses bbp_get_topic_post_type() To get the topic post type
2266 * @uses wpdb::prepare() To prepare our sql query
2267 * @uses wpdb::get_col() To execute our query and get the column back
2268 * @uses update_post_meta() To update the topic voice count meta
2269 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
2270 * count and topic id
2271 * @return int Voice count
2272 */
2273 function bbp_update_topic_voice_count( $topic_id = 0 ) {
2274 global $wpdb;
2275
2276 // If it's a reply, then get the parent (topic id)
2277 if ( bbp_is_reply( $topic_id ) )
2278 $topic_id = bbp_get_reply_topic_id( $topic_id );
2279 elseif ( bbp_is_topic( $topic_id ) )
2280 $topic_id = bbp_get_topic_id( $topic_id );
2281 else
2282 return;
2283
2284 // Query the DB to get voices in this topic
2285 $voices = $wpdb->get_col( $wpdb->prepare( "SELECT COUNT( DISTINCT post_author ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' ) OR ( ID = %d AND post_type = '%s' );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
2286
2287 // If there's an error, make sure we at least have 1 voice
2288 $voices = ( empty( $voices ) || is_wp_error( $voices ) ) ? 1 : $voices[0];
2289
2290 // Update the voice count for this topic id
2291 update_post_meta( $topic_id, '_bbp_voice_count', (int) $voices );
2292
2293 return apply_filters( 'bbp_update_topic_voice_count', (int) $voices, $topic_id );
2294 }
2295
2296 /**
2297 * Adjust the total anonymous reply count of a topic
2298 *
2299 * @since bbPress (r2567)
2300 *
2301 * @param int $topic_id Optional. Topic id to update
2302 * @uses bbp_is_reply() To check if the passed topic id is a reply
2303 * @uses bbp_get_reply_topic_id() To get the reply topic id
2304 * @uses bbp_get_topic_id() To get the topic id
2305 * @uses bbp_get_reply_topic_id() To get the reply topic id
2306 * @uses bbp_get_reply_post_type() To get the reply post type
2307 * @uses bbp_get_topic_post_type() To get the topic post type
2308 * @uses wpdb::prepare() To prepare our sql query
2309 * @uses wpdb::get_col() To execute our query and get the column back
2310 * @uses update_post_meta() To update the topic anonymous reply count meta
2311 * @uses apply_filters() Calls 'bbp_update_topic_anonymous_reply_count' with the
2312 * anonymous reply count and topic id
2313 * @return int Anonymous reply count
2314 */
2315 function bbp_update_topic_anonymous_reply_count( $topic_id = 0 ) {
2316 global $wpdb;
2317
2318 // If it's a reply, then get the parent (topic id)
2319 if ( bbp_is_reply( $topic_id ) )
2320 $topic_id = bbp_get_reply_topic_id( $topic_id );
2321 elseif ( bbp_is_topic( $topic_id ) )
2322 $topic_id = bbp_get_topic_id( $topic_id );
2323 else
2324 return;
2325
2326 $anonymous_replies = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( ID ) FROM {$wpdb->posts} WHERE ( post_parent = %d AND post_status = '%s' AND post_type = '%s' AND post_author = 0 ) OR ( ID = %d AND post_type = '%s' AND post_author = 0 );", $topic_id, bbp_get_public_status_id(), bbp_get_reply_post_type(), $topic_id, bbp_get_topic_post_type() ) );
2327
2328 update_post_meta( $topic_id, '_bbp_anonymous_reply_count', (int) $anonymous_replies );
2329
2330 return apply_filters( 'bbp_update_topic_anonymous_reply_count', (int) $anonymous_replies, $topic_id );
2331 }
2332
2333 /**
2334 * Update the revision log of the topic
2335 *
2336 * @since bbPress (r2782)
2337 *
2338 * @param mixed $args Supports these args:
2339 * - topic_id: Topic id
2340 * - author_id: Author id
2341 * - reason: Reason for editing
2342 * - revision_id: Revision id
2343 * @uses bbp_get_topic_id() To get the topic id
2344 * @uses bbp_get_user_id() To get the user id
2345 * @uses bbp_format_revision_reason() To format the reason
2346 * @uses bbp_get_topic_raw_revision_log() To get the raw topic revision log
2347 * @uses update_post_meta() To update the topic revision log meta
2348 * @return mixed False on failure, true on success
2349 */
2350 function bbp_update_topic_revision_log( $args = '' ) {
2351 $defaults = array (
2352 'reason' => '',
2353 'topic_id' => 0,
2354 'author_id' => 0,
2355 'revision_id' => 0
2356 );
2357
2358 $r = wp_parse_args( $args, $defaults );
2359 extract( $r );
2360
2361 // Populate the variables
2362 $reason = bbp_format_revision_reason( $reason );
2363 $topic_id = bbp_get_topic_id( $topic_id );
2364 $author_id = bbp_get_user_id ( $author_id, false, true );
2365 $revision_id = (int) $revision_id;
2366
2367 // Get the logs and append the new one to those
2368 $revision_log = bbp_get_topic_raw_revision_log( $topic_id );
2369 $revision_log[$revision_id] = array( 'author' => $author_id, 'reason' => $reason );
2370
2371 // Finally, update
2372 return update_post_meta( $topic_id, '_bbp_revision_log', $revision_log );
2373 }
2374
2375 /** Topic Actions *************************************************************/
2376
2377 /**
2378 * Closes a topic
2379 *
2380 * @since bbPress (r2740)
2381 *
2382 * @param int $topic_id Topic id
2383 * @uses wp_get_single_post() To get the topic
2384 * @uses do_action() Calls 'bbp_close_topic' with the topic id
2385 * @uses add_post_meta() To add the previous status to a meta
2386 * @uses wp_insert_post() To update the topic with the new status
2387 * @uses do_action() Calls 'bbp_opened_topic' with the topic id
2388 * @return mixed False or {@link WP_Error} on failure, topic id on success
2389 */
2390 function bbp_close_topic( $topic_id = 0 ) {
2391
2392 // Get topic
2393 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2394 return $topic;
2395
2396 // Bail if already closed
2397 if ( bbp_get_closed_status_id == $topic['post_status'] )
2398 return false;
2399
2400 // Execute pre close code
2401 do_action( 'bbp_close_topic', $topic_id );
2402
2403 // Add pre close status
2404 add_post_meta( $topic_id, '_bbp_status', $topic['post_status'] );
2405
2406 // Set closed status
2407 $topic['post_status'] = bbp_get_closed_status_id();
2408
2409 // No revisions
2410 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2411
2412 // Update topic
2413 $topic_id = wp_insert_post( $topic );
2414
2415 // Execute post close code
2416 do_action( 'bbp_closed_topic', $topic_id );
2417
2418 // Return topic_id
2419 return $topic_id;
2420 }
2421
2422 /**
2423 * Opens a topic
2424 *
2425 * @since bbPress (r2740)
2426 *
2427 * @param int $topic_id Topic id
2428 * @uses wp_get_single_post() To get the topic
2429 * @uses do_action() Calls 'bbp_open_topic' with the topic id
2430 * @uses get_post_meta() To get the previous status
2431 * @uses delete_post_meta() To delete the previous status meta
2432 * @uses wp_insert_post() To update the topic with the new status
2433 * @uses do_action() Calls 'bbp_opened_topic' with the topic id
2434 * @return mixed False or {@link WP_Error} on failure, topic id on success
2435 */
2436 function bbp_open_topic( $topic_id = 0 ) {
2437
2438 // Get topic
2439 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2440 return $topic;
2441
2442 // Bail if already open
2443 if ( bbp_get_closed_status_id() != $topic['post_status'])
2444 return false;
2445
2446 // Execute pre open code
2447 do_action( 'bbp_open_topic', $topic_id );
2448
2449 // Get previous status
2450 $topic_status = get_post_meta( $topic_id, '_bbp_status', true );
2451
2452 // Set previous status
2453 $topic['post_status'] = $topic_status;
2454
2455 // Remove old status meta
2456 delete_post_meta( $topic_id, '_bbp_status' );
2457
2458 // No revisions
2459 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2460
2461 // Update topic
2462 $topic_id = wp_insert_post( $topic );
2463
2464 // Execute post open code
2465 do_action( 'bbp_opened_topic', $topic_id );
2466
2467 // Return topic_id
2468 return $topic_id;
2469 }
2470
2471 /**
2472 * Marks a topic as spam
2473 *
2474 * @since bbPress (r2740)
2475 *
2476 * @param int $topic_id Topic id
2477 * @uses wp_get_single_post() To get the topic
2478 * @uses do_action() Calls 'bbp_spam_topic' with the topic id
2479 * @uses add_post_meta() To add the previous status to a meta
2480 * @uses wp_insert_post() To update the topic with the new status
2481 * @uses do_action() Calls 'bbp_spammed_topic' with the topic id
2482 * @return mixed False or {@link WP_Error} on failure, topic id on success
2483 */
2484 function bbp_spam_topic( $topic_id = 0 ) {
2485
2486 // Get the topic
2487 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2488 return $topic;
2489
2490 // Bail if topic is spam
2491 if ( bbp_get_spam_status_id() == $topic['post_status'] )
2492 return false;
2493
2494 // Execute pre spam code
2495 do_action( 'bbp_spam_topic', $topic_id );
2496
2497 // Add the original post status as post meta for future restoration
2498 add_post_meta( $topic_id, '_bbp_spam_meta_status', $topic['post_status'] );
2499
2500 // Get topic tags
2501 $terms = get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() );
2502
2503 // Topic has tags
2504 if ( !empty( $terms ) ) {
2505
2506 // Loop through and collect term names
2507 foreach( $terms as $term ) {
2508 $term_names[] = $term->name;
2509 }
2510
2511 // Topic terms have slugs
2512 if ( !empty( $term_names ) ) {
2513
2514 // Add the original post status as post meta for future restoration
2515 add_post_meta( $topic_id, '_bbp_spam_topic_tags', $term_names );
2516
2517 // Empty the topic of its tags
2518 $topic['tax_input'] = array( bbp_get_topic_tag_tax_id() => '' );
2519 }
2520 }
2521
2522 // Set post status to spam
2523 $topic['post_status'] = bbp_get_spam_status_id();
2524
2525 // No revisions
2526 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2527
2528 // Update the topic
2529 $topic_id = wp_insert_post( $topic );
2530
2531 // Execute post spam code
2532 do_action( 'bbp_spammed_topic', $topic_id );
2533
2534 // Return topic_id
2535 return $topic_id;
2536 }
2537
2538 /**
2539 * Unspams a topic
2540 *
2541 * @since bbPress (r2740)
2542 *
2543 * @param int $topic_id Topic id
2544 * @uses wp_get_single_post() To get the topic
2545 * @uses do_action() Calls 'bbp_unspam_topic' with the topic id
2546 * @uses get_post_meta() To get the previous status
2547 * @uses delete_post_meta() To delete the previous status meta
2548 * @uses wp_insert_post() To update the topic with the new status
2549 * @uses do_action() Calls 'bbp_unspammed_topic' with the topic id
2550 * @return mixed False or {@link WP_Error} on failure, topic id on success
2551 */
2552 function bbp_unspam_topic( $topic_id = 0 ) {
2553
2554 // Get the topic
2555 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2556 return $topic;
2557
2558 // Bail if already not spam
2559 if ( bbp_get_spam_status_id() != $topic['post_status'] )
2560 return false;
2561
2562 // Execute pre unspam code
2563 do_action( 'bbp_unspam_topic', $topic_id );
2564
2565 // Get pre spam status
2566 $topic_status = get_post_meta( $topic_id, '_bbp_spam_meta_status', true );
2567
2568 // Set post status to pre spam
2569 $topic['post_status'] = $topic_status;
2570
2571 // Delete pre spam meta
2572 delete_post_meta( $topic_id, '_bbp_spam_meta_status' );
2573
2574 // No revisions
2575 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2576
2577 // Get pre-spam topic tags
2578 $terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
2579
2580 // Topic had tags before it was spammed
2581 if ( !empty( $terms ) ) {
2582
2583 // Set the tax_input of the topic
2584 $topic['tax_input'] = array( bbp_get_topic_tag_tax_id() => $terms );
2585
2586 // Delete pre-spam topic tag meta
2587 delete_post_meta( $topic_id, '_bbp_spam_topic_tags' );
2588 }
2589
2590 // Update the topic
2591 $topic_id = wp_insert_post( $topic );
2592
2593 // Execute post unspam code
2594 do_action( 'bbp_unspammed_topic', $topic_id );
2595
2596 // Return topic_id
2597 return $topic_id;
2598 }
2599
2600 /**
2601 * Sticks a topic to a forum or front
2602 *
2603 * @since bbPress (r2754)
2604 *
2605 * @param int $topic_id Optional. Topic id
2606 * @param int $super Should we make the topic a super sticky?
2607 * @uses bbp_get_topic_id() To get the topic id
2608 * @uses bbp_unstick_topic() To unstick the topic
2609 * @uses bbp_get_topic_forum_id() To get the topic forum id
2610 * @uses bbp_get_stickies() To get the stickies
2611 * @uses do_action() 'bbp_stick_topic' with topic id and bool super
2612 * @uses update_option() To update the super stickies option
2613 * @uses update_post_meta() To update the forum stickies meta
2614 * @uses do_action() Calls 'bbp_sticked_topic' with the topic id, bool super
2615 * and success
2616 * @return bool True on success, false on failure
2617 */
2618 function bbp_stick_topic( $topic_id = 0, $super = false ) {
2619 $topic_id = bbp_get_topic_id( $topic_id );
2620
2621 // We may have a super sticky to which we want to convert into a normal sticky and vice versa
2622 // So, unstick the topic first to avoid any possible error
2623 bbp_unstick_topic( $topic_id );
2624
2625 $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
2626 $stickies = bbp_get_stickies( $forum_id );
2627
2628 do_action( 'bbp_stick_topic', $topic_id, $super );
2629
2630 if ( !is_array( $stickies ) )
2631 $stickies = array( $topic_id );
2632 else
2633 $stickies[] = $topic_id;
2634
2635 $stickies = array_unique( array_filter( $stickies ) );
2636
2637 $success = !empty( $super ) ? update_option( '_bbp_super_sticky_topics', $stickies ) : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
2638
2639 do_action( 'bbp_sticked_topic', $topic_id, $super, $success );
2640
2641 return $success;
2642 }
2643
2644 /**
2645 * Unsticks a topic both from front and it's forum
2646 *
2647 * @since bbPress (r2754)
2648 *
2649 * @param int $topic_id Optional. Topic id
2650 * @uses bbp_get_topic_id() To get the topic id
2651 * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
2652 * @uses bbp_get_topic_forum_id() To get the topic forum id
2653 * @uses bbp_get_stickies() To get the forum stickies
2654 * @uses do_action() Calls 'bbp_unstick_topic' with the topic id
2655 * @uses delete_option() To delete the super stickies option
2656 * @uses update_option() To update the super stickies option
2657 * @uses delete_post_meta() To delete the forum stickies meta
2658 * @uses update_post_meta() To update the forum stickies meta
2659 * @uses do_action() Calls 'bbp_unsticked_topic' with the topic id and success
2660 * @return bool Always true.
2661 */
2662 function bbp_unstick_topic( $topic_id = 0 ) {
2663 $topic_id = bbp_get_topic_id( $topic_id );
2664 $super = bbp_is_topic_super_sticky( $topic_id );
2665 $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
2666 $stickies = bbp_get_stickies( $forum_id );
2667 $offset = array_search( $topic_id, $stickies );
2668
2669 do_action( 'bbp_unstick_topic', $topic_id );
2670
2671 if ( empty( $stickies ) ) {
2672 $success = true;
2673 } elseif ( !in_array( $topic_id, $stickies ) ) {
2674 $success = true;
2675 } elseif ( false === $offset ) {
2676 $success = true;
2677 } else {
2678 array_splice( $stickies, $offset, 1 );
2679 if ( empty( $stickies ) )
2680 $success = !empty( $super ) ? delete_option( '_bbp_super_sticky_topics' ) : delete_post_meta( $forum_id, '_bbp_sticky_topics' );
2681 else
2682 $success = !empty( $super ) ? update_option( '_bbp_super_sticky_topics', $stickies ) : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
2683 }
2684
2685 do_action( 'bbp_unsticked_topic', $topic_id, $success );
2686
2687 return true;
2688 }
2689
2690 /** Before Delete/Trash/Untrash ***********************************************/
2691
2692 /**
2693 * Called before deleting a topic.
2694 *
2695 * This function is supplemental to the actual topic deletion which is
2696 * handled by WordPress core API functions. It is used to clean up after
2697 * a topic that is being deleted.
2698 *
2699 * @uses bbp_get_topic_id() To get the topic id
2700 * @uses bbp_is_topic() To check if the passed id is a topic
2701 * @uses do_action() Calls 'bbp_delete_topic' with the topic id
2702 * @uses bbp_has_replies() To check if the topic has replies
2703 * @uses bbp_replies() To loop through the replies
2704 * @uses bbp_the_reply() To set a reply as the current reply in the loop
2705 * @uses bbp_get_reply_id() To get the reply id
2706 * @uses wp_delete_post() To delete the reply
2707 */
2708 function bbp_delete_topic( $topic_id = 0 ) {
2709
2710 // Validate topic ID
2711 $topic_id = bbp_get_topic_id( $topic_id );
2712
2713 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2714 return false;
2715
2716 do_action( 'bbp_delete_topic', $topic_id );
2717
2718 // Valid topic/reply statuses
2719 $post_stati = join( ',', array( bbp_get_public_status_id(), bbp_get_spam_status_id(), bbp_get_trash_status_id() ) );
2720
2721 // Topic is being permanently deleted, so its replies gotta go too
2722 if ( bbp_has_replies( array(
2723 'post_type' => bbp_get_reply_post_type(),
2724 'post_status' => $post_stati,
2725 'posts_per_page' => -1,
2726 'meta_query' => array( array(
2727 'key' => '_bbp_topic_id',
2728 'value' => $topic_id,
2729 'compare' => '='
2730 ) )
2731 ) ) ) {
2732 while ( bbp_replies() ) {
2733 bbp_the_reply();
2734 wp_delete_post( bbp_get_reply_id(), true );
2735 }
2736 }
2737 }
2738
2739 /**
2740 * Called before trashing a topic
2741 *
2742 * This function is supplemental to the actual topic being trashed which is
2743 * handled by WordPress core API functions. It is used to clean up after
2744 * a topic that is being trashed.
2745 *
2746 * @uses bbp_get_topic_id() To get the topic id
2747 * @uses bbp_is_topic() To check if the passed id is a topic
2748 * @uses do_action() Calls 'bbp_trash_topic' with the topic id
2749 * @uses bbp_has_replies() To check if the topic has replies
2750 * @uses bbp_replies() To loop through the replies
2751 * @uses bbp_the_reply() To set a reply as the current reply in the loop
2752 * @uses bbp_get_reply_id() To get the reply id
2753 * @uses wp_trash_post() To trash the reply
2754 * @uses update_post_meta() To save a list of just trashed replies for future use
2755 */
2756 function bbp_trash_topic( $topic_id = 0 ) {
2757 global $bbp;
2758
2759 $topic_id = bbp_get_topic_id( $topic_id );
2760
2761 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2762 return false;
2763
2764 do_action( 'bbp_trash_topic', $topic_id );
2765
2766 // Topic is being trashed, so its replies are trashed too
2767 if ( bbp_has_replies( array(
2768 'post_type' => bbp_get_reply_post_type(),
2769 'post_status' => bbp_get_public_status_id(),
2770 'posts_per_page' => -1,
2771 'meta_query' => array( array(
2772 'key' => '_bbp_topic_id',
2773 'value' => $topic_id,
2774 'compare' => '='
2775 ) )
2776 ) ) ) {
2777
2778 // Prevent debug notices
2779 $pre_trashed_replies = array();
2780
2781 // Loop through replies, trash them, and add them to array
2782 while ( bbp_replies() ) {
2783 bbp_the_reply();
2784 wp_trash_post( $bbp->reply_query->post->ID );
2785 $pre_trashed_replies[] = $bbp->reply_query->post->ID;
2786 }
2787
2788 // Set a post_meta entry of the replies that were trashed by this action.
2789 // This is so we can possibly untrash them, without untrashing replies
2790 // that were purposefully trashed before.
2791 update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
2792 }
2793 }
2794
2795 /**
2796 * Called before untrashing a topic
2797 *
2798 * @uses bbp_get_topic_id() To get the topic id
2799 * @uses bbp_is_topic() To check if the passed id is a topic
2800 * @uses do_action() Calls 'bbp_untrash_topic' with the topic id
2801 * @uses get_post_meta() To get the list of replies which were trashed with the
2802 * topic
2803 * @uses wp_untrash_post() To untrash the reply
2804 */
2805 function bbp_untrash_topic( $topic_id = 0 ) {
2806 $topic_id = bbp_get_topic_id( $topic_id );
2807
2808 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2809 return false;
2810
2811 do_action( 'bbp_untrash_topic', $topic_id );
2812
2813 // Get the replies that were not previously trashed
2814 $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
2815
2816 // There are replies to untrash
2817 if ( !empty( $pre_trashed_replies ) ) {
2818
2819 // Maybe reverse the trashed replies array
2820 if ( is_array( $pre_trashed_replies ) )
2821 $pre_trashed_replies = array_reverse( $pre_trashed_replies );
2822
2823 // Loop through replies
2824 foreach ( (array) $pre_trashed_replies as $reply ) {
2825 wp_untrash_post( $reply );
2826 }
2827 }
2828 }
2829
2830 /** After Delete/Trash/Untrash ************************************************/
2831
2832 /**
2833 * Called after deleting a topic
2834 *
2835 * @uses bbp_get_topic_id() To get the topic id
2836 * @uses bbp_is_topic() To check if the passed id is a topic
2837 * @uses do_action() Calls 'bbp_deleted_topic' with the topic id
2838 */
2839 function bbp_deleted_topic( $topic_id = 0 ) {
2840 $topic_id = bbp_get_topic_id( $topic_id );
2841
2842 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2843 return false;
2844
2845 do_action( 'bbp_deleted_topic', $topic_id );
2846 }
2847
2848 /**
2849 * Called after trashing a topic
2850 *
2851 * @uses bbp_get_topic_id() To get the topic id
2852 * @uses bbp_is_topic() To check if the passed id is a topic
2853 * @uses do_action() Calls 'bbp_trashed_topic' with the topic id
2854 */
2855 function bbp_trashed_topic( $topic_id = 0 ) {
2856 $topic_id = bbp_get_topic_id( $topic_id );
2857
2858 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2859 return false;
2860
2861 do_action( 'bbp_trashed_topic', $topic_id );
2862 }
2863
2864 /**
2865 * Called after untrashing a topic
2866 *
2867 * @uses bbp_get_topic_id() To get the topic id
2868 * @uses bbp_is_topic() To check if the passed id is a topic
2869 * @uses do_action() Calls 'bbp_untrashed_topic' with the topic id
2870 */
2871 function bbp_untrashed_topic( $topic_id = 0 ) {
2872 $topic_id = bbp_get_topic_id( $topic_id );
2873
2874 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2875 return false;
2876
2877 do_action( 'bbp_untrashed_topic', $topic_id );
2878 }
2879
2880 /** Feeds *********************************************************************/
2881
2882 /**
2883 * Output an RSS2 feed of topics, based on the query passed.
2884 *
2885 * @since bbPress (r3171)
2886 *
2887 * @uses bbp_version()
2888 * @uses bbp_is_single_topic()
2889 * @uses bbp_user_can_view_forum()
2890 * @uses bbp_get_topic_forum_id()
2891 * @uses bbp_show_load_topic()
2892 * @uses bbp_topic_permalink()
2893 * @uses bbp_topic_title()
2894 * @uses bbp_get_topic_reply_count()
2895 * @uses bbp_topic_content()
2896 * @uses bbp_has_topics()
2897 * @uses bbp_topics()
2898 * @uses bbp_the_topic()
2899 * @uses get_wp_title_rss()
2900 * @uses get_option()
2901 * @uses bloginfo_rss
2902 * @uses self_link()
2903 * @uses the_author()
2904 * @uses get_post_time()
2905 * @uses rss_enclosure()
2906 * @uses do_action()
2907 * @uses apply_filters()
2908 *
2909 * @param array $topics_query
2910 */
2911 function bbp_display_topics_feed_rss2( $topics_query = array() ) {
2912
2913 // User cannot access this forum
2914 if ( bbp_is_single_forum() && !bbp_user_can_view_forum( array( 'forum_id' => bbp_get_forum_id() ) ) )
2915 return;
2916
2917 // Display the feed
2918 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
2919 header( 'Status: 200 OK' );
2920 echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>
2921
2922 <rss version="2.0"
2923 xmlns:content="http://purl.org/rss/1.0/modules/content/"
2924 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
2925 xmlns:dc="http://purl.org/dc/elements/1.1/"
2926 xmlns:atom="http://www.w3.org/2005/Atom"
2927
2928 <?php do_action( 'bbp_feed' ); ?>
2929 >
2930
2931 <channel>
2932
2933 <title><?php bloginfo_rss( 'name' ); ?> &#187; <?php _e( 'All Topics', 'bbpress' ); ?></title>
2934 <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
2935 <link><?php self_link(); ?></link>
2936 <description><?php //?></description>
2937 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s O', '', false ); ?></pubDate>
2938 <generator>http://bbpress.org/?v=<?php bbp_version(); ?></generator>
2939 <language><?php echo get_option( 'rss_language' ); ?></language>
2940
2941 <?php do_action( 'bbp_feed_head' ); ?>
2942
2943 <?php if ( bbp_has_topics( $topics_query ) ) : ?>
2944
2945 <?php while ( bbp_topics() ) : bbp_the_topic(); ?>
2946
2947 <item>
2948 <guid><?php bbp_topic_permalink(); ?></guid>
2949 <title><![CDATA[<?php bbp_topic_title(); ?>]]></title>
2950 <link><?php bbp_topic_permalink(); ?></link>
2951 <pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_meta( bbp_get_topic_id(), '_bbp_last_active_time', true ) ); ?></pubDate>
2952 <dc:creator><?php the_author() ?></dc:creator>
2953
2954 <?php if ( !post_password_required() ) : ?>
2955
2956 <description>
2957 <![CDATA[
2958 <p><?php printf( __( 'Replies: %s', 'bbpress' ), bbp_get_topic_reply_count() ); ?></p>
2959 <?php bbp_topic_content(); ?>
2960 ]]>
2961 </description>
2962
2963 <?php rss_enclosure(); ?>
2964
2965 <?php endif; ?>
2966
2967 <?php do_action( 'bbp_feed_item' ); ?>
2968
2969 </item>
2970
2971 <?php endwhile; ?>
2972 <?php endif; ?>
2973
2974 <?php do_action( 'bbp_feed_footer' ); ?>
2975
2976 </channel>
2977 </rss>
2978
2979 <?php
2980 exit();
2981 }
2982
2983 ?>
2984