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

bbp-forum-functions.php in bbPress 2.0-beta-3, at bbp-includes/bbp-forum-functions.php

930 lines 30.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Forum Functions
5 *
6 * @package bbPress
7 * @subpackage Functions
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /** Walk **********************************************************************/
14
15 /**
16 * Walk the forum tree
17 *
18 * @param object $forums Forums
19 * @param int $depth Depth
20 * @param int $current Current forum
21 * @param array $r Parsed arguments, supported by the walker. If you want to
22 * use your own walker, pass the 'walker' arg with the walker.
23 * The walker defaults to {@link BBP_Walker_Forum}
24 * @return object Walked forum tree
25 */
26 function bbp_walk_forum( $forums, $depth, $current, $r ) {
27 $walker = empty( $r['walker'] ) ? new BBP_Walker_Forum : $r['walker'];
28 $args = array( $forums, $depth, $r, $current );
29 return call_user_func_array( array( &$walker, 'walk' ), $args );
30 }
31
32 /** Forum Actions *************************************************************/
33
34 /**
35 * Closes a forum
36 *
37 * @since bbPress (r2746)
38 *
39 * @param int $forum_id forum id
40 * @uses wp_get_single_post() To get the forum
41 * @uses do_action() Calls 'bbp_close_forum' with the forum id
42 * @uses update_post_meta() To add the previous status to a meta
43 * @uses do_action() Calls 'bbp_opened_forum' with the forum id
44 * @return mixed False or {@link WP_Error} on failure, forum id on success
45 */
46 function bbp_close_forum( $forum_id = 0 ) {
47
48 $forum_id = bbp_get_forum_id( $forum_id );
49
50 do_action( 'bbp_close_forum', $forum_id );
51
52 update_post_meta( $forum_id, '_bbp_status', 'closed' );
53
54 do_action( 'bbp_closed_forum', $forum_id );
55
56 return $forum_id;
57 }
58
59 /**
60 * Opens a forum
61 *
62 * @since bbPress (r2746)
63 *
64 * @param int $forum_id forum id
65 * @uses wp_get_single_post() To get the forum
66 * @uses do_action() Calls 'bbp_open_forum' with the forum id
67 * @uses get_post_meta() To get the previous status
68 * @uses update_post_meta() To delete the previous status meta
69 * @uses do_action() Calls 'bbp_opened_forum' with the forum id
70 * @return mixed False or {@link WP_Error} on failure, forum id on success
71 */
72 function bbp_open_forum( $forum_id = 0 ) {
73
74 $forum_id = bbp_get_forum_id( $forum_id );
75
76 do_action( 'bbp_open_forum', $forum_id );
77
78 update_post_meta( $forum_id, '_bbp_status', 'open' );
79
80 do_action( 'bbp_opened_forum', $forum_id );
81
82 return $forum_id;
83 }
84
85 /**
86 * Make the forum a category
87 *
88 * @since bbPress (r2746)
89 *
90 * @param int $forum_id Optional. Forum id
91 * @uses update_post_meta() To update the forum category meta
92 * @return bool False on failure, true on success
93 */
94 function bbp_categorize_forum( $forum_id = 0 ) {
95
96 $forum_id = bbp_get_forum_id( $forum_id );
97
98 do_action( 'bbp_categorize_forum', $forum_id );
99
100 update_post_meta( $forum_id, '_bbp_forum_type', 'category' );
101
102 do_action( 'bbp_categorized_forum', $forum_id );
103
104 return $forum_id;
105 }
106
107 /**
108 * Remove the category status from a forum
109 *
110 * @since bbPress (r2746)
111 *
112 * @param int $forum_id Optional. Forum id
113 * @uses delete_post_meta() To delete the forum category meta
114 * @return bool False on failure, true on success
115 */
116 function bbp_normalize_forum( $forum_id = 0 ) {
117
118 $forum_id = bbp_get_forum_id( $forum_id );
119
120 do_action( 'bbp_normalize_forum', $forum_id );
121
122 update_post_meta( $forum_id, '_bbp_forum_type', 'forum' );
123
124 do_action( 'bbp_normalized_forum', $forum_id );
125
126 return $forum_id;
127 }
128
129 /**
130 * Mark the forum as public
131 *
132 * @since bbPress (r2746)
133 *
134 * @param int $forum_id Optional. Forum id
135 * @uses update_post_meta() To update the forum private meta
136 * @return bool False on failure, true on success
137 */
138 function bbp_publicize_forum( $forum_id = 0, $current_visibility = '' ) {
139
140 $forum_id = bbp_get_forum_id( $forum_id );
141
142 do_action( 'bbp_publicize_forum', $forum_id );
143
144 // Only run queries if visibility is changing
145 if ( 'publish' != $current_visibility ) {
146
147 // Remove from _bbp_private_forums site option
148 if ( 'private' == $current_visibility ) {
149
150 // Get private forums
151 $private = bbp_get_private_forum_ids();
152
153 // Find this forum in the array
154 if ( in_array( $forum_id, $private ) ) {
155
156 $offset = array_search( $forum_id, (array) $private );
157
158 // Splice around it
159 array_splice( $private, $offset, 1 );
160
161 // Update private forums minus this one
162 update_option( '_bbp_private_forums', array_unique( array_values( $private ) ) );
163 }
164 }
165
166 // Remove from _bbp_hidden_forums site option
167 if ( 'hidden' == $current_visibility ) {
168
169 // Get hidden forums
170 $hidden = bbp_get_hidden_forum_ids();
171
172 // Find this forum in the array
173 if ( in_array( $forum_id, $hidden ) ) {
174
175 $offset = array_search( $forum_id, (array) $hidden );
176
177 // Splice around it
178 array_splice( $hidden, $offset, 1 );
179
180 // Update hidden forums minus this one
181 update_option( '_bbp_hidden_forums', array_unique( array_values( $hidden ) ) );
182 }
183 }
184
185 // Update forum post_status
186 global $wpdb;
187 $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $forum_id ) );
188 wp_transition_post_status( 'publish', $current_visibility, get_post( $forum_id ) );
189 }
190
191 do_action( 'bbp_publicized_forum', $forum_id );
192
193 return $forum_id;
194 }
195
196 /**
197 * Mark the forum as private
198 *
199 * @since bbPress (r2746)
200 *
201 * @param int $forum_id Optional. Forum id
202 * @uses update_post_meta() To update the forum private meta
203 * @return bool False on failure, true on success
204 */
205 function bbp_privatize_forum( $forum_id = 0, $current_visibility = '' ) {
206
207 $forum_id = bbp_get_forum_id( $forum_id );
208
209 do_action( 'bbp_privatize_forum', $forum_id );
210
211 // Only run queries if visibility is changing
212 if ( 'private' != $current_visibility ) {
213
214 // Remove from _bbp_hidden_forums site option
215 if ( 'hidden' == $current_visibility ) {
216
217 // Get hidden forums
218 $hidden = bbp_get_hidden_forum_ids();
219
220 // Find this forum in the array
221 if ( in_array( $forum_id, $hidden ) ) {
222
223 $offset = array_search( $forum_id, (array) $hidden );
224
225 // Splice around it
226 array_splice( $hidden, $offset, 1 );
227
228 // Update hidden forums minus this one
229 update_option( '_bbp_hidden_forums', array_unique( array_values( $hidden ) ) );
230 }
231 }
232
233 // Add to '_bbp_private_forums' site option
234 $private = bbp_get_private_forum_ids();
235 $private[] = $forum_id;
236 update_option( '_bbp_private_forums', array_unique( array_values( $private ) ) );
237
238 // Update forums visibility setting
239 global $wpdb;
240 $wpdb->update( $wpdb->posts, array( 'post_status' => 'private' ), array( 'ID' => $forum_id ) );
241 wp_transition_post_status( 'private', $current_visibility, get_post( $forum_id ) );
242 }
243
244 do_action( 'bbp_privatized_forum', $forum_id );
245
246 return $forum_id;
247 }
248
249 /**
250 * Mark the forum as hidden
251 *
252 * @since bbPress (r2996)
253 *
254 * @param int $forum_id Optional. Forum id
255 * @uses update_post_meta() To update the forum private meta
256 * @return bool False on failure, true on success
257 */
258 function bbp_hide_forum( $forum_id = 0, $current_visibility = '' ) {
259
260 $forum_id = bbp_get_forum_id( $forum_id );
261
262 do_action( 'bbp_hide_forum', $forum_id );
263
264 // Only run queries if visibility is changing
265 if ( 'hidden' != $current_visibility ) {
266
267 // Remove from _bbp_private_forums site option
268 if ( 'private' == $current_visibility ) {
269
270 // Get private forums
271 $private = bbp_get_private_forum_ids();
272
273 // Find this forum in the array
274 if ( in_array( $forum_id, $private ) ) {
275
276 $offset = array_search( $forum_id, (array) $private );
277
278 // Splice around it
279 array_splice( $private, $offset, 1 );
280
281 // Update private forums minus this one
282 update_option( '_bbp_private_forums', array_unique( array_values( $private ) ) );
283 }
284 }
285
286 // Add to '_bbp_hidden_forums' site option
287 $hidden = bbp_get_hidden_forum_ids();
288 $hidden[] = $forum_id;
289 update_option( '_bbp_hidden_forums', array_unique( array_values( $hidden ) ) );
290
291 // Update forums visibility setting
292 global $bbp, $wpdb;
293 $wpdb->update( $wpdb->posts, array( 'post_status' => 'hidden' ), array( 'ID' => $forum_id ) );
294 wp_transition_post_status( $bbp->hidden_status_id, $current_visibility, get_post( $forum_id ) );
295 }
296
297 do_action( 'bbp_hid_forum', $forum_id );
298
299 return $forum_id;
300 }
301
302 /** Forum Updaters ************************************************************/
303
304 /**
305 * Update the forum last topic id
306 *
307 * @since bbPress (r2625)
308 *
309 * @param int $forum_id Optional. Forum id
310 * @param int $topic_id Optional. Topic id
311 * @uses bbp_get_forum_id() To get the forum id
312 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
313 * @uses bbp_update_forum_last_topic_id() To update the last topic id of child
314 * forums
315 * @uses get_posts() To get the most recent topic in the forum
316 * @uses update_post_meta() To update the forum's last active id meta
317 * @uses apply_filters() Calls 'bbp_update_forum_last_topic_id' with the last
318 * reply id and forum id
319 * @return bool True on success, false on failure
320 */
321 function bbp_update_forum_last_topic_id( $forum_id = 0, $topic_id = 0 ) {
322 $forum_id = bbp_get_forum_id( $forum_id );
323
324 // Do some calculation if not manually set
325 if ( empty( $topic_id ) ) {
326
327 // Loop through children and add together forum reply counts
328 if ( $children = bbp_forum_query_subforum_ids( $forum_id ) )
329 foreach ( (array) $children as $child )
330 $children_last_topic = bbp_update_forum_last_topic_id ( $child );
331
332 // Setup recent topic query vars
333 $post_vars = array(
334 'post_parent' => $forum_id,
335 'post_type' => bbp_get_topic_post_type(),
336 'meta_key' => '_bbp_last_active_time',
337 'orderby' => 'meta_value',
338 'numberposts' => 1
339 );
340
341 // Get the most recent topic in this forum_id
342 if ( $recent_topic = get_posts( $post_vars ) )
343 $topic_id = $recent_topic[0]->ID;
344 }
345
346 // If child forums have higher id, use that instead
347 if ( !empty( $children ) && ( $children_last_topic > $topic_id ) )
348 $topic_id = $children_last_topic;
349
350 // Update the last topic id
351 update_post_meta( $forum_id, '_bbp_last_topic_id', (int) $topic_id );
352
353 return apply_filters( 'bbp_update_forum_last_topic_id', (int) $topic_id, $forum_id );
354 }
355
356 /**
357 * Update the forum last reply id
358 *
359 * @since bbPress (r2625)
360 *
361 * @param int $forum_id Optional. Forum id
362 * @param int $reply_id Optional. Reply id
363 * @uses bbp_get_forum_id() To get the forum id
364 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
365 * @uses bbp_update_forum_last_reply_id() To update the last reply id of child
366 * forums
367 * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
368 * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
369 * @uses update_post_meta() To update the forum's last active id meta
370 * @uses apply_filters() Calls 'bbp_update_forum_last_reply_id' with the last
371 * reply id and forum id
372 * @return bool True on success, false on failure
373 */
374 function bbp_update_forum_last_reply_id( $forum_id = 0, $reply_id = 0 ) {
375 $forum_id = bbp_get_forum_id( $forum_id );
376
377 // Do some calculation if not manually set
378 if ( empty( $reply_id ) ) {
379
380 // Loop through children and get the most recent reply id
381 if ( $children = bbp_forum_query_subforum_ids( $forum_id ) )
382 foreach ( (array) $children as $child )
383 $children_last_reply = bbp_update_forum_last_reply_id ( $child );
384
385 // If this forum has topics...
386 if ( $topic_ids = bbp_forum_query_topic_ids( $forum_id ) ) {
387
388 // ...get the most recent reply from those topics...
389 $reply_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids );
390
391 // ...and compare it to the most recent topic id...
392 $reply_id = ( $reply_id > max( $topic_ids ) ) ? $reply_id : max( $topic_ids );
393 }
394 }
395
396 // If child forums have higher ID, check for newer reply id
397 if ( !empty( $children ) && ( (int) $children_last_reply > (int) $reply_id ) )
398 $reply_id = $children_last_reply;
399
400 // Update the last reply id with what was passed
401 update_post_meta( $forum_id, '_bbp_last_reply_id', (int) $reply_id );
402
403 return apply_filters( 'bbp_update_forum_last_reply_id', (int) $reply_id, $forum_id );
404 }
405
406 /**
407 * Update the forum last active post id
408 *
409 * @since bbPress (r2860)
410 *
411 * @param int $forum_id Optional. Forum id
412 * @param int $active_id Optional. Active post id
413 * @uses bbp_get_forum_id() To get the forum id
414 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
415 * @uses bbp_update_forum_last_active_id() To update the last active id of
416 * child forums
417 * @uses bbp_forum_query_topic_ids() To get the topic ids in the forum
418 * @uses bbp_forum_query_last_reply_id() To get the forum's last reply id
419 * @uses update_post_meta() To update the forum's last active id meta
420 * @uses apply_filters() Calls 'bbp_update_forum_last_active_id' with the last
421 * active post id and forum id
422 * @return bool True on success, false on failure
423 */
424 function bbp_update_forum_last_active_id( $forum_id = 0, $active_id = 0 ) {
425 $forum_id = bbp_get_forum_id( $forum_id );
426
427 // Do some calculation if not manually set
428 if ( empty( $active_id ) ) {
429
430 // Loop through children and add together forum reply counts
431 if ( $children = bbp_forum_query_subforum_ids( $forum_id ) )
432 foreach ( (array) $children as $child )
433 $children_last_active = bbp_update_forum_last_active_id ( $child, $active_id );
434
435 // Don't count replies if the forum is a category
436 if ( $topic_ids = bbp_forum_query_topic_ids( $forum_id ) ) {
437 $active_id = bbp_forum_query_last_reply_id( $forum_id, $topic_ids );
438 $active_id = $active_id > max( $topic_ids ) ? $active_id : max( $topic_ids );
439
440 // Forum has no topics
441 } else {
442 $active_id = 0;
443 }
444 }
445
446 // If child forums have higher id, use that instead
447 if ( !empty( $children ) && ( $children_last_active > $active_id ) )
448 $active_id = $children_last_active;
449
450 update_post_meta( $forum_id, '_bbp_last_active_id', (int) $active_id );
451
452 return apply_filters( 'bbp_update_forum_last_active_id', (int) $active_id, $forum_id );
453 }
454
455 /**
456 * Update the forums last active date/time (aka freshness)
457 *
458 * @since bbPress (r2680)
459 *
460 * @param int $forum_id Optional. Topic id
461 * @param string $new_time Optional. New time in mysql format
462 * @uses bbp_get_forum_id() To get the forum id
463 * @uses bbp_get_forum_last_active_id() To get the forum's last post id
464 * @uses get_post_field() To get the post date of the forum's last post
465 * @uses update_post_meta() To update the forum last active time
466 * @uses apply_filters() Calls 'bbp_update_forum_last_active' with the new time
467 * and forum id
468 * @return bool True on success, false on failure
469 */
470 function bbp_update_forum_last_active_time( $forum_id = 0, $new_time = '' ) {
471 $forum_id = bbp_get_forum_id( $forum_id );
472
473 // Check time and use current if empty
474 if ( empty( $new_time ) )
475 $new_time = get_post_field( 'post_date', bbp_get_forum_last_active_id( $forum_id ) );
476
477 update_post_meta( $forum_id, '_bbp_last_active_time', $new_time );
478
479 return apply_filters( 'bbp_update_forum_last_active', $new_time, $forum_id );
480 }
481
482 /**
483 * Update the forum sub-forum count
484 *
485 * @since bbPress (r2625)
486 *
487 * @param int $forum_id Optional. Forum id
488 * @uses bbp_get_forum_id() To get the forum id
489 * @return bool True on success, false on failure
490 */
491 function bbp_update_forum_subforum_count( $forum_id = 0, $subforums = 0 ) {
492 $forum_id = bbp_get_forum_id( $forum_id );
493
494 if ( empty( $subforums ) )
495 $subforums = count( bbp_forum_query_subforum_ids( $forum_id ) );
496
497 update_post_meta( $forum_id, '_bbp_forum_subforum_count', (int) $subforums );
498
499 return apply_filters( 'bbp_update_forum_subforum_count', (int) $subforums, $forum_id );
500 }
501
502 /**
503 * Adjust the total topic count of a forum
504 *
505 * @since bbPress (r2464)
506 *
507 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it
508 * is a topic or a forum. If it's a topic, its parent,
509 * i.e. the forum is automatically retrieved.
510 * @param bool $total_count Optional. To return the total count or normal
511 * count?
512 * @uses bbp_get_forum_id() To get the forum id
513 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
514 * @uses bbp_update_forum_topic_count() To update the forum topic count
515 * @uses bbp_forum_query_topic_ids() To get the forum topic ids
516 * @uses update_post_meta() To update the forum's topic count meta
517 * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the topic
518 * count and forum id
519 * @return int Forum topic count
520 */
521 function bbp_update_forum_topic_count( $forum_id = 0 ) {
522 $forum_id = bbp_get_forum_id( $forum_id );
523 $children_topic_count = 0;
524
525 // Loop through subforums and add together forum topic counts
526 if ( $children = bbp_forum_query_subforum_ids( $forum_id ) )
527 foreach ( (array) $children as $child )
528 $children_topic_count += bbp_update_forum_topic_count( $child );
529
530 // Get total topics for this forum
531 $topics = (int) count( bbp_forum_query_topic_ids( $forum_id ) );
532
533 // Calculate total topics in this forum
534 $total_topics = $topics + $children_topic_count;
535
536 // Update the count
537 update_post_meta( $forum_id, '_bbp_forum_topic_count', (int) $topics );
538 update_post_meta( $forum_id, '_bbp_total_topic_count', (int) $total_topics );
539
540 return apply_filters( 'bbp_update_forum_topic_count', (int) $total_topics, $forum_id );
541 }
542
543 /**
544 * Adjust the total hidden topic count of a forum (hidden includes trashed and spammed topics)
545 *
546 * @since bbPress (r2888)
547 *
548 * @param int $forum_id Optional. Topic id to update
549 * @param int $topic_count Optional. Set the topic count manually
550 * @uses bbp_is_topic() To check if the supplied id is a topic
551 * @uses bbp_get_topic_id() To get the topic id
552 * @uses bbp_get_topic_forum_id() To get the topic forum id
553 * @uses bbp_get_forum_id() To get the forum id
554 * @uses wpdb::prepare() To prepare our sql query
555 * @uses wpdb::get_col() To execute our query and get the column back
556 * @uses update_post_meta() To update the forum hidden topic count meta
557 * @uses apply_filters() Calls 'bbp_update_forum_hidden_topic_count' with the
558 * hidden topic count and forum id
559 * @return int Topic hidden topic count
560 */
561 function bbp_update_forum_hidden_topic_count( $forum_id = 0, $topic_count = 0 ) {
562 global $wpdb, $bbp;
563
564 // If topic_id was passed as $forum_id, then get its forum
565 if ( bbp_is_topic( $forum_id ) ) {
566 $topic_id = bbp_get_topic_id( $forum_id );
567 $forum_id = bbp_get_topic_forum_id( $topic_id );
568
569 // $forum_id is not a topic_id, so validate and proceed
570 } else {
571 $forum_id = bbp_get_forum_id( $forum_id );
572 }
573
574 // Can't update what isn't there
575 if ( !empty( $forum_id ) ) {
576
577 // Get topics of forum
578 if ( empty( $topic_count ) )
579 $topic_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent = %d AND post_status IN ( '" . join( '\',\'', array( $bbp->trash_status_id, $bbp->spam_status_id ) ) . "') AND post_type = '%s';", $forum_id, bbp_get_topic_post_type() ) );
580
581 // Update the count
582 update_post_meta( $forum_id, '_bbp_topic_count_hidden', (int) $topic_count );
583 }
584
585 return apply_filters( 'bbp_update_forum_hidden_topic_count', (int) $topic_count, $forum_id );
586 }
587
588 /**
589 * Adjust the total reply count of a forum
590 *
591 * @since bbPress (r2464)
592 *
593 * @param int $forum_id Optional. Forum id or topic id. It is checked whether it
594 * is a topic or a forum. If it's a topic, its parent,
595 * i.e. the forum is automatically retrieved.
596 * @param bool $total_count Optional. To return the total count or normal
597 * count?
598 * @uses bbp_get_forum_id() To get the forum id
599 * @uses bbp_forum_query_subforum_ids() To get the subforum ids
600 * @uses bbp_update_forum_reply_count() To update the forum reply count
601 * @uses bbp_forum_query_topic_ids() To get the forum topic ids
602 * @uses wpdb::prepare() To prepare the sql statement
603 * @uses wpdb::get_var() To execute the query and get the var back
604 * @uses update_post_meta() To update the forum's reply count meta
605 * @uses apply_filters() Calls 'bbp_update_forum_topic_count' with the reply
606 * count and forum id
607 * @return int Forum reply count
608 */
609 function bbp_update_forum_reply_count( $forum_id = 0 ) {
610 global $wpdb, $bbp;
611
612 $forum_id = bbp_get_forum_id( $forum_id );
613 $children_reply_count = 0;
614
615 // Loop through children and add together forum reply counts
616 if ( $children = bbp_forum_query_subforum_ids( $forum_id ) )
617 foreach ( (array) $children as $child )
618 $children_reply_count += bbp_update_forum_reply_count( $child );
619
620 // Don't count replies if the forum is a category
621 if ( $topic_ids = bbp_forum_query_topic_ids( $forum_id ) )
622 $reply_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = 'publish' AND post_type = '%s';", bbp_get_reply_post_type() ) );
623 else
624 $reply_count = 0;
625
626 // Calculate total replies in this forum
627 $total_replies = (int) $reply_count + $children_reply_count;
628
629 // Update the count
630 update_post_meta( $forum_id, '_bbp_reply_count', $reply_count );
631 update_post_meta( $forum_id, '_bbp_total_reply_count', $total_replies );
632
633 return apply_filters( 'bbp_update_forum_reply_count', $total_replies, $forum_id );
634 }
635
636 /**
637 * Updates the counts of a forum.
638 *
639 * This calls a few internal functions that all run manual queries against the
640 * database to get their results. As such, this function can be costly to run
641 * but is necessary to keep everything accurate.
642 *
643 * @since bbPress (r2908)
644 *
645 * @param mixed $args Supports these arguments:
646 * - forum_id: Forum id
647 * - last_topic_id: Last topic id
648 * - last_reply_id: Last reply id
649 * - last_active_id: Last active post id
650 * - last_active_time: last active time
651 * @uses bbp_update_forum_last_topic_id() To update the forum last topic id
652 * @uses bbp_update_forum_last_reply_id() To update the forum last reply id
653 * @uses bbp_update_forum_last_active_id() To update the last active post id
654 * @uses get_post_field() To get the post date of the last active id
655 * @uses bbp_update_forum_last_active_time() To update the last active time
656 * @uses bbp_update_forum_subforum_count() To update the subforum count
657 * @uses bbp_update_forum_topic_count() To update the forum topic count
658 * @uses bbp_update_forum_reply_count() To update the forum reply count
659 * @uses bbp_update_forum_hidden_topic_count() To update the hidden topic count
660 */
661 function bbp_update_forum( $args = '' ) {
662 $defaults = array(
663 'forum_id' => 0,
664 'post_parent' => 0,
665 'last_topic_id' => 0,
666 'last_reply_id' => 0,
667 'last_active_id' => 0,
668 'last_active_time' => 0,
669 );
670
671 $r = wp_parse_args( $args, $defaults );
672 extract( $r );
673
674 // Last topic and reply ID's
675 bbp_update_forum_last_topic_id( $forum_id, $last_topic_id );
676 bbp_update_forum_last_reply_id( $forum_id, $last_reply_id );
677
678 // Active dance
679 $last_active_id = bbp_update_forum_last_active_id( $forum_id, $last_active_id );
680
681 // If no active time was passed, get it from the last_active_id
682 if ( empty( $last_active_time ) )
683 $last_active_time = get_post_field( 'post_date', $last_active_id );
684
685 bbp_update_forum_last_active_time( $forum_id, $last_active_time );
686
687 // Counts
688 bbp_update_forum_subforum_count ( $forum_id );
689 bbp_update_forum_reply_count ( $forum_id );
690 bbp_update_forum_topic_count ( $forum_id );
691 bbp_update_forum_hidden_topic_count( $forum_id );
692
693 // Update the parent forum if one was passed
694 if ( !empty( $post_parent ) && is_numeric( $post_parent ) ) {
695 bbp_update_forum( array(
696 'forum_id' => $post_parent,
697 'post_parent' => get_post_field( 'post_parent', $post_parent )
698 ) );
699 }
700 }
701
702 /** Queries *******************************************************************/
703
704 /**
705 * Returns the hidden forum ids
706 *
707 * Only hidden forum ids are returned. Public and private ids are not.
708 *
709 * @since bbPress (r3007)
710 *
711 * @uses get_option() Returns the unserialized array of hidden forum ids
712 * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
713 * and forum id
714 */
715 function bbp_get_hidden_forum_ids() {
716 $forum_ids = get_option( '_bbp_hidden_forums', array() );
717
718 return apply_filters( 'bbp_get_hidden_forum_ids', (array) $forum_ids );
719 }
720
721 /**
722 * Returns the private forum ids
723 *
724 * Only private forum ids are returned. Public and hidden ids are not.
725 *
726 * @since bbPress (r3007)
727 *
728 * @uses get_option() Returns the unserialized array of private forum ids
729 * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
730 * and forum id
731 */
732 function bbp_get_private_forum_ids() {
733 $forum_ids = get_option( '_bbp_private_forums', array() );
734
735 return apply_filters( 'bbp_get_private_forum_ids', (array) $forum_ids );
736 }
737
738 /**
739 * Returns a meta_query that either includes or excludes hidden forum IDs
740 * from a query.
741 *
742 * @since bbPress (r3291)
743 *
744 * @param string Optional. The type of value to return. (string|array|meta_query)
745 *
746 * @uses is_super_admin()
747 * @uses bbp_is_user_home()
748 * @uses bbp_get_hidden_forum_ids()
749 * @uses bbp_get_private_forum_ids()
750 * @uses apply_filters()
751 */
752 function bbp_exclude_forum_ids( $type = 'string' ) {
753
754 // Setup arrays
755 $retval = $private = $hidden = $meta_query = $forum_ids = array();
756
757 // Exclude for everyone but super admins
758 if ( !is_super_admin() ) {
759
760 // Private forums
761 if ( !current_user_can( 'read_private_forums' ) )
762 $private = bbp_get_private_forum_ids();
763
764 // Hidden forums
765 if ( !current_user_can( 'read_hidden_forums' ) )
766 $hidden = bbp_get_hidden_forum_ids();
767
768 // Merge private and hidden forums together
769 $forum_ids = (array) array_filter( array_merge( $private, $hidden ) );
770
771 // There are forums that need to be excluded
772 if ( !empty( $forum_ids ) ) {
773
774 switch ( $type ) {
775
776 // Separate forum ID's into a comma separated string
777 case 'string' :
778 $retval = implode( ',', $forum_ids );
779 break;
780
781 // Use forum_ids array
782 case 'array' :
783 $retval = $forum_ids;
784 break;
785
786 // Build a meta_query
787 case 'meta_query' :
788 $retval = array(
789 'key' => '_bbp_forum_id',
790 'value' => implode( ',', $forum_ids ),
791 'compare' => ( 1 < count( $forum_ids ) ) ? 'NOT IN' : '!='
792 );
793 break;
794 }
795 }
796 }
797
798 // Filter and return the results
799 return apply_filters( 'bbp_exclude_forum_ids', $retval, $forum_ids, $type );
800 }
801
802 /**
803 * Returns the forum's topic ids
804 *
805 * Only topics with published and closed statuses are returned
806 *
807 * @since bbPress (r2908)
808 *
809 * @param int $forum_id Forum id
810 * @uses bbp_get_topic_post_type() To get the topic post type
811 * @uses bbp_get_public_child_ids() To get the topic ids
812 * @uses apply_filters() Calls 'bbp_forum_query_topic_ids' with the topic ids
813 * and forum id
814 */
815 function bbp_forum_query_topic_ids( $forum_id ) {
816 global $bbp;
817
818 $topic_ids = bbp_get_public_child_ids( $forum_id, bbp_get_topic_post_type() );
819
820 return apply_filters( 'bbp_forum_query_topic_ids', $topic_ids, $forum_id );
821 }
822
823 /**
824 * Returns the forum's subforum ids
825 *
826 * Only forums with published status are returned
827 *
828 * @since bbPress (r2908)
829 *
830 * @param int $forum_id Forum id
831 * @uses bbp_get_forum_post_type() To get the forum post type
832 * @uses bbp_get_public_child_ids() To get the forum ids
833 * @uses apply_filters() Calls 'bbp_forum_query_subforum_ids' with the subforum
834 * ids and forum id
835 */
836 function bbp_forum_query_subforum_ids( $forum_id ) {
837 global $bbp, $wpdb;
838
839 $subforum_ids = bbp_get_public_child_ids( $forum_id, bbp_get_forum_post_type() );
840
841 return apply_filters( 'bbp_get_forum_subforum_ids', $subforum_ids, $forum_id );
842 }
843
844 /**
845 * Returns the forum's last reply id
846 *
847 * @since bbPress (r2908)
848 *
849 * @param int $forum_id Forum id
850 * @param int $topic_ids Optional. Topic ids
851 * @uses wp_cache_get() To check for cache and retrieve it
852 * @uses bbp_forum_query_topic_ids() To get the forum's topic ids
853 * @uses wpdb::prepare() To prepare the query
854 * @uses wpdb::get_var() To execute the query and get the var back
855 * @uses bbp_get_reply_post_type() To get the reply post type
856 * @uses wp_cache_set() To set the cache for future use
857 * @uses apply_filters() Calls 'bbp_forum_query_last_reply_id' with the reply id
858 * and forum id
859 */
860 function bbp_forum_query_last_reply_id( $forum_id, $topic_ids = 0 ) {
861 global $bbp, $wpdb;
862
863 $cache_id = 'bbp_get_forum_' . $forum_id . '_reply_id';
864
865 if ( !$reply_id = (int) wp_cache_get( $cache_id, 'bbpress' ) ) {
866
867 if ( empty( $topic_ids ) )
868 $topic_ids = bbp_forum_query_topic_ids( $forum_id );
869
870 if ( !empty( $topic_ids ) && ( $reply_id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent IN ( " . join( ',', $topic_ids ) . " ) AND post_status = 'publish' AND post_type = '%s' ORDER BY ID DESC LIMIT 1;", bbp_get_reply_post_type() ) ) ) )
871 wp_cache_set( $cache_id, $reply_id, 'bbpress' );
872 else
873 wp_cache_set( $cache_id, '0', 'bbpress' );
874 }
875
876 return apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );
877 }
878
879 /** Listeners *****************************************************************/
880
881 /**
882 * Check if it's a private forum or a topic or reply of a private forum and if
883 * the user can't view it, then sets a 404
884 *
885 * @since bbPress (r2996)
886 *
887 * @uses current_user_can() To check if the current user can read private forums
888 * @uses is_singular() To check if it's a singular page
889 * @uses bbp_get_forum_post_type() To get the forum post type
890 * @uses bbp_get_topic_post_type() To get the topic post type
891 * @uses bbp_get_reply_post_type() TO get the reply post type
892 * @uses bbp_get_topic_forum_id() To get the topic forum id
893 * @uses bbp_get_reply_forum_id() To get the reply forum id
894 * @uses bbp_is_forum_private() To check if the forum is private or not
895 * @uses bbp_set_404() To set a 404 status
896 */
897 function bbp_forum_visibility_check() {
898 global $wp_query;
899
900 // Bail if not viewing a single item or if user has caps
901 if ( !is_singular() || is_super_admin() || ( current_user_can( 'read_private_forums' ) && current_user_can( 'read_hidden_forums' ) ) )
902 return;
903
904 // Check post type
905 switch ( $wp_query->get( 'post_type' ) ) {
906
907 // Forum
908 case bbp_get_forum_post_type() :
909 $forum_id = bbp_get_forum_id( $wp_query->post->ID );
910 break;
911
912 // Topic
913 case bbp_get_topic_post_type() :
914 $forum_id = bbp_get_topic_forum_id( $wp_query->post->ID );
915 break;
916
917 // Reply
918 case bbp_get_reply_post_type() :
919 $forum_id = bbp_get_reply_forum_id( $wp_query->post->ID );
920 break;
921
922 }
923
924 // If forum is explicitly hidden and user not capable, set 404
925 if ( !empty( $forum_id ) && bbp_is_forum_hidden( $forum_id ) && !current_user_can( 'read_hidden_forums' ) )
926 bbp_set_404();
927 }
928
929 ?>
930