PluginProbe
bbPress / 2.6.19
bbPress v2.6.19
2.6.19 2.6.18 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 All 74 releases
bbpress / includes / forums / template.php

template.php in bbPress 2.6.19, at includes/forums/template.php

2,892 lines 82.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * bbPress Forum Template Tags
5 *
6 * @package bbPress
7 * @subpackage TemplateTags
8 */
9
10 // Exit if accessed directly
11 defined( 'ABSPATH' ) || exit;
12
13 /** Post Type *****************************************************************/
14
15 /**
16 * Output the unique id of the custom post type for forums
17 *
18 * @since 2.0.0 bbPress (r2858)
19 */
20 function bbp_forum_post_type() {
21 echo bbp_get_forum_post_type();
22 }
23 /**
24 * Return the unique id of the custom post type for forums
25 *
26 * @since 2.0.0 bbPress (r2858)
27 *
28 * @return string The unique forum post type id
29 */
30 function bbp_get_forum_post_type() {
31
32 // Filter & return
33 return apply_filters( 'bbp_get_forum_post_type', bbpress()->forum_post_type );
34 }
35
36
37 /**
38 * Return array of labels used by the forum post type
39 *
40 * @since 2.5.0 bbPress (r5130)
41 *
42 * @return array
43 */
44 function bbp_get_forum_post_type_labels() {
45
46 // Filter & return
47 return (array) apply_filters(
48 'bbp_get_forum_post_type_labels',
49 array(
50 'name' => esc_attr__( 'Forums', 'bbpress' ),
51 'menu_name' => esc_attr__( 'Forums', 'bbpress' ),
52 'singular_name' => esc_attr__( 'Forum', 'bbpress' ),
53 'all_items' => esc_attr__( 'All Forums', 'bbpress' ),
54 'add_new' => esc_attr__( 'Add New', 'bbpress' ),
55 'add_new_item' => esc_attr__( 'Create New Forum', 'bbpress' ),
56 'edit' => esc_attr__( 'Edit', 'bbpress' ),
57 'edit_item' => esc_attr__( 'Edit Forum', 'bbpress' ),
58 'new_item' => esc_attr__( 'New Forum', 'bbpress' ),
59 'view' => esc_attr__( 'View Forum', 'bbpress' ),
60 'view_item' => esc_attr__( 'View Forum', 'bbpress' ),
61 'view_items' => esc_attr__( 'View Forums', 'bbpress' ),
62 'search_items' => esc_attr__( 'Search Forums', 'bbpress' ),
63 'not_found' => esc_attr__( 'No forums found', 'bbpress' ),
64 'not_found_in_trash' => esc_attr__( 'No forums found in Trash', 'bbpress' ),
65 'filter_items_list' => esc_attr__( 'Filter forums list', 'bbpress' ),
66 'items_list' => esc_attr__( 'Forums list', 'bbpress' ),
67 'items_list_navigation' => esc_attr__( 'Forums list navigation', 'bbpress' ),
68 'parent_item_colon' => esc_attr__( 'Parent Forum:', 'bbpress' ),
69 'archives' => esc_attr__( 'Forums', 'bbpress' ),
70 'attributes' => esc_attr__( 'Forum Attributes', 'bbpress' ),
71 'insert_into_item' => esc_attr__( 'Insert into forum', 'bbpress' ),
72 'uploaded_to_this_item' => esc_attr__( 'Uploaded to this forum', 'bbpress' ),
73 'featured_image' => esc_attr__( 'Forum Image', 'bbpress' ),
74 'set_featured_image' => esc_attr__( 'Set forum image', 'bbpress' ),
75 'remove_featured_image' => esc_attr__( 'Remove forum image', 'bbpress' ),
76 'use_featured_image' => esc_attr__( 'Use as forum image', 'bbpress' ),
77 'item_published' => esc_attr__( 'Forum published.', 'bbpress' ),
78 'item_published_privately' => esc_attr__( 'Forum published privately.', 'bbpress' ),
79 'item_reverted_to_draft' => esc_attr__( 'Forum reverted to draft.', 'bbpress' ),
80 'item_scheduled' => esc_attr__( 'Forum scheduled.', 'bbpress' ),
81 'item_updated' => esc_attr__( 'Forum updated.', 'bbpress' )
82 )
83 );
84 }
85
86 /**
87 * Return array of forum post type rewrite settings
88 *
89 * @since 2.5.0 bbPress (r5130)
90 *
91 * @return array
92 */
93 function bbp_get_forum_post_type_rewrite() {
94
95 // Filter & return
96 return (array) apply_filters(
97 'bbp_get_forum_post_type_rewrite',
98 array(
99 'slug' => bbp_get_forum_slug(),
100 'with_front' => false
101 )
102 );
103 }
104
105 /**
106 * Return array of features the forum post type supports
107 *
108 * @since 2.5.0 bbPress (r5130)
109 *
110 * @return array
111 */
112 function bbp_get_forum_post_type_supports() {
113
114 // Filter & return
115 return (array) apply_filters(
116 'bbp_get_forum_post_type_supports',
117 array(
118 'title',
119 'editor',
120 'revisions'
121 )
122 );
123 }
124
125 /** Forum Loop ****************************************************************/
126
127 /**
128 * The main forum loop.
129 *
130 * WordPress makes this easy for us.
131 *
132 * @since 2.0.0 bbPress (r2464)
133 *
134 * @param array $args All the arguments supported by {@link WP_Query}
135 *
136 * @return object Multidimensional array of forum information
137 */
138 function bbp_has_forums( $args = array() ) {
139
140 // Forum archive only shows root
141 if ( bbp_is_forum_archive() ) {
142 $default_post_parent = 0;
143
144 // User subscriptions shows any
145 } elseif ( bbp_is_subscriptions() ) {
146 $default_post_parent = 'any';
147
148 // Could be anything, so look for possible parent ID
149 } else {
150 $default_post_parent = bbp_get_forum_id();
151 }
152
153 $default_forum_search = bbp_sanitize_search_request( 'fs' );
154
155 // Default argument array
156 $default = array(
157 'post_type' => bbp_get_forum_post_type(),
158 'post_parent' => $default_post_parent,
159 'post_status' => bbp_get_public_status_id(),
160 'posts_per_page' => get_option( '_bbp_forums_per_page', 50 ),
161 'orderby' => 'menu_order title',
162 'order' => 'ASC',
163 'no_found_rows' => true,
164 'ignore_sticky_posts' => true,
165
166 // Conditionally prime the cache for last active posts
167 'update_post_family_cache' => true
168 );
169
170 // Only add 's' arg if searching for forums
171 // See https://bbpress.trac.wordpress.org/ticket/2607
172 if ( ! empty( $default_forum_search ) ) {
173 $default['s'] = $default_forum_search;
174 }
175
176 // Parse arguments with default forum query for most circumstances
177 $r = bbp_parse_args( $args, $default, 'has_forums' );
178
179 // Run the query
180 $bbp = bbpress();
181 $bbp->forum_query = new WP_Query( $r );
182
183 // Maybe prime last active posts
184 if ( ! empty( $r['update_post_family_cache'] ) ) {
185 bbp_update_post_family_caches( $bbp->forum_query->posts );
186 }
187
188 // Filter & return
189 return apply_filters( 'bbp_has_forums', $bbp->forum_query->have_posts(), $bbp->forum_query );
190 }
191
192 /**
193 * Whether there are more forums available in the loop
194 *
195 * @since 2.0.0 bbPress (r2464)
196 *
197 * @return object Forum information
198 */
199 function bbp_forums() {
200
201 // Put into variable to check against next
202 $have_posts = bbpress()->forum_query->have_posts();
203
204 // Reset the post data when finished
205 if ( empty( $have_posts ) ) {
206 wp_reset_postdata();
207 }
208
209 return $have_posts;
210 }
211
212 /**
213 * Loads up the current forum in the loop
214 *
215 * @since 2.0.0 bbPress (r2464)
216 *
217 * @return object Forum information
218 */
219 function bbp_the_forum() {
220 return bbpress()->forum_query->the_post();
221 }
222
223 /** Forum *********************************************************************/
224
225 /**
226 * Output forum id
227 *
228 * @since 2.0.0 bbPress (r2464)
229 *
230 * @param $forum_id Optional. Used to check emptiness
231 */
232 function bbp_forum_id( $forum_id = 0 ) {
233 echo bbp_get_forum_id( $forum_id );
234 }
235 /**
236 * Return the forum id
237 *
238 * @since 2.0.0 bbPress (r2464)
239 *
240 * @param $forum_id Optional. Used to check emptiness
241 * @return int The forum id
242 */
243 function bbp_get_forum_id( $forum_id = 0 ) {
244 $bbp = bbpress();
245 $wp_query = bbp_get_wp_query();
246
247 // Easy empty checking
248 if ( ! empty( $forum_id ) && is_numeric( $forum_id ) ) {
249 $bbp_forum_id = $forum_id;
250
251 // Currently inside a forum loop
252 } elseif ( ! empty( $bbp->forum_query->in_the_loop ) && isset( $bbp->forum_query->post->ID ) ) {
253 $bbp_forum_id = $bbp->forum_query->post->ID;
254
255 // Currently inside a search loop
256 } elseif ( ! empty( $bbp->search_query->in_the_loop ) && isset( $bbp->search_query->post->ID ) && bbp_is_forum( $bbp->search_query->post->ID ) ) {
257 $bbp_forum_id = $bbp->search_query->post->ID;
258
259 // Currently viewing a forum
260 } elseif ( ( bbp_is_single_forum() || bbp_is_forum_edit() ) && ! empty( $bbp->current_forum_id ) ) {
261 $bbp_forum_id = $bbp->current_forum_id;
262
263 // Currently viewing a forum
264 } elseif ( ( bbp_is_single_forum() || bbp_is_forum_edit() ) && isset( $wp_query->post->ID ) ) {
265 $bbp_forum_id = $wp_query->post->ID;
266
267 // Currently viewing a topic
268 } elseif ( bbp_is_single_topic() ) {
269 $bbp_forum_id = bbp_get_topic_forum_id();
270
271 // Fallback
272 } else {
273 $bbp_forum_id = 0;
274 }
275
276 // Filter & return
277 return (int) apply_filters( 'bbp_get_forum_id', (int) $bbp_forum_id, $forum_id );
278 }
279
280 /**
281 * Gets a forum
282 *
283 * @since 2.0.0 bbPress (r2787)
284 *
285 * @param int|object $forum forum id or forum object
286 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N. Default = OBJECT
287 * @param string $filter Optional Sanitation filter. See {@link sanitize_post()}
288 *
289 * @return mixed Null if error or forum (in specified form) if success
290 */
291 function bbp_get_forum( $forum, $output = OBJECT, $filter = 'raw' ) {
292
293 // Maybe get ID from empty or int
294 if ( empty( $forum ) || is_numeric( $forum ) ) {
295 $forum = bbp_get_forum_id( $forum );
296 }
297
298 // Bail if no post object
299 $forum = get_post( $forum, OBJECT, $filter );
300 if ( empty( $forum ) ) {
301 return $forum;
302 }
303
304 // Bail if not correct post type
305 if ( bbp_get_forum_post_type() !== $forum->post_type ) {
306 return null;
307 }
308
309 // Default return value is OBJECT
310 $retval = $forum;
311
312 // Array A
313 if ( ARRAY_A === $output ) {
314 $retval = get_object_vars( $forum );
315
316 // Array N
317 } elseif ( ARRAY_N === $output ) {
318 $retval = array_values( get_object_vars( $forum ) );
319 }
320
321 // Filter & return
322 return apply_filters( 'bbp_get_forum', $retval, $forum, $output, $filter );
323 }
324
325 /**
326 * Output the link to the forum
327 *
328 * @since 2.0.0 bbPress (r2464)
329 *
330 * @param int $forum_id Optional. Forum id
331 * @param string $redirect_to Optional. Pass a redirect value for use with
332 * shortcodes and other fun things.
333 */
334 function bbp_forum_permalink( $forum_id = 0, $redirect_to = '' ) {
335 echo esc_url( bbp_get_forum_permalink( $forum_id, $redirect_to ) );
336 }
337 /**
338 * Return the link to the forum
339 *
340 * @since 2.0.0 bbPress (r2464)
341 *
342 * @param int $forum_id Optional. Forum id
343 * @param string $redirect_to Optional. Pass a redirect value for use with
344 * shortcodes and other fun things.
345 *
346 * @return string Permanent link to forum
347 */
348 function bbp_get_forum_permalink( $forum_id = 0, $redirect_to = '' ) {
349 $forum_id = bbp_get_forum_id( $forum_id );
350
351 // Use the redirect address
352 if ( ! empty( $redirect_to ) ) {
353 $forum_permalink = esc_url_raw( $redirect_to );
354
355 // Use the topic permalink
356 } else {
357 $forum_permalink = get_permalink( $forum_id );
358 }
359
360 // Filter & return
361 return apply_filters( 'bbp_get_forum_permalink', $forum_permalink, $forum_id );
362 }
363
364 /**
365 * Output the title of the forum
366 *
367 * @since 2.0.0 bbPress (r2464)
368 *
369 * @param int $forum_id Optional. Forum id
370 */
371 function bbp_forum_title( $forum_id = 0 ) {
372 echo bbp_get_forum_title( $forum_id );
373 }
374 /**
375 * Return the title of the forum
376 *
377 * @since 2.0.0 bbPress (r2464)
378 *
379 * @param int $forum_id Optional. Forum id
380 * @return string Title of forum
381 */
382 function bbp_get_forum_title( $forum_id = 0 ) {
383 $forum_id = bbp_get_forum_id( $forum_id );
384 $title = get_the_title( $forum_id );
385
386 // Filter & return
387 return apply_filters( 'bbp_get_forum_title', $title, $forum_id );
388 }
389
390 /**
391 * Output the forum archive title
392 *
393 * @since 2.0.0 bbPress (r3250)
394 *
395 * @param string $title Default text to use as title
396 */
397 function bbp_forum_archive_title( $title = '' ) {
398 echo bbp_get_forum_archive_title( $title );
399 }
400 /**
401 * Return the forum archive title
402 *
403 * @since 2.0.0 bbPress (r3250)
404 *
405 * @param string $title Default text to use as title
406 *
407 * @return string The forum archive title
408 */
409 function bbp_get_forum_archive_title( $title = '' ) {
410
411 // If no title was passed
412 if ( empty( $title ) ) {
413
414 // Set root text to page title
415 $page = bbp_get_page_by_path( bbp_get_root_slug() );
416 if ( ! empty( $page ) ) {
417 $title = get_the_title( $page->ID );
418
419 // Default to forum post type name label
420 } else {
421 $fto = get_post_type_object( bbp_get_forum_post_type() );
422 $title = $fto->labels->name;
423 }
424 }
425
426 // Filter & return
427 return apply_filters( 'bbp_get_forum_archive_title', $title );
428 }
429
430 /**
431 * Output the content of the forum
432 *
433 * @since 2.0.0 bbPress (r2794)
434 *
435 * @param int $forum_id Optional. Topic id
436 */
437 function bbp_forum_content( $forum_id = 0 ) {
438 echo bbp_get_forum_content( $forum_id );
439 }
440 /**
441 * Return the content of the forum
442 *
443 * @since 2.0.0 bbPress (r2794)
444 *
445 * @param int $forum_id Optional. Topic id
446 *
447 * @return string Content of the forum
448 */
449 function bbp_get_forum_content( $forum_id = 0 ) {
450 $forum_id = bbp_get_forum_id( $forum_id );
451
452 // Check the forum and its ancestors.
453 $password_id = bbp_get_password_required_id( $forum_id );
454 if ( ! empty( $password_id ) ) {
455 return get_the_password_form( $password_id );
456 }
457
458 $content = get_post_field( 'post_content', $forum_id );
459
460 // Filter & return
461 return apply_filters( 'bbp_get_forum_content', $content, $forum_id );
462 }
463
464 /**
465 * Allow forum rows to have administrative actions
466 *
467 * @since 2.1.0 bbPress (r3654)
468 *
469 * @todo Links and filter
470 */
471 function bbp_forum_row_actions() {
472 do_action( 'bbp_forum_row_actions' );
473 }
474
475 /**
476 * Output the forums last active ID
477 *
478 * @since 2.0.0 bbPress (r2876)
479 *
480 * @param int $forum_id Optional. Forum id
481 */
482 function bbp_forum_last_active_id( $forum_id = 0 ) {
483 echo bbp_get_forum_last_active_id( $forum_id );
484 }
485 /**
486 * Return the forums last active ID
487 *
488 * @since 2.0.0 bbPress (r2876)
489 *
490 * @param int $forum_id Optional. Forum id
491 * the last active id and forum id
492 * @return int Forum's last active id
493 */
494 function bbp_get_forum_last_active_id( $forum_id = 0 ) {
495 $forum_id = bbp_get_forum_id( $forum_id );
496 $active_id = (int) get_post_meta( $forum_id, '_bbp_last_active_id', true );
497
498 // Filter & return
499 return (int) apply_filters( 'bbp_get_forum_last_active_id', $active_id, $forum_id );
500 }
501
502 /**
503 * Output the forums last update date/time (aka freshness)
504 *
505 * @since 2.0.0 bbPress (r2876)
506 *
507 * @param int $forum_id Optional. Forum id
508 */
509 function bbp_forum_last_active_time( $forum_id = 0 ) {
510 echo bbp_get_forum_last_active_time( $forum_id );
511 }
512 /**
513 * Return the forums last update date/time (aka freshness)
514 *
515 * @since 2.0.0 bbPress (r2876)
516 *
517 * @param int $forum_id Optional. Forum id
518 * @return string Forum last update date/time (freshness)
519 */
520 function bbp_get_forum_last_active_time( $forum_id = 0 ) {
521
522 // Verify forum and get last active meta
523 $forum_id = bbp_get_forum_id( $forum_id );
524 $active_id = bbp_get_forum_last_active_id( $forum_id );
525 if ( empty( $active_id ) ) {
526 $active_id = bbp_get_forum_last_reply_id( $forum_id );
527 }
528 if ( empty( $active_id ) ) {
529 $active_id = bbp_get_forum_last_topic_id( $forum_id );
530 }
531 if ( ! empty( $active_id ) && ! bbp_is_forum_activity_public( $active_id ) ) {
532 return apply_filters( 'bbp_get_forum_last_active', '', $forum_id );
533 }
534 $last_active = get_post_meta( $forum_id, '_bbp_last_active_time', true );
535
536 if ( empty( $last_active ) ) {
537 $reply_id = bbp_get_forum_last_reply_id( $forum_id );
538 if ( ! empty( $reply_id ) ) {
539 $last_active = get_post_field( 'post_date', $reply_id );
540 } else {
541 $topic_id = bbp_get_forum_last_topic_id( $forum_id );
542 if ( ! empty( $topic_id ) ) {
543 $last_active = bbp_get_topic_last_active_time( $topic_id );
544 }
545 }
546 }
547
548 $active_time = ! empty( $last_active ) ? bbp_get_time_since( bbp_convert_date( $last_active ) ) : '';
549
550 // Filter & return
551 return apply_filters( 'bbp_get_forum_last_active', $active_time, $forum_id );
552 }
553
554 /**
555 * Output link to the most recent activity inside a forum.
556 *
557 * Outputs a complete link with attributes and content.
558 *
559 * @since 2.0.0 bbPress (r2627)
560 *
561 * @param int $forum_id Optional. Forum id
562 */
563 function bbp_forum_freshness_link( $forum_id = 0 ) {
564 echo bbp_get_forum_freshness_link( $forum_id );
565 }
566
567 /**
568 * Is a topic or reply eligible for public forum activity?
569 *
570 * @since 2.6.19 bbPress (r7630)
571 *
572 * @param int $post_id Topic or reply ID.
573 * @return bool Whether the activity is public.
574 */
575 function bbp_is_forum_activity_public( $post_id = 0 ) {
576 $post_id = (int) $post_id;
577 if ( empty( $post_id ) ) {
578 return false;
579 }
580
581 if ( bbp_is_topic( $post_id ) ) {
582 return bbp_is_topic_public( $post_id );
583 }
584
585 if ( bbp_is_reply( $post_id ) ) {
586 return bbp_is_reply_public( $post_id ) && bbp_is_topic_public( bbp_get_reply_topic_id( $post_id ) );
587 }
588
589 return false;
590 }
591
592 /**
593 * Returns link to the most recent activity inside a forum.
594 *
595 * Returns a complete link with attributes and content.
596 *
597 * @since 2.0.0 bbPress (r2627)
598 *
599 * @param int $forum_id Optional. Forum id
600 */
601 function bbp_get_forum_freshness_link( $forum_id = 0 ) {
602 $forum_id = bbp_get_forum_id( $forum_id );
603 $active_id = bbp_get_forum_last_active_id( $forum_id );
604 $link_url = $title = '';
605
606 if ( empty( $active_id ) ) {
607 $active_id = bbp_get_forum_last_reply_id( $forum_id );
608 }
609
610 if ( empty( $active_id ) ) {
611 $active_id = bbp_get_forum_last_topic_id( $forum_id );
612 }
613
614 // Existing forum metadata may still point to non-public content
615 if ( ! empty( $active_id ) && ! bbp_is_forum_activity_public( $active_id ) ) {
616 return apply_filters( 'bbp_get_forum_freshness_link', '-', $forum_id, '', '', '', $active_id );
617 }
618
619 if ( bbp_is_topic( $active_id ) ) {
620 $link_url = bbp_get_forum_last_topic_permalink( $forum_id );
621 $title = bbp_get_forum_last_topic_title( $forum_id );
622 } elseif ( bbp_is_reply( $active_id ) ) {
623 $link_url = bbp_get_forum_last_reply_url( $forum_id );
624 $title = bbp_get_forum_last_reply_title( $forum_id );
625 }
626
627 $time_since = bbp_get_forum_last_active_time( $forum_id );
628
629 if ( ! empty( $time_since ) && ! empty( $link_url ) ) {
630 $anchor = '<a href="' . esc_url( $link_url ) . '" title="' . esc_attr( $title ) . '">' . esc_html( $time_since ) . '</a>';
631 } else {
632 $anchor = esc_html__( 'No Topics', 'bbpress' );
633 }
634
635 // Filter & return
636 return apply_filters( 'bbp_get_forum_freshness_link', $anchor, $forum_id, $time_since, $link_url, $title, $active_id );
637 }
638
639 /**
640 * Output parent ID of a forum, if exists
641 *
642 * @since 2.1.0 bbPress (r3678)
643 *
644 * @param int $forum_id Forum ID
645 */
646 function bbp_forum_parent_id( $forum_id = 0 ) {
647 echo bbp_get_forum_parent_id( $forum_id );
648 }
649 /**
650 * Return ID of forum parent, if exists
651 *
652 * @since 2.1.0 bbPress (r3678)
653 *
654 * @param int $forum_id Optional. Forum id
655 * @return int Forum parent
656 */
657 function bbp_get_forum_parent_id( $forum_id = 0 ) {
658 $forum_id = bbp_get_forum_id( $forum_id );
659 $parent_id = (int) get_post_field( 'post_parent', $forum_id );
660
661 // Meta-data fallback
662 if ( empty( $parent_id ) ) {
663 $parent_id = (int) get_post_meta( $forum_id, '_bbp_forum_id', true );
664 }
665
666 // Filter
667 if ( ! empty( $parent_id ) ) {
668 $parent_id = (int) bbp_get_forum_id( $parent_id );
669 }
670
671 // Filter & return
672 return (int) apply_filters( 'bbp_get_forum_parent_id', $parent_id, $forum_id );
673 }
674
675 /**
676 * Return array of parent forums
677 *
678 * @since 2.0.0 bbPress (r2627)
679 *
680 * @param int $forum_id Optional. Forum id
681 * @return array Forum ancestors
682 */
683 function bbp_get_forum_ancestors( $forum_id = 0 ) {
684 $forum_id = bbp_get_forum_id( $forum_id );
685 $ancestors = array();
686 $forum = bbp_get_forum( $forum_id );
687
688 if ( ! empty( $forum ) ) {
689 while ( 0 !== (int) $forum->post_parent ) {
690 $ancestors[] = $forum->post_parent;
691 $forum = bbp_get_forum( $forum->post_parent );
692 }
693 }
694
695 // Filter & return
696 return apply_filters( 'bbp_get_forum_ancestors', $ancestors, $forum_id );
697 }
698
699 /**
700 * Return subforums of given forum
701 *
702 * @since 2.0.0 bbPress (r2914)
703 *
704 * @param array $args All the arguments supported by {@link WP_Query}
705 * @return mixed false if none, array of subs if yes
706 */
707 function bbp_forum_get_subforums( $args = array() ) {
708
709 // Default return value
710 $retval = array();
711
712 // Use passed integer as post_parent
713 if ( is_numeric( $args ) && ! empty( $args ) ) {
714 $args = array( 'post_parent' => bbp_get_forum_id( $args ) );
715 }
716
717 // Parse arguments against default values
718 $r = bbp_parse_args(
719 $args,
720 array(
721 'post_parent' => 0,
722 'post_type' => bbp_get_forum_post_type(),
723 'posts_per_page' => get_option( '_bbp_forums_per_page', 50 ),
724 'orderby' => 'menu_order title',
725 'order' => 'ASC',
726 'ignore_sticky_posts' => true,
727 'no_found_rows' => true
728 ),
729 'forum_get_subforums'
730 );
731
732 // Ensure post_parent is properly set
733 $r['post_parent'] = bbp_get_forum_id( $r['post_parent'] );
734
735 // Query if post_parent has subforums
736 if ( ! empty( $r['post_parent'] ) && bbp_get_forum_subforum_count( $r['post_parent'], true ) ) {
737 $get_posts = new WP_Query();
738 $retval = $get_posts->query( $r );
739 }
740
741 // Filter & return
742 return (array) apply_filters( 'bbp_forum_get_subforums', $retval, $r, $args );
743 }
744
745 /**
746 * Output a list of forums (can be used to list subforums)
747 *
748 * @since 2.0.0 bbPress (r2708)
749 *
750 * @param array $args The function supports these args:
751 * - before: To put before the output. Defaults to '<ul class="bbp-forums-list">'
752 * - after: To put after the output. Defaults to '</ul>'
753 * - link_before: To put before every link. Defaults to '<li class="bbp-forum">'
754 * - link_after: To put after every link. Defaults to '</li>'
755 * - sep: Separator. Defaults to ''. Make sure your markup is valid!
756 * - count_before: String before each count Defaults to ' ('
757 * - count_after: String before each count Defaults to ')'
758 * - count_sep: Count separator. Defaults to ', '
759 * - forum_id: Forum id. Defaults to ''
760 * - show_topic_count - To show forum topic count or not. Defaults to true
761 * - show_reply_count - To show forum reply count or not. Defaults to true
762 */
763 function bbp_list_forums( $args = array() ) {
764
765 // Parse arguments against default values
766 $r = bbp_parse_args(
767 $args,
768 array(
769 'before' => '<ul class="bbp-forums-list">',
770 'after' => '</ul>',
771 'link_before' => '<li class="bbp-forum css-sep">',
772 'link_after' => '</li>',
773 'sep' => '',
774 'count_before' => ' (',
775 'count_after' => ')',
776 'count_sep' => ', ',
777 'forum_id' => bbp_get_forum_id(),
778 'show_topic_count' => true,
779 'show_reply_count' => true,
780 'echo' => true,
781
782 // Retired, use 'sep' instead
783 'separator' => false
784 ),
785 'list_forums'
786 );
787
788 /**
789 * Necessary for backwards compatibility
790 * @see https://bbpress.trac.wordpress.org/ticket/2900
791 */
792 if ( ! empty( $r['separator'] ) ) {
793 $r['sep'] = $r['separator'];
794 }
795
796 // Default values
797 $links = array();
798 $output = '';
799
800 // Query for subforums
801 $sub_forums = ! empty( $r['forum_id'] )
802 ? bbp_forum_get_subforums( $r['forum_id'] )
803 : array();
804
805 // Loop through forums and create a list
806 if ( ! empty( $sub_forums ) ) {
807 foreach ( $sub_forums as $sub_forum ) {
808
809 // Get forum details
810 $count = array();
811 $permalink = bbp_get_forum_permalink( $sub_forum->ID );
812 $title = bbp_get_forum_title( $sub_forum->ID );
813
814 // Show topic count
815 if ( ! empty( $r['show_topic_count'] ) && ! bbp_is_forum_category( $sub_forum->ID ) ) {
816 $count['topic'] = bbp_get_forum_topic_count( $sub_forum->ID );
817 }
818
819 // Show reply count
820 if ( ! empty( $r['show_reply_count'] ) && ! bbp_is_forum_category( $sub_forum->ID ) ) {
821 $count['reply'] = bbp_get_forum_reply_count( $sub_forum->ID );
822 }
823
824 // Counts to show
825 $counts = ! empty( $count )
826 ? $r['count_before'] . implode( $r['count_sep'], $count ) . $r['count_after']
827 : '';
828
829 // Subforum classes
830 $subforum_classes = array( 'bbp-forum-link' );
831 $subforum_classes = apply_filters( 'bbp_list_forums_subforum_classes', $subforum_classes, $sub_forum->ID );
832
833 // This could use bbp_get_forum_class() eventually...
834 $subforum_classes_attr = 'class="' . implode( ' ', array_map( 'sanitize_html_class', $subforum_classes ) ) . '"';
835
836 // Build this sub forums link
837 $links[] = $r['link_before'] . '<a href="' . esc_url( $permalink ) . '" ' . $subforum_classes_attr . '>' . $title . $counts . '</a>' . $r['link_after'];
838 }
839
840 // Maybe wrap output
841 $output = ! empty( $links )
842 ? $r['before'] . implode( $r['sep'], $links ) . $r['after']
843 : '';
844 }
845
846 // Filter output
847 $the_list = apply_filters( 'bbp_list_forums', $output, $r, $args );
848
849 // Echo or return the forums list
850 if ( ! empty( $r['echo'] ) ) {
851 echo $the_list;
852 } else {
853 return $the_list;
854 }
855 }
856
857 /** Forum Subscriptions *******************************************************/
858
859 /**
860 * Output the forum subscription link
861 *
862 * @since 2.5.0 bbPress (r5156)
863 * @since 2.6.0 bbPress (r6307) Add 'redirect_to' support
864 */
865 function bbp_forum_subscription_link( $args = array() ) {
866 echo bbp_get_forum_subscription_link( $args );
867 }
868
869 /**
870 * Get the forum subscription link
871 *
872 * A custom wrapper for bbp_get_user_subscribe_link()
873 *
874 * @since 2.5.0 bbPress (r5156)
875 * @since 2.6.0 bbPress (r6307) Add 'redirect_to' support
876 */
877 function bbp_get_forum_subscription_link( $args = array() ) {
878
879 // Defaults
880 $retval = false;
881 $redirect_to = bbp_is_subscriptions()
882 ? bbp_get_subscriptions_permalink()
883 : '';
884
885 // Parse the arguments
886 $r = bbp_parse_args(
887 $args,
888 array(
889 'user_id' => bbp_get_current_user_id(),
890 'object_id' => bbp_get_forum_id(),
891 'object_type' => 'post',
892 'before' => '',
893 'after' => '',
894 'subscribe' => esc_html__( 'Subscribe', 'bbpress' ),
895 'unsubscribe' => esc_html__( 'Unsubscribe', 'bbpress' ),
896 'redirect_to' => $redirect_to
897 ),
898 'get_forum_subscribe_link'
899 );
900
901 // No link for categories until we support subscription hierarchy
902 // @see https://bbpress.trac.wordpress.org/ticket/2475
903 if ( ! bbp_is_forum_category() ) {
904 $retval = bbp_get_user_subscribe_link( $r );
905 }
906
907 // Filter & return
908 return apply_filters( 'bbp_get_forum_subscribe_link', $retval, $r, $args );
909 }
910
911 /** Forum Last Topic **********************************************************/
912
913 /**
914 * Output the forum's last topic id
915 *
916 * @since 2.0.0 bbPress (r2627)
917 *
918 * @param int $forum_id Optional. Forum id
919 */
920 function bbp_forum_last_topic_id( $forum_id = 0 ) {
921 echo bbp_get_forum_last_topic_id( $forum_id );
922 }
923 /**
924 * Return the forum's last topic id
925 *
926 * @since 2.0.0 bbPress (r2627)
927 *
928 * @param int $forum_id Optional. Forum id
929 * @return int Forum's last topic id
930 */
931 function bbp_get_forum_last_topic_id( $forum_id = 0 ) {
932 $forum_id = bbp_get_forum_id( $forum_id );
933 $topic_id = (int) get_post_meta( $forum_id, '_bbp_last_topic_id', true );
934
935 // Filter & return
936 return (int) apply_filters( 'bbp_get_forum_last_topic_id', $topic_id, $forum_id );
937 }
938
939 /**
940 * Output the title of the last topic inside a forum
941 *
942 * @since 2.0.0 bbPress (r2627)
943 *
944 * @param int $forum_id Optional. Forum id
945 */
946 function bbp_forum_last_topic_title( $forum_id = 0 ) {
947 echo bbp_get_forum_last_topic_title( $forum_id );
948 }
949 /**
950 * Return the title of the last topic inside a forum
951 *
952 * @since 2.0.0 bbPress (r2627)
953 *
954 * @param int $forum_id Optional. Forum id
955 * @return string Forum's last topic's title
956 */
957 function bbp_get_forum_last_topic_title( $forum_id = 0 ) {
958 $forum_id = bbp_get_forum_id( $forum_id );
959 $topic_id = bbp_get_forum_last_topic_id( $forum_id );
960 $title = ! empty( $topic_id ) ? bbp_get_topic_title( $topic_id ) : '';
961
962 // Filter & return
963 return apply_filters( 'bbp_get_forum_last_topic_title', $title, $forum_id );
964 }
965
966 /**
967 * Output the link to the last topic in a forum
968 *
969 * @since 2.0.0 bbPress (r2627)
970 *
971 * @param int $forum_id Optional. Forum id
972 */
973 function bbp_forum_last_topic_permalink( $forum_id = 0 ) {
974 echo esc_url( bbp_get_forum_last_topic_permalink( $forum_id ) );
975 }
976 /**
977 * Return the link to the last topic in a forum
978 *
979 * @since 2.0.0 bbPress (r2627)
980 *
981 * @param int $forum_id Optional. Forum id
982 * @return string Permanent link to topic
983 */
984 function bbp_get_forum_last_topic_permalink( $forum_id = 0 ) {
985 $forum_id = bbp_get_forum_id( $forum_id );
986 $topic_id = bbp_get_forum_last_topic_id( $forum_id );
987 $link = bbp_get_topic_permalink( $topic_id );
988
989 // Filter & return
990 return apply_filters( 'bbp_get_forum_last_topic_permalink', $link, $forum_id, $topic_id );
991 }
992
993 /**
994 * Return the author ID of the last topic of a forum
995 *
996 * @since 2.0.0 bbPress (r2627)
997 *
998 * @param int $forum_id Optional. Forum id
999 * @return int Forum's last topic's author id
1000 */
1001 function bbp_get_forum_last_topic_author_id( $forum_id = 0 ) {
1002 $forum_id = bbp_get_forum_id( $forum_id );
1003 $topic_id = bbp_get_forum_last_topic_id( $forum_id );
1004 $author_id = bbp_get_topic_author_id( $topic_id );
1005
1006 // Filter & return
1007 return (int) apply_filters( 'bbp_get_forum_last_topic_author_id', (int) $author_id, $forum_id, $topic_id );
1008 }
1009
1010 /**
1011 * Output link to author of last topic of forum
1012 *
1013 * @since 2.0.0 bbPress (r2627)
1014 *
1015 * @param int $forum_id Optional. Forum id
1016 */
1017 function bbp_forum_last_topic_author_link( $forum_id = 0 ) {
1018 echo bbp_get_forum_last_topic_author_link( $forum_id );
1019 }
1020 /**
1021 * Return link to author of last topic of forum
1022 *
1023 * @since 2.0.0 bbPress (r2627)
1024 *
1025 * @param int $forum_id Optional. Forum id
1026 * @return string Forum's last topic's author link
1027 */
1028 function bbp_get_forum_last_topic_author_link( $forum_id = 0 ) {
1029 $forum_id = bbp_get_forum_id( $forum_id );
1030 $author_id = bbp_get_forum_last_topic_author_id( $forum_id );
1031 $author_link = bbp_get_user_profile_link( $author_id );
1032
1033 // Filter & return
1034 return apply_filters( 'bbp_get_forum_last_topic_author_link', $author_link, $forum_id );
1035 }
1036
1037 /** Forum Last Reply **********************************************************/
1038
1039 /**
1040 * Output the forums last reply id
1041 *
1042 * @since 2.0.0 bbPress (r2627)
1043 *
1044 * @param int $forum_id Optional. Forum id
1045 */
1046 function bbp_forum_last_reply_id( $forum_id = 0 ) {
1047 echo bbp_get_forum_last_reply_id( $forum_id );
1048 }
1049 /**
1050 * Return the forums last reply id
1051 *
1052 * @since 2.0.0 bbPress (r2627)
1053 *
1054 * @param int $forum_id Optional. Forum id
1055 * @return int Forum's last reply id
1056 */
1057 function bbp_get_forum_last_reply_id( $forum_id = 0 ) {
1058 $forum_id = bbp_get_forum_id( $forum_id );
1059 $reply_id = (int) get_post_meta( $forum_id, '_bbp_last_reply_id', true );
1060
1061 // Filter & return
1062 return (int) apply_filters( 'bbp_get_forum_last_reply_id', $reply_id, $forum_id );
1063 }
1064
1065 /**
1066 * Output the title of the last reply inside a forum
1067 *
1068 * @param int $forum_id Optional. Forum id
1069 */
1070 function bbp_forum_last_reply_title( $forum_id = 0 ) {
1071 echo bbp_get_forum_last_reply_title( $forum_id );
1072 }
1073 /**
1074 * Return the title of the last reply inside a forum
1075 *
1076 * @param int $forum_id Optional. Forum id
1077 * @return string
1078 */
1079 function bbp_get_forum_last_reply_title( $forum_id = 0 ) {
1080 $forum_id = bbp_get_forum_id( $forum_id );
1081 $reply_id = bbp_get_forum_last_reply_id( $forum_id );
1082 $title = bbp_get_reply_title( $reply_id );
1083
1084 // Filter & return
1085 return apply_filters( 'bbp_get_forum_last_reply_title', $title, $forum_id, $reply_id );
1086 }
1087
1088 /**
1089 * Output the link to the last reply in a forum
1090 *
1091 * @since 2.0.0 bbPress (r2627)
1092 *
1093 * @param int $forum_id Optional. Forum id
1094 */
1095 function bbp_forum_last_reply_permalink( $forum_id = 0 ) {
1096 echo esc_url( bbp_get_forum_last_reply_permalink( $forum_id ) );
1097 }
1098 /**
1099 * Return the link to the last reply in a forum
1100 *
1101 * @since 2.0.0 bbPress (r2627)
1102 *
1103 * @param int $forum_id Optional. Forum id
1104 *
1105 * @return string Permanent link to the forum's last reply
1106 */
1107 function bbp_get_forum_last_reply_permalink( $forum_id = 0 ) {
1108 $forum_id = bbp_get_forum_id( $forum_id );
1109 $reply_id = bbp_get_forum_last_reply_id( $forum_id );
1110 $link = bbp_get_reply_permalink( $reply_id );
1111
1112 // Filter & return
1113 return apply_filters( 'bbp_get_forum_last_reply_permalink', $link, $forum_id, $reply_id );
1114 }
1115
1116 /**
1117 * Output the url to the last reply in a forum
1118 *
1119 * @since 2.0.0 bbPress (r2684)
1120 *
1121 * @param int $forum_id Optional. Forum id
1122 */
1123 function bbp_forum_last_reply_url( $forum_id = 0 ) {
1124 echo esc_url( bbp_get_forum_last_reply_url( $forum_id ) );
1125 }
1126 /**
1127 * Return the url to the last reply in a forum
1128 *
1129 * @since 2.0.0 bbPress (r2684)
1130 *
1131 * @param int $forum_id Optional. Forum id
1132 * @return string Paginated URL to latest reply
1133 */
1134 function bbp_get_forum_last_reply_url( $forum_id = 0 ) {
1135 $forum_id = bbp_get_forum_id( $forum_id );
1136
1137 // If forum has replies, get the last reply and use its url
1138 $reply_id = bbp_get_forum_last_reply_id( $forum_id );
1139 if ( ! empty( $reply_id ) ) {
1140 $reply_url = bbp_get_reply_url( $reply_id );
1141
1142 // No replies, so look for topics and use last permalink
1143 } else {
1144 $reply_url = bbp_get_forum_last_topic_permalink( $forum_id );
1145
1146 // No topics either, so set $reply_url as empty string
1147 if ( empty( $reply_url ) ) {
1148 $reply_url = '';
1149 }
1150 }
1151
1152 // Filter & return
1153 return apply_filters( 'bbp_get_forum_last_reply_url', $reply_url, $forum_id, $reply_id );
1154 }
1155
1156 /**
1157 * Output author ID of last reply of forum
1158 *
1159 * @since 2.0.0 bbPress (r2627)
1160 *
1161 * @param int $forum_id Optional. Forum id
1162 */
1163 function bbp_forum_last_reply_author_id( $forum_id = 0 ) {
1164 echo bbp_get_forum_last_reply_author_id( $forum_id );
1165 }
1166 /**
1167 * Return author ID of last reply of forum
1168 *
1169 * @since 2.0.0 bbPress (r2627)
1170 *
1171 * @param int $forum_id Optional. Forum id
1172 * @return int Forum's last reply author id
1173 */
1174 function bbp_get_forum_last_reply_author_id( $forum_id = 0 ) {
1175 $forum_id = bbp_get_forum_id( $forum_id );
1176 $reply_id = bbp_get_forum_last_reply_id( $forum_id );
1177 $author_id = bbp_get_reply_author_id( $reply_id );
1178
1179 // Filter & return
1180 return apply_filters( 'bbp_get_forum_last_reply_author_id', $author_id, $forum_id, $reply_id );
1181 }
1182
1183 /**
1184 * Output link to author of last reply of forum
1185 *
1186 * @since 2.0.0 bbPress (r2627)
1187 *
1188 * @param int $forum_id Optional. Forum id
1189 */
1190 function bbp_forum_last_reply_author_link( $forum_id = 0 ) {
1191 echo bbp_get_forum_last_reply_author_link( $forum_id );
1192 }
1193 /**
1194 * Return link to author of last reply of forum
1195 *
1196 * @since 2.0.0 bbPress (r2627)
1197 *
1198 * @param int $forum_id Optional. Forum id
1199 * @return string Link to author of last reply of forum
1200 */
1201 function bbp_get_forum_last_reply_author_link( $forum_id = 0 ) {
1202 $forum_id = bbp_get_forum_id( $forum_id );
1203 $author_id = bbp_get_forum_last_reply_author_id( $forum_id );
1204 $author_link = bbp_get_user_profile_link( $author_id );
1205
1206 // Filter & return
1207 return apply_filters( 'bbp_get_forum_last_reply_author_link', $author_link, $forum_id, $author_id );
1208 }
1209
1210 /** Forum Counts **************************************************************/
1211
1212 /**
1213 * Output the topics link of the forum
1214 *
1215 * @since 2.0.0 bbPress (r2889)
1216 *
1217 * @param int $forum_id Optional. Topic id
1218 */
1219 function bbp_forum_topics_link( $forum_id = 0 ) {
1220 echo bbp_get_forum_topics_link( $forum_id );
1221 }
1222
1223 /**
1224 * Return the topics link of the forum
1225 *
1226 * @since 2.0.0 bbPress (r2889)
1227 *
1228 * @param int $forum_id Optional. Topic id
1229 */
1230 function bbp_get_forum_topics_link( $forum_id = 0 ) {
1231 $forum_id = bbp_get_forum_id( $forum_id );
1232 $link = bbp_get_forum_permalink( $forum_id );
1233 /* translators: %s: Number of topics */
1234 $topics = sprintf( _n( '%s topic', '%s topics', bbp_get_forum_topic_count( $forum_id, true, true ), 'bbpress' ), bbp_get_forum_topic_count( $forum_id, true, false ) );
1235
1236 // First link never has view=all
1237 $retval = bbp_get_view_all( 'edit_others_topics' )
1238 ? "<a href='" . esc_url( bbp_remove_view_all( $link ) ) . "'>" . esc_html( $topics ) . '</a>'
1239 : esc_html( $topics );
1240
1241 // Get deleted topics
1242 $deleted_int = bbp_get_forum_topic_count_hidden( $forum_id, false, true );
1243
1244 // This forum has hidden topics
1245 if ( ! empty( $deleted_int ) && current_user_can( 'edit_others_topics' ) ) {
1246
1247 // Hidden text
1248 $deleted_num = bbp_get_forum_topic_count_hidden( $forum_id, false, false );
1249 /* translators: %s: Number of hidden topics */
1250 $extra = ' ' . sprintf( _n( '(+%s hidden)', '(+%s hidden)', $deleted_int, 'bbpress' ), $deleted_num );
1251
1252 // Hidden link
1253 $retval .= ! bbp_get_view_all( 'edit_others_topics' )
1254 ? " <a href='" . esc_url( bbp_add_view_all( $link, true ) ) . "'>" . esc_html( $extra ) . '</a>'
1255 : " {$extra}";
1256 }
1257
1258 // Filter & return
1259 return apply_filters( 'bbp_get_forum_topics_link', $retval, $forum_id );
1260 }
1261
1262 /**
1263 * Output total sub-forum count of a forum
1264 *
1265 * @since 2.0.0 bbPress (r2627)
1266 *
1267 * @param int $forum_id Optional. Forum id to check
1268 * @param boolean $integer Optional. Whether or not to format the result
1269 */
1270 function bbp_forum_subforum_count( $forum_id = 0, $integer = false ) {
1271 echo bbp_get_forum_subforum_count( $forum_id, $integer );
1272 }
1273 /**
1274 * Return total subforum count of a forum
1275 *
1276 * @since 2.0.0 bbPress (r2627)
1277 *
1278 * @param int $forum_id Optional. Forum id
1279 * @param boolean $integer Optional. Whether or not to format the result
1280 * @return int Forum's subforum count
1281 */
1282 function bbp_get_forum_subforum_count( $forum_id = 0, $integer = false ) {
1283 $forum_id = bbp_get_forum_id( $forum_id );
1284 $forum_count = (int) get_post_meta( $forum_id, '_bbp_forum_subforum_count', true );
1285 $filter = ( true === $integer )
1286 ? 'bbp_get_forum_subforum_count_int'
1287 : 'bbp_get_forum_subforum_count';
1288
1289 return apply_filters( $filter, $forum_count, $forum_id );
1290 }
1291
1292 /**
1293 * Output total topic count of a forum
1294 *
1295 * @since 2.0.0 bbPress (r2464)
1296 *
1297 * @param int $forum_id Optional. Forum id
1298 * @param bool $total_count Optional. To get the total count or normal count?
1299 * @param boolean $integer Optional. Whether or not to format the result
1300 */
1301 function bbp_forum_topic_count( $forum_id = 0, $total_count = true, $integer = false ) {
1302 echo bbp_get_forum_topic_count( $forum_id, $total_count, $integer );
1303 }
1304 /**
1305 * Return total topic count of a forum
1306 *
1307 * @since 2.0.0 bbPress (r2464)
1308 *
1309 * @param int $forum_id Optional. Forum id
1310 * @param bool $total_count Optional. To get the total count or normal
1311 * count? Defaults to total.
1312 * @param boolean $integer Optional. Whether or not to format the result
1313 * @return int Forum topic count
1314 */
1315 function bbp_get_forum_topic_count( $forum_id = 0, $total_count = true, $integer = false ) {
1316 $forum_id = bbp_get_forum_id( $forum_id );
1317 $meta_key = empty( $total_count ) ? '_bbp_topic_count' : '_bbp_total_topic_count';
1318 $topics = (int) get_post_meta( $forum_id, $meta_key, true );
1319 $filter = ( true === $integer )
1320 ? 'bbp_get_forum_topic_count_int'
1321 : 'bbp_get_forum_topic_count';
1322
1323 return apply_filters( $filter, $topics, $forum_id );
1324 }
1325
1326 /**
1327 * Output total reply count of a forum
1328 *
1329 * @since 2.0.0 bbPress (r2615)
1330 *
1331 * @param int $forum_id Optional. Forum id
1332 * @param bool $total_count Optional. To get the total count or normal count?
1333 * @param boolean $integer Optional. Whether or not to format the result
1334 */
1335 function bbp_forum_reply_count( $forum_id = 0, $total_count = true, $integer = false ) {
1336 echo bbp_get_forum_reply_count( $forum_id, $total_count, $integer );
1337 }
1338 /**
1339 * Return total post count of a forum
1340 *
1341 * @since 2.0.0 bbPress (r2615)
1342 *
1343 * @param int $forum_id Optional. Forum id
1344 * @param bool $total_count Optional. To get the total count or normal
1345 * count?
1346 * @param boolean $integer Optional. Whether or not to format the result
1347 * @return int Forum reply count
1348 */
1349 function bbp_get_forum_reply_count( $forum_id = 0, $total_count = true, $integer = false ) {
1350 $forum_id = bbp_get_forum_id( $forum_id );
1351 $meta_key = empty( $total_count ) ? '_bbp_reply_count' : '_bbp_total_reply_count';
1352 $replies = (int) get_post_meta( $forum_id, $meta_key, true );
1353 $filter = ( true === $integer )
1354 ? 'bbp_get_forum_reply_count_int'
1355 : 'bbp_get_forum_reply_count';
1356
1357 return apply_filters( $filter, $replies, $forum_id );
1358 }
1359
1360 /**
1361 * Output total post count of a forum
1362 *
1363 * @since 2.0.0 bbPress (r2464)
1364 *
1365 * @param int $forum_id Optional. Forum id
1366 * @param bool $total_count Optional. To get the total count or normal count?
1367 * @param boolean $integer Optional. Whether or not to format the result
1368 */
1369 function bbp_forum_post_count( $forum_id = 0, $total_count = true, $integer = false ) {
1370 echo bbp_get_forum_post_count( $forum_id, $total_count, $integer );
1371 }
1372 /**
1373 * Return total post count of a forum
1374 *
1375 * @since 2.0.0 bbPress (r2464)
1376 *
1377 * @param int $forum_id Optional. Forum id
1378 * @param bool $total_count Optional. To get the total count or normal
1379 * count?
1380 * @param boolean $integer Optional. Whether or not to format the result
1381 * @return int Forum post count
1382 */
1383 function bbp_get_forum_post_count( $forum_id = 0, $total_count = true, $integer = false ) {
1384 $forum_id = bbp_get_forum_id( $forum_id );
1385 $topics = bbp_get_forum_topic_count( $forum_id, $total_count, true );
1386 $replies = bbp_get_forum_reply_count( $forum_id, $total_count, true );
1387 $retval = ( $replies + $topics );
1388 $filter = ( true === $integer )
1389 ? 'bbp_get_forum_post_count_int'
1390 : 'bbp_get_forum_post_count';
1391
1392 return apply_filters( $filter, $retval, $forum_id );
1393 }
1394
1395 /**
1396 * Output total hidden topic count of a forum (hidden includes trashed, spammed,
1397 * and pending topics)
1398 *
1399 * @since 2.0.0 bbPress (r3349)
1400 * @since 2.6.0 bbPress (r6922) Changed function signature to add total counts
1401 *
1402 * @param int $forum_id Optional. Forum id
1403 * @param bool $total_count Optional. To get the total count or normal count?
1404 * @param boolean $integer Optional. Whether or not to format the result
1405 */
1406 function bbp_forum_topic_count_hidden( $forum_id = 0, $total_count = true, $integer = null ) {
1407 echo bbp_get_forum_topic_count_hidden( $forum_id, $total_count, $integer );
1408 }
1409 /**
1410 * Return total hidden topic count of a forum (hidden includes trashed,
1411 * spammed and pending topics)
1412 *
1413 * @since 2.0.0 bbPress (r3349)
1414 * @since 2.6.0 bbPress (r6922) Changed function signature to add total counts
1415 *
1416 * @param int $forum_id Optional. Forum id
1417 * @param bool $total_count Optional. To get the total count or normal count?
1418 * @param boolean $integer Optional. Whether or not to format the result
1419 * @return int Topic hidden topic count
1420 */
1421 function bbp_get_forum_topic_count_hidden( $forum_id = 0, $total_count = true, $integer = null ) {
1422 $forum_id = bbp_get_forum_id( $forum_id );
1423 $meta_key = empty( $total_count ) ? '_bbp_topic_count_hidden' : '_bbp_total_topic_count_hidden';
1424 $topics = (int) get_post_meta( $forum_id, $meta_key, true );
1425 $filter = ( true === $integer )
1426 ? 'bbp_get_forum_topic_count_hidden_int'
1427 : 'bbp_get_forum_topic_count_hidden';
1428
1429 return apply_filters( $filter, $topics, $forum_id );
1430 }
1431
1432 /**
1433 * Output total hidden reply count of a forum (hidden includes trashed, spammed,
1434 * and pending replies)
1435 *
1436 * @since 2.6.0 bbPress (r6923)
1437 *
1438 * @param int $forum_id Optional. Forum id
1439 * @param bool $total_count Optional. To get the total count or normal count?
1440 * @param boolean $integer Optional. Whether or not to format the result
1441 */
1442 function bbp_forum_reply_count_hidden( $forum_id = 0, $total_count = true, $integer = false ) {
1443 echo bbp_get_forum_reply_count_hidden( $forum_id, $total_count, $integer );
1444 }
1445 /**
1446 * Return total hidden reply count of a forum (hidden includes trashed,
1447 * spammed and pending replies)
1448 *
1449 * @since 2.6.0 bbPress (r6923)
1450 *
1451 * @param int $forum_id Optional. Forum id
1452 * @param bool $total_count Optional. To get the total count or normal
1453 * count?
1454 * @param boolean $integer Optional. Whether or not to format the result
1455 * @return int Forum reply count
1456 */
1457 function bbp_get_forum_reply_count_hidden( $forum_id = 0, $total_count = true, $integer = false ) {
1458 $forum_id = bbp_get_forum_id( $forum_id );
1459 $meta_key = empty( $total_count ) ? '_bbp_reply_count_hidden' : '_bbp_total_reply_count_hidden';
1460 $replies = (int) get_post_meta( $forum_id, $meta_key, true );
1461 $filter = ( true === $integer )
1462 ? 'bbp_get_forum_reply_count_hidden_int'
1463 : 'bbp_get_forum_reply_count_hidden';
1464
1465 return apply_filters( $filter, $replies, $forum_id );
1466 }
1467
1468 /**
1469 * Output the status of the forum
1470 *
1471 * @since 2.0.0 bbPress (r2678)
1472 *
1473 * @param int $forum_id Optional. Forum id
1474 */
1475 function bbp_forum_status( $forum_id = 0 ) {
1476 echo bbp_get_forum_status( $forum_id );
1477 }
1478 /**
1479 * Return the status of the forum
1480 *
1481 * @since 2.0.0 bbPress (r2678)
1482 *
1483 * @param int $forum_id Optional. Forum id
1484 * @return string Status of forum
1485 */
1486 function bbp_get_forum_status( $forum_id = 0 ) {
1487 $forum_id = bbp_get_forum_id( $forum_id );
1488 $status = get_post_meta( $forum_id, '_bbp_status', true );
1489
1490 if ( empty( $status ) ) {
1491 $status = 'open';
1492 }
1493
1494 // Filter & return
1495 return apply_filters( 'bbp_get_forum_status', $status, $forum_id );
1496 }
1497
1498 /**
1499 * Output the visibility of the forum
1500 *
1501 * @since 2.0.0 bbPress (r2998)
1502 *
1503 * @param int $forum_id Optional. Forum id
1504 */
1505 function bbp_forum_visibility( $forum_id = 0 ) {
1506 echo bbp_get_forum_visibility( $forum_id );
1507 }
1508 /**
1509 * Return the visibility of the forum
1510 *
1511 * @since 2.0.0 bbPress (r2998)
1512 *
1513 * @param int $forum_id Optional. Forum id
1514 * @return string Status of forum
1515 */
1516 function bbp_get_forum_visibility( $forum_id = 0 ) {
1517 $forum_id = bbp_get_forum_id( $forum_id );
1518 $visibility = get_post_status( $forum_id );
1519
1520 // Filter & return
1521 return apply_filters( 'bbp_get_forum_visibility', $visibility, $forum_id );
1522 }
1523
1524 /**
1525 * Output the type of the forum
1526 *
1527 * @since 2.1.0 bbPress (r3563)
1528 *
1529 * @param int $forum_id Optional. Forum id
1530 */
1531 function bbp_forum_type( $forum_id = 0 ) {
1532 echo bbp_get_forum_type( $forum_id );
1533 }
1534 /**
1535 * Return the type of forum (category/forum/etc...)
1536 *
1537 * @since 2.1.0 bbPress (r3563)
1538 *
1539 * @param int $forum_id Optional. Forum id
1540 * @return bool Whether the forum is a category or not
1541 */
1542 function bbp_get_forum_type( $forum_id = 0 ) {
1543 $forum_id = bbp_get_forum_id( $forum_id );
1544 $retval = get_post_meta( $forum_id, '_bbp_forum_type', true );
1545
1546 if ( empty( $retval ) ) {
1547 $retval = 'forum';
1548 }
1549
1550 // Filter & return
1551 return apply_filters( 'bbp_get_forum_type', $retval, $forum_id );
1552 }
1553
1554 /**
1555 * Is the forum a category?
1556 *
1557 * @since 2.0.0 bbPress (r2746)
1558 *
1559 * @param int $forum_id Optional. Forum id
1560 * @return bool Whether the forum is a category or not
1561 */
1562 function bbp_is_forum_category( $forum_id = 0 ) {
1563 $forum_id = bbp_get_forum_id( $forum_id );
1564 $type = bbp_get_forum_type( $forum_id );
1565 $retval = ( ! empty( $type ) && 'category' === $type );
1566
1567 // Filter & return
1568 return (bool) apply_filters( 'bbp_is_forum_category', (bool) $retval, $forum_id );
1569 }
1570
1571 /**
1572 * Is the forum open?
1573 *
1574 * @since 2.0.0 bbPress (r2746)
1575 *
1576 * @param int $forum_id Optional. Forum id
1577 * @param bool $check_ancestors Check if the ancestors are open (only
1578 * if they're a category)
1579 * @return bool Whether the forum is open or not
1580 */
1581 function bbp_is_forum_open( $forum_id = 0, $check_ancestors = true ) {
1582 return ! bbp_is_forum_closed( $forum_id, $check_ancestors );
1583 }
1584
1585 /**
1586 * Is the forum closed?
1587 *
1588 * @since 2.0.0 bbPress (r2746)
1589 *
1590 * @param int $forum_id Optional. Forum id
1591 * @param bool $check_ancestors Check if the ancestors are closed (only
1592 * if they're a category)
1593 * @return bool True if closed, false if not
1594 */
1595 function bbp_is_forum_closed( $forum_id = 0, $check_ancestors = true ) {
1596
1597 // Get the forum ID
1598 $forum_id = bbp_get_forum_id( $forum_id );
1599
1600 // Check if the forum or one of it's ancestors is closed
1601 $retval = bbp_is_forum_status( $forum_id, bbp_get_closed_status_id(), $check_ancestors, 'OR' );
1602
1603 // Filter & return
1604 return (bool) apply_filters( 'bbp_is_forum_closed', (bool) $retval, $forum_id, $check_ancestors );
1605 }
1606
1607 /**
1608 * Check if the forum status is a specific one, also maybe checking ancestors
1609 *
1610 * @since 2.6.0 bbPress (r5499)
1611 *
1612 * @param bool $status_name The forum status name to check
1613 * @param bool $check_ancestors Check the forum ancestors
1614 * @param string $operator The logical operation to perform.
1615 * 'OR' means only one forum from the tree needs to match;
1616 * 'AND' means all forums must match. The default is 'AND'.
1617 * @return bool True if match, false if not
1618 */
1619 function bbp_is_forum_status( $forum_id, $status_name, $check_ancestors = true, $operator = 'AND' ) {
1620
1621 // Setup some default variables
1622 $count = 0;
1623 $retval = false;
1624 $operator = strtoupper( $operator );
1625 $forum_id = bbp_get_forum_id( $forum_id );
1626 $forum_status = bbp_get_forum_status( $forum_id );
1627
1628 // Quickly compare statuses of first forum ID
1629 if ( $status_name === $forum_status ) {
1630 $retval = true;
1631 $count++;
1632 }
1633
1634 // Let's check the forum's ancestors too
1635 if ( ! empty( $check_ancestors ) ) {
1636
1637 // Adjust the ancestor check based on the count
1638 switch ( $operator ) {
1639 default:
1640 case 'AND':
1641 $check_ancestors = ( $count > 0 );
1642 break;
1643
1644 case 'OR':
1645 $check_ancestors = ( $count < 1 );
1646 break;
1647 }
1648
1649 // Ancestor check passed, so continue looping through them
1650 if ( ! empty( $check_ancestors ) ) {
1651
1652 // Loop through the forum ancestors
1653 foreach ( (array) bbp_get_forum_ancestors( $forum_id ) as $ancestor ) {
1654
1655 // Check if the forum is a category
1656 if ( bbp_is_forum_category( $ancestor ) ) {
1657
1658 // Check the ancestor forum status
1659 $retval = bbp_is_forum_status( $ancestor, $status_name, false );
1660 if ( true === $retval ) {
1661 ++$count;
1662 }
1663 }
1664
1665 // Break when it reach the max count
1666 if ( ( 'OR' === $operator ) && ( $count >= 1 ) ) {
1667 break;
1668 }
1669 }
1670 }
1671 }
1672
1673 // Filter & return
1674 return (bool) apply_filters( 'bbp_is_forum_status', $retval, $count, $forum_id, $status_name, $check_ancestors, $operator );
1675 }
1676
1677 /**
1678 * Is the forum public?
1679 *
1680 * @since 2.0.0 bbPress (r2998)
1681 *
1682 * @param int $forum_id Optional. Forum id
1683 * @param bool $check_ancestors Check if the ancestors are public
1684 * @return bool True if closed, false if not
1685 */
1686 function bbp_is_forum_public( $forum_id = 0, $check_ancestors = true ) {
1687
1688 // Get the forum ID
1689 $forum_id = bbp_get_forum_id( $forum_id );
1690
1691 // Check if the forum and all of it's ancestors are public
1692 $retval = bbp_is_forum_visibility( $forum_id, bbp_get_public_status_id(), $check_ancestors );
1693
1694 // Filter & return
1695 return (bool) apply_filters( 'bbp_is_forum_public', $retval, $forum_id, $check_ancestors );
1696 }
1697
1698 /**
1699 * Is the forum private?
1700 *
1701 * @since 2.0.0 bbPress (r2746)
1702 *
1703 * @param int $forum_id Optional. Forum id
1704 * @param bool $check_ancestors Check if the ancestors are private
1705 * @return bool True if private, false if not
1706 */
1707 function bbp_is_forum_private( $forum_id = 0, $check_ancestors = true ) {
1708
1709 // Get the forum ID
1710 $forum_id = bbp_get_forum_id( $forum_id );
1711
1712 // Check if the forum or one of it's ancestors is private
1713 $retval = bbp_is_forum_visibility( $forum_id, bbp_get_private_status_id(), $check_ancestors, 'OR' );
1714
1715 // Filter & return
1716 return (bool) apply_filters( 'bbp_is_forum_private', $retval, $forum_id, $check_ancestors );
1717 }
1718
1719 /**
1720 * Is the forum hidden?
1721 *
1722 * @since 2.0.0 bbPress (r2998)
1723 *
1724 * @param int $forum_id Optional. Forum id
1725 * @param bool $check_ancestors Check if the ancestors are private (only if
1726 * they're a category)
1727 * @return bool True if hidden, false if not
1728 */
1729 function bbp_is_forum_hidden( $forum_id = 0, $check_ancestors = true ) {
1730
1731 // Get the forum ID
1732 $forum_id = bbp_get_forum_id( $forum_id );
1733
1734 // Check if the forum or one of it's ancestors is hidden
1735 $retval = bbp_is_forum_visibility( $forum_id, bbp_get_hidden_status_id(), $check_ancestors, 'OR' );
1736
1737 // Filter & return
1738 return (bool) apply_filters( 'bbp_is_forum_hidden', $retval, $forum_id, $check_ancestors );
1739 }
1740
1741 /**
1742 * Is the forum private or hidden?
1743 *
1744 * @since 2.6.17 bbPress (r7474)
1745 *
1746 * @param int $forum_id Optional. Forum id.
1747 * @param bool $check_ancestors Whether to check the forum ancestors.
1748 * @return bool True if private or hidden, false if not.
1749 */
1750 function bbp_is_forum_restricted( $forum_id = 0, $check_ancestors = false ) {
1751 $forum_id = bbp_get_forum_id( $forum_id );
1752 $retval = bbp_is_forum_private( $forum_id, $check_ancestors ) || bbp_is_forum_hidden( $forum_id, $check_ancestors );
1753
1754 // Filter & return
1755 return (bool) apply_filters( 'bbp_is_forum_restricted', $retval, $forum_id, $check_ancestors );
1756 }
1757
1758 /**
1759 * Is the forum restricted from a user?
1760 *
1761 * Checks the forum and its ancestors for private or hidden visibility, and
1762 * whether the user is a forum moderator or can otherwise read the forum.
1763 *
1764 * @since 2.6.17 bbPress (r7504)
1765 *
1766 * @param int $forum_id Optional. Forum ID.
1767 * @param int $user_id Optional. User ID. Defaults to 0.
1768 * @return bool True if the forum is restricted from the user, false if not.
1769 */
1770 function bbp_is_forum_restricted_for_user( $forum_id = 0, $user_id = 0 ) {
1771 $forum_id = bbp_get_forum_id( $forum_id );
1772 $user_id = ! empty( $user_id )
1773 ? bbp_get_user_id( $user_id, false, false )
1774 : 0;
1775 $retval = ! empty( $forum_id )
1776 && bbp_is_forum_restricted( $forum_id, true )
1777 && ( empty( $user_id ) || ! bbp_is_user_forum_moderator( $user_id, $forum_id ) )
1778 && ! user_can( $user_id, 'read_forum', $forum_id );
1779
1780 // Filter & return
1781 return (bool) apply_filters( 'bbp_is_forum_restricted_for_user', $retval, $forum_id, $user_id );
1782 }
1783
1784 /**
1785 * Check the forum visibility ID
1786 *
1787 * @since 2.6.0 bbPress (r5499)
1788 *
1789 * @param int $forum_id Optional. Forum id
1790 * @param bool $status_name The post status name to check
1791 * @param bool $check_ancestors Check the forum ancestors
1792 * @param string $operator The logical operation to perform.
1793 * 'OR' means only one forum from the tree needs to match;
1794 * 'AND' means all forums must match. The default is 'AND'.
1795 * @return bool True if match, false if not
1796 */
1797 function bbp_is_forum_visibility( $forum_id, $status_name, $check_ancestors = true, $operator = 'AND' ) {
1798
1799 // Setup some default variables
1800 $count = 0;
1801 $retval = false;
1802 $operator = strtoupper( $operator );
1803 $forum_id = bbp_get_forum_id( $forum_id );
1804 $visibility = bbp_get_forum_visibility( $forum_id );
1805
1806 // Quickly compare visibility of first forum ID
1807 if ( $status_name === $visibility ) {
1808 $retval = true;
1809 $count++;
1810 }
1811
1812 // Let's check the forum's ancestors too
1813 if ( ! empty( $check_ancestors ) ) {
1814
1815 // Adjust the ancestor check based on the count
1816 switch ( $operator ) {
1817
1818 // Adjust the ancestor check based on the count
1819 default:
1820 case 'AND':
1821 $check_ancestors = ( $count > 0 );
1822 break;
1823
1824 case 'OR':
1825 $check_ancestors = ( $count < 1 );
1826 break;
1827 }
1828
1829 // Ancestor check passed, so continue looping through them
1830 if ( ! empty( $check_ancestors ) ) {
1831
1832 // Loop through the forum ancestors
1833 foreach ( (array) bbp_get_forum_ancestors( $forum_id ) as $ancestor ) {
1834
1835 // Check if the forum is not a category
1836 if ( bbp_is_forum( $ancestor ) ) {
1837
1838 // Check the forum visibility
1839 $ancestor_matches = bbp_is_forum_visibility( $ancestor, $status_name, false );
1840 if ( true === $ancestor_matches ) {
1841 ++$count;
1842 }
1843
1844 // Combine visibility across the whole forum hierarchy.
1845 $retval = ( 'OR' === $operator )
1846 ? ( $retval || $ancestor_matches )
1847 : ( $retval && $ancestor_matches );
1848 }
1849
1850 // Break when it reach the max count
1851 if ( ( 'OR' === $operator ) && ( $count >= 1 ) ) {
1852 break;
1853 }
1854 }
1855 }
1856 }
1857
1858 // Filter & return
1859 return (bool) apply_filters( 'bbp_is_forum_visibility', $retval, $count, $forum_id, $status_name, $check_ancestors, $operator );
1860 }
1861
1862 /**
1863 * Output the author ID of the forum
1864 *
1865 * @since 2.1.0 bbPress (r3678)
1866 *
1867 * @param int $forum_id Optional. Forum id
1868 */
1869 function bbp_forum_author_id( $forum_id = 0 ) {
1870 echo bbp_get_forum_author_id( $forum_id );
1871 }
1872 /**
1873 * Return the author ID of the forum
1874 *
1875 * @since 2.1.0 bbPress (r3678)
1876 *
1877 * @param int $forum_id Optional. Forum id
1878 *
1879 * @return string Author of forum
1880 */
1881 function bbp_get_forum_author_id( $forum_id = 0 ) {
1882 $forum_id = bbp_get_forum_id( $forum_id );
1883 $author_id = get_post_field( 'post_author', $forum_id );
1884
1885 // Filter & return
1886 return (int) apply_filters( 'bbp_get_forum_author_id', (int) $author_id, $forum_id );
1887 }
1888
1889 /**
1890 * Output the author of the forum
1891 *
1892 * @since 2.1.0 bbPress (r3678)
1893 *
1894 * @param int $forum_id Optional. Forum id
1895 */
1896 function bbp_forum_author_display_name( $forum_id = 0 ) {
1897 echo bbp_get_forum_author_display_name( $forum_id );
1898 }
1899 /**
1900 * Return the author of the forum
1901 *
1902 * @since 2.1.0 bbPress (r3678)
1903 *
1904 * @param int $forum_id Optional. Forum id
1905 * @return string Author of forum
1906 */
1907 function bbp_get_forum_author_display_name( $forum_id = 0 ) {
1908 $forum_id = bbp_get_forum_id( $forum_id );
1909 $author_id = bbp_get_forum_author_id( $forum_id );
1910 $author = get_the_author_meta( 'display_name', $author_id );
1911
1912 // Filter & return
1913 return apply_filters( 'bbp_get_forum_author_display_name', $author, $forum_id, $author_id );
1914 }
1915
1916 /**
1917 * Replace forum meta details for users that cannot view them.
1918 *
1919 * Forum visibility is inherited from every ancestor. A public descendant of a
1920 * private or hidden forum remains restricted to users who can read the
1921 * restricting ancestor. Aggregate metadata must therefore be checked against
1922 * the forum that supplied it, rather than only the forum where it is rendered.
1923 *
1924 * @since 2.0.0 bbPress (r3008)
1925 *
1926 * @param string $retval
1927 * @param int $forum_id
1928 * @param string $time_since
1929 * @param string $link_url
1930 * @param string $title
1931 * @param int $active_id
1932 *
1933 * @return string
1934 */
1935 function bbp_suppress_private_forum_meta( $retval, $forum_id, $time_since = '', $link_url = '', $title = '', $active_id = 0 ) {
1936
1937 // Check the source forum when filtering a freshness link
1938 if ( ! empty( $active_id ) ) {
1939 if ( bbp_is_topic( $active_id ) ) {
1940 $forum_id = bbp_get_topic_forum_id( $active_id );
1941 } elseif ( bbp_is_reply( $active_id ) ) {
1942 $forum_id = bbp_get_reply_forum_id( $active_id );
1943 }
1944 }
1945
1946 // Suppress if this forum or an ancestor is restricted from the current user
1947 if ( bbp_is_forum_restricted_for_user( $forum_id, bbp_get_current_user_id() ) ) {
1948 $retval = '-';
1949 }
1950
1951 // Filter & return
1952 return apply_filters( 'bbp_suppress_private_forum_meta', $retval );
1953 }
1954
1955 /**
1956 * Replace forum author details for users that cannot view them.
1957 *
1958 * @since 2.0.0 bbPress (r3008)
1959 *
1960 * @param string $author_link
1961 * @param array $args
1962 * @param array|int $original_args Original author-link arguments.
1963 *
1964 * @return string
1965 */
1966 function bbp_suppress_private_author_link( $author_link = '', $args = array(), $original_args = array() ) {
1967
1968 // Assume the author link is the return value
1969 $retval = $author_link;
1970 $post_id = ! empty( $args['post_id'] )
1971 ? (int) $args['post_id']
1972 : ( is_numeric( $original_args ) ? (int) $original_args : 0 );
1973
1974 // Suppress non-public authors unless this user moderates their forum
1975 if ( ! empty( $post_id ) && ( bbp_is_topic( $post_id ) || bbp_is_reply( $post_id ) ) && ! bbp_is_forum_activity_public( $post_id ) && ! current_user_can( 'moderate', $post_id ) ) {
1976 $retval = '-';
1977 }
1978
1979 // Show the normal author link
1980 if ( ! empty( $post_id ) ) {
1981
1982 // What post type are we looking at?
1983 switch ( get_post_type( $post_id ) ) {
1984
1985 // Topic
1986 case bbp_get_topic_post_type() :
1987 $forum_id = bbp_get_topic_forum_id( $post_id );
1988 break;
1989
1990 // Reply
1991 case bbp_get_reply_post_type() :
1992 $forum_id = bbp_get_reply_forum_id( $post_id );
1993 break;
1994
1995 // Post
1996 default :
1997 $forum_id = bbp_get_forum_id( $post_id );
1998 break;
1999 }
2000
2001 // Suppress if this forum or an ancestor is restricted from the current user
2002 if ( bbp_is_forum_restricted_for_user( $forum_id, bbp_get_current_user_id() ) ) {
2003 $retval = '';
2004 }
2005 }
2006
2007 // Filter & return
2008 return apply_filters( 'bbp_suppress_private_author_link', $retval, $author_link, $args );
2009 }
2010
2011 /**
2012 * Output the row class of a forum
2013 *
2014 * @since 2.0.0 bbPress (r2678)
2015 *
2016 * @param int $forum_id Optional. Forum ID.
2017 * @param array Extra classes you can pass when calling this function
2018 */
2019 function bbp_forum_class( $forum_id = 0, $classes = array() ) {
2020 echo bbp_get_forum_class( $forum_id, $classes );
2021 }
2022 /**
2023 * Return the row class of a forum
2024 *
2025 * @since 2.0.0 bbPress (r2678)
2026 *
2027 * @param int $forum_id Optional. Forum ID
2028 * @param array Extra classes you can pass when calling this function
2029 * @return string Row class of the forum
2030 */
2031 function bbp_get_forum_class( $forum_id = 0, $classes = array() ) {
2032 $bbp = bbpress();
2033 $forum_id = bbp_get_forum_id( $forum_id );
2034 $parent_id = bbp_get_forum_parent_id( $forum_id );
2035 $author_id = bbp_get_forum_author_id( $forum_id );
2036 $status = bbp_get_forum_status( $forum_id );
2037 $visibility = bbp_get_forum_visibility( $forum_id );
2038 $classes = array_filter( (array) $classes );
2039 $count = isset( $bbp->forum_query->current_post )
2040 ? (int) $bbp->forum_query->current_post
2041 : 1;
2042
2043 // Stripes
2044 $even_odd = ( $count % 2 )
2045 ? 'even'
2046 : 'odd';
2047
2048 // User is moderator of forum
2049 $forum_moderator = ( bbp_is_user_forum_moderator( $author_id, $forum_id ) === $author_id )
2050 ? 'forum-mod'
2051 : '';
2052
2053 // Is forum a non-postable category?
2054 $category = bbp_is_forum_category( $forum_id )
2055 ? 'status-category'
2056 : '';
2057
2058 // Forum has children?
2059 $subs = bbp_get_forum_subforum_count( $forum_id )
2060 ? 'bbp-has-subforums'
2061 : '';
2062
2063 // Forum has parent?
2064 $parent = ! empty( $parent_id )
2065 ? 'bbp-parent-forum-' . $parent_id
2066 : '';
2067
2068 // Get forum classes
2069 $forum_classes = array(
2070 'loop-item-' . $count,
2071 'bbp-forum-status-' . $status,
2072 'bbp-forum-visibility-' . $visibility,
2073 $even_odd,
2074 $forum_moderator,
2075 $category,
2076 $subs,
2077 $parent
2078 );
2079
2080 // Run the topic classes through the post-class filters, which also
2081 // handles the escaping of each individual class.
2082 $post_classes = get_post_class( array_merge( $classes, $forum_classes ), $forum_id );
2083
2084 // Filter
2085 $new_classes = apply_filters( 'bbp_get_forum_class', $post_classes, $forum_id, $classes );
2086
2087 // Return
2088 return 'class="' . implode( ' ', $new_classes ) . '"';
2089 }
2090
2091 /** Single Forum **************************************************************/
2092
2093 /**
2094 * Output a fancy description of the current forum, including total topics,
2095 * total replies, and last activity.
2096 *
2097 * @since 2.0.0 bbPress (r2876)
2098 *
2099 * @param array $args Arguments passed to alter output
2100 */
2101 function bbp_single_forum_description( $args = array() ) {
2102 echo bbp_get_single_forum_description( $args );
2103 }
2104 /**
2105 * Return a fancy description of the current forum, including total
2106 * topics, total replies, and last activity.
2107 *
2108 * @since 2.0.0 bbPress (r2876)
2109 *
2110 * @param array $args This function supports these arguments:
2111 * - forum_id: Forum id
2112 * - before: Before the text
2113 * - after: After the text
2114 * - size: Size of the avatar
2115 * @return string Filtered forum description
2116 */
2117 function bbp_get_single_forum_description( $args = array() ) {
2118
2119 // Parse arguments against default values
2120 $r = bbp_parse_args(
2121 $args,
2122 array(
2123 'forum_id' => 0,
2124 'before' => '<div class="bbp-template-notice info"><ul><li class="bbp-forum-description">',
2125 'after' => '</li></ul></div>',
2126 'size' => 14,
2127 'feed' => true
2128 ),
2129 'get_single_forum_description'
2130 );
2131
2132 // Validate forum_id
2133 $forum_id = bbp_get_forum_id( $r['forum_id'] );
2134
2135 // Unhook the 'view all' query var adder
2136 remove_filter( 'bbp_get_forum_permalink', 'bbp_add_view_all' );
2137
2138 // Get some forum data
2139 $tc_int = bbp_get_forum_topic_count( $forum_id, true, true );
2140 $rc_int = bbp_get_forum_reply_count( $forum_id, true, true );
2141 $topic_count = bbp_get_forum_topic_count( $forum_id, true, false );
2142 $reply_count = bbp_get_forum_reply_count( $forum_id, true, false );
2143 $last_active = bbp_get_forum_last_active_id( $forum_id );
2144 if ( ! empty( $last_active ) && ! bbp_is_forum_activity_public( $last_active ) ) {
2145 $last_active = 0;
2146 }
2147
2148 // Has replies
2149 if ( ! empty( $reply_count ) ) {
2150 /* translators: %s: Number of replies */
2151 $reply_text = sprintf( _n( '%s reply', '%s replies', $rc_int, 'bbpress' ), $reply_count );
2152 }
2153
2154 // Forum has active data
2155 if ( ! empty( $last_active ) ) {
2156 $topic_text = bbp_get_forum_topics_link( $forum_id );
2157 $time_since = bbp_get_forum_freshness_link( $forum_id );
2158 $last_updated_by = bbp_get_author_link(
2159 array(
2160 'post_id' => $last_active,
2161 'size' => $r['size']
2162 )
2163 );
2164
2165 // Forum has no last active data
2166 } else {
2167 /* translators: %s: Number of topics */
2168 $topic_text = sprintf( _n( '%s topic', '%s topics', $tc_int, 'bbpress' ), $topic_count );
2169 }
2170
2171 // Forum has active data
2172 if ( ! empty( $last_active ) ) {
2173
2174 // Has replies
2175 if ( ! empty( $reply_count ) ) {
2176 $retstr = bbp_is_forum_category( $forum_id )
2177 /* translators: 1: Topics link, 2: Replies text, 3: Last update time, 4: Author link */
2178 ? sprintf( esc_html__( 'This category has %1$s, %2$s, and was last updated %3$s by %4$s.', 'bbpress' ), $topic_text, $reply_text, $time_since, $last_updated_by )
2179 /* translators: 1: Topics link, 2: Replies text, 3: Last update time, 4: Author link */
2180 : sprintf( esc_html__( 'This forum has %1$s, %2$s, and was last updated %3$s by %4$s.', 'bbpress' ), $topic_text, $reply_text, $time_since, $last_updated_by );
2181
2182 // Only has topics
2183 } else {
2184 $retstr = bbp_is_forum_category( $forum_id )
2185 /* translators: 1: Topics link, 2: Last update time, 3: Author link */
2186 ? sprintf( esc_html__( 'This category has %1$s, and was last updated %2$s by %3$s.', 'bbpress' ), $topic_text, $time_since, $last_updated_by )
2187 /* translators: 1: Topics link, 2: Last update time, 3: Author link */
2188 : sprintf( esc_html__( 'This forum has %1$s, and was last updated %2$s by %3$s.', 'bbpress' ), $topic_text, $time_since, $last_updated_by );
2189 }
2190
2191 // Forum has no last active data (but does have topics & replies)
2192 } elseif ( ! empty( $reply_count ) ) {
2193 $retstr = bbp_is_forum_category( $forum_id )
2194 /* translators: 1: Topics link, 2: Replies text */
2195 ? sprintf( esc_html__( 'This category has %1$s and %2$s.', 'bbpress' ), $topic_text, $reply_text )
2196 /* translators: 1: Topics link, 2: Replies text */
2197 : sprintf( esc_html__( 'This forum has %1$s and %2$s.', 'bbpress' ), $topic_text, $reply_text );
2198
2199 // Forum has no last active data or replies (but does have topics)
2200 } elseif ( ! empty( $topic_count ) ) {
2201 $retstr = bbp_is_forum_category( $forum_id )
2202 /* translators: 1: Topics link */
2203 ? sprintf( esc_html__( 'This category has %1$s.', 'bbpress' ), $topic_text )
2204 /* translators: 1: Topics link */
2205 : sprintf( esc_html__( 'This forum has %1$s.', 'bbpress' ), $topic_text );
2206
2207 // Forum is empty
2208 } else {
2209 $retstr = esc_html__( 'This forum is empty.', 'bbpress' );
2210 }
2211
2212 // Add the 'view all' filter back
2213 add_filter( 'bbp_get_forum_permalink', 'bbp_add_view_all' );
2214
2215 // Combine the elements together
2216 $retstr = $r['before'] . $retstr . $r['after'];
2217
2218 // Filter & return
2219 return apply_filters( 'bbp_get_single_forum_description', $retstr, $r, $args );
2220 }
2221
2222 /** Forms *********************************************************************/
2223
2224 /**
2225 * Output the value of forum title field
2226 *
2227 * @since 2.1.0 bbPress (r3551)
2228 */
2229 function bbp_form_forum_title() {
2230 echo bbp_get_form_forum_title();
2231 }
2232 /**
2233 * Return the value of forum title field
2234 *
2235 * @since 2.1.0 bbPress (r3551)
2236 *
2237 * @return string Value of forum title field
2238 */
2239 function bbp_get_form_forum_title() {
2240
2241 // Get _POST data
2242 if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_forum_title'] ) ) {
2243 $forum_title = wp_unslash( $_POST['bbp_forum_title'] );
2244
2245 // Get edit data
2246 } elseif ( bbp_is_forum_edit() ) {
2247 $forum_title = bbp_get_global_post_field( 'post_title', 'raw' );
2248
2249 // No data
2250 } else {
2251 $forum_title = '';
2252 }
2253
2254 // Filter & return
2255 return apply_filters( 'bbp_get_form_forum_title', $forum_title );
2256 }
2257
2258 /**
2259 * Output the value of forum content field
2260 *
2261 * @since 2.1.0 bbPress (r3551)
2262 */
2263 function bbp_form_forum_content() {
2264 echo bbp_get_form_forum_content();
2265 }
2266 /**
2267 * Return the value of forum content field
2268 *
2269 * @since 2.1.0 bbPress (r3551)
2270 *
2271 * @return string Value of forum content field
2272 */
2273 function bbp_get_form_forum_content() {
2274
2275 // Get _POST data
2276 if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_forum_content'] ) ) {
2277 $forum_content = wp_unslash( $_POST['bbp_forum_content'] );
2278
2279 // Get edit data
2280 } elseif ( bbp_is_forum_edit() ) {
2281 $forum_content = bbp_get_global_post_field( 'post_content', 'raw' );
2282
2283 // No data
2284 } else {
2285 $forum_content = '';
2286 }
2287
2288 // Filter & return
2289 return apply_filters( 'bbp_get_form_forum_content', $forum_content );
2290 }
2291
2292 /**
2293 * Output value of forum moderators field
2294 *
2295 * @since 2.6.0 bbPress (r6056)
2296 */
2297 function bbp_form_forum_moderators() {
2298 echo bbp_get_form_forum_moderators();
2299 }
2300 /**
2301 * Return value of forum moderators field
2302 *
2303 * @since 2.6.0 bbPress (r6056)
2304 *
2305 * @return string Value of forum mods field
2306 */
2307 function bbp_get_form_forum_moderators() {
2308
2309 // Default return value
2310 $forum_mods = '';
2311
2312 // Get _POST data
2313 if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_moderators'] ) ) {
2314 $forum_mods = wp_unslash( $_POST['bbp_moderators'] );
2315
2316 // Get edit data
2317 } elseif ( bbp_is_single_forum() || bbp_is_forum_edit() ) {
2318
2319 // Get the forum ID
2320 $forum_id = bbp_get_forum_id( get_the_ID() );
2321
2322 // Forum exists
2323 if ( ! empty( $forum_id ) ) {
2324
2325 // Get moderator IDs
2326 $user_ids = bbp_get_moderator_ids( $forum_id );
2327 if ( ! empty( $user_ids ) ) {
2328 $user_nicenames = bbp_get_user_nicenames_from_ids( $user_ids );
2329
2330 // Comma separate user nicenames
2331 if ( ! empty( $user_nicenames ) ) {
2332 $forum_mods = implode( ', ', wp_list_pluck( $user_nicenames, 'user_nicename' ) );
2333 }
2334 }
2335 }
2336 }
2337
2338 // Filter & return
2339 return apply_filters( 'bbp_get_form_forum_moderators', $forum_mods );
2340 }
2341
2342 /**
2343 * Output value of forum parent
2344 *
2345 * @since 2.1.0 bbPress (r3551)
2346 */
2347 function bbp_form_forum_parent() {
2348 echo bbp_get_form_forum_parent();
2349 }
2350 /**
2351 * Return value of forum parent
2352 *
2353 * @since 2.1.0 bbPress (r3551)
2354 *
2355 * @return string Value of topic content field
2356 */
2357 function bbp_get_form_forum_parent() {
2358
2359 // Get _POST data
2360 if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_forum_id'] ) ) {
2361 $forum_parent = (int) $_POST['bbp_forum_id'];
2362
2363 // Get edit data
2364 } elseif ( bbp_is_forum_edit() ) {
2365 $forum_parent = bbp_get_forum_parent_id();
2366
2367 // No data
2368 } else {
2369 $forum_parent = 0;
2370 }
2371
2372 // Filter & return
2373 return apply_filters( 'bbp_get_form_forum_parent', $forum_parent );
2374 }
2375
2376 /**
2377 * Output value of forum type
2378 *
2379 * @since 2.1.0 bbPress (r3563)
2380 */
2381 function bbp_form_forum_type() {
2382 echo bbp_get_form_forum_type();
2383 }
2384 /**
2385 * Return value of forum type
2386 *
2387 * @since 2.1.0 bbPress (r3563)
2388 *
2389 * @return string Value of topic content field
2390 */
2391 function bbp_get_form_forum_type() {
2392
2393 // Get _POST data
2394 if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_forum_type'] ) ) {
2395 $forum_type = sanitize_key( $_POST['bbp_forum_type'] );
2396
2397 // Get edit data
2398 } elseif ( bbp_is_forum_edit() ) {
2399 $forum_type = bbp_get_forum_type();
2400
2401 // No data
2402 } else {
2403 $forum_type = 'forum';
2404 }
2405
2406 // Filter & return
2407 return apply_filters( 'bbp_get_form_forum_type', $forum_type );
2408 }
2409
2410 /**
2411 * Output value of forum visibility
2412 *
2413 * @since 2.1.0 bbPress (r3563)
2414 */
2415 function bbp_form_forum_visibility() {
2416 echo bbp_get_form_forum_visibility();
2417 }
2418 /**
2419 * Return value of forum visibility
2420 *
2421 * @since 2.1.0 bbPress (r3563)
2422 *
2423 * @return string Value of topic content field
2424 */
2425 function bbp_get_form_forum_visibility() {
2426
2427 // Get _POST data
2428 if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_forum_visibility'] ) ) {
2429 $forum_visibility = sanitize_key( $_POST['bbp_forum_visibility'] );
2430
2431 // Get edit data
2432 } elseif ( bbp_is_forum_edit() ) {
2433 $forum_visibility = bbp_get_forum_visibility();
2434
2435 // No data
2436 } else {
2437 $forum_visibility = bbpress()->public_status_id;
2438 }
2439
2440 // Filter & return
2441 return apply_filters( 'bbp_get_form_forum_visibility', $forum_visibility );
2442 }
2443
2444 /**
2445 * Output checked value of forum subscription
2446 *
2447 * @since 2.5.0 bbPress (r5156)
2448 */
2449 function bbp_form_forum_subscribed() {
2450 echo bbp_get_form_forum_subscribed();
2451 }
2452 /**
2453 * Return checked value of forum subscription
2454 *
2455 * @since 2.5.0 bbPress (r5156)
2456 *
2457 * @return string Checked value of forum subscription
2458 */
2459 function bbp_get_form_forum_subscribed() {
2460
2461 // Default value
2462 $forum_subscribed = false;
2463
2464 // Get _POST data
2465 if ( bbp_is_forum_form_post_request() && isset( $_POST['bbp_forum_subscription'] ) ) {
2466 $forum_subscribed = (bool) $_POST['bbp_forum_subscription'];
2467
2468 // Get edit data
2469 } elseif ( bbp_is_forum_edit() || bbp_is_reply_edit() ) {
2470 $post_author = (int) bbp_get_global_post_field( 'post_author', 'raw' );
2471 $forum_subscribed = bbp_is_user_subscribed( $post_author, bbp_get_forum_id() );
2472
2473 // Get current status
2474 } elseif ( bbp_is_single_forum() ) {
2475 $forum_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id(), bbp_get_forum_id() );
2476 }
2477
2478 // Get checked output
2479 $checked = checked( $forum_subscribed, true, false );
2480
2481 // Filter & return
2482 return apply_filters( 'bbp_get_form_forum_subscribed', $checked, $forum_subscribed );
2483 }
2484
2485 /** Form Dropdowns ************************************************************/
2486
2487 /**
2488 * Output value forum type dropdown
2489 *
2490 * @since 2.1.0 bbPress (r3563)
2491 *
2492 * @param $args This function supports these arguments:
2493 * - select_id: Select id. Defaults to bbp_forum_type
2494 * - tab: Deprecated. Tabindex
2495 * - forum_id: Forum id
2496 * - selected: Override the selected option
2497 */
2498 function bbp_form_forum_type_dropdown( $args = array() ) {
2499 echo bbp_get_form_forum_type_dropdown( $args );
2500 }
2501 /**
2502 * Return the forum type dropdown
2503 *
2504 * @since 2.1.0 bbPress (r3563)
2505 *
2506 * @param $args This function supports these arguments:
2507 * - select_id: Select id. Defaults to bbp_forum_type
2508 * - tab: Deprecated. Tabindex
2509 * - forum_id: Forum id
2510 * - selected: Override the selected option
2511 * @return string HTML select list for selecting forum type
2512 */
2513 function bbp_get_form_forum_type_dropdown( $args = array() ) {
2514
2515 // Backpat for handling passing of a forum ID as integer
2516 if ( is_int( $args ) ) {
2517 $forum_id = (int) $args;
2518 $args = array();
2519 } else {
2520 $forum_id = 0;
2521 }
2522
2523 // Parse arguments against default values
2524 $r = bbp_parse_args(
2525 $args,
2526 array(
2527 'select_id' => 'bbp_forum_type',
2528 'select_class' => 'bbp_dropdown',
2529 'tab' => false,
2530 'forum_id' => $forum_id,
2531 'selected' => false
2532 ),
2533 'forum_type_select'
2534 );
2535
2536 // No specific selected value passed
2537 if ( empty( $r['selected'] ) ) {
2538
2539 // Post value is passed
2540 if ( bbp_is_forum_form_post_request() && isset( $_POST[ $r['select_id'] ] ) ) {
2541 $r['selected'] = sanitize_key( $_POST[ $r['select_id'] ] );
2542
2543 // Edit topic
2544 } elseif ( bbp_is_forum_edit() ) {
2545 $r['forum_id'] = bbp_get_forum_id( $r['forum_id'] );
2546 $r['selected'] = bbp_get_forum_type( $r['forum_id'] );
2547
2548 // New topic
2549 } else {
2550 $r['selected'] = bbp_get_public_status_id();
2551 }
2552 }
2553
2554 // Start an output buffer, we'll finish it after the select loop
2555 ob_start(); ?>
2556
2557 <select name="<?php echo esc_attr( $r['select_id'] ); ?>" id="<?php echo esc_attr( $r['select_id'] ); ?>_select" class="<?php echo esc_attr( $r['select_class'] ); ?>"<?php bbp_tab_index_attribute( $r['tab'] ); ?>>
2558
2559 <?php foreach ( bbp_get_forum_types( $r['forum_id'] ) as $key => $label ) : ?>
2560
2561 <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $r['selected'] ); ?>><?php echo esc_html( $label ); ?></option>
2562
2563 <?php endforeach; ?>
2564
2565 </select>
2566
2567 <?php
2568
2569 // Filter & return
2570 return apply_filters( 'bbp_get_form_forum_type_dropdown', ob_get_clean(), $r, $args );
2571 }
2572
2573 /**
2574 * Output value forum status dropdown
2575 *
2576 * @since 2.1.0 bbPress (r3563)
2577 *
2578 * @param $args This function supports these arguments:
2579 * - select_id: Select id. Defaults to bbp_forum_status
2580 * - tab: Deprecated. Tabindex
2581 * - forum_id: Forum id
2582 * - selected: Override the selected option
2583 */
2584 function bbp_form_forum_status_dropdown( $args = array() ) {
2585 echo bbp_get_form_forum_status_dropdown( $args );
2586 }
2587 /**
2588 * Return the forum status dropdown
2589 *
2590 * @since 2.1.0 bbPress (r3563)
2591 *
2592 * @param $args This function supports these arguments:
2593 * - select_id: Select id. Defaults to bbp_forum_status
2594 * - tab: Deprecated. Tabindex
2595 * - forum_id: Forum id
2596 * - selected: Override the selected option
2597 * @return string HTML select list for selecting forum status
2598 */
2599 function bbp_get_form_forum_status_dropdown( $args = array() ) {
2600
2601 // Backpat for handling passing of a forum ID
2602 if ( is_int( $args ) ) {
2603 $forum_id = (int) $args;
2604 $args = array();
2605 } else {
2606 $forum_id = 0;
2607 }
2608
2609 // Parse arguments against default values
2610 $r = bbp_parse_args(
2611 $args,
2612 array(
2613 'select_id' => 'bbp_forum_status',
2614 'select_class' => 'bbp_dropdown',
2615 'tab' => false,
2616 'forum_id' => $forum_id,
2617 'selected' => false
2618 ),
2619 'forum_status_select'
2620 );
2621
2622 // No specific selected value passed
2623 if ( empty( $r['selected'] ) ) {
2624
2625 // Post value is passed
2626 if ( bbp_is_forum_form_post_request() && isset( $_POST[ $r['select_id'] ] ) ) {
2627 $r['selected'] = sanitize_key( $_POST[ $r['select_id'] ] );
2628
2629 // Edit topic
2630 } elseif ( bbp_is_forum_edit() ) {
2631 $r['forum_id'] = bbp_get_forum_id( $r['forum_id'] );
2632 $r['selected'] = bbp_get_forum_status( $r['forum_id'] );
2633
2634 // New topic
2635 } else {
2636 $r['selected'] = bbp_get_public_status_id();
2637 }
2638 }
2639
2640 // Start an output buffer, we'll finish it after the select loop
2641 ob_start(); ?>
2642
2643 <select name="<?php echo esc_attr( $r['select_id'] ); ?>" id="<?php echo esc_attr( $r['select_id'] ); ?>_select" class="<?php echo esc_attr( $r['select_class'] ); ?>"<?php bbp_tab_index_attribute( $r['tab'] ); ?>>
2644
2645 <?php foreach ( bbp_get_forum_statuses( $r['forum_id'] ) as $key => $label ) : ?>
2646
2647 <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $r['selected'] ); ?>><?php echo esc_html( $label ); ?></option>
2648
2649 <?php endforeach; ?>
2650
2651 </select>
2652
2653 <?php
2654
2655 // Filter & return
2656 return apply_filters( 'bbp_get_form_forum_status_dropdown', ob_get_clean(), $r, $args );
2657 }
2658
2659 /**
2660 * Output value forum visibility dropdown
2661 *
2662 * @since 2.1.0 bbPress (r3563)
2663 *
2664 * @param $args This function supports these arguments:
2665 * - select_id: Select id. Defaults to bbp_forum_visibility
2666 * - tab: Deprecated. Tabindex
2667 * - forum_id: Forum id
2668 * - selected: Override the selected option
2669 */
2670 function bbp_form_forum_visibility_dropdown( $args = array() ) {
2671 echo bbp_get_form_forum_visibility_dropdown( $args );
2672 }
2673 /**
2674 * Return the forum visibility dropdown
2675 *
2676 * @since 2.1.0 bbPress (r3563)
2677 *
2678 * @param $args This function supports these arguments:
2679 * - select_id: Select id. Defaults to bbp_forum_visibility
2680 * - tab: Deprecated. Tabindex
2681 * - forum_id: Forum id
2682 * - selected: Override the selected option
2683 * @return string HTML select list for selecting forum visibility
2684 */
2685 function bbp_get_form_forum_visibility_dropdown( $args = array() ) {
2686
2687 // Backpat for handling passing of a forum ID
2688 if ( is_int( $args ) ) {
2689 $forum_id = (int) $args;
2690 $args = array();
2691 } else {
2692 $forum_id = 0;
2693 }
2694
2695 // Parse arguments against default values
2696 $r = bbp_parse_args(
2697 $args,
2698 array(
2699 'select_id' => 'bbp_forum_visibility',
2700 'select_class' => 'bbp_dropdown',
2701 'tab' => false,
2702 'forum_id' => $forum_id,
2703 'selected' => false
2704 ),
2705 'forum_type_select'
2706 );
2707
2708 // No specific selected value passed
2709 if ( empty( $r['selected'] ) ) {
2710
2711 // Post value is passed
2712 if ( bbp_is_forum_form_post_request() && isset( $_POST[ $r['select_id'] ] ) ) {
2713 $r['selected'] = sanitize_key( $_POST[ $r['select_id'] ] );
2714
2715 // Edit topic
2716 } elseif ( bbp_is_forum_edit() ) {
2717 $r['forum_id'] = bbp_get_forum_id( $r['forum_id'] );
2718 $r['selected'] = bbp_get_forum_visibility( $r['forum_id'] );
2719
2720 // New topic
2721 } else {
2722 $r['selected'] = bbp_get_public_status_id();
2723 }
2724 }
2725
2726 // Start an output buffer, we'll finish it after the select loop
2727 ob_start(); ?>
2728
2729 <select name="<?php echo esc_attr( $r['select_id'] ); ?>" id="<?php echo esc_attr( $r['select_id'] ); ?>_select" class="<?php echo esc_attr( $r['select_class'] ); ?>"<?php bbp_tab_index_attribute( $r['tab'] ); ?>>
2730
2731 <?php foreach ( bbp_get_forum_visibilities( $r['forum_id'] ) as $key => $label ) : ?>
2732
2733 <option value="<?php echo esc_attr( $key ); ?>"<?php selected( $key, $r['selected'] ); ?>><?php echo esc_html( $label ); ?></option>
2734
2735 <?php endforeach; ?>
2736
2737 </select>
2738
2739 <?php
2740
2741 // Filter & return
2742 return apply_filters( 'bbp_get_form_forum_type_dropdown', ob_get_clean(), $r, $args );
2743 }
2744
2745 /**
2746 * Verify if a POST request came from a failed forum attempt.
2747 *
2748 * Used to avoid cross-site request forgeries when checking posted forum form
2749 * content.
2750 *
2751 * @see bbp_forum_form_fields()
2752 *
2753 * @since 2.6.0 bbPress (r5558)
2754 *
2755 * @return boolean True if is a post request with valid nonce
2756 */
2757 function bbp_is_forum_form_post_request() {
2758
2759 // Bail if not a post request
2760 if ( ! bbp_is_post_request() ) {
2761 return false;
2762 }
2763
2764 // Creating a new forum
2765 if ( bbp_verify_nonce_request( 'bbp-new-forum' ) ) {
2766 return true;
2767 }
2768
2769 // Editing an existing forum
2770 if ( bbp_verify_nonce_request( 'bbp-edit-forum_' . bbp_get_forum_id() ) ) {
2771 return true;
2772 }
2773
2774 return false;
2775 }
2776
2777 /** Feeds *********************************************************************/
2778
2779 /**
2780 * Output the link for the forum feed
2781 *
2782 * @since 2.0.0 bbPress (r3172)
2783 *
2784 * @param int $forum_id Optional. Forum ID.
2785 */
2786 function bbp_forum_topics_feed_link( $forum_id = 0 ) {
2787 echo bbp_get_forum_topics_feed_link( $forum_id );
2788 }
2789 /**
2790 * Retrieve the link for the forum feed
2791 *
2792 * @since 2.0.0 bbPress (r3172)
2793 *
2794 * @param int $forum_id Optional. Forum ID.
2795 *
2796 * @return string
2797 */
2798 function bbp_get_forum_topics_feed_link( $forum_id = 0 ) {
2799
2800 // Validate forum id
2801 $forum_id = bbp_get_forum_id( $forum_id );
2802
2803 // Forum is valid
2804 if ( ! empty( $forum_id ) ) {
2805
2806 // Define local variable(s)
2807 $link = '';
2808
2809 // Pretty permalinks
2810 if ( get_option( 'permalink_structure' ) ) {
2811
2812 // Forum link
2813 $url = trailingslashit( bbp_get_forum_permalink( $forum_id ) ) . 'feed';
2814 $url = user_trailingslashit( $url, 'single_feed' );
2815
2816 // Unpretty permalinks
2817 } else {
2818 $url = home_url(
2819 add_query_arg(
2820 array(
2821 'feed' => 'rss2',
2822 bbp_get_forum_post_type() => get_post_field( 'post_name', $forum_id )
2823 )
2824 )
2825 );
2826 }
2827
2828 $link = '<a href="' . esc_url( $url ) . '" class="bbp-forum-rss-link topics"><span>' . esc_attr__( 'Topics', 'bbpress' ) . '</span></a>';
2829 }
2830
2831 // Filter & return
2832 return apply_filters( 'bbp_get_forum_topics_feed_link', $link, $url, $forum_id );
2833 }
2834
2835 /**
2836 * Output the link for the forum replies feed
2837 *
2838 * @since 2.0.0 bbPress (r3172)
2839 *
2840 * @param int $forum_id Optional. Forum ID.
2841 */
2842 function bbp_forum_replies_feed_link( $forum_id = 0 ) {
2843 echo bbp_get_forum_replies_feed_link( $forum_id );
2844 }
2845 /**
2846 * Retrieve the link for the forum replies feed
2847 *
2848 * @since 2.0.0 bbPress (r3172)
2849 *
2850 * @param int $forum_id Optional. Forum ID.
2851 *
2852 * @return string
2853 */
2854 function bbp_get_forum_replies_feed_link( $forum_id = 0 ) {
2855
2856 // Validate forum id
2857 $forum_id = bbp_get_forum_id( $forum_id );
2858
2859 // Forum is valid
2860 if ( ! empty( $forum_id ) ) {
2861
2862 // Define local variable(s)
2863 $link = '';
2864
2865 // Pretty permalinks
2866 if ( get_option( 'permalink_structure' ) ) {
2867
2868 // Forum link
2869 $url = trailingslashit( bbp_get_forum_permalink( $forum_id ) ) . 'feed';
2870 $url = user_trailingslashit( $url, 'single_feed' );
2871 $url = add_query_arg( array( 'type' => 'reply' ), $url );
2872
2873 // Unpretty permalinks
2874 } else {
2875 $url = home_url(
2876 add_query_arg(
2877 array(
2878 'type' => 'reply',
2879 'feed' => 'rss2',
2880 bbp_get_forum_post_type() => get_post_field( 'post_name', $forum_id )
2881 )
2882 )
2883 );
2884 }
2885
2886 $link = '<a href="' . esc_url( $url ) . '" class="bbp-forum-rss-link replies"><span>' . esc_html__( 'Replies', 'bbpress' ) . '</span></a>';
2887 }
2888
2889 // Filter & return
2890 return apply_filters( 'bbp_get_forum_replies_feed_link', $link, $url, $forum_id );
2891 }
2892