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

bbp-topic-functions.php in bbPress 2.0-beta-3, at bbp-includes/bbp-topic-functions.php

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