PluginProbe
bbPress / 2.1.2
bbPress v2.1.2
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.2, at bbp-includes/bbp-topic-template.php

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