PluginProbe
bbPress / 2.0-beta-2b
bbPress v2.0-beta-2b
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-2b, at bbp-includes/bbp-topic-template.php

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