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

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

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