PluginProbe
bbPress / 2.1.2
bbPress v2.1.2
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 2.2.2 All 71 releases
bbpress / bbp-includes / bbp-topic-functions.php

bbp-topic-functions.php in bbPress 2.1.2, at bbp-includes/bbp-topic-functions.php

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