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

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