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

bbp-topic-template.php in bbPress 2.1, at bbp-includes/bbp-topic-template.php

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