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

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