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

3,214 lines 111.4 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 'guid' => ''
1421 );
1422
1423 // Update the topic
1424 $destination_topic_id = wp_update_post( $postarr );
1425 $destination_topic = bbp_get_topic( $destination_topic_id );
1426
1427 // Make sure the new topic knows its a topic
1428 bbp_update_topic_topic_id( $from_reply->ID );
1429
1430 // Shouldn't happen
1431 if ( false == $destination_topic_id || is_wp_error( $destination_topic_id ) || empty( $destination_topic ) ) {
1432 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' ) );
1433 }
1434
1435 // User cannot publish posts
1436 } else {
1437 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' ) );
1438 }
1439
1440 break;
1441 }
1442 }
1443
1444 // Bail ir there are errors
1445 if ( bbp_has_errors() )
1446 return;
1447
1448 /** No Errors - Do the Spit ***********************************************/
1449
1450 // Update counts, etc...
1451 do_action( 'bbp_pre_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
1452
1453 /** Subscriptions *********************************************************/
1454
1455 // Copy the subscribers
1456 if ( !empty( $_POST['bbp_topic_subscribers'] ) && 1 == $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active() ) {
1457
1458 // Get the subscribers
1459 $subscribers = bbp_get_topic_subscribers( $source_topic->ID );
1460
1461 if ( !empty( $subscribers ) ) {
1462
1463 // Add subscribers to new topic
1464 foreach ( (array) $subscribers as $subscriber ) {
1465 bbp_add_user_subscription( $subscriber, $destination_topic->ID );
1466 }
1467 }
1468 }
1469
1470 /** Favorites *************************************************************/
1471
1472 // Copy the favoriters if told to
1473 if ( !empty( $_POST['bbp_topic_favoriters'] ) && 1 == $_POST['bbp_topic_favoriters'] ) {
1474
1475 // Get the favoriters
1476 $favoriters = bbp_get_topic_favoriters( $source_topic->ID );
1477
1478 if ( !empty( $favoriters ) ) {
1479
1480 // Add the favoriters to new topic
1481 foreach ( (array) $favoriters as $favoriter ) {
1482 bbp_add_user_favorite( $favoriter, $destination_topic->ID );
1483 }
1484 }
1485 }
1486
1487 /** Tags ******************************************************************/
1488
1489 // Copy the tags if told to
1490 if ( !empty( $_POST['bbp_topic_tags'] ) && ( 1 == $_POST['bbp_topic_tags'] ) ) {
1491
1492 // Get the source topic tags
1493 $source_topic_tags = wp_get_post_terms( $source_topic->ID, bbp_get_topic_tag_tax_id(), array( 'fields' => 'names' ) );
1494
1495 if ( !empty( $source_topic_tags ) ) {
1496 wp_set_post_terms( $destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true );
1497 }
1498 }
1499
1500 /** Split Replies *********************************************************/
1501
1502 // get_posts() is not used because it doesn't allow us to use '>='
1503 // comparision without a filter.
1504 $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() ) );
1505
1506 // Make sure there are replies to loop through
1507 if ( !empty( $replies ) && !is_wp_error( $replies ) ) {
1508
1509 // Change the post_parent of each reply to the destination topic id
1510 foreach ( $replies as $reply ) {
1511
1512 // New reply data
1513 $postarr = array(
1514 'ID' => $reply->ID,
1515 'post_title' => sprintf( __( 'Reply To: %s', 'bbpress' ), $destination_topic->post_title ),
1516 'post_name' => false, // will be automatically generated
1517 'post_parent' => $destination_topic->ID,
1518 'guid' => ''
1519 );
1520
1521 // Update the reply
1522 wp_update_post( $postarr );
1523
1524 // Adjust reply meta values
1525 bbp_update_reply_topic_id( $reply->ID, $destination_topic->ID );
1526 bbp_update_reply_forum_id( $reply->ID, bbp_get_topic_forum_id( $destination_topic->ID ) );
1527
1528 // Do additional actions per split reply
1529 do_action( 'bbp_split_topic_reply', $reply->ID, $destination_topic->ID );
1530 }
1531 }
1532
1533 // It is a new topic and we need to set some default metas to make
1534 // the topic display in bbp_has_topics() list
1535 if ( 'reply' == $split_option ) {
1536 $last_reply_id = ( empty( $reply ) || empty( $reply->ID ) ) ? 0 : $reply->ID;
1537 $freshness = ( empty( $reply ) || empty( $reply->post_date ) ) ? '' : $reply->post_date;
1538
1539 bbp_update_topic_last_reply_id ( $destination_topic->ID, $last_reply_id );
1540 bbp_update_topic_last_active_time( $destination_topic->ID, $freshness );
1541 }
1542
1543 /** Successful Split ******************************************************/
1544
1545 // Update counts, etc...
1546 do_action( 'bbp_post_split_topic', $from_reply->ID, $source_topic->ID, $destination_topic->ID );
1547
1548 // Redirect back to the topic
1549 wp_safe_redirect( bbp_get_topic_permalink( $destination_topic->ID ) );
1550
1551 // For good measure
1552 exit();
1553 }
1554
1555 /**
1556 * Fix counts on topic split
1557 *
1558 * When a topic is split, update the counts of source and destination topic
1559 * and their forums.
1560 *
1561 * @since bbPress (r2756)
1562 *
1563 * @param int $from_reply_id From reply id
1564 * @param int $source_topic_id Source topic id
1565 * @param int $destination_topic_id Destination topic id
1566 * @uses bbp_update_forum_topic_count() To update the forum topic counts
1567 * @uses bbp_update_forum_reply_count() To update the forum reply counts
1568 * @uses bbp_update_topic_reply_count() To update the topic reply counts
1569 * @uses bbp_update_topic_voice_count() To update the topic voice counts
1570 * @uses bbp_update_topic_reply_count_hidden() To update the topic hidden reply
1571 * count
1572 * @uses do_action() Calls 'bbp_split_topic_count' with the from reply id,
1573 * source topic id & destination topic id
1574 */
1575 function bbp_split_topic_count( $from_reply_id, $source_topic_id, $destination_topic_id ) {
1576
1577 // Forum Topic Counts
1578 bbp_update_forum_topic_count( $destination_topic_id );
1579
1580 // Forum Reply Counts
1581 bbp_update_forum_reply_count( $destination_topic_id );
1582
1583 // Topic Reply Counts
1584 bbp_update_topic_reply_count( $source_topic_id );
1585 bbp_update_topic_reply_count( $destination_topic_id );
1586
1587 // Topic Hidden Reply Counts
1588 bbp_update_topic_reply_count_hidden( $source_topic_id );
1589 bbp_update_topic_reply_count_hidden( $destination_topic_id );
1590
1591 // Topic Voice Counts
1592 bbp_update_topic_voice_count( $source_topic_id );
1593 bbp_update_topic_voice_count( $destination_topic_id );
1594
1595 do_action( 'bbp_split_topic_count', $from_reply_id, $source_topic_id, $destination_topic_id );
1596 }
1597
1598 /**
1599 * Handles the front end tag management (renaming, merging, destroying)
1600 *
1601 * @since bbPress (r2768)
1602 *
1603 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
1604 * @uses current_user_can() To check if the current user can edit/delete tags
1605 * @uses bbPress::errors::add() To log the error messages
1606 * @uses wp_update_term() To update the topic tag
1607 * @uses get_term_link() To get the topic tag url
1608 * @uses term_exists() To check if the topic tag already exists
1609 * @uses wp_insert_term() To insert a topic tag
1610 * @uses wp_delete_term() To delete the topic tag
1611 * @uses home_url() To get the blog's home page url
1612 * @uses do_action() Calls actions based on the actions with associated args
1613 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
1614 * @uses wp_safe_redirect() To redirect to the url
1615 */
1616 function bbp_edit_topic_tag_handler() {
1617
1618 // Bail if not a POST action
1619 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1620 return;
1621
1622 // Bail if required POST actions aren't passed
1623 if ( empty( $_POST['tag-id'] ) || empty( $_POST['action'] ) )
1624 return;
1625
1626 // Setup possible get actions
1627 $possible_actions = array(
1628 'bbp-update-topic-tag',
1629 'bbp-merge-topic-tag',
1630 'bbp-delete-topic-tag'
1631 );
1632
1633 // Bail if actions aren't meant for this function
1634 if ( !in_array( $_POST['action'], $possible_actions ) )
1635 return;
1636
1637 // Setup vars
1638 $action = $_POST['action'];
1639 $tag_id = (int) $_POST['tag-id'];
1640 $tag = get_term( $tag_id, bbp_get_topic_tag_tax_id() );
1641
1642 // Tag does not exist
1643 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1644 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() ) );
1645 return;
1646 }
1647
1648 // What action are we trying to perform?
1649 switch ( $action ) {
1650
1651 // Update tag
1652 case 'bbp-update-topic-tag' :
1653
1654 // Nonce check
1655 if ( ! bbp_verify_nonce_request( 'update-tag_' . $tag_id ) ) {
1656 bbp_add_error( 'bbp_manage_topic_tag_update_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1657 return;
1658 }
1659
1660 // Can user edit topic tags?
1661 if ( !current_user_can( 'edit_topic_tags' ) ) {
1662 bbp_add_error( 'bbp_manage_topic_tag_update_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
1663 return;
1664 }
1665
1666 // No tag name was provided
1667 if ( empty( $_POST['tag-name'] ) || !$name = $_POST['tag-name'] ) {
1668 bbp_add_error( 'bbp_manage_topic_tag_update_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
1669 return;
1670 }
1671
1672 // Attempt to update the tag
1673 $slug = !empty( $_POST['tag-slug'] ) ? $_POST['tag-slug'] : '';
1674 $tag = wp_update_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'name' => $name, 'slug' => $slug ) );
1675
1676 // Cannot update tag
1677 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1678 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() ) );
1679 return;
1680 }
1681
1682 // Redirect
1683 $redirect = get_term_link( $tag_id, bbp_get_topic_tag_tax_id() );
1684
1685 // Update counts, etc...
1686 do_action( 'bbp_update_topic_tag', $tag_id, $tag, $name, $slug );
1687
1688 break;
1689
1690 // Merge two tags
1691 case 'bbp-merge-topic-tag' :
1692
1693 // Nonce check
1694 if ( ! bbp_verify_nonce_request( 'merge-tag_' . $tag_id ) ) {
1695 bbp_add_error( 'bbp_manage_topic_tag_merge_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1696 return;
1697 }
1698
1699 // Can user edit topic tags?
1700 if ( !current_user_can( 'edit_topic_tags' ) ) {
1701 bbp_add_error( 'bbp_manage_topic_tag_merge_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to edit the topic tags.', 'bbpress' ) );
1702 return;
1703 }
1704
1705 // No tag name was provided
1706 if ( empty( $_POST['tag-existing-name'] ) || !$name = $_POST['tag-existing-name'] ) {
1707 bbp_add_error( 'bbp_manage_topic_tag_merge_name', __( '<strong>ERROR</strong>: You need to enter a tag name.', 'bbpress' ) );
1708 return;
1709 }
1710
1711 // If term does not exist, create it
1712 if ( !$tag = term_exists( $name, bbp_get_topic_tag_tax_id() ) )
1713 $tag = wp_insert_term( $name, bbp_get_topic_tag_tax_id() );
1714
1715 // Problem inserting the new term
1716 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1717 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() ) );
1718 return;
1719 }
1720
1721 // Merging in to...
1722 $to_tag = $tag['term_id'];
1723
1724 // Attempting to merge a tag into itself
1725 if ( $tag_id == $to_tag ) {
1726 bbp_add_error( 'bbp_manage_topic_tag_merge_same', __( '<strong>ERROR</strong>: The tags which are being merged can not be the same.', 'bbpress' ) );
1727 return;
1728 }
1729
1730 // Delete the old term
1731 $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id(), array( 'default' => $to_tag, 'force_default' => true ) );
1732
1733 // Error merging the terms
1734 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1735 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() ) );
1736 return;
1737 }
1738
1739 // Redirect
1740 $redirect = get_term_link( (int) $to_tag, bbp_get_topic_tag_tax_id() );
1741
1742 // Update counts, etc...
1743 do_action( 'bbp_merge_topic_tag', $tag_id, $to_tag, $tag );
1744
1745 break;
1746
1747 // Delete tag
1748 case 'bbp-delete-topic-tag' :
1749
1750 // Nonce check
1751 if ( ! bbp_verify_nonce_request( 'delete-tag_' . $tag_id ) ) {
1752 bbp_add_error( 'bbp_manage_topic_tag_delete_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
1753 return;
1754 }
1755
1756 // Can user delete topic tags?
1757 if ( !current_user_can( 'delete_topic_tags' ) ) {
1758 bbp_add_error( 'bbp_manage_topic_tag_delete_permissions', __( '<strong>ERROR</strong>: You do not have the permissions to delete the topic tags.', 'bbpress' ) );
1759 return;
1760 }
1761
1762 // Attempt to delete term
1763 $tag = wp_delete_term( $tag_id, bbp_get_topic_tag_tax_id() );
1764
1765 // Error deleting term
1766 if ( is_wp_error( $tag ) && $tag->get_error_message() ) {
1767 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() ) );
1768 return;
1769 }
1770
1771 // We don't have any other place to go other than home! Or we may die because of the 404 disease
1772 $redirect = home_url();
1773
1774 // Update counts, etc...
1775 do_action( 'bbp_delete_topic_tag', $tag_id, $tag );
1776
1777 break;
1778 }
1779
1780 /** Successful Moderation *************************************************/
1781
1782 // Redirect back
1783 $redirect = ( !empty( $redirect ) && !is_wp_error( $redirect ) ) ? $redirect : home_url();
1784 wp_safe_redirect( $redirect );
1785
1786 // For good measure
1787 exit();
1788 }
1789
1790 /** Stickies ******************************************************************/
1791
1792 /**
1793 * Return sticky topics of a forum
1794 *
1795 * @since bbPress (r2592)
1796 *
1797 * @param int $forum_id Optional. If not passed, super stickies are returned.
1798 * @uses bbp_get_super_stickies() To get the super stickies
1799 * @uses get_post_meta() To get the forum stickies
1800 * @uses apply_filters() Calls 'bbp_get_stickies' with the stickies and forum id
1801 * @return array IDs of sticky topics of a forum or super stickies
1802 */
1803 function bbp_get_stickies( $forum_id = 0 ) {
1804 $stickies = empty( $forum_id ) ? bbp_get_super_stickies() : get_post_meta( $forum_id, '_bbp_sticky_topics', true );
1805 $stickies = ( empty( $stickies ) || !is_array( $stickies ) ) ? array() : $stickies;
1806
1807 return apply_filters( 'bbp_get_stickies', $stickies, (int) $forum_id );
1808 }
1809
1810 /**
1811 * Return topics stuck to front page of the forums
1812 *
1813 * @since bbPress (r2592)
1814 *
1815 * @uses get_option() To get super sticky topics
1816 * @uses apply_filters() Calls 'bbp_get_super_stickies' with the stickies
1817 * @return array IDs of super sticky topics
1818 */
1819 function bbp_get_super_stickies() {
1820 $stickies = get_option( '_bbp_super_sticky_topics', array() );
1821 $stickies = ( empty( $stickies ) || !is_array( $stickies ) ) ? array() : $stickies;
1822
1823 return apply_filters( 'bbp_get_super_stickies', $stickies );
1824 }
1825
1826 /** Topics Actions ************************************************************/
1827
1828 /**
1829 * Handles the front end opening/closing, spamming/unspamming,
1830 * sticking/unsticking and trashing/untrashing/deleting of topics
1831 *
1832 * @since bbPress (r2727)
1833 *
1834 * @uses bbp_get_topic() To get the topic
1835 * @uses current_user_can() To check if the user is capable of editing or
1836 * deleting the topic
1837 * @uses bbp_get_topic_post_type() To get the topic post type
1838 * @uses check_ajax_referer() To verify the nonce and check the referer
1839 * @uses bbp_is_topic_open() To check if the topic is open
1840 * @uses bbp_close_topic() To close the topic
1841 * @uses bbp_open_topic() To open the topic
1842 * @uses bbp_is_topic_sticky() To check if the topic is a sticky
1843 * @uses bbp_unstick_topic() To unstick the topic
1844 * @uses bbp_stick_topic() To stick the topic
1845 * @uses bbp_is_topic_spam() To check if the topic is marked as spam
1846 * @uses bbp_spam_topic() To make the topic as spam
1847 * @uses bbp_unspam_topic() To unmark the topic as spam
1848 * @uses wp_trash_post() To trash the topic
1849 * @uses wp_untrash_post() To untrash the topic
1850 * @uses wp_delete_post() To delete the topic
1851 * @uses do_action() Calls 'bbp_toggle_topic_handler' with success, post data
1852 * and action
1853 * @uses bbp_get_forum_permalink() To get the forum link
1854 * @uses bbp_get_topic_permalink() To get the topic link
1855 * @uses add_query_arg() To add args to the url
1856 * @uses wp_safe_redirect() To redirect to the topic
1857 * @uses bbPress::errors:add() To log the error messages
1858 */
1859 function bbp_toggle_topic_handler() {
1860
1861 // Bail if not a GET action
1862 if ( 'GET' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
1863 return;
1864
1865 // Bail if required GET actions aren't passed
1866 if ( empty( $_GET['topic_id'] ) || empty( $_GET['action'] ) )
1867 return;
1868
1869 // Setup possible get actions
1870 $possible_actions = array(
1871 'bbp_toggle_topic_close',
1872 'bbp_toggle_topic_stick',
1873 'bbp_toggle_topic_spam',
1874 'bbp_toggle_topic_trash'
1875 );
1876
1877 // Bail if actions aren't meant for this function
1878 if ( !in_array( $_GET['action'], $possible_actions ) )
1879 return;
1880
1881 $failure = ''; // Empty failure string
1882 $view_all = false; // Assume not viewing all
1883 $action = $_GET['action']; // What action is taking place?
1884 $topic_id = (int) $_GET['topic_id']; // What's the topic id?
1885 $success = false; // Flag
1886 $post_data = array( 'ID' => $topic_id ); // Prelim array
1887 $redirect = ''; // Empty redirect URL
1888
1889 // Make sure topic exists
1890 $topic = bbp_get_topic( $topic_id );
1891 if ( empty( $topic ) )
1892 return;
1893
1894 // What is the user doing here?
1895 if ( !current_user_can( 'edit_topic', $topic->ID ) || ( 'bbp_toggle_topic_trash' == $action && !current_user_can( 'delete_topic', $topic->ID ) ) ) {
1896 bbp_add_error( 'bbp_toggle_topic_permission', __( '<strong>ERROR:</strong> You do not have the permission to do that.', 'bbpress' ) );
1897 return;
1898 }
1899
1900 // What action are we trying to perform?
1901 switch ( $action ) {
1902
1903 // Toggle open/close
1904 case 'bbp_toggle_topic_close' :
1905 check_ajax_referer( 'close-topic_' . $topic_id );
1906
1907 $is_open = bbp_is_topic_open( $topic_id );
1908 $success = $is_open ? bbp_close_topic( $topic_id ) : bbp_open_topic( $topic_id );
1909 $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' );
1910
1911 break;
1912
1913 // Toggle sticky/super-sticky/unstick
1914 case 'bbp_toggle_topic_stick' :
1915 check_ajax_referer( 'stick-topic_' . $topic_id );
1916
1917 $is_sticky = bbp_is_topic_sticky( $topic_id );
1918 $is_super = ( empty( $is_sticky ) && !empty( $_GET['super'] ) && 1 == (int) $_GET['super'] ) ? true : false;
1919 $success = $is_sticky ? bbp_unstick_topic( $topic_id ) : bbp_stick_topic( $topic_id, $is_super );
1920 $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' );
1921
1922 break;
1923
1924 // Toggle spam
1925 case 'bbp_toggle_topic_spam' :
1926 check_ajax_referer( 'spam-topic_' . $topic_id );
1927
1928 $is_spam = bbp_is_topic_spam( $topic_id );
1929 $success = $is_spam ? bbp_unspam_topic( $topic_id ) : bbp_spam_topic( $topic_id );
1930 $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' );
1931 $view_all = !$is_spam;
1932
1933 break;
1934
1935 // Toggle trash
1936 case 'bbp_toggle_topic_trash' :
1937
1938 $sub_action = in_array( $_GET['sub_action'], array( 'trash', 'untrash', 'delete' ) ) ? $_GET['sub_action'] : false;
1939
1940 if ( empty( $sub_action ) )
1941 break;
1942
1943 switch ( $sub_action ) {
1944 case 'trash':
1945 check_ajax_referer( 'trash-' . bbp_get_topic_post_type() . '_' . $topic_id );
1946
1947 $view_all = true;
1948 $success = wp_trash_post( $topic_id );
1949 $failure = __( '<strong>ERROR</strong>: There was a problem trashing the topic.', 'bbpress' );
1950
1951 break;
1952
1953 case 'untrash':
1954 check_ajax_referer( 'untrash-' . bbp_get_topic_post_type() . '_' . $topic_id );
1955
1956 $success = wp_untrash_post( $topic_id );
1957 $failure = __( '<strong>ERROR</strong>: There was a problem untrashing the topic.', 'bbpress' );
1958
1959 break;
1960
1961 case 'delete':
1962 check_ajax_referer( 'delete-' . bbp_get_topic_post_type() . '_' . $topic_id );
1963
1964 $success = wp_delete_post( $topic_id );
1965 $failure = __( '<strong>ERROR</strong>: There was a problem deleting the topic.', 'bbpress' );
1966
1967 break;
1968 }
1969
1970 break;
1971 }
1972
1973 // Do additional topic toggle actions
1974 do_action( 'bbp_toggle_topic_handler', $success, $post_data, $action );
1975
1976 // No errors
1977 if ( false != $success && !is_wp_error( $success ) ) {
1978
1979 // Redirect back to the topic's forum
1980 if ( isset( $sub_action ) && ( 'delete' == $sub_action ) ) {
1981 $redirect = bbp_get_forum_permalink( $success->post_parent );
1982
1983 // Redirect back to the topic
1984 } else {
1985
1986 // Get the redirect detination
1987 $permalink = bbp_get_topic_permalink( $topic_id );
1988 $redirect = bbp_add_view_all( $permalink, $view_all );
1989 }
1990
1991 wp_safe_redirect( $redirect );
1992
1993 // For good measure
1994 exit();
1995
1996 // Handle errors
1997 } else {
1998 bbp_add_error( 'bbp_toggle_topic', $failure );
1999 }
2000 }
2001
2002 /** Favorites & Subscriptions *************************************************/
2003
2004 /**
2005 * Remove a deleted topic from all users' favorites
2006 *
2007 * @since bbPress (r2652)
2008 *
2009 * @param int $topic_id Topic ID to remove
2010 * @uses bbp_get_topic_favoriters() To get the topic's favoriters
2011 * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
2012 */
2013 function bbp_remove_topic_from_all_favorites( $topic_id = 0 ) {
2014 $topic_id = bbp_get_topic_id( $topic_id );
2015
2016 // Bail if no topic
2017 if ( empty( $topic_id ) )
2018 return;
2019
2020 // Get users
2021 $users = (array) bbp_get_topic_favoriters( $topic_id );
2022
2023 // Users exist
2024 if ( !empty( $users ) ) {
2025
2026 // Loop through users
2027 foreach ( $users as $user ) {
2028
2029 // Remove each user
2030 bbp_remove_user_favorite( $user, $topic_id );
2031 }
2032 }
2033 }
2034
2035 /**
2036 * Remove a deleted topic from all users' subscriptions
2037 *
2038 * @since bbPress (r2652)
2039 *
2040 * @param int $topic_id Topic ID to remove
2041 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
2042 * @uses bbp_get_topic_subscribers() To get the topic subscribers
2043 * @uses bbp_remove_user_subscription() To remove the user subscription
2044 */
2045 function bbp_remove_topic_from_all_subscriptions( $topic_id = 0 ) {
2046
2047 // Subscriptions are not active
2048 if ( !bbp_is_subscriptions_active() )
2049 return;
2050
2051 $topic_id = bbp_get_topic_id( $topic_id );
2052
2053 // Bail if no topic
2054 if ( empty( $topic_id ) )
2055 return;
2056
2057 // Get users
2058 $users = (array) bbp_get_topic_subscribers( $topic_id );
2059
2060 // Users exist
2061 if ( !empty( $users ) ) {
2062
2063 // Loop through users
2064 foreach ( $users as $user ) {
2065
2066 // Remove each user
2067 bbp_remove_user_subscription( $user, $topic_id );
2068 }
2069 }
2070 }
2071
2072 /** Count Bumpers *************************************************************/
2073
2074 /**
2075 * Bump the total reply count of a topic
2076 *
2077 * @since bbPress (r3825)
2078 *
2079 * @param int $topic_id Optional. Forum id.
2080 * @param int $difference Optional. Default 1
2081 * @param bool $update_ancestors Optional. Default true
2082 * @uses bbp_get_topic_id() To get the topic id
2083 * @uses update_post_meta() To update the topic's reply count meta
2084 * @uses apply_filters() Calls 'bbp_bump_topic_reply_count' with the reply
2085 * count, topic id, and difference
2086 * @return int Forum reply count
2087 */
2088 function bbp_bump_topic_reply_count( $topic_id = 0, $difference = 1 ) {
2089
2090 // Get counts
2091 $topic_id = bbp_get_topic_id( $topic_id );
2092 $reply_count = bbp_get_topic_reply_count( $topic_id, false );
2093 $new_count = (int) $reply_count + (int) $difference;
2094
2095 // Update this topic id's reply count
2096 update_post_meta( $topic_id, '_bbp_reply_count', (int) $new_count );
2097
2098 return (int) apply_filters( 'bbp_bump_topic_reply_count', (int) $new_count, $topic_id, (int) $difference );
2099 }
2100
2101 /**
2102 * Bump the total hidden reply count of a topic
2103 *
2104 * @since bbPress (r3825)
2105 *
2106 * @param int $topic_id Optional. Forum id.
2107 * @param int $difference Optional. Default 1
2108 * @uses bbp_get_topic_id() To get the topic id
2109 * @uses update_post_meta() To update the topic's reply count meta
2110 * @uses apply_filters() Calls 'bbp_bump_topic_reply_count_hidden' with the
2111 * reply count, topic id, and difference
2112 * @return int Forum hidden reply count
2113 */
2114 function bbp_bump_topic_reply_count_hidden( $topic_id = 0, $difference = 1 ) {
2115
2116 // Get counts
2117 $topic_id = bbp_get_topic_id( $topic_id );
2118 $reply_count = bbp_get_topic_reply_count_hidden( $topic_id, false );
2119 $new_count = (int) $reply_count + (int) $difference;
2120
2121 // Update this topic id's hidder reply count
2122 update_post_meta( $topic_id, '_bbp_reply_count_hidden', (int) $new_count );
2123
2124 return (int) apply_filters( 'bbp_bump_topic_reply_count_hidden', (int) $new_count, $topic_id, (int) $difference );
2125 }
2126
2127 /** Topic Updaters ************************************************************/
2128
2129 /**
2130 * Update the topic's forum id
2131 *
2132 * @since bbPress (r2855)
2133 *
2134 * @param int $topic_id Optional. Topic id to update
2135 * @param int $forum_id Optional. Forum id
2136 * @uses bbp_is_reply() TO check if the passed topic id is a reply
2137 * @uses bbp_get_reply_topic_id() To get the reply topic id
2138 * @uses bbp_get_topic_id() To get the topic id
2139 * @uses get_post_field() To get the post parent of the topic id
2140 * @uses bbp_get_forum_id() To get the forum id
2141 * @uses update_post_meta() To update the topic forum id meta
2142 * @uses apply_filters() Calls 'bbp_update_topic_forum_id' with the forum id
2143 * and topic id
2144 * @return int Forum id
2145 */
2146 function bbp_update_topic_forum_id( $topic_id = 0, $forum_id = 0 ) {
2147
2148 // If it's a reply, then get the parent (topic id)
2149 if ( bbp_is_reply( $topic_id ) )
2150 $topic_id = bbp_get_reply_topic_id( $topic_id );
2151 else
2152 $topic_id = bbp_get_topic_id( $topic_id );
2153
2154 if ( empty( $forum_id ) )
2155 $forum_id = get_post_field( 'post_parent', $topic_id );
2156
2157 update_post_meta( $topic_id, '_bbp_forum_id', (int) $forum_id );
2158
2159 return apply_filters( 'bbp_update_topic_forum_id', (int) $forum_id, $topic_id );
2160 }
2161
2162 /**
2163 * Update the topic's topic id
2164 *
2165 * @since bbPress (r2954)
2166 *
2167 * @param int $topic_id Optional. Topic id to update
2168 * @uses bbp_get_topic_id() To get the topic id
2169 * @uses update_post_meta() To update the topic's topic id meta
2170 * @uses apply_filters() Calls 'bbp_update_topic_topic_id' with the topic id
2171 * @return int Topic id
2172 */
2173 function bbp_update_topic_topic_id( $topic_id = 0 ) {
2174 $topic_id = bbp_get_topic_id( $topic_id );
2175
2176 update_post_meta( $topic_id, '_bbp_topic_id', (int) $topic_id );
2177
2178 return apply_filters( 'bbp_update_topic_topic_id', (int) $topic_id );
2179 }
2180
2181 /**
2182 * Adjust the total reply count of a topic
2183 *
2184 * @since bbPress (r2467)
2185 *
2186 * @param int $topic_id Optional. Topic id to update
2187 * @param int $reply_count Optional. Set the reply count manually.
2188 * @uses bbp_is_reply() To check if the passed topic id is a reply
2189 * @uses bbp_get_reply_topic_id() To get the reply topic id
2190 * @uses bbp_get_topic_id() To get the topic id
2191 * @uses bbp_get_reply_post_type() To get the reply post type
2192 * @uses bbp_get_public_child_count() To get the reply count
2193 * @uses update_post_meta() To update the topic reply count meta
2194 * @uses apply_filters() Calls 'bbp_update_topic_reply_count' with the reply
2195 * count and topic id
2196 * @return int Topic reply count
2197 */
2198 function bbp_update_topic_reply_count( $topic_id = 0, $reply_count = 0 ) {
2199
2200 // If it's a reply, then get the parent (topic id)
2201 if ( bbp_is_reply( $topic_id ) )
2202 $topic_id = bbp_get_reply_topic_id( $topic_id );
2203 else
2204 $topic_id = bbp_get_topic_id( $topic_id );
2205
2206 // Get replies of topic if not passed
2207 if ( empty( $reply_count ) )
2208 $reply_count = bbp_get_public_child_count( $topic_id, bbp_get_reply_post_type() );
2209
2210 update_post_meta( $topic_id, '_bbp_reply_count', (int) $reply_count );
2211
2212 return apply_filters( 'bbp_update_topic_reply_count', (int) $reply_count, $topic_id );
2213 }
2214
2215 /**
2216 * Adjust the total hidden reply count of a topic (hidden includes trashed and spammed replies)
2217 *
2218 * @since bbPress (r2740)
2219 *
2220 * @param int $topic_id Optional. Topic id to update
2221 * @param int $reply_count Optional. Set the reply count manually
2222 * @uses bbp_is_reply() To check if the passed topic id is a reply
2223 * @uses bbp_get_reply_topic_id() To get the reply topic id
2224 * @uses bbp_get_topic_id() To get the topic id
2225 * @uses bbp_get_reply_post_type() To get the reply post type
2226 * @uses wpdb::prepare() To prepare our sql query
2227 * @uses wpdb::get_var() To execute our query and get the var back
2228 * @uses update_post_meta() To update the topic hidden reply count meta
2229 * @uses apply_filters() Calls 'bbp_update_topic_reply_count_hidden' with the
2230 * hidden reply count and topic id
2231 * @return int Topic hidden reply count
2232 */
2233 function bbp_update_topic_reply_count_hidden( $topic_id = 0, $reply_count = 0 ) {
2234 global $wpdb;
2235
2236 // If it's a reply, then get the parent (topic id)
2237 if ( bbp_is_reply( $topic_id ) )
2238 $topic_id = bbp_get_reply_topic_id( $topic_id );
2239 else
2240 $topic_id = bbp_get_topic_id( $topic_id );
2241
2242 // Get replies of topic
2243 if ( empty( $reply_count ) )
2244 $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() ) );
2245
2246 update_post_meta( $topic_id, '_bbp_reply_count_hidden', (int) $reply_count );
2247
2248 return apply_filters( 'bbp_update_topic_reply_count_hidden', (int) $reply_count, $topic_id );
2249 }
2250
2251 /**
2252 * Update the topic with the last active post ID
2253 *
2254 * @since bbPress (r2888)
2255 *
2256 * @param int $topic_id Optional. Topic id to update
2257 * @param int $active_id Optional. active id
2258 * @uses bbp_is_reply() To check if the passed topic id is a reply
2259 * @uses bbp_get_reply_topic_id() To get the reply topic id
2260 * @uses bbp_get_topic_id() To get the topic id
2261 * @uses bbp_get_reply_post_type() To get the reply post type
2262 * @uses bbp_get_public_child_last_id() To get the last public reply id
2263 * @uses bbp_get_active_id() To get the active id
2264 * @uses update_post_meta() To update the topic last active id meta
2265 * @uses apply_filters() Calls 'bbp_update_topic_last_active_id' with the active
2266 * id and topic id
2267 * @return int Active id
2268 */
2269 function bbp_update_topic_last_active_id( $topic_id = 0, $active_id = 0 ) {
2270
2271 // If it's a reply, then get the parent (topic id)
2272 if ( bbp_is_reply( $topic_id ) )
2273 $topic_id = bbp_get_reply_topic_id( $topic_id );
2274 else
2275 $topic_id = bbp_get_topic_id( $topic_id );
2276
2277 if ( empty( $active_id ) )
2278 $active_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
2279
2280 // Adjust last_id's based on last_reply post_type
2281 if ( empty( $active_id ) || !bbp_is_reply( $active_id ) )
2282 $active_id = $topic_id;
2283
2284 // Update only if published
2285 if ( bbp_get_public_status_id() == get_post_status( $active_id ) )
2286 update_post_meta( $topic_id, '_bbp_last_active_id', (int) $active_id );
2287
2288 return apply_filters( 'bbp_update_topic_last_active_id', (int) $active_id, $topic_id );
2289 }
2290
2291 /**
2292 * Update the topics last active date/time (aka freshness)
2293 *
2294 * @since bbPress (r2680)
2295 *
2296 * @param int $topic_id Optional. Topic id
2297 * @param string $new_time Optional. New time in mysql format
2298 * @uses bbp_get_topic_id() To get the topic id
2299 * @uses bbp_get_reply_topic_id() To get the reply topic id
2300 * @uses current_time() To get the current time
2301 * @uses update_post_meta() To update the topic last active meta
2302 * @return bool True on success, false on failure
2303 */
2304 function bbp_update_topic_last_active_time( $topic_id = 0, $new_time = '' ) {
2305
2306 // If it's a reply, then get the parent (topic id)
2307 if ( bbp_is_reply( $topic_id ) )
2308 $topic_id = bbp_get_reply_topic_id( $topic_id );
2309 else
2310 $topic_id = bbp_get_topic_id( $topic_id );
2311
2312 // Check time and use current if empty
2313 if ( empty( $new_time ) )
2314 $new_time = get_post_field( 'post_date', bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() ) );
2315
2316 // Update only if published
2317 if ( !empty( $new_time ) )
2318 update_post_meta( $topic_id, '_bbp_last_active_time', $new_time );
2319
2320 return apply_filters( 'bbp_update_topic_last_active_time', $new_time, $topic_id );
2321 }
2322
2323 /**
2324 * Update the topic with the most recent reply ID
2325 *
2326 * @since bbPress (r2625)
2327 *
2328 * @param int $topic_id Optional. Topic id to update
2329 * @param int $reply_id Optional. Reply id
2330 * @uses bbp_is_reply() To check if the passed topic id is a reply
2331 * @uses bbp_get_reply_id() To get the reply id
2332 * @uses bbp_get_reply_topic_id() To get the reply topic id
2333 * @uses bbp_get_topic_id() To get the topic id
2334 * @uses bbp_get_reply_post_type() To get the reply post type
2335 * @uses bbp_get_public_child_last_id() To get the last public reply id
2336 * @uses update_post_meta() To update the topic last reply id meta
2337 * @uses apply_filters() Calls 'bbp_update_topic_last_reply_id' with the reply
2338 * id and topic id
2339 * @return int Reply id
2340 */
2341 function bbp_update_topic_last_reply_id( $topic_id = 0, $reply_id = 0 ) {
2342
2343 // If it's a reply, then get the parent (topic id)
2344 if ( empty( $reply_id ) && bbp_is_reply( $topic_id ) ) {
2345 $reply_id = bbp_get_reply_id( $topic_id );
2346 $topic_id = bbp_get_reply_topic_id( $reply_id );
2347 } else {
2348 $reply_id = bbp_get_reply_id( $reply_id );
2349 $topic_id = bbp_get_topic_id( $topic_id );
2350 }
2351
2352 if ( empty( $reply_id ) )
2353 $reply_id = bbp_get_public_child_last_id( $topic_id, bbp_get_reply_post_type() );
2354
2355 // Adjust last_id's based on last_reply post_type
2356 if ( empty( $reply_id ) || !bbp_is_reply( $reply_id ) )
2357 $reply_id = 0;
2358
2359 // Update if reply is published
2360 if ( bbp_is_reply_published( $reply_id ) )
2361 update_post_meta( $topic_id, '_bbp_last_reply_id', (int) $reply_id );
2362
2363 return apply_filters( 'bbp_update_topic_last_reply_id', (int) $reply_id, $topic_id );
2364 }
2365
2366 /**
2367 * Adjust the total voice count of a topic
2368 *
2369 * @since bbPress (r2567)
2370 *
2371 * @param int $topic_id Optional. Topic id to update
2372 * @uses bbp_is_reply() To check if the passed topic id is a reply
2373 * @uses bbp_get_reply_topic_id() To get the reply topic id
2374 * @uses bbp_get_topic_id() To get the topic id
2375 * @uses bbp_get_reply_topic_id() To get the reply topic id
2376 * @uses bbp_get_reply_post_type() To get the reply post type
2377 * @uses bbp_get_topic_post_type() To get the topic post type
2378 * @uses wpdb::prepare() To prepare our sql query
2379 * @uses wpdb::get_col() To execute our query and get the column back
2380 * @uses update_post_meta() To update the topic voice count meta
2381 * @uses apply_filters() Calls 'bbp_update_topic_voice_count' with the voice
2382 * count and topic id
2383 * @return int Voice count
2384 */
2385 function bbp_update_topic_voice_count( $topic_id = 0 ) {
2386 global $wpdb;
2387
2388 // If it's a reply, then get the parent (topic id)
2389 if ( bbp_is_reply( $topic_id ) )
2390 $topic_id = bbp_get_reply_topic_id( $topic_id );
2391 elseif ( bbp_is_topic( $topic_id ) )
2392 $topic_id = bbp_get_topic_id( $topic_id );
2393 else
2394 return;
2395
2396 // Query the DB to get voices in this topic
2397 $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() ) );
2398
2399 // If there's an error, make sure we have at least have 1 voice
2400 $voices = ( empty( $voices ) || is_wp_error( $voices ) ) ? 1 : $voices[0];
2401
2402 // Update the voice count for this topic id
2403 update_post_meta( $topic_id, '_bbp_voice_count', (int) $voices );
2404
2405 return apply_filters( 'bbp_update_topic_voice_count', (int) $voices, $topic_id );
2406 }
2407
2408 /**
2409 * Adjust the total anonymous reply count of a topic
2410 *
2411 * @since bbPress (r2567)
2412 *
2413 * @param int $topic_id Optional. Topic id to update
2414 * @uses bbp_is_reply() To check if the passed topic id is a reply
2415 * @uses bbp_get_reply_topic_id() To get the reply topic id
2416 * @uses bbp_get_topic_id() To get the topic id
2417 * @uses bbp_get_reply_topic_id() To get the reply topic id
2418 * @uses bbp_get_reply_post_type() To get the reply post type
2419 * @uses bbp_get_topic_post_type() To get the topic post type
2420 * @uses wpdb::prepare() To prepare our sql query
2421 * @uses wpdb::get_col() To execute our query and get the column back
2422 * @uses update_post_meta() To update the topic anonymous reply count meta
2423 * @uses apply_filters() Calls 'bbp_update_topic_anonymous_reply_count' with the
2424 * anonymous reply count and topic id
2425 * @return int Anonymous reply count
2426 */
2427 function bbp_update_topic_anonymous_reply_count( $topic_id = 0 ) {
2428 global $wpdb;
2429
2430 // If it's a reply, then get the parent (topic id)
2431 if ( bbp_is_reply( $topic_id ) )
2432 $topic_id = bbp_get_reply_topic_id( $topic_id );
2433 elseif ( bbp_is_topic( $topic_id ) )
2434 $topic_id = bbp_get_topic_id( $topic_id );
2435 else
2436 return;
2437
2438 $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() ) );
2439
2440 update_post_meta( $topic_id, '_bbp_anonymous_reply_count', (int) $anonymous_replies );
2441
2442 return apply_filters( 'bbp_update_topic_anonymous_reply_count', (int) $anonymous_replies, $topic_id );
2443 }
2444
2445 /**
2446 * Update the revision log of the topic
2447 *
2448 * @since bbPress (r2782)
2449 *
2450 * @param mixed $args Supports these args:
2451 * - topic_id: Topic id
2452 * - author_id: Author id
2453 * - reason: Reason for editing
2454 * - revision_id: Revision id
2455 * @uses bbp_get_topic_id() To get the topic id
2456 * @uses bbp_get_user_id() To get the user id
2457 * @uses bbp_format_revision_reason() To format the reason
2458 * @uses bbp_get_topic_raw_revision_log() To get the raw topic revision log
2459 * @uses update_post_meta() To update the topic revision log meta
2460 * @return mixed False on failure, true on success
2461 */
2462 function bbp_update_topic_revision_log( $args = '' ) {
2463 $defaults = array (
2464 'reason' => '',
2465 'topic_id' => 0,
2466 'author_id' => 0,
2467 'revision_id' => 0
2468 );
2469 $r = bbp_parse_args( $args, $defaults, 'update_topic_revision_log' );
2470 extract( $r );
2471
2472 // Populate the variables
2473 $reason = bbp_format_revision_reason( $reason );
2474 $topic_id = bbp_get_topic_id( $topic_id );
2475 $author_id = bbp_get_user_id ( $author_id, false, true );
2476 $revision_id = (int) $revision_id;
2477
2478 // Get the logs and append the new one to those
2479 $revision_log = bbp_get_topic_raw_revision_log( $topic_id );
2480 $revision_log[$revision_id] = array( 'author' => $author_id, 'reason' => $reason );
2481
2482 // Finally, update
2483 return update_post_meta( $topic_id, '_bbp_revision_log', $revision_log );
2484 }
2485
2486 /** Topic Actions *************************************************************/
2487
2488 /**
2489 * Closes a topic
2490 *
2491 * @since bbPress (r2740)
2492 *
2493 * @param int $topic_id Topic id
2494 * @uses wp_get_single_post() To get the topic
2495 * @uses do_action() Calls 'bbp_close_topic' with the topic id
2496 * @uses add_post_meta() To add the previous status to a meta
2497 * @uses wp_insert_post() To update the topic with the new status
2498 * @uses do_action() Calls 'bbp_opened_topic' with the topic id
2499 * @return mixed False or {@link WP_Error} on failure, topic id on success
2500 */
2501 function bbp_close_topic( $topic_id = 0 ) {
2502
2503 // Get topic
2504 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2505 return $topic;
2506
2507 // Bail if already closed
2508 if ( bbp_get_closed_status_id() == $topic['post_status'] )
2509 return false;
2510
2511 // Execute pre close code
2512 do_action( 'bbp_close_topic', $topic_id );
2513
2514 // Add pre close status
2515 add_post_meta( $topic_id, '_bbp_status', $topic['post_status'] );
2516
2517 // Set closed status
2518 $topic['post_status'] = bbp_get_closed_status_id();
2519
2520 // No revisions
2521 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2522
2523 // Update topic
2524 $topic_id = wp_insert_post( $topic );
2525
2526 // Execute post close code
2527 do_action( 'bbp_closed_topic', $topic_id );
2528
2529 // Return topic_id
2530 return $topic_id;
2531 }
2532
2533 /**
2534 * Opens a topic
2535 *
2536 * @since bbPress (r2740)
2537 *
2538 * @param int $topic_id Topic id
2539 * @uses wp_get_single_post() To get the topic
2540 * @uses do_action() Calls 'bbp_open_topic' with the topic id
2541 * @uses get_post_meta() To get the previous status
2542 * @uses delete_post_meta() To delete the previous status meta
2543 * @uses wp_insert_post() To update the topic with the new status
2544 * @uses do_action() Calls 'bbp_opened_topic' with the topic id
2545 * @return mixed False or {@link WP_Error} on failure, topic id on success
2546 */
2547 function bbp_open_topic( $topic_id = 0 ) {
2548
2549 // Get topic
2550 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2551 return $topic;
2552
2553 // Bail if already open
2554 if ( bbp_get_closed_status_id() != $topic['post_status'])
2555 return false;
2556
2557 // Execute pre open code
2558 do_action( 'bbp_open_topic', $topic_id );
2559
2560 // Get previous status
2561 $topic_status = get_post_meta( $topic_id, '_bbp_status', true );
2562
2563 // Set previous status
2564 $topic['post_status'] = $topic_status;
2565
2566 // Remove old status meta
2567 delete_post_meta( $topic_id, '_bbp_status' );
2568
2569 // No revisions
2570 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2571
2572 // Update topic
2573 $topic_id = wp_insert_post( $topic );
2574
2575 // Execute post open code
2576 do_action( 'bbp_opened_topic', $topic_id );
2577
2578 // Return topic_id
2579 return $topic_id;
2580 }
2581
2582 /**
2583 * Marks a topic as spam
2584 *
2585 * @since bbPress (r2740)
2586 *
2587 * @param int $topic_id Topic id
2588 * @uses wp_get_single_post() To get the topic
2589 * @uses do_action() Calls 'bbp_spam_topic' with the topic id
2590 * @uses add_post_meta() To add the previous status to a meta
2591 * @uses wp_insert_post() To update the topic with the new status
2592 * @uses do_action() Calls 'bbp_spammed_topic' with the topic id
2593 * @return mixed False or {@link WP_Error} on failure, topic id on success
2594 */
2595 function bbp_spam_topic( $topic_id = 0 ) {
2596
2597 // Get the topic
2598 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2599 return $topic;
2600
2601 // Bail if topic is spam
2602 if ( bbp_get_spam_status_id() == $topic['post_status'] )
2603 return false;
2604
2605 // Execute pre spam code
2606 do_action( 'bbp_spam_topic', $topic_id );
2607
2608 // Add the original post status as post meta for future restoration
2609 add_post_meta( $topic_id, '_bbp_spam_meta_status', $topic['post_status'] );
2610
2611 // Get topic tags
2612 $terms = get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() );
2613
2614 // Define local variable(s)
2615 $term_names = array();
2616
2617 // Topic has tags
2618 if ( !empty( $terms ) ) {
2619
2620 // Loop through and collect term names
2621 foreach( $terms as $term ) {
2622 $term_names[] = $term->name;
2623 }
2624
2625 // Topic terms have slugs
2626 if ( !empty( $term_names ) ) {
2627
2628 // Add the original post status as post meta for future restoration
2629 add_post_meta( $topic_id, '_bbp_spam_topic_tags', $term_names );
2630
2631 // Empty the topic of its tags
2632 $topic['tax_input'] = array( bbp_get_topic_tag_tax_id() => '' );
2633 }
2634 }
2635
2636 // Set post status to spam
2637 $topic['post_status'] = bbp_get_spam_status_id();
2638
2639 // No revisions
2640 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2641
2642 // Update the topic
2643 $topic_id = wp_insert_post( $topic );
2644
2645 // Execute post spam code
2646 do_action( 'bbp_spammed_topic', $topic_id );
2647
2648 // Return topic_id
2649 return $topic_id;
2650 }
2651
2652 /**
2653 * Unspams a topic
2654 *
2655 * @since bbPress (r2740)
2656 *
2657 * @param int $topic_id Topic id
2658 * @uses wp_get_single_post() To get the topic
2659 * @uses do_action() Calls 'bbp_unspam_topic' with the topic id
2660 * @uses get_post_meta() To get the previous status
2661 * @uses delete_post_meta() To delete the previous status meta
2662 * @uses wp_insert_post() To update the topic with the new status
2663 * @uses do_action() Calls 'bbp_unspammed_topic' with the topic id
2664 * @return mixed False or {@link WP_Error} on failure, topic id on success
2665 */
2666 function bbp_unspam_topic( $topic_id = 0 ) {
2667
2668 // Get the topic
2669 if ( !$topic = wp_get_single_post( $topic_id, ARRAY_A ) )
2670 return $topic;
2671
2672 // Bail if already not spam
2673 if ( bbp_get_spam_status_id() != $topic['post_status'] )
2674 return false;
2675
2676 // Execute pre unspam code
2677 do_action( 'bbp_unspam_topic', $topic_id );
2678
2679 // Get pre spam status
2680 $topic_status = get_post_meta( $topic_id, '_bbp_spam_meta_status', true );
2681
2682 // Set post status to pre spam
2683 $topic['post_status'] = $topic_status;
2684
2685 // Delete pre spam meta
2686 delete_post_meta( $topic_id, '_bbp_spam_meta_status' );
2687
2688 // No revisions
2689 remove_action( 'pre_post_update', 'wp_save_post_revision' );
2690
2691 // Get pre-spam topic tags
2692 $terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
2693
2694 // Topic had tags before it was spammed
2695 if ( !empty( $terms ) ) {
2696
2697 // Set the tax_input of the topic
2698 $topic['tax_input'] = array( bbp_get_topic_tag_tax_id() => $terms );
2699
2700 // Delete pre-spam topic tag meta
2701 delete_post_meta( $topic_id, '_bbp_spam_topic_tags' );
2702 }
2703
2704 // Update the topic
2705 $topic_id = wp_insert_post( $topic );
2706
2707 // Execute post unspam code
2708 do_action( 'bbp_unspammed_topic', $topic_id );
2709
2710 // Return topic_id
2711 return $topic_id;
2712 }
2713
2714 /**
2715 * Sticks a topic to a forum or front
2716 *
2717 * @since bbPress (r2754)
2718 *
2719 * @param int $topic_id Optional. Topic id
2720 * @param int $super Should we make the topic a super sticky?
2721 * @uses bbp_get_topic_id() To get the topic id
2722 * @uses bbp_unstick_topic() To unstick the topic
2723 * @uses bbp_get_topic_forum_id() To get the topic forum id
2724 * @uses bbp_get_stickies() To get the stickies
2725 * @uses do_action() 'bbp_stick_topic' with topic id and bool super
2726 * @uses update_option() To update the super stickies option
2727 * @uses update_post_meta() To update the forum stickies meta
2728 * @uses do_action() Calls 'bbp_sticked_topic' with the topic id, bool super
2729 * and success
2730 * @return bool True on success, false on failure
2731 */
2732 function bbp_stick_topic( $topic_id = 0, $super = false ) {
2733 $topic_id = bbp_get_topic_id( $topic_id );
2734
2735 // We may have a super sticky to which we want to convert into a normal sticky and vice versa
2736 // So, unstick the topic first to avoid any possible error
2737 bbp_unstick_topic( $topic_id );
2738
2739 $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
2740 $stickies = bbp_get_stickies( $forum_id );
2741
2742 do_action( 'bbp_stick_topic', $topic_id, $super );
2743
2744 if ( !is_array( $stickies ) )
2745 $stickies = array( $topic_id );
2746 else
2747 $stickies[] = $topic_id;
2748
2749 $stickies = array_unique( array_filter( $stickies ) );
2750
2751 $success = !empty( $super ) ? update_option( '_bbp_super_sticky_topics', $stickies ) : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
2752
2753 do_action( 'bbp_sticked_topic', $topic_id, $super, $success );
2754
2755 return $success;
2756 }
2757
2758 /**
2759 * Unsticks a topic both from front and it's forum
2760 *
2761 * @since bbPress (r2754)
2762 *
2763 * @param int $topic_id Optional. Topic id
2764 * @uses bbp_get_topic_id() To get the topic id
2765 * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
2766 * @uses bbp_get_topic_forum_id() To get the topic forum id
2767 * @uses bbp_get_stickies() To get the forum stickies
2768 * @uses do_action() Calls 'bbp_unstick_topic' with the topic id
2769 * @uses delete_option() To delete the super stickies option
2770 * @uses update_option() To update the super stickies option
2771 * @uses delete_post_meta() To delete the forum stickies meta
2772 * @uses update_post_meta() To update the forum stickies meta
2773 * @uses do_action() Calls 'bbp_unsticked_topic' with the topic id and success
2774 * @return bool Always true.
2775 */
2776 function bbp_unstick_topic( $topic_id = 0 ) {
2777 $topic_id = bbp_get_topic_id( $topic_id );
2778 $super = bbp_is_topic_super_sticky( $topic_id );
2779 $forum_id = empty( $super ) ? bbp_get_topic_forum_id( $topic_id ) : 0;
2780 $stickies = bbp_get_stickies( $forum_id );
2781 $offset = array_search( $topic_id, $stickies );
2782
2783 do_action( 'bbp_unstick_topic', $topic_id );
2784
2785 if ( empty( $stickies ) ) {
2786 $success = true;
2787 } elseif ( !in_array( $topic_id, $stickies ) ) {
2788 $success = true;
2789 } elseif ( false === $offset ) {
2790 $success = true;
2791 } else {
2792 array_splice( $stickies, $offset, 1 );
2793 if ( empty( $stickies ) )
2794 $success = !empty( $super ) ? delete_option( '_bbp_super_sticky_topics' ) : delete_post_meta( $forum_id, '_bbp_sticky_topics' );
2795 else
2796 $success = !empty( $super ) ? update_option( '_bbp_super_sticky_topics', $stickies ) : update_post_meta( $forum_id, '_bbp_sticky_topics', $stickies );
2797 }
2798
2799 do_action( 'bbp_unsticked_topic', $topic_id, $success );
2800
2801 return true;
2802 }
2803
2804 /** Before Delete/Trash/Untrash ***********************************************/
2805
2806 /**
2807 * Called before deleting a topic.
2808 *
2809 * This function is supplemental to the actual topic deletion which is
2810 * handled by WordPress core API functions. It is used to clean up after
2811 * a topic that is being deleted.
2812 *
2813 * @uses bbp_get_topic_id() To get the topic id
2814 * @uses bbp_is_topic() To check if the passed id is a topic
2815 * @uses do_action() Calls 'bbp_delete_topic' with the topic id
2816 * @uses bbp_has_replies() To check if the topic has replies
2817 * @uses bbp_replies() To loop through the replies
2818 * @uses bbp_the_reply() To set a reply as the current reply in the loop
2819 * @uses bbp_get_reply_id() To get the reply id
2820 * @uses wp_delete_post() To delete the reply
2821 */
2822 function bbp_delete_topic( $topic_id = 0 ) {
2823
2824 // Validate topic ID
2825 $topic_id = bbp_get_topic_id( $topic_id );
2826
2827 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2828 return false;
2829
2830 do_action( 'bbp_delete_topic', $topic_id );
2831
2832 // Topic is being permanently deleted, so its replies gotta go too
2833 // @todo remove meta query
2834 if ( bbp_has_replies( array(
2835 'post_type' => bbp_get_reply_post_type(),
2836 'post_status' => 'any',
2837 'posts_per_page' => -1,
2838 'meta_query' => array( array(
2839 'key' => '_bbp_topic_id',
2840 'value' => $topic_id,
2841 'type' => 'numeric',
2842 'compare' => '='
2843 ) )
2844 ) ) ) {
2845 while ( bbp_replies() ) {
2846 bbp_the_reply();
2847 wp_delete_post( bbp_get_reply_id(), true );
2848 }
2849 }
2850 }
2851
2852 /**
2853 * Called before trashing a topic
2854 *
2855 * This function is supplemental to the actual topic being trashed which is
2856 * handled by WordPress core API functions. It is used to clean up after
2857 * a topic that is being trashed.
2858 *
2859 * @uses bbp_get_topic_id() To get the topic id
2860 * @uses bbp_is_topic() To check if the passed id is a topic
2861 * @uses do_action() Calls 'bbp_trash_topic' with the topic id
2862 * @uses bbp_has_replies() To check if the topic has replies
2863 * @uses bbp_replies() To loop through the replies
2864 * @uses bbp_the_reply() To set a reply as the current reply in the loop
2865 * @uses bbp_get_reply_id() To get the reply id
2866 * @uses wp_trash_post() To trash the reply
2867 * @uses update_post_meta() To save a list of just trashed replies for future use
2868 */
2869 function bbp_trash_topic( $topic_id = 0 ) {
2870 $bbp = bbpress();
2871 $topic_id = bbp_get_topic_id( $topic_id );
2872
2873 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2874 return false;
2875
2876 do_action( 'bbp_trash_topic', $topic_id );
2877
2878 // Topic is being trashed, so its replies are trashed too
2879 // @todo remove meta query
2880 if ( bbp_has_replies( array(
2881 'post_type' => bbp_get_reply_post_type(),
2882 'post_status' => bbp_get_public_status_id(),
2883 'posts_per_page' => -1,
2884 'meta_query' => array( array(
2885 'key' => '_bbp_topic_id',
2886 'value' => $topic_id,
2887 'type' => 'numeric',
2888 'compare' => '='
2889 ) )
2890 ) ) ) {
2891
2892 // Prevent debug notices
2893 $pre_trashed_replies = array();
2894
2895 // Loop through replies, trash them, and add them to array
2896 while ( bbp_replies() ) {
2897 bbp_the_reply();
2898 wp_trash_post( $bbp->reply_query->post->ID );
2899 $pre_trashed_replies[] = $bbp->reply_query->post->ID;
2900 }
2901
2902 // Set a post_meta entry of the replies that were trashed by this action.
2903 // This is so we can possibly untrash them, without untrashing replies
2904 // that were purposefully trashed before.
2905 update_post_meta( $topic_id, '_bbp_pre_trashed_replies', $pre_trashed_replies );
2906 }
2907 }
2908
2909 /**
2910 * Called before untrashing a topic
2911 *
2912 * @uses bbp_get_topic_id() To get the topic id
2913 * @uses bbp_is_topic() To check if the passed id is a topic
2914 * @uses do_action() Calls 'bbp_untrash_topic' with the topic id
2915 * @uses get_post_meta() To get the list of replies which were trashed with the
2916 * topic
2917 * @uses wp_untrash_post() To untrash the reply
2918 */
2919 function bbp_untrash_topic( $topic_id = 0 ) {
2920 $topic_id = bbp_get_topic_id( $topic_id );
2921
2922 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2923 return false;
2924
2925 do_action( 'bbp_untrash_topic', $topic_id );
2926
2927 // Get the replies that were not previously trashed
2928 $pre_trashed_replies = get_post_meta( $topic_id, '_bbp_pre_trashed_replies', true );
2929
2930 // There are replies to untrash
2931 if ( !empty( $pre_trashed_replies ) ) {
2932
2933 // Maybe reverse the trashed replies array
2934 if ( is_array( $pre_trashed_replies ) )
2935 $pre_trashed_replies = array_reverse( $pre_trashed_replies );
2936
2937 // Loop through replies
2938 foreach ( (array) $pre_trashed_replies as $reply ) {
2939 wp_untrash_post( $reply );
2940 }
2941 }
2942 }
2943
2944 /** After Delete/Trash/Untrash ************************************************/
2945
2946 /**
2947 * Called after deleting a topic
2948 *
2949 * @uses bbp_get_topic_id() To get the topic id
2950 * @uses bbp_is_topic() To check if the passed id is a topic
2951 * @uses do_action() Calls 'bbp_deleted_topic' with the topic id
2952 */
2953 function bbp_deleted_topic( $topic_id = 0 ) {
2954 $topic_id = bbp_get_topic_id( $topic_id );
2955
2956 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2957 return false;
2958
2959 do_action( 'bbp_deleted_topic', $topic_id );
2960 }
2961
2962 /**
2963 * Called after trashing a topic
2964 *
2965 * @uses bbp_get_topic_id() To get the topic id
2966 * @uses bbp_is_topic() To check if the passed id is a topic
2967 * @uses do_action() Calls 'bbp_trashed_topic' with the topic id
2968 */
2969 function bbp_trashed_topic( $topic_id = 0 ) {
2970 $topic_id = bbp_get_topic_id( $topic_id );
2971
2972 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2973 return false;
2974
2975 do_action( 'bbp_trashed_topic', $topic_id );
2976 }
2977
2978 /**
2979 * Called after untrashing a topic
2980 *
2981 * @uses bbp_get_topic_id() To get the topic id
2982 * @uses bbp_is_topic() To check if the passed id is a topic
2983 * @uses do_action() Calls 'bbp_untrashed_topic' with the topic id
2984 */
2985 function bbp_untrashed_topic( $topic_id = 0 ) {
2986 $topic_id = bbp_get_topic_id( $topic_id );
2987
2988 if ( empty( $topic_id ) || !bbp_is_topic( $topic_id ) )
2989 return false;
2990
2991 do_action( 'bbp_untrashed_topic', $topic_id );
2992 }
2993
2994 /** Settings ******************************************************************/
2995
2996 /**
2997 * Return the topics per page setting
2998 *
2999 * @since bbPress (r3540)
3000 *
3001 * @uses get_option() To get the setting
3002 * @uses apply_filters() To allow the return value to be manipulated
3003 * @return int
3004 */
3005 function bbp_get_topics_per_page() {
3006
3007 // The default per setting
3008 $default = 15;
3009
3010 // Get database option and cast as integer
3011 $per = $retval = (int) get_option( '_bbp_topics_per_page', $default );
3012
3013 // If return val is empty, set it to default
3014 if ( empty( $retval ) )
3015 $retval = $default;
3016
3017 // Filter and return
3018 return (int) apply_filters( 'bbp_get_topics_per_page', $retval, $per );
3019 }
3020
3021 /**
3022 * Return the topics per RSS page setting
3023 *
3024 * @since bbPress (r3540)
3025 *
3026 * @uses get_option() To get the setting
3027 * @uses apply_filters() To allow the return value to be manipulated
3028 * @return int
3029 */
3030 function bbp_get_topics_per_rss_page() {
3031
3032 // The default per setting
3033 $default = 25;
3034
3035 // Get database option and cast as integer
3036 $per = $retval = (int) get_option( '_bbp_topics_per_rss_page', $default );
3037
3038 // If return val is empty, set it to default
3039 if ( empty( $retval ) )
3040 $retval = $default;
3041
3042 // Filter and return
3043 return (int) apply_filters( 'bbp_get_topics_per_rss_page', $retval, $per );
3044 }
3045
3046 /** Autoembed *****************************************************************/
3047
3048 /**
3049 * Check if autoembeds are enabled and hook them in if so
3050 *
3051 * @since bbPress (r3752)
3052 * @global WP_Embed $wp_embed
3053 */
3054 function bbp_topic_content_autoembed() {
3055 global $wp_embed;
3056
3057 if ( bbp_use_autoembed() && is_a( $wp_embed, 'WP_Embed' ) ) {
3058 add_filter( 'bbp_get_topic_content', array( $wp_embed, 'autoembed' ), 8 );
3059 }
3060 }
3061
3062 /** Feeds *********************************************************************/
3063
3064 /**
3065 * Output an RSS2 feed of topics, based on the query passed.
3066 *
3067 * @since bbPress (r3171)
3068 *
3069 * @uses bbp_version()
3070 * @uses bbp_is_single_topic()
3071 * @uses bbp_user_can_view_forum()
3072 * @uses bbp_get_topic_forum_id()
3073 * @uses bbp_show_load_topic()
3074 * @uses bbp_topic_permalink()
3075 * @uses bbp_topic_title()
3076 * @uses bbp_get_topic_reply_count()
3077 * @uses bbp_topic_content()
3078 * @uses bbp_has_topics()
3079 * @uses bbp_topics()
3080 * @uses bbp_the_topic()
3081 * @uses get_wp_title_rss()
3082 * @uses get_option()
3083 * @uses bloginfo_rss
3084 * @uses self_link()
3085 * @uses the_author()
3086 * @uses get_post_time()
3087 * @uses rss_enclosure()
3088 * @uses do_action()
3089 * @uses apply_filters()
3090 *
3091 * @param array $topics_query
3092 */
3093 function bbp_display_topics_feed_rss2( $topics_query = array() ) {
3094
3095 // User cannot access this forum
3096 if ( bbp_is_single_forum() && !bbp_user_can_view_forum( array( 'forum_id' => bbp_get_forum_id() ) ) )
3097 return;
3098
3099 // Display the feed
3100 header( 'Content-Type: text/xml; charset=' . get_option( 'blog_charset' ), true );
3101 header( 'Status: 200 OK' );
3102 echo '<?xml version="1.0" encoding="' . get_option( 'blog_charset' ) . '"?' . '>'; ?>
3103
3104 <rss version="2.0"
3105 xmlns:content="http://purl.org/rss/1.0/modules/content/"
3106 xmlns:wfw="http://wellformedweb.org/CommentAPI/"
3107 xmlns:dc="http://purl.org/dc/elements/1.1/"
3108 xmlns:atom="http://www.w3.org/2005/Atom"
3109
3110 <?php do_action( 'bbp_feed' ); ?>
3111 >
3112
3113 <channel>
3114
3115 <title><?php bloginfo_rss( 'name' ); ?> &#187; <?php _e( 'All Topics', 'bbpress' ); ?></title>
3116 <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
3117 <link><?php self_link(); ?></link>
3118 <description><?php //?></description>
3119 <pubDate><?php echo mysql2date( 'D, d M Y H:i:s O', '', false ); ?></pubDate>
3120 <generator>http://bbpress.org/?v=<?php bbp_version(); ?></generator>
3121 <language><?php echo get_option( 'rss_language' ); ?></language>
3122
3123 <?php do_action( 'bbp_feed_head' ); ?>
3124
3125 <?php if ( bbp_has_topics( $topics_query ) ) : ?>
3126
3127 <?php while ( bbp_topics() ) : bbp_the_topic(); ?>
3128
3129 <item>
3130 <guid><?php bbp_topic_permalink(); ?></guid>
3131 <title><![CDATA[<?php bbp_topic_title(); ?>]]></title>
3132 <link><?php bbp_topic_permalink(); ?></link>
3133 <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>
3134 <dc:creator><?php the_author() ?></dc:creator>
3135
3136 <?php if ( !post_password_required() ) : ?>
3137
3138 <description>
3139 <![CDATA[
3140 <p><?php printf( __( 'Replies: %s', 'bbpress' ), bbp_get_topic_reply_count() ); ?></p>
3141 <?php bbp_topic_content(); ?>
3142 ]]>
3143 </description>
3144
3145 <?php rss_enclosure(); ?>
3146
3147 <?php endif; ?>
3148
3149 <?php do_action( 'bbp_feed_item' ); ?>
3150
3151 </item>
3152
3153 <?php endwhile; ?>
3154 <?php endif; ?>
3155
3156 <?php do_action( 'bbp_feed_footer' ); ?>
3157
3158 </channel>
3159 </rss>
3160
3161 <?php
3162 exit();
3163 }
3164
3165 /** Permissions ***************************************************************/
3166
3167 /**
3168 * Redirect if unathorized user is attempting to edit a topic
3169 *
3170 * @since bbPress (r3605)
3171 *
3172 * @uses bbp_is_topic_edit()
3173 * @uses current_user_can()
3174 * @uses bbp_get_topic_id()
3175 * @uses wp_safe_redirect()
3176 * @uses bbp_get_topic_permalink()
3177 */
3178 function bbp_check_topic_edit() {
3179
3180 // Bail if not editing a topic
3181 if ( !bbp_is_topic_edit() )
3182 return;
3183
3184 // User cannot edit topic, so redirect back to topic
3185 if ( !current_user_can( 'edit_topic', bbp_get_topic_id() ) ) {
3186 wp_safe_redirect( bbp_get_topic_permalink() );
3187 exit();
3188 }
3189 }
3190
3191 /**
3192 * Redirect if unathorized user is attempting to edit a topic tag
3193 *
3194 * @since bbPress (r3605)
3195 *
3196 * @uses bbp_is_topic_tag_edit()
3197 * @uses current_user_can()
3198 * @uses bbp_get_topic_tag_id()
3199 * @uses wp_safe_redirect()
3200 * @uses bbp_get_topic_tag_link()
3201 */
3202 function bbp_check_topic_tag_edit() {
3203
3204 // Bail if not editing a topic tag
3205 if ( !bbp_is_topic_tag_edit() )
3206 return;
3207
3208 // Bail if current user cannot edit topic tags
3209 if ( !current_user_can( 'edit_topic_tags', bbp_get_topic_tag_id() ) ) {
3210 wp_safe_redirect( bbp_get_topic_tag_link() );
3211 exit();
3212 }
3213 }
3214