Skip to:
Content

bbPress.org

Ticket #1712: bbp-topic-template.php

File bbp-topic-template.php, 106.8 KB (added by rolandinsh, 14 years ago)
Line 
1<?php
2
3/**
4 * bbPress Topic Template Tags
5 *
6 * @package bbPress
7 * @subpackage TemplateTags
8 */
9
10// Exit if accessed directly
11if ( !defined( 'ABSPATH' ) ) exit;
12
13/** Post Type *****************************************************************/
14
15/**
16 * Output the unique id of the custom post type for topics
17 *
18 * @since bbPress (r2857)
19 *
20 * @uses bbp_get_topic_post_type() To get the topic post type
21 */
22function bbp_topic_post_type() {
23        echo bbp_get_topic_post_type();
24}
25        /**
26         * Return the unique id of the custom post type for topics
27         *
28         * @since bbPress (r2857)
29         *
30         * @uses apply_filters() Calls 'bbp_get_topic_post_type' with the topic
31         *                        post type id
32         * @return string The unique topic post type id
33         */
34        function bbp_get_topic_post_type() {
35                global $bbp;
36
37                return apply_filters( 'bbp_get_topic_post_type', $bbp->topic_post_type );
38        }
39
40/** Topic Loop ****************************************************************/
41
42/**
43 * The main topic loop. WordPress makes this easy for us
44 *
45 * @since bbPress (r2485)
46 *
47 * @param mixed $args All the arguments supported by {@link WP_Query}
48 * @uses current_user_can() To check if the current user can edit other's topics
49 * @uses bbp_get_topic_post_type() To get the topic post type
50 * @uses WP_Query To make query and get the topics
51 * @uses is_page() To check if it's a page
52 * @uses bbp_is_single_forum() To check if it's a forum
53 * @uses bbp_get_forum_id() To get the forum id
54 * @uses bbp_get_paged() To get the current page value
55 * @uses bbp_get_super_stickies() To get the super stickies
56 * @uses bbp_get_stickies() To get the forum stickies
57 * @uses wpdb::get_results() To execute our query and get the results
58 * @uses WP_Rewrite::using_permalinks() To check if the blog is using permalinks
59 * @uses get_permalink() To get the permalink
60 * @uses add_query_arg() To add custom args to the url
61 * @uses apply_filters() Calls 'bbp_topics_pagination' with the pagination args
62 * @uses paginate_links() To paginate the links
63 * @uses apply_filters() Calls 'bbp_has_topics' with
64 *                        bbPres::topic_query::have_posts()
65 *                        and bbPres::topic_query
66 * @return object Multidimensional array of topic information
67 */
68function bbp_has_topics( $args = '' ) {
69        global $wp_rewrite, $wp_query, $bbp, $wpdb;
70
71        // What are the default allowed statuses (based on user caps)
72        if ( !bbp_is_query_name( 'bbp_widget' ) && bbp_get_view_all() )
73                $default_status = join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id(), bbp_get_spam_status_id(), bbp_get_trash_status_id() ) );
74        else
75                $default_status = join( ',', array( bbp_get_public_status_id(), bbp_get_closed_status_id() ) );
76
77        // Default arguments
78        $default = array(
79
80                // Narrow query down to bbPress topics
81                'post_type'      => bbp_get_topic_post_type(),
82
83                // Forum ID
84                'post_parent'    => bbp_is_single_forum() ? bbp_get_forum_id() : 'any',
85
86                // Make sure topic has some last activity time
87                'meta_key'       => '_bbp_last_active_time',
88
89                // 'meta_value', 'author', 'date', 'title', 'modified', 'parent', rand',
90                'orderby'        => 'meta_value',
91
92                // 'ASC', 'DESC'
93                'order'          => 'DESC',
94
95                // Topics per page
96                'posts_per_page' => bbp_get_topics_per_page(),
97
98                // Page Number
99                'paged'          => bbp_get_paged(),
100
101                // Topic Search
102                's'              => !empty( $_REQUEST['ts'] ) ? $_REQUEST['ts'] : '',
103
104                // Ignore sticky topics?
105                'show_stickies'  => bbp_is_single_forum(),
106
107                // Maximum number of pages to show
108                'max_num_pages'  => false,
109
110                // Post Status
111                'post_status'    => $default_status,
112        );
113
114        // Maybe query for topic tags
115        if ( !bbp_is_query_name( 'bbp_widget' ) && bbp_is_topic_tag() ) {
116                $default['term']     = bbp_get_topic_tag_slug();
117                $default['taxonomy'] = bbp_get_topic_tag_tax_id();
118        }
119
120        // Filter the default arguments
121        $args  = apply_filters( 'bbp_pre_has_topics_query', $args );
122
123        // Set up topic variables
124        $bbp_t = wp_parse_args( $args, $default );
125
126        // Filter the topics query to allow just-in-time modifications
127        $bbp_t = apply_filters( 'bbp_has_topics_query', $bbp_t );
128
129        // Extract the query variables
130        extract( $bbp_t );
131
132        // Call the query
133        $bbp->topic_query = new WP_Query( $bbp_t );
134
135        // Set post_parent back to 0 if originally set to 'any'
136        if ( 'any' == $bbp_t['post_parent'] )
137                $bbp_t['post_parent'] = $post_parent = 0;
138
139        // Limited the number of pages shown
140        if ( !empty( $max_num_pages ) )
141                $bbp->topic_query->max_num_pages = $max_num_pages;
142
143        // Put sticky posts at the top of the posts array
144        if ( !empty( $show_stickies ) && $paged <= 1 ) {
145
146                // Get super stickies and stickies in this forum
147                $stickies = bbp_get_super_stickies();
148                $stickies = !empty( $post_parent ) ? array_merge( $stickies, bbp_get_stickies( $post_parent ) ) : $stickies;
149                $stickies = array_unique( $stickies );
150
151                // We have stickies
152                if ( is_array( $stickies ) && !empty( $stickies ) ) {
153
154                        // Setup the number of stickies and reset offset to 0
155                        $num_topics    = count( $bbp->topic_query->posts );
156                        $sticky_offset = 0;
157
158                        // Loop over topics and relocate stickies to the front.
159                        for ( $i = 0; $i < $num_topics; $i++ ) {
160                                if ( in_array( $bbp->topic_query->posts[$i]->ID, $stickies ) ) {
161                                        $sticky = $bbp->topic_query->posts[$i];
162
163                                        // Remove sticky from current position
164                                        array_splice( $bbp->topic_query->posts, $i, 1 );
165
166                                        // Move to front, after other stickies
167                                        array_splice( $bbp->topic_query->posts, $sticky_offset, 0, array( $sticky ) );
168
169                                        // Increment the sticky offset.  The next sticky will be placed at this offset.
170                                        $sticky_offset++;
171
172                                        // Remove post from sticky posts array
173                                        $offset = array_search( $sticky->ID, $stickies );
174
175                                        // Cleanup
176                                        unset( $stickies[$offset] );
177                                        unset( $sticky            );
178                                }
179                        }
180
181                        // If any posts have been excluded specifically, Ignore those that are sticky.
182                        if ( !empty( $stickies ) && !empty( $post__not_in ) )
183                                $stickies = array_diff( $stickies, $post__not_in );
184
185                        // Fetch sticky posts that weren't in the query results
186                        if ( !empty( $stickies ) ) {
187
188                                // Query to use in get_posts to get sticky posts
189                                $sticky_query = array(
190                                        'post_type'   => bbp_get_topic_post_type(),
191                                        'post_parent' => 'any',
192                                        'include'     => $stickies
193                                );
194
195                                // Get all stickies
196                                $sticky_posts = get_posts( $sticky_query );
197                                if ( !empty( $sticky_posts ) ) {
198
199                                        // Get a count of the visible stickies
200                                        $sticky_count = count( $sticky_posts );
201
202                                        // Loop through stickies and add them to beginning of array
203                                        foreach ( $sticky_posts as $sticky )
204                                                $topics[] = $sticky;
205
206                                        // Loop through topics and add them to end of array
207                                        foreach ( $bbp->topic_query->posts as $topic )
208                                                $topics[] = $topic;
209
210                                        // Adjust loop and counts for new sticky positions
211                                        $bbp->topic_query->posts       = $topics;
212                                        $bbp->topic_query->found_posts = (int) $bbp->topic_query->found_posts + (int) $sticky_count;
213                                        $bbp->topic_query->post_count  = (int) $bbp->topic_query->post_count  + (int) $sticky_count;
214
215                                        // Cleanup
216                                        unset( $topics       );
217                                        unset( $stickies     );
218                                        unset( $sticky_posts );
219                                }
220                        }
221                }
222        }
223
224        // If no limit to posts per page, set it to the current post_count
225        if ( -1 == $posts_per_page )
226                $posts_per_page = $bbp->topic_query->post_count;
227
228        // Add pagination values to query object
229        $bbp->topic_query->posts_per_page = $posts_per_page;
230        $bbp->topic_query->paged          = $paged;
231
232        // Only add pagination if query returned results
233        if ( !bbp_is_query_name( 'bbp_widget' ) && ( (int) $bbp->topic_query->post_count || (int) $bbp->topic_query->found_posts ) && (int) $bbp->topic_query->posts_per_page ) {
234
235                // Limit the number of topics shown based on maximum allowed pages
236                if ( ( !empty( $max_num_pages ) ) && $bbp->topic_query->found_posts > $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count )
237                        $bbp->topic_query->found_posts = $bbp->topic_query->max_num_pages * $bbp->topic_query->post_count;
238
239                // If pretty permalinks are enabled, make our pagination pretty
240                if ( $wp_rewrite->using_permalinks() ) {
241
242                        // Profile page
243                        if ( bbp_is_single_user() )
244                                $base = bbp_get_user_profile_url( bbp_get_displayed_user_id() );
245
246                        // View
247                        elseif ( bbp_is_single_view() )
248                                $base = bbp_get_view_url();
249
250                        // Topic tag
251                        elseif ( bbp_is_topic_tag() )
252                                $base = bbp_get_topic_tag_link();
253
254                        // Page or single post
255                        elseif ( is_page() || is_single() )
256                                $base = get_permalink();
257
258                        // Topic archive
259                        elseif ( bbp_is_topic_archive() )
260                                $base = home_url( $bbp->topic_archive_slug );
261
262                        // Default
263                        else
264                                $base = get_permalink( $post_parent );
265
266                        // Use pagination base
267                        $base = trailingslashit( $base ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
268
269                // Unpretty pagination
270                } else {
271                        $base = add_query_arg( 'paged', '%#%' );
272                }
273
274                // Pagination settings with filter
275                $bbp_topic_pagination = apply_filters( 'bbp_topic_pagination', array (
276                        'base'      => $base,
277                        'format'    => '',
278                        'total'     => $posts_per_page == $bbp->topic_query->found_posts ? 1 : ceil( (int) $bbp->topic_query->found_posts / (int) $posts_per_page ),
279                        'current'   => (int) $bbp->topic_query->paged,
280                        'prev_text' => '&larr;',
281                        'next_text' => '&rarr;',
282                        'mid_size'  => 1
283                ) );
284
285                // Add pagination to query object
286                $bbp->topic_query->pagination_links = paginate_links ( $bbp_topic_pagination );
287
288                // Remove first page from pagination
289                $bbp->topic_query->pagination_links = str_replace( $wp_rewrite->pagination_base . "/1/'", "'", $bbp->topic_query->pagination_links );
290        }
291
292        // Return object
293        return apply_filters( 'bbp_has_topics', $bbp->topic_query->have_posts(), $bbp->topic_query );
294}
295
296/**
297 * Whether there are more topics available in the loop
298 *
299 * @since bbPress (r2485)
300 *
301 * @uses WP_Query bbPress::topic_query::have_posts()
302 * @return object Topic information
303 */
304function bbp_topics() {
305        global $bbp;
306
307        // Put into variable to check against next
308        $have_posts = $bbp->topic_query->have_posts();
309
310        // Reset the post data when finished
311        if ( empty( $have_posts ) )
312                wp_reset_postdata();
313
314        return $have_posts;
315}
316
317/**
318 * Loads up the current topic in the loop
319 *
320 * @since bbPress (r2485)
321 *
322 * @uses WP_Query bbPress::topic_query::the_post()
323 * @return object Topic information
324 */
325function bbp_the_topic() {
326        global $bbp;
327        return $bbp->topic_query->the_post();
328}
329
330/**
331 * Output the topic id
332 *
333 * @since bbPress (r2485)
334 *
335 * @uses bbp_get_topic_id() To get the topic id
336 */
337function bbp_topic_id( $topic_id = 0) {
338        echo bbp_get_topic_id( $topic_id );
339}
340        /**
341         * Return the topic id
342         *
343         * @since bbPress (r2485)
344         *
345         * @param $topic_id Optional. Used to check emptiness
346         * @uses bbPress::topic_query::post::ID To get the topic id
347         * @uses bbp_is_single_topic() To check if it's a topic page
348         * @uses bbp_is_topic_edit() To check if it's a topic edit page
349         * @uses bbp_is_single_reply() To check if it it's a reply page
350         * @uses bbp_is_reply_edit() To check if it's a reply edit page
351         * @uses bbp_get_reply_topic_edit() To get the reply topic id
352         * @uses get_post_field() To get the post's post type
353         * @uses WP_Query::post::ID To get the topic id
354         * @uses bbp_get_topic_post_type() To get the topic post type
355         * @uses apply_filters() Calls 'bbp_get_topic_id' with the topic id and
356         *                        supplied topic id
357         * @return int The topic id
358         */
359        function bbp_get_topic_id( $topic_id = 0 ) {
360                global $bbp, $wp_query;
361
362                // Easy empty checking
363                if ( !empty( $topic_id ) && is_numeric( $topic_id ) )
364                        $bbp_topic_id = $topic_id;
365
366                // Currently inside a topic loop
367                elseif ( !empty( $bbp->topic_query->in_the_loop ) && isset( $bbp->topic_query->post->ID ) )
368                        $bbp_topic_id = $bbp->topic_query->post->ID;
369
370                // Currently viewing a topic
371                elseif ( ( bbp_is_single_topic() || bbp_is_topic_edit() ) && isset( $wp_query->post->ID ) )
372                        $bbp_topic_id = $bbp->current_topic_id = $wp_query->post->ID;
373
374                // Currently viewing a topic
375                elseif ( bbp_is_single_reply() )
376                        $bbp_topic_id = $bbp->current_topic_id = bbp_get_reply_topic_id();
377
378                // Fallback
379                else
380                        $bbp_topic_id = 0;
381
382                // Check if current_reply_id is set, and check post_type if so
383                if ( !empty( $bbp->current_topic_id ) && ( bbp_get_topic_post_type() != get_post_field( 'post_type', $bbp_topic_id ) ) )
384                        $bbp->current_topic_id = null;
385
386                return apply_filters( 'bbp_get_topic_id', (int) $bbp_topic_id, $topic_id );
387        }
388
389/**
390 * Gets a topic
391 *
392 * @since bbPress (r2787)
393 *
394 * @param int|object $topic Topic id or topic object
395 * @param string $output Optional. OBJECT, ARRAY_A, or ARRAY_N. Default = OBJECT
396 * @param string $filter Optional Sanitation filter. See {@link sanitize_post()}
397 * @uses get_post() To get the topic
398 * @uses apply_filters() Calls 'bbp_get_topic' with the topic, output type and
399 *                        sanitation filter
400 * @return mixed Null if error or topic (in specified form) if success
401 */
402function bbp_get_topic( $topic, $output = OBJECT, $filter = 'raw' ) {
403
404        if ( empty( $topic ) || is_numeric( $topic ) )
405                $topic = bbp_get_topic_id( $topic );
406
407        if ( !$topic = get_post( $topic, OBJECT, $filter ) )
408                return $topic;
409
410        if ( $topic->post_type !== bbp_get_topic_post_type() )
411                return null;
412
413        if ( $output == OBJECT ) {
414                return $topic;
415
416        } elseif ( $output == ARRAY_A ) {
417                $_topic = get_object_vars( $topic );
418                return $_topic;
419
420        } elseif ( $output == ARRAY_N ) {
421                $_topic = array_values( get_object_vars( $topic ) );
422                return $_topic;
423
424        }
425
426        return apply_filters( 'bbp_get_topic', $topic, $output, $filter );
427}
428
429/**
430 * Output the link to the topic in the topic loop
431 *
432 * @since bbPress (r2485)
433 *
434 * @param int $topic_id Optional. Topic id
435 * @uses bbp_get_topic_permalink() To get the topic permalink
436 */
437function bbp_topic_permalink( $topic_id = 0 ) {
438        echo bbp_get_topic_permalink( $topic_id );
439}
440        /**
441         * Return the link to the topic
442         *
443         * @since bbPress (r2485)
444         *
445         * @param int $topic_id Optional. Topic id
446         * @param $string $redirect_to Optional. Pass a redirect value for use with
447         *                              shortcodes and other fun things.
448         * @uses bbp_get_topic_id() To get the topic id
449         * @uses get_permalink() To get the topic permalink
450         * @uses esc_url_raw() To clean the redirect_to url
451         * @uses apply_filters() Calls 'bbp_get_topic_permalink' with the link
452         *                        and topic id
453         * @return string Permanent link to topic
454         */
455        function bbp_get_topic_permalink( $topic_id = 0, $redirect_to = '' ) {
456                $topic_id = bbp_get_topic_id( $topic_id );
457
458                // Use the redirect address
459                if ( !empty( $redirect_to ) )
460                        $topic_permalink = esc_url_raw( $redirect_to );
461
462                // Use the topic permalink
463                else
464                        $topic_permalink = get_permalink( $topic_id );
465
466                return apply_filters( 'bbp_get_topic_permalink', $topic_permalink, $topic_id );
467        }
468
469/**
470 * Output the title of the topic
471 *
472 * @since bbPress (r2485)
473 *
474 * @param int $topic_id Optional. Topic id
475 * @uses bbp_get_topic_title() To get the topic title
476 */
477function bbp_topic_title( $topic_id = 0 ) {
478        echo bbp_get_topic_title( $topic_id );
479}
480        /**
481         * Return the title of the topic
482         *
483         * @since bbPress (r2485)
484         *
485         * @param int $topic_id Optional. Topic id
486         * @uses bbp_get_topic_id() To get the topic id
487         * @uses get_the_title() To get the title
488         * @uses apply_filters() Calls 'bbp_get_topic_title' with the title and
489         *                        topic id
490         * @return string Title of topic
491         */
492        function bbp_get_topic_title( $topic_id = 0 ) {
493                $topic_id = bbp_get_topic_id( $topic_id );
494
495                return apply_filters( 'bbp_get_topic_title', get_the_title( $topic_id ), $topic_id );
496        }
497
498/**
499 * Output the topic archive title
500 *
501 * @since bbPress (r3249)
502 *
503 * @param string $title Default text to use as title
504 */
505function bbp_topic_archive_title( $title = '' ) {
506        echo bbp_get_topic_archive_title( $title );
507}
508        /**
509         * Return the topic archive title
510         *
511         * @since bbPress (r3249)
512         *
513         * @global bbPress $bbp The main bbPress class
514         * @param string $title Default text to use as title
515         *
516         * @uses bbp_get_page_by_path() Check if page exists at root path
517         * @uses get_the_title() Use the page title at the root path
518         * @uses get_post_type_object() Load the post type object
519         * @uses bbp_get_topic_post_type() Get the topic post type ID
520         * @uses get_post_type_labels() Get labels for topic post type
521         * @uses apply_filters() Allow output to be manipulated
522         *
523         * @return string The topic archive title
524         */
525        function bbp_get_topic_archive_title( $title = '' ) {
526                global $bbp;
527
528                // If no title was passed
529                if ( empty( $title ) ) {
530
531                        // Set root text to page title
532                        $page = bbp_get_page_by_path( $bbp->topic_archive_slug );
533                        if ( !empty( $page ) ) {
534                                $title = get_the_title( $page->ID );
535
536                        // Default to topic post type name label
537                        } else {
538                                $tto    = get_post_type_object( bbp_get_topic_post_type() );
539                                $title  = $tto->labels->name;
540                        }
541                }
542
543                return apply_filters( 'bbp_get_topic_archive_title', $title );
544        }
545
546/**
547 * Output the content of the topic
548 *
549 * @since bbPress (r2780)
550 *
551 * @param int $topic_id Optional. Topic id
552 * @uses bbp_get_topic_content() To get the topic content
553 */
554function bbp_topic_content( $topic_id = 0 ) {
555        echo bbp_get_topic_content( $topic_id );
556}
557        /**
558         * Return the content of the topic
559         *
560         * @since bbPress (r2780)
561         *
562         * @param int $topic_id Optional. Topic id
563         * @uses bbp_get_topic_id() To get the topic id
564         * @uses post_password_required() To check if the topic requires pass
565         * @uses get_the_password_form() To get the password form
566         * @uses get_post_field() To get the content post field
567         * @uses apply_filters() Calls 'bbp_get_topic_content' with the content
568         *                        and topic id
569         * @return string Content of the topic
570         */
571        function bbp_get_topic_content( $topic_id = 0 ) {
572                $topic_id = bbp_get_topic_id( $topic_id );
573
574                // Check if password is required
575                if ( post_password_required( $topic_id ) )
576                        return get_the_password_form();
577
578                $content = get_post_field( 'post_content', $topic_id );
579
580                return apply_filters( 'bbp_get_topic_content', $content, $topic_id );
581        }
582
583/**
584 * Output the excerpt of the topic
585 *
586 * @since bbPress (r2780)
587 *
588 * @param int $topic_id Optional. Topic id
589 * @param int $length Optional. Length of the excerpt. Defaults to 100 letters
590 * @uses bbp_get_topic_excerpt() To get the topic excerpt
591 */
592function bbp_topic_excerpt( $topic_id = 0, $length = 100 ) {
593        echo bbp_get_topic_excerpt( $topic_id, $length );
594}
595        /**
596         * Return the excerpt of the topic
597         *
598         * @since bbPress (r2780)
599         *
600         * @param int $topic_id Optional. topic id
601         * @param int $length Optional. Length of the excerpt. Defaults to 100
602         *                     letters
603         * @uses bbp_get_topic_id() To get the topic id
604         * @uses get_post_field() To get the excerpt
605         * @uses bbp_get_topic_content() To get the topic content
606         * @uses apply_filters() Calls 'bbp_get_topic_excerpt' with the excerpt,
607         *                        topic id and length
608         * @return string topic Excerpt
609         */
610        function bbp_get_topic_excerpt( $topic_id = 0, $length = 100 ) {
611                $topic_id = bbp_get_topic_id( $topic_id );
612                $length   = (int) $length;
613                $excerpt  = get_post_field( $topic_id, 'post_excerpt' );
614
615                if ( empty( $excerpt ) )
616                        $excerpt = bbp_get_topic_content( $topic_id );
617
618                $excerpt = trim( strip_tags( $excerpt ) );
619
620                if ( !empty( $length ) && strlen( $excerpt ) > $length ) {
621                        $excerpt  = substr( $excerpt, 0, $length - 1 );
622                        $excerpt .= '&hellip;';
623                }
624
625                return apply_filters( 'bbp_get_topic_excerpt', $excerpt, $topic_id, $length );
626        }
627
628/**
629 * Output pagination links of a topic within the topic loop
630 *
631 * @since bbPress (r2966)
632 *
633 * @param mixed $args See {@link bbp_get_topic_pagination()}
634 * @uses bbp_get_topic_pagination() To get the topic pagination links
635 */
636function bbp_topic_pagination( $args = '' ) {
637        echo bbp_get_topic_pagination( $args );
638}
639        /**
640         * Returns pagination links of a topic within the topic loop
641         *
642         * @since bbPress (r2966)
643         *
644         * @param mixed $args This function supports these arguments:
645         *  - topic_id: Topic id
646         *  - before: Before the links
647         *  - after: After the links
648         * @uses bbp_get_topic_id() To get the topic id
649         * @uses WP_Rewrite::using_permalinks() To check if the blog is using
650         *                                       permalinks
651         * @uses user_trailingslashit() To add a trailing slash
652         * @uses trailingslashit() To add a trailing slash
653         * @uses get_permalink() To get the permalink of the topic
654         * @uses add_query_arg() To add query args
655         * @uses bbp_get_topic_reply_count() To get topic reply count
656         * @uses bbp_show_topic_lead() Are we showing the topic as a lead?
657         * @uses get_option() To get replies per page option
658         * @uses paginate_links() To paginate the links
659         * @uses apply_filters() Calls 'bbp_get_topic_pagination' with the links
660         *                        and arguments
661         * @return string Pagination links
662         */
663        function bbp_get_topic_pagination( $args = '' ) {
664                global $wp_rewrite;
665
666                $defaults = array(
667                        'topic_id' => bbp_get_topic_id(),
668                        'before'   => '<span class="bbp-topic-pagination">',
669                        'after'    => '</span>',
670                );
671
672                $r = wp_parse_args( $args, $defaults );
673                extract( $r );
674
675                // If pretty permalinks are enabled, make our pagination pretty
676                if ( $wp_rewrite->using_permalinks() )
677                        $base = trailingslashit( get_permalink( $topic_id ) ) . user_trailingslashit( $wp_rewrite->pagination_base . '/%#%/' );
678                else
679                        $base = add_query_arg( 'paged', '%#%', get_permalink( $topic_id ) );
680
681                // Get total and add 1 if topic is included in the reply loop
682                $total = bbp_get_topic_reply_count( $topic_id );
683
684                // Bump if topic is in loop
685                if ( !bbp_show_lead_topic() )
686                        $total++;
687
688                // Pagination settings
689                $pagination = array(
690                        'base'      => $base,
691                        'format'    => '',
692                        'total'     => ceil( (int) $total / (int) bbp_get_replies_per_page() ),
693                        'current'   => 0,
694                        'prev_next' => false,
695                        'mid_size'  => 2,
696                        'end_size'  => 3,
697                        'add_args'  => ( bbp_get_view_all() ) ? array( 'view' => 'all' ) : false
698                );
699
700                // Add pagination to query object
701                $pagination_links = paginate_links( $pagination );
702                if ( !empty( $pagination_links ) ) {
703
704                        // Remove first page from pagination
705                        if ( $wp_rewrite->using_permalinks() ) {
706                                $pagination_links = str_replace( $wp_rewrite->pagination_base . '/1/', '', $pagination_links );
707                        } else {
708                                $pagination_links = str_replace( '&#038;paged=1', '', $pagination_links );
709                        }
710
711                        // Add before and after to pagination links
712                        $pagination_links = $before . $pagination_links . $after;
713                }
714
715                return apply_filters( 'bbp_get_topic_pagination', $pagination_links, $args );
716        }
717
718/**
719 * Append revisions to the topic content
720 *
721 * @since bbPress (r2782)
722 *
723 * @param string $content Optional. Content to which we need to append the revisions to
724 * @param int $topic_id Optional. Topic id
725 * @uses bbp_get_topic_revision_log() To get the topic revision log
726 * @uses apply_filters() Calls 'bbp_topic_append_revisions' with the processed
727 *                        content, original content and topic id
728 * @return string Content with the revisions appended
729 */
730function bbp_topic_content_append_revisions( $content = '', $topic_id = 0 ) {
731
732        // Bail if in admin
733        if ( is_admin() )
734                return;
735
736        // Validate the ID
737        $topic_id = bbp_get_topic_id( $topic_id );
738
739        return apply_filters( 'bbp_topic_append_revisions', $content . bbp_get_topic_revision_log( $topic_id ), $content, $topic_id );
740}
741
742/**
743 * Output the revision log of the topic
744 *
745 * @since bbPress (r2782)
746 *
747 * @param int $topic_id Optional. Topic id
748 * @uses bbp_get_topic_revision_log() To get the topic revision log
749 */
750function bbp_topic_revision_log( $topic_id = 0 ) {
751        echo bbp_get_topic_revision_log( $topic_id );
752}
753        /**
754         * Return the formatted revision log of the topic
755         *
756         * @since bbPress (r2782)
757         *
758         * @param int $topic_id Optional. Topic id
759         * @uses bbp_get_topic_id() To get the topic id
760         * @uses bbp_get_topic_revisions() To get the topic revisions
761         * @uses bbp_get_topic_raw_revision_log() To get the raw revision log
762         * @uses bbp_get_topic_author_display_name() To get the topic author
763         * @uses bbp_get_author_link() To get the topic author link
764         * @uses bbp_convert_date() To convert the date
765         * @uses bbp_get_time_since() To get the time in since format
766         * @uses apply_filters() Calls 'bbp_get_topic_revision_log' with the
767         *                        log and topic id
768         * @return string Revision log of the topic
769         */
770        function bbp_get_topic_revision_log( $topic_id = 0 ) {
771                // Create necessary variables
772                $topic_id     = bbp_get_topic_id( $topic_id );
773                $revision_log = bbp_get_topic_raw_revision_log( $topic_id );
774
775                if ( empty( $topic_id ) || empty( $revision_log ) || !is_array( $revision_log ) )
776                        return false;
777
778                if ( !$revisions = bbp_get_topic_revisions( $topic_id ) )
779                        return false;
780
781                $r = "\n\n" . '<ul id="bbp-topic-revision-log-' . $topic_id . '" class="bbp-topic-revision-log">' . "\n\n";
782
783                // Loop through revisions
784                foreach ( (array) $revisions as $revision ) {
785
786                        if ( empty( $revision_log[$revision->ID] ) ) {
787                                $author_id = $revision->post_author;
788                                $reason    = '';
789                        } else {
790                                $author_id = $revision_log[$revision->ID]['author'];
791                                $reason    = $revision_log[$revision->ID]['reason'];
792                        }
793
794                        $author = bbp_get_author_link( array( 'size' => 14, 'link_text' => bbp_get_topic_author_display_name( $revision->ID ), 'post_id' => $revision->ID ) );
795                        $since  = bbp_get_time_since( bbp_convert_date( $revision->post_modified ) );
796
797                        $r .= "\t" . '<li id="bbp-topic-revision-log-' . $topic_id . '-item-' . $revision->ID . '" class="bbp-topic-revision-log-item">' . "\n";
798                        $r .= "\t\t" . sprintf( __( empty( $reason ) ? 'This topic was modified %1$s ago by %2$s.' : 'This topic was modified %1$s ago by %2$s. Reason: %3$s', 'bbpress' ), $since, $author, $reason ) . "\n";
799                        $r .= "\t" . '</li>' . "\n";
800
801                }
802
803                $r .= "\n" . '</ul>' . "\n\n";
804
805                return apply_filters( 'bbp_get_topic_revision_log', $r, $topic_id );
806        }
807                /**
808                 * Return the raw revision log of the topic
809                 *
810                 * @since bbPress (r2782)
811                 *
812                 * @param int $topic_id Optional. Topic id
813                 * @uses bbp_get_topic_id() To get the topic id
814                 * @uses get_post_meta() To get the revision log meta
815                 * @uses apply_filters() Calls 'bbp_get_topic_raw_revision_log'
816                 *                        with the log and topic id
817                 * @return string Raw revision log of the topic
818                 */
819                function bbp_get_topic_raw_revision_log( $topic_id = 0 ) {
820                        $topic_id = bbp_get_topic_id( $topic_id );
821
822                        $revision_log = get_post_meta( $topic_id, '_bbp_revision_log', true );
823                        $revision_log = empty( $revision_log ) ? array() : $revision_log;
824
825                        return apply_filters( 'bbp_get_topic_raw_revision_log', $revision_log, $topic_id );
826                }
827
828/**
829 * Return the revisions of the topic
830 *
831 * @since bbPress (r2782)
832 *
833 * @param int $topic_id Optional. Topic id
834 * @uses bbp_get_topic_id() To get the topic id
835 * @uses wp_get_post_revisions() To get the topic revisions
836 * @uses apply_filters() Calls 'bbp_get_topic_revisions'
837 *                        with the revisions and topic id
838 * @return string Topic revisions
839 */
840function bbp_get_topic_revisions( $topic_id = 0 ) {
841        $topic_id  = bbp_get_topic_id( $topic_id );
842        $revisions = wp_get_post_revisions( $topic_id, array( 'order' => 'ASC' ) );
843
844        return apply_filters( 'bbp_get_topic_revisions', $revisions, $topic_id );
845}
846
847/**
848 * Return the revision count of the topic
849 *
850 * @since bbPress (r2782)
851 *
852 * @param int $topic_id Optional. Topic id
853 * @uses bbp_get_topic_revisions() To get the topic revisions
854 * @uses apply_filters() Calls 'bbp_get_topic_revision_count'
855 *                        with the revision count and topic id
856 * @return string Topic revision count
857 */
858function bbp_get_topic_revision_count( $topic_id = 0 ) {
859        return apply_filters( 'bbp_get_topic_revisions', count( bbp_get_topic_revisions( $topic_id ) ), $topic_id );
860}
861
862/**
863 * Output the status of the topic
864 *
865 * @since bbPress (r2667)
866 *
867 * @param int $topic_id Optional. Topic id
868 * @uses bbp_get_topic_status() To get the topic status
869 */
870function bbp_topic_status( $topic_id = 0 ) {
871        echo bbp_get_topic_status( $topic_id );
872}
873        /**
874         * Return the status of the topic
875         *
876         * @since bbPress (r2667)
877         *
878         * @param int $topic_id Optional. Topic id
879         * @uses bbp_get_topic_id() To get the topic id
880         * @uses get_post_status() To get the topic status
881         * @uses apply_filters() Calls 'bbp_get_topic_status' with the status
882         *                        and topic id
883         * @return string Status of topic
884         */
885        function bbp_get_topic_status( $topic_id = 0 ) {
886                $topic_id = bbp_get_topic_id( $topic_id );
887
888                return apply_filters( 'bbp_get_topic_status', get_post_status( $topic_id ), $topic_id );
889        }
890
891/**
892 * Is the topic open to new replies?
893 *
894 * @since bbPress (r2727)
895 *
896 * @uses bbp_get_topic_status()
897 *
898 * @param int $topic_id Optional. Topic id
899 * @uses bbp_is_topic_closed() To check if the topic is closed
900 * @return bool True if open, false if closed.
901 */
902function bbp_is_topic_open( $topic_id = 0 ) {
903        return !bbp_is_topic_closed( $topic_id );
904}
905
906        /**
907         * Is the topic closed to new replies?
908         *
909         * @since bbPress (r2746)
910         *
911         * @param int $topic_id Optional. Topic id
912         * @uses bbp_get_topic_status() To get the topic status
913         * @return bool True if closed, false if not.
914         */
915        function bbp_is_topic_closed( $topic_id = 0 ) {
916                if ( bbp_get_closed_status_id() == bbp_get_topic_status( $topic_id ) )
917                        return true;
918
919                return false;
920        }
921
922/**
923 * Is the topic a sticky or super sticky?
924 *
925 * @since bbPress (r2754)
926 *
927 * @param int $topic_id Optional. Topic id
928 * @param int $check_super Optional. If set to true and if the topic is not a
929 *                           normal sticky, it is checked if it is a super
930 *                           sticky or not. Defaults to true.
931 * @uses bbp_get_topic_id() To get the topic id
932 * @uses bbp_get_topic_forum_id() To get the topic forum id
933 * @uses bbp_get_stickies() To get the stickies
934 * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
935 * @return bool True if sticky or super sticky, false if not.
936 */
937function bbp_is_topic_sticky( $topic_id = 0, $check_super = true ) {
938        $topic_id = bbp_get_topic_id( $topic_id );
939        $forum_id = bbp_get_topic_forum_id( $topic_id );
940        $stickies = bbp_get_stickies( $forum_id );
941
942        if ( in_array( $topic_id, $stickies ) || ( !empty( $check_super ) && bbp_is_topic_super_sticky( $topic_id ) ) )
943                return true;
944
945        return false;
946}
947
948/**
949 * Is the topic a super sticky?
950 *
951 * @since bbPress (r2754)
952 *
953 * @param int $topic_id Optional. Topic id
954 * @uses bbp_get_topic_id() To get the topic id
955 * @uses bbp_get_super_stickies() To get the super stickies
956 * @return bool True if super sticky, false if not.
957 */
958function bbp_is_topic_super_sticky( $topic_id = 0 ) {
959        $topic_id = bbp_get_topic_id( $topic_id );
960        $stickies = bbp_get_super_stickies( $topic_id );
961
962        return in_array( $topic_id, $stickies );
963}
964
965/**
966 * Is the topic not spam or deleted?
967 *
968 * @since bbPress (r3496)
969 *
970 * @param int $topic_id Optional. Topic id
971 * @uses bbp_get_topic_id() To get the topic id
972 * @uses bbp_get_topic_status() To get the topic status
973 * @return bool True if published, false if not.
974 */
975function bbp_is_topic_published( $topic_id = 0 ) {
976        $topic_status = bbp_get_topic_status( bbp_get_topic_id( $topic_id ) );
977        return bbp_get_public_status_id() == $topic_status;
978}
979
980/**
981 * Is the topic marked as spam?
982 *
983 * @since bbPress (r2727)
984 *
985 * @param int $topic_id Optional. Topic id
986 * @uses bbp_get_topic_id() To get the topic id
987 * @uses bbp_get_topic_status() To get the topic status
988 * @return bool True if spam, false if not.
989 */
990function bbp_is_topic_spam( $topic_id = 0 ) {
991        $topic_status = bbp_get_topic_status( bbp_get_topic_id( $topic_id ) );
992        return bbp_get_spam_status_id() == $topic_status;
993}
994
995/**
996 * Is the topic trashed?
997 *
998 * @since bbPress (r2888)
999 *
1000 * @param int $topic_id Optional. Topic id
1001 * @uses bbp_get_topic_id() To get the topic id
1002 * @uses bbp_get_topic_status() To get the topic status
1003 * @return bool True if trashed, false if not.
1004 */
1005function bbp_is_topic_trash( $topic_id = 0 ) {
1006        $topic_status = bbp_get_topic_status( bbp_get_topic_id( $topic_id ) );
1007        return bbp_get_trash_status_id() == $topic_status;
1008}
1009
1010/**
1011 * Is the posted by an anonymous user?
1012 *
1013 * @since bbPress (r2753)
1014 *
1015 * @param int $topic_id Optional. Topic id
1016 * @uses bbp_get_topic_id() To get the topic id
1017 * @uses bbp_get_topic_author_id() To get the topic author id
1018 * @uses get_post_meta() To get the anonymous user name and email meta
1019 * @return bool True if the post is by an anonymous user, false if not.
1020 */
1021function bbp_is_topic_anonymous( $topic_id = 0 ) {
1022        $topic_id = bbp_get_topic_id( $topic_id );
1023
1024        if ( 0 != bbp_get_topic_author_id( $topic_id ) )
1025                return false;
1026
1027        if ( false == get_post_meta( $topic_id, '_bbp_anonymous_name', true ) )
1028                return false;
1029
1030        if ( false == get_post_meta( $topic_id, '_bbp_anonymous_email', true ) )
1031                return false;
1032
1033        // The topic is by an anonymous user
1034        return true;
1035}
1036
1037/**
1038 * Output the author of the topic
1039 *
1040 * @since bbPress (r2590)
1041 *
1042 * @param int $topic_id Optional. Topic id
1043 * @uses bbp_get_topic_author() To get the topic author
1044 */
1045function bbp_topic_author( $topic_id = 0 ) {
1046        echo bbp_get_topic_author( $topic_id );
1047}
1048        /**
1049         * Return the author of the topic
1050         *
1051         * @since bbPress (r2590)
1052         *
1053         * @param int $topic_id Optional. Topic id
1054         * @uses bbp_get_topic_id() To get the topic id
1055         * @uses bbp_is_topic_anonymous() To check if the topic is by an
1056         *                                 anonymous user
1057         * @uses bbp_get_topic_author_id() To get the topic author id
1058         * @uses get_the_author_meta() To get the display name of the author
1059         * @uses get_post_meta() To get the name of the anonymous poster
1060         * @uses apply_filters() Calls 'bbp_get_topic_author' with the author
1061         *                        and topic id
1062         * @return string Author of topic
1063         */
1064        function bbp_get_topic_author( $topic_id = 0 ) {
1065                $topic_id = bbp_get_topic_id( $topic_id );
1066
1067                if ( !bbp_is_topic_anonymous( $topic_id ) )
1068                        $author = get_the_author_meta( 'display_name', bbp_get_topic_author_id( $topic_id ) );
1069                else
1070                        $author = get_post_meta( $topic_id, '_bbp_anonymous_name', true );
1071
1072                return apply_filters( 'bbp_get_topic_author', $author, $topic_id );
1073        }
1074
1075/**
1076 * Output the author ID of the topic
1077 *
1078 * @since bbPress (r2590)
1079 *
1080 * @param int $topic_id Optional. Topic id
1081 * @uses bbp_get_topic_author_id() To get the topic author id
1082 */
1083function bbp_topic_author_id( $topic_id = 0 ) {
1084        echo bbp_get_topic_author_id( $topic_id );
1085}
1086        /**
1087         * Return the author ID of the topic
1088         *
1089         * @since bbPress (r2590)
1090         *
1091         * @param int $topic_id Optional. Topic id
1092         * @uses bbp_get_topic_id() To get the topic id
1093         * @uses get_post_field() To get the topic author id
1094         * @uses apply_filters() Calls 'bbp_get_topic_author_id' with the author
1095         *                        id and topic id
1096         * @return string Author of topic
1097         */
1098        function bbp_get_topic_author_id( $topic_id = 0 ) {
1099                $topic_id  = bbp_get_topic_id( $topic_id );
1100                $author_id = get_post_field( 'post_author', $topic_id );
1101
1102                return apply_filters( 'bbp_get_topic_author_id', (int) $author_id, $topic_id );
1103        }
1104
1105/**
1106 * Output the author display_name of the topic
1107 *
1108 * @since bbPress (r2590)
1109 *
1110 * @param int $topic_id Optional. Topic id
1111 * @uses bbp_get_topic_author_display_name() To get the topic author's display
1112 *                                            name
1113 */
1114function bbp_topic_author_display_name( $topic_id = 0 ) {
1115        echo bbp_get_topic_author_display_name( $topic_id );
1116}
1117        /**
1118         * Return the author display_name of the topic
1119         *
1120         * @since bbPress (r2485)
1121         *
1122         * @param int $topic_id Optional. Topic id
1123         * @uses bbp_get_topic_id() To get the topic id
1124         * @uses bbp_is_topic_anonymous() To check if the topic is by an
1125         *                                 anonymous user
1126         * @uses bbp_get_topic_author_id() To get the topic author id
1127         * @uses get_the_author_meta() To get the author meta
1128         * @uses get_post_meta() To get the anonymous user name
1129         * @uses apply_filters() Calls 'bbp_get_topic_author_id' with the
1130         *                        display name and topic id
1131         * @return string Topic's author's display name
1132         */
1133        function bbp_get_topic_author_display_name( $topic_id = 0 ) {
1134                $topic_id = bbp_get_topic_id( $topic_id );
1135
1136                // Check for anonymous user
1137                if ( !bbp_is_topic_anonymous( $topic_id ) )
1138                        $author_name = get_the_author_meta( 'display_name', bbp_get_topic_author_id( $topic_id ) );
1139                else
1140                        $author_name = get_post_meta( $topic_id, '_bbp_anonymous_name', true );
1141
1142                return apply_filters( 'bbp_get_topic_author_id', esc_attr( $author_name ), $topic_id );
1143        }
1144
1145/**
1146 * Output the author avatar of the topic
1147 *
1148 * @since bbPress (r2590)
1149 *
1150 * @param int $topic_id Optional. Topic id
1151 * @param int $size Optional. Avatar size. Defaults to 40
1152 * @uses bbp_get_topic_author_avatar() To get the topic author avatar
1153 */
1154function bbp_topic_author_avatar( $topic_id = 0, $size = 40 ) {
1155        echo bbp_get_topic_author_avatar( $topic_id, $size );
1156}
1157        /**
1158         * Return the author avatar of the topic
1159         *
1160         * @since bbPress (r2590)
1161         *
1162         * @param int $topic_id Optional. Topic id
1163         * @param int $size Optional. Avatar size. Defaults to 40
1164         * @uses bbp_get_topic_id() To get the topic id
1165         * @uses bbp_is_topic_anonymous() To check if the topic is by an
1166         *                                 anonymous user
1167         * @uses bbp_get_topic_author_id() To get the topic author id
1168         * @uses get_post_meta() To get the anonymous user's email
1169         * @uses get_avatar() To get the avatar
1170         * @uses apply_filters() Calls 'bbp_get_topic_author_avatar' with the
1171         *                        avatar, topic id and size
1172         * @return string Avatar of the author of the topic
1173         */
1174        function bbp_get_topic_author_avatar( $topic_id = 0, $size = 40 ) {
1175                $author_avatar = '';
1176
1177                $topic_id = bbp_get_topic_id( $topic_id );
1178                if ( !empty( $topic_id ) ) {
1179                        if ( !bbp_is_topic_anonymous( $topic_id ) ) {
1180                                $author_avatar = get_avatar( bbp_get_topic_author_id( $topic_id ), $size );
1181                        } else {
1182                                $author_avatar = get_avatar( get_post_meta( $topic_id, '_bbp_anonymous_email', true ), $size );
1183                        }
1184                }
1185
1186                return apply_filters( 'bbp_get_topic_author_avatar', $author_avatar, $topic_id, $size );
1187        }
1188
1189/**
1190 * Output the author link of the topic
1191 *
1192 * @since bbPress (r2717)
1193 *
1194 * @param mixed|int $args If it is an integer, it is used as topic_id. Optional.
1195 * @uses bbp_get_topic_author_link() To get the topic author link
1196 */
1197function bbp_topic_author_link( $args = '' ) {
1198        echo bbp_get_topic_author_link( $args );
1199}
1200        /**
1201         * Return the author link of the topic
1202         *
1203         * @since bbPress (r2717)
1204         *
1205         * @param mixed|int $args If it is an integer, it is used as topic id.
1206         *                         Optional.
1207         * @uses bbp_get_topic_id() To get the topic id
1208         * @uses bbp_get_topic_author_display_name() To get the topic author
1209         * @uses bbp_is_topic_anonymous() To check if the topic is by an
1210         *                                 anonymous user
1211         * @uses bbp_get_topic_author_avatar() To get the topic author avatar
1212         * @uses bbp_get_topic_author_url() To get the topic author url
1213         * @uses apply_filters() Calls 'bbp_get_topic_author_link' with the link
1214         *                        and args
1215         * @return string Author link of topic
1216         */
1217        function bbp_get_topic_author_link( $args = '' ) {
1218                $defaults = array (
1219                        'post_id'    => 0,
1220                        'link_title' => '',
1221                        'type'       => 'both',
1222                        'size'       => 80,
1223                        'sep'        => '&nbsp;'
1224                );
1225
1226                $r = wp_parse_args( $args, $defaults );
1227                extract( $r );
1228
1229                // Used as topic_id
1230                if ( is_numeric( $args ) )
1231                        $topic_id = bbp_get_topic_id( $args );
1232                else
1233                        $topic_id = bbp_get_topic_id( $post_id );
1234
1235                if ( !empty( $topic_id ) ) {
1236                        if ( empty( $link_title ) )
1237                                $link_title = sprintf( !bbp_is_topic_anonymous( $topic_id ) ? __( 'View %s\'s profile', 'bbpress' ) : __( 'Visit %s\'s website', 'bbpress' ), bbp_get_topic_author_display_name( $topic_id ) );
1238
1239                        $link_title = !empty( $link_title ) ? ' title="' . $link_title . '"' : '';
1240                        $author_url = bbp_get_topic_author_url( $topic_id );
1241                        $anonymous  = bbp_is_topic_anonymous( $topic_id );
1242
1243                        // Get avatar
1244                        if ( 'avatar' == $type || 'both' == $type )
1245                                $author_links['avatar'] = bbp_get_topic_author_avatar( $topic_id, $size );
1246
1247                        // Get display name
1248                        if ( 'name' == $type   || 'both' == $type )
1249                                $author_links['name'] = bbp_get_topic_author_display_name( $topic_id );
1250
1251                        // Link class
1252                        $link_class = ' class="bbp-author-' . $type . '"';
1253
1254                        // Add links if not anonymous
1255                        if ( empty( $anonymous ) ) {
1256                                foreach ( $author_links as $link => $link_text ) {
1257                                        $link_class = ' class="bbp-author-' . $link . '"';
1258                                        $author_link[] = sprintf( '<a href="%1$s"%2$s%3$s>%4$s</a>', $author_url, $link_title, $link_class, $link_text );
1259                                }
1260                                $author_link = join( $sep, $author_link );
1261
1262                        // No links if anonymous
1263                        } else {
1264                                $author_link = join( $sep, $author_links );
1265                        }
1266
1267                } else {
1268                        $author_link = '';
1269                }
1270
1271                return apply_filters( 'bbp_get_topic_author_link', $author_link, $args );
1272        }
1273
1274/**
1275 * Output the author url of the topic
1276 *
1277 * @since bbPress (r2590)
1278 *
1279 * @param int $topic_id Optional. Topic id
1280 * @uses bbp_get_topic_author_url() To get the topic author url
1281 */
1282function bbp_topic_author_url( $topic_id = 0 ) {
1283        echo bbp_get_topic_author_url( $topic_id );
1284}
1285
1286        /**
1287         * Return the author url of the topic
1288         *
1289         * @since bbPress (r2590)
1290         *
1291         * @param int $topic_id Optional. Topic id
1292         * @uses bbp_get_topic_id() To get the topic id
1293         * @uses bbp_is_topic_anonymous() To check if the topic
1294         *                                 is by an anonymous
1295         *                                 user or not
1296         * @uses bbp_get_topic_author_id() To get topic author
1297         *                                  id
1298         * @uses bbp_get_user_profile_url() To get profile url
1299         * @uses get_post_meta() To get anonmous user's website
1300         * @uses apply_filters() Calls
1301         *                        'bbp_get_topic_author_url'
1302         *                        with the link & topic id
1303         * @return string Author URL of topic
1304         */
1305        function bbp_get_topic_author_url( $topic_id = 0 ) {
1306                $topic_id = bbp_get_topic_id( $topic_id );
1307
1308                // Check for anonymous user
1309                if ( !bbp_is_topic_anonymous( $topic_id ) ) {
1310                        $author_url = bbp_get_user_profile_url( bbp_get_topic_author_id( $topic_id ) );
1311                } else {
1312                        if ( !$author_url = get_post_meta( $topic_id, '_bbp_anonymous_website', true ) ) {
1313                                $author_url = '';
1314                        }
1315                }
1316
1317                return apply_filters( 'bbp_get_topic_author_url', $author_url, $topic_id );
1318        }
1319
1320/**
1321 * Output the topic author email address
1322 *
1323 * @since bbPress (r3445)
1324 *
1325 * @param int $topic_id Optional. Reply id
1326 * @uses bbp_get_topic_author_email() To get the topic author email
1327 */
1328function bbp_topic_author_email( $topic_id = 0 ) {
1329        echo bbp_get_topic_author_email( $topic_id );
1330}
1331        /**
1332         * Return the topic author email address
1333         *
1334         * @since bbPress (r3445)
1335         *
1336         * @param int $topic_id Optional. Reply id
1337         * @uses bbp_get_topic_id() To get the topic id
1338         * @uses bbp_is_topic_anonymous() To check if the topic is by an anonymous
1339         *                                 user
1340         * @uses bbp_get_topic_author_id() To get the topic author id
1341         * @uses get_userdata() To get the user data
1342         * @uses get_post_meta() To get the anonymous poster's email
1343         * @uses apply_filters() Calls bbp_get_topic_author_email with the author
1344         *                        email & topic id
1345         * @return string Topic author email address
1346         */
1347        function bbp_get_topic_author_email( $topic_id = 0 ) {
1348                $topic_id = bbp_get_topic_id( $topic_id );
1349
1350                // Not anonymous user
1351                if ( !bbp_is_topic_anonymous( $topic_id ) ) {
1352
1353                        // Use topic author email address
1354                        $user_id      = bbp_get_topic_author_id( $topic_id );
1355                        $user         = get_userdata( $user_id );
1356                        $author_email = !empty( $user->user_email ) ? $user->user_email : '';
1357
1358                // Anonymous
1359                } else {
1360
1361                        // Get email from post meta
1362                        $author_email = get_post_meta( $topic_id, '_bbp_anonymous_email', true );
1363
1364                        // Sanity check for missing email address
1365                        if ( empty( $author_email ) ) {
1366                                $author_email = '';
1367                        }
1368                }
1369
1370                return apply_filters( 'bbp_get_topic_author_email', $author_email, $topic_id );
1371        }
1372
1373/**
1374 * Output the title of the forum a topic belongs to
1375 *
1376 * @since bbPress (r2485)
1377 *
1378 * @param int $topic_id Optional. Topic id
1379 * @uses bbp_get_topic_forum_title() To get the topic's forum title
1380 */
1381function bbp_topic_forum_title( $topic_id = 0 ) {
1382        echo bbp_get_topic_forum_title( $topic_id );
1383}
1384        /**
1385         * Return the title of the forum a topic belongs to
1386         *
1387         * @since bbPress (r2485)
1388         *
1389         * @param int $topic_id Optional. Topic id
1390         * @uses bbp_get_topic_id() To get topic id
1391         * @uses bbp_get_topic_forum_id() To get topic's forum id
1392         * @uses apply_filters() Calls 'bbp_get_topic_forum' with the forum
1393         *                        title and topic id
1394         * @return string Topic forum title
1395         */
1396        function bbp_get_topic_forum_title( $topic_id = 0 ) {
1397                $topic_id = bbp_get_topic_id( $topic_id );
1398                $forum_id = bbp_get_topic_forum_id( $topic_id );
1399
1400                return apply_filters( 'bbp_get_topic_forum', bbp_get_forum_title( $forum_id ), $topic_id );
1401        }
1402
1403/**
1404 * Output the forum id a topic belongs to
1405 *
1406 * @since bbPress (r2491)
1407 *
1408 * @param int $topic_id Optional. Topic id
1409 * @uses bbp_get_topic_forum_id()
1410 */
1411function bbp_topic_forum_id( $topic_id = 0 ) {
1412        echo bbp_get_topic_forum_id( $topic_id );
1413}
1414        /**
1415         * Return the forum id a topic belongs to
1416         *
1417         * @since bbPress (r2491)
1418         *
1419         * @param int $topic_id Optional. Topic id
1420         * @uses bbp_get_topic_id() To get topic id
1421         * @uses get_post_meta() To retrieve get topic's forum id meta
1422         * @uses apply_filters() Calls 'bbp_get_topic_forum_id' with the forum
1423         *                        id and topic id
1424         * @return int Topic forum id
1425         */
1426        function bbp_get_topic_forum_id( $topic_id = 0 ) {
1427                $topic_id = bbp_get_topic_id( $topic_id );
1428                $forum_id = get_post_meta( $topic_id, '_bbp_forum_id', true );
1429
1430                return apply_filters( 'bbp_get_topic_forum_id', (int) $forum_id, $topic_id );
1431        }
1432
1433/**
1434 * Output the topics last active ID
1435 *
1436 * @since bbPress (r2860)
1437 *
1438 * @param int $topic_id Optional. Forum id
1439 * @uses bbp_get_topic_last_active_id() To get the topic's last active id
1440 */
1441function bbp_topic_last_active_id( $topic_id = 0 ) {
1442        echo bbp_get_topic_last_active_id( $topic_id );
1443}
1444        /**
1445         * Return the topics last active ID
1446         *
1447         * @since bbPress (r2860)
1448         *
1449         * @param int $topic_id Optional. Forum id
1450         * @uses bbp_get_topic_id() To get the topic id
1451         * @uses get_post_meta() To get the topic's last active id
1452         * @uses apply_filters() Calls 'bbp_get_topic_last_active_id' with
1453         *                        the last active id and topic id
1454         * @return int Forum's last active id
1455         */
1456        function bbp_get_topic_last_active_id( $topic_id = 0 ) {
1457                $topic_id  = bbp_get_topic_id( $topic_id );
1458                $active_id = get_post_meta( $topic_id, '_bbp_last_active_id', true );
1459
1460                return apply_filters( 'bbp_get_topic_last_active_id', (int) $active_id, $topic_id );
1461        }
1462
1463/**
1464 * Output the topics last update date/time (aka freshness)
1465 *
1466 * @since bbPress (r2625)
1467 *
1468 * @param int $topic_id Optional. Topic id
1469 * @uses bbp_get_topic_last_active_time() To get topic freshness
1470 */
1471function bbp_topic_last_active_time( $topic_id = 0 ) {
1472        echo bbp_get_topic_last_active_time( $topic_id );
1473}
1474        /**
1475         * Return the topics last update date/time (aka freshness)
1476         *
1477         * @since bbPress (r2625)
1478         *
1479         * @param int $topic_id Optional. Topic id
1480         * @uses bbp_get_topic_id() To get topic id
1481         * @uses get_post_meta() To get the topic lst active meta
1482         * @uses bbp_get_topic_last_reply_id() To get topic last reply id
1483         * @uses get_post_field() To get the post date of topic/reply
1484         * @uses bbp_convert_date() To convert date
1485         * @uses bbp_get_time_since() To get time in since format
1486         * @uses apply_filters() Calls 'bbp_get_topic_last_active' with topic
1487         *                        freshness and topic id
1488         * @return string Topic freshness
1489         */
1490        function bbp_get_topic_last_active_time( $topic_id = 0 ) {
1491                $topic_id = bbp_get_topic_id( $topic_id );
1492
1493                // Try to get the most accurate freshness time possible
1494                $last_active = get_post_meta( $topic_id, '_bbp_last_active_time', true );
1495                if ( empty( $last_active ) ) {
1496                        $reply_id = bbp_get_topic_last_reply_id( $topic_id );
1497                        if ( !empty( $reply_id ) ) {
1498                                $last_active = get_post_field( 'post_date', $reply_id );
1499                        } else {
1500                                $last_active = get_post_field( 'post_date', $topic_id );
1501                        }
1502                }
1503
1504                $last_active = !empty( $last_active ) ? bbp_get_time_since( bbp_convert_date( $last_active ) ) : '';
1505
1506                // Return the time since
1507                return apply_filters( 'bbp_get_topic_last_active', $last_active, $topic_id );
1508        }
1509
1510/** Topic Last Reply **********************************************************/
1511
1512/**
1513 * Output the id of the topics last reply
1514 *
1515 * @since bbPress (r2625)
1516 *
1517 * @param int $topic_id Optional. Topic id
1518 * @uses bbp_get_topic_last_reply_id() To get the topic last reply id
1519 */
1520function bbp_topic_last_reply_id( $topic_id = 0 ) {
1521        echo bbp_get_topic_last_reply_id( $topic_id );
1522}
1523        /**
1524         * Return the topics last update date/time (aka freshness)
1525         *
1526         * @since bbPress (r2625)
1527         *
1528         * @param int $topic_id Optional. Topic id
1529         * @uses bbp_get_topic_id() To get the topic id
1530         * @uses get_post_meta() To get the last reply id meta
1531         * @uses apply_filters() Calls 'bbp_get_topic_last_reply_id' with the
1532         *                        last reply id and topic id
1533         * @return int Topic last reply id
1534         */
1535        function bbp_get_topic_last_reply_id( $topic_id = 0 ) {
1536                $topic_id = bbp_get_topic_id( $topic_id );
1537                $reply_id = get_post_meta( $topic_id, '_bbp_last_reply_id', true );
1538
1539                if ( empty( $reply_id ) )
1540                        $reply_id = $topic_id;
1541
1542                return apply_filters( 'bbp_get_topic_last_reply_id', (int) $reply_id, $topic_id );
1543        }
1544
1545/**
1546 * Output the title of the last reply inside a topic
1547 *
1548 * @param int $topic_id Optional. Topic id
1549 * @uses bbp_get_topic_last_reply_title() To get the topic last reply title
1550 */
1551function bbp_topic_last_reply_title( $topic_id = 0 ) {
1552        echo bbp_get_topic_last_reply_title( $topic_id );
1553}
1554        /**
1555         * Return the title of the last reply inside a topic
1556         *
1557         * @param int $topic_id Optional. Topic id
1558         * @uses bbp_get_topic_id() To get the topic id
1559         * @uses bbp_get_topic_last_reply_id() To get the topic last reply id
1560         * @uses bbp_get_reply_title() To get the reply title
1561         * @uses apply_filters() Calls 'bbp_get_topic_last_topic_title' with
1562         *                        the reply title and topic id
1563         * @return string Topic last reply title
1564         */
1565        function bbp_get_topic_last_reply_title( $topic_id = 0 ) {
1566                $topic_id = bbp_get_topic_id( $topic_id );
1567                return apply_filters( 'bbp_get_topic_last_topic_title', bbp_get_reply_title( bbp_get_topic_last_reply_id( $topic_id ) ), $topic_id );
1568        }
1569
1570/**
1571 * Output the link to the last reply in a topic
1572 *
1573 * @since bbPress (r2464)
1574 *
1575 * @param int $topic_id Optional. Topic id
1576 * @uses bbp_get_topic_last_reply_permalink() To get the topic's last reply link
1577 */
1578function bbp_topic_last_reply_permalink( $topic_id = 0 ) {
1579        echo bbp_get_topic_last_reply_permalink( $topic_id );
1580}
1581        /**
1582         * Return the link to the last reply in a topic
1583         *
1584         * @since bbPress (r2464)
1585         *
1586         * @param int $topic_id Optional. Topic id
1587         * @uses bbp_get_topic_id() To get the topic id
1588         * @uses bbp_get_topic_last_reply_id() To get the topic last reply id
1589         * @uses bbp_get_reply_permalink() To get the reply permalink
1590         * @uses apply_filters() Calls 'bbp_get_topic_last_topic_permalink' with
1591         *                        the reply permalink and topic id
1592         * @return string Permanent link to the reply
1593         */
1594        function bbp_get_topic_last_reply_permalink( $topic_id = 0 ) {
1595                $topic_id = bbp_get_topic_id( $topic_id );
1596                return apply_filters( 'bbp_get_topic_last_reply_permalink', bbp_get_reply_permalink( bbp_get_topic_last_reply_id( $topic_id ) ) );
1597        }
1598
1599/**
1600 * Output the link to the last reply in a topic
1601 *
1602 * @since bbPress (r2683)
1603 *
1604 * @param int $topic_id Optional. Topic id
1605 * @uses bbp_get_topic_last_reply_url() To get the topic last reply url
1606 */
1607function bbp_topic_last_reply_url( $topic_id = 0 ) {
1608        echo bbp_get_topic_last_reply_url( $topic_id );
1609}
1610        /**
1611         * Return the link to the last reply in a topic
1612         *
1613         * @since bbPress (r2683)
1614         *
1615         * @param int $topic_id Optional. Topic id
1616         * @uses bbp_get_topic_id() To get the topic id
1617         * @uses bbp_get_topic_last_reply_id() To get the topic last reply id
1618         * @uses bbp_get_reply_url() To get the reply url
1619         * @uses bbp_get_reply_permalink() To get the reply permalink
1620         * @uses apply_filters() Calls 'bbp_get_topic_last_topic_url' with
1621         *                        the reply url and topic id
1622         * @return string Topic last reply url
1623         */
1624        function bbp_get_topic_last_reply_url( $topic_id = 0 ) {
1625                $topic_id = bbp_get_topic_id( $topic_id );
1626                $reply_id = bbp_get_topic_last_reply_id( $topic_id );
1627
1628                if ( !empty( $reply_id ) && ( $reply_id != $topic_id ) )
1629                        $reply_url = bbp_get_reply_url( $reply_id );
1630                else
1631                        $reply_url = bbp_get_topic_permalink( $topic_id );
1632
1633                return apply_filters( 'bbp_get_topic_last_reply_url', $reply_url );
1634        }
1635
1636/**
1637 * Output link to the most recent activity inside a topic, complete with link
1638 * attributes and content.
1639 *
1640 * @since bbPress (r2625)
1641 *
1642 * @param int $topic_id Optional. Topic id
1643 * @uses bbp_get_topic_freshness_link() To get the topic freshness link
1644 */
1645function bbp_topic_freshness_link( $topic_id = 0 ) {
1646        echo bbp_get_topic_freshness_link( $topic_id );
1647}
1648        /**
1649         * Returns link to the most recent activity inside a topic, complete
1650         * with link attributes and content.
1651         *
1652         * @since bbPress (r2625)
1653         *
1654         * @param int $topic_id Optional. Topic id
1655         * @uses bbp_get_topic_id() To get the topic id
1656         * @uses bbp_get_topic_last_reply_url() To get the topic last reply url
1657         * @uses bbp_get_topic_last_reply_title() To get the reply title
1658         * @uses bbp_get_topic_last_active_time() To get the topic freshness
1659         * @uses apply_filters() Calls 'bbp_get_topic_freshness_link' with the
1660         *                        link and topic id
1661         * @return string Topic freshness link
1662         */
1663        function bbp_get_topic_freshness_link( $topic_id = 0 ) {
1664                $topic_id   = bbp_get_topic_id( $topic_id );
1665                $link_url   = bbp_get_topic_last_reply_url( $topic_id );
1666                $title      = bbp_get_topic_last_reply_title( $topic_id );
1667                $time_since = bbp_get_topic_last_active_time( $topic_id );
1668
1669                if ( !empty( $time_since ) )
1670                        $anchor = '<a href="' . $link_url . '" title="' . esc_attr( $title ) . '">' . $time_since . '</a>';
1671                else
1672                        $anchor = __( 'No Replies', 'bbpress' );
1673
1674                return apply_filters( 'bbp_get_topic_freshness_link', $anchor, $topic_id );
1675        }
1676
1677/**
1678 * Output the replies link of the topic
1679 *
1680 * @since bbPress (r2740)
1681 *
1682 * @param int $topic_id Optional. Topic id
1683 * @uses bbp_get_topic_replies_link() To get the topic replies link
1684 */
1685function bbp_topic_replies_link( $topic_id = 0 ) {
1686        echo bbp_get_topic_replies_link( $topic_id );
1687}
1688
1689        /**
1690         * Return the replies link of the topic
1691         *
1692         * @since bbPress (r2740)
1693         *
1694         * @param int $topic_id Optional. Topic id
1695         * @uses bbp_get_topic_id() To get the topic id
1696         * @uses bbp_get_topic() To get the topic
1697         * @uses bbp_get_topic_reply_count() To get the topic reply count
1698         * @uses bbp_get_topic_permalink() To get the topic permalink
1699         * @uses remove_query_arg() To remove args from the url
1700         * @uses bbp_get_topic_reply_count_hidden() To get the topic hidden
1701         *                                           reply count
1702         * @uses current_user_can() To check if the current user can edit others
1703         *                           replies
1704         * @uses add_query_arg() To add custom args to the url
1705         * @uses apply_filters() Calls 'bbp_get_topic_replies_link' with the
1706         *                        replies link and topic id
1707         */
1708        function bbp_get_topic_replies_link( $topic_id = 0 ) {
1709
1710                $topic    = bbp_get_topic( bbp_get_topic_id( (int) $topic_id ) );
1711                $topic_id = $topic->ID;
1712                $replies  = bbp_get_topic_reply_count( $topic_id );
1713                $replies  = sprintf( _n( '%s reply', '%s replies', $replies, 'bbpress' ), $replies );
1714                $retval   = '';
1715
1716                // First link never has view=all
1717                if ( bbp_get_view_all( 'edit_others_replies' ) )
1718                        $retval .= "<a href='" . esc_url( bbp_remove_view_all( bbp_get_topic_permalink( $topic_id ) ) ) . "'>$replies</a>";
1719                else
1720                        $retval .= $replies;
1721
1722                // This forum has hidden topics
1723                if ( current_user_can( 'edit_others_replies' ) && ( $deleted = bbp_get_topic_reply_count_hidden( $topic_id ) ) ) {
1724
1725                        // Extra text
1726                        $extra = sprintf( __( ' (+ %d hidden)', 'bbpress' ), $deleted );
1727
1728                        // No link
1729                        if ( bbp_get_view_all() ) {
1730                                $retval .= " $extra";
1731
1732                        // Link
1733                        } else {
1734                                $retval .= " <a href='" . esc_url( bbp_add_view_all( bbp_get_topic_permalink( $topic_id ), true ) ) . "'>$extra</a>";
1735                        }
1736                }
1737
1738                return apply_filters( 'bbp_get_topic_replies_link', $retval, $topic_id );
1739        }
1740
1741/**
1742 * Output total reply count of a topic
1743 *
1744 * @since bbPress (r2485)
1745 *
1746 * @param int $topic_id Optional. Topic id
1747 * @uses bbp_get_topic_reply_count() To get the topic reply count
1748 */
1749function bbp_topic_reply_count( $topic_id = 0 ) {
1750        echo bbp_get_topic_reply_count( $topic_id );
1751}
1752        /**
1753         * Return total reply count of a topic
1754         *
1755         * @since bbPress (r2485)
1756         *
1757         * @param int $topic_id Optional. Topic id
1758         * @uses bbp_get_topic_id() To get the topic id
1759         * @uses get_post_meta() To get the topic reply count meta
1760         * @uses apply_filters() Calls 'bbp_get_topic_reply_count' with the
1761         *                        reply count and topic id
1762         * @return int Reply count
1763         */
1764        function bbp_get_topic_reply_count( $topic_id = 0 ) {
1765                $topic_id = bbp_get_topic_id( $topic_id );
1766                $replies  = get_post_meta( $topic_id, '_bbp_reply_count', true );
1767
1768                return apply_filters( 'bbp_get_topic_reply_count', (int) $replies, $topic_id );
1769        }
1770
1771/**
1772 * Output total post count of a topic
1773 *
1774 * @since bbPress (r2954)
1775 *
1776 * @param int $topic_id Optional. Topic id
1777 * @uses bbp_get_topic_post_count() To get the topic post count
1778 */
1779function bbp_topic_post_count( $topic_id = 0 ) {
1780        echo bbp_get_topic_post_count( $topic_id );
1781}
1782        /**
1783         * Return total post count of a topic
1784         *
1785         * @since bbPress (r2954)
1786         *
1787         * @param int $topic_id Optional. Topic id
1788         * @uses bbp_get_topic_id() To get the topic id
1789         * @uses get_post_meta() To get the topic post count meta
1790         * @uses apply_filters() Calls 'bbp_get_topic_post_count' with the
1791         *                        post count and topic id
1792         * @return int Post count
1793         */
1794        function bbp_get_topic_post_count( $topic_id = 0 ) {
1795                $topic_id = bbp_get_topic_id( $topic_id );
1796                $replies  = get_post_meta( $topic_id, '_bbp_reply_count', true );
1797
1798                return apply_filters( 'bbp_get_topic_post_count', (int) $replies + 1, $topic_id );
1799        }
1800
1801/**
1802 * Output total hidden reply count of a topic (hidden includes trashed and
1803 * spammed replies)
1804 *
1805 * @since bbPress (r2740)
1806 *
1807 * @param int $topic_id Optional. Topic id
1808 * @uses bbp_get_topic_reply_count_hidden() To get the topic hidden reply count
1809 */
1810function bbp_topic_reply_count_hidden( $topic_id = 0 ) {
1811        echo bbp_get_topic_reply_count_hidden( $topic_id );
1812}
1813        /**
1814         * Return total hidden reply count of a topic (hidden includes trashed
1815         * and spammed replies)
1816         *
1817         * @since bbPress (r2740)
1818         *
1819         * @param int $topic_id Optional. Topic id
1820         * @uses bbp_get_topic_id() To get the topic id
1821         * @uses get_post_meta() To get the hidden reply count
1822         * @uses apply_filters() Calls 'bbp_get_topic_reply_count_hidden' with
1823         *                        the hidden reply count and topic id
1824         * @return int Topic hidden reply count
1825         */
1826        function bbp_get_topic_reply_count_hidden( $topic_id = 0 ) {
1827                $topic_id = bbp_get_topic_id( $topic_id );
1828                $replies  = get_post_meta( $topic_id, '_bbp_reply_count_hidden', true );
1829
1830                return apply_filters( 'bbp_get_topic_reply_count_hidden', (int) $replies, $topic_id );
1831        }
1832
1833/**
1834 * Output total voice count of a topic
1835 *
1836 * @since bbPress (r2567)
1837 *
1838 * @param int $topic_id Optional. Topic id
1839 * @uses bbp_get_topic_voice_count() To get the topic voice count
1840 */
1841function bbp_topic_voice_count( $topic_id = 0 ) {
1842        echo bbp_get_topic_voice_count( $topic_id );
1843}
1844        /**
1845         * Return total voice count of a topic
1846         *
1847         * @since bbPress (r2567)
1848         *
1849         * @param int $topic_id Optional. Topic id
1850         * @uses bbp_get_topic_id() To get the topic id
1851         * @uses get_post_meta() To get the voice count meta
1852         * @uses apply_filters() Calls 'bbp_get_topic_voice_count' with the
1853         *                        voice count and topic id
1854         * @return int Voice count of the topic
1855         */
1856        function bbp_get_topic_voice_count( $topic_id = 0 ) {
1857                $topic_id = bbp_get_topic_id( $topic_id );
1858                $voices   = get_post_meta( $topic_id, '_bbp_voice_count', true );
1859
1860                return apply_filters( 'bbp_get_topic_voice_count', (int) $voices, $topic_id );
1861        }
1862
1863/**
1864 * Output a the tags of a topic
1865 *
1866 * @param int $topic_id Optional. Topic id
1867 * @param mixed $args See {@link bbp_get_topic_tag_list()}
1868 * @uses bbp_get_topic_tag_list() To get the topic tag list
1869 */
1870function bbp_topic_tag_list( $topic_id = 0, $args = '' ) {
1871        echo bbp_get_topic_tag_list( $topic_id, $args );
1872}
1873        /**
1874         * Return the tags of a topic
1875         *
1876         * @param int $topic_id Optional. Topic id
1877         * @param array $args This function supports these arguments:
1878         *  - before: Before the tag list
1879         *  - sep: Tag separator
1880         *  - after: After the tag list
1881         * @uses bbp_get_topic_id() To get the topic id
1882         * @uses get_the_term_list() To get the tags list
1883         * @return string Tag list of the topic
1884         */
1885        function bbp_get_topic_tag_list( $topic_id = 0, $args = '' ) {
1886
1887                $defaults = array(
1888                        'before' => '<div class="bbp-topic-tags"><p>' . __( 'Tagged:', 'bbpress' ) . '&nbsp;',
1889                        'sep'    => ', ',
1890                        'after'  => '</p></div>'
1891                );
1892
1893                $r = wp_parse_args( $args, $defaults );
1894                extract( $r );
1895
1896                $topic_id = bbp_get_topic_id( $topic_id );
1897
1898                // Topic is spammed, so display pre-spam terms
1899                if ( bbp_is_topic_spam( $topic_id ) ) {
1900
1901                        // Get pre-spam terms
1902                        $terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
1903
1904                        // If terms exist, explode them and compile the return value
1905                        if ( !empty( $terms ) ) {
1906                                $terms  = implode( $sep, $terms );
1907                                $retval = $before . $terms . $after;
1908
1909                        // No terms so return emty string
1910                        } else {
1911                                $retval = '';
1912                        }
1913
1914                // Topic is not spam so display a clickable term list
1915                } else {
1916                        $retval = get_the_term_list( $topic_id, bbp_get_topic_tag_tax_id(), $before, $sep, $after );
1917                }
1918
1919                return $retval;
1920        }
1921
1922/**
1923 * Output the row class of a topic
1924 *
1925 * @since bbPress (r2667)
1926 *
1927 * @param int $topic_id Optional. Topic id
1928 * @uses bbp_get_topic_class() To get the topic class
1929 */
1930function bbp_topic_class( $topic_id = 0,$class='') {
1931        echo bbp_get_topic_class( $topic_id,$class);
1932}
1933        /**
1934         * Return the row class of a topic
1935         *
1936         * @since bbPress (r2667)
1937         *
1938         * @param int $topic_id Optional. Topic id
1939         * @uses bbp_is_topic_sticky() To check if the topic is a sticky
1940         * @uses bbp_is_topic_super_sticky() To check if the topic is a super
1941         *                                    sticky
1942         * @uses get_post_class() To get the topic classes
1943         * @uses apply_filters() Calls 'bbp_get_topic_class' with the classes
1944         *                        and topic id
1945         * @return string Row class of a topic
1946         */
1947        function bbp_get_topic_class( $topic_id = 0,$class='') {
1948                global $bbp;
1949
1950                $topic_id  = bbp_get_topic_id( $topic_id );
1951                $count     = isset( $bbp->topic_query->current_post ) ? $bbp->topic_query->current_post : 1;
1952                $classes   = array();
1953                $classes[] = ( (int) $count % 2 )                    ? 'even'         : 'odd';
1954                $classes[] = bbp_is_topic_sticky( $topic_id, false ) ? 'sticky'       : '';
1955                $classes[] = bbp_is_topic_super_sticky( $topic_id  ) ? 'super-sticky' : '';
1956               
1957                if ( !empty($class) ) {
1958                        if ( !is_array( $class ) )
1959                                $class = preg_split('#\s+#', $class);
1960                        $classes = array_merge($classes, $class);
1961            }
1962                $classes   = array_filter( $classes );
1963                $classes   = get_post_class( $classes, $topic_id );
1964                $classes   = apply_filters( 'bbp_get_topic_class', $classes,$class, $topic_id );
1965                $retval    = 'class="' . join( ' ', $classes ) . '"';
1966
1967                return $retval;
1968        }
1969
1970/** Topic Admin Links *********************************************************/
1971
1972/**
1973 * Output admin links for topic
1974 *
1975 * @param mixed $args See {@link bbp_get_topic_admin_links()}
1976 * @uses bbp_get_topic_admin_links() To get the topic admin links
1977 */
1978function bbp_topic_admin_links( $args = '' ) {
1979        echo bbp_get_topic_admin_links( $args );
1980}
1981        /**
1982         * Return admin links for topic.
1983         *
1984         * Move topic functionality is handled by the edit topic page.
1985         *
1986         * @param mixed $args This function supports these arguments:
1987         *  - id: Optional. Topic id
1988         *  - before: Before the links
1989         *  - after: After the links
1990         *  - sep: Links separator
1991         *  - links: Topic admin links array
1992         * @uses current_user_can() To check if the current user can edit/delete
1993         *                           the topic
1994         * @uses bbp_get_topic_edit_link() To get the topic edit link
1995         * @uses bbp_get_topic_trash_link() To get the topic trash link
1996         * @uses bbp_get_topic_close_link() To get the topic close link
1997         * @uses bbp_get_topic_spam_link() To get the topic spam link
1998         * @uses bbp_get_topic_stick_link() To get the topic stick link
1999         * @uses bbp_get_topic_merge_link() To get the topic merge link
2000         * @uses bbp_get_topic_status() To get the topic status
2001         * @uses apply_filters() Calls 'bbp_get_topic_admin_links' with the
2002         *                        topic admin links and args
2003         * @return string Topic admin links
2004         */
2005        function bbp_get_topic_admin_links( $args = '' ) {
2006
2007                if ( !bbp_is_single_topic() )
2008                        return;
2009
2010                $defaults = array (
2011                        'id'     => bbp_get_topic_id(),
2012                        'before' => '<span class="bbp-admin-links">',
2013                        'after'  => '</span>',
2014                        'sep'    => ' | ',
2015                        'links'  => array()
2016                );
2017
2018                $r = wp_parse_args( $args, $defaults );
2019
2020                if ( !current_user_can( 'edit_topic', $r['id'] ) )
2021                        return;
2022
2023                if ( empty( $r['links'] ) ) {
2024                        $r['links'] = array(
2025                                'edit'  => bbp_get_topic_edit_link ( $r ),
2026                                'close' => bbp_get_topic_close_link( $r ),
2027                                'stick' => bbp_get_topic_stick_link( $r ),
2028                                'merge' => bbp_get_topic_merge_link( $r ),
2029                                'trash' => bbp_get_topic_trash_link( $r ),
2030                                'spam'  => bbp_get_topic_spam_link ( $r ),
2031                        );
2032                }
2033
2034                // Check caps for trashing the topic
2035                if ( !current_user_can( 'delete_topic', $r['id'] ) && !empty( $r['links']['trash'] ) )
2036                        unset( $r['links']['trash'] );
2037
2038                // See if links need to be unset
2039                $topic_status = bbp_get_topic_status( $r['id'] );
2040                if ( in_array( $topic_status, array( bbp_get_spam_status_id(), bbp_get_trash_status_id() ) ) ) {
2041
2042                        // Close link shouldn't be visible on trashed/spammed topics
2043                        unset( $r['links']['close'] );
2044
2045                        // Spam link shouldn't be visible on trashed topics
2046                        if ( $topic_status == bbp_get_trash_status_id() )
2047                                unset( $r['links']['spam'] );
2048
2049                        // Trash link shouldn't be visible on spam topics
2050                        elseif ( $topic_status == bbp_get_spam_status_id() )
2051                                unset( $r['links']['trash'] );
2052                }
2053
2054                // Process the admin links
2055                $links = implode( $r['sep'], array_filter( $r['links'] ) );
2056
2057                return apply_filters( 'bbp_get_topic_admin_links', $r['before'] . $links . $r['after'], $args );
2058        }
2059
2060/**
2061 * Output the edit link of the topic
2062 *
2063 * @since bbPress (r2727)
2064 *
2065 * @param mixed $args See {@link bbp_get_topic_edit_link()}
2066 * @uses bbp_get_topic_edit_link() To get the topic edit link
2067 */
2068function bbp_topic_edit_link( $args = '' ) {
2069        echo bbp_get_topic_edit_link( $args );
2070}
2071
2072        /**
2073         * Return the edit link of the topic
2074         *
2075         * @since bbPress (r2727)
2076         *
2077         * @param mixed $args This function supports these args:
2078         *  - id: Optional. Topic id
2079         *  - link_before: Before the link
2080         *  - link_after: After the link
2081         *  - edit_text: Edit text
2082         * @uses bbp_get_topic_id() To get the topic id
2083         * @uses bbp_get_topic() To get the topic
2084         * @uses current_user_can() To check if the current user can edit the
2085         *                           topic
2086         * @uses bbp_get_topic_edit_url() To get the topic edit url
2087         * @uses apply_filters() Calls 'bbp_get_topic_edit_link' with the link
2088         *                        and args
2089         * @return string Topic edit link
2090         */
2091        function bbp_get_topic_edit_link( $args = '' ) {
2092                $defaults = array (
2093                        'id'           => 0,
2094                        'link_before'  => '',
2095                        'link_after'   => '',
2096                        'edit_text'    => __( 'Edit', 'bbpress' )
2097                );
2098
2099                $r = wp_parse_args( $args, $defaults );
2100                extract( $r );
2101
2102                $topic = bbp_get_topic( bbp_get_topic_id( (int) $id ) );
2103
2104                // Bypass check if user has caps
2105                if ( !current_user_can( 'edit_others_topics' ) ) {
2106
2107                        // User cannot edit or it is past the lock time
2108                        if ( empty( $topic ) || !current_user_can( 'edit_topic', $topic->ID ) || bbp_past_edit_lock( $topic->post_date_gmt ) )
2109                                return;
2110                }
2111
2112                // No uri to edit topic
2113                if ( !$uri = bbp_get_topic_edit_url( $id ) )
2114                        return;
2115
2116                return apply_filters( 'bbp_get_topic_edit_link', $link_before . '<a href="' . $uri . '">' . $edit_text . '</a>' . $link_after, $args );
2117        }
2118
2119/**
2120 * Output URL to the topic edit page
2121 *
2122 * @since bbPress (r2753)
2123 *
2124 * @param int $topic_id Optional. Topic id
2125 * @uses bbp_get_topic_edit_url() To get the topic edit url
2126 */
2127function bbp_topic_edit_url( $topic_id = 0 ) {
2128        echo bbp_get_topic_edit_url( $topic_id );
2129}
2130        /**
2131         * Return URL to the topic edit page
2132         *
2133         * @since bbPress (r2753)
2134         *
2135         * @param int $topic_id Optional. Topic id
2136         * @uses bbp_get_topic_id() To get the topic id
2137         * @uses bbp_get_topic() To get the topic
2138         * @uses add_query_arg() To add custom args to the url
2139         * @uses home_url() To get the home url
2140         * @uses apply_filters() Calls 'bbp_get_topic_edit_url' with the edit
2141         *                        url and topic id
2142         * @return string Topic edit url
2143         */
2144        function bbp_get_topic_edit_url( $topic_id = 0 ) {
2145                global $wp_rewrite, $bbp;
2146
2147                if ( !$topic = bbp_get_topic( bbp_get_topic_id( $topic_id ) ) )
2148                        return;
2149
2150                // Pretty permalinks
2151                if ( $wp_rewrite->using_permalinks() ) {
2152                        $url = $wp_rewrite->root . $bbp->topic_slug . '/' . $topic->post_name . '/edit';
2153                        $url = home_url( user_trailingslashit( $url ) );
2154
2155                // Unpretty permalinks
2156                } else {
2157                        $url = add_query_arg( array( bbp_get_topic_post_type() => $topic->post_name, 'edit' => '1' ), home_url( '/' ) );
2158                }
2159
2160                return apply_filters( 'bbp_get_topic_edit_url', $url, $topic_id );
2161        }
2162
2163/**
2164 * Output the trash link of the topic
2165 *
2166 * @since bbPress (r2727)
2167 *
2168 * @param mixed $args See {@link bbp_get_topic_trash_link()}
2169 * @uses bbp_get_topic_trash_link() To get the topic trash link
2170 */
2171function bbp_topic_trash_link( $args = '' ) {
2172        echo bbp_get_topic_trash_link( $args );
2173}
2174
2175        /**
2176         * Return the trash link of the topic
2177         *
2178         * @since bbPress (r2727)
2179         *
2180         * @param mixed $args This function supports these args:
2181         *  - id: Optional. Topic id
2182         *  - link_before: Before the link
2183         *  - link_after: After the link
2184         *  - sep: Links separator
2185         *  - trash_text: Trash text
2186         *  - restore_text: Restore text
2187         *  - delete_text: Delete text
2188         * @uses bbp_get_topic_id() To get the topic id
2189         * @uses bbp_get_topic() To get the topic
2190         * @uses current_user_can() To check if the current user can delete the
2191         *                           topic
2192         * @uses bbp_is_topic_trash() To check if the topic is trashed
2193         * @uses bbp_get_topic_status() To get the topic status
2194         * @uses add_query_arg() To add custom args to the url
2195         * @uses wp_nonce_url() To nonce the url
2196         * @uses esc_url() To escape the url
2197         * @uses apply_filters() Calls 'bbp_get_topic_trash_link' with the link
2198         *                        and args
2199         * @return string Topic trash link
2200         */
2201        function bbp_get_topic_trash_link( $args = '' ) {
2202
2203                $defaults = array (
2204                        'id'           => 0,
2205                        'link_before'  => '',
2206                        'link_after'   => '',
2207                        'sep'          => ' | ',
2208                        'trash_text'   => __( 'Trash',   'bbpress' ),
2209                        'restore_text' => __( 'Restore', 'bbpress' ),
2210                        'delete_text'  => __( 'Delete',  'bbpress' )
2211                );
2212                $r = wp_parse_args( $args, $defaults );
2213                extract( $r );
2214
2215                $actions = array();
2216                $topic   = bbp_get_topic( bbp_get_topic_id( (int) $id ) );
2217
2218                if ( empty( $topic ) || !current_user_can( 'delete_topic', $topic->ID ) ) {
2219                        return;
2220                }
2221
2222                if ( bbp_is_topic_trash( $topic->ID ) ) {
2223                        $actions['untrash'] = '<a title="' . esc_attr( __( 'Restore this item from the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'untrash', 'topic_id' => $topic->ID ) ), 'untrash-' . $topic->post_type . '_' . $topic->ID ) ) . '">' . esc_html( $restore_text ) . '</a>';
2224                } elseif ( EMPTY_TRASH_DAYS ) {
2225                        $actions['trash']   = '<a title="' . esc_attr( __( 'Move this item to the Trash', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'trash', 'topic_id' => $topic->ID ) ), 'trash-' . $topic->post_type . '_' . $topic->ID ) ) . '">' . esc_html( $trash_text ) . '</a>';
2226                }
2227
2228                if ( bbp_is_topic_trash( $topic->ID ) || !EMPTY_TRASH_DAYS ) {
2229                        $actions['delete']  = '<a title="' . esc_attr( __( 'Delete this item permanently', 'bbpress' ) ) . '" href="' . esc_url( wp_nonce_url( add_query_arg( array( 'action' => 'bbp_toggle_topic_trash', 'sub_action' => 'delete', 'topic_id' => $topic->ID ) ), 'delete-' . $topic->post_type . '_' . $topic->ID ) ) . '" onclick="return confirm(\'' . esc_js( __( 'Are you sure you want to delete that permanently?', 'bbpress' ) ) . '\' );">' . esc_html( $delete_text ) . '</a>';
2230                }
2231
2232                // Process the admin links
2233                $actions = implode( $sep, $actions );
2234
2235                return apply_filters( 'bbp_get_topic_trash_link', $link_before . $actions . $link_after, $args );
2236        }
2237
2238/**
2239 * Output the close link of the topic
2240 *
2241 * @since bbPress (r2727)
2242 *
2243 * @param mixed $args See {@link bbp_get_topic_close_link()}
2244 * @uses bbp_get_topic_close_link() To get the topic close link
2245 */
2246function bbp_topic_close_link( $args = '' ) {
2247        echo bbp_get_topic_close_link( $args );
2248}
2249
2250        /**
2251         * Return the close link of the topic
2252         *
2253         * @since bbPress (r2727)
2254         *
2255         * @param mixed $args This function supports these args:
2256         *  - id: Optional. Topic id
2257         *  - link_before: Before the link
2258         *  - link_after: After the link
2259         *  - close_text: Close text
2260         *  - open_text: Open text
2261         * @uses bbp_get_topic_id() To get the topic id
2262         * @uses bbp_get_topic() To get the topic
2263         * @uses current_user_can() To check if the current user can edit the
2264         *                           topic
2265         * @uses bbp_is_topic_open() To check if the topic is open
2266         * @uses add_query_arg() To add custom args to the url
2267         * @uses wp_nonce_url() To nonce the url
2268         * @uses esc_url() To escape the url
2269         * @uses apply_filters() Calls 'bbp_get_topic_close_link' with the link
2270         *                        and args
2271         * @return string Topic close link
2272         */
2273        function bbp_get_topic_close_link( $args = '' ) {
2274                $defaults = array (
2275                        'id'          => 0,
2276                        'link_before' => '',
2277                        'link_after'  => '',
2278                        'sep'         => ' | ',
2279                        'close_text'  => __( 'Close', 'bbpress' ),
2280                        'open_text'   => __( 'Open',  'bbpress' )
2281                );
2282
2283                $r = wp_parse_args( $args, $defaults );
2284                extract( $r );
2285
2286                $topic = bbp_get_topic( bbp_get_topic_id( (int) $id ) );
2287
2288                if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
2289                        return;
2290
2291                $display = bbp_is_topic_open( $topic->ID ) ? $close_text : $open_text;
2292
2293                $uri = add_query_arg( array( 'action' => 'bbp_toggle_topic_close', 'topic_id' => $topic->ID ) );
2294                $uri = esc_url( wp_nonce_url( $uri, 'close-topic_' . $topic->ID ) );
2295
2296                return apply_filters( 'bbp_get_topic_close_link', $link_before . '<a href="' . $uri . '">' . $display . '</a>' . $link_after, $args );
2297        }
2298
2299/**
2300 * Output the stick link of the topic
2301 *
2302 * @since bbPress (r2754)
2303 *
2304 * @param mixed $args See {@link bbp_get_topic_stick_link()}
2305 * @uses bbp_get_topic_stick_link() To get the topic stick link
2306 */
2307function bbp_topic_stick_link( $args = '' ) {
2308        echo bbp_get_topic_stick_link( $args );
2309}
2310
2311        /**
2312         * Return the stick link of the topic
2313         *
2314         * @since bbPress (r2754)
2315         *
2316         * @param mixed $args This function supports these args:
2317         *  - id: Optional. Topic id
2318         *  - link_before: Before the link
2319         *  - link_after: After the link
2320         *  - stick_text: Stick text
2321         *  - unstick_text: Unstick text
2322         *  - super_text: Stick to front text
2323         * @uses bbp_get_topic_id() To get the topic id
2324         * @uses bbp_get_topic() To get the topic
2325         * @uses current_user_can() To check if the current user can edit the
2326         *                           topic
2327         * @uses bbp_is_topic_sticky() To check if the topic is a sticky
2328         * @uses add_query_arg() To add custom args to the url
2329         * @uses wp_nonce_url() To nonce the url
2330         * @uses esc_url() To escape the url
2331         * @uses apply_filters() Calls 'bbp_get_topic_stick_link' with the link
2332         *                        and args
2333         * @return string Topic stick link
2334         */
2335        function bbp_get_topic_stick_link( $args = '' ) {
2336                $defaults = array (
2337                        'id'           => 0,
2338                        'link_before'  => '',
2339                        'link_after'   => '',
2340                        'stick_text'   => __( 'Stick',    'bbpress' ),
2341                        'unstick_text' => __( 'Unstick',  'bbpress' ),
2342                        'super_text'   => __( 'to front', 'bbpress' ),
2343                );
2344
2345                $r = wp_parse_args( $args, $defaults );
2346                extract( $r );
2347
2348                $topic = bbp_get_topic( bbp_get_topic_id( (int) $id ) );
2349
2350                if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
2351                        return;
2352
2353                $is_sticky = bbp_is_topic_sticky( $topic->ID );
2354
2355                $stick_uri = add_query_arg( array( 'action' => 'bbp_toggle_topic_stick', 'topic_id' => $topic->ID ) );
2356                $stick_uri = esc_url( wp_nonce_url( $stick_uri, 'stick-topic_' . $topic->ID ) );
2357
2358                $stick_display = true == $is_sticky ? $unstick_text : $stick_text;
2359                $stick_display = '<a href="' . $stick_uri . '">' . $stick_display . '</a>';
2360
2361                if ( empty( $is_sticky ) ) {
2362                        $super_uri = add_query_arg( array( 'action' => 'bbp_toggle_topic_stick', 'topic_id' => $topic->ID, 'super' => 1 ) );
2363                        $super_uri = esc_url( wp_nonce_url( $super_uri, 'stick-topic_' . $topic->ID ) );
2364
2365                        $super_display = ' (<a href="' . $super_uri . '">' . $super_text . '</a>)';
2366                } else {
2367                        $super_display = '';
2368                }
2369
2370                return apply_filters( 'bbp_get_topic_stick_link', $link_before . $stick_display . $super_display . $link_after, $args );
2371        }
2372
2373/**
2374 * Output the merge link of the topic
2375 *
2376 * @since bbPress (r2756)
2377 *
2378 * @param mixed $args
2379 * @uses bbp_get_topic_merge_link() To get the topic merge link
2380 */
2381function bbp_topic_merge_link( $args = '' ) {
2382        echo bbp_get_topic_merge_link( $args );
2383}
2384
2385        /**
2386         * Return the merge link of the topic
2387         *
2388         * @since bbPress (r2756)
2389         *
2390         * @param mixed $args This function supports these args:
2391         *  - id: Optional. Topic id
2392         *  - link_before: Before the link
2393         *  - link_after: After the link
2394         *  - merge_text: Merge text
2395         * @uses bbp_get_topic_id() To get the topic id
2396         * @uses bbp_get_topic() To get the topic
2397         * @uses bbp_get_topic_edit_url() To get the topic edit url
2398         * @uses add_query_arg() To add custom args to the url
2399         * @uses esc_url() To escape the url
2400         * @uses apply_filters() Calls 'bbp_get_topic_merge_link' with the link
2401         *                        and args
2402         * @return string Topic merge link
2403         */
2404        function bbp_get_topic_merge_link( $args = '' ) {
2405                $defaults = array (
2406                        'id'           => 0,
2407                        'link_before'  => '',
2408                        'link_after'   => '',
2409                        'merge_text'    => __( 'Merge', 'bbpress' ),
2410                );
2411
2412                $r = wp_parse_args( $args, $defaults );
2413                extract( $r );
2414
2415                $topic = bbp_get_topic( bbp_get_topic_id( (int) $id ) );
2416
2417                if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
2418                        return;
2419
2420                $uri = esc_url( add_query_arg( array( 'action' => 'merge' ), bbp_get_topic_edit_url( $topic->ID ) ) );
2421
2422                return apply_filters( 'bbp_get_topic_merge_link', $link_before . '<a href="' . $uri . '">' . $merge_text . '</a>' . $link_after, $args );
2423        }
2424
2425/**
2426 * Output the spam link of the topic
2427 *
2428 * @since bbPress (r2727)
2429 *
2430 * @param mixed $args See {@link bbp_get_topic_spam_link()}
2431 * @uses bbp_get_topic_spam_link() Topic spam link
2432 */
2433function bbp_topic_spam_link( $args = '' ) {
2434        echo bbp_get_topic_spam_link( $args );
2435}
2436
2437        /**
2438         * Return the spam link of the topic
2439         *
2440         * @since bbPress (r2727)
2441         *
2442         * @param mixed $args This function supports these args:
2443         *  - id: Optional. Topic id
2444         *  - link_before: Before the link
2445         *  - link_after: After the link
2446         *  - spam_text: Spam text
2447         *  - unspam_text: Unspam text
2448         * @uses bbp_get_topic_id() To get the topic id
2449         * @uses bbp_get_topic() To get the topic
2450         * @uses current_user_can() To check if the current user can edit the
2451         *                           topic
2452         * @uses bbp_is_topic_spam() To check if the topic is marked as spam
2453         * @uses add_query_arg() To add custom args to the url
2454         * @uses wp_nonce_url() To nonce the url
2455         * @uses esc_url() To escape the url
2456         * @uses apply_filters() Calls 'bbp_get_topic_spam_link' with the link
2457         *                        and args
2458         * @return string Topic spam link
2459         */
2460        function bbp_get_topic_spam_link( $args = '' ) {
2461                $defaults = array (
2462                        'id'           => 0,
2463                        'link_before'  => '',
2464                        'link_after'   => '',
2465                        'sep'          => ' | ',
2466                        'spam_text'    => __( 'Spam',   'bbpress' ),
2467                        'unspam_text'  => __( 'Unspam', 'bbpress' )
2468                );
2469
2470                $r = wp_parse_args( $args, $defaults );
2471                extract( $r );
2472
2473                $topic = bbp_get_topic( bbp_get_topic_id( (int) $id ) );
2474
2475                if ( empty( $topic ) || !current_user_can( 'moderate', $topic->ID ) )
2476                        return;
2477
2478                $display = bbp_is_topic_spam( $topic->ID ) ? $unspam_text : $spam_text;
2479
2480                $uri = add_query_arg( array( 'action' => 'bbp_toggle_topic_spam', 'topic_id' => $topic->ID ) );
2481                $uri = esc_url( wp_nonce_url( $uri, 'spam-topic_' . $topic->ID ) );
2482
2483                return apply_filters( 'bbp_get_topic_spam_link', $link_before . '<a href="' . $uri . '">' . $display . '</a>' . $link_after, $args );
2484        }
2485
2486/** Topic Pagination **********************************************************/
2487
2488/**
2489 * Output the pagination count
2490 *
2491 * @since bbPress (r2519)
2492 *
2493 * @uses bbp_get_forum_pagination_count() To get the forum pagination count
2494 */
2495function bbp_forum_pagination_count() {
2496        echo bbp_get_forum_pagination_count();
2497}
2498        /**
2499         * Return the pagination count
2500         *
2501         * @since bbPress (r2519)
2502         *
2503         * @uses bbp_number_format() To format the number value
2504         * @uses apply_filters() Calls 'bbp_get_forum_pagination_count' with the
2505         *                        pagination count
2506         * @return string Forum Pagintion count
2507         */
2508        function bbp_get_forum_pagination_count() {
2509                global $bbp;
2510
2511                if ( empty( $bbp->topic_query ) )
2512                        return false;
2513
2514                // Set pagination values
2515                $start_num = intval( ( $bbp->topic_query->paged - 1 ) * $bbp->topic_query->posts_per_page ) + 1;
2516                $from_num  = bbp_number_format( $start_num );
2517                $to_num    = bbp_number_format( ( $start_num + ( $bbp->topic_query->posts_per_page - 1 ) > $bbp->topic_query->found_posts ) ? $bbp->topic_query->found_posts : $start_num + ( $bbp->topic_query->posts_per_page - 1 ) );
2518                $total     = bbp_number_format( !empty( $bbp->topic_query->found_posts ) ? $bbp->topic_query->found_posts : $bbp->topic_query->post_count );
2519
2520                /**
2521                 * Translators - _n() should not be needed, as singular/plural strings
2522                 * are already separated into unique strings for you
2523                 */
2524
2525                // More than one topic
2526                if ( $total > 1 ) {
2527
2528                        // Single topic in a forum with several pages
2529                        if ( (int) $from_num == (int) $to_num ) {
2530                                $retstr = sprintf( __( 'Viewing topic %1$s (of %2$s total)', 'bbpress' ), $from_num, $total );
2531
2532                        // Several topics in a forum with a single page
2533                        } elseif ( empty( $to_num ) ) {
2534                                $retstr = sprintf( __( 'Viewing %1$s topics', 'bbpress' ), $total );
2535
2536                        // Several topics in a forum with several pages
2537                        } elseif ( (int) $from_num != (int) $to_num ) {
2538                                $retstr = sprintf( __( 'Viewing %1$s topics - %2$s through %3$s (of %4$s total)', 'bbpress' ), $bbp->topic_query->post_count, $from_num, $to_num, $total );
2539                        }
2540
2541                // Only 1 topic
2542                } else {
2543                        $retstr = sprintf( __( 'Viewing %1$s topic', 'bbpress' ), $total );
2544                }
2545
2546                // Filter and return
2547                return apply_filters( 'bbp_get_topic_pagination_count', $retstr );
2548        }
2549
2550/**
2551 * Output pagination links
2552 *
2553 * @since bbPress (r2519)
2554 *
2555 * @uses bbp_get_forum_pagination_links() To get the pagination links
2556 */
2557function bbp_forum_pagination_links() {
2558        echo bbp_get_forum_pagination_links();
2559}
2560        /**
2561         * Return pagination links
2562         *
2563         * @since bbPress (r2519)
2564         *
2565         * @uses bbPress::topic_query::pagination_links To get the links
2566         * @return string Pagination links
2567         */
2568        function bbp_get_forum_pagination_links() {
2569                global $bbp;
2570
2571                if ( empty( $bbp->topic_query ) )
2572                        return false;
2573
2574                return apply_filters( 'bbp_get_forum_pagination_links', $bbp->topic_query->pagination_links );
2575        }
2576
2577/**
2578 * Displays topic notices
2579 *
2580 * @since bbPress (r2744)
2581 *
2582 * @uses bbp_is_single_topic() To check if it's a topic page
2583 * @uses bbp_get_topic_status() To get the topic status
2584 * @uses bbp_get_topic_id() To get the topic id
2585 * @uses apply_filters() Calls 'bbp_topic_notices' with the notice text, topic
2586 *                        status and topic id
2587 * @uses bbPress::errors::add() To add the notices to the error handler
2588 */
2589function bbp_topic_notices() {
2590
2591        // Bail if not viewing a topic
2592        if ( !bbp_is_single_topic() )
2593                return;
2594
2595        // Get the topic_status
2596        $topic_status = bbp_get_topic_status();
2597
2598        // Get the topic status
2599        switch ( $topic_status ) {
2600
2601                // Spam notice
2602                case bbp_get_spam_status_id() :
2603                        $notice_text = __( 'This topic is marked as spam.', 'bbpress' );
2604                        break;
2605
2606                // Trashed notice
2607                case bbp_get_trash_status_id() :
2608                        $notice_text = __( 'This topic is in the trash.',   'bbpress' );
2609                        break;
2610
2611                // Standard status
2612                default :
2613                        $notice_text = '';
2614                        break;
2615        }
2616
2617        // Filter notice text and bail if empty
2618        if ( !$notice_text = apply_filters( 'bbp_topic_notices', $notice_text, $topic_status, bbp_get_topic_id() ) )
2619                return;
2620
2621        bbp_add_error( 'topic_notice', $notice_text, 'message' );
2622}
2623
2624/**
2625 * Displays topic type select box (normal/sticky/super sticky)
2626 *
2627 * @since bbPress (r2784)
2628 *
2629 * @param $args This function supports these arguments:
2630 *  - stick_text: Sticky text
2631 *  - super_text: Super Sticky text
2632 *  - unstick_text: Unstick (normal) text
2633 *  - select_id: Select id. Defaults to bbp_stick_topic
2634 *  - tab: Tabindex
2635 *  - topic_id: Topic id
2636 * @uses bbp_get_topic_id() To get the topic id
2637 * @uses bbp_is_single_topic() To check if we're viewing a single topic
2638 * @uses bbp_is_topic_edit() To check if it is the topic edit page
2639 * @uses bbp_is_topic_super_sticky() To check if the topic is a super sticky
2640 * @uses bbp_is_topic_sticky() To check if the topic is a sticky
2641 */
2642function bbp_topic_type_select( $args = '' ) {
2643
2644        $defaults = array (
2645                'unstick_text' => __( 'Normal',       'bbpress' ),
2646                'stick_text'   => __( 'Sticky',       'bbpress' ),
2647                'super_text'   => __( 'Super Sticky', 'bbpress' ),
2648                'select_id'    => 'bbp_stick_topic',
2649                'tab'          => bbp_get_tab_index(),
2650                'topic_id'     => 0
2651        );
2652
2653        $r = wp_parse_args( $args, $defaults );
2654        extract( $r );
2655
2656        // Edit topic
2657        if ( bbp_is_single_topic() || bbp_is_topic_edit() ) {
2658
2659                // Get current topic id
2660                $topic_id = bbp_get_topic_id( $topic_id );
2661
2662                // Post value is passed
2663                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST[$select_id] ) ) {
2664                        $sticky_current = $_POST[$select_id];
2665
2666                // Topic is super sticky
2667                } elseif ( bbp_is_topic_super_sticky( $topic_id ) ) {
2668                        $sticky_current = 'super';
2669
2670                // Topic is sticky or normal
2671                } else {
2672                        $sticky_current = bbp_is_topic_sticky( $topic_id, false ) ? 'stick' : 'unstick';
2673                }
2674
2675        // New topic
2676        } else {
2677
2678                // Post value is passed
2679                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST[$select_id] ) ) {
2680                        $sticky_current = $_POST[$select_id];
2681
2682                // Default to unstick
2683                } else {
2684                        $sticky_current = 'unstick';
2685                }
2686        }
2687
2688        // Used variables
2689        $tab             = !empty( $tab ) ? ' tabindex="' . $tab . '"' : '';
2690        $select_id       = esc_attr( $select_id );
2691        $sticky_statuses = array (
2692                'unstick' => $unstick_text,
2693                'stick'   => $stick_text,
2694                'super'   => $super_text,
2695        ); ?>
2696
2697        <select name="<?php echo $select_id; ?>" id="<?php echo $select_id; ?>"<?php echo $tab; ?>>
2698
2699                <?php foreach ( $sticky_statuses as $sticky_status => $label ) : ?>
2700
2701                        <option value="<?php echo $sticky_status; ?>"<?php selected( $sticky_current, $sticky_status ); ?>><?php echo $label; ?></option>
2702
2703                <?php endforeach; ?>
2704
2705        </select>
2706
2707        <?php
2708}
2709
2710/** Single Topic **************************************************************/
2711
2712/**
2713 * Output a fancy description of the current topic, including total topics,
2714 * total replies, and last activity.
2715 *
2716 * @since bbPress (r2860)
2717 *
2718 * @param array $args See {@link bbp_get_single_topic_description()}
2719 * @uses bbp_get_single_topic_description() Return the eventual output
2720 */
2721function bbp_single_topic_description( $args = '' ) {
2722        echo bbp_get_single_topic_description( $args );
2723}
2724        /**
2725         * Return a fancy description of the current topic, including total topics,
2726         * total replies, and last activity.
2727         *
2728         * @since bbPress (r2860)
2729         *
2730         * @param mixed $args This function supports these arguments:
2731         *  - topic_id: Topic id
2732         *  - before: Before the text
2733         *  - after: After the text
2734         *  - size: Size of the avatar
2735         * @uses bbp_get_topic_id() To get the topic id
2736         * @uses bbp_get_topic_voice_count() To get the topic voice count
2737         * @uses bbp_get_topic_reply_count() To get the topic reply count
2738         * @uses bbp_get_topic_freshness_link() To get the topic freshness link
2739         * @uses bbp_get_topic_last_active_id() To get the topic last active id
2740         * @uses bbp_get_reply_author_link() To get the reply author link
2741         * @uses apply_filters() Calls 'bbp_get_single_topic_description' with
2742         *                        the description and args
2743         * @return string Filtered topic description
2744         */
2745        function bbp_get_single_topic_description( $args = '' ) {
2746                // Default arguments
2747                $defaults = array (
2748                        'topic_id'  => 0,
2749                        'before'    => '<div class="bbp-template-notice info"><p class="bbp-topic-description">',
2750                        'after'     => '</p></div>',
2751                        'size'      => 14
2752                );
2753                $r = wp_parse_args( $args, $defaults );
2754                extract( $r );
2755
2756                // Validate topic_id
2757                $topic_id = bbp_get_topic_id( $topic_id );
2758
2759                // Unhook the 'view all' query var adder
2760                remove_filter( 'bbp_get_topic_permalink', 'bbp_add_view_all' );
2761
2762                // Build the topic description
2763                $forum_id        = bbp_get_topic_forum_id      ( $topic_id );
2764                $voice_count     = bbp_get_topic_voice_count   ( $topic_id );
2765                $reply_count     = bbp_get_topic_replies_link  ( $topic_id );
2766                $time_since      = bbp_get_topic_freshness_link( $topic_id );
2767
2768                // Singular/Plural
2769                $voice_count     = sprintf( _n( '%s voice', '%s voices', $voice_count, 'bbpress' ), $voice_count );
2770
2771                // Topic has replies
2772                $last_reply = bbp_get_topic_last_active_id( $topic_id );
2773                if ( !empty( $last_reply ) ) {
2774                        $last_updated_by = bbp_get_author_link( array( 'post_id' => $last_reply, 'size' => $size ) );
2775                        $retstr = sprintf( __( 'This topic contains %1$s, has %2$s, and was last updated by %3$s %4$s ago.', 'bbpress' ), $reply_count, $voice_count, $last_updated_by, $time_since );
2776
2777                // Topic has no replies
2778                } else {
2779                        $retstr = sprintf( __( 'This topic contains %1$s and has %2$s.', 'bbpress' ), $voice_count, $reply_count );
2780                }
2781
2782                // Add the 'view all' filter back
2783                add_filter( 'bbp_get_topic_permalink', 'bbp_add_view_all' );
2784
2785                // Combine the elements together
2786                $retstr = $before . $retstr . $after;
2787
2788                // Return filtered result
2789                return apply_filters( 'bbp_get_single_topic_description', $retstr, $args );
2790        }
2791
2792/** Topic Tags ****************************************************************/
2793
2794/**
2795 * Output the unique id of the topic tag taxonomy
2796 *
2797 * @since bbPress (r3348)
2798 *
2799 * @uses bbp_get_topic_post_type() To get the topic post type
2800 */
2801function bbp_topic_tag_tax_id() {
2802        echo bbp_get_topic_tag_tax_id();
2803}
2804        /**
2805         * Return the unique id of the topic tag taxonomy
2806         *
2807         * @since bbPress (r3348)
2808         *
2809         * @uses apply_filters() Calls 'bbp_get_topic_tag_tax_id' with the topic tax id
2810         * @return string The unique topic tag taxonomy
2811         */
2812        function bbp_get_topic_tag_tax_id() {
2813                global $bbp;
2814
2815                return apply_filters( 'bbp_get_topic_tag_tax_id', $bbp->topic_tag_tax_id );
2816        }
2817
2818/**
2819 * Output the id of the current tag
2820 *
2821 * @since bbPress (r3109)
2822 *
2823 * @uses bbp_get_topic_tag_id()
2824 */
2825function bbp_topic_tag_id( $tag = '' ) {
2826        echo bbp_get_topic_tag_id( $tag );
2827}
2828        /**
2829         * Return the id of the current tag
2830         *
2831         * @since bbPress (r3109)
2832         *
2833         * @uses get_term_by()
2834         * @uses get_query_var()
2835         * @uses apply_filters()
2836         *
2837         * @return string Term Name
2838         */
2839        function bbp_get_topic_tag_id( $tag = '' ) {
2840
2841                // Get the term
2842                $tag  = !empty( $tag ) ? $tag : get_query_var( 'term' );
2843                $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
2844
2845                // Add before and after if description exists
2846                if ( !empty( $term->term_id ) )
2847                        $retval = $term->term_id;
2848
2849                // No id
2850                else
2851                        $retval = '';
2852
2853                return apply_filters( 'bbp_get_topic_tag_id', (int) $retval );
2854        }
2855
2856/**
2857 * Output the name of the current tag
2858 *
2859 * @since bbPress (r3109)
2860 *
2861 * @uses bbp_get_topic_tag_name()
2862 */
2863function bbp_topic_tag_name( $tag = '' ) {
2864        echo bbp_get_topic_tag_name( $tag );
2865}
2866        /**
2867         * Return the name of the current tag
2868         *
2869         * @since bbPress (r3109)
2870         *
2871         * @uses get_term_by()
2872         * @uses get_query_var()
2873         * @uses apply_filters()
2874         *
2875         * @return string Term Name
2876         */
2877        function bbp_get_topic_tag_name( $tag = '' ) {
2878
2879                // Get the term
2880                $tag  = !empty( $tag ) ? $tag : get_query_var( 'term' );
2881                $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
2882
2883                // Add before and after if description exists
2884                if ( !empty( $term->name ) )
2885                        $retval = $term->name;
2886
2887                // No name
2888                else
2889                        $retval = '';
2890
2891                return apply_filters( 'bbp_get_topic_tag_name', $retval );
2892        }
2893
2894/**
2895 * Output the slug of the current tag
2896 *
2897 * @since bbPress (r3109)
2898 *
2899 * @uses bbp_get_topic_tag_slug()
2900 */
2901function bbp_topic_tag_slug( $tag = '' ) {
2902        echo bbp_get_topic_tag_slug( $tag );
2903}
2904        /**
2905         * Return the slug of the current tag
2906         *
2907         * @since bbPress (r3109)
2908         *
2909         * @uses get_term_by()
2910         * @uses get_query_var()
2911         * @uses apply_filters()
2912         *
2913         * @return string Term Name
2914         */
2915        function bbp_get_topic_tag_slug( $tag = '' ) {
2916
2917                // Get the term
2918                $tag  = !empty( $tag ) ? $tag : get_query_var( 'term' );
2919                $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
2920
2921                // Add before and after if description exists
2922                if ( !empty( $term->slug ) )
2923                        $retval = $term->slug;
2924
2925                // No slug
2926                else
2927                        $retval = '';
2928
2929                return apply_filters( 'bbp_get_topic_tag_slug', $retval );
2930        }
2931
2932/**
2933 * Output the link of the current tag
2934 *
2935 * @since bbPress (r3348)
2936 *
2937 * @uses bbp_get_topic_tag_link()
2938 */
2939function bbp_topic_tag_link( $tag = '' ) {
2940        echo bbp_get_topic_tag_link( $tag );
2941}
2942        /**
2943         * Return the link of the current tag
2944         *
2945         * @since bbPress (r3348)
2946         *
2947         * @uses get_term_by()
2948         * @uses get_query_var()
2949         * @uses apply_filters()
2950         *
2951         * @return string Term Name
2952         */
2953        function bbp_get_topic_tag_link( $tag = '' ) {
2954
2955                // Get the term
2956                $tag  = !empty( $tag ) ? $tag : get_query_var( 'term' );
2957                $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
2958
2959                // Add before and after if description exists
2960                if ( !empty( $term->term_id ) )
2961                        $retval = get_term_link( $term, bbp_get_topic_tag_tax_id() );
2962
2963                // No link
2964                else
2965                        $retval = '';
2966
2967                return apply_filters( 'bbp_get_topic_tag_link', $retval );
2968        }
2969
2970/**
2971 * Output the link of the current tag
2972 *
2973 * @since bbPress (r3348)
2974 *
2975 * @uses bbp_get_topic_tag_edit_link()
2976 */
2977function bbp_topic_tag_edit_link( $tag = '' ) {
2978        echo bbp_get_topic_tag_edit_link( $tag );
2979}
2980        /**
2981         * Return the link of the current tag
2982         *
2983         * @since bbPress (r3348)
2984         *
2985         * @uses get_term_by()
2986         * @uses get_query_var()
2987         * @uses apply_filters()
2988         *
2989         * @return string Term Name
2990         */
2991        function bbp_get_topic_tag_edit_link( $tag = '' ) {
2992                global $wp_query, $wp_rewrite;
2993
2994                // Get the term
2995                $tag  = !empty( $tag ) ? $tag : get_query_var( 'term' );
2996                $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
2997
2998                // Add before and after if description exists
2999                if ( !empty( $term->term_id ) ) {
3000
3001                        // Pretty
3002                        if ( $wp_rewrite->using_permalinks() ) {
3003                                $retval = user_trailingslashit( trailingslashit( bbp_get_topic_tag_link() ) . 'edit' );
3004
3005                        // Ugly
3006                        } else {
3007                                $retval = add_query_arg( array( 'edit' => '1' ), bbp_get_topic_tag_link() );
3008                        }
3009
3010                // No link
3011                } else {
3012                        $retval = '';
3013                }
3014
3015                return apply_filters( 'bbp_get_topic_tag_edit_link', $retval );
3016        }
3017
3018/**
3019 * Output the description of the current tag
3020 *
3021 * @since bbPress (r3109)
3022 *
3023 * @uses bbp_get_topic_tag_description()
3024 */
3025function bbp_topic_tag_description( $args = array() ) {
3026        echo bbp_get_topic_tag_description( $args );
3027}
3028        /**
3029         * Return the description of the current tag
3030         *
3031         * @since bbPress (r3109)
3032         *
3033         * @uses get_term_by()
3034         * @uses get_query_var()
3035         * @uses apply_filters()
3036         *
3037         * @return string Term Name
3038         */
3039        function bbp_get_topic_tag_description( $args = array() ) {
3040
3041                $defaults = array(
3042                        'before' => '<div class="bbp-topic-tag-description"><p>',
3043                        'after'  => '</p></div>',
3044                        'tag'    => ''
3045                );
3046                $r = wp_parse_args( $args, $defaults );
3047                extract( $r );
3048
3049                // Get the term
3050                $tag  = !empty( $tag ) ? $tag : get_query_var( 'term' );
3051                $term = get_term_by( 'slug', $tag, bbp_get_topic_tag_tax_id() );
3052
3053                // Add before and after if description exists
3054                if ( !empty( $term->description ) )
3055                        $retval = $before . $term->description . $after;
3056
3057                // No description, no HTML
3058                else
3059                        $retval = '';
3060
3061                return apply_filters( 'bbp_get_topic_tag_description', $retval, $args );
3062        }
3063
3064/** Forms *********************************************************************/
3065
3066/**
3067 * Output the value of topic title field
3068 *
3069 * @since bbPress (r2976)
3070 *
3071 * @uses bbp_get_form_topic_title() To get the value of topic title field
3072 */
3073function bbp_form_topic_title() {
3074        echo bbp_get_form_topic_title();
3075}
3076        /**
3077         * Return the value of topic title field
3078         *
3079         * @since bbPress (r2976)
3080         *
3081         * @uses bbp_is_topic_edit() To check if it's topic edit page
3082         * @uses apply_filters() Calls 'bbp_get_form_topic_title' with the title
3083         * @return string Value of topic title field
3084         */
3085        function bbp_get_form_topic_title() {
3086                global $post;
3087
3088                // Get _POST data
3089                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['bbp_topic_title'] ) )
3090                        $topic_title = $_POST['bbp_topic_title'];
3091
3092                // Get edit data
3093                elseif ( !empty( $post->post_title ) && bbp_is_topic_edit() )
3094                        $topic_title = $post->post_title;
3095
3096                // No data
3097                else
3098                        $topic_title = '';
3099
3100                return apply_filters( 'bbp_get_form_topic_title', esc_attr( $topic_title ) );
3101        }
3102
3103/**
3104 * Output the value of topic content field
3105 *
3106 * @since bbPress (r2976)
3107 *
3108 * @uses bbp_get_form_topic_content() To get value of topic content field
3109 */
3110function bbp_form_topic_content() {
3111        echo bbp_get_form_topic_content();
3112}
3113        /**
3114         * Return the value of topic content field
3115         *
3116         * @since bbPress (r2976)
3117         *
3118         * @uses bbp_is_topic_edit() To check if it's the topic edit page
3119         * @uses apply_filters() Calls 'bbp_get_form_topic_content' with the content
3120         * @return string Value of topic content field
3121         */
3122        function bbp_get_form_topic_content() {
3123                global $post;
3124
3125                // Get _POST data
3126                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['bbp_topic_content'] ) )
3127                        $topic_content = $_POST['bbp_topic_content'];
3128
3129                // Get edit data
3130                elseif ( !empty( $post->post_content ) && bbp_is_topic_edit() )
3131                        $topic_content = $post->post_content;
3132
3133                // No data
3134                else
3135                        $topic_content = '';
3136
3137                return apply_filters( 'bbp_get_form_topic_content', esc_textarea( $topic_content ) );
3138        }
3139
3140/**
3141 * Output value of topic tags field
3142 *
3143 * @since bbPress (r2976)
3144 * @uses bbp_get_form_topic_tags() To get the value of topic tags field
3145 */
3146function bbp_form_topic_tags() {
3147        echo bbp_get_form_topic_tags();
3148}
3149        /**
3150         * Return value of topic tags field
3151         *
3152         * @since bbPress (r2976)
3153         *
3154         * @uses bbp_is_topic_edit() To check if it's the topic edit page
3155         * @uses apply_filters() Calls 'bbp_get_form_topic_tags' with the tags
3156         * @return string Value of topic tags field
3157         */
3158        function bbp_get_form_topic_tags() {
3159                global $post;
3160
3161                // Get _POST data
3162                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['bbp_topic_tags'] ) ) {
3163                        $topic_tags = $_POST['bbp_topic_tags'];
3164
3165                // Get edit data
3166                } elseif ( !empty( $post ) ) {
3167
3168                        // Post is a topic
3169                        if ( bbp_get_topic_post_type() == $post->post_type ) {
3170                                $topic_id = $post->ID;
3171
3172                        // Post is a reply
3173                        } elseif ( bbp_get_reply_post_type() == $post->post_type ) {
3174                                $topic_id = bbp_get_reply_topic_id( $post->ID );
3175                        }
3176
3177                        // Topic exists
3178                        if ( !empty( $topic_id ) ) {
3179
3180                                // Topic is spammed so display pre-spam terms
3181                                if ( bbp_is_topic_spam( $topic_id ) ) {
3182
3183                                        // Get pre-spam terms
3184                                        $new_terms = get_post_meta( $topic_id, '_bbp_spam_topic_tags', true );
3185
3186                                        // If terms exist, explode them and compile the return value
3187                                        if ( empty( $new_terms ) ) {
3188                                                $new_terms = '';
3189                                        }
3190
3191                                // Topic is not spam so get real terms
3192                                } else {
3193                                        $terms = array_filter( (array) get_the_terms( $topic_id, bbp_get_topic_tag_tax_id() ) );
3194
3195                                        // Loop through them
3196                                        foreach( $terms as $term ) {
3197                                                $new_terms[] = $term->name;
3198                                        }
3199                                }
3200
3201                        // Define local variable(s)
3202                        } else {
3203                                $new_terms = '';
3204                        }
3205
3206                        // Set the return value
3207                        $topic_tags = ( !empty( $new_terms ) ) ? implode( ', ', $new_terms ) : '';
3208
3209                // No data
3210                } else {
3211                        $topic_tags = '';
3212                }
3213
3214                return apply_filters( 'bbp_get_form_topic_tags', esc_attr( $topic_tags ) );
3215        }
3216
3217/**
3218 * Output value of topic forum
3219 *
3220 * @since bbPress (r2976)
3221 *
3222 * @uses bbp_get_form_topic_forum() To get the topic's forum id
3223 */
3224function bbp_form_topic_forum() {
3225        echo bbp_get_form_topic_forum();
3226}
3227        /**
3228         * Return value of topic forum
3229         *
3230         * @since bbPress (r2976)
3231         *
3232         * @uses bbp_is_topic_edit() To check if it's the topic edit page
3233         * @uses bbp_get_topic_forum_id() To get the topic forum id
3234         * @uses apply_filters() Calls 'bbp_get_form_topic_forum' with the forum
3235         * @return string Value of topic content field
3236         */
3237        function bbp_get_form_topic_forum() {
3238
3239                // Get _POST data
3240                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['bbp_forum_id'] ) )
3241                        $topic_forum = $_POST['bbp_forum_id'];
3242
3243                // Get edit data
3244                elseif ( bbp_is_topic_edit() )
3245                        $topic_forum = bbp_get_topic_forum_id();
3246
3247                // No data
3248                else
3249                        $topic_forum = 0;
3250
3251                return apply_filters( 'bbp_get_form_topic_forum', esc_attr( $topic_forum ) );
3252        }
3253
3254/**
3255 * Output checked value of topic subscription
3256 *
3257 * @since bbPress (r2976)
3258 *
3259 * @uses bbp_get_form_topic_subscribed() To get the subscribed checkbox value
3260 */
3261function bbp_form_topic_subscribed() {
3262        echo bbp_get_form_topic_subscribed();
3263}
3264        /**
3265         * Return checked value of topic subscription
3266         *
3267         * @since bbPress (r2976)
3268         *
3269         * @uses bbp_is_topic_edit() To check if it's the topic edit page
3270         * @uses bbp_is_user_subscribed() To check if the user is subscribed to
3271         *                                 the topic
3272         * @uses apply_filters() Calls 'bbp_get_form_topic_subscribed' with the
3273         *                        option
3274         * @return string Checked value of topic subscription
3275         */
3276        function bbp_get_form_topic_subscribed() {
3277                global $post;
3278
3279                // Get _POST data
3280                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['bbp_topic_subscription'] ) ) {
3281                        $topic_subscribed = $_POST['bbp_topic_subscription'];
3282
3283                // Get edit data
3284                } elseif ( bbp_is_topic_edit() || bbp_is_reply_edit() ) {
3285
3286                        // Post author is not the current user
3287                        if ( $post->post_author != bbp_get_current_user_id() ) {
3288                                $topic_subscribed = bbp_is_user_subscribed( $post->post_author );
3289
3290                        // Post author is the current user
3291                        } else {
3292                                $topic_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() );
3293                        }
3294
3295                // Get current status
3296                } elseif ( bbp_is_single_topic() ) {
3297                        $topic_subscribed = bbp_is_user_subscribed( bbp_get_current_user_id() );
3298
3299                // No data
3300                } else {
3301                        $topic_subscribed = 0;
3302                }
3303
3304                // Get checked output
3305                $checked = checked( $topic_subscribed, true, false );
3306
3307                return apply_filters( 'bbp_get_form_topic_subscribed', $checked, $topic_subscribed );
3308        }
3309
3310/**
3311 * Output checked value of topic log edit field
3312 *
3313 * @since bbPress (r2976)
3314 *
3315 * @uses bbp_get_form_topic_log_edit() To get the topic log edit value
3316 */
3317function bbp_form_topic_log_edit() {
3318        echo bbp_get_form_topic_log_edit();
3319}
3320        /**
3321         * Return checked value of topic log edit field
3322         *
3323         * @since bbPress (r2976)
3324         *
3325         * @uses apply_filters() Calls 'bbp_get_form_topic_log_edit' with the
3326         *                        log edit value
3327         * @return string Topic log edit checked value
3328         */
3329        function bbp_get_form_topic_log_edit() {
3330                global $post;
3331
3332                // Get _POST data
3333                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['bbp_log_topic_edit'] ) )
3334                        $topic_revision = $_POST['bbp_log_topic_edit'];
3335
3336                // No data
3337                else
3338                        $topic_revision = 1;
3339
3340                return apply_filters( 'bbp_get_form_topic_log_edit', checked( $topic_revision, true, false ) );
3341        }
3342
3343/**
3344 * Output the value of the topic edit reason
3345 *
3346 * @since bbPress (r2976)
3347 *
3348 * @uses bbp_get_form_topic_edit_reason() To get the topic edit reason value
3349 */
3350function bbp_form_topic_edit_reason() {
3351        echo bbp_get_form_topic_edit_reason();
3352}
3353        /**
3354         * Return the value of the topic edit reason
3355         *
3356         * @since bbPress (r2976)
3357         *
3358         * @uses apply_filters() Calls 'bbp_get_form_topic_edit_reason' with the
3359         *                        topic edit reason value
3360         * @return string Topic edit reason value
3361         */
3362        function bbp_get_form_topic_edit_reason() {
3363                global $post;
3364
3365                // Get _POST data
3366                if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && isset( $_POST['bbp_topic_edit_reason'] ) )
3367                        $topic_edit_reason = $_POST['bbp_topic_edit_reason'];
3368
3369                // No data
3370                else
3371                        $topic_edit_reason = '';
3372
3373                return apply_filters( 'bbp_get_form_topic_edit_reason', esc_attr( $topic_edit_reason ) );
3374        }
3375
3376?>