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

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