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-template.php

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

1,835 lines 58.7 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 Template Tags
5 *
6 * @package bbPress
7 * @subpackage TemplateTags
8 */
9
10 // Exit if accessed directly
11 if ( !defined( 'ABSPATH' ) ) exit;
12
13 /** Post Type *****************************************************************/
14
15 /**
16 * Output the unique id of the custom post type for forums
17 *
18 * @since bbPress (r2857)
19 * @uses bbp_get_forum_post_type() To get the forum post type
20 */
21 function bbp_forum_post_type() {
22 echo bbp_get_forum_post_type();
23 }
24 /**
25 * Return the unique id of the custom post type for forums
26 *
27 * @since bbPress (r2857)
28 *
29 * @uses apply_filters() Calls 'bbp_get_forum_post_type' with the forum
30 * post type id
31 * @return string The unique forum post type id
32 */
33 function bbp_get_forum_post_type() {
34 global $bbp;
35
36 return apply_filters( 'bbp_get_forum_post_type', $bbp->forum_post_type );
37 }
38
39 /** Forum Loop ****************************************************************/
40
41 /**
42 * The main forum loop.
43 *
44 * WordPress makes this easy for us.
45 *
46 * @since bbPress (r2464)
47 *
48 * @param mixed $args All the arguments supported by {@link WP_Query}
49 * @uses WP_Query To make query and get the forums
50 * @uses bbp_get_forum_post_type() To get the forum post type id
51 * @uses bbp_get_forum_id() To get the forum id
52 * @uses get_option() To get the forums per page option
53 * @uses current_user_can() To check if the current user is capable of editing
54 * others' forums
55 * @uses apply_filters() Calls 'bbp_has_forums' with
56 * bbPres::forum_query::have_posts()
57 * and bbPres::forum_query
58 * @return object Multidimensional array of forum information
59 */
60 function bbp_has_forums( $args = '' ) {
61 global $bbp;
62
63 // Make sure we're back where we started
64 wp_reset_postdata();
65
66 // Setup possible post__not_in array
67 $post_stati[] = 'publish';
68
69 // Super admin get whitelisted post statuses
70 if ( is_super_admin() ) {
71 $post_stati = array( 'publish', 'private', 'hidden' );
72
73 // Not a super admin, so check caps
74 } else {
75
76 // Check if user can read private forums
77 if ( current_user_can( 'read_private_forums' ) )
78 $post_stati[] = 'private';
79
80 // Check if user can read hidden forums
81 if ( current_user_can( 'read_hidden_forums' ) )
82 $post_stati[] = 'hidden';
83 }
84
85 // The default forum query for most circumstances
86 $default = array (
87 'post_type' => bbp_get_forum_post_type(),
88 'post_parent' => bbp_is_forum_archive() ? 0 : bbp_get_forum_id() ,
89 'post_status' => implode( ',', $post_stati ),
90 'posts_per_page' => get_option( '_bbp_forums_per_page', 15 ),
91 'orderby' => 'menu_order',
92 'order' => 'ASC'
93 );
94
95 // Parse the default against what is requested
96 $bbp_f = wp_parse_args( $args, $default );
97
98 // Filter the forums query to allow just-in-time modifications
99 $bbp_f = apply_filters( 'bbp_has_forums_query', $bbp_f );
100
101 // Run the query
102 $bbp->forum_query = new WP_Query( $bbp_f );
103
104 return apply_filters( 'bbp_has_forums', $bbp->forum_query->have_posts(), $bbp->forum_query );
105 }
106
107 /**
108 * Whether there are more forums available in the loop
109 *
110 * @since bbPress (r2464)
111 *
112 * @uses bbPress:forum_query::have_posts() To check if there are more forums
113 * available
114 * @return object Forum information
115 */
116 function bbp_forums() {
117 global $bbp;
118 return $bbp->forum_query->have_posts();
119 }
120
121 /**
122 * Loads up the current forum in the loop
123 *
124 * @since bbPress (r2464)
125 *
126 * @uses bbPress:forum_query::the_post() To get the current forum
127 * @return object Forum information
128 */
129 function bbp_the_forum() {
130 global $bbp;
131 return $bbp->forum_query->the_post();
132 }
133
134 /** Forum *********************************************************************/
135
136 /**
137 * Output forum id
138 *
139 * @since bbPress (r2464)
140 *
141 * @param $forum_id Optional. Used to check emptiness
142 * @uses bbp_get_forum_id() To get the forum id
143 */
144 function bbp_forum_id( $forum_id = 0 ) {
145 echo bbp_get_forum_id( $forum_id );
146 }
147 /**
148 * Return the forum id
149 *
150 * @since bbPress (r2464)
151 *
152 * @param $forum_id Optional. Used to check emptiness
153 * @uses bbPress::forum_query::in_the_loop To check if we're in the loop
154 * @uses bbPress::forum_query::post::ID To get the forum id
155 * @uses WP_Query::post::ID To get the forum id
156 * @uses bbp_is_forum() To check if it's a forum page
157 * @uses bbp_is_topic() To check if it's a topic page
158 * @uses bbp_get_topic_forum_id() To get the topic forum id
159 * @uses get_post_field() To get the post's post type
160 * @uses apply_filters() Calls 'bbp_get_forum_id' with the forum id and
161 * supplied forum id
162 * @return int The forum id
163 */
164 function bbp_get_forum_id( $forum_id = 0 ) {
165 global $bbp, $wp_query;
166
167 // Easy empty checking
168 if ( !empty( $forum_id ) && is_numeric( $forum_id ) )
169 $bbp_forum_id = $bbp->current_forum_id = $forum_id;
170
171 // Currently inside a forum loop
172 elseif ( !empty( $bbp->forum_query->in_the_loop ) && isset( $bbp->forum_query->post->ID ) )
173 $bbp_forum_id = $bbp->current_forum_id = $bbp->forum_query->post->ID;
174
175 // Currently viewing a forum
176 elseif ( bbp_is_forum() && isset( $wp_query->post->ID ) )
177 $bbp_forum_id = $bbp->current_forum_id = $wp_query->post->ID;
178
179 // Currently viewing a topic
180 elseif ( bbp_is_topic() )
181 $bbp_forum_id = $bbp->current_forum_id = bbp_get_topic_forum_id();
182
183 // Fallback
184 else
185 $bbp_forum_id = 0;
186
187 return apply_filters( 'bbp_get_forum_id', (int) $bbp_forum_id, $forum_id );
188 }
189
190 /**
191 * Gets a forum
192 *
193 * @since bbPress (r2787)
194 *
195 * @param int|object $forum forum id or forum object
196 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N. Default = OBJECT
197 * @param string $filter Optional Sanitation filter. See {@link sanitize_post()}
198 * @uses get_post() To get the forum
199 * @uses apply_filters() Calls 'bbp_get_forum' with the forum, output type and
200 * sanitation filter
201 * @return mixed Null if error or forum (in specified form) if success
202 */
203 function bbp_get_forum( $forum, $output = OBJECT, $filter = 'raw' ) {
204 if ( empty( $forum ) || is_numeric( $forum ) )
205 $forum = bbp_get_forum_id( $forum );
206
207 if ( !$forum = get_post( $forum, OBJECT, $filter ) )
208 return $forum;
209
210 if ( $forum->post_type !== bbp_get_forum_post_type() )
211 return null;
212
213 if ( $output == OBJECT ) {
214 return $forum;
215
216 } elseif ( $output == ARRAY_A ) {
217 $_forum = get_object_vars( $forum );
218 return $_forum;
219
220 } elseif ( $output == ARRAY_N ) {
221 $_forum = array_values( get_object_vars( $forum ) );
222 return $_forum;
223
224 }
225
226 return apply_filters( 'bbp_get_forum', $forum, $output, $filter );
227 }
228
229 /**
230 * Output the link to the forum
231 *
232 * @since bbPress (r2464)
233 *
234 * @param int $forum_id Optional. Forum id
235 * @uses bbp_get_forum_permalink() To get the permalink
236 */
237 function bbp_forum_permalink( $forum_id = 0 ) {
238 echo bbp_get_forum_permalink( $forum_id );
239 }
240 /**
241 * Return the link to the forum
242 *
243 * @since bbPress (r2464)
244 *
245 * @param int $forum_id Optional. Forum id
246 * @uses bbp_get_forum_id() To get the forum id
247 * @uses get_permalink() Get the permalink of the forum
248 * @uses apply_filters() Calls 'bbp_get_forum_permalink' with the forum
249 * link
250 * @return string Permanent link to forum
251 */
252 function bbp_get_forum_permalink( $forum_id = 0 ) {
253 $forum_id = bbp_get_forum_id( $forum_id );
254 return apply_filters( 'bbp_get_forum_permalink', get_permalink( $forum_id ) );
255 }
256
257 /**
258 * Output the title of the forum
259 *
260 * @since bbPress (r2464)
261 *
262 * @param int $forum_id Optional. Forum id
263 * @uses bbp_get_forum_title() To get the forum title
264 */
265 function bbp_forum_title( $forum_id = 0 ) {
266 echo bbp_get_forum_title( $forum_id );
267 }
268 /**
269 * Return the title of the forum
270 *
271 * @since bbPress (r2464)
272 *
273 * @param int $forum_id Optional. Forum id
274 * @uses bbp_get_forum_id() To get the forum id
275 * @uses get_the_title() To get the forum title
276 * @uses apply_filters() Calls 'bbp_get_forum_title' with the title
277 * @return string Title of forum
278 */
279 function bbp_get_forum_title( $forum_id = 0 ) {
280 $forum_id = bbp_get_forum_id( $forum_id );
281
282 return apply_filters( 'bbp_get_forum_title', get_the_title( $forum_id ) );
283 }
284
285 /**
286 * Output the forum archive title
287 *
288 * @since bbPress (r3249)
289 *
290 * @param string $title Default text to use as title
291 */
292 function bbp_forum_archive_title( $title = '' ) {
293 echo bbp_get_forum_archive_title( $title );
294 }
295 /**
296 * Return the forum archive title
297 *
298 * @since bbPress (r3249)
299 *
300 * @global bbPress $bbp The main bbPress class
301 * @param string $title Default text to use as title
302 *
303 * @uses bbp_get_page_by_path() Check if page exists at root path
304 * @uses get_the_title() Use the page title at the root path
305 * @uses get_post_type_object() Load the post type object
306 * @uses bbp_get_forum_post_type() Get the forum post type ID
307 * @uses get_post_type_labels() Get labels for forum post type
308 * @uses apply_filters() Allow output to be manipulated
309 *
310 * @return string The forum archive title
311 */
312 function bbp_get_forum_archive_title( $title = '' ) {
313 global $bbp;
314
315 // If no title was passed
316 if ( empty( $title ) ) {
317
318 // Set root text to page title
319 if ( $page = bbp_get_page_by_path( $bbp->root_slug ) ) {
320 $title = get_the_title( $page->ID );
321
322 // Default to forum post type name label
323 } else {
324 $fto = get_post_type_object( bbp_get_forum_post_type() );
325 $title = $fto->labels->name;
326 }
327 }
328
329 return apply_filters( 'bbp_get_forum_archive_title', $title );
330 }
331
332 /**
333 * Output the content of the forum
334 *
335 * @since bbPress (r2780)
336 *
337 * @param int $forum_id Optional. Topic id
338 * @uses bbp_get_forum_content() To get the forum content
339 */
340 function bbp_forum_content( $forum_id = 0 ) {
341 echo bbp_get_forum_content( $forum_id );
342 }
343 /**
344 * Return the content of the forum
345 *
346 * @since bbPress (r2780)
347 *
348 * @param int $forum_id Optional. Topic id
349 * @uses bbp_get_forum_id() To get the forum id
350 * @uses post_password_required() To check if the forum requires pass
351 * @uses get_the_password_form() To get the password form
352 * @uses get_post_field() To get the content post field
353 * @uses apply_filters() Calls 'bbp_get_forum_content' with the content
354 * and forum id
355 * @return string Content of the forum
356 */
357 function bbp_get_forum_content( $forum_id = 0 ) {
358 $forum_id = bbp_get_forum_id( $forum_id );
359
360 // Check if password is required
361 if ( post_password_required( $forum_id ) )
362 return get_the_password_form();
363
364 $content = get_post_field( 'post_content', $forum_id );
365
366 return apply_filters( 'bbp_get_forum_content', $content, $forum_id );
367 }
368
369 /**
370 * Output the forums last active ID
371 *
372 * @since bbPress (r2860)
373 *
374 * @uses bbp_get_forum_last_active_id() To get the forum's last active id
375 * @param int $forum_id Optional. Forum id
376 */
377 function bbp_forum_last_active_id( $forum_id = 0 ) {
378 echo bbp_get_forum_last_active_id( $forum_id );
379 }
380 /**
381 * Return the forums last active ID
382 *
383 * @since bbPress (r2860)
384 *
385 * @param int $forum_id Optional. Forum id
386 * @uses bbp_get_forum_id() To get the forum id
387 * @uses get_post_meta() To get the forum's last active id
388 * @uses apply_filters() Calls 'bbp_get_forum_last_active_id' with
389 * the last active id and forum id
390 * @return int Forum's last active id
391 */
392 function bbp_get_forum_last_active_id( $forum_id = 0 ) {
393 $forum_id = bbp_get_forum_id( $forum_id );
394 $active_id = get_post_meta( $forum_id, '_bbp_last_active_id', true );
395
396 return apply_filters( 'bbp_get_forum_last_active_id', (int) $active_id, $forum_id );
397 }
398
399 /**
400 * Output the forums last update date/time (aka freshness)
401 *
402 * @since bbPress (r2464)
403 *
404 * @uses bbp_get_forum_last_active_time() To get the forum freshness
405 * @param int $forum_id Optional. Forum id
406 */
407 function bbp_forum_last_active_time( $forum_id = 0 ) {
408 echo bbp_get_forum_last_active_time( $forum_id );
409 }
410 /**
411 * Return the forums last update date/time (aka freshness)
412 *
413 * @since bbPress (r2464)
414 *
415 * @param int $forum_id Optional. Forum id
416 * @uses bbp_get_forum_id() To get the forum id
417 * @uses get_post_meta() To retrieve forum last active meta
418 * @uses bbp_get_forum_last_reply_id() To get forum's last reply id
419 * @uses get_post_field() To get the post date of the reply
420 * @uses bbp_get_forum_last_topic_id() To get forum's last topic id
421 * @uses bbp_get_topic_last_active_time() To get time when the topic was
422 * last active
423 * @uses bbp_convert_date() To convert the date
424 * @uses bbp_get_time_since() To get time in since format
425 * @uses apply_filters() Calls 'bbp_get_forum_last_active' with last
426 * active time and forum id
427 * @return string Forum last update date/time (freshness)
428 */
429 function bbp_get_forum_last_active_time( $forum_id = 0 ) {
430 $forum_id = bbp_get_forum_id( $forum_id );
431
432 if ( !$last_active = get_post_meta( $forum_id, '_bbp_last_active_time', true ) ) {
433 if ( $reply_id = bbp_get_forum_last_reply_id( $forum_id ) ) {
434 $last_active = get_post_field( 'post_date', $reply_id );
435 } else {
436 if ( $topic_id = bbp_get_forum_last_topic_id( $forum_id ) ) {
437 $last_active = bbp_get_topic_last_active_time( $topic_id );
438 }
439 }
440 }
441
442 $last_active = !empty( $last_active ) ? bbp_get_time_since( bbp_convert_date( $last_active ) ) : '';
443
444 return apply_filters( 'bbp_get_forum_last_active', $last_active, $forum_id );
445 }
446
447 /**
448 * Output link to the most recent activity inside a forum.
449 *
450 * Outputs a complete link with attributes and content.
451 *
452 * @since bbPress (r2625)
453 *
454 * @param int $forum_id Optional. Forum id
455 * @uses bbp_get_forum_freshness_link() To get the forum freshness link
456 */
457 function bbp_forum_freshness_link( $forum_id = 0) {
458 echo bbp_get_forum_freshness_link( $forum_id );
459 }
460 /**
461 * Returns link to the most recent activity inside a forum.
462 *
463 * Returns a complete link with attributes and content.
464 *
465 * @since bbPress (r2625)
466 *
467 * @param int $forum_id Optional. Forum id
468 * @uses bbp_get_forum_id() To get the forum id
469 * @uses bbp_get_forum_last_active_id() To get the forum last active id
470 * @uses bbp_get_forum_last_reply_id() To get the forum last reply id
471 * @uses bbp_get_forum_last_topic_id() To get the forum last topic id
472 * @uses bbp_get_forum_last_reply_url() To get the forum last reply url
473 * @uses bbp_get_forum_last_reply_title() To get the forum last reply
474 * title
475 * @uses bbp_get_forum_last_topic_permalink() To get the forum last
476 * topic permalink
477 * @uses bbp_get_forum_last_topic_title() To get the forum last topic
478 * title
479 * @uses bbp_get_forum_last_active_time() To get the time when the forum
480 * was last active
481 * @uses apply_filters() Calls 'bbp_get_forum_freshness_link' with the
482 * link and forum id
483 */
484 function bbp_get_forum_freshness_link( $forum_id = 0 ) {
485 $forum_id = bbp_get_forum_id( $forum_id );
486 $active_id = bbp_get_forum_last_active_id( $forum_id );
487
488 if ( empty( $active_id ) )
489 $active_id = bbp_get_forum_last_reply_id( $forum_id );
490
491 if ( empty( $active_id ) )
492 $active_id = bbp_get_forum_last_topic_id( $forum_id );
493
494 if ( bbp_is_topic( $active_id ) ) {
495 $link_url = bbp_get_forum_last_topic_permalink( $forum_id );
496 $title = bbp_get_forum_last_topic_title( $forum_id );
497 } elseif ( bbp_is_reply( $active_id ) ) {
498 $link_url = bbp_get_forum_last_reply_url( $forum_id );
499 $title = bbp_get_forum_last_reply_title( $forum_id );
500 }
501
502 $time_since = bbp_get_forum_last_active_time( $forum_id );
503
504 if ( !empty( $time_since ) && !empty( $link_url ) )
505 $anchor = '<a href="' . $link_url . '" title="' . esc_attr( $title ) . '">' . $time_since . '</a>';
506 else
507 $anchor = __( 'No Topics', 'bbpress' );
508
509 return apply_filters( 'bbp_get_forum_freshness_link', $anchor, $forum_id );
510 }
511
512 /**
513 * Return ID of forum parent, if exists
514 *
515 * @since bbPress (r2625)
516 *
517 * @param int $forum_id Optional. Forum id
518 * @uses bbp_get_forum_id() To get the forum id
519 * @uses get_post_field() To get the forum parent
520 * @uses apply_filters() Calls 'bbp_get_forum_parent' with the parent & forum id
521 * @return int Forum parent
522 */
523 function bbp_get_forum_parent( $forum_id = 0 ) {
524 $forum_id = bbp_get_forum_id( $forum_id );
525 $parent_id = get_post_field( 'post_parent', $forum_id );
526
527 return apply_filters( 'bbp_get_forum_parent', (int) $parent_id, $forum_id );
528 }
529
530 /**
531 * Return array of parent forums
532 *
533 * @since bbPress (r2625)
534 *
535 * @param int $forum_id Optional. Forum id
536 * @uses bbp_get_forum_id() To get the forum id
537 * @uses bbp_get_forum() To get the forum
538 * @uses apply_filters() Calls 'bbp_get_forum_ancestors' with the ancestors
539 * and forum id
540 * @return array Forum ancestors
541 */
542 function bbp_get_forum_ancestors( $forum_id = 0 ) {
543 $forum_id = bbp_get_forum_id( $forum_id );
544 $ancestors = array();
545
546 if ( $forum = bbp_get_forum( $forum_id ) ) {
547 while ( 0 !== $forum->post_parent ) {
548 $ancestors[] = $forum->post_parent;
549 $forum = bbp_get_forum( $forum->post_parent );
550 }
551 }
552
553 return apply_filters( 'bbp_get_forum_ancestors', $ancestors, $forum_id );
554 }
555
556 /**
557 * Return subforums of given forum
558 *
559 * @since bbPress (r2747)
560 *
561 * @param mixed $args All the arguments supported by {@link WP_Query}
562 * @uses bbp_get_forum_id() To get the forum id
563 * @uses current_user_can() To check if the current user is capable of
564 * reading private forums
565 * @uses get_posts() To get the subforums
566 * @uses apply_filters() Calls 'bbp_forum_get_subforums' with the subforums
567 * and the args
568 * @return mixed false if none, array of subs if yes
569 */
570 function bbp_forum_get_subforums( $args = '' ) {
571
572 // Use passed integer as post_parent
573 if ( is_numeric( $args ) )
574 $args = array( 'post_parent' => $args );
575
576 // Setup possible post__not_in array
577 $post_stati[] = 'publish';
578
579 // Super admin get whitelisted post statuses
580 if ( is_super_admin() ) {
581 $post_stati = array( 'publish', 'private', 'hidden' );
582
583 // Not a super admin, so check caps
584 } else {
585
586 // Check if user can read private forums
587 if ( current_user_can( 'read_private_forums' ) )
588 $post_stati[] = 'private';
589
590 // Check if user can read hidden forums
591 if ( current_user_can( 'read_hidden_forums' ) )
592 $post_stati[] = 'hidden';
593 }
594
595 $default = array(
596 'post_parent' => 0,
597 'post_type' => bbp_get_forum_post_type(),
598 'post_status' => implode( ',', $post_stati ),
599 'posts_per_page' => get_option( '_bbp_forums_per_page', 15 ),
600 'sort_column' => 'menu_order, post_title',
601 'order' => 'ASC'
602 );
603
604 $r = wp_parse_args( $args, $default );
605 $r['post_parent'] = bbp_get_forum_id( $r['post_parent'] );
606
607 // No forum passed
608 $sub_forums = !empty( $r['post_parent'] ) ? get_posts( $r ) : '';
609
610 return apply_filters( 'bbp_forum_get_sub_forums', (array) $sub_forums, $args );
611 }
612
613 /**
614 * Output a list of forums (can be used to list subforums)
615 *
616 * @param mixed $args The function supports these args:
617 * - before: To put before the output. Defaults to '<ul class="bbp-forums">'
618 * - after: To put after the output. Defaults to '</ul>'
619 * - link_before: To put before every link. Defaults to '<li class="bbp-forum">'
620 * - link_after: To put after every link. Defaults to '</li>'
621 * - separator: Separator. Defaults to ', '
622 * - forum_id: Forum id. Defaults to ''
623 * - show_topic_count - To show forum topic count or not. Defaults to true
624 * - show_reply_count - To show forum reply count or not. Defaults to true
625 * @uses bbp_forum_get_subforums() To check if the forum has subforums or not
626 * @uses bbp_get_forum_permalink() To get forum permalink
627 * @uses bbp_get_forum_title() To get forum title
628 * @uses bbp_is_forum_category() To check if a forum is a category
629 * @uses bbp_get_forum_topic_count() To get forum topic count
630 * @uses bbp_get_forum_reply_count() To get forum reply count
631 */
632 function bbp_list_forums( $args = '' ) {
633 global $bbp;
634
635 // Define used variables
636 $output = $sub_forums = $topic_count = $reply_count = $counts = '';
637 $i = 0;
638 $count = array();
639
640 // Defaults and arguments
641 $defaults = array (
642 'before' => '<ul class="bbp-forums">',
643 'after' => '</ul>',
644 'link_before' => '<li class="bbp-forum">',
645 'link_after' => '</li>',
646 'count_before' => ' (',
647 'count_after' => ')',
648 'count_sep' => ', ',
649 'separator' => ', ',
650 'forum_id' => '',
651 'show_topic_count' => true,
652 'show_reply_count' => true,
653 );
654 $r = wp_parse_args( $args, $defaults );
655 extract( $r, EXTR_SKIP );
656
657 // Bail if there are no subforums
658 if ( !bbp_get_forum_subforum_count( $forum_id ) )
659 return;
660
661 // Loop through forums and create a list
662 if ( $sub_forums = bbp_forum_get_subforums( $forum_id ) ) {
663
664 // Total count (for separator)
665 $total_subs = count( $sub_forums );
666 foreach ( $sub_forums as $sub_forum ) {
667 $i++; // Separator count
668
669 // Get forum details
670 $count = array();
671 $show_sep = $total_subs > $i ? $separator : '';
672 $permalink = bbp_get_forum_permalink( $sub_forum->ID );
673 $title = bbp_get_forum_title( $sub_forum->ID );
674
675 // Show topic count
676 if ( !empty( $show_topic_count ) && !bbp_is_forum_category( $sub_forum->ID ) )
677 $count['topic'] = bbp_get_forum_topic_count( $sub_forum->ID );
678
679 // Show reply count
680 if ( !empty( $show_reply_count ) && !bbp_is_forum_category( $sub_forum->ID ) )
681 $count['reply'] = bbp_get_forum_reply_count( $sub_forum->ID );
682
683 // Counts to show
684 if ( !empty( $count ) )
685 $counts = $count_before . implode( $count_sep, $count ) . $count_after;
686
687 // Build this sub forums link
688 $output .= $link_before . '<a href="' . $permalink . '" class="bbp-forum-link">' . $title . $counts . '</a>' . $show_sep . $link_after;
689 }
690
691 // Output the list
692 echo $before . $output . $after;
693 }
694 }
695
696 /** Forum Last Topic **********************************************************/
697
698 /**
699 * Output the forum's last topic id
700 *
701 * @since bbPress (r2464)
702 *
703 * @uses bbp_get_forum_last_topic_id() To get the forum's last topic id
704 * @param int $forum_id Optional. Forum id
705 */
706 function bbp_forum_last_topic_id( $forum_id = 0 ) {
707 echo bbp_get_forum_last_topic_id( $forum_id );
708 }
709 /**
710 * Return the forum's last topic id
711 *
712 * @since bbPress (r2464)
713 *
714 * @param int $forum_id Optional. Forum id
715 * @uses bbp_get_forum_id() To get the forum id
716 * @uses get_post_meta() To get the forum's last topic id
717 * @uses apply_filters() Calls 'bbp_get_forum_last_topic_id' with the
718 * forum and topic id
719 * @return int Forum's last topic id
720 */
721 function bbp_get_forum_last_topic_id( $forum_id = 0 ) {
722 $forum_id = bbp_get_forum_id( $forum_id );
723 $topic_id = get_post_meta( $forum_id, '_bbp_last_topic_id', true );
724
725 return apply_filters( 'bbp_get_forum_last_topic_id', (int) $topic_id, $forum_id );
726 }
727
728 /**
729 * Output the title of the last topic inside a forum
730 *
731 * @since bbPress (r2625)
732 *
733 * @param int $forum_id Optional. Forum id
734 * @uses bbp_get_forum_last_topic_title() To get the forum's last topic's title
735 */
736 function bbp_forum_last_topic_title( $forum_id = 0 ) {
737 echo bbp_get_forum_last_topic_title( $forum_id );
738 }
739 /**
740 * Return the title of the last topic inside a forum
741 *
742 * @since bbPress (r2625)
743 *
744 * @param int $forum_id Optional. Forum id
745 * @uses bbp_get_forum_id() To get the forum id
746 * @uses bbp_get_forum_last_topic_id() To get the forum's last topic id
747 * @uses bbp_get_topic_title() To get the topic's title
748 * @uses apply_filters() Calls 'bbp_get_forum_last_topic_title' with the
749 * topic title and forum id
750 * @return string Forum's last topic's title
751 */
752 function bbp_get_forum_last_topic_title( $forum_id = 0 ) {
753 $forum_id = bbp_get_forum_id( $forum_id );
754 return apply_filters( 'bbp_get_forum_last_topic_title', bbp_get_topic_title( bbp_get_forum_last_topic_id( $forum_id ) ), $forum_id );
755 }
756
757 /**
758 * Output the link to the last topic in a forum
759 *
760 * @since bbPress (r2464)
761 *
762 * @param int $forum_id Optional. Forum id
763 * @uses bbp_get_forum_last_topic_permalink() To get the forum's last topic's
764 * permanent link
765 */
766 function bbp_forum_last_topic_permalink( $forum_id = 0 ) {
767 echo bbp_get_forum_last_topic_permalink( $forum_id );
768 }
769 /**
770 * Return the link to the last topic in a forum
771 *
772 * @since bbPress (r2464)
773 *
774 * @param int $forum_id Optional. Forum id
775 * @uses bbp_get_forum_id() To get the forum id
776 * @uses bbp_get_forum_last_topic_id() To get the forum's last topic id
777 * @uses bbp_get_topic_permalink() To get the topic's permalink
778 * @uses apply_filters() Calls 'bbp_get_forum_last_topic_permalink' with
779 * the topic link and forum id
780 * @return string Permanent link to topic
781 */
782 function bbp_get_forum_last_topic_permalink( $forum_id = 0 ) {
783 $forum_id = bbp_get_forum_id( $forum_id );
784 return apply_filters( 'bbp_get_forum_last_topic_permalink', bbp_get_topic_permalink( bbp_get_forum_last_topic_id( $forum_id ) ), $forum_id );
785 }
786
787 /**
788 * Return the author ID of the last topic of a forum
789 *
790 * @since bbPress (r2625)
791 *
792 * @param int $forum_id Optional. Forum id
793 * @uses bbp_get_forum_id() To get the forum id
794 * @uses bbp_get_forum_last_topic_id() To get the forum's last topic id
795 * @uses bbp_get_topic_author_id() To get the topic's author id
796 * @uses apply_filters() Calls 'bbp_get_forum_last_topic_author' with the author
797 * id and forum id
798 * @return int Forum's last topic's author id
799 */
800 function bbp_get_forum_last_topic_author_id( $forum_id = 0 ) {
801 $forum_id = bbp_get_forum_id( $forum_id );
802 $author_id = bbp_get_topic_author_id( bbp_get_forum_last_topic_id( $forum_id ) );
803 return apply_filters( 'bbp_get_forum_last_topic_author_id', (int) $author_id, $forum_id );
804 }
805
806 /**
807 * Output link to author of last topic of forum
808 *
809 * @since bbPress (r2625)
810 *
811 * @param int $forum_id Optional. Forum id
812 * @uses bbp_get_forum_last_topic_author_link() To get the forum's last topic's
813 * author link
814 */
815 function bbp_forum_last_topic_author_link( $forum_id = 0 ) {
816 echo bbp_get_forum_last_topic_author_link( $forum_id );
817 }
818 /**
819 * Return link to author of last topic of forum
820 *
821 * @since bbPress (r2625)
822 *
823 * @param int $forum_id Optional. Forum id
824 * @uses bbp_get_forum_id() To get the forum id
825 * @uses bbp_get_forum_last_topic_author_id() To get the forum's last
826 * topic's author id
827 * @uses bbp_get_user_profile_link() To get the author's profile link
828 * @uses apply_filters() Calls 'bbp_get_forum_last_topic_author_link'
829 * with the author link and forum id
830 * @return string Forum's last topic's author link
831 */
832 function bbp_get_forum_last_topic_author_link( $forum_id = 0 ) {
833 $forum_id = bbp_get_forum_id( $forum_id );
834 $author_id = bbp_get_forum_last_topic_author_id( $forum_id );
835 $author_link = bbp_get_user_profile_link( $author_id );
836 return apply_filters( 'bbp_get_forum_last_topic_author_link', $author_link, $forum_id );
837 }
838
839 /** Forum Last Reply **********************************************************/
840
841 /**
842 * Output the forums last reply id
843 *
844 * @since bbPress (r2464)
845 *
846 * @uses bbp_get_forum_last_reply_id() To get the forum's last reply id
847 * @param int $forum_id Optional. Forum id
848 */
849 function bbp_forum_last_reply_id( $forum_id = 0 ) {
850 echo bbp_get_forum_last_reply_id( $forum_id );
851 }
852 /**
853 * Return the forums last reply id
854 *
855 * @since bbPress (r2464)
856 *
857 * @param int $forum_id Optional. Forum id
858 * @uses bbp_get_forum_id() To get the forum id
859 * @uses get_post_meta() To get the forum's last reply id
860 * @uses bbp_get_forum_last_topic_id() To get the forum's last topic id
861 * @uses apply_filters() Calls 'bbp_get_forum_last_reply_id' with
862 * the last reply id and forum id
863 * @return int Forum's last reply id
864 */
865 function bbp_get_forum_last_reply_id( $forum_id = 0 ) {
866 $forum_id = bbp_get_forum_id( $forum_id );
867 $reply_id = get_post_meta( $forum_id, '_bbp_last_reply_id', true );
868
869 if ( empty( $reply_id ) )
870 $reply_id = bbp_get_forum_last_topic_id( $forum_id );
871
872 return apply_filters( 'bbp_get_forum_last_reply_id', (int) $reply_id, $forum_id );
873 }
874
875 /**
876 * Output the title of the last reply inside a forum
877 *
878 * @param int $forum_id Optional. Forum id
879 * @uses bbp_get_forum_last_reply_title() To get the forum's last reply's title
880 */
881 function bbp_forum_last_reply_title( $forum_id = 0 ) {
882 echo bbp_get_forum_last_reply_title( $forum_id );
883 }
884 /**
885 * Return the title of the last reply inside a forum
886 *
887 * @param int $forum_id Optional. Forum id
888 * @uses bbp_get_forum_id() To get the forum id
889 * @uses bbp_get_forum_last_reply_id() To get the forum's last reply id
890 * @uses bbp_get_reply_title() To get the reply title
891 * @uses apply_filters() Calls 'bbp_get_forum_last_reply_title' with the
892 * reply title and forum id
893 * @return string
894 */
895 function bbp_get_forum_last_reply_title( $forum_id = 0 ) {
896 $forum_id = bbp_get_forum_id( $forum_id );
897 return apply_filters( 'bbp_get_forum_last_reply_title', bbp_get_reply_title( bbp_get_forum_last_reply_id( $forum_id ) ), $forum_id );
898 }
899
900 /**
901 * Output the link to the last reply in a forum
902 *
903 * @since bbPress (r2464)
904 *
905 * @param int $forum_id Optional. Forum id
906 * @uses bbp_get_forum_last_reply_permalink() To get the forum last reply link
907 */
908 function bbp_forum_last_reply_permalink( $forum_id = 0 ) {
909 echo bbp_get_forum_last_reply_permalink( $forum_id );
910 }
911 /**
912 * Return the link to the last reply in a forum
913 *
914 * @since bbPress (r2464)
915 *
916 * @param int $forum_id Optional. Forum id
917 * @uses bbp_get_forum_id() To get the forum id
918 * @uses bbp_get_forum_last_reply_id() To get the forum's last reply id
919 * @uses bbp_get_reply_permalink() To get the reply permalink
920 * @uses apply_filters() Calls 'bbp_get_forum_last_reply_permalink' with
921 * the reply link and forum id
922 * @return string Permanent link to the forum's last reply
923 */
924 function bbp_get_forum_last_reply_permalink( $forum_id = 0 ) {
925 $forum_id = bbp_get_forum_id( $forum_id );
926 return apply_filters( 'bbp_get_forum_last_reply_permalink', bbp_get_reply_permalink( bbp_get_forum_last_reply_id( $forum_id ) ), $forum_id );
927 }
928
929 /**
930 * Output the url to the last reply in a forum
931 *
932 * @since bbPress (r2683)
933 *
934 * @param int $forum_id Optional. Forum id
935 * @uses bbp_get_forum_last_reply_url() To get the forum last reply url
936 */
937 function bbp_forum_last_reply_url( $forum_id = 0 ) {
938 echo bbp_get_forum_last_reply_url( $forum_id );
939 }
940 /**
941 * Return the url to the last reply in a forum
942 *
943 * @since bbPress (r2683)
944 *
945 * @param int $forum_id Optional. Forum id
946 * @uses bbp_get_forum_id() To get the forum id
947 * @uses bbp_get_forum_last_reply_id() To get the forum's last reply id
948 * @uses bbp_get_reply_url() To get the reply url
949 * @uses bbp_get_forum_last_topic_permalink() To get the forum's last
950 * topic's permalink
951 * @uses apply_filters() Calls 'bbp_get_forum_last_reply_url' with the
952 * reply url and forum id
953 * @return string Paginated URL to latest reply
954 */
955 function bbp_get_forum_last_reply_url( $forum_id = 0 ) {
956 $forum_id = bbp_get_forum_id( $forum_id );
957
958 // If forum has replies, get the last reply and use its url
959 if ( $reply_id = bbp_get_forum_last_reply_id( $forum_id ) ) {
960 $reply_url = bbp_get_reply_url( $reply_id );
961
962 // No replies, so look for topics and use last permalink
963 } else {
964 if ( !$reply_url = bbp_get_forum_last_topic_permalink( $forum_id ) ) {
965 // No topics either, so set $reply_url as empty
966 $reply_url = '';
967 }
968 }
969
970 // Filter and return
971 return apply_filters( 'bbp_get_forum_last_reply_url', $reply_url, $forum_id );
972 }
973
974 /**
975 * Output author ID of last reply of forum
976 *
977 * @since bbPress (r2625)
978 *
979 * @param int $forum_id Optional. Forum id
980 * @uses bbp_get_forum_last_reply_author_id() To get the forum's last reply
981 * author id
982 */
983 function bbp_forum_last_reply_author_id( $forum_id = 0 ) {
984 echo bbp_get_forum_last_reply_author_id( $forum_id );
985 }
986 /**
987 * Return author ID of last reply of forum
988 *
989 * @since bbPress (r2625)
990 *
991 * @param int $forum_id Optional. Forum id
992 * @uses bbp_get_forum_id() To get the forum id
993 * @uses bbp_get_forum_last_reply_author_id() To get the forum's last
994 * reply's author id
995 * @uses bbp_get_reply_author_id() To get the reply's author id
996 * @uses apply_filters() Calls 'bbp_get_forum_last_reply_author_id' with
997 * the author id and forum id
998 * @return int Forum's last reply author id
999 */
1000 function bbp_get_forum_last_reply_author_id( $forum_id = 0 ) {
1001 $forum_id = bbp_get_forum_id( $forum_id );
1002 $author_id = bbp_get_reply_author_id( bbp_get_forum_last_reply_id( $forum_id ) );
1003 return apply_filters( 'bbp_get_forum_last_reply_author_id', $author_id, $forum_id );
1004 }
1005
1006 /**
1007 * Output link to author of last reply of forum
1008 *
1009 * @since bbPress (r2625)
1010 *
1011 * @param int $forum_id Optional. Forum id
1012 * @uses bbp_get_forum_last_reply_author_link() To get the forum's last reply's
1013 * author link
1014 */
1015 function bbp_forum_last_reply_author_link( $forum_id = 0 ) {
1016 echo bbp_get_forum_last_reply_author_link( $forum_id );
1017 }
1018 /**
1019 * Return link to author of last reply of forum
1020 *
1021 * @since bbPress (r2625)
1022 *
1023 * @param int $forum_id Optional. Forum id
1024 * @uses bbp_get_forum_id() To get the forum id
1025 * @uses bbp_get_forum_last_reply_author_id() To get the forum's last
1026 * reply's author id
1027 * @uses bbp_get_user_profile_link() To get the reply's author's profile
1028 * link
1029 * @uses apply_filters() Calls 'bbp_get_forum_last_reply_author_link'
1030 * with the author link and forum id
1031 * @return string Link to author of last reply of forum
1032 */
1033 function bbp_get_forum_last_reply_author_link( $forum_id = 0 ) {
1034 $forum_id = bbp_get_forum_id( $forum_id );
1035 $author_id = bbp_get_forum_last_reply_author_id( $forum_id );
1036 $author_link = bbp_get_user_profile_link( $author_id );
1037 return apply_filters( 'bbp_get_forum_last_reply_author_link', $author_link, $forum_id );
1038 }
1039
1040 /** Forum Counts **************************************************************/
1041
1042 /**
1043 * Output the topics link of the forum
1044 *
1045 * @since bbPress (r2883)
1046 *
1047 * @param int $forum_id Optional. Topic id
1048 * @uses bbp_get_forum_topics_link() To get the forum topics link
1049 */
1050 function bbp_forum_topics_link( $forum_id = 0 ) {
1051 echo bbp_get_forum_topics_link( $forum_id );
1052 }
1053
1054 /**
1055 * Return the topics link of the forum
1056 *
1057 * @since bbPress (r2883)
1058 *
1059 * @param int $forum_id Optional. Topic id
1060 * @uses bbp_get_forum_id() To get the forum id
1061 * @uses bbp_get_forum() To get the forum
1062 * @uses bbp_get_forum_topic_count() To get the forum topic count
1063 * @uses bbp_get_forum_permalink() To get the forum permalink
1064 * @uses remove_query_arg() To remove args from the url
1065 * @uses bbp_get_forum_hidden_topic_count() To get the forum hidden
1066 * topic count
1067 * @uses current_user_can() To check if the current user can edit others
1068 * topics
1069 * @uses add_query_arg() To add custom args to the url
1070 * @uses apply_filters() Calls 'bbp_get_forum_topics_link' with the
1071 * topics link and forum id
1072 */
1073 function bbp_get_forum_topics_link( $forum_id = 0 ) {
1074 global $bbp;
1075
1076 $forum = bbp_get_forum( bbp_get_forum_id( (int) $forum_id ) );
1077 $forum_id = $forum->ID;
1078 $topics = bbp_get_forum_topic_count( $forum_id );
1079 $topics = sprintf( _n( '%s topic', '%s topics', $topics, 'bbpress' ), $topics );
1080 $retval = '';
1081
1082 if ( !empty( $_GET['view'] ) && 'all' == $_GET['view'] && current_user_can( 'edit_others_topics' ) )
1083 $retval .= "<a href='" . esc_url( remove_query_arg( array( 'view' => 'all' ), bbp_get_forum_permalink( $forum_id ) ) ) . "'>$topics</a>";
1084 else
1085 $retval .= $topics;
1086
1087 if ( current_user_can( 'edit_others_topics' ) && $deleted = bbp_get_forum_hidden_topic_count( $forum_id ) ) {
1088 $extra = sprintf( __( ' + %d more', 'bbpress' ), $deleted );
1089 if ( !empty( $_GET['view'] ) && 'all' == $_GET['view'] )
1090 $retval .= " $extra";
1091 else
1092 $retval .= " <a href='" . esc_url( add_query_arg( array( 'view' => 'all' ) ) ) . "'>$extra</a>";
1093 }
1094
1095 return apply_filters( 'bbp_get_forum_topics_link', $retval, $forum_id );
1096 }
1097
1098 /**
1099 * Output total sub-forum count of a forum
1100 *
1101 * @since bbPress (r2464)
1102 *
1103 * @uses bbp_get_forum_subforum_count() To get the forum's subforum count
1104 * @param int $forum_id Optional. Forum id to check
1105 */
1106 function bbp_forum_subforum_count( $forum_id = 0 ) {
1107 echo bbp_get_forum_subforum_count( $forum_id );
1108 }
1109 /**
1110 * Return total subforum count of a forum
1111 *
1112 * @since bbPress (r2464)
1113 *
1114 * @param int $forum_id Optional. Forum id
1115 * @uses bbp_get_forum_id() To get the forum id
1116 * @uses get_post_meta() To get the subforum count
1117 * @uses apply_filters() Calls 'bbp_get_forum_subforum_count' with the
1118 * subforum count and forum id
1119 * @return int Forum's subforum count
1120 */
1121 function bbp_get_forum_subforum_count( $forum_id = 0 ) {
1122 $forum_id = bbp_get_forum_id( $forum_id );
1123 $forum_count = get_post_meta( $forum_id, '_bbp_forum_subforum_count', true );
1124
1125 return apply_filters( 'bbp_get_forum_subforum_count', (int) $forum_count, $forum_id );
1126 }
1127
1128 /**
1129 * Output total topic count of a forum
1130 *
1131 * @since bbPress (r2464)
1132 *
1133 * @param int $forum_id Optional. Forum id
1134 * @param bool $total_count Optional. To get the total count or normal count?
1135 * @uses bbp_get_forum_topic_count() To get the forum topic count
1136 */
1137 function bbp_forum_topic_count( $forum_id = 0, $total_count = true ) {
1138 echo bbp_get_forum_topic_count( $forum_id );
1139 }
1140 /**
1141 * Return total topic count of a forum
1142 *
1143 * @since bbPress (r2464)
1144 *
1145 * @param int $forum_id Optional. Forum id
1146 * @param bool $total_count Optional. To get the total count or normal
1147 * count? Defaults to total.
1148 * @uses bbp_get_forum_id() To get the forum id
1149 * @uses get_post_meta() To get the forum topic count
1150 * @uses apply_filters() Calls 'bbp_get_forum_topic_count' with the
1151 * topic count and forum id
1152 * @return int Forum topic count
1153 */
1154 function bbp_get_forum_topic_count( $forum_id = 0, $total_count = true ) {
1155 $forum_id = bbp_get_forum_id( $forum_id );
1156 $topics = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_forum_topic_count' : '_bbp_total_topic_count', true );
1157
1158 return apply_filters( 'bbp_get_forum_topic_count', (int) $topics, $forum_id );
1159 }
1160
1161 /**
1162 * Output total reply count of a forum
1163 *
1164 * @since bbPress (r2464)
1165 *
1166 * @param int $forum_id Optional. Forum id
1167 * @param bool $total_count Optional. To get the total count or normal count?
1168 * @uses bbp_get_forum_reply_count() To get the forum reply count
1169 */
1170 function bbp_forum_reply_count( $forum_id = 0, $total_count = true ) {
1171 echo bbp_get_forum_reply_count( $forum_id, $total_count );
1172 }
1173 /**
1174 * Return total post count of a forum
1175 *
1176 * @since bbPress (r2464)
1177 *
1178 * @param int $forum_id Optional. Forum id
1179 * @param bool $total_count Optional. To get the total count or normal
1180 * count?
1181 * @uses bbp_get_forum_id() To get the forum id
1182 * @uses get_post_meta() To get the forum reply count
1183 * @uses apply_filters() Calls 'bbp_get_forum_reply_count' with the
1184 * reply count and forum id
1185 * @return int Forum reply count
1186 */
1187 function bbp_get_forum_reply_count( $forum_id = 0, $total_count = true ) {
1188 $forum_id = bbp_get_forum_id( $forum_id );
1189 $replies = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_reply_count' : '_bbp_total_reply_count', true );
1190
1191 return apply_filters( 'bbp_get_forum_reply_count', (int) $replies, $forum_id );
1192 }
1193
1194 /**
1195 * Output total post count of a forum
1196 *
1197 * @since bbPress (r2954)
1198 *
1199 * @param int $forum_id Optional. Forum id
1200 * @param bool $total_count Optional. To get the total count or normal count?
1201 * @uses bbp_get_forum_post_count() To get the forum post count
1202 */
1203 function bbp_forum_post_count( $forum_id = 0, $total_count = true ) {
1204 echo bbp_get_forum_post_count( $forum_id, $total_count );
1205 }
1206 /**
1207 * Return total post count of a forum
1208 *
1209 * @since bbPress (r2954)
1210 *
1211 * @param int $forum_id Optional. Forum id
1212 * @param bool $total_count Optional. To get the total count or normal
1213 * count?
1214 * @uses bbp_get_forum_id() To get the forum id
1215 * @uses get_post_meta() To get the forum post count
1216 * @uses apply_filters() Calls 'bbp_get_forum_post_count' with the
1217 * post count and forum id
1218 * @return int Forum post count
1219 */
1220 function bbp_get_forum_post_count( $forum_id = 0, $total_count = true ) {
1221 $forum_id = bbp_get_forum_id( $forum_id );
1222 $topics = bbp_get_forum_topic_count( $forum_id, $total_count );
1223 $replies = get_post_meta( $forum_id, empty( $total_count ) ? '_bbp_reply_count' : '_bbp_total_reply_count', true );
1224
1225 return apply_filters( 'bbp_get_forum_post_count', (int) $replies + (int) $topics, $forum_id );
1226 }
1227
1228 /**
1229 * Output total hidden topic count of a forum (hidden includes trashed and
1230 * spammed topics)
1231 *
1232 * @since bbPress (r2883)
1233 *
1234 * @param int $forum_id Optional. Topic id
1235 * @uses bbp_get_forum_hidden_topic_count() To get the forum hidden topic count
1236 */
1237 function bbp_forum_hidden_topic_count( $forum_id = 0 ) {
1238 echo bbp_get_forum_hidden_topic_count( $forum_id );
1239 }
1240 /**
1241 * Return total hidden topic count of a forum (hidden includes trashed
1242 * and spammed topics)
1243 *
1244 * @since bbPress (r2883)
1245 *
1246 * @param int $forum_id Optional. Topic id
1247 * @uses bbp_get_forum_id() To get the forum id
1248 * @uses get_post_meta() To get the hidden topic count
1249 * @uses apply_filters() Calls 'bbp_get_forum_hidden_topic_count' with
1250 * the hidden topic count and forum id
1251 * @return int Topic hidden topic count
1252 */
1253 function bbp_get_forum_hidden_topic_count( $forum_id = 0 ) {
1254 $forum_id = bbp_get_forum_id( $forum_id );
1255 $topics = get_post_meta( $forum_id, '_bbp_topic_count_hidden', true );
1256
1257 return apply_filters( 'bbp_get_forum_hidden_topic_count', (int) $topics, $forum_id );
1258 }
1259
1260 /**
1261 * Output the status of the forum
1262 *
1263 * @since bbPress (r2667)
1264 *
1265 * @param int $forum_id Optional. Forum id
1266 * @uses bbp_get_forum_status() To get the forum status
1267 */
1268 function bbp_forum_status( $forum_id = 0 ) {
1269 echo bbp_get_forum_status( $forum_id );
1270 }
1271 /**
1272 * Return the status of the forum
1273 *
1274 * @since bbPress (r2667)
1275 *
1276 * @param int $forum_id Optional. Forum id
1277 * @uses bbp_get_forum_id() To get the forum id
1278 * @uses get_post_status() To get the forum's status
1279 * @uses apply_filters() Calls 'bbp_get_forum_status' with the status
1280 * and forum id
1281 * @return string Status of forum
1282 */
1283 function bbp_get_forum_status( $forum_id = 0 ) {
1284 $forum_id = bbp_get_forum_id( $forum_id );
1285
1286 return apply_filters( 'bbp_get_forum_status', get_post_meta( $forum_id, '_bbp_status', true ), $forum_id );
1287 }
1288
1289 /**
1290 * Output the visibility of the forum
1291 *
1292 * @since bbPress (r2997)
1293 *
1294 * @param int $forum_id Optional. Forum id
1295 * @uses bbp_get_forum_visibility() To get the forum visibility
1296 */
1297 function bbp_forum_visibility( $forum_id = 0 ) {
1298 echo bbp_get_forum_visibility( $forum_id );
1299 }
1300 /**
1301 * Return the visibility of the forum
1302 *
1303 * @since bbPress (r2997)
1304 *
1305 * @param int $forum_id Optional. Forum id
1306 * @uses bbp_get_forum_id() To get the forum id
1307 * @uses get_post_visibility() To get the forum's visibility
1308 * @uses apply_filters() Calls 'bbp_get_forum_visibility' with the visibility
1309 * and forum id
1310 * @return string Status of forum
1311 */
1312 function bbp_get_forum_visibility( $forum_id = 0 ) {
1313 $forum_id = bbp_get_forum_id( $forum_id );
1314
1315 return apply_filters( 'bbp_get_forum_visibility', get_post_status( $forum_id ), $forum_id );
1316 }
1317
1318 /**
1319 * Is the forum a category?
1320 *
1321 * @since bbPress (r2746)
1322 *
1323 * @param int $forum_id Optional. Forum id
1324 * @uses get_post_meta() To get the forum category meta
1325 * @return bool Whether the forum is a category or not
1326 */
1327 function bbp_is_forum_category( $forum_id = 0 ) {
1328 $forum_id = bbp_get_forum_id( $forum_id );
1329 $type = get_post_meta( $forum_id, '_bbp_forum_type', true );
1330 $retval = ( !empty( $type ) && 'category' == $type );
1331
1332 return apply_filters( 'bbp_is_forum_category', (bool) $retval, $forum_id );
1333 }
1334
1335 /**
1336 * Is the forum open?
1337 *
1338 * @since bbPress (r2746)
1339 * @param int $forum_id Optional. Forum id
1340 *
1341 * @param int $forum_id Optional. Forum id
1342 * @uses bbp_is_forum_closed() To check if the forum is closed or not
1343 * @return bool Whether the forum is open or not
1344 */
1345 function bbp_is_forum_open( $forum_id = 0 ) {
1346 return !bbp_is_forum_closed( $forum_id );
1347 }
1348
1349 /**
1350 * Is the forum closed?
1351 *
1352 * @since bbPress (r2746)
1353 *
1354 * @param int $forum_id Optional. Forum id
1355 * @param bool $check_ancestors Check if the ancestors are closed (only
1356 * if they're a category)
1357 * @uses bbp_get_forum_status() To get the forum status
1358 * @uses bbp_get_forum_ancestors() To get the forum ancestors
1359 * @uses bbp_is_forum_category() To check if the forum is a category
1360 * @uses bbp_is_forum_closed() To check if the forum is closed
1361 * @return bool True if closed, false if not
1362 */
1363 function bbp_is_forum_closed( $forum_id = 0, $check_ancestors = true ) {
1364 global $bbp;
1365
1366 $forum_id = bbp_get_forum_id( $forum_id );
1367 $retval = ( $bbp->closed_status_id == bbp_get_forum_status( $forum_id ) );
1368
1369 if ( !empty( $check_ancestors ) ) {
1370 $ancestors = bbp_get_forum_ancestors( $forum_id );
1371
1372 foreach ( (array) $ancestors as $ancestor ) {
1373 if ( bbp_is_forum_category( $ancestor, false ) && bbp_is_forum_closed( $ancestor, false ) ) {
1374 $retval = true;
1375 }
1376 }
1377 }
1378
1379 return apply_filters( 'bbp_is_forum_closed', (bool) $retval, $forum_id, $check_ancestors );
1380 }
1381
1382 /**
1383 * Is the forum public?
1384 *
1385 * @since bbPress (r2997)
1386 *
1387 * @param int $forum_id Optional. Forum id
1388 * @param bool $check_ancestors Check if the ancestors are public (only if
1389 * they're a category)
1390 * @uses get_post_meta() To get the forum public meta
1391 * @uses bbp_get_forum_ancestors() To get the forum ancestors
1392 * @uses bbp_is_forum_category() To check if the forum is a category
1393 * @uses bbp_is_forum_closed() To check if the forum is closed
1394 * @return bool True if closed, false if not
1395 */
1396 function bbp_is_forum_public( $forum_id = 0, $check_ancestors = true ) {
1397 global $bbp;
1398
1399 $forum_id = bbp_get_forum_id( $forum_id );
1400 $visibility = bbp_get_forum_visibility( $forum_id );
1401
1402 // If post status is public, return true
1403 $retval = ( 'publish' == $visibility );
1404
1405 // Check ancestors and inherit their privacy setting for display
1406 if ( !empty( $check_ancestors ) ) {
1407 $ancestors = bbp_get_forum_ancestors( $forum_id );
1408
1409 foreach ( (array) $ancestors as $ancestor ) {
1410 if ( bbp_is_forum( $ancestor ) && bbp_is_forum_public( $ancestor, false ) ) {
1411 $retval = true;
1412 }
1413 }
1414 }
1415
1416 return apply_filters( 'bbp_is_forum_public', (bool) $retval, $forum_id, $check_ancestors );
1417 }
1418
1419 /**
1420 * Is the forum private?
1421 *
1422 * @since bbPress (r2746)
1423 *
1424 * @param int $forum_id Optional. Forum id
1425 * @param bool $check_ancestors Check if the ancestors are private (only if
1426 * they're a category)
1427 * @uses get_post_meta() To get the forum private meta
1428 * @uses bbp_get_forum_ancestors() To get the forum ancestors
1429 * @uses bbp_is_forum_category() To check if the forum is a category
1430 * @uses bbp_is_forum_closed() To check if the forum is closed
1431 * @return bool True if closed, false if not
1432 */
1433 function bbp_is_forum_private( $forum_id = 0, $check_ancestors = true ) {
1434 global $bbp;
1435
1436 $forum_id = bbp_get_forum_id( $forum_id );
1437 $visibility = bbp_get_forum_visibility( $forum_id );
1438
1439 // If post status is private, return true
1440 $retval = ( 'private' == $visibility );
1441
1442 // Check ancestors and inherit their privacy setting for display
1443 if ( !empty( $check_ancestors ) ) {
1444 $ancestors = bbp_get_forum_ancestors( $forum_id );
1445
1446 foreach ( (array) $ancestors as $ancestor ) {
1447 if ( bbp_is_forum( $ancestor ) && bbp_is_forum_private( $ancestor, false ) ) {
1448 $retval = true;
1449 }
1450 }
1451 }
1452
1453 return apply_filters( 'bbp_is_forum_private', (bool) $retval, $forum_id, $check_ancestors );
1454 }
1455
1456 /**
1457 * Is the forum hidden?
1458 *
1459 * @since bbPress (r2997)
1460 *
1461 * @param int $forum_id Optional. Forum id
1462 * @param bool $check_ancestors Check if the ancestors are private (only if
1463 * they're a category)
1464 * @uses get_post_meta() To get the forum private meta
1465 * @uses bbp_get_forum_ancestors() To get the forum ancestors
1466 * @uses bbp_is_forum_category() To check if the forum is a category
1467 * @uses bbp_is_forum_closed() To check if the forum is closed
1468 * @return bool True if closed, false if not
1469 */
1470 function bbp_is_forum_hidden( $forum_id = 0, $check_ancestors = true ) {
1471 global $bbp;
1472
1473 $forum_id = bbp_get_forum_id( $forum_id );
1474 $visibility = bbp_get_forum_visibility( $forum_id );
1475
1476 // If post status is private, return true
1477 $retval = ( 'hidden' == $visibility );
1478
1479 // Check ancestors and inherit their privacy setting for display
1480 if ( !empty( $check_ancestors ) ) {
1481 $ancestors = bbp_get_forum_ancestors( $forum_id );
1482
1483 foreach ( (array) $ancestors as $ancestor ) {
1484 if ( bbp_is_forum( $ancestor ) && bbp_is_forum_hidden( $ancestor, false ) ) {
1485 $retval = true;
1486 }
1487 }
1488 }
1489
1490 return apply_filters( 'bbp_is_forum_hidden', (bool) $retval, $forum_id, $check_ancestors );
1491 }
1492
1493 /**
1494 * Replace forum meta details for users that cannot view them.
1495 *
1496 * @since bbPress (r3162)
1497 *
1498 * @param string $retval
1499 * @param int $forum_id
1500 *
1501 * @uses bbp_is_forum_private()
1502 * @uses current_user_can()
1503 *
1504 * @return string
1505 */
1506 function bbp_suppress_private_forum_meta( $retval, $forum_id ) {
1507 if ( bbp_is_forum_private( $forum_id, false ) && !current_user_can( 'read_private_forums' ) )
1508 $retval = '-';
1509
1510 return apply_filters( 'bbp_suppress_private_forum_meta', $retval );
1511 }
1512
1513 /**
1514 * Replace forum author details for users that cannot view them.
1515 *
1516 * @since bbPress (r3162)
1517 *
1518 * @param string $retval
1519 * @param int $forum_id
1520 *
1521 * @uses bbp_is_forum_private()
1522 * @uses get_post_field()
1523 * @uses bbp_get_topic_post_type()
1524 * @uses bbp_is_forum_private()
1525 * @uses bbp_get_topic_forum_id()
1526 * @uses bbp_get_reply_post_type()
1527 * @uses bbp_get_reply_forum_id()
1528 *
1529 * @return string
1530 */
1531 function bbp_suppress_private_author_link( $author_link, $args ) {
1532
1533 // Assume the author link is the return value
1534 $retval = $author_link;
1535
1536 // Show the normal author link
1537 if ( !empty( $args['post_id'] ) && !current_user_can( 'read_private_forums' ) ) {
1538
1539 // What post type are we looking at?
1540 $post_type = get_post_field( 'post_type', $args['post_id'] );
1541
1542 switch ( $post_type ) {
1543
1544 // Topic
1545 case bbp_get_topic_post_type() :
1546 if ( bbp_is_forum_private( bbp_get_topic_forum_id( $args['post_id'] ) ) )
1547 $retval = '';
1548
1549 break;
1550
1551 // Reply
1552 case bbp_get_reply_post_type() :
1553 if ( bbp_is_forum_private( bbp_get_reply_forum_id( $args['post_id'] ) ) )
1554 $retval = '';
1555
1556 break;
1557
1558 // Post
1559 default :
1560 if ( bbp_is_forum_private( $args['post_id'] ) )
1561 $retval = '';
1562
1563 break;
1564 }
1565 }
1566
1567 return apply_filters( 'bbp_suppress_private_author_link', $retval );
1568 }
1569
1570 /**
1571 * Output the row class of a forum
1572 *
1573 * @since bbPress (r2667)
1574 *
1575 * @uses bbp_get_forum_class() To get the row class of the forum
1576 */
1577 function bbp_forum_class() {
1578 echo bbp_get_forum_class();
1579 }
1580 /**
1581 * Return the row class of a forum
1582 *
1583 * @since bbPress (r2667)
1584 *
1585 * @uses post_class() To get all the classes including ours
1586 * @uses apply_filters() Calls 'bbp_get_forum_class' with the classes
1587 * @return string Row class of the forum
1588 */
1589 function bbp_get_forum_class() {
1590 global $bbp;
1591
1592 $classes = array();
1593 $classes[] = $bbp->forum_query->current_post % 2 ? 'even' : 'odd';
1594 $classes[] = bbp_is_forum_category() ? 'status-category' : '';
1595 $classes[] = bbp_is_forum_private() ? 'status-private' : '';
1596 $classes = array_filter( $classes );
1597
1598 $post = post_class( $classes );
1599
1600 return apply_filters( 'bbp_get_forum_class', $post );
1601 }
1602
1603 /** Single Forum **************************************************************/
1604
1605 /**
1606 * Output a fancy description of the current forum, including total topics,
1607 * total replies, and last activity.
1608 *
1609 * @since bbPress (r2860)
1610 *
1611 * @param array $args Arguments passed to alter output
1612 * @uses bbp_get_single_forum_description() Return the eventual output
1613 */
1614 function bbp_single_forum_description( $args = '' ) {
1615 echo bbp_get_single_forum_description( $args );
1616 }
1617 /**
1618 * Return a fancy description of the current forum, including total
1619 * topics, total replies, and last activity.
1620 *
1621 * @since bbPress (r2860)
1622 *
1623 * @param mixed $args This function supports these arguments:
1624 * - topic_id: Topic id
1625 * - before: Before the text
1626 * - after: After the text
1627 * - size: Size of the avatar
1628 * @uses bbp_get_forum_id() To get the forum id
1629 * @uses bbp_get_forum_topic_count() To get the forum topic count
1630 * @uses bbp_get_forum_reply_count() To get the forum reply count
1631 * @uses bbp_get_forum_subforum_count() To get the forum subforum count
1632 * @uses bbp_get_forum_freshness_link() To get the forum freshness link
1633 * @uses bbp_get_forum_last_active_id() To get the forum last active id
1634 * @uses bbp_get_author_link() To get the author link
1635 * @uses add_filter() To add the 'view all' filter back
1636 * @uses apply_filters() Calls 'bbp_get_single_forum_description' with
1637 * the description and args
1638 * @return string Filtered forum description
1639 */
1640 function bbp_get_single_forum_description( $args = '' ) {
1641 // Default arguments
1642 $defaults = array (
1643 'forum_id' => 0,
1644 'before' => '<div class="bbp-template-notice info"><p class="post-meta description">',
1645 'after' => '</p></div>',
1646 'size' => 14,
1647 'feed' => true
1648 );
1649 $r = wp_parse_args( $args, $defaults );
1650 extract( $r );
1651
1652 // Validate forum_id
1653 $forum_id = bbp_get_forum_id( $forum_id );
1654
1655 // Unhook the 'view all' query var adder
1656 remove_filter( 'bbp_get_forum_permalink', 'bbp_add_view_all' );
1657
1658 // Build the forum description
1659 $topic_count = bbp_get_forum_topics_link ( $forum_id );
1660 $reply_count = bbp_get_forum_reply_count ( $forum_id );
1661 $subforum_count = bbp_get_forum_subforum_count( $forum_id );
1662 $time_since = bbp_get_forum_freshness_link( $forum_id );
1663
1664 // Singlular/Plural
1665 $reply_count = sprintf( _n( '%s reply', '%s replies', $reply_count, 'bbpress' ), $reply_count );
1666
1667 // Forum has posts
1668 if ( $last_reply = bbp_get_forum_last_active_id( $forum_id ) ) {
1669
1670 // Freshness author
1671 $last_updated_by = bbp_get_author_link( array( 'post_id' => $last_reply, 'size' => $size ) );
1672
1673 // Category
1674 if ( bbp_is_forum_category( $forum_id ) )
1675 $retstr = sprintf( __( 'This category contains %1$s and %2$s, and was last updated by %3$s %4$s ago.', 'bbpress' ), $topic_count, $reply_count, $last_updated_by, $time_since );
1676
1677 // Forum
1678 else
1679 $retstr = sprintf( __( 'This forum contains %1$s and %2$s, and was last updated by %3$s %4$s ago.', 'bbpress' ), $topic_count, $reply_count, $last_updated_by, $time_since );
1680
1681 // Forum has no last active data
1682 } else {
1683
1684 // Category
1685 if ( bbp_is_forum_category( $forum_id ) )
1686 $retstr = sprintf( __( 'This category contains %1$s and %2$s.', 'bbpress' ), $topic_count, $reply_count );
1687
1688 // Forum
1689 else
1690 $retstr = sprintf( __( 'This forum contains %1$s and %2$s.', 'bbpress' ), $topic_count, $reply_count );
1691 }
1692
1693 // Add feeds
1694 $feed_links = ( !empty( $feed ) ) ? bbp_get_forum_topics_feed_link ( $forum_id ) . bbp_get_forum_replies_feed_link( $forum_id ) : '';
1695
1696 // Add the 'view all' filter back
1697 add_filter( 'bbp_get_forum_permalink', 'bbp_add_view_all' );
1698
1699 // Combine the elements together
1700 $retstr = $before . $retstr . $after;
1701
1702 // Return filtered result
1703 return apply_filters( 'bbp_get_single_forum_description', $retstr, $args );
1704 }
1705
1706 /** Feeds *********************************************************************/
1707
1708 /**
1709 * Output the link for the forum feed
1710 *
1711 * @since bbPress (r3172)
1712 *
1713 * @param type $forum_id Optional. Forum ID.
1714 *
1715 * @uses bbp_get_forum_topics_feed_link()
1716 */
1717 function bbp_forum_topics_feed_link( $forum_id = 0 ) {
1718 echo bbp_get_forum_topics_feed_link( $forum_id );
1719 }
1720 /**
1721 * Retrieve the link for the forum feed
1722 *
1723 * @since bbPress (r3172)
1724 *
1725 * @param int $forum_id Optional. Forum ID.
1726 *
1727 * @uses bbp_get_forum_id()
1728 * @uses get_option()
1729 * @uses trailingslashit()
1730 * @uses bbp_get_forum_permalink()
1731 * @uses user_trailingslashit()
1732 * @uses bbp_get_forum_post_type()
1733 * @uses get_post_field()
1734 * @uses apply_filters()
1735 *
1736 * @return string
1737 */
1738 function bbp_get_forum_topics_feed_link( $forum_id = 0 ) {
1739
1740 // Validate forum id
1741 $forum_id = bbp_get_forum_id( $forum_id );
1742
1743 // Forum is valid
1744 if ( !empty( $forum_id ) ) {
1745
1746 // Define local variable(s)
1747 $link = '';
1748
1749 // Pretty permalinks
1750 if ( get_option( 'permalink_structure' ) ) {
1751
1752 // Forum link
1753 $url = trailingslashit( bbp_get_forum_permalink( $forum_id ) ) . 'feed';
1754 $url = user_trailingslashit( $url, 'single_feed' );
1755
1756 // Unpretty permalinks
1757 } else {
1758 $url = home_url( add_query_arg( array(
1759 'feed' => 'rss2',
1760 bbp_get_forum_post_type() => get_post_field( 'post_name', $forum_id )
1761 ) ) );
1762 }
1763
1764 $link = '<a href="' . $url . '" class="bbp-forum-rss-link topics"><span>' . __( 'Topics', 'bbpress' ) . '</span></a>';
1765 }
1766
1767 return apply_filters( 'bbp_get_forum_topics_feed_link', $link, $url, $forum_id );
1768 }
1769
1770 /**
1771 * Output the link for the forum replies feed
1772 *
1773 * @since bbPress (r3172)
1774 *
1775 * @param type $forum_id Optional. Forum ID.
1776 *
1777 * @uses bbp_get_forum_replies_feed_link()
1778 */
1779 function bbp_forum_replies_feed_link( $forum_id = 0 ) {
1780 echo bbp_get_forum_replies_feed_link( $forum_id );
1781 }
1782 /**
1783 * Retrieve the link for the forum replies feed
1784 *
1785 * @since bbPress (r3172)
1786 *
1787 * @param int $forum_id Optional. Forum ID.
1788 *
1789 * @uses bbp_get_forum_id()
1790 * @uses get_option()
1791 * @uses trailingslashit()
1792 * @uses bbp_get_forum_permalink()
1793 * @uses user_trailingslashit()
1794 * @uses bbp_get_forum_post_type()
1795 * @uses get_post_field()
1796 * @uses apply_filters()
1797 *
1798 * @return string
1799 */
1800 function bbp_get_forum_replies_feed_link( $forum_id = 0 ) {
1801
1802 // Validate forum id
1803 $forum_id = bbp_get_forum_id( $forum_id );
1804
1805 // Forum is valid
1806 if ( !empty( $forum_id ) ) {
1807
1808 // Define local variable(s)
1809 $link = '';
1810
1811 // Pretty permalinks
1812 if ( get_option( 'permalink_structure' ) ) {
1813
1814 // Forum link
1815 $url = trailingslashit( bbp_get_forum_permalink( $forum_id ) ) . 'feed';
1816 $url = user_trailingslashit( $url, 'single_feed' );
1817 $url = add_query_arg( array( 'type' => 'reply' ), $url );
1818
1819 // Unpretty permalinks
1820 } else {
1821 $url = home_url( add_query_arg( array(
1822 'type' => 'reply',
1823 'feed' => 'rss2',
1824 bbp_get_forum_post_type() => get_post_field( 'post_name', $forum_id )
1825 ) ) );
1826 }
1827
1828 $link = '<a href="' . $url . '" class="bbp-forum-rss-link replies"><span>' . __( 'Replies', 'bbpress' ) . '</span></a>';
1829 }
1830
1831 return apply_filters( 'bbp_get_forum_replies_feed_link', $link, $url, $forum_id );
1832 }
1833
1834 ?>
1835