PluginProbe
bbPress / 2.0-beta-2b
bbPress v2.0-beta-2b
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-2b, at bbp-includes/bbp-topic-functions.php

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