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

bbp-topic-template.php in bbPress 2.0-beta-1, at bbp-includes/bbp-topic-template.php

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