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

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

3,095 lines 107.1 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 // Define local variable(s)
797 $active_id = 0;
798
799 // Topic was passed
800 if ( !empty( $topic_id ) ) {
801
802 // Get the forum ID if none was passed
803 if ( empty( $forum_id ) ) {
804 $forum_id = bbp_get_topic_forum_id( $topic_id );
805 }
806
807 // Set the active_id based on topic_id/reply_id
808 $active_id = empty( $reply_id ) ? $topic_id : $reply_id;
809 }
810
811 // Get topic ancestors
812 $ancestors = array_values( array_unique( array_merge( array( $forum_id ), get_post_ancestors( $topic_id ) ) ) );
813
814 // Topic status
815 $topic_status = get_post_status( $topic_id );
816
817 // If we want a full refresh, unset any of the possibly passed variables
818 if ( true == $refresh ) {
819 $forum_id = $topic_id = $reply_id = $active_id = $last_active_time = 0;
820 $topic_status = bbp_get_public_status_id();
821 }
822
823 // Loop through ancestors
824 foreach ( $ancestors as $ancestor ) {
825
826 // If ancestor is a forum, update counts
827 if ( bbp_is_forum( $ancestor ) ) {
828
829 // Update the forum
830 bbp_update_forum( array(
831 'forum_id' => $ancestor,
832 'last_topic_id' => $topic_id,
833 'last_reply_id' => $reply_id,
834 'last_active_id' => $active_id,
835 'last_active_time' => 0,
836 'last_active_status' => $topic_status
837 ) );
838 }
839 }
840 }
841
842 /**
843 * Handle the moving of a topic from one forum to another. This includes walking
844 * up the old and new branches and updating the counts.
845 *
846 * @param int $topic_id Topic id
847 * @param int $old_forum_id Old forum id
848 * @param int $new_forum_id New forum id
849 * @uses bbp_get_topic_id() To get the topic id
850 * @uses bbp_get_forum_id() To get the forum id
851 * @uses bbp_get_stickies() To get the old forums sticky topics
852 * @uses delete_post_meta() To delete the forum sticky meta
853 * @uses update_post_meta() To update the old forum sticky meta
854 * @uses bbp_stick_topic() To stick the topic in the new forum
855 * @uses bbp_get_reply_post_type() To get the reply post type
856 * @uses bbp_get_all_child_ids() To get the public child ids
857 * @uses bbp_update_reply_forum_id() To update the reply forum id
858 * @uses bbp_update_topic_forum_id() To update the topic forum id
859 * @uses get_post_ancestors() To get the topic's ancestors
860 * @uses bbp_is_forum() To check if the ancestor is a forum
861 * @uses bbp_update_forum() To update the forum
862 */
863 function bbp_move_topic_handler( $topic_id, $old_forum_id, $new_forum_id ) {
864
865 // Validate parameters
866 $topic_id = bbp_get_topic_id( $topic_id );
867 $old_forum_id = bbp_get_forum_id( $old_forum_id );
868 $new_forum_id = bbp_get_forum_id( $new_forum_id );
869
870 /** Stickies **************************************************************/
871
872 // Get forum stickies
873 $old_stickies = bbp_get_stickies( $old_forum_id );
874
875 // Only proceed if stickies are found
876 if ( !empty( $old_stickies ) ) {
877
878 // Define local variables
879 $updated_stickies = array();
880
881 // Loop through stickies of forum
882 foreach ( $old_stickies as $sticky_topic_id ) {
883
884 // Add non-matches to the updated array
885 if ( $topic_id != $sticky_topic_id ) {
886 $updated_stickies[] = $sticky_topic_id;
887 }
888 }
889
890 // No more stickies so delete the beta
891 if ( empty( $updated_stickies ) ) {
892 delete_post_meta ( $old_forum_id, '_bbp_sticky_topics' );
893
894 // Still stickies so update the meta
895 } else {
896 update_post_meta( $old_forum_id, '_bbp_sticky_topics', $updated_stickies );
897 }
898
899 // Topic was sticky, so restick in new forum
900 bbp_stick_topic( $topic_id );
901 }
902
903 /** Topic Replies *********************************************************/
904
905 // Get the topics replies
906 $replies = bbp_get_all_child_ids( $topic_id, bbp_get_reply_post_type() );
907
908 // Update the forum_id of all replies in the topic
909 foreach ( $replies as $reply_id )
910 bbp_update_reply_forum_id( $reply_id, $new_forum_id );
911
912 // Forum topic meta
913 bbp_update_topic_forum_id( $topic_id, $new_forum_id );
914
915 /** Old forum_id **********************************************************/
916
917 // Get topic ancestors
918 $ancestors = array_values( array_unique( array_merge( array( $old_forum_id ), get_post_ancestors( $old_forum_id ) ) ) );
919
920 // Loop through ancestors
921 foreach ( $ancestors as $ancestor ) {
922
923 // If ancestor is a forum, update counts
924 if ( bbp_is_forum( $ancestor ) ) {
925
926 // Update the forum
927 bbp_update_forum( array(
928 'forum_id' => $ancestor,
929 ) );
930 }
931 }
932
933 /** New forum_id **********************************************************/
934
935 // Make sure we're not walking twice
936 if ( !in_array( $new_forum_id, $ancestors ) ) {
937
938 // Get topic ancestors
939 $ancestors = array_values( array_unique( array_merge( array( $new_forum_id ), get_post_ancestors( $new_forum_id ) ) ) );
940
941 // Loop through ancestors
942 foreach ( $ancestors as $ancestor ) {
943
944 // If ancestor is a forum, update counts
945 if ( bbp_is_forum( $ancestor ) ) {
946
947 // Update the forum
948 bbp_update_forum( array(
949 'forum_id' => $ancestor,
950 ) );
951 }
952 }
953 }
954 }
955
956 /**
957 * Merge topic handler
958 *
959 * Handles the front end merge topic submission
960 *
961 * @since bbPress (r2756)
962 *
963 * @uses bbPress:errors::add() To log various error messages
964 * @uses bbp_get_topic() To get the topics
965 * @uses check_admin_referer() To verify the nonce and check the referer
966 * @uses current_user_can() To check if the current user can edit the topics
967 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
968 * @uses do_action() Calls 'bbp_merge_topic' with the destination and source
969 * topic ids
970 * @uses bbp_get_topic_subscribers() To get the source topic subscribers
971 * @uses bbp_add_user_subscription() To add the user subscription
972 * @uses bbp_remove_user_subscription() To remove the user subscription
973 * @uses bbp_get_topic_favoriters() To get the source topic favoriters
974 * @uses bbp_add_user_favorite() To add the user favorite
975 * @uses bbp_remove_user_favorite() To remove the user favorite
976 * @uses wp_get_post_terms() To get the source topic tags
977 * @uses wp_set_post_terms() To set the topic tags
978 * @uses wp_delete_object_term_relationships() To delete the topic tags
979 * @uses bbp_open_topic() To open the topic
980 * @uses bbp_unstick_topic() To unstick the topic
981 * @uses bbp_get_reply_post_type() To get the reply post type
982 * @uses get_posts() To get the replies
983 * @uses wp_update_post() To update the topic
984 * @uses bbp_update_reply_topic_id() To update the reply topic id
985 * @uses bbp_get_topic_forum_id() To get the topic forum id
986 * @uses bbp_update_reply_forum_id() To update the reply forum id
987 * @uses do_action() Calls 'bbp_merged_topic_reply' with the reply id and
988 * destination topic id
989 * @uses do_action() Calls 'bbp_merged_topic' with the destination and source
990 * topic ids and source topic's forum id
991 * @uses bbp_get_topic_permalink() To get the topic permalink
992 * @uses wp_redirect() To redirect to the topic link
993 */
994 function bbp_merge_topic_handler() {
995
996 // Bail if not a POST action
997 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
998 return;
999
1000 // Bail if action is not bbp-merge-topic
1001 if ( empty( $_POST['action'] ) || ( 'bbp-merge-topic' !== $_POST['action'] ) )
1002 return;
1003
1004 // Define local variable(s)
1005 $source_topic_id = $destination_topic_id = 0;
1006 $source_topic = $destination_topic = 0;
1007 $subscribers = $favoriters = $replies = array();
1008
1009 /** Source Topic **********************************************************/
1010
1011 // Topic id
1012 if ( empty( $_POST['bbp_topic_id'] ) )
1013 bbp_add_error( 'bbp_merge_topic_source_id', __( '<strong>ERROR</strong>: Topic ID not found.', 'bbpress' ) );
1014 else
1015 $source_topic_id = (int) $_POST['bbp_topic_id'];
1016
1017 // Nonce check
1018 check_admin_referer( 'bbp-merge-topic_' . $source_topic_id );
1019
1020 // Source topic not found
1021 if ( !$source_topic = bbp_get_topic( $source_topic_id ) )
1022 bbp_add_error( 'bbp_merge_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to merge was not found.', 'bbpress' ) );
1023
1024 // Cannot edit source topic
1025 if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
1026 bbp_add_error( 'bbp_merge_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
1027
1028 /** Destination Topic *****************************************************/
1029
1030 // Topic id
1031 if ( empty( $_POST['bbp_destination_topic'] ) )
1032 bbp_add_error( 'bbp_merge_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found.', 'bbpress' ) );
1033 else
1034 $destination_topic_id = (int) $_POST['bbp_destination_topic'];
1035
1036 // Destination topic not found
1037 if ( !$destination_topic = bbp_get_topic( $destination_topic_id ) )
1038 bbp_add_error( 'bbp_merge_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to merge to was not found.', 'bbpress' ) );
1039
1040 // Cannot edit destination topic
1041 if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
1042 bbp_add_error( 'bbp_merge_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic.', 'bbpress' ) );
1043
1044 /** No Errors *************************************************************/
1045
1046 if ( !bbp_has_errors() ) {
1047
1048 // Update counts, etc...
1049 do_action( 'bbp_merge_topic', $destination_topic->ID, $source_topic->ID );
1050
1051 /** Date Check ********************************************************/
1052
1053 // Check if the destination topic is older than the source topic
1054 if ( strtotime( $source_topic->post_date ) < strtotime( $destination_topic->post_date ) ) {
1055
1056 // Set destination topic post_date to 1 second before source topic
1057 $destination_post_date = date( 'Y-m-d H:i:s', strtotime( $source_topic->post_date ) - 1 );
1058
1059 $postarr = array(
1060 'ID' => $destination_topic_id,
1061 'post_date' => $destination_post_date,
1062 'post_date_gmt' => get_gmt_from_date( $destination_post_date )
1063 );
1064
1065 // Update destination topic
1066 wp_update_post( $postarr );
1067 }
1068
1069 /** Subscriptions *****************************************************/
1070
1071 // Get subscribers from source topic
1072 $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
1073
1074 // Remove the topic from everybody's subscriptions
1075 if ( !empty( $subscribers ) ) {
1076
1077 // Loop through each user
1078 foreach ( (array) $subscribers as $subscriber ) {
1079
1080 // Shift the subscriber if told to
1081 if ( !empty( $_POST['bbp_topic_subscribers'] ) && ( 1 == $_POST['bbp_topic_subscribers'] ) && bbp_is_subscriptions_active() )
1082 bbp_add_user_subscription( $subscriber, $destination_topic->ID );
1083
1084 // Remove old subscription
1085 bbp_remove_user_subscription( $subscriber, $source_topic->ID );
1086 }
1087 }
1088
1089 /** Favorites *********************************************************/
1090
1091 // Get favoriters from source topic
1092 $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
1093
1094 // Remove the topic from everybody's favorites
1095 if ( !empty( $favoriters ) ) {
1096
1097 // Loop through each user
1098 foreach ( (array) $favoriters as $favoriter ) {
1099
1100 // Shift the favoriter if told to
1101 if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] )
1102 bbp_add_user_favorite( $favoriter, $destination_topic->ID );
1103
1104 // Remove old favorite
1105 bbp_remove_user_favorite( $favoriter, $source_topic->ID );
1106 }
1107 }
1108
1109 /** Tags **************************************************************/
1110
1111 // Get the source topic tags
1112 $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
1113
1114 // Tags to possibly merge
1115 if ( !empty( $source_topic_tags ) && !is_wp_error( $source_topic_tags ) ) {
1116
1117 // Shift the tags if told to
1118 if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) )
1119 wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
1120
1121 // Delete the tags from the source topic
1122 wp_delete_object_term_relationships( $source_topic->ID, bbp_get_topic_tag_tax_id() );
1123 }
1124
1125 /** Source Topic ******************************************************/
1126
1127 // Status
1128 bbp_open_topic( $source_topic->ID );
1129
1130 // Sticky
1131 bbp_unstick_topic( $source_topic->ID );
1132
1133 // Get the replies of the source topic
1134 $replies = (array) get_posts( array(
1135 'post_parent' => $source_topic->ID,
1136 'post_type' => bbp_get_reply_post_type(),
1137 'posts_per_page' => -1,
1138 'order' => 'ASC'
1139 ) );
1140
1141 // Prepend the source topic to its replies array for processing
1142 array_unshift( $replies, $source_topic );
1143
1144 if ( !empty( $replies ) ) {
1145
1146 /** Merge Replies *************************************************/
1147
1148 // Change the post_parent of each reply to the destination topic id
1149 foreach ( $replies as $reply ) {
1150 $postarr = array(
1151 'ID' => $reply->ID,
1152 'post_title' => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
1153 'post_name' => false,
1154 'post_type' => bbp_get_reply_post_type(),
1155 'post_parent' => $destination_topic->ID,
1156 'guid' => ''
1157 );
1158
1159 wp_update_post( $postarr );
1160
1161 // Adjust reply meta values
1162 bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID );
1163 bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
1164
1165 // Do additional actions per merged reply
1166 do_action( 'bbp_merged_topic_reply', $reply->ID, $destination_topic->ID );
1167 }
1168 }
1169
1170 /** Successful Merge **************************************************/
1171
1172 // Update topic's last meta data
1173 bbp_update_topic_last_reply_id ( $destination_topic->ID );
1174 bbp_update_topic_last_active_id ( $destination_topic->ID );
1175 bbp_update_topic_last_active_time( $destination_topic->ID );
1176
1177 // Send the post parent of the source topic as it has been shifted
1178 // (possibly to a new forum) so we need to update the counts of the
1179 // old forum as well as the new one
1180 do_action( 'bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent );
1181
1182 // Redirect back to new topic
1183 wp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
1184
1185 // For good measure
1186 exit();
1187 }
1188 }
1189
1190 /**
1191 * Fix counts on topic merge
1192 *
1193 * When a topic is merged, update the counts of source and destination topic
1194 * and their forums.
1195 *
1196 * @since bbPress (r2756)
1197 *
1198 * @param int $destination_topic_id Destination topic id
1199 * @param int $source_topic_id Source topic id
1200 * @param int $source_topic_forum Source topic's forum id
1201 * @uses bbp_update_forum_topic_count() To update the forum topic counts
1202 * @uses bbp_update_forum_reply_count() To update the forum reply counts
1203 * @uses bbp_update_topic_reply_count() To update the topic reply counts
1204 * @uses bbp_update_topic_voice_count() To update the topic voice counts
1205 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
1206 * count
1207 * @uses do_action() Calls 'bbp_merge_topic_count' with the destination topic
1208 * id, source topic id & source topic forum id
1209 */
1210 function bbp_merge_topic_count( $destination_topic_id, $source_topic_id, $source_topic_forum_id ) {
1211
1212 /** Source Topic **********************************************************/
1213
1214 // Forum Topic Counts
1215 bbp_update_forum_topic_count( $source_topic_forum_id );
1216
1217 // Forum Reply Counts
1218 bbp_update_forum_reply_count( $source_topic_forum_id );
1219
1220 /** Destination Topic *****************************************************/
1221
1222 // Topic Reply Counts
1223 bbp_update_topic_reply_count( $destination_topic_id );
1224
1225 // Topic Hidden Reply Counts
1226 bbp_update_topic_reply_count_hidden( $destination_topic_id );
1227
1228 // Topic Voice Counts
1229 bbp_update_topic_voice_count( $destination_topic_id );
1230
1231 do_action( 'bbp_merge_topic_count', $destination_topic_id, $source_topic_id, $source_topic_forum_id );
1232 }
1233
1234 /**
1235 * Split topic handler
1236 *
1237 * Handles the front end split topic submission
1238 *
1239 * @since bbPress (r2756)
1240 *
1241 * @uses bbPress:errors::add() To log various error messages
1242 * @uses bbp_get_reply() To get the reply
1243 * @uses bbp_get_topic() To get the topics
1244 * @uses check_admin_referer() To verify the nonce and check the referer
1245 * @uses current_user_can() To check if the current user can edit the topics
1246 * @uses bbp_get_topic_post_type() To get the topic post type
1247 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
1248 * @uses do_action() Calls 'bbp_pre_split_topic' with the from reply id, source
1249 * and destination topic ids
1250 * @uses bbp_get_topic_subscribers() To get the source topic subscribers
1251 * @uses bbp_add_user_subscription() To add the user subscription
1252 * @uses bbp_get_topic_favoriters() To get the source topic favoriters
1253 * @uses bbp_add_user_favorite() To add the user favorite
1254 * @uses wp_get_post_terms() To get the source topic tags
1255 * @uses wp_set_post_terms() To set the topic tags
1256 * @uses bbp_get_reply_post_type() To get the reply post type
1257 * @uses wpdb::prepare() To prepare our sql query
1258 * @uses wpdb::get_results() To execute the sql query and get results
1259 * @uses wp_update_post() To update the replies
1260 * @uses bbp_update_reply_topic_id() To update the reply topic id
1261 * @uses bbp_get_topic_forum_id() To get the topic forum id
1262 * @uses bbp_update_reply_forum_id() To update the reply forum id
1263 * @uses do_action() Calls 'bbp_split_topic_reply' with the reply id and
1264 * destination topic id
1265 * @uses bbp_update_topic_last_reply_id() To update the topic last reply id
1266 * @uses bbp_update_topic_last_active_time() To update the topic last active meta
1267 * @uses do_action() Calls 'bbp_post_split_topic' with the destination and
1268 * source topic ids and source topic's forum id
1269 * @uses bbp_get_topic_permalink() To get the topic permalink
1270 * @uses wp_redirect() To redirect to the topic link
1271 */
1272 function bbp_split_topic_handler() {
1273
1274 // Bail if not a POST action
1275 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1276 return;
1277
1278 // Bail if action is not 'bbp-split-topic'
1279 if ( empty( $_POST['action'] ) || ( 'bbp-split-topic' !== $_POST['action'] ) )
1280 return;
1281
1282 global $wpdb;
1283
1284 // Prevent debug notices
1285 $from_reply_id = $destination_topic_id = 0;
1286 $destination_topic_title = '';
1287 $destination_topic = $from_reply = $source_topic = '';
1288 $split_option = false;
1289
1290 /** Split Reply ***********************************************************/
1291
1292 if ( empty( $_POST['bbp_reply_id'] ) )
1293 bbp_add_error( 'bbp_split_topic_reply_id', __( '<strong>ERROR</strong>: Reply ID to split the topic from not found!', 'bbpress' ) );
1294 else
1295 $from_reply_id = (int) $_POST['bbp_reply_id'];
1296
1297 $from_reply = bbp_get_reply( $from_reply_id );
1298
1299 // Reply exists
1300 if ( empty( $from_reply ) )
1301 bbp_add_error( 'bbp_split_topic_r_not_found', __( '<strong>ERROR</strong>: The reply you want to split from was not found.', 'bbpress' ) );
1302
1303 /** Topic to Split ********************************************************/
1304
1305 // Get the topic being split
1306 $source_topic = bbp_get_topic( $from_reply->post_parent );
1307
1308 // No topic
1309 if ( empty( $source_topic ) )
1310 bbp_add_error( 'bbp_split_topic_source_not_found', __( '<strong>ERROR</strong>: The topic you want to split was not found.', 'bbpress' ) );
1311
1312 // Nonce check
1313 check_admin_referer( 'bbp-split-topic_' . $source_topic->ID );
1314
1315 // Use cannot edit topic
1316 if ( !current_user_can( 'edit_topic', $source_topic->ID ) )
1317 bbp_add_error( 'bbp_split_topic_source_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress' ) );
1318
1319 /** How to Split **********************************************************/
1320
1321 if ( !empty( $_POST['bbp_topic_split_option'] ) )
1322 $split_option = (string) trim( $_POST['bbp_topic_split_option'] );
1323
1324 // Invalid split option
1325 if ( empty( $split_option ) || !in_array( $split_option, array( 'existing', 'reply' ) ) ) {
1326 bbp_add_error( 'bbp_split_topic_option', __( '<strong>ERROR</strong>: You need to choose a valid split option.', 'bbpress' ) );
1327
1328 // Valid Split Option
1329 } else {
1330
1331 // What kind of split
1332 switch ( $split_option ) {
1333
1334 // Into an existing topic
1335 case 'existing' :
1336
1337 // Get destination topic id
1338 if ( empty( $_POST['bbp_destination_topic'] ) )
1339 bbp_add_error( 'bbp_split_topic_destination_id', __( '<strong>ERROR</strong>: Destination topic ID not found!', 'bbpress' ) );
1340 else
1341 $destination_topic_id = (int) $_POST['bbp_destination_topic'];
1342
1343 // Get the destination topic
1344 $destination_topic = bbp_get_topic( $destination_topic_id );
1345
1346 // No destination topic
1347 if ( empty( $destination_topic ) )
1348 bbp_add_error( 'bbp_split_topic_destination_not_found', __( '<strong>ERROR</strong>: The topic you want to split to was not found!', 'bbpress' ) );
1349
1350 // User cannot edit the destination topic
1351 if ( !current_user_can( 'edit_topic', $destination_topic->ID ) )
1352 bbp_add_error( 'bbp_split_topic_destination_permission', __( '<strong>ERROR</strong>: You do not have the permissions to edit the destination topic!', 'bbpress' ) );
1353
1354 break;
1355
1356 // Split at reply into a new topic
1357 case 'reply' :
1358 default :
1359
1360 // User needs to be able to publish topics
1361 if ( current_user_can( 'publish_topics' ) ) {
1362
1363 // Use the new title that was passed
1364 if ( !empty( $_POST['bbp_topic_split_destination_title'] ) ) {
1365 $destination_topic_title = esc_attr( strip_tags( $_POST['bbp_topic_split_destination_title'] ) );
1366
1367 // Use the source topic title
1368 } else {
1369 $destination_topic_title = $source_topic->post_title;
1370 }
1371
1372 // Setup the updated topic parameters
1373 $postarr = array(
1374 'ID' => $from_reply->ID,
1375 'post_title' => $destination_topic_title,
1376 'post_name' => false,
1377 'post_type' => bbp_get_topic_post_type(),
1378 'post_parent' => $source_topic->post_parent,
1379 'guid' => ''
1380 );
1381
1382 // Update the topic
1383 $destination_topic_id = wp_update_post( $postarr );
1384 $destination_topic = bbp_get_topic( $destination_topic_id );
1385
1386 // Make sure the new topic knows its a topic
1387 bbp_update_topic_topic_id( $from_reply->ID );
1388
1389 // Shouldn't happen
1390 if ( false == $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) {
1391 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' ) );
1392 }
1393
1394 // User cannot publish posts
1395 } else {
1396 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' ) );
1397 }
1398
1399 break;
1400 }
1401 }
1402
1403 /** No Errors - Do the Spit ***********************************************/
1404
1405 if ( !bbp_has_errors() ) {
1406
1407 // Update counts, etc...
1408 do_action( 'bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
1409
1410 /** Subscriptions *****************************************************/
1411
1412 // Copy the subscribers
1413 if ( !empty( $_POST['bbp_topic_subscribers'] ) && 1 == $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active() ) {
1414
1415 // Get the subscribers
1416 $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
1417
1418 if ( !empty( $subscribers ) ) {
1419
1420 // Add subscribers to new topic
1421 foreach ( (array) $subscribers as $subscriber ) {
1422 bbp_add_user_subscription( $subscriber, $destination_topic->ID );
1423 }
1424 }
1425 }
1426
1427 /** Favorites *********************************************************/
1428
1429 // Copy the favoriters if told to
1430 if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] ) {
1431
1432 // Get the favoriters
1433 $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
1434
1435 if ( !empty( $favoriters ) ) {
1436
1437 // Add the favoriters to new topic
1438 foreach ( (array) $favoriters as $favoriter ) {
1439 bbp_add_user_favorite( $favoriter, $destination_topic->ID );
1440 }
1441 }
1442 }
1443
1444 /** Tags **************************************************************/
1445
1446 // Copy the tags if told to
1447 if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) ) {
1448
1449 // Get the source topic tags
1450 $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
1451
1452 if ( !empty( $source_topic_tags ) ) {
1453 wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
1454 }
1455 }
1456
1457 /** Split Replies *************************************************/
1458
1459 // get_posts() is not used because it doesn't allow us to use '>='
1460 // comparision without a filter.
1461 $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() ) );
1462
1463 // Make sure there are replies to loop through
1464 if ( !empty( $replies ) && !is_wp_error( $replies ) ) {
1465
1466 // Change the post_parent of each reply to the destination topic id
1467 foreach ( $replies as $reply ) {
1468
1469 // New reply data
1470 $postarr = array(
1471 'ID' => $reply->ID,
1472 'post_title' => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
1473 'post_name' => false, // will be automatically generated
1474 'post_parent' => $destination_topic->ID,
1475 'guid' => ''
1476 );
1477
1478 // Update the reply
1479 wp_update_post( $postarr );
1480
1481 // Adjust reply meta values
1482 bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID );
1483 bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
1484
1485 // Do additional actions per split reply
1486 do_action( 'bbp_split_topic_reply', $reply->ID, $destination_topic->ID );
1487 }
1488 }
1489
1490 // It is a new topic and we need to set some default metas to make
1491 // the topic display in bbp_has_topics() list
1492 if ( 'reply' == $split_option ) {
1493 $last_reply_id = ( empty( $reply ) || empty( $reply->ID ) ) ? 0 : $reply->ID;
1494 $freshness = ( empty( $reply ) || empty( $reply->post_date ) ) ? '' : $reply->post_date;
1495
1496 bbp_update_topic_last_reply_id ( $destination_topic->ID, $last_reply_id );
1497 bbp_update_topic_last_active_time( $destination_topic->ID, $freshness );
1498 }
1499
1500 /** Successful Split **************************************************/
1501
1502 // Update counts, etc...
1503 do_action( 'bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
1504
1505 // Redirect back to the topic
1506 wp_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
1507
1508 // For good measure
1509 exit();
1510 }
1511 }
1512
1513 /**
1514 * Fix counts on topic split
1515 *
1516 * When a topic is split, update the counts of source and destination topic
1517 * and their forums.
1518 *
1519 * @since bbPress (r2756)
1520 *
1521 * @param int $from_reply_id From reply id
1522 * @param int $source_topic_id Source topic id
1523 * @param int $destination_topic_id Destination topic id
1524 * @uses bbp_update_forum_topic_count() To update the forum topic counts
1525 * @uses bbp_update_forum_reply_count() To update the forum reply counts
1526 * @uses bbp_update_topic_reply_count() To update the topic reply counts
1527 * @uses bbp_update_topic_voice_count() To update the topic voice counts
1528 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
1529 * count
1530 * @uses do_action() Calls 'bbp_split_topic_count' with the from reply id,
1531 * source topic id & destination topic id
1532 */
1533 function bbp_split_topic_count( $from_reply_id, $source_topic_id, $destination_topic_id ) {
1534
1535 // Forum Topic Counts
1536 bbp_update_forum_topic_count( $destination_topic_id );
1537
1538 // Forum Reply Counts
1539 bbp_update_forum_reply_count( $destination_topic_id );
1540
1541 // Topic Reply Counts
1542 bbp_update_topic_reply_count( $source_topic_id );
1543 bbp_update_topic_reply_count( $destination_topic_id );
1544
1545 // Topic Hidden Reply Counts
1546 bbp_update_topic_reply_count_hidden( $source_topic_id );
1547 bbp_update_topic_reply_count_hidden( $destination_topic_id );
1548
1549 // Topic Voice Counts
1550 bbp_update_topic_voice_count( $source_topic_id );
1551 bbp_update_topic_voice_count( $destination_topic_id );
1552
1553 do_action( 'bbp_split_topic_count', $from_reply_id, $source_topic_id, $destination_topic_id );
1554 }
1555
1556 /**
1557 * Handles the front end tag management (renaming, merging, destroying)
1558 *
1559 * @since bbPress (r2768)
1560 *
1561 * @uses check_admin_referer() To verify the nonce and check the referer
1562 * @uses current_user_can() To check if the current user can edit/delete tags
1563 * @uses bbPress::errors::add() To log the error messages
1564 * @uses wp_update_term() To update the topic tag
1565 * @uses get_term_link() To get the topic tag url
1566 * @uses term_exists() To check if the topic tag already exists
1567 * @uses wp_insert_term() To insert a topic tag
1568 * @uses wp_delete_term() To delete the topic tag
1569 * @uses home_url() To get the blog's home page url
1570 * @uses do_action() Calls actions based on the actions with associated args
1571 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
1572 * @uses wp_redirect() To redirect to the url
1573 */
1574 function bbp_manage_topic_tag_handler() {
1575
1576 // Bail if not a POST action
1577 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1578 return;
1579
1580 // Bail if required POST actions aren't passed
1581 if ( empty( $_POST['tag-id'] ) || empty( $_POST['action'] ) )
1582 return;
1583
1584 // Setup possible get actions
1585 $possible_actions = array(
1586 'bbp-update-topic-tag',
1587 'bbp-merge-topic-tag',
1588 'bbp-delete-topic-tag'
1589 );
1590
1591 // Bail if actions aren't meant for this function
1592 if ( !in_array( $_POST['action'], $possible_actions ) )
1593 return;
1594
1595 // Setup vars
1596 $action = $_POST['action'];
1597 $tag_id = (int) $_POST['tag-id'];
1598 $tag = get_term( $tag_id, bbp_get_topic_tag_tax_id() );
1599
1600 // Tag does not exist
1601 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1602 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() ) );
1603 return;
1604 }
1605
1606 // What action are we trying to perform?
1607 switch ( $action ) {
1608
1609 // Update tag
1610 case 'bbp-update-topic-tag' :
1611
1612 // Nonce check
1613 check_admin_referer( 'update-tag_' . $tag_id );
1614
1615 // Can user edit topic tags?
1616 if ( !current_user_can( 'edit_topic_tags' ) ) {
1617 bbp_add_error( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
1618 return;
1619 }
1620
1621 // No tag name was provided
1622 if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
1623 bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
1624 return;
1625 }
1626
1627 // Attempt to update the tag
1628 $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : '';
1629 $tag = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) );
1630
1631 // Cannot update tag
1632 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1633 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() ) );
1634 return;
1635 }
1636
1637 // Redirect
1638 $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() );
1639
1640 // Update counts, etc...
1641 do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug );
1642
1643 break;
1644
1645 // Merge two tags
1646 case 'bbp-merge-topic-tag' :
1647
1648 // Nonce check
1649 check_admin_referer( 'merge-tag_' . $tag_id );
1650
1651 // Can user edit topic tags?
1652 if ( !current_user_can( 'edit_topic_tags' ) ) {
1653 bbp_add_error( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
1654 return;
1655 }
1656
1657 // No tag name was provided
1658 if ( empty( $_POST['tag-existing-name'] ) || !$name = $_POST['tag-existing-name'] ) {
1659 bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
1660 return;
1661 }
1662
1663 // If term does not exist, create it
1664 if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) )
1665 $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() );
1666
1667 // Problem inserting the new term
1668 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1669 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() ) );
1670 return;
1671 }
1672
1673 // Merging in to...
1674 $to_tag = $tag['term_id'];
1675
1676 // Attempting to merge a tag into itself
1677 if ( $tag_id == $to_tag ) {
1678 bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'bbpress' ) );
1679 return;
1680 }
1681
1682 // Delete the old term
1683 $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) );
1684
1685 // Error merging the terms
1686 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1687 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() ) );
1688 return;
1689 }
1690
1691 // Redirect
1692 $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() );
1693
1694 // Update counts, etc...
1695 do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag );
1696
1697 break;
1698
1699 // Delete tag
1700 case 'bbp-delete-topic-tag' :
1701
1702 // Nonce check
1703 check_admin_referer( 'delete-tag_' . $tag_id );
1704
1705 // Can user delete topic tags?
1706 if ( !current_user_can( 'delete_topic_tags' ) ) {
1707 bbp_add_error( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the topic tags.', 'bbpress' ) );
1708 return;
1709 }
1710
1711 // Attempt to delete term
1712 $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() );
1713
1714 // Error deleting term
1715 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1716 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() ) );
1717 return;
1718 }
1719
1720 // We don't have any other place to go other than home! Or we may die because of the 404 disease
1721 $redirect = home_url();
1722
1723 // Update counts, etc...
1724 do_action( 'bbp_delete_topic_tag', $tag_id, $tag );
1725
1726 break;
1727 }
1728
1729 /** Successful Moderation *************************************************/
1730
1731 // Redirect back
1732 $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url();
1733 wp_safe_redirect( $redirect );
1734
1735 // For good measure
1736 exit();
1737 }
1738
1739 /** Stickies ******************************************************************/
1740
1741 /**
1742 * Return sticky topics of a forum
1743 *
1744 * @since bbPress (r2592)
1745 *
1746 * @param int $forum_id Optional. If not passed, super stickies are returned.
1747 * @uses bbp_get_super_stickies() To get the super stickies
1748 * @uses get_post_meta() To get the forum stickies
1749 * @uses apply_filters() Calls 'bbp_get_stickies' with the stickies and forum id
1750 * @return array IDs of sticky topics of a forum or super stickies
1751 */
1752 function bbp_get_stickies( $forum_id = 0 ) {
1753 $stickies = empty( $forum_id ) ? bbp_get_super_stickies() : get_post_meta( $forum_id, '_bbp_sticky_topics', true );
1754 $stickies = ( empty( $stickies ) || !is_array( $stickies ) ) ? array() : $stickies;
1755
1756 return apply_filters( 'bbp_get_stickies', $stickies, (int) $forum_id );
1757 }
1758
1759 /**
1760 * Return topics stuck to front page of the forums
1761 *
1762 * @since bbPress (r2592)
1763 *
1764 * @uses get_option() To get super sticky topics
1765 * @uses apply_filters() Calls 'bbp_get_super_stickies' with the stickies
1766 * @return array IDs of super sticky topics
1767 */
1768 function bbp_get_super_stickies() {
1769 $stickies = get_option( '_bbp_super_sticky_topics', array() );
1770 $stickies = ( empty( $stickies ) || !is_array( $stickies ) ) ? array() : $stickies;
1771
1772 return apply_filters( 'bbp_get_super_stickies', $stickies );
1773 }
1774
1775 /** Topics Actions ************************************************************/
1776
1777 /**
1778 * Handles the front end opening/closing, spamming/unspamming,
1779 * sticking/unsticking and trashing/untrashing/deleting of topics
1780 *
1781 * @since bbPress (r2727)
1782 *
1783 * @uses bbp_get_topic() To get the topic
1784 * @uses current_user_can() To check if the user is capable of editing or
1785 * deleting the topic
1786 * @uses bbp_get_topic_post_type() To get the topic post type
1787 * @uses check_ajax_referer() To verify the nonce and check the referer
1788 * @uses bbp_is_topic_open() To check if the topic is open
1789 * @uses bbp_close_topic() To close the topic
1790 * @uses bbp_open_topic() To open the topic
1791 * @uses bbp_is_topic_sticky() To check if the topic is a sticky
1792 * @uses bbp_unstick_topic() To unstick the topic
1793 * @uses bbp_stick_topic() To stick the topic
1794 * @uses bbp_is_topic_spam() To check if the topic is marked as spam
1795 * @uses bbp_spam_topic() To make the topic as spam
1796 * @uses bbp_unspam_topic() To unmark the topic as spam
1797 * @uses wp_trash_post() To trash the topic
1798 * @uses wp_untrash_post() To untrash the topic
1799 * @uses wp_delete_post() To delete the topic
1800 * @uses do_action() Calls 'bbp_toggle_topic_handler' with success, post data
1801 * and action
1802 * @uses bbp_get_forum_permalink() To get the forum link
1803 * @uses bbp_get_topic_permalink() To get the topic link
1804 * @uses add_query_arg() To add args to the url
1805 * @uses wp_redirect() To redirect to the topic
1806 * @uses bbPress::errors:add() To log the error messages
1807 */
1808 function bbp_toggle_topic_handler() {
1809
1810 // Bail if not a GET action
1811 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1812 return;
1813
1814 // Bail if required GET actions aren't passed
1815 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
1816 return;
1817
1818 // Setup possible get actions
1819 $possible_actions = array(
1820 'bbp_toggle_topic_close',
1821 'bbp_toggle_topic_stick',
1822 'bbp_toggle_topic_spam',
1823 'bbp_toggle_topic_trash'
1824 );
1825
1826 // Bail if actions aren't meant for this function
1827 if ( !in_array( $_GET['action'], $possible_actions ) )
1828 return;
1829
1830 $failure = ''; // Empty failure string
1831 $view_all = false; // Assume not viewing all
1832 $action = $_GET['action']; // What action is taking place?
1833 $topic_id = (int) $_GET['topic_id']; // What's the topic id?
1834 $success = false; // Flag
1835 $post_data = array( 'ID' => $topic_id ); // Prelim array
1836 $redirect = ''; // Empty redirect URL
1837
1838 // Make sure topic exists
1839 $topic = bbp_get_topic( $topic_id );
1840 if ( empty( $topic ) )
1841 return;
1842
1843 // What is the user doing here?
1844 if ( !current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' == $action && !current_user_can( 'delete_topic', $topic->ID ) ) ) {
1845 bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that.', 'bbpress' ) );
1846 return;
1847 }
1848
1849 // What action are we trying to perform?
1850 switch ( $action ) {
1851
1852 // Toggle open/close
1853 case 'bbp_toggle_topic_close' :
1854 check_ajax_referer( 'close-topic_' . $topic_id );
1855
1856 $is_open = bbp_is_topic_open( $topic_id );
1857 $success = $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id );
1858 $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' );
1859
1860 break;
1861
1862 // Toggle sticky/super-sticky/unstick
1863 case 'bbp_toggle_topic_stick' :
1864 check_ajax_referer( 'stick-topic_' . $topic_id );
1865
1866 $is_sticky = bbp_is_topic_sticky( $topic_id );
1867 $is_super = ( empty( $is_sticky ) && !empty( $_GET['super'] ) && 1 == (int) $_GET['super'] ) ? true : false;
1868 $success = $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );
1869 $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' );
1870
1871 break;
1872
1873 // Toggle spam
1874 case 'bbp_toggle_topic_spam' :
1875 check_ajax_referer( 'spam-topic_' . $topic_id );
1876
1877 $is_spam = bbp_is_topic_spam( $topic_id );
1878 $success = $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id );
1879 $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' );
1880 $view_all = !$is_spam;
1881
1882 break;
1883
1884 // Toggle trash
1885 case 'bbp_toggle_topic_trash' :
1886
1887 $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
1888
1889 if ( empty( $sub_action ) )
1890 break;
1891
1892 switch ( $sub_action ) {
1893 case 'trash':
1894 check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id );
1895
1896 $view_all = true;
1897 $success = wp_trash_post( $topic_id );
1898 $failure = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
1899
1900 break;
1901
1902 case 'untrash':
1903 check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id );
1904
1905 $success = wp_untrash_post( $topic_id );
1906 $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
1907
1908 break;
1909
1910 case 'delete':
1911 check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id );
1912
1913 $success = wp_delete_post( $topic_id );
1914 $failure = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
1915
1916 break;
1917 }
1918
1919 break;
1920 }
1921
1922 // Do additional topic toggle actions
1923 do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action );
1924
1925 // No errors
1926 if ( false != $success && !is_wp_error( $success ) ) {
1927
1928 // Redirect back to the topic's forum
1929 if ( isset( $sub_action ) && ( 'delete' == $sub_action ) ) {
1930 $redirect = bbp_get_forum_permalink( $success->post_parent );
1931
1932 // Redirect back to the topic
1933 } else {
1934
1935 // Get the redirect detination
1936 $permalink = bbp_get_topic_permalink( $topic_id );
1937 $redirect = bbp_add_view_all( $permalink, $view_all );
1938 }
1939
1940 wp_redirect( $redirect );
1941
1942 // For good measure
1943 exit();
1944
1945 // Handle errors
1946 } else {
1947 bbp_add_error( 'bbp_toggle_topic', $failure );
1948 }
1949 }
1950
1951 /** Favorites & Subscriptions *************************************************/
1952
1953 /**
1954 * Remove a deleted topic from all users' favorites
1955 *
1956 * @since bbPress (r2652)
1957 *
1958 * @param int $topic_id Topic ID to remove
1959 * @uses bbp_get_topic_favoriters() To get the topic's favoriters
1960 * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
1961 */
1962 function bbp_remove_topic_from_all_favorites( $topic_id = 0 ) {
1963 $topic_id = bbp_get_topic_id( $topic_id );
1964
1965 // Bail if no topic
1966 if ( empty( $topic_id ) )
1967 return;
1968
1969 // Get users
1970 $users = (array) bbp_get_topic_favoriters( $topic_id );
1971
1972 // Users exist
1973 if ( !empty( $users ) ) {
1974
1975 // Loop through users
1976 foreach ( $users as $user ) {
1977
1978 // Remove each user
1979 bbp_remove_user_favorite( $user, $topic_id );
1980 }
1981 }
1982 }
1983
1984 /**
1985 * Remove a deleted topic from all users' subscriptions
1986 *
1987 * @since bbPress (r2652)
1988 *
1989 * @param int $topic_id Topic ID to remove
1990 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
1991 * @uses bbp_get_topic_subscribers() To get the topic subscribers
1992 * @uses bbp_remove_user_subscription() To remove the user subscription
1993 */
1994 function bbp_remove_topic_from_all_subscriptions( $topic_id = 0 ) {
1995
1996 // Subscriptions are not active
1997 if ( !bbp_is_subscriptions_active() )
1998 return;
1999
2000 $topic_id = bbp_get_topic_id( $topic_id );
2001
2002 // Bail if no topic
2003 if ( empty( $topic_id ) )
2004 return;
2005
2006 // Get users
2007 $users = (array) bbp_get_topic_subscribers( $topic_id );
2008
2009 // Users exist
2010 if ( !empty( $users ) ) {
2011
2012 // Loop through users
2013 foreach ( $users as $user ) {
2014
2015 // Remove each user
2016 bbp_remove_user_subscription( $user, $topic_id );
2017 }
2018 }
2019 }
2020
2021 /** Topic Updaters ************************************************************/
2022
2023 /**
2024 * Update the topic's forum id
2025 *
2026 * @since bbPress (r2855)
2027 *
2028 * @param int $topic_id Optional. Topic id to update
2029 * @param int $forum_id Optional. Forum id
2030 * @uses bbp_is_reply() TO check if the passed topic id is a reply
2031 * @uses bbp_get_reply_topic_id() To get the reply topic id
2032 * @uses bbp_get_topic_id() To get the topic id
2033 * @uses get_post_field() To get the post parent of the topic id
2034 * @uses bbp_get_forum_id() To get the forum id
2035 * @uses update_post_meta() To update the topic forum id meta
2036 * @uses apply_filters() Calls 'bbp_update_topic_forum_id' with the forum id
2037 * and topic id
2038 * @return int Forum id
2039 */
2040 function bbp_update_topic_forum_id( $topic_id = 0, $forum_id = 0 ) {
2041
2042 // If it's a reply, then get the parent (topic id)
2043 if ( bbp_is_reply( $topic_id ) )
2044 $topic_id = bbp_get_reply_topic_id( $topic_id );
2045 else
2046 $topic_id = bbp_get_topic_id( $topic_id );
2047
2048 if ( empty( $forum_id ) )
2049 $forum_id = get_post_field( 'post_parent', $topic_id );
2050
2051 update_post_meta( $topic_id, '_bbp_forum_id', (int) $forum_id );
2052
2053 return apply_filters( 'bbp_update_topic_forum_id', (int) $forum_id, $topic_id );
2054 }
2055
2056 /**
2057 * Update the topic's topic id
2058 *
2059 * @since bbPress (r2954)
2060 *
2061 * @param int $topic_id Optional. Topic id to update
2062 * @uses bbp_get_topic_id() To get the topic id
2063 * @uses update_post_meta() To update the topic's topic id meta
2064 * @uses apply_filters() Calls 'bbp_update_topic_topic_id' with the topic id
2065 * @return int Topic id
2066 */
2067 function bbp_update_topic_topic_id( $topic_id = 0 ) {
2068 $topic_id = bbp_get_topic_id( $topic_id );
2069
2070 update_post_meta( $topic_id, '_bbp_topic_id', (int) $topic_id );
2071
2072 return apply_filters( 'bbp_update_topic_topic_id', (int) $topic_id );
2073 }
2074
2075 /**
2076 * Adjust the total reply count of a topic
2077 *
2078 * @since bbPress (r2467)
2079 *
2080 * @param int $topic_id Optional. Topic id to update
2081 * @param int $reply_count Optional. Set the reply count manually.
2082 * @uses bbp_is_reply() To check if the passed topic id is a reply
2083 * @uses bbp_get_reply_topic_id() To get the reply topic id
2084 * @uses bbp_get_topic_id() To get the topic id
2085 * @uses bbp_get_reply_post_type() To get the reply post type
2086 * @uses bbp_get_public_child_count() To get the reply count
2087 * @uses update_post_meta() To update the topic reply count meta
2088 * @uses apply_filters() Calls 'bbp_update_topic_reply_count' with the reply
2089 * count and topic id
2090 * @return int Topic reply count
2091 */
2092 function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = 0 ) {
2093
2094 // If it's a reply, then get the parent (topic id)
2095 if ( bbp_is_reply( $topic_id ) )
2096 $topic_id = bbp_get_reply_topic_id( $topic_id );
2097 else
2098 $topic_id = bbp_get_topic_id( $topic_id );
2099
2100 // Get replies of topic if not passed
2101 if ( empty( $reply_count ) )
2102 $reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
2103
2104 update_post_meta( $topic_id, '_bbp_reply_count', (int) $reply_count );
2105
2106 return apply_filters( 'bbp_update_topic_reply_count', (int) $reply_count, $topic_id );
2107 }
2108
2109 /**
2110 * Adjust the total hidden reply count of a topic (hidden includes trashed and spammed replies)
2111 *
2112 * @since bbPress (r2740)
2113 *
2114 * @param int $topic_id Optional. Topic id to update
2115 * @param int $reply_count Optional. Set the reply count manually
2116 * @uses bbp_is_reply() To check if the passed topic id is a reply
2117 * @uses bbp_get_reply_topic_id() To get the reply topic id
2118 * @uses bbp_get_topic_id() To get the topic id
2119 * @uses bbp_get_reply_post_type() To get the reply post type
2120 * @uses wpdb::prepare() To prepare our sql query
2121 * @uses wpdb::get_var() To execute our query and get the var back
2122 * @uses update_post_meta() To update the topic hidden reply count meta
2123 * @uses apply_filters() Calls 'bbp_update_topic_reply_count_hidden' with the
2124 * hidden reply count and topic id
2125 * @return int Topic hidden reply count
2126 */
2127 function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
2128 global $wpdb;
2129
2130 // If it's a reply, then get the parent (topic id)
2131 if ( bbp_is_reply( $topic_id ) )
2132 $topic_id = bbp_get_reply_topic_id( $topic_id );
2133 else
2134 $topic_id = bbp_get_topic_id( $topic_id );
2135
2136 // Get replies of topic
2137 if ( empty( $reply_count ) )
2138 $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() ) );
2139
2140 update_post_meta( $topic_id, '_bbp_reply_count_hidden', (int) $reply_count );
2141
2142 return apply_filters( 'bbp_update_topic_reply_count_hidden', (int) $reply_count, $topic_id );
2143 }
2144
2145 /**
2146 * Update the topic with the last active post ID
2147 *
2148 * @since bbPress (r2888)
2149 *
2150 * @param int $topic_id Optional. Topic id to update
2151 * @param int $active_id Optional. active id
2152 * @uses bbp_is_reply() To check if the passed topic id is a reply
2153 * @uses bbp_get_reply_topic_id() To get the reply topic id
2154 * @uses bbp_get_topic_id() To get the topic id
2155 * @uses bbp_get_reply_post_type() To get the reply post type
2156 * @uses bbp_get_public_child_last_id() To get the last public reply id
2157 * @uses bbp_get_active_id() To get the active id
2158 * @uses update_post_meta() To update the topic last active id meta
2159 * @uses apply_filters() Calls 'bbp_update_topic_last_active_id' with the active
2160 * id and topic id
2161 * @return int Active id
2162 */
2163 function bbp_update_topic_last_active_id( $topic_id = 0, $active_id = 0 ) {
2164
2165 // If it's a reply, then get the parent (topic id)
2166 if ( bbp_is_reply( $topic_id ) )
2167 $topic_id = bbp_get_reply_topic_id( $topic_id );
2168 else
2169 $topic_id = bbp_get_topic_id( $topic_id );
2170
2171 if ( empty( $active_id ) )
2172 $active_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
2173
2174 // Adjust last_id's based on last_reply post_type
2175 if ( empty( $active_id ) || !bbp_is_reply( $active_id ) )
2176 $active_id = $topic_id;
2177
2178 // Update only if published
2179 if ( bbp_get_public_status_id() == get_post_status( $active_id ) )
2180 update_post_meta( $topic_id, '_bbp_last_active_id', (int) $active_id );
2181
2182 return apply_filters( 'bbp_update_topic_last_active_id', (int) $active_id, $topic_id );
2183 }
2184
2185 /**
2186 * Update the topics last active date/time (aka freshness)
2187 *
2188 * @since bbPress (r2680)
2189 *
2190 * @param int $topic_id Optional. Topic id
2191 * @param string $new_time Optional. New time in mysql format
2192 * @uses bbp_get_topic_id() To get the topic id
2193 * @uses bbp_get_reply_topic_id() To get the reply topic id
2194 * @uses current_time() To get the current time
2195 * @uses update_post_meta() To update the topic last active meta
2196 * @return bool True on success, false on failure
2197 */
2198 function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) {
2199
2200 // If it's a reply, then get the parent (topic id)
2201 if ( bbp_is_reply( $topic_id ) )
2202 $topic_id = bbp_get_reply_topic_id( $topic_id );
2203 else
2204 $topic_id = bbp_get_topic_id( $topic_id );
2205
2206 // Check time and use current if empty
2207 if ( empty( $new_time ) )
2208 $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) );
2209
2210 // Update only if published
2211 if ( !empty( $new_time ) )
2212 update_post_meta( $topic_id, '_bbp_last_active_time', $new_time );
2213
2214 return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id );
2215 }
2216
2217 /**
2218 * Update the topic with the most recent reply ID
2219 *
2220 * @since bbPress (r2625)
2221 *
2222 * @param int $topic_id Optional. Topic id to update
2223 * @param int $reply_id Optional. Reply id
2224 * @uses bbp_is_reply() To check if the passed topic id is a reply
2225 * @uses bbp_get_reply_id() To get the reply id
2226 * @uses bbp_get_reply_topic_id() To get the reply topic id
2227 * @uses bbp_get_topic_id() To get the topic id
2228 * @uses bbp_get_reply_post_type() To get the reply post type
2229 * @uses bbp_get_public_child_last_id() To get the last public reply id
2230 * @uses update_post_meta() To update the topic last reply id meta
2231 * @uses apply_filters() Calls 'bbp_update_topic_last_reply_id' with the reply
2232 * id and topic id
2233 * @return int Reply id
2234 */
2235 function bbp_update_topic_last_reply_id( $topic_id = 0, $reply_id = 0 ) {
2236
2237 // If it's a reply, then get the parent (topic id)
2238 if ( empty( $reply_id ) && bbp_is_reply( $topic_id ) ) {
2239 $reply_id = bbp_get_reply_id( $topic_id );
2240 $topic_id = bbp_get_reply_topic_id( $reply_id );
2241 } else {
2242 $reply_id = bbp_get_reply_id( $reply_id );
2243 $topic_id = bbp_get_topic_id( $topic_id );
2244 }
2245
2246 if ( empty( $reply_id ) )
2247 $reply_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
2248
2249 // Adjust last_id's based on last_reply post_type
2250 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
2251 $reply_id = 0;
2252
2253 // Update if reply is published
2254 if ( bbp_is_reply_published( $reply_id ) )
2255 update_post_meta( $topic_id, '_bbp_last_reply_id', (int) $reply_id );
2256
2257 return apply_filters( 'bbp_update_topic_last_reply_id', (int) $reply_id, $topic_id );
2258 }
2259
2260 /**
2261 * Adjust the total voice count of a topic
2262 *
2263 * @since bbPress (r2567)
2264 *
2265 * @param int $topic_id Optional. Topic id to update
2266 * @uses bbp_is_reply() To check if the passed topic id is a reply
2267 * @uses bbp_get_reply_topic_id() To get the reply topic id
2268 * @uses bbp_get_topic_id() To get the topic id
2269 * @uses bbp_get_reply_topic_id() To get the reply topic id
2270 * @uses bbp_get_reply_post_type() To get the reply post type
2271 * @uses bbp_get_topic_post_type() To get the topic post type
2272 * @uses wpdb::prepare() To prepare our sql query
2273 * @uses wpdb::get_col() To execute our query and get the column back
2274 * @uses update_post_meta() To update the topic voice count meta
2275 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
2276 * count and topic id
2277 * @return int Voice count
2278 */
2279 function bbp_update_topic_voice_count( $topic_id = 0 ) {
2280 global $wpdb;
2281
2282 // If it's a reply, then get the parent (topic id)
2283 if ( bbp_is_reply( $topic_id ) )
2284 $topic_id = bbp_get_reply_topic_id( $topic_id );
2285 elseif ( bbp_is_topic( $topic_id ) )
2286 $topic_id = bbp_get_topic_id( $topic_id );
2287 else
2288 return;
2289
2290 // Query the DB to get voices in this topic
2291 $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() ) );
2292
2293 // If there's an error, make sure we at least have 1 voice
2294 $voices = ( empty( $voices ) || is_wp_error( $voices ) ) ? 1 : $voices[0];
2295
2296 // Update the voice count for this topic id
2297 update_post_meta( $topic_id, '_bbp_voice_count', (int) $voices );
2298
2299 return apply_filters( 'bbp_update_topic_voice_count', (int) $voices, $topic_id );
2300 }
2301
2302 /**
2303 * Adjust the total anonymous reply count of a topic
2304 *
2305 * @since bbPress (r2567)
2306 *
2307 * @param int $topic_id Optional. Topic id to update
2308 * @uses bbp_is_reply() To check if the passed topic id is a reply
2309 * @uses bbp_get_reply_topic_id() To get the reply topic id
2310 * @uses bbp_get_topic_id() To get the topic id
2311 * @uses bbp_get_reply_topic_id() To get the reply topic id
2312 * @uses bbp_get_reply_post_type() To get the reply post type
2313 * @uses bbp_get_topic_post_type() To get the topic post type
2314 * @uses wpdb::prepare() To prepare our sql query
2315 * @uses wpdb::get_col() To execute our query and get the column back
2316 * @uses update_post_meta() To update the topic anonymous reply count meta
2317 * @uses apply_filters() Calls 'bbp_update_topic_anonymous_reply_count' with the
2318 * anonymous reply count and topic id
2319 * @return int Anonymous reply count
2320 */
2321 function bbp_update_topic_anonymous_reply_count( $topic_id = 0 ) {
2322 global $wpdb;
2323
2324 // If it's a reply, then get the parent (topic id)
2325 if ( bbp_is_reply( $topic_id ) )
2326 $topic_id = bbp_get_reply_topic_id( $topic_id );
2327 elseif ( bbp_is_topic( $topic_id ) )
2328 $topic_id = bbp_get_topic_id( $topic_id );
2329 else
2330 return;
2331
2332 $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() ) );
2333
2334 update_post_meta( $topic_id, '_bbp_anonymous_reply_count', (int) $anonymous_replies );
2335
2336 return apply_filters( 'bbp_update_topic_anonymous_reply_count', (int) $anonymous_replies, $topic_id );
2337 }
2338
2339 /**
2340 * Update the revision log of the topic
2341 *
2342 * @since bbPress (r2782)
2343 *
2344 * @param mixed $args Supports these args:
2345 * - topic_id: Topic id
2346 * - author_id: Author id
2347 * - reason: Reason for editing
2348 * - revision_id: Revision id
2349 * @uses bbp_get_topic_id() To get the topic id
2350 * @uses bbp_get_user_id() To get the user id
2351 * @uses bbp_format_revision_reason() To format the reason
2352 * @uses bbp_get_topic_raw_revision_log() To get the raw topic revision log
2353 * @uses update_post_meta() To update the topic revision log meta
2354 * @return mixed False on failure, true on success
2355 */
2356 function bbp_update_topic_revision_log( $args = '' ) {
2357 $defaults = array (
2358 'reason' => '',
2359 'topic_id' => 0,
2360 'author_id' => 0,
2361 'revision_id' => 0
2362 );
2363
2364 $r = wp_parse_args( $args, $defaults );
2365 extract( $r );
2366
2367 // Populate the variables
2368 $reason = bbp_format_revision_reason( $reason );
2369 $topic_id = bbp_get_topic_id( $topic_id );
2370 $author_id = bbp_get_user_id ( $author_id, false, true );
2371 $revision_id = (int) $revision_id;
2372
2373 // Get the logs and append the new one to those
2374 $revision_log = bbp_get_topic_raw_revision_log( $topic_id );
2375 $revision_log[$revision_id] = array( 'author' => $author_id, 'reason' => $reason );
2376
2377 // Finally, update
2378 return update_post_meta( $topic_id, '_bbp_revision_log', $revision_log );
2379 }
2380
2381 /** Topic Actions *************************************************************/
2382
2383 /**
2384 * Closes a topic
2385 *
2386 * @since bbPress (r2740)
2387 *
2388 * @param int $topic_id Topic id
2389 * @uses wp_get_single_post() To get the topic
2390 * @uses do_action() Calls 'bbp_close_topic' with the topic id
2391 * @uses add_post_meta() To add the previous status to a meta
2392 * @uses wp_insert_post() To update the topic with the new status
2393 * @uses do_action() Calls 'bbp_opened_topic' with the topic id
2394 * @return mixed False or {@link WP_Error} on failure, topic id on success
2395 */
2396 function bbp_close_topic( $topic_id = 0 ) {
2397
2398 // Get topic
2399 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2400 return $topic;
2401
2402 // Bail if already closed
2403 if ( bbp_get_closed_status_id() == $topic['post_status'] )
2404 return false;
2405
2406 // Execute pre close code
2407 do_action( 'bbp_close_topic', $topic_id );
2408
2409 // Add pre close status
2410 add_post_meta( $topic_id, '_bbp_status', $topic['post_status'] );
2411
2412 // Set closed status
2413 $topic['post_status'] = bbp_get_closed_status_id();
2414
2415 // No revisions
2416 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2417
2418 // Update topic
2419 $topic_id = wp_insert_post( $topic );
2420
2421 // Execute post close code
2422 do_action( 'bbp_closed_topic', $topic_id );
2423
2424 // Return topic_id
2425 return $topic_id;
2426 }
2427
2428 /**
2429 * Opens a topic
2430 *
2431 * @since bbPress (r2740)
2432 *
2433 * @param int $topic_id Topic id
2434 * @uses wp_get_single_post() To get the topic
2435 * @uses do_action() Calls 'bbp_open_topic' with the topic id
2436 * @uses get_post_meta() To get the previous status
2437 * @uses delete_post_meta() To delete the previous status meta
2438 * @uses wp_insert_post() To update the topic with the new status
2439 * @uses do_action() Calls 'bbp_opened_topic' with the topic id
2440 * @return mixed False or {@link WP_Error} on failure, topic id on success
2441 */
2442 function bbp_open_topic( $topic_id = 0 ) {
2443
2444 // Get topic
2445 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2446 return $topic;
2447
2448 // Bail if already open
2449 if ( bbp_get_closed_status_id() != $topic['post_status'])
2450 return false;
2451
2452 // Execute pre open code
2453 do_action( 'bbp_open_topic', $topic_id );
2454
2455 // Get previous status
2456 $topic_status = get_post_meta( $topic_id, '_bbp_status', true );
2457
2458 // Set previous status
2459 $topic['post_status'] = $topic_status;
2460
2461 // Remove old status meta
2462 delete_post_meta( $topic_id, '_bbp_status' );
2463
2464 // No revisions
2465 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2466
2467 // Update topic
2468 $topic_id = wp_insert_post( $topic );
2469
2470 // Execute post open code
2471 do_action( 'bbp_opened_topic', $topic_id );
2472
2473 // Return topic_id
2474 return $topic_id;
2475 }
2476
2477 /**
2478 * Marks a topic as spam
2479 *
2480 * @since bbPress (r2740)
2481 *
2482 * @param int $topic_id Topic id
2483 * @uses wp_get_single_post() To get the topic
2484 * @uses do_action() Calls 'bbp_spam_topic' with the topic id
2485 * @uses add_post_meta() To add the previous status to a meta
2486 * @uses wp_insert_post() To update the topic with the new status
2487 * @uses do_action() Calls 'bbp_spammed_topic' with the topic id
2488 * @return mixed False or {@link WP_Error} on failure, topic id on success
2489 */
2490 function bbp_spam_topic( $topic_id = 0 ) {
2491
2492 // Get the topic
2493 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2494 return $topic;
2495
2496 // Bail if topic is spam
2497 if ( bbp_get_spam_status_id() == $topic['post_status'] )
2498 return false;
2499
2500 // Execute pre spam code
2501 do_action( 'bbp_spam_topic', $topic_id );
2502
2503 // Add the original post status as post meta for future restoration
2504 add_post_meta( $topic_id, '_bbp_spam_meta_status', $topic['post_status'] );
2505
2506 // Get topic tags
2507 $terms = get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() );
2508
2509 // Define local variable(s)
2510 $term_names = array();
2511
2512 // Topic has tags
2513 if ( !empty( $terms ) ) {
2514
2515 // Loop through and collect term names
2516 foreach( $terms as $term ) {
2517 $term_names[] = $term->name;
2518 }
2519
2520 // Topic terms have slugs
2521 if ( !empty( $term_names ) ) {
2522
2523 // Add the original post status as post meta for future restoration
2524 add_post_meta( $topic_id, '_bbp_spam_topic_tags', $term_names );
2525
2526 // Empty the topic of its tags
2527 $topic['tax_input'] = array( bbp_get_topic_tag_tax_id() => '' );
2528 }
2529 }
2530
2531 // Set post status to spam
2532 $topic['post_status'] = bbp_get_spam_status_id();
2533
2534 // No revisions
2535 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2536
2537 // Update the topic
2538 $topic_id = wp_insert_post( $topic );
2539
2540 // Execute post spam code
2541 do_action( 'bbp_spammed_topic', $topic_id );
2542
2543 // Return topic_id
2544 return $topic_id;
2545 }
2546
2547 /**
2548 * Unspams a topic
2549 *
2550 * @since bbPress (r2740)
2551 *
2552 * @param int $topic_id Topic id
2553 * @uses wp_get_single_post() To get the topic
2554 * @uses do_action() Calls 'bbp_unspam_topic' with the topic id
2555 * @uses get_post_meta() To get the previous status
2556 * @uses delete_post_meta() To delete the previous status meta
2557 * @uses wp_insert_post() To update the topic with the new status
2558 * @uses do_action() Calls 'bbp_unspammed_topic' with the topic id
2559 * @return mixed False or {@link WP_Error} on failure, topic id on success
2560 */
2561 function bbp_unspam_topic( $topic_id = 0 ) {
2562
2563 // Get the topic
2564 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2565 return $topic;
2566
2567 // Bail if already not spam
2568 if ( bbp_get_spam_status_id() != $topic['post_status'] )
2569 return false;
2570
2571 // Execute pre unspam code
2572 do_action( 'bbp_unspam_topic', $topic_id );
2573
2574 // Get pre spam status
2575 $topic_status = get_post_meta( $topic_id, '_bbp_spam_meta_status', true );
2576
2577 // Set post status to pre spam
2578 $topic['post_status'] = $topic_status;
2579
2580 // Delete pre spam meta
2581 delete_post_meta( $topic_id, '_bbp_spam_meta_status' );
2582
2583 // No revisions
2584 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2585
2586 // Get pre-spam topic tags
2587 $terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
2588
2589 // Topic had tags before it was spammed
2590 if ( !empty( $terms ) ) {
2591
2592 // Set the tax_input of the topic
2593 $topic['tax_input'] = array( bbp_get_topic_tag_tax_id() => $terms );
2594
2595 // Delete pre-spam topic tag meta
2596 delete_post_meta( $topic_id, '_bbp_spam_topic_tags' );
2597 }
2598
2599 // Update the topic
2600 $topic_id = wp_insert_post( $topic );
2601
2602 // Execute post unspam code
2603 do_action( 'bbp_unspammed_topic', $topic_id );
2604
2605 // Return topic_id
2606 return $topic_id;
2607 }
2608
2609 /**
2610 * Sticks a topic to a forum or front
2611 *
2612 * @since bbPress (r2754)
2613 *
2614 * @param int $topic_id Optional. Topic id
2615 * @param int $super Should we make the topic a super sticky?
2616 * @uses bbp_get_topic_id() To get the topic id
2617 * @uses bbp_unstick_topic() To unstick the topic
2618 * @uses bbp_get_topic_forum_id() To get the topic forum id
2619 * @uses bbp_get_stickies() To get the stickies
2620 * @uses do_action() 'bbp_stick_topic' with topic id and bool super
2621 * @uses update_option() To update the super stickies option
2622 * @uses update_post_meta() To update the forum stickies meta
2623 * @uses do_action() Calls 'bbp_sticked_topic' with the topic id, bool super
2624 * and success
2625 * @return bool True on success, false on failure
2626 */
2627 function bbp_stick_topic( $topic_id = 0, $super = false ) {
2628 $topic_id = bbp_get_topic_id( $topic_id );
2629
2630 // We may have a super sticky to which we want to convert into a normal sticky and vice versa
2631 // So, unstick the topic first to avoid any possible error
2632 bbp_unstick_topic( $topic_id );
2633
2634 $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
2635 $stickies = bbp_get_stickies( $forum_id );
2636
2637 do_action( 'bbp_stick_topic', $topic_id, $super );
2638
2639 if ( !is_array( $stickies ) )
2640 $stickies = array( $topic_id );
2641 else
2642 $stickies[] = $topic_id;
2643
2644 $stickies = array_unique( array_filter( $stickies ) );
2645
2646 $success = !empty( $super ) ? update_option( '_bbp_super_sticky_topics', $stickies ) : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
2647
2648 do_action( 'bbp_sticked_topic', $topic_id, $super, $success );
2649
2650 return $success;
2651 }
2652
2653 /**
2654 * Unsticks a topic both from front and it's forum
2655 *
2656 * @since bbPress (r2754)
2657 *
2658 * @param int $topic_id Optional. Topic id
2659 * @uses bbp_get_topic_id() To get the topic id
2660 * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
2661 * @uses bbp_get_topic_forum_id() To get the topic forum id
2662 * @uses bbp_get_stickies() To get the forum stickies
2663 * @uses do_action() Calls 'bbp_unstick_topic' with the topic id
2664 * @uses delete_option() To delete the super stickies option
2665 * @uses update_option() To update the super stickies option
2666 * @uses delete_post_meta() To delete the forum stickies meta
2667 * @uses update_post_meta() To update the forum stickies meta
2668 * @uses do_action() Calls 'bbp_unsticked_topic' with the topic id and success
2669 * @return bool Always true.
2670 */
2671 function bbp_unstick_topic( $topic_id = 0 ) {
2672 $topic_id = bbp_get_topic_id( $topic_id );
2673 $super = bbp_is_topic_super_sticky( $topic_id );
2674 $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
2675 $stickies = bbp_get_stickies( $forum_id );
2676 $offset = array_search( $topic_id, $stickies );
2677
2678 do_action( 'bbp_unstick_topic', $topic_id );
2679
2680 if ( empty( $stickies ) ) {
2681 $success = true;
2682 } elseif ( !in_array( $topic_id, $stickies ) ) {
2683 $success = true;
2684 } elseif ( false === $offset ) {
2685 $success = true;
2686 } else {
2687 array_splice( $stickies, $offset, 1 );
2688 if ( empty( $stickies ) )
2689 $success = !empty( $super ) ? delete_option( '_bbp_super_sticky_topics' ) : delete_post_meta( $forum_id, '_bbp_sticky_topics' );
2690 else
2691 $success = !empty( $super ) ? update_option( '_bbp_super_sticky_topics', $stickies ) : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
2692 }
2693
2694 do_action( 'bbp_unsticked_topic', $topic_id, $success );
2695
2696 return true;
2697 }
2698
2699 /** Before Delete/Trash/Untrash ***********************************************/
2700
2701 /**
2702 * Called before deleting a topic.
2703 *
2704 * This function is supplemental to the actual topic deletion which is
2705 * handled by WordPress core API functions. It is used to clean up after
2706 * a topic that is being deleted.
2707 *
2708 * @uses bbp_get_topic_id() To get the topic id
2709 * @uses bbp_is_topic() To check if the passed id is a topic
2710 * @uses do_action() Calls 'bbp_delete_topic' with the topic id
2711 * @uses bbp_has_replies() To check if the topic has replies
2712 * @uses bbp_replies() To loop through the replies
2713 * @uses bbp_the_reply() To set a reply as the current reply in the loop
2714 * @uses bbp_get_reply_id() To get the reply id
2715 * @uses wp_delete_post() To delete the reply
2716 */
2717 function bbp_delete_topic( $topic_id = 0 ) {
2718
2719 // Validate topic ID
2720 $topic_id = bbp_get_topic_id( $topic_id );
2721
2722 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2723 return false;
2724
2725 do_action( 'bbp_delete_topic', $topic_id );
2726
2727 // Valid topic/reply statuses
2728 $post_stati = join( ',', array( bbp_get_public_status_id(), bbp_get_spam_status_id(), bbp_get_trash_status_id() ) );
2729
2730 // Topic is being permanently deleted, so its replies gotta go too
2731 if ( bbp_has_replies( array(
2732 'post_type' => bbp_get_reply_post_type(),
2733 'post_status' => $post_stati,
2734 'posts_per_page' => -1,
2735 'meta_query' => array( array(
2736 'key' => '_bbp_topic_id',
2737 'value' => $topic_id,
2738 'compare' => '='
2739 ) )
2740 ) ) ) {
2741 while ( bbp_replies() ) {
2742 bbp_the_reply();
2743 wp_delete_post( bbp_get_reply_id(), true );
2744 }
2745 }
2746 }
2747
2748 /**
2749 * Called before trashing a topic
2750 *
2751 * This function is supplemental to the actual topic being trashed which is
2752 * handled by WordPress core API functions. It is used to clean up after
2753 * a topic that is being trashed.
2754 *
2755 * @uses bbp_get_topic_id() To get the topic id
2756 * @uses bbp_is_topic() To check if the passed id is a topic
2757 * @uses do_action() Calls 'bbp_trash_topic' with the topic id
2758 * @uses bbp_has_replies() To check if the topic has replies
2759 * @uses bbp_replies() To loop through the replies
2760 * @uses bbp_the_reply() To set a reply as the current reply in the loop
2761 * @uses bbp_get_reply_id() To get the reply id
2762 * @uses wp_trash_post() To trash the reply
2763 * @uses update_post_meta() To save a list of just trashed replies for future use
2764 */
2765 function bbp_trash_topic( $topic_id = 0 ) {
2766 global $bbp;
2767
2768 $topic_id = bbp_get_topic_id( $topic_id );
2769
2770 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2771 return false;
2772
2773 do_action( 'bbp_trash_topic', $topic_id );
2774
2775 // Topic is being trashed, so its replies are trashed too
2776 if ( bbp_has_replies( array(
2777 'post_type' => bbp_get_reply_post_type(),
2778 'post_status' => bbp_get_public_status_id(),
2779 'posts_per_page' => -1,
2780 'meta_query' => array( array(
2781 'key' => '_bbp_topic_id',
2782 'value' => $topic_id,
2783 'compare' => '='
2784 ) )
2785 ) ) ) {
2786
2787 // Prevent debug notices
2788 $pre_trashed_replies = array();
2789
2790 // Loop through replies, trash them, and add them to array
2791 while ( bbp_replies() ) {
2792 bbp_the_reply();
2793 wp_trash_post( $bbp->reply_query->post->ID );
2794 $pre_trashed_replies[] = $bbp->reply_query->post->ID;
2795 }
2796
2797 // Set a post_meta entry of the replies that were trashed by this action.
2798 // This is so we can possibly untrash them, without untrashing replies
2799 // that were purposefully trashed before.
2800 update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
2801 }
2802 }
2803
2804 /**
2805 * Called before untrashing a topic
2806 *
2807 * @uses bbp_get_topic_id() To get the topic id
2808 * @uses bbp_is_topic() To check if the passed id is a topic
2809 * @uses do_action() Calls 'bbp_untrash_topic' with the topic id
2810 * @uses get_post_meta() To get the list of replies which were trashed with the
2811 * topic
2812 * @uses wp_untrash_post() To untrash the reply
2813 */
2814 function bbp_untrash_topic( $topic_id = 0 ) {
2815 $topic_id = bbp_get_topic_id( $topic_id );
2816
2817 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2818 return false;
2819
2820 do_action( 'bbp_untrash_topic', $topic_id );
2821
2822 // Get the replies that were not previously trashed
2823 $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
2824
2825 // There are replies to untrash
2826 if ( !empty( $pre_trashed_replies ) ) {
2827
2828 // Maybe reverse the trashed replies array
2829 if ( is_array( $pre_trashed_replies ) )
2830 $pre_trashed_replies = array_reverse( $pre_trashed_replies );
2831
2832 // Loop through replies
2833 foreach ( (array) $pre_trashed_replies as $reply ) {
2834 wp_untrash_post( $reply );
2835 }
2836 }
2837 }
2838
2839 /** After Delete/Trash/Untrash ************************************************/
2840
2841 /**
2842 * Called after deleting a topic
2843 *
2844 * @uses bbp_get_topic_id() To get the topic id
2845 * @uses bbp_is_topic() To check if the passed id is a topic
2846 * @uses do_action() Calls 'bbp_deleted_topic' with the topic id
2847 */
2848 function bbp_deleted_topic( $topic_id = 0 ) {
2849 $topic_id = bbp_get_topic_id( $topic_id );
2850
2851 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2852 return false;
2853
2854 do_action( 'bbp_deleted_topic', $topic_id );
2855 }
2856
2857 /**
2858 * Called after trashing a topic
2859 *
2860 * @uses bbp_get_topic_id() To get the topic id
2861 * @uses bbp_is_topic() To check if the passed id is a topic
2862 * @uses do_action() Calls 'bbp_trashed_topic' with the topic id
2863 */
2864 function bbp_trashed_topic( $topic_id = 0 ) {
2865 $topic_id = bbp_get_topic_id( $topic_id );
2866
2867 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2868 return false;
2869
2870 do_action( 'bbp_trashed_topic', $topic_id );
2871 }
2872
2873 /**
2874 * Called after untrashing a topic
2875 *
2876 * @uses bbp_get_topic_id() To get the topic id
2877 * @uses bbp_is_topic() To check if the passed id is a topic
2878 * @uses do_action() Calls 'bbp_untrashed_topic' with the topic id
2879 */
2880 function bbp_untrashed_topic( $topic_id = 0 ) {
2881 $topic_id = bbp_get_topic_id( $topic_id );
2882
2883 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2884 return false;
2885
2886 do_action( 'bbp_untrashed_topic', $topic_id );
2887 }
2888
2889 /** Settings ******************************************************************/
2890
2891 /**
2892 * Return the topics per page setting
2893 *
2894 * @since bbPress (r3540)
2895 *
2896 * @uses get_option() To get the setting
2897 * @uses apply_filters() To allow the return value to be manipulated
2898 * @return int
2899 */
2900 function bbp_get_topics_per_page() {
2901
2902 // The default per setting
2903 $default = 15;
2904
2905 // Get database option and cast as integer
2906 $per = $retval = (int) get_option( '_bbp_topics_per_page', $default );
2907
2908 // If return val is empty, set it to default
2909 if ( empty( $retval ) )
2910 $retval = $default;
2911
2912 // Filter and return
2913 return (int) apply_filters( 'bbp_get_topics_per_page', $retval, $per );
2914 }
2915
2916 /**
2917 * Return the topics per RSS page setting
2918 *
2919 * @since bbPress (r3540)
2920 *
2921 * @uses get_option() To get the setting
2922 * @uses apply_filters() To allow the return value to be manipulated
2923 * @return int
2924 */
2925 function bbp_get_topics_per_rss_page() {
2926
2927 // The default per setting
2928 $default = 25;
2929
2930 // Get database option and cast as integer
2931 $per = $retval = (int) get_option( '_bbp_topics_per_rss_page', $default );
2932
2933 // If return val is empty, set it to default
2934 if ( empty( $retval ) )
2935 $retval = $default;
2936
2937 // Filter and return
2938 return (int) apply_filters( 'bbp_get_topics_per_rss_page', $retval, $per );
2939 }
2940
2941 /** Feeds *********************************************************************/
2942
2943 /**
2944 * Output an RSS2 feed of topics, based on the query passed.
2945 *
2946 * @since bbPress (r3171)
2947 *
2948 * @uses bbp_version()
2949 * @uses bbp_is_single_topic()
2950 * @uses bbp_user_can_view_forum()
2951 * @uses bbp_get_topic_forum_id()
2952 * @uses bbp_show_load_topic()
2953 * @uses bbp_topic_permalink()
2954 * @uses bbp_topic_title()
2955 * @uses bbp_get_topic_reply_count()
2956 * @uses bbp_topic_content()
2957 * @uses bbp_has_topics()
2958 * @uses bbp_topics()
2959 * @uses bbp_the_topic()
2960 * @uses get_wp_title_rss()
2961 * @uses get_option()
2962 * @uses bloginfo_rss
2963 * @uses self_link()
2964 * @uses the_author()
2965 * @uses get_post_time()
2966 * @uses rss_enclosure()
2967 * @uses do_action()
2968 * @uses apply_filters()
2969 *
2970 * @param array $topics_query
2971 */
2972 function bbp_display_topics_feed_rss2( $topics_query = array() ) {
2973
2974 // User cannot access this forum
2975 if ( bbp_is_single_forum() && !bbp_user_can_view_forum( array( 'forum_id' => bbp_get_forum_id() ) ) )
2976 return;
2977
2978 // Display the feed
2979 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
2980 header( 'Status: 200 OK' );
2981 echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>
2982
2983 <rss version="2.0"
2984 xmlns:content="http://purl.org/rss/1.0/modules/content/"
2985 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
2986 xmlns:dc="http://purl.org/dc/elements/1.1/"
2987 xmlns:atom="http://www.w3.org/2005/Atom"
2988
2989 <?php do_action( 'bbp_feed' ); ?>
2990 >
2991
2992 <channel>
2993
2994 <title><?php bloginfo_rss( 'name' ); ?> &#187; <?php _e( 'All Topics', 'bbpress' ); ?></title>
2995 <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
2996 <link><?php self_link(); ?></link>
2997 <description><?php //?></description>
2998 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s O', '', false ); ?></pubDate>
2999 <generator>http://bbpress.org/?v=<?php bbp_version(); ?></generator>
3000 <language><?php echo get_option( 'rss_language' ); ?></language>
3001
3002 <?php do_action( 'bbp_feed_head' ); ?>
3003
3004 <?php if ( bbp_has_topics( $topics_query ) ) : ?>
3005
3006 <?php while ( bbp_topics() ) : bbp_the_topic(); ?>
3007
3008 <item>
3009 <guid><?php bbp_topic_permalink(); ?></guid>
3010 <title><![CDATA[<?php bbp_topic_title(); ?>]]></title>
3011 <link><?php bbp_topic_permalink(); ?></link>
3012 <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>
3013 <dc:creator><?php the_author() ?></dc:creator>
3014
3015 <?php if ( !post_password_required() ) : ?>
3016
3017 <description>
3018 <![CDATA[
3019 <p><?php printf( __( 'Replies: %s', 'bbpress' ), bbp_get_topic_reply_count() ); ?></p>
3020 <?php bbp_topic_content(); ?>
3021 ]]>
3022 </description>
3023
3024 <?php rss_enclosure(); ?>
3025
3026 <?php endif; ?>
3027
3028 <?php do_action( 'bbp_feed_item' ); ?>
3029
3030 </item>
3031
3032 <?php endwhile; ?>
3033 <?php endif; ?>
3034
3035 <?php do_action( 'bbp_feed_footer' ); ?>
3036
3037 </channel>
3038 </rss>
3039
3040 <?php
3041 exit();
3042 }
3043
3044 /** Permissions ***************************************************************/
3045
3046 /**
3047 * Redirect if unathorized user is attempting to edit a topic
3048 *
3049 * @since bbPress (r3605)
3050 *
3051 * @uses bbp_is_topic_edit()
3052 * @uses current_user_can()
3053 * @uses bbp_get_topic_id()
3054 * @uses wp_safe_redirect()
3055 * @uses bbp_get_topic_permalink()
3056 */
3057 function bbp_check_topic_edit() {
3058
3059 // Bail if not editing a topic
3060 if ( !bbp_is_topic_edit() )
3061 return;
3062
3063 // User cannot edit topic, so redirect back to topic
3064 if ( !current_user_can( 'edit_topic', bbp_get_topic_id() ) ) {
3065 wp_safe_redirect( bbp_get_topic_permalink() );
3066 exit();
3067 }
3068 }
3069
3070 /**
3071 * Redirect if unathorized user is attempting to edit a topic tag
3072 *
3073 * @since bbPress (r3605)
3074 *
3075 * @uses bbp_is_topic_tag_edit()
3076 * @uses current_user_can()
3077 * @uses bbp_get_topic_tag_id()
3078 * @uses wp_safe_redirect()
3079 * @uses bbp_get_topic_tag_link()
3080 */
3081 function bbp_check_topic_tag_edit() {
3082
3083 // Bail if not editing a topic tag
3084 if ( !bbp_is_topic_tag_edit() )
3085 return;
3086
3087 // Bail if current user cannot edit topic tags
3088 if ( !current_user_can( 'edit_topic_tags', bbp_get_topic_tag_id() ) ) {
3089 wp_safe_redirect( bbp_get_topic_tag_link() );
3090 exit();
3091 }
3092 }
3093
3094 ?>
3095