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

bbp-forum-functions.php in bbPress 2.1-rc4, at bbp-includes/bbp-forum-functions.php

2,200 lines 74.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Forum 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 forum to function properly.
18 *
19 * @since bbPress (r3349)
20 *
21 * @uses bbp_parse_args()
22 * @uses bbp_get_forum_post_type()
23 * @uses wp_insert_post()
24 * @uses update_post_meta()
25 *
26 * @param array $forum_data Forum post data
27 * @param arrap $forum_meta Forum meta data
28 */
29 function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) {
30
31 // Forum
32 $default_forum = array(
33 'post_parent' => 0, // forum ID
34 'post_status' => bbp_get_public_status_id(),
35 'post_type' => bbp_get_forum_post_type(),
36 'post_author' => bbp_get_current_user_id(),
37 'post_password' => '',
38 'post_content' => '',
39 'post_title' => '',
40 'menu_order' => 0,
41 'comment_status' => 'closed'
42 );
43 $forum_data = bbp_parse_args( $forum_data, $default_forum, 'insert_forum' );
44
45 // Insert forum
46 $forum_id = wp_insert_post( $forum_data );
47
48 // Bail if no forum was added
49 if ( empty( $forum_id ) )
50 return false;
51
52 // Forum meta
53 $default_meta = array(
54 'reply_count' => 0,
55 'topic_count' => 0,
56 'topic_count_hidden' => 0,
57 'total_reply_count' => 0,
58 'total_topic_count' => 0,
59 'last_topic_id' => 0,
60 'last_reply_id' => 0,
61 'last_active_id' => 0,
62 'last_active_time' => 0,
63 'forum_subforum_count' => 0,
64 );
65 $forum_meta = bbp_parse_args( $forum_meta, $default_meta, 'insert_forum_meta' );
66
67 // Insert forum meta
68 foreach ( $forum_meta as $meta_key => $meta_value )
69 update_post_meta( $forum_id, '_bbp_' . $meta_key, $meta_value );
70
71 // Return new forum ID
72 return $forum_id;
73 }
74
75 /** Post Form Handlers ********************************************************/
76
77 /**
78 * Handles the front end forum submission
79 *
80 * @uses bbPress:errors::add() To log various error messages
81 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
82 * @uses bbp_is_anonymous() To check if an anonymous post is being made
83 * @uses current_user_can() To check if the current user can publish forum
84 * @uses bbp_get_current_user_id() To get the current user id
85 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
86 * @uses bbp_set_current_anonymous_user_data() To set the anonymous user cookies
87 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
88 * @uses esc_attr() For sanitization
89 * @uses bbp_is_forum_category() To check if the forum is a category
90 * @uses bbp_is_forum_closed() To check if the forum is closed
91 * @uses bbp_is_forum_private() To check if the forum is private
92 * @uses bbp_check_for_flood() To check for flooding
93 * @uses bbp_check_for_duplicate() To check for duplicates
94 * @uses bbp_get_forum_post_type() To get the forum post type
95 * @uses remove_filter() To remove 'wp_filter_kses' filters if needed
96 * @uses apply_filters() Calls 'bbp_new_forum_pre_title' with the content
97 * @uses apply_filters() Calls 'bbp_new_forum_pre_content' with the content
98 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
99 * @uses wp_insert_post() To insert the forum
100 * @uses do_action() Calls 'bbp_new_forum' with the forum id, forum id,
101 * anonymous data and reply author
102 * @uses bbp_stick_forum() To stick or super stick the forum
103 * @uses bbp_unstick_forum() To unstick the forum
104 * @uses bbp_get_forum_permalink() To get the forum permalink
105 * @uses wp_safe_redirect() To redirect to the forum link
106 * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
107 * messages
108 */
109 function bbp_new_forum_handler() {
110
111 // Bail if not a POST action
112 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
113 return;
114
115 // Bail if action is not bbp-new-forum
116 if ( empty( $_POST['action'] ) || ( 'bbp-new-forum' !== $_POST['action'] ) )
117 return;
118
119 // Nonce check
120 if ( ! bbp_verify_nonce_request( 'bbp-new-forum' ) ) {
121 bbp_add_error( 'bbp_new_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
122 return;
123 }
124
125 // Define local variable(s)
126 $view_all = $anonymous_data = false;
127 $forum_parent_id = $forum_author = 0;
128 $forum_title = $forum_content = '';
129
130 /** Forum Author **********************************************************/
131
132 // User cannot create forums
133 if ( !current_user_can( 'publish_forums' ) ) {
134 bbp_add_error( 'bbp_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to create new forums.', 'bbpress' ) );
135 return;
136 }
137
138 // Forum author is current user
139 $forum_author = bbp_get_current_user_id();
140
141 // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
142 if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && wp_create_nonce( 'bbp-unfiltered-html-forum_new' ) == $_POST['_bbp_unfiltered_html_forum'] ) {
143 remove_filter( 'bbp_new_forum_pre_title', 'wp_filter_kses' );
144 remove_filter( 'bbp_new_forum_pre_content', 'wp_filter_kses' );
145 }
146
147 /** Forum Title ***********************************************************/
148
149 if ( !empty( $_POST['bbp_forum_title'] ) )
150 $forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) );
151
152 // Filter and sanitize
153 $forum_title = apply_filters( 'bbp_new_forum_pre_title', $forum_title );
154
155 // No forum title
156 if ( empty( $forum_title ) )
157 bbp_add_error( 'bbp_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) );
158
159 /** Forum Content *********************************************************/
160
161 if ( !empty( $_POST['bbp_forum_content'] ) )
162 $forum_content = $_POST['bbp_forum_content'];
163
164 // Filter and sanitize
165 $forum_content = apply_filters( 'bbp_new_forum_pre_content', $forum_content );
166
167 // No forum content
168 if ( empty( $forum_content ) )
169 bbp_add_error( 'bbp_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) );
170
171 /** Forum Parent **********************************************************/
172
173 // Forum parent was passed (the norm)
174 if ( !empty( $_POST['bbp_forum_parent_id'] ) )
175 $forum_parent_id = (int) $_POST['bbp_forum_parent_id'];
176
177 // Filter and sanitize
178 $forum_parent_id = apply_filters( 'bbp_new_forum_pre_parent_id', $forum_parent_id );
179
180 // No forum parent was passed (should never happen)
181 if ( empty( $forum_parent_id ) ) {
182 bbp_add_error( 'bbp_new_forum_missing_parent', __( '<strong>ERROR</strong>: Your forum must have a parent.', 'bbpress' ) );
183
184 // Forum exists
185 } elseif ( !empty( $forum_parent_id ) ) {
186
187 // Forum is a category
188 if ( bbp_is_forum_category( $forum_parent_id ) ) {
189 bbp_add_error( 'bbp_new_forum_forum_category', __( '<strong>ERROR</strong>: This forum is a category. No forums can be created in this forum.', 'bbpress' ) );
190 }
191
192 // Forum is closed and user cannot access
193 if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) {
194 bbp_add_error( 'bbp_new_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) );
195 }
196
197 // Forum is private and user cannot access
198 if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) {
199 bbp_add_error( 'bbp_new_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
200 }
201
202 // Forum is hidden and user cannot access
203 if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) {
204 bbp_add_error( 'bbp_new_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
205 }
206 }
207
208 /** Forum Flooding ********************************************************/
209
210 if ( !bbp_check_for_flood( $anonymous_data, $forum_author ) )
211 bbp_add_error( 'bbp_forum_flood', __( '<strong>ERROR</strong>: Slow down; you move too fast.', 'bbpress' ) );
212
213 /** Forum Duplicate *******************************************************/
214
215 if ( !bbp_check_for_duplicate( array( 'post_type' => bbp_get_forum_post_type(), 'post_author' => $forum_author, 'post_content' => $forum_content, 'anonymous_data' => $anonymous_data ) ) )
216 bbp_add_error( 'bbp_forum_duplicate', __( '<strong>ERROR</strong>: This forum already exists.', 'bbpress' ) );
217
218 /** Forum Blacklist *******************************************************/
219
220 if ( !bbp_check_for_blacklist( $anonymous_data, $forum_author, $forum_title, $forum_content ) )
221 bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be created at this time.', 'bbpress' ) );
222
223 /** Forum Moderation ******************************************************/
224
225 $post_status = bbp_get_public_status_id();
226 if ( !bbp_check_for_moderation( $anonymous_data, $forum_author, $forum_title, $forum_content ) )
227 $post_status = bbp_get_pending_status_id();
228
229 /** Additional Actions (Before Save) **************************************/
230
231 do_action( 'bbp_new_forum_pre_extras', $forum_parent_id );
232
233 // Bail if errors
234 if ( bbp_has_errors() )
235 return;
236
237 /** No Errors *************************************************************/
238
239 // Add the content of the form to $forum_data as an array
240 // Just in time manipulation of forum data before being created
241 $forum_data = apply_filters( 'bbp_new_forum_pre_insert', array(
242 'post_author' => $forum_author,
243 'post_title' => $forum_title,
244 'post_content' => $forum_content,
245 'post_parent' => $forum_parent_id,
246 'post_status' => $post_status,
247 'post_type' => bbp_get_forum_post_type(),
248 'comment_status' => 'closed'
249 ) );
250
251 // Insert forum
252 $forum_id = wp_insert_post( $forum_data );
253
254 /** No Errors *************************************************************/
255
256 if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) {
257
258 /** Trash Check *******************************************************/
259
260 // If the forum is trash, or the forum_status is switched to
261 // trash, trash it properly
262 if ( ( get_post_field( 'post_status', $forum_id ) == bbp_get_trash_status_id() ) || ( $forum_data['post_status'] == bbp_get_trash_status_id() ) ) {
263
264 // Trash the reply
265 wp_trash_post( $forum_id );
266
267 // Force view=all
268 $view_all = true;
269 }
270
271 /** Spam Check ********************************************************/
272
273 // If reply or forum are spam, officially spam this reply
274 if ( $forum_data['post_status'] == bbp_get_spam_status_id() ) {
275 add_post_meta( $forum_id, '_bbp_spam_meta_status', bbp_get_public_status_id() );
276
277 // Force view=all
278 $view_all = true;
279 }
280
281 /** Update counts, etc... *********************************************/
282
283 $forum_args = array(
284 'forum_id' => $forum_id,
285 'post_parent' => $forum_parent_id,
286 'forum_author' => $forum_author,
287 'last_topic_id' => 0,
288 'last_reply_id' => 0,
289 'last_active_id' => 0,
290 'last_active_time' => 0,
291 'last_active_status' => bbp_get_public_status_id()
292 );
293 do_action( 'bbp_new_forum', $forum_args );
294
295 /** Additional Actions (After Save) ***********************************/
296
297 do_action( 'bbp_new_forum_post_extras', $forum_id );
298
299 /** Redirect **********************************************************/
300
301 // Redirect to
302 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
303
304 // Get the forum URL
305 $redirect_url = bbp_get_forum_permalink( $forum_id, $redirect_to );
306
307 // Add view all?
308 if ( bbp_get_view_all() || !empty( $view_all ) ) {
309
310 // User can moderate, so redirect to forum with view all set
311 if ( current_user_can( 'moderate' ) ) {
312 $redirect_url = bbp_add_view_all( $redirect_url );
313
314 // User cannot moderate, so redirect to forum
315 } else {
316 $redirect_url = bbp_get_forum_permalink( $forum_id );
317 }
318 }
319
320 // Allow to be filtered
321 $redirect_url = apply_filters( 'bbp_new_forum_redirect_to', $redirect_url, $redirect_to );
322
323 /** Successful Save ***************************************************/
324
325 // Redirect back to new forum
326 wp_safe_redirect( $redirect_url );
327
328 // For good measure
329 exit();
330
331 // Errors
332 } else {
333 $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : '';
334 bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error, 'bbpress' ) );
335 }
336 }
337
338 /**
339 * Handles the front end edit forum submission
340 *
341 * @uses bbPress:errors::add() To log various error messages
342 * @uses bbp_get_forum() To get the forum
343 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
344 * @uses bbp_is_forum_anonymous() To check if forum is by an anonymous user
345 * @uses current_user_can() To check if the current user can edit the forum
346 * @uses bbp_filter_anonymous_post_data() To filter anonymous data
347 * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error}
348 * @uses esc_attr() For sanitization
349 * @uses bbp_is_forum_category() To check if the forum is a category
350 * @uses bbp_is_forum_closed() To check if the forum is closed
351 * @uses bbp_is_forum_private() To check if the forum is private
352 * @uses remove_filter() To remove 'wp_filter_kses' filters if needed
353 * @uses apply_filters() Calls 'bbp_edit_forum_pre_title' with the title and
354 * forum id
355 * @uses apply_filters() Calls 'bbp_edit_forum_pre_content' with the content
356 * and forum id
357 * @uses bbPress::errors::get_error_codes() To get the {@link WP_Error} errors
358 * @uses wp_save_post_revision() To save a forum revision
359 * @uses bbp_update_forum_revision_log() To update the forum revision log
360 * @uses wp_update_post() To update the forum
361 * @uses do_action() Calls 'bbp_edit_forum' with the forum id, forum id,
362 * anonymous data and reply author
363 * @uses bbp_move_forum_handler() To handle movement of a forum from one forum
364 * to another
365 * @uses bbp_get_forum_permalink() To get the forum permalink
366 * @uses wp_safe_redirect() To redirect to the forum link
367 * @uses bbPress::errors::get_error_messages() To get the {@link WP_Error} error
368 * messages
369 */
370 function bbp_edit_forum_handler() {
371
372 // Bail if not a POST action
373 if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
374 return;
375
376 // Bail if action is not bbp-edit-forum
377 if ( empty( $_POST['action'] ) || ( 'bbp-edit-forum' !== $_POST['action'] ) )
378 return;
379
380 // Define local variable(s)
381 $anonymous_data = array();
382 $forum = $forum_id = $forum_parent_id = 0;
383 $forum_title = $forum_content = $forum_edit_reason = '';
384
385 /** Forum *****************************************************************/
386
387 // Forum id was not passed
388 if ( empty( $_POST['bbp_forum_id'] ) ) {
389 bbp_add_error( 'bbp_edit_forum_id', __( '<strong>ERROR</strong>: Forum ID not found.', 'bbpress' ) );
390 return;
391
392 // Forum id was passed
393 } elseif ( is_numeric( $_POST['bbp_forum_id'] ) ) {
394 $forum_id = (int) $_POST['bbp_forum_id'];
395 $forum = bbp_get_forum( $forum_id );
396 }
397
398 // Nonce check
399 if ( ! bbp_verify_nonce_request( 'bbp-edit-forum_' . $forum_id ) ) {
400 bbp_add_error( 'bbp_edit_forum_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
401 return;
402
403 // Forum does not exist
404 } elseif ( empty( $forum ) ) {
405 bbp_add_error( 'bbp_edit_forum_not_found', __( '<strong>ERROR</strong>: The forum you want to edit was not found.', 'bbpress' ) );
406 return;
407
408 // User cannot edit this forum
409 } elseif ( !current_user_can( 'edit_forum', $forum_id ) ) {
410 bbp_add_error( 'bbp_edit_forum_permissions', __( '<strong>ERROR</strong>: You do not have permission to edit that forum.', 'bbpress' ) );
411 return;
412 }
413
414 // Remove wp_filter_kses filters from title and content for capable users and if the nonce is verified
415 if ( current_user_can( 'unfiltered_html' ) && !empty( $_POST['_bbp_unfiltered_html_forum'] ) && ( wp_create_nonce( 'bbp-unfiltered-html-forum_' . $forum_id ) == $_POST['_bbp_unfiltered_html_forum'] ) ) {
416 remove_filter( 'bbp_edit_forum_pre_title', 'wp_filter_kses' );
417 remove_filter( 'bbp_edit_forum_pre_content', 'wp_filter_kses' );
418 }
419
420 /** Forum Parent ***********************************************************/
421
422 // Forum parent id was passed
423 if ( is_numeric( $_POST['bbp_forum_parent_id'] ) ) {
424 $forum_parent_id = (int) $_POST['bbp_forum_parent_id'];
425 }
426
427 // Current forum this forum is in
428 $current_parent_forum_id = bbp_get_forum_parent_id( $forum_id );
429
430 // Forum exists
431 if ( !empty( $forum_parent_id ) && ( $forum_parent_id !== $current_parent_forum_id ) ) {
432
433 // Forum is closed and user cannot access
434 if ( bbp_is_forum_closed( $forum_parent_id ) && !current_user_can( 'edit_forum', $forum_parent_id ) ) {
435 bbp_add_error( 'bbp_edit_forum_forum_closed', __( '<strong>ERROR</strong>: This forum has been closed to new forums.', 'bbpress' ) );
436 }
437
438 // Forum is private and user cannot access
439 if ( bbp_is_forum_private( $forum_parent_id ) && !current_user_can( 'read_private_forums' ) ) {
440 bbp_add_error( 'bbp_edit_forum_forum_private', __( '<strong>ERROR</strong>: This forum is private and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
441 }
442
443 // Forum is hidden and user cannot access
444 if ( bbp_is_forum_hidden( $forum_parent_id ) && !current_user_can( 'read_hidden_forums' ) ) {
445 bbp_add_error( 'bbp_edit_forum_forum_hidden', __( '<strong>ERROR</strong>: This forum is hidden and you do not have the capability to read or create new forums in it.', 'bbpress' ) );
446 }
447 }
448
449 /** Forum Title ***********************************************************/
450
451 if ( !empty( $_POST['bbp_forum_title'] ) )
452 $forum_title = esc_attr( strip_tags( $_POST['bbp_forum_title'] ) );
453
454 // Filter and sanitize
455 $forum_title = apply_filters( 'bbp_edit_forum_pre_title', $forum_title, $forum_id );
456
457 // No forum title
458 if ( empty( $forum_title ) )
459 bbp_add_error( 'bbp_edit_forum_title', __( '<strong>ERROR</strong>: Your forum needs a title.', 'bbpress' ) );
460
461 /** Forum Content *********************************************************/
462
463 if ( !empty( $_POST['bbp_forum_content'] ) )
464 $forum_content = $_POST['bbp_forum_content'];
465
466 // Filter and sanitize
467 $forum_content = apply_filters( 'bbp_edit_forum_pre_content', $forum_content, $forum_id );
468
469 // No forum content
470 if ( empty( $forum_content ) )
471 bbp_add_error( 'bbp_edit_forum_content', __( '<strong>ERROR</strong>: Your forum description cannot be empty.', 'bbpress' ) );
472
473 /** Forum Blacklist *******************************************************/
474
475 if ( !bbp_check_for_blacklist( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) )
476 bbp_add_error( 'bbp_forum_blacklist', __( '<strong>ERROR</strong>: Your forum cannot be edited at this time.', 'bbpress' ) );
477
478 /** Forum Moderation ******************************************************/
479
480 $post_status = bbp_get_public_status_id();
481 if ( !bbp_check_for_moderation( $anonymous_data, bbp_get_forum_author_id( $forum_id ), $forum_title, $forum_content ) )
482 $post_status = bbp_get_pending_status_id();
483
484 /** Additional Actions (Before Save) **************************************/
485
486 do_action( 'bbp_edit_forum_pre_extras', $forum_id );
487
488 // Bail if errors
489 if ( bbp_has_errors() )
490 return;
491
492 /** No Errors *************************************************************/
493
494 // Add the content of the form to $forum_data as an array
495 // Just in time manipulation of forum data before being edited
496 $forum_data = apply_filters( 'bbp_edit_forum_pre_insert', array(
497 'ID' => $forum_id,
498 'post_title' => $forum_title,
499 'post_content' => $forum_content,
500 'post_status' => $post_status,
501 'post_parent' => $forum_parent_id
502 ) );
503
504 // Insert forum
505 $forum_id = wp_update_post( $forum_data );
506
507 /** Revisions *************************************************************/
508
509 /**
510 * @todo omitted for 2.1
511 // Revision Reason
512 if ( !empty( $_POST['bbp_forum_edit_reason'] ) )
513 $forum_edit_reason = esc_attr( strip_tags( $_POST['bbp_forum_edit_reason'] ) );
514
515 // Update revision log
516 if ( !empty( $_POST['bbp_log_forum_edit'] ) && ( 1 == $_POST['bbp_log_forum_edit'] ) && ( $revision_id = wp_save_post_revision( $forum_id ) ) ) {
517 bbp_update_forum_revision_log( array(
518 'forum_id' => $forum_id,
519 'revision_id' => $revision_id,
520 'author_id' => bbp_get_current_user_id(),
521 'reason' => $forum_edit_reason
522 ) );
523 }
524 */
525
526 /** No Errors *************************************************************/
527
528 if ( !empty( $forum_id ) && !is_wp_error( $forum_id ) ) {
529
530 // Update counts, etc...
531 $forum_args = array(
532 'forum_id' => $forum_id,
533 'post_parent' => $forum_parent_id,
534 'forum_author' => $forum->post_author,
535 'last_topic_id' => 0,
536 'last_reply_id' => 0,
537 'last_active_id' => 0,
538 'last_active_time' => 0,
539 'last_active_status' => bbp_get_public_status_id()
540 );
541 do_action( 'bbp_edit_forum', $forum_args );
542
543 // If the new forum parent id is not equal to the old forum parent
544 // id, run the bbp_move_forum action and pass the forum's parent id
545 // as the first arg and new forum parent id as the second.
546 // @todo implement
547 //if ( $forum_id != $forum->post_parent )
548 // bbp_move_forum_handler( $forum_parent_id, $forum->post_parent, $forum_id );
549
550 /** Additional Actions (After Save) ***********************************/
551
552 do_action( 'bbp_edit_forum_post_extras', $forum_id );
553
554 /** Redirect **********************************************************/
555
556 // Redirect to
557 $redirect_to = !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '';
558
559 // View all?
560 $view_all = bbp_get_view_all();
561
562 // Get the forum URL
563 $forum_url = bbp_get_forum_permalink( $forum_id, $redirect_to );
564
565 // Add view all?
566 if ( !empty( $view_all ) )
567 $forum_url = bbp_add_view_all( $forum_url );
568
569 // Allow to be filtered
570 $forum_url = apply_filters( 'bbp_edit_forum_redirect_to', $forum_url, $view_all, $redirect_to );
571
572 /** Successful Edit ***************************************************/
573
574 // Redirect back to new forum
575 wp_safe_redirect( $forum_url );
576
577 // For good measure
578 exit();
579
580 /** Errors ****************************************************************/
581
582 } else {
583 $append_error = ( is_wp_error( $forum_id ) && $forum_id->get_error_message() ) ? $forum_id->get_error_message() . ' ' : '';
584 bbp_add_error( 'bbp_forum_error', __( '<strong>ERROR</strong>: The following problem(s) have been found with your forum:' . $append_error . 'Please try again.', 'bbpress' ) );
585 }
586 }
587
588 /**
589 * Handle the saving of core forum metadata (Status, Visibility, and Type)
590 *
591 * @since bbPress (r3678)
592 * @param int $forum_id
593 * @uses bbp_is_forum_closed() To check if forum is closed
594 * @uses bbp_close_forum() To close forum
595 * @uses bbp_open_forum() To open forum
596 * @uses bbp_is_forum_category() To check if forum is a category
597 * @uses bbp_categorize_forum() To turn forum into a category
598 * @uses bbp_normalize_forum() To turn category into forum
599 * @uses bbp_get_public_status_id() To get the public status ID
600 * @uses bbp_get_private_status_id() To get the private status ID
601 * @uses bbp_get_hidden_status_id() To get the hidden status ID
602 * @uses bbp_get_forum_visibility() To get the forums visibility
603 * @uses bbp_hide_forum() To hide a forum
604 * @uses bbp_privatize_forum() To make a forum private
605 * @uses bbp_publicize_forum() To make a forum public
606 * @return If forum ID is empty
607 */
608 function bbp_save_forum_extras( $forum_id = 0 ) {
609
610 // Validate the forum ID
611 $forum_id = bbp_get_forum_id( $forum_id );
612
613 // Bail if forum ID is empty
614 if ( empty( $forum_id ) )
615 return;
616
617 /** Forum Status ******************************************************/
618
619 if ( !empty( $_POST['bbp_forum_status'] ) && in_array( $_POST['bbp_forum_status'], array( 'open', 'closed' ) ) ) {
620 if ( 'closed' == $_POST['bbp_forum_status'] && !bbp_is_forum_closed( $forum_id, false ) ) {
621 bbp_close_forum( $forum_id );
622 } elseif ( 'open' == $_POST['bbp_forum_status'] && bbp_is_forum_closed( $forum_id, false ) ) {
623 bbp_open_forum( $forum_id );
624 }
625 }
626
627 /** Forum Type ********************************************************/
628
629 if ( !empty( $_POST['bbp_forum_type'] ) && in_array( $_POST['bbp_forum_type'], array( 'forum', 'category' ) ) ) {
630 if ( 'category' == $_POST['bbp_forum_type'] && !bbp_is_forum_category( $forum_id ) ) {
631 bbp_categorize_forum( $forum_id );
632 } elseif ( 'forum' == $_POST['bbp_forum_type'] && bbp_is_forum_category( $forum_id ) ) {
633 bbp_normalize_forum( $forum_id );
634 }
635 }
636
637 /** Forum Visibility **************************************************/
638
639 if ( !empty( $_POST['bbp_forum_visibility'] ) && in_array( $_POST['bbp_forum_visibility'], array( bbp_get_public_status_id(), bbp_get_private_status_id(), bbp_get_hidden_status_id() ) ) ) {
640
641 // Get forums current visibility
642 $visibility = bbp_get_forum_visibility( $forum_id );
643
644 // What is the new forum visibility setting?
645 switch ( $_POST['bbp_forum_visibility'] ) {
646
647 // Hidden
648 case bbp_get_hidden_status_id() :
649 bbp_hide_forum( $forum_id, $visibility );
650 break;
651
652 // Private
653 case bbp_get_private_status_id() :
654 bbp_privatize_forum( $forum_id, $visibility );
655 break;
656
657 // Publish (default)
658 case bbp_get_public_status_id() :
659 default :
660 bbp_publicize_forum( $forum_id, $visibility );
661 break;
662 }
663 }
664 }
665
666 /** Walk **********************************************************************/
667
668 /**
669 * Walk the forum tree
670 *
671 * @param object $forums Forums
672 * @param int $depth Depth
673 * @param int $current Current forum
674 * @param array $r Parsed arguments, supported by the walker. If you want to
675 * use your own walker, pass the 'walker' arg with the walker.
676 * The walker defaults to {@link BBP_Walker_Forum}
677 * @return object Walked forum tree
678 */
679 function bbp_walk_forum( $forums, $depth, $current, $r ) {
680 $walker = empty( $r['walker'] ) ? new BBP_Walker_Forum : $r['walker'];
681 $args = array( $forums, $depth, $r, $current );
682 return call_user_func_array( array( &$walker, 'walk' ), $args );
683 }
684
685 /** Forum Actions *************************************************************/
686
687 /**
688 * Closes a forum
689 *
690 * @since bbPress (r2746)
691 *
692 * @param int $forum_id forum id
693 * @uses wp_get_single_post() To get the forum
694 * @uses do_action() Calls 'bbp_close_forum' with the forum id
695 * @uses update_post_meta() To add the previous status to a meta
696 * @uses do_action() Calls 'bbp_opened_forum' with the forum id
697 * @return mixed False or {@link WP_Error} on failure, forum id on success
698 */
699 function bbp_close_forum( $forum_id = 0 ) {
700
701 $forum_id = bbp_get_forum_id( $forum_id );
702
703 do_action( 'bbp_close_forum', $forum_id );
704
705 update_post_meta( $forum_id, '_bbp_status', 'closed' );
706
707 do_action( 'bbp_closed_forum', $forum_id );
708
709 return $forum_id;
710 }
711
712 /**
713 * Opens a forum
714 *
715 * @since bbPress (r2746)
716 *
717 * @param int $forum_id forum id
718 * @uses wp_get_single_post() To get the forum
719 * @uses do_action() Calls 'bbp_open_forum' with the forum id
720 * @uses get_post_meta() To get the previous status
721 * @uses update_post_meta() To delete the previous status meta
722 * @uses do_action() Calls 'bbp_opened_forum' with the forum id
723 * @return mixed False or {@link WP_Error} on failure, forum id on success
724 */
725 function bbp_open_forum( $forum_id = 0 ) {
726
727 $forum_id = bbp_get_forum_id( $forum_id );
728
729 do_action( 'bbp_open_forum', $forum_id );
730
731 update_post_meta( $forum_id, '_bbp_status', 'open' );
732
733 do_action( 'bbp_opened_forum', $forum_id );
734
735 return $forum_id;
736 }
737
738 /**
739 * Make the forum a category
740 *
741 * @since bbPress (r2746)
742 *
743 * @param int $forum_id Optional. Forum id
744 * @uses update_post_meta() To update the forum category meta
745 * @return bool False on failure, true on success
746 */
747 function bbp_categorize_forum( $forum_id = 0 ) {
748
749 $forum_id = bbp_get_forum_id( $forum_id );
750
751 do_action( 'bbp_categorize_forum', $forum_id );
752
753 update_post_meta( $forum_id, '_bbp_forum_type', 'category' );
754
755 do_action( 'bbp_categorized_forum', $forum_id );
756
757 return $forum_id;
758 }
759
760 /**
761 * Remove the category status from a forum
762 *
763 * @since bbPress (r2746)
764 *
765 * @param int $forum_id Optional. Forum id
766 * @uses delete_post_meta() To delete the forum category meta
767 * @return bool False on failure, true on success
768 */
769 function bbp_normalize_forum( $forum_id = 0 ) {
770
771 $forum_id = bbp_get_forum_id( $forum_id );
772
773 do_action( 'bbp_normalize_forum', $forum_id );
774
775 update_post_meta( $forum_id, '_bbp_forum_type', 'forum' );
776
777 do_action( 'bbp_normalized_forum', $forum_id );
778
779 return $forum_id;
780 }
781
782 /**
783 * Mark the forum as public
784 *
785 * @since bbPress (r2746)
786 *
787 * @param int $forum_id Optional. Forum id
788 * @uses update_post_meta() To update the forum private meta
789 * @return bool False on failure, true on success
790 */
791 function bbp_publicize_forum( $forum_id = 0, $current_visibility = '' ) {
792
793 $forum_id = bbp_get_forum_id( $forum_id );
794
795 do_action( 'bbp_publicize_forum', $forum_id );
796
797 // Get private forums
798 $private = bbp_get_private_forum_ids();
799
800 // Find this forum in the array
801 if ( in_array( $forum_id, $private ) ) {
802
803 $offset = array_search( $forum_id, $private );
804
805 // Splice around it
806 array_splice( $private, $offset, 1 );
807
808 // Update private forums minus this one
809 update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
810 }
811
812 // Get hidden forums
813 $hidden = bbp_get_hidden_forum_ids();
814
815 // Find this forum in the array
816 if ( in_array( $forum_id, $hidden ) ) {
817
818 $offset = array_search( $forum_id, $hidden );
819
820 // Splice around it
821 array_splice( $hidden, $offset, 1 );
822
823 // Update hidden forums minus this one
824 update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
825 }
826
827 // Only run queries if visibility is changing
828 if ( bbp_get_public_status_id() != $current_visibility ) {
829
830 // Update forum post_status
831 global $wpdb;
832 $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_public_status_id() ), array( 'ID' => $forum_id ) );
833 wp_transition_post_status( bbp_get_public_status_id(), $current_visibility, get_post( $forum_id ) );
834 }
835
836 do_action( 'bbp_publicized_forum', $forum_id );
837
838 return $forum_id;
839 }
840
841 /**
842 * Mark the forum as private
843 *
844 * @since bbPress (r2746)
845 *
846 * @param int $forum_id Optional. Forum id
847 * @uses update_post_meta() To update the forum private meta
848 * @return bool False on failure, true on success
849 */
850 function bbp_privatize_forum( $forum_id = 0, $current_visibility = '' ) {
851
852 $forum_id = bbp_get_forum_id( $forum_id );
853
854 do_action( 'bbp_privatize_forum', $forum_id );
855
856 // Only run queries if visibility is changing
857 if ( bbp_get_private_status_id() != $current_visibility ) {
858
859 // Get hidden forums
860 $hidden = bbp_get_hidden_forum_ids();
861
862 // Find this forum in the array
863 if ( in_array( $forum_id, $hidden ) ) {
864
865 $offset = array_search( $forum_id, $hidden );
866
867 // Splice around it
868 array_splice( $hidden, $offset, 1 );
869
870 // Update hidden forums minus this one
871 update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
872 }
873
874 // Add to '_bbp_private_forums' site option
875 $private = bbp_get_private_forum_ids();
876 $private[] = $forum_id;
877 update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
878
879 // Update forums visibility setting
880 global $wpdb;
881 $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_private_status_id() ), array( 'ID' => $forum_id ) );
882 wp_transition_post_status( bbp_get_private_status_id(), $current_visibility, get_post( $forum_id ) );
883 }
884
885 do_action( 'bbp_privatized_forum', $forum_id );
886
887 return $forum_id;
888 }
889
890 /**
891 * Mark the forum as hidden
892 *
893 * @since bbPress (r2996)
894 *
895 * @param int $forum_id Optional. Forum id
896 * @uses update_post_meta() To update the forum private meta
897 * @return bool False on failure, true on success
898 */
899 function bbp_hide_forum( $forum_id = 0, $current_visibility = '' ) {
900
901 $forum_id = bbp_get_forum_id( $forum_id );
902
903 do_action( 'bbp_hide_forum', $forum_id );
904
905 // Only run queries if visibility is changing
906 if ( bbp_get_hidden_status_id() != $current_visibility ) {
907
908 // Get private forums
909 $private = bbp_get_private_forum_ids();
910
911 // Find this forum in the array
912 if ( in_array( $forum_id, $private ) ) {
913
914 $offset = array_search( $forum_id, $private );
915
916 // Splice around it
917 array_splice( $private, $offset, 1 );
918
919 // Update private forums minus this one
920 update_option( '_bbp_private_forums', array_unique( array_filter( array_values( $private ) ) ) );
921 }
922
923 // Add to '_bbp_hidden_forums' site option
924 $hidden = bbp_get_hidden_forum_ids();
925 $hidden[] = $forum_id;
926 update_option( '_bbp_hidden_forums', array_unique( array_filter( array_values( $hidden ) ) ) );
927
928 // Update forums visibility setting
929 global $wpdb;
930 $wpdb->update( $wpdb->posts, array( 'post_status' => bbp_get_hidden_status_id() ), array( 'ID' => $forum_id ) );
931 wp_transition_post_status( bbp_get_hidden_status_id(), $current_visibility, get_post( $forum_id ) );
932 }
933
934 do_action( 'bbp_hid_forum', $forum_id );
935
936 return $forum_id;
937 }
938
939 /** Count Bumpers *************************************************************/
940
941 /**
942 * Bump the total topic count of a forum
943 *
944 * @since bbPress (r3825)
945 *
946 * @param int $forum_id Optional. Forum id.
947 * @param int $difference Optional. Default 1
948 * @param bool $update_ancestors Optional. Default true
949 * @uses bbp_get_forum_id() To get the forum id
950 * @uses update_post_meta() To update the forum's topic count meta
951 * @uses apply_filters() Calls 'bbp_bump_forum_topic_count' with the topic
952 * count, forum id, and difference
953 * @return int Forum topic count
954 */
955 function bbp_bump_forum_topic_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) {
956
957 // Get some counts
958 $forum_id = bbp_get_forum_id( $forum_id );
959 $topic_count = bbp_get_forum_topic_count( $forum_id, false );
960 $total_topic_count = bbp_get_forum_topic_count( $forum_id, true );
961
962 // Update this forum id
963 update_post_meta( $forum_id, '_bbp_topic_count', (int) $topic_count + (int) $difference );
964 update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topic_count + (int) $difference );
965
966 // Check for ancestors
967 if ( true === $update_ancestors ) {
968
969 // Get post ancestors
970 $forum = get_post( $forum_id );
971 $ancestors = get_post_ancestors( $forum );
972
973 // If has ancestors, loop through them...
974 if ( !empty( $ancestors ) ) {
975 foreach ( (array) $ancestors as $parent_forum_id ) {
976
977 // Get forum counts
978 $parent_topic_count = bbp_get_forum_topic_count( $parent_forum_id, false );
979 $parent_total_topic_count = bbp_get_forum_topic_count( $parent_forum_id, true );
980
981 // Update counts
982 update_post_meta( $parent_forum_id, '_bbp_topic_count', (int) $parent_topic_count + (int) $difference );
983 update_post_meta( $parent_forum_id, '_bbp_total_topic_count', (int) $parent_total_topic_count + (int) $difference );
984 }
985 }
986 }
987
988 return (int) apply_filters( 'bbp_bump_forum_topic_count', (int) $total_topic_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors );
989 }
990
991 /**
992 * Bump the total hidden topic count of a forum
993 *
994 * @since bbPress (r3825)
995 *
996 * @param int $forum_id Optional. Forum id.
997 * @param int $difference Optional. Default 1
998 * @uses bbp_get_forum_id() To get the forum id
999 * @uses update_post_meta() To update the forum's topic count meta
1000 * @uses apply_filters() Calls 'bbp_bump_forum_topic_count_hidden' with the
1001 * topic count, forum id, and difference
1002 * @return int Forum hidden topic count
1003 */
1004 function bbp_bump_forum_topic_count_hidden( $forum_id = 0, $difference = 1 ) {
1005
1006 // Get some counts
1007 $forum_id = bbp_get_forum_id( $forum_id );
1008 $topic_count = bbp_get_forum_topic_count_hidden( $forum_id, false );
1009 $new_count = (int) $topic_count + (int) $difference;
1010
1011 // Update this forum id
1012 update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $new_count );
1013
1014 return (int) apply_filters( 'bbp_bump_forum_topic_count_hidden', (int) $new_count, $forum_id, (int) $difference );
1015 }
1016
1017 /**
1018 * Bump the total topic count of a forum
1019 *
1020 * @since bbPress (r3825)
1021 *
1022 * @param int $forum_id Optional. Forum id.
1023 * @param int $difference Optional. Default 1
1024 * @param bool $update_ancestors Optional. Default true
1025 * @uses bbp_get_forum_id() To get the forum id
1026 * @uses update_post_meta() To update the forum's topic count meta
1027 * @uses apply_filters() Calls 'bbp_bump_forum_reply_count' with the topic
1028 * count, forum id, and difference
1029 * @return int Forum topic count
1030 */
1031 function bbp_bump_forum_reply_count( $forum_id = 0, $difference = 1, $update_ancestors = true ) {
1032
1033 // Get some counts
1034 $forum_id = bbp_get_forum_id( $forum_id );
1035 $topic_count = bbp_get_forum_reply_count( $forum_id, false );
1036 $total_reply_count = bbp_get_forum_reply_count( $forum_id, true );
1037
1038 // Update this forum id
1039 update_post_meta( $forum_id, '_bbp_reply_count', (int) $topic_count + (int) $difference );
1040 update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_reply_count + (int) $difference );
1041
1042 // Check for ancestors
1043 if ( true === $update_ancestors ) {
1044
1045 // Get post ancestors
1046 $forum = get_post( $forum_id );
1047 $ancestors = get_post_ancestors( $forum );
1048
1049 // If has ancestors, loop through them...
1050 if ( !empty( $ancestors ) ) {
1051 foreach ( (array) $ancestors as $parent_forum_id ) {
1052
1053 // Get forum counts
1054 $parent_topic_count = bbp_get_forum_reply_count( $parent_forum_id, false );
1055 $parent_total_reply_count = bbp_get_forum_reply_count( $parent_forum_id, true );
1056
1057 // Update counts
1058 update_post_meta( $parent_forum_id, '_bbp_reply_count', (int) $parent_topic_count + (int) $difference );
1059 update_post_meta( $parent_forum_id, '_bbp_total_reply_count', (int) $parent_total_reply_count + (int) $difference );
1060 }
1061 }
1062 }
1063
1064 return (int) apply_filters( 'bbp_bump_forum_reply_count', (int) $total_reply_count + (int) $difference, $forum_id, (int) $difference, (bool) $update_ancestors );
1065 }
1066
1067 /** Forum Updaters ************************************************************/
1068
1069 /**
1070 * Update the forum last topic id
1071 *
1072 * @since bbPress (r2625)
1073 *
1074 * @param int $forum_id Optional. Forum id
1075 * @param int $topic_id Optional. Topic id
1076 * @uses bbp_get_forum_id() To get the forum id
1077 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
1078 * @uses bbp_update_forum_last_topic_id() To update the last topic id of child
1079 * forums
1080 * @uses get_posts() To get the most recent topic in the forum
1081 * @uses update_post_meta() To update the forum's last active id meta
1082 * @uses apply_filters() Calls 'bbp_update_forum_last_topic_id' with the last
1083 * reply id and forum id
1084 * @return bool True on success, false on failure
1085 */
1086 function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) {
1087 $forum_id = bbp_get_forum_id( $forum_id );
1088
1089 // Define local variable(s)
1090 $children_last_topic = 0;
1091
1092 // Do some calculation if not manually set
1093 if ( empty( $topic_id ) ) {
1094
1095 // Loop through children and add together forum reply counts
1096 $children = bbp_forum_query_subforum_ids( $forum_id );
1097 if ( !empty( $children ) ) {
1098 foreach ( (array) $children as $child ) {
1099 $children_last_topic = bbp_update_forum_last_topic_id( $child ); // Recursive
1100 }
1101 }
1102
1103 // Setup recent topic query vars
1104 $post_vars = array(
1105 'post_parent' => $forum_id,
1106 'post_type' => bbp_get_topic_post_type(),
1107 'meta_key' => '_bbp_last_active_time',
1108 'orderby' => 'meta_value',
1109 'numberposts' => 1
1110 );
1111
1112 // Get the most recent topic in this forum_id
1113 $recent_topic = get_posts( $post_vars );
1114 if ( !empty( $recent_topic ) ) {
1115 $topic_id = $recent_topic[0]->ID;
1116 }
1117 }
1118
1119 // Cast as integer in case of empty or string
1120 $topic_id = (int) $topic_id;
1121 $children_last_topic = (int) $children_last_topic;
1122
1123 // If child forums have higher id, use that instead
1124 if ( !empty( $children ) && ( $children_last_topic > $topic_id ) )
1125 $topic_id = $children_last_topic;
1126
1127 // Update the last public topic ID
1128 if ( bbp_is_topic_published( $topic_id ) )
1129 update_post_meta( $forum_id, '_bbp_last_topic_id', $topic_id );
1130
1131 return (int) apply_filters( 'bbp_update_forum_last_topic_id', $topic_id, $forum_id );
1132 }
1133
1134 /**
1135 * Update the forum last reply id
1136 *
1137 * @since bbPress (r2625)
1138 *
1139 * @param int $forum_id Optional. Forum id
1140 * @param int $reply_id Optional. Reply id
1141 * @uses bbp_get_forum_id() To get the forum id
1142 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
1143 * @uses bbp_update_forum_last_reply_id() To update the last reply id of child
1144 * forums
1145 * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
1146 * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
1147 * @uses bbp_is_reply_published() To make sure the reply is published
1148 * @uses update_post_meta() To update the forum's last active id meta
1149 * @uses apply_filters() Calls 'bbp_update_forum_last_reply_id' with the last
1150 * reply id and forum id
1151 * @return bool True on success, false on failure
1152 */
1153 function bbp_update_forum_last_reply_id( $forum_id = 0, $reply_id = 0 ) {
1154 $forum_id = bbp_get_forum_id( $forum_id );
1155
1156 // Define local variable(s)
1157 $children_last_reply = 0;
1158
1159 // Do some calculation if not manually set
1160 if ( empty( $reply_id ) ) {
1161
1162 // Loop through children and get the most recent reply id
1163 $children = bbp_forum_query_subforum_ids( $forum_id );
1164 if ( !empty( $children ) ) {
1165 foreach ( (array) $children as $child ) {
1166 $children_last_reply = bbp_update_forum_last_reply_id( $child ); // Recursive
1167 }
1168 }
1169
1170 // If this forum has topics...
1171 $topic_ids = bbp_forum_query_topic_ids( $forum_id );
1172 if ( !empty( $topic_ids ) ) {
1173
1174 // ...get the most recent reply from those topics...
1175 $reply_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids );
1176
1177 // ...and compare it to the most recent topic id...
1178 $reply_id = ( $reply_id > max( $topic_ids ) ) ? $reply_id : max( $topic_ids );
1179 }
1180 }
1181
1182 // Cast as integer in case of empty or string
1183 $reply_id = (int) $reply_id;
1184 $children_last_reply = (int) $children_last_reply;
1185
1186 // If child forums have higher ID, check for newer reply id
1187 if ( !empty( $children ) && ( $children_last_reply > $reply_id ) )
1188 $reply_id = $children_last_reply;
1189
1190 // Update the last public reply ID
1191 if ( bbp_is_reply_published( $reply_id ) )
1192 update_post_meta( $forum_id, '_bbp_last_reply_id', $reply_id );
1193
1194 return (int) apply_filters( 'bbp_update_forum_last_reply_id', $reply_id, $forum_id );
1195 }
1196
1197 /**
1198 * Update the forum last active post id
1199 *
1200 * @since bbPress (r2860)
1201 *
1202 * @param int $forum_id Optional. Forum id
1203 * @param int $active_id Optional. Active post id
1204 * @uses bbp_get_forum_id() To get the forum id
1205 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
1206 * @uses bbp_update_forum_last_active_id() To update the last active id of
1207 * child forums
1208 * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
1209 * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
1210 * @uses get_post_status() To make sure the reply is published
1211 * @uses update_post_meta() To update the forum's last active id meta
1212 * @uses apply_filters() Calls 'bbp_update_forum_last_active_id' with the last
1213 * active post id and forum id
1214 * @return bool True on success, false on failure
1215 */
1216 function bbp_update_forum_last_active_id( $forum_id = 0, $active_id = 0 ) {
1217
1218 $forum_id = bbp_get_forum_id( $forum_id );
1219
1220 // Define local variable(s)
1221 $children_last_active = 0;
1222
1223 // Do some calculation if not manually set
1224 if ( empty( $active_id ) ) {
1225
1226 // Loop through children and add together forum reply counts
1227 $children = bbp_forum_query_subforum_ids( $forum_id );
1228 if ( !empty( $children ) ) {
1229 foreach ( (array) $children as $child ) {
1230 $children_last_active = bbp_update_forum_last_active_id( $child, $active_id );
1231 }
1232 }
1233
1234 // Don't count replies if the forum is a category
1235 $topic_ids = bbp_forum_query_topic_ids( $forum_id );
1236 if ( !empty( $topic_ids ) ) {
1237 $active_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids );
1238 $active_id = $active_id > max( $topic_ids ) ? $active_id : max( $topic_ids );
1239
1240 // Forum has no topics
1241 } else {
1242 $active_id = 0;
1243 }
1244 }
1245
1246 // Cast as integer in case of empty or string
1247 $active_id = (int) $active_id;
1248 $children_last_active = (int) $children_last_active;
1249
1250 // If child forums have higher id, use that instead
1251 if ( !empty( $children ) && ( $children_last_active > $active_id ) )
1252 $active_id = $children_last_active;
1253
1254 // Update only if published
1255 if ( bbp_get_public_status_id() == get_post_status( $active_id ) )
1256 update_post_meta( $forum_id, '_bbp_last_active_id', (int) $active_id );
1257
1258 return (int) apply_filters( 'bbp_update_forum_last_active_id', (int) $active_id, $forum_id );
1259 }
1260
1261 /**
1262 * Update the forums last active date/time (aka freshness)
1263 *
1264 * @since bbPress (r2680)
1265 *
1266 * @param int $forum_id Optional. Topic id
1267 * @param string $new_time Optional. New time in mysql format
1268 * @uses bbp_get_forum_id() To get the forum id
1269 * @uses bbp_get_forum_last_active_id() To get the forum's last post id
1270 * @uses get_post_field() To get the post date of the forum's last post
1271 * @uses update_post_meta() To update the forum last active time
1272 * @uses apply_filters() Calls 'bbp_update_forum_last_active' with the new time
1273 * and forum id
1274 * @return bool True on success, false on failure
1275 */
1276 function bbp_update_forum_last_active_time( $forum_id = 0, $new_time = '' ) {
1277 $forum_id = bbp_get_forum_id( $forum_id );
1278
1279 // Check time and use current if empty
1280 if ( empty( $new_time ) )
1281 $new_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $forum_id ) );
1282
1283 // Update only if there is a time
1284 if ( !empty( $new_time ) )
1285 update_post_meta( $forum_id, '_bbp_last_active_time', $new_time );
1286
1287 return (int) apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id );
1288 }
1289
1290 /**
1291 * Update the forum sub-forum count
1292 *
1293 * @since bbPress (r2625)
1294 *
1295 * @param int $forum_id Optional. Forum id
1296 * @uses bbp_get_forum_id() To get the forum id
1297 * @return bool True on success, false on failure
1298 */
1299 function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = 0 ) {
1300 $forum_id = bbp_get_forum_id( $forum_id );
1301
1302 if ( empty( $subforums ) )
1303 $subforums = count( bbp_forum_query_subforum_ids( $forum_id ) );
1304
1305 update_post_meta( $forum_id, '_bbp_forum_subforum_count', (int) $subforums );
1306
1307 return (int) apply_filters( 'bbp_update_forum_subforum_count', (int) $subforums, $forum_id );
1308 }
1309
1310 /**
1311 * Adjust the total topic count of a forum
1312 *
1313 * @since bbPress (r2464)
1314 *
1315 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it
1316 * is a topic or a forum. If it's a topic, its parent,
1317 * i.e. the forum is automatically retrieved.
1318 * @param bool $total_count Optional. To return the total count or normal
1319 * count?
1320 * @uses bbp_get_forum_id() To get the forum id
1321 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
1322 * @uses bbp_update_forum_topic_count() To update the forum topic count
1323 * @uses bbp_forum_query_topic_ids() To get the forum topic ids
1324 * @uses update_post_meta() To update the forum's topic count meta
1325 * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the topic
1326 * count and forum id
1327 * @return int Forum topic count
1328 */
1329 function bbp_update_forum_topic_count( $forum_id = 0 ) {
1330 $forum_id = bbp_get_forum_id( $forum_id );
1331 $children_topic_count = 0;
1332
1333 // Loop through subforums and add together forum topic counts
1334 $children = bbp_forum_query_subforum_ids( $forum_id );
1335 if ( !empty( $children ) ) {
1336 foreach ( (array) $children as $child ) {
1337 $children_topic_count += bbp_update_forum_topic_count( $child ); // Recursive
1338 }
1339 }
1340
1341 // Get total topics for this forum
1342 $topics = (int) count( bbp_forum_query_topic_ids( $forum_id ) );
1343
1344 // Calculate total topics in this forum
1345 $total_topics = $topics + $children_topic_count;
1346
1347 // Update the count
1348 update_post_meta( $forum_id, '_bbp_topic_count', (int) $topics );
1349 update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topics );
1350
1351 return (int) apply_filters( 'bbp_update_forum_topic_count', (int) $total_topics, $forum_id );
1352 }
1353
1354 /**
1355 * Adjust the total hidden topic count of a forum (hidden includes trashed and spammed topics)
1356 *
1357 * @since bbPress (r2888)
1358 *
1359 * @param int $forum_id Optional. Topic id to update
1360 * @param int $topic_count Optional. Set the topic count manually
1361 * @uses bbp_is_topic() To check if the supplied id is a topic
1362 * @uses bbp_get_topic_id() To get the topic id
1363 * @uses bbp_get_topic_forum_id() To get the topic forum id
1364 * @uses bbp_get_forum_id() To get the forum id
1365 * @uses wpdb::prepare() To prepare our sql query
1366 * @uses wpdb::get_col() To execute our query and get the column back
1367 * @uses update_post_meta() To update the forum hidden topic count meta
1368 * @uses apply_filters() Calls 'bbp_update_forum_topic_count_hidden' with the
1369 * hidden topic count and forum id
1370 * @return int Topic hidden topic count
1371 */
1372 function bbp_update_forum_topic_count_hidden( $forum_id = 0, $topic_count = 0 ) {
1373 global $wpdb;
1374
1375 // If topic_id was passed as $forum_id, then get its forum
1376 if ( bbp_is_topic( $forum_id ) ) {
1377 $topic_id = bbp_get_topic_id( $forum_id );
1378 $forum_id = bbp_get_topic_forum_id( $topic_id );
1379
1380 // $forum_id is not a topic_id, so validate and proceed
1381 } else {
1382 $forum_id = bbp_get_forum_id( $forum_id );
1383 }
1384
1385 // Can't update what isn't there
1386 if ( !empty( $forum_id ) ) {
1387
1388 // Get topics of forum
1389 if ( empty( $topic_count ) )
1390 $topic_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';", $forum_id, bbp_get_topic_post_type() ) );
1391
1392 // Update the count
1393 update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count );
1394 }
1395
1396 return (int) apply_filters( 'bbp_update_forum_topic_count_hidden', (int) $topic_count, $forum_id );
1397 }
1398
1399 /**
1400 * Adjust the total reply count of a forum
1401 *
1402 * @since bbPress (r2464)
1403 *
1404 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it
1405 * is a topic or a forum. If it's a topic, its parent,
1406 * i.e. the forum is automatically retrieved.
1407 * @param bool $total_count Optional. To return the total count or normal
1408 * count?
1409 * @uses bbp_get_forum_id() To get the forum id
1410 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
1411 * @uses bbp_update_forum_reply_count() To update the forum reply count
1412 * @uses bbp_forum_query_topic_ids() To get the forum topic ids
1413 * @uses wpdb::prepare() To prepare the sql statement
1414 * @uses wpdb::get_var() To execute the query and get the var back
1415 * @uses update_post_meta() To update the forum's reply count meta
1416 * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the reply
1417 * count and forum id
1418 * @return int Forum reply count
1419 */
1420 function bbp_update_forum_reply_count( $forum_id = 0 ) {
1421 global $wpdb;
1422
1423 $forum_id = bbp_get_forum_id( $forum_id );
1424 $children_reply_count = 0;
1425
1426 // Loop through children and add together forum reply counts
1427 $children = bbp_forum_query_subforum_ids( $forum_id );
1428 if ( !empty( $children ) ) {
1429 foreach ( (array) $children as $child ) {
1430 $children_reply_count += bbp_update_forum_reply_count( $child );
1431 }
1432 }
1433
1434 // Don't count replies if the forum is a category
1435 $topic_ids = bbp_forum_query_topic_ids( $forum_id );
1436 if ( !empty( $topic_ids ) )
1437 $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s';", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
1438 else
1439 $reply_count = 0;
1440
1441 // Calculate total replies in this forum
1442 $total_replies = (int) $reply_count + $children_reply_count;
1443
1444 // Update the count
1445 update_post_meta( $forum_id, '_bbp_reply_count', (int) $reply_count );
1446 update_post_meta( $forum_id, '_bbp_total_reply_count', (int) $total_replies );
1447
1448 return (int) apply_filters( 'bbp_update_forum_reply_count', (int) $total_replies, $forum_id );
1449 }
1450
1451 /**
1452 * Updates the counts of a forum.
1453 *
1454 * This calls a few internal functions that all run manual queries against the
1455 * database to get their results. As such, this function can be costly to run
1456 * but is necessary to keep everything accurate.
1457 *
1458 * @since bbPress (r2908)
1459 *
1460 * @param mixed $args Supports these arguments:
1461 * - forum_id: Forum id
1462 * - last_topic_id: Last topic id
1463 * - last_reply_id: Last reply id
1464 * - last_active_id: Last active post id
1465 * - last_active_time: last active time
1466 * @uses bbp_update_forum_last_topic_id() To update the forum last topic id
1467 * @uses bbp_update_forum_last_reply_id() To update the forum last reply id
1468 * @uses bbp_update_forum_last_active_id() To update the last active post id
1469 * @uses get_post_field() To get the post date of the last active id
1470 * @uses bbp_update_forum_last_active_time() To update the last active time
1471 * @uses bbp_update_forum_subforum_count() To update the subforum count
1472 * @uses bbp_update_forum_topic_count() To update the forum topic count
1473 * @uses bbp_update_forum_reply_count() To update the forum reply count
1474 * @uses bbp_update_forum_topic_count_hidden() To update the hidden topic count
1475 */
1476 function bbp_update_forum( $args = '' ) {
1477 $defaults = array(
1478 'forum_id' => 0,
1479 'post_parent' => 0,
1480 'last_topic_id' => 0,
1481 'last_reply_id' => 0,
1482 'last_active_id' => 0,
1483 'last_active_time' => 0,
1484 'last_active_status' => bbp_get_public_status_id()
1485 );
1486 $r = bbp_parse_args( $args, $defaults, 'update_forum' );
1487 extract( $r );
1488
1489 // Last topic and reply ID's
1490 bbp_update_forum_last_topic_id( $forum_id, $last_topic_id );
1491 bbp_update_forum_last_reply_id( $forum_id, $last_reply_id );
1492
1493 // Active dance
1494 $last_active_id = bbp_update_forum_last_active_id( $forum_id, $last_active_id );
1495
1496 // If no active time was passed, get it from the last_active_id
1497 if ( empty( $last_active_time ) )
1498 $last_active_time = get_post_field( 'post_date', $last_active_id );
1499
1500 if ( bbp_get_public_status_id() == $last_active_status ) {
1501 bbp_update_forum_last_active_time( $forum_id, $last_active_time );
1502 }
1503
1504 // Counts
1505 bbp_update_forum_subforum_count ( $forum_id );
1506 bbp_update_forum_reply_count ( $forum_id );
1507 bbp_update_forum_topic_count ( $forum_id );
1508 bbp_update_forum_topic_count_hidden( $forum_id );
1509
1510 // Update the parent forum if one was passed
1511 if ( !empty( $post_parent ) && is_numeric( $post_parent ) ) {
1512 bbp_update_forum( array(
1513 'forum_id' => $post_parent,
1514 'post_parent' => get_post_field( 'post_parent', $post_parent )
1515 ) );
1516 }
1517 }
1518
1519 /** Queries *******************************************************************/
1520
1521 /**
1522 * Returns the hidden forum ids
1523 *
1524 * Only hidden forum ids are returned. Public and private ids are not.
1525 *
1526 * @since bbPress (r3007)
1527 *
1528 * @uses get_option() Returns the unserialized array of hidden forum ids
1529 * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
1530 * and forum id
1531 */
1532 function bbp_get_hidden_forum_ids() {
1533 $forum_ids = get_option( '_bbp_hidden_forums', array() );
1534
1535 return apply_filters( 'bbp_get_hidden_forum_ids', (array) $forum_ids );
1536 }
1537
1538 /**
1539 * Returns the private forum ids
1540 *
1541 * Only private forum ids are returned. Public and hidden ids are not.
1542 *
1543 * @since bbPress (r3007)
1544 *
1545 * @uses get_option() Returns the unserialized array of private forum ids
1546 * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
1547 * and forum id
1548 */
1549 function bbp_get_private_forum_ids() {
1550 $forum_ids = get_option( '_bbp_private_forums', array() );
1551
1552 return apply_filters( 'bbp_get_private_forum_ids', (array) $forum_ids );
1553 }
1554
1555 /**
1556 * Returns a meta_query that either includes or excludes hidden forum IDs
1557 * from a query.
1558 *
1559 * @since bbPress (r3291)
1560 *
1561 * @param string Optional. The type of value to return. (string|array|meta_query)
1562 *
1563 * @uses is_super_admin()
1564 * @uses bbp_get_hidden_forum_ids()
1565 * @uses bbp_get_private_forum_ids()
1566 * @uses apply_filters()
1567 */
1568 function bbp_exclude_forum_ids( $type = 'string' ) {
1569
1570 // Setup arrays
1571 $retval = $private = $hidden = $meta_query = $forum_ids = array();
1572
1573 // Exclude for everyone but super admins
1574 if ( !is_super_admin() ) {
1575
1576 // Private forums
1577 if ( !current_user_can( 'read_private_forums' ) )
1578 $private = bbp_get_private_forum_ids();
1579
1580 // Hidden forums
1581 if ( !current_user_can( 'read_hidden_forums' ) )
1582 $hidden = bbp_get_hidden_forum_ids();
1583
1584 // Merge private and hidden forums together
1585 $forum_ids = (array) array_filter( array_merge( $private, $hidden ) );
1586
1587 // There are forums that need to be excluded
1588 if ( !empty( $forum_ids ) ) {
1589
1590 switch ( $type ) {
1591
1592 // Separate forum ID's into a comma separated string
1593 case 'string' :
1594 $retval = implode( ',', $forum_ids );
1595 break;
1596
1597 // Use forum_ids array
1598 case 'array' :
1599 $retval = $forum_ids;
1600 break;
1601
1602 // Build a meta_query
1603 case 'meta_query' :
1604 $retval = array(
1605 'key' => '_bbp_forum_id',
1606 'value' => implode( ',', $forum_ids ),
1607 'compare' => ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!='
1608 );
1609 break;
1610 }
1611 }
1612 }
1613
1614 // Filter and return the results
1615 return apply_filters( 'bbp_exclude_forum_ids', $retval, $forum_ids, $type );
1616 }
1617
1618 /**
1619 * Adjusts topic and reply queries to exclude items that might be contained
1620 * inside hidden or private forums that the user does not have the capability
1621 * to view.
1622 *
1623 * @since bbPress (r3291)
1624 *
1625 * @param WP_Query $posts_query
1626 *
1627 * @uses apply_filters()
1628 * @uses bbp_exclude_forum_ids()
1629 * @uses bbp_get_topic_post_type()
1630 * @uses bbp_get_reply_post_type()
1631
1632 * @return WP_Query
1633 */
1634 function bbp_pre_get_posts_exclude_forums( $posts_query ) {
1635
1636 // Bail if all forums are explicitly allowed
1637 if ( true === apply_filters( 'bbp_include_all_forums', $posts_query ) )
1638 return;
1639
1640 // Bail if $posts_query is not an object or of incorrect class
1641 if ( !is_object( $posts_query ) || !is_a( $posts_query, 'WP_Query' ) )
1642 return;
1643
1644 // Bail if filters are suppressed on this query
1645 if ( true == $posts_query->get( 'suppress_filters' ) )
1646 return;
1647
1648 // Only exclude forums on bbPress queries
1649 switch ( $posts_query->get( 'post_type' ) ) {
1650
1651 // Forums
1652 case bbp_get_forum_post_type() :
1653
1654 // Prevent accidental wp-admin post_row override
1655 if ( is_admin() && isset( $_REQUEST['post_status'] ) )
1656 break;
1657
1658 // Define local variable
1659 $status = array();
1660
1661 // All users can see published forums
1662 $status[] = bbp_get_public_status_id();
1663
1664 // Add bbp_get_private_status_id() if user is capable
1665 if ( current_user_can( 'read_private_forums' ) ) {
1666 $status[] = bbp_get_private_status_id();
1667 }
1668
1669 // Add bbp_get_hidden_status_id() if user is capable
1670 if ( current_user_can( 'read_hidden_forums' ) ) {
1671 $status[] = bbp_get_hidden_status_id();
1672 }
1673
1674 // Implode and add the statuses
1675 $posts_query->set( 'post_status', implode( ',', $status ) );
1676
1677 break;
1678
1679 // Topics
1680 case bbp_get_topic_post_type() :
1681
1682 // Replies
1683 case bbp_get_reply_post_type() :
1684
1685 // Topics and replies
1686 case array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) :
1687
1688 // Get forums to exclude
1689 $forum_ids = bbp_exclude_forum_ids( 'meta_query' );
1690
1691 // Bail if no forums to exclude
1692 if ( empty( $forum_ids ) )
1693 return;
1694
1695 // Get any existing meta queries
1696 $meta_query = $posts_query->get( 'meta_query' );
1697
1698 // Add our meta query to existing
1699 $meta_query[] = $forum_ids;
1700
1701 // Set the meta_query var
1702 $posts_query->set( 'meta_query', $meta_query );
1703
1704 break;
1705 }
1706 }
1707
1708 /**
1709 * Returns the forum's topic ids
1710 *
1711 * Only topics with published and closed statuses are returned
1712 *
1713 * @since bbPress (r2908)
1714 *
1715 * @param int $forum_id Forum id
1716 * @uses bbp_get_topic_post_type() To get the topic post type
1717 * @uses bbp_get_public_child_ids() To get the topic ids
1718 * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
1719 * and forum id
1720 */
1721 function bbp_forum_query_topic_ids( $forum_id ) {
1722 $topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() );
1723
1724 return apply_filters( 'bbp_forum_query_topic_ids', $topic_ids, $forum_id );
1725 }
1726
1727 /**
1728 * Returns the forum's subforum ids
1729 *
1730 * Only forums with published status are returned
1731 *
1732 * @since bbPress (r2908)
1733 *
1734 * @param int $forum_id Forum id
1735 * @uses bbp_get_forum_post_type() To get the forum post type
1736 * @uses bbp_get_public_child_ids() To get the forum ids
1737 * @uses apply_filters() Calls 'bbp_forum_query_subforum_ids' with the subforum
1738 * ids and forum id
1739 */
1740 function bbp_forum_query_subforum_ids( $forum_id ) {
1741 $subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() );
1742 //usort( $subforum_ids, '_bbp_forum_query_usort_subforum_ids' );
1743
1744 return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id );
1745 }
1746
1747 /**
1748 * Callback to sort forum ID's based on last active time
1749 *
1750 * @since bbPress (r3789)
1751 * @param int $a First forum ID to compare
1752 * @param int $b Second forum ID to compare
1753 * @return Position change based on sort
1754 */
1755 function _bbp_forum_query_usort_subforum_ids( $a = 0, $b = 0 ) {
1756 $ta = get_post_meta( $a, '_bbp_last_active_time', true );
1757 $tb = get_post_meta( $b, '_bbp_last_active_time', true );
1758 return ( $ta < $tb ) ? -1 : 1;
1759 }
1760
1761 /**
1762 * Returns the forum's last reply id
1763 *
1764 * @since bbPress (r2908)
1765 *
1766 * @param int $forum_id Forum id
1767 * @param int $topic_ids Optional. Topic ids
1768 * @uses wp_cache_get() To check for cache and retrieve it
1769 * @uses bbp_forum_query_topic_ids() To get the forum's topic ids
1770 * @uses wpdb::prepare() To prepare the query
1771 * @uses wpdb::get_var() To execute the query and get the var back
1772 * @uses bbp_get_reply_post_type() To get the reply post type
1773 * @uses wp_cache_set() To set the cache for future use
1774 * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id
1775 * and forum id
1776 */
1777 function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) {
1778 global $wpdb;
1779
1780 $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
1781 $reply_id = (int) wp_cache_get( $cache_id, 'bbpress' );
1782
1783 if ( empty( $reply_id ) ) {
1784
1785 if ( empty( $topic_ids ) ) {
1786 $topic_ids = bbp_forum_query_topic_ids( $forum_id );
1787 }
1788
1789 if ( !empty( $topic_ids ) ) {
1790 $reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = '%s' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_public_status_id(), bbp_get_reply_post_type() ) );
1791 wp_cache_set( $cache_id, $reply_id, 'bbpress' ); // May be (int) 0
1792 } else {
1793 wp_cache_set( $cache_id, '0', 'bbpress' );
1794 }
1795 }
1796
1797 return (int) apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );
1798 }
1799
1800 /** Listeners *****************************************************************/
1801
1802 /**
1803 * Check if it's a hidden forum or a topic or reply of a hidden forum and if
1804 * the user can't view it, then sets a 404
1805 *
1806 * @since bbPress (r2996)
1807 *
1808 * @uses current_user_can() To check if the current user can read private forums
1809 * @uses is_singular() To check if it's a singular page
1810 * @uses bbp_get_forum_post_type() To get the forum post type
1811 * @uses bbp_get_topic_post_type() To get the topic post type
1812 * @uses bbp_get_reply_post_type() TO get the reply post type
1813 * @uses bbp_get_topic_forum_id() To get the topic forum id
1814 * @uses bbp_get_reply_forum_id() To get the reply forum id
1815 * @uses bbp_is_forum_hidden() To check if the forum is hidden or not
1816 * @uses bbp_set_404() To set a 404 status
1817 */
1818 function bbp_forum_enforce_hidden() {
1819
1820 // Bail if not viewing a single item or if user has caps
1821 if ( !is_singular() || is_super_admin() || current_user_can( 'read_hidden_forums' ) )
1822 return;
1823
1824 global $wp_query;
1825
1826 // Define local variable
1827 $forum_id = 0;
1828
1829 // Check post type
1830 switch ( $wp_query->get( 'post_type' ) ) {
1831
1832 // Forum
1833 case bbp_get_forum_post_type() :
1834 $forum_id = bbp_get_forum_id( $wp_query->post->ID );
1835 break;
1836
1837 // Topic
1838 case bbp_get_topic_post_type() :
1839 $forum_id = bbp_get_topic_forum_id( $wp_query->post->ID );
1840 break;
1841
1842 // Reply
1843 case bbp_get_reply_post_type() :
1844 $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID );
1845 break;
1846
1847 }
1848
1849 // If forum is explicitly hidden and user not capable, set 404
1850 if ( !empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
1851 bbp_set_404();
1852 }
1853
1854 /**
1855 * Check if it's a private forum or a topic or reply of a private forum and if
1856 * the user can't view it, then sets a 404
1857 *
1858 * @since bbPress (r2996)
1859 *
1860 * @uses current_user_can() To check if the current user can read private forums
1861 * @uses is_singular() To check if it's a singular page
1862 * @uses bbp_get_forum_post_type() To get the forum post type
1863 * @uses bbp_get_topic_post_type() To get the topic post type
1864 * @uses bbp_get_reply_post_type() TO get the reply post type
1865 * @uses bbp_get_topic_forum_id() To get the topic forum id
1866 * @uses bbp_get_reply_forum_id() To get the reply forum id
1867 * @uses bbp_is_forum_private() To check if the forum is private or not
1868 * @uses bbp_set_404() To set a 404 status
1869 */
1870 function bbp_forum_enforce_private() {
1871
1872 // Bail if not viewing a single item or if user has caps
1873 if ( !is_singular() || is_super_admin() || current_user_can( 'read_private_forums' ) )
1874 return;
1875
1876 global $wp_query;
1877
1878 // Define local variable
1879 $forum_id = 0;
1880
1881 // Check post type
1882 switch ( $wp_query->get( 'post_type' ) ) {
1883
1884 // Forum
1885 case bbp_get_forum_post_type() :
1886 $forum_id = bbp_get_forum_id( $wp_query->post->ID );
1887 break;
1888
1889 // Topic
1890 case bbp_get_topic_post_type() :
1891 $forum_id = bbp_get_topic_forum_id( $wp_query->post->ID );
1892 break;
1893
1894 // Reply
1895 case bbp_get_reply_post_type() :
1896 $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID );
1897 break;
1898
1899 }
1900
1901 // If forum is explicitly hidden and user not capable, set 404
1902 if ( !empty( $forum_id ) && bbp_is_forum_private( $forum_id ) && !current_user_can( 'read_private_forums' ) )
1903 bbp_set_404();
1904 }
1905
1906 /** Permissions ***************************************************************/
1907
1908 /**
1909 * Redirect if unathorized user is attempting to edit a forum
1910 *
1911 * @since bbPress (r3607)
1912 *
1913 * @uses bbp_is_forum_edit()
1914 * @uses current_user_can()
1915 * @uses bbp_get_forum_id()
1916 * @uses wp_safe_redirect()
1917 * @uses bbp_get_forum_permalink()
1918 */
1919 function bbp_check_forum_edit() {
1920
1921 // Bail if not editing a topic
1922 if ( !bbp_is_forum_edit() )
1923 return;
1924
1925 // User cannot edit topic, so redirect back to reply
1926 if ( !current_user_can( 'edit_forum', bbp_get_forum_id() ) ) {
1927 wp_safe_redirect( bbp_get_forum_permalink() );
1928 exit();
1929 }
1930 }
1931
1932 /**
1933 * Delete all topics (and their replies) for a specific forum ID
1934 *
1935 * @since bbPress (r3668)
1936 *
1937 * @param int $forum_id
1938 * @uses bbp_get_forum_id() To validate the forum ID
1939 * @uses bbp_is_forum() To make sure it's a forum
1940 * @uses bbp_get_topic_post_type() To get the topic post type
1941 * @uses bbp_has_topics() To get the topics
1942 * @uses bbp_topics() To make sure there are topics to loop through
1943 * @uses wp_trash_post() To trash the post
1944 * @uses update_post_meta() To update the forum meta of trashed topics
1945 * @return If forum is not valid
1946 */
1947 function bbp_delete_forum_topics( $forum_id = 0 ) {
1948
1949 // Validate forum ID
1950 $forum_id = bbp_get_forum_id( $forum_id );
1951
1952 if ( empty( $forum_id ) )
1953 return;
1954
1955 // Forum is being permanently deleted, so its topics gotta go too
1956 if ( bbp_has_topics( array(
1957 'post_type' => bbp_get_topic_post_type(),
1958 'post_status' => 'any',
1959 'posts_per_page' => -1,
1960 'meta_query' => array( array(
1961 'key' => '_bbp_forum_id',
1962 'value' => $forum_id,
1963 'compare' => '='
1964 ) )
1965 ) ) ) {
1966 while ( bbp_topics() ) {
1967 bbp_the_topic();
1968 wp_delete_post( bbpress()->topic_query->post->ID, true );
1969 }
1970 }
1971 }
1972
1973 /**
1974 * Trash all topics inside a forum
1975 *
1976 * @since bbPress (r3668)
1977 *
1978 * @param int $forum_id
1979 * @uses bbp_get_forum_id() To validate the forum ID
1980 * @uses bbp_is_forum() To make sure it's a forum
1981 * @uses bbp_get_public_status_id() To return public post status
1982 * @uses bbp_get_closed_status_id() To return closed post status
1983 * @uses bbp_get_pending_status_id() To return pending post status
1984 * @uses bbp_get_topic_post_type() To get the topic post type
1985 * @uses bbp_has_topics() To get the topics
1986 * @uses bbp_topics() To make sure there are topics to loop through
1987 * @uses wp_trash_post() To trash the post
1988 * @uses update_post_meta() To update the forum meta of trashed topics
1989 * @return If forum is not valid
1990 */
1991 function bbp_trash_forum_topics( $forum_id = 0 ) {
1992
1993 // Validate forum ID
1994 $forum_id = bbp_get_forum_id( $forum_id );
1995
1996 if ( empty( $forum_id ) )
1997 return;
1998
1999 // Allowed post statuses to pre-trash
2000 $post_stati = join( ',', array(
2001 bbp_get_public_status_id(),
2002 bbp_get_closed_status_id(),
2003 bbp_get_pending_status_id()
2004 ) );
2005
2006 // Forum is being trashed, so its topics are trashed too
2007 if ( bbp_has_topics( array(
2008 'post_type' => bbp_get_topic_post_type(),
2009 'post_status' => $post_stati,
2010 'posts_per_page' => -1,
2011 'meta_query' => array( array(
2012 'key' => '_bbp_forum_id',
2013 'value' => $forum_id,
2014 'compare' => '='
2015 ) )
2016 ) ) ) {
2017
2018 // Prevent debug notices
2019 $pre_trashed_topics = array();
2020 $bbp = bbpress();
2021
2022 // Loop through topics, trash them, and add them to array
2023 while ( bbp_topics() ) {
2024 bbp_the_topic();
2025 wp_trash_post( $bbp->topic_query->post->ID );
2026 $pre_trashed_topics[] = $bbp->topic_query->post->ID;
2027 }
2028
2029 // Set a post_meta entry of the topics that were trashed by this action.
2030 // This is so we can possibly untrash them, without untrashing topics
2031 // that were purposefully trashed before.
2032 update_post_meta( $forum_id, '_bbp_pre_trashed_topics', $pre_trashed_topics );
2033 }
2034 }
2035
2036 /**
2037 * Trash all topics inside a forum
2038 *
2039 * @since bbPress (r3668)
2040 *
2041 * @param int $forum_id
2042 * @uses bbp_get_forum_id() To validate the forum ID
2043 * @uses bbp_is_forum() To make sure it's a forum
2044 * @uses get_post_meta() To update the forum meta of trashed topics
2045 * @uses wp_untrash_post() To trash the post
2046 * @return If forum is not valid
2047 */
2048 function bbp_untrash_forum_topics( $forum_id = 0 ) {
2049
2050 // Validate forum ID
2051 $forum_id = bbp_get_forum_id( $forum_id );
2052
2053 if ( empty( $forum_id ) )
2054 return;
2055
2056 // Get the topics that were not previously trashed
2057 $pre_trashed_topics = get_post_meta( $forum_id, '_bbp_pre_trashed_topics', true );
2058
2059 // There are topics to untrash
2060 if ( !empty( $pre_trashed_topics ) ) {
2061
2062 // Maybe reverse the trashed topics array
2063 if ( is_array( $pre_trashed_topics ) )
2064 $pre_trashed_topics = array_reverse( $pre_trashed_topics );
2065
2066 // Loop through topics
2067 foreach ( (array) $pre_trashed_topics as $topic ) {
2068 wp_untrash_post( $topic );
2069 }
2070 }
2071 }
2072
2073 /** Before Delete/Trash/Untrash ***********************************************/
2074
2075 /**
2076 * Called before deleting a forum.
2077 *
2078 * This function is supplemental to the actual forum deletion which is
2079 * handled by WordPress core API functions. It is used to clean up after
2080 * a forum that is being deleted.
2081 *
2082 * @since bbPress (r3668)
2083 * @uses bbp_get_forum_id() To get the forum id
2084 * @uses bbp_is_forum() To check if the passed id is a forum
2085 * @uses do_action() Calls 'bbp_delete_forum' with the forum id
2086 * @uses bbp_has_topics() To check if the forum has topics
2087 * @uses bbp_topics() To loop through the topics
2088 * @uses bbp_the_topic() To set a topic as the current topic in the loop
2089 * @uses bbp_get_topic_id() To get the topic id
2090 * @uses wp_delete_post() To delete the topic
2091 */
2092 function bbp_delete_forum( $forum_id = 0 ) {
2093 $forum_id = bbp_get_forum_id( $forum_id );
2094
2095 if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
2096 return false;
2097
2098 do_action( 'bbp_delete_forum', $forum_id );
2099 }
2100
2101 /**
2102 * Called before trashing a forum
2103 *
2104 * This function is supplemental to the actual forum being trashed which is
2105 * handled by WordPress core API functions. It is used to clean up after
2106 * a forum that is being trashed.
2107 *
2108 * @since bbPress (r3668)
2109 * @uses bbp_get_forum_id() To get the forum id
2110 * @uses bbp_is_forum() To check if the passed id is a forum
2111 * @uses do_action() Calls 'bbp_trash_forum' with the forum id
2112 * @uses bbp_has_topics() To check if the forum has topics
2113 * @uses bbp_topics() To loop through the topics
2114 * @uses bbp_the_topic() To set a topic as the current topic in the loop
2115 * @uses bbp_get_topic_id() To get the topic id
2116 * @uses wp_trash_post() To trash the topic
2117 * @uses update_post_meta() To save a list of just trashed topics for future use
2118 */
2119 function bbp_trash_forum( $forum_id = 0 ) {
2120 $forum_id = bbp_get_forum_id( $forum_id );
2121
2122 if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
2123 return false;
2124
2125 do_action( 'bbp_trash_forum', $forum_id );
2126 }
2127
2128 /**
2129 * Called before untrashing a forum
2130 *
2131 * @since bbPress (r3668)
2132 * @uses bbp_get_forum_id() To get the forum id
2133 * @uses bbp_is_forum() To check if the passed id is a forum
2134 * @uses do_action() Calls 'bbp_untrash_forum' with the forum id
2135 * @uses get_post_meta() To get the list of topics which were trashed with the
2136 * forum
2137 * @uses wp_untrash_post() To untrash the topic
2138 */
2139 function bbp_untrash_forum( $forum_id = 0 ) {
2140 $forum_id = bbp_get_forum_id( $forum_id );
2141
2142 if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
2143 return false;
2144
2145 do_action( 'bbp_untrash_forum', $forum_id );
2146 }
2147
2148 /** After Delete/Trash/Untrash ************************************************/
2149
2150 /**
2151 * Called after deleting a forum
2152 *
2153 * @since bbPress (r3668)
2154 * @uses bbp_get_forum_id() To get the forum id
2155 * @uses bbp_is_forum() To check if the passed id is a forum
2156 * @uses do_action() Calls 'bbp_deleted_forum' with the forum id
2157 */
2158 function bbp_deleted_forum( $forum_id = 0 ) {
2159 $forum_id = bbp_get_forum_id( $forum_id );
2160
2161 if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
2162 return false;
2163
2164 do_action( 'bbp_deleted_forum', $forum_id );
2165 }
2166
2167 /**
2168 * Called after trashing a forum
2169 *
2170 * @since bbPress (r3668)
2171 * @uses bbp_get_forum_id() To get the forum id
2172 * @uses bbp_is_forum() To check if the passed id is a forum
2173 * @uses do_action() Calls 'bbp_trashed_forum' with the forum id
2174 */
2175 function bbp_trashed_forum( $forum_id = 0 ) {
2176 $forum_id = bbp_get_forum_id( $forum_id );
2177
2178 if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
2179 return false;
2180
2181 do_action( 'bbp_trashed_forum', $forum_id );
2182 }
2183
2184 /**
2185 * Called after untrashing a forum
2186 *
2187 * @since bbPress (r3668)
2188 * @uses bbp_get_forum_id() To get the forum id
2189 * @uses bbp_is_forum() To check if the passed id is a forum
2190 * @uses do_action() Calls 'bbp_untrashed_forum' with the forum id
2191 */
2192 function bbp_untrashed_forum( $forum_id = 0 ) {
2193 $forum_id = bbp_get_forum_id( $forum_id );
2194
2195 if ( empty( $forum_id ) || !bbp_is_forum( $forum_id ) )
2196 return false;
2197
2198 do_action( 'bbp_untrashed_forum', $forum_id );
2199 }
2200