Skip to:
Content

bbPress.org

Changeset 2688


Ignore:
Timestamp:
12/06/2010 03:18:19 AM (16 years ago)
Author:
johnjamesjacoby
Message:

First pass at user profiles and front-end user editing. Fixes #1364 props GautamGupta via Google Code-in. Split bbp-template.php into smaller files. Rename 'profile-' pages to 'user-'. (This is the Ultra Magnus of changesets.)

Location:
branches/plugin
Files:
11 added
6 deleted
9 edited
2 moved

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-admin/bbp-admin.php

    r2679 r2688  
    331331         */
    332332        function user_profile_update ( $user_id ) {
    333                 if ( !bbp_has_access() )
    334                         return false;
     333                return false;
    335334
    336335                // Add extra actions to bbPress profile update
    … …  
    346345         */
    347346        function user_profile_forums ( $profileuser ) {
    348 
    349                 if ( !bbp_has_access() )
    350                         return false;
     347                return false;
    351348
    352349?>
  • branches/plugin/bbp-includes/bbp-functions.php

    r2684 r2688  
    307307
    308308/**
     309 * bbp_edit_user_handler ()
     310 *
     311 * Handles the front end user editing
     312 *
     313 * @since bbPress (r2688)
     314 */
     315function bbp_edit_user_handler () {
     316
     317        if ( 'POST' == $_SERVER['REQUEST_METHOD'] && !empty( $_POST['action'] ) && 'bbp-update-user' == $_POST['action'] ) {
     318
     319                global $errors, $bbp;
     320
     321                // Execute confirmed email change. See send_confirmation_on_profile_email().
     322                if ( is_multisite() && bbp_is_user_home() && isset( $_GET['newuseremail'] ) && $bbp->displayed_user->ID ) {
     323
     324                        $new_email = get_option( $bbp->displayed_user->ID . '_new_email' );
     325
     326                        if ( $new_email['hash'] == $_GET['newuseremail'] ) {
     327                                $user->ID         = $bbp->displayed_user->ID;
     328                                $user->user_email = esc_html( trim( $new_email['newemail'] ) );
     329
     330                                if ( $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $bbp->displayed_user->user_login ) ) )
     331                                        $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $user->user_email, $bbp->displayed_user->user_login ) );
     332
     333                                wp_update_user( get_object_vars( $user ) );
     334                                delete_option( $bbp->displayed_user->ID . '_new_email' );
     335
     336                                wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) ) );
     337                                exit;
     338                        }
     339
     340                } elseif ( is_multisite() && bbp_is_user_home() && !empty( $_GET['dismiss'] ) && $bbp->displayed_user->ID . '_new_email' == $_GET['dismiss'] ) {
     341
     342                        delete_option( $bbp->displayed_user->ID . '_new_email' );
     343                        wp_redirect( add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) ) );
     344                        exit;
     345
     346                }
     347
     348                check_admin_referer( 'update-user_' . $bbp->displayed_user->ID );
     349
     350                if ( !current_user_can( 'edit_user', $bbp->displayed_user->ID ) )
     351                        wp_die( __( 'What are you doing here? You don\'t have the permission to edit this user!', 'bbpress' ) );
     352
     353                if ( bbp_is_user_home() )
     354                        do_action( 'personal_options_update', $bbp->displayed_user->ID );
     355                else
     356                        do_action( 'edit_user_profile_update', $bbp->displayed_user->ID );
     357
     358                if ( !is_multisite() ) {
     359                        $errors = edit_user( $bbp->displayed_user->ID ); // Handles the trouble for us ;)
     360                } else {
     361                        $user   = get_userdata( $bbp->displayed_user->ID );
     362
     363                        // Update the email address in signups, if present.
     364                        if ( $user->user_login && isset( $_POST['email'] ) && is_email( $_POST['email' ]) && $wpdb->get_var( $wpdb->prepare( "SELECT user_login FROM {$wpdb->signups} WHERE user_login = %s", $user->user_login ) ) )
     365                                $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->signups} SET user_email = %s WHERE user_login = %s", $_POST['email'], $user_login ) );
     366
     367                        // WPMU must delete the user from the current blog if WP added him after editing.
     368                        $delete_role = false;
     369                        $blog_prefix = $wpdb->get_blog_prefix();
     370
     371                        if ( $bbp->displayed_user->ID != $bbp->displayed_user->ID ) {
     372                                $cap = $wpdb->get_var( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = '{$bbp->displayed_user->ID}' AND meta_key = '{$blog_prefix}capabilities' AND meta_value = 'a:0:{}'" );
     373                                if ( !is_network_admin() && null == $cap && $_POST['role'] == '' ) {
     374                                        $_POST['role'] = 'contributor';
     375                                        $delete_role = true;
     376                                }
     377                        }
     378
     379                        if ( !isset( $errors ) || ( isset( $errors ) && is_object( $errors ) && false == $errors->get_error_codes() ) )
     380                                $errors = edit_user( $bbp->displayed_user->ID );
     381
     382                        if ( $delete_role ) // stops users being added to current blog when they are edited
     383                                delete_user_meta( $bbp->displayed_user->ID, $blog_prefix . 'capabilities' );
     384
     385                        if ( is_multisite() && is_network_admin() & !bbp_is_user_home() && current_user_can( 'manage_network_options' ) && !isset( $super_admins ) && empty( $_POST['super_admin'] ) == is_super_admin( $bbp->displayed_user->ID ) )
     386                                empty( $_POST['super_admin'] ) ? revoke_super_admin( $bbp->displayed_user->ID ) : grant_super_admin( $bbp->displayed_user->ID );
     387                }
     388
     389                if ( !is_wp_error( $errors ) ) {
     390                        $redirect = add_query_arg( array( 'updated' => 'true' ), bbp_get_user_profile_edit_url( $bbp->displayed_user->ID ) );
     391
     392                        wp_redirect( $redirect );
     393                        exit;
     394                }
     395
     396        }
     397
     398}
     399add_action( 'template_redirect', 'bbp_edit_user_handler', 1 );
     400
     401/**
    309402 * bbp_new_topic_update_topic ()
    310403 *
    … …  
    353446
    354447/**
     448 * bbp_check_for_profile_page ()
     449 *
     450 * Add checks for a user page. If it is, then locate the user page template.
     451 *
     452 * @since bbPress (r2688)
     453 */
     454function bbp_check_for_profile_page ( $template = '' ) {
     455
     456        // Viewing a profile
     457        if ( bbp_is_user_profile_page() ) {
     458                $template = array( 'user.php', 'author.php', 'index.php' );
     459
     460        // Editing a profile
     461        } elseif ( bbp_is_user_profile_edit() ) {
     462                $template = array( 'user-edit.php', 'user.php', 'author.php', 'index.php' );
     463        }       
     464
     465        if ( !$template = apply_filters( 'bbp_check_for_profile_page', $template ) )
     466                return false;
     467
     468        // Try to load a template file
     469        bbp_load_template( $template );
     470}
     471add_action( 'template_redirect', 'bbp_check_for_profile_page', 2 );
     472
     473/**
     474 * bbp_pre_get_posts ()
     475 *
     476 * Add checks for a user page. If it is, then locate the user page template.
     477 *
     478 * @since bbPress (r2688)
     479 */
     480function bbp_pre_get_posts ( $wp_query ) {
     481        global $bbp;
     482
     483        $bbp_user     = get_query_var( 'bbp_user'         );
     484        $is_user_edit = get_query_var( 'bbp_edit_profile' );
     485
     486        if ( empty( $bbp_user ) )
     487                return;
     488
     489        // Create new user
     490        $user = new WP_User( $bbp_user );
     491
     492        // Stop if no user
     493        if ( !isset( $user ) || empty( $user ) || empty( $user->ID ) ) {
     494                $wp_query->set_404();
     495                return;
     496        }
     497
     498        // Confirmed existence of the bbPress user
     499
     500        // Define new query variable
     501        if ( !empty( $is_user_edit ) ) {
     502
     503                // Only allow super admins on multisite to edit every user.
     504                if ( ( is_multisite() && !current_user_can( 'manage_network_users' ) && $user_id != $current_user->ID && ! apply_filters( 'enable_edit_any_user_configuration', true ) ) || !current_user_can( 'edit_user', $user->ID ) )
     505                        wp_die( __( 'You do not have the permission to edit this user.', 'bbpress' ) );
     506
     507                $wp_query->bbp_is_user_profile_edit = true;
     508
     509                // Load the required user editing functions
     510                include_once( ABSPATH . 'wp-includes/registration.php' );
     511                require_once( ABSPATH . 'wp-admin/includes/user.php' );
     512
     513        } else {
     514                $wp_query->bbp_is_user_profile_page = true;
     515        }
     516
     517        // Set query variables
     518        $wp_query->is_home = false;                                     // Correct is_home variable
     519        $wp_query->query_vars['bbp_user_id'] = $user->ID;               // Set bbp_user_id for future reference
     520        $wp_query->query_vars['author_name'] = $user->user_nicename;    // Set author_name as current user's nicename to get correct posts
     521
     522        // Set the displayed user global to this user
     523        $bbp->displayed_user = $user;
     524
     525        add_filter( 'wp_title', 'bbp_profile_page_title', 10, 3 );      // Correct wp_title
     526}
     527add_action( 'pre_get_posts', 'bbp_pre_get_posts', 100 );
     528
     529        /**
     530         * bbp_profile_page_title ()
     531         *
     532         * Custom wp_title for bbPress User Profile Pages
     533         *
     534         * @since bbPress (r2688)
     535         *
     536         * @param string $title Optional. The title (not used).
     537         * @param string $sep Optional, default is '»'. How to separate the various items within the page title.
     538         * @param string $seplocation Optional. Direction to display title, 'right'.
     539         * @return string The tite
     540         */
     541        function bbp_profile_page_title ( $title = '', $sep = '»', $seplocation = '' ) {
     542                $userdata = get_userdata( get_query_var( 'bbp_user_id' ) );
     543                $title    = apply_filters( 'bbp_profile_page_wp_raw_title', $userdata->display_name, $sep, $seplocation );
     544                $t_sep    = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
     545
     546                $prefix = '';
     547                if ( !empty( $title ) )
     548                        $prefix = " $sep ";
     549
     550                // Determines position of the separator and direction of the breadcrumb
     551                if ( 'right' == $seplocation ) { // sep on right, so reverse the order
     552                        $title_array = explode( $t_sep, $title );
     553                        $title_array = array_reverse( $title_array );
     554                        $title       = implode( " $sep ", $title_array ) . $prefix;
     555                } else {
     556                        $title_array = explode( $t_sep, $title );
     557                        $title       = $prefix . implode( " $sep ", $title_array );
     558                }
     559
     560                $title = apply_filters( 'bbp_profile_page_wp_title', $title, $sep, $seplocation );
     561
     562                return $title;
     563        }
     564
     565/**
    355566 * bbp_load_template( $files )
    356567 *
    … …  
    473684
    474685                // Load user info
    475                 $current_user = wp_get_current_user();
    476                 $user_id      = $current_user->ID;
     686                if ( bbp_is_favorites( false ) ) {
     687                        $user_id = get_query_var( 'bbp_user_id' );
     688                } else {
     689                        $current_user = wp_get_current_user();
     690                        $user_id      = $current_user->ID;
     691                }
    477692
    478693                // Check current user's ability to edit the user
    … …  
    500715
    501716                                // Redirect back to new reply
    502                                 $redirect = bbp_is_favorites() ? bbp_get_favorites_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
     717                                $redirect = bbp_is_favorites( false ) ? bbp_get_favorites_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
    503718                                wp_redirect( $redirect );
    504719
    … …  
    509724        }
    510725}
    511 add_action( 'template_redirect', 'bbp_favorites_handler' );
     726add_action( 'template_redirect', 'bbp_favorites_handler', 1 );
    512727
    513728/**
    … …  
    569784
    570785                // Load user info
    571                 $current_user = wp_get_current_user();
    572                 $user_id      = $current_user->ID;
     786                if ( bbp_is_subscriptions( false ) ) {
     787                        $user_id = get_query_var( 'bbp_user_id' );
     788                } else {
     789                        $current_user = wp_get_current_user();
     790                        $user_id      = $current_user->ID;
     791                }
    573792
    574793                // Check current user's ability to edit the user
    … …  
    595814
    596815                                // Redirect back to new reply
    597                                 $redirect = bbp_get_topic_permalink( $topic_id );
     816                                $redirect = bbp_is_subscriptions( false ) ? bbp_get_subscriptions_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
    598817                                wp_redirect( $redirect );
    599818
    … …  
    604823        }
    605824}
    606 add_action( 'template_redirect', 'bbp_subscriptions_handler' );
     825add_action( 'template_redirect', 'bbp_subscriptions_handler', 1 );
    607826
    608827/**
  • branches/plugin/bbp-includes/bbp-general-template.php

    r2687 r2688  
    3333/** END - WordPress Add-on Actions ********************************************/
    3434
    35 /** START - Forum Loop Functions **********************************************/
    36 
    37 /**
    38  * bbp_has_forums ()
    39  *
    40  * The main forum loop. WordPress makes this easy for us
    41  *
    42  * @package bbPress
    43  * @subpackage Template Tags
    44  * @since bbPress (r2464)
    45  *
    46  * @global WP_Query $bbp_forums_template
    47  * @param array $args Possible arguments to change returned forums
    48  * @return object Multidimensional array of forum information
    49  */
    50 function bbp_has_forums ( $args = '' ) {
    51         global $wp_query, $bbp_forums_template, $bbp;
    52 
    53         $default = array (
    54                 'post_type'      => $bbp->forum_id,
    55                 'post_parent'    => bbp_get_forum_id(),
    56                 'posts_per_page' => -1,
    57                 'orderby'        => 'menu_order',
    58                 'order'          => 'ASC'
    59         );
    60 
    61         $r = wp_parse_args( $args, $default );
    62 
    63         $bbp_forums_template = new WP_Query( $r );
    64 
    65         return apply_filters( 'bbp_has_forums', $bbp_forums_template->have_posts(), $bbp_forums_template );
    66 }
    67 
    68 /**
    69  * bbp_forums ()
    70  *
    71  * Whether there are more forums available in the loop
    72  *
    73  * @package bbPress
    74  * @subpackage Template Tags
    75  * @since bbPress (r2464)
    76  *
    77  * @global WP_Query $bbp_forums_template
    78  * @return object Forum information
    79  */
    80 function bbp_forums () {
    81         global $bbp_forums_template;
    82         return $bbp_forums_template->have_posts();
    83 }
    84 
    85 /**
    86  * bbp_the_forum ()
    87  *
    88  * Loads up the current forum in the loop
    89  *
    90  * @package bbPress
    91  * @subpackage Template Tags
    92  * @since bbPress (r2464)
    93  *
    94  * @global WP_Query $bbp_forums_template
    95  * @return object Forum information
    96  */
    97 function bbp_the_forum () {
    98         global $bbp_forums_template;
    99         return $bbp_forums_template->the_post();
    100 }
    101 
    102 /** FORUM *********************************************************************/
    103 
    104 /**
    105  * bbp_forum_id ()
    106  *
    107  * Output id from bbp_forum_id()
    108  *
    109  * @package bbPress
    110  * @subpackage Template Tags
    111  * @since bbPress (r2464)
    112  *
    113  * @uses bbp_get_forum_id()
    114  */
    115 function bbp_forum_id () {
    116         echo bbp_get_forum_id();
    117 }
    118         /**
    119          * bbp_get_forum_id ()
    120          *
    121          * Return the forum ID
    122          *
    123          * @package bbPress
    124          * @subpackage Template Tags
    125          * @since bbPress (r2464)
    126          *
    127          * @param $forum_id Use to check emptiness
    128          * @global object $forums_template
    129          * @return string Forum id
    130          */
    131         function bbp_get_forum_id ( $forum_id = 0 ) {
    132                 global $bbp_forums_template, $wp_query, $bbp;
    133 
    134                 // Easy empty checking
    135                 if ( !empty( $forum_id ) && is_numeric( $forum_id ) )
    136                         $bbp_forum_id = $forum_id;
    137 
    138                 // Currently inside a forum loop
    139                 elseif ( !empty( $bbp_forums_template->in_the_loop ) && isset( $bbp_forums_template->post->ID ) )
    140                         $bbp_forum_id = $bbp_forums_template->post->ID;
    141 
    142                 // Currently viewing a forum
    143                 elseif ( bbp_is_forum() && isset( $wp_query->post->ID ) )
    144                         $bbp_forum_id = $wp_query->post->ID;
    145 
    146                 // Currently viewing a topic
    147                 elseif ( bbp_is_topic() )
    148                         $bbp_forum_id = bbp_get_topic_forum_id();
    149 
    150                 // Fallback
    151                 else
    152                         $bbp_forum_id = 0;
    153 
    154                 // Set global
    155                 $bbp->current_forum_id = $bbp_forum_id;
    156 
    157                 return apply_filters( 'bbp_get_forum_id', (int)$bbp_forum_id );
    158         }
    159 
    160 /**
    161  * bbp_forum_permalink ()
    162  *
    163  * Output the link to the forum
    164  *
    165  * @package bbPress
    166  * @subpackage Template Tags
    167  * @since bbPress (r2464)
    168  *
    169  * @param int $forum_id optional
    170  * @uses bbp_get_forum_permalink()
    171  */
    172 function bbp_forum_permalink ( $forum_id = 0 ) {
    173         echo bbp_get_forum_permalink( $forum_id );
    174 }
    175         /**
    176          * bbp_get_forum_permalink ()
    177          *
    178          * Return the link to the forum
    179          *
    180          * @package bbPress
    181          * @subpackage Template Tags
    182          * @since bbPress (r2464)
    183          *
    184          * @param int $forum_id optional
    185          * @uses apply_filters
    186          * @uses get_permalink
    187          * @return string Permanent link to forum
    188          */
    189         function bbp_get_forum_permalink ( $forum_id = 0 ) {
    190                 $forum_id = bbp_get_forum_id( $forum_id );
    191                 return apply_filters( 'bbp_get_forum_permalink', get_permalink( $forum_id ) );
    192         }
    193 
    194 /**
    195  * bbp_forum_title ()
    196  *
    197  * Output the title of the forum in the loop
    198  *
    199  * @package bbPress
    200  * @subpackage Template Tags
    201  * @since bbPress (r2464)
    202  *
    203  * @param int $forum_id optional
    204  * @uses bbp_get_forum_title()
    205  */
    206 function bbp_forum_title ( $forum_id = 0 ) {
    207         echo bbp_get_forum_title( $forum_id );
    208 }
    209         /**
    210          * bbp_get_forum_title ()
    211          *
    212          * Return the title of the forum in the loop
    213          *
    214          * @package bbPress
    215          * @subpackage Template Tags
    216          * @since bbPress (r2464)
    217          *
    218          * @param int $forum_id optional
    219          * @uses apply_filters
    220          * @uses get_the_title()
    221          * @return string Title of forum
    222          *
    223          */
    224         function bbp_get_forum_title ( $forum_id = 0 ) {
    225                 $forum_id = bbp_get_forum_id( $forum_id );
    226 
    227                 return apply_filters( 'bbp_get_forum_title', get_the_title( $forum_id ) );
    228         }
    229 
    230 /**
    231  * bbp_forum_last_active ()
    232  *
    233  * Output the forums last update date/time (aka freshness)
    234  *
    235  * @package bbPress
    236  * @subpackage Template Tags
    237  * @since bbPress (r2464)
    238  *
    239  * @uses bbp_get_forum_last_active()
    240  * @param int $forum_id optional
    241  */
    242 function bbp_forum_last_active ( $forum_id = 0 ) {
    243         echo bbp_get_forum_last_active( $forum_id );
    244 }
    245         /**
    246          * bbp_get_forum_last_active ()
    247          *
    248          * Return the forums last update date/time (aka freshness)
    249          *
    250          * @package bbPress
    251          * @subpackage Template Tags
    252          * @since bbPress (r2464)
    253          *
    254          * @return string
    255          * @param int $forum_id optional
    256          */
    257         function bbp_get_forum_last_active ( $forum_id = 0 ) {
    258                 $forum_id = bbp_get_forum_id( $forum_id );
    259 
    260                 if ( !$last_active = get_post_meta( $forum_id, '_bbp_forum_last_active', true ) ) {
    261                         if ( $reply_id = bbp_get_forum_last_reply_id( $forum_id ) ) {
    262                                 $last_active = get_post_field( 'post_date', $reply_id );
    263                         } else {
    264                                 if ( $topic_id = bbp_get_forum_last_topic_id( $forum_id ) ) {
    265                                         $last_active = bbp_get_topic_last_active( $forum_id, '_bbp_forum_last_active', true );
    266                                 }
    267                         }
    268                 }
    269 
    270                 $last_active = !empty( $last_active ) ? bbp_get_time_since( bbp_get_modified_time( $last_active ) ) : '';
    271 
    272                 return apply_filters( 'bbp_get_forum_last_active', $last_active );
    273         }
    274 
    275 /**
    276  * bbp_get_forum_parent ()
    277  *
    278  * Return ID of forum parent, if exists
    279  *
    280  * @package bbPress
    281  * @subpackage Template Tags
    282  * @since bbPress (r2625)
    283  *
    284  * @param int $forum_id
    285  * @return int
    286  */
    287 function bbp_get_forum_parent ( $forum_id = 0 ) {
    288         $forum_id = bbp_get_forum_id( $forum_id );
    289         return apply_filters( 'bbp_get_forum_parent', (int)get_post_field( 'post_parent', $forum_id ) );
    290 }
    291 
    292 /**
    293  * bbp_get_forum_ancestors ()
    294  *
    295  * Return array of parent forums
    296  *
    297  * @package bbPress
    298  * @subpackage Template Tags
    299  * @since bbPress (r2625)
    300  *
    301  * @param int $forum_id
    302  * @return array
    303  */
    304 function bbp_get_forum_ancestors ( $forum_id = 0 ) {
    305         $forum_id = bbp_get_forum_id( $forum_id );
    306 
    307         if ( $forum = get_post( $forum_id ) ) {
    308                 $ancestors = array();
    309                 while ( 0 !== $forum->post_parent ) {
    310                         $ancestors[] = $forum->post_parent;
    311                         $forum       = get_post( $forum->post_parent );
    312                 }
    313         }
    314 
    315         return apply_filters( 'bbp_get_forum_ancestors', $ancestors, $forum_id );
    316 }
    317 
    318 /**
    319  * bbp_forum_has_sub_forums ()
    320  *
    321  * Return if forum has sub forums
    322  *
    323  * @package bbPress
    324  * @subpackage Template Tags
    325  * @since bbPress (r2625)
    326  *
    327  * @param int $forum_id
    328  * @return false if none, array of subs if yes
    329  */
    330 function bbp_forum_has_sub_forums( $forum_id = 0 ) {
    331         $forum_id = bbp_get_forum_id( $forum_id );
    332         $has_subs = false;
    333 
    334         if ( !empty( $forum_id ) )
    335                 $has_subs = bbp_get_sub_forums( $forum_id );
    336 
    337         return apply_filters( 'bbp_forum_has_sub_forums', $has_subs );
    338 }
    339 
    340 /** FORUM LAST TOPIC **********************************************************/
    341 
    342 /**
    343  * bbp_forum_last_topic_id ()
    344  *
    345  * Output the forums last topic id
    346  *
    347  * @package bbPress
    348  * @subpackage Template Tags
    349  * @since bbPress (r2464)
    350  *
    351  * @uses bbp_get_forum_last_active()
    352  * @param int $forum_id optional
    353  */
    354 function bbp_forum_last_topic_id ( $forum_id = 0 ) {
    355         echo bbp_get_forum_last_topic_id( $forum_id );
    356 }
    357         /**
    358          * bbp_get_forum_last_topic_id ()
    359          *
    360          * Return the forums last topic
    361          *
    362          * @package bbPress
    363          * @subpackage Template Tags
    364          * @since bbPress (r2464)
    365          *
    366          * @return string
    367          * @param int $forum_id optional
    368          */
    369         function bbp_get_forum_last_topic_id ( $forum_id = 0 ) {
    370                 $forum_id = bbp_get_forum_id( $forum_id );
    371                 $topic_id = get_post_meta( $forum_id, '_bbp_forum_last_topic_id', true );
    372 
    373                 return apply_filters( 'bbp_get_forum_last_topic_id', $topic_id );
    374         }
    375 
    376 /**
    377  * bbp_forum_last_topic_title ()
    378  *
    379  * Output the title of the last topic inside a forum
    380  *
    381  * @package bbPress
    382  * @subpackage Template Tags
    383  * @since bbPress (r2625)
    384  *
    385  * @param int $forum_id
    386  */
    387 function bbp_forum_last_topic_title ( $forum_id = 0 ) {
    388         echo bbp_get_forum_last_topic_title( $forum_id );
    389 }
    390         /**
    391          * bbp_get_forum_last_topic_title ()
    392          *
    393          * Return the title of the last topic inside a forum
    394          *
    395          * @package bbPress
    396          * @subpackage Template Tags
    397          * @since bbPress (r2625)
    398          *
    399          * @param int $forum_id
    400          * @return string
    401          */
    402         function bbp_get_forum_last_topic_title( $forum_id = 0 ) {
    403                 $forum_id = bbp_get_forum_id( $forum_id );
    404                 return apply_filters( 'bbp_get_forum_last_topic_title', bbp_get_topic_title( bbp_get_forum_last_topic_id( $forum_id ) ) );
    405         }
    406 
    407 /**
    408  * bbp_forum_last_topic_permalink ()
    409  *
    410  * Output the link to the last topic in a forum
    411  *
    412  * @package bbPress
    413  * @subpackage Template Tags
    414  * @since bbPress (r2464)
    415  *
    416  * @param int $forum_id optional
    417  * @uses bbp_get_forum_permalink()
    418  */
    419 function bbp_forum_last_topic_permalink ( $forum_id = 0 ) {
    420         echo bbp_get_forum_last_topic_permalink( $forum_id );
    421 }
    422         /**
    423          * bbp_get_forum_last_topic_permalink ()
    424          *
    425          * Return the link to the last topic in a forum
    426          *
    427          * @package bbPress
    428          * @subpackage Template Tags
    429          * @since bbPress (r2464)
    430          *
    431          * @param int $forum_id optional
    432          * @uses apply_filters
    433          * @uses get_permalink
    434          * @return string Permanent link to topic
    435          */
    436         function bbp_get_forum_last_topic_permalink ( $forum_id = 0 ) {
    437                 $forum_id = bbp_get_forum_id( $forum_id );
    438                 return apply_filters( 'bbp_get_forum_last_topic_permalink', bbp_get_topic_permalink( bbp_get_forum_last_topic_id( $forum_id ) ) );
    439         }
    440 
    441 /** FORUM LAST REPLY **********************************************************/
    442 
    443 /**
    444  * bbp_forum_last_reply_id ()
    445  *
    446  * Output the forums last reply id
    447  *
    448  * @package bbPress
    449  * @subpackage Template Tags
    450  * @since bbPress (r2464)
    451  *
    452  * @uses bbp_get_forum_last_reply_id()
    453  * @param int $forum_id optional
    454  */
    455 function bbp_forum_last_reply_id ( $forum_id = 0 ) {
    456         echo bbp_get_forum_last_reply_id( $forum_id );
    457 }
    458         /**
    459          * bbp_get_forum_last_reply_id ()
    460          *
    461          * Return the forums last reply id
    462          *
    463          * @package bbPress
    464          * @subpackage Template Tags
    465          * @since bbPress (r2464)
    466          *
    467          * @return string
    468          * @param int $forum_id optional
    469          */
    470         function bbp_get_forum_last_reply_id ( $forum_id = 0 ) {
    471                 $forum_id = bbp_get_forum_id( $forum_id );
    472                 $reply_id = get_post_meta( $forum_id, '_bbp_forum_last_reply_id', true );
    473 
    474                 if ( '' === $reply_id )
    475                         $reply_id = bbp_update_forum_last_reply_id( $forum_id );
    476 
    477                 return apply_filters( 'bbp_get_forum_last_reply_id', $reply_id );
    478         }
    479 
    480 /**
    481  * bbp_forum_last_reply_title ()
    482  *
    483  * Output the title of the last reply inside a forum
    484  *
    485  * @param int $forum_id
    486  */
    487 function bbp_forum_last_reply_title ( $forum_id = 0 ) {
    488         echo bbp_get_forum_last_reply_title( $forum_id );
    489 }
    490         /**
    491          * bbp_get_forum_last_reply_title ()
    492          *
    493          * Return the title of the last reply inside a forum
    494          *
    495          * @param int $forum_id
    496          * @return string
    497          */
    498         function bbp_get_forum_last_reply_title( $forum_id = 0 ) {
    499                 $forum_id = bbp_get_forum_id( $forum_id );
    500                 return apply_filters( 'bbp_get_forum_last_topic_title', bbp_get_reply_title( bbp_get_forum_last_reply_id( $forum_id ) ) );
    501         }
    502 
    503 /**
    504  * bbp_forum_last_reply_permalink ()
    505  *
    506  * Output the link to the last reply in a forum
    507  *
    508  * @package bbPress
    509  * @subpackage Template Tags
    510  * @since bbPress (r2464)
    511  *
    512  * @param int $forum_id optional
    513  * @uses bbp_get_forum_permalink()
    514  */
    515 function bbp_forum_last_reply_permalink ( $forum_id = 0 ) {
    516         echo bbp_get_forum_last_reply_permalink( $forum_id );
    517 }
    518         /**
    519          * bbp_get_forum_last_reply_permalink ()
    520          *
    521          * Return the link to the last reply in a forum
    522          *
    523          * @package bbPress
    524          * @subpackage Template Tags
    525          * @since bbPress (r2464)
    526          *
    527          * @param int $forum_id optional
    528          * @uses apply_filters
    529          * @uses get_permalink
    530          * @return string Permanent link to topic
    531          */
    532         function bbp_get_forum_last_reply_permalink ( $forum_id = 0 ) {
    533                 $forum_id = bbp_get_forum_id( $forum_id );
    534                 return apply_filters( 'bbp_get_forum_last_reply_permalink', bbp_get_reply_permalink( bbp_get_forum_last_reply_id( $forum_id ) ) );
    535         }
    536 
    537 /**
    538  * bbp_forum_last_reply_url ()
    539  *
    540  * Output the link to the last reply in a forum
    541  *
    542  * @package bbPress
    543  * @subpackage Template Tags
    544  * @since bbPress (r2683)
    545  *
    546  * @param int $forum_id optional
    547  * @uses bbp_get_forum_url()
    548  */
    549 function bbp_forum_last_reply_url ( $forum_id = 0 ) {
    550         echo bbp_get_forum_last_reply_url( $forum_id );
    551 }
    552         /**
    553          * bbp_get_forum_last_reply_url ()
    554          *
    555          * Return the link to the last reply in a forum
    556          *
    557          * @package bbPress
    558          * @subpackage Template Tags
    559          * @since bbPress (r2683)
    560          *
    561          * @param int $forum_id optional
    562          * @uses apply_filters
    563          * @uses get_url
    564          * @return string Paginated URL to latest reply
    565          */
    566         function bbp_get_forum_last_reply_url ( $forum_id = 0 ) {
    567                 $forum_id  = bbp_get_forum_id( $forum_id );
    568 
    569                 // If forum has replies, get the last reply and use its url
    570                 if ( $reply_id  = bbp_get_forum_last_reply_id( $forum_id ) ) {
    571                         $reply_url = bbp_get_reply_url( $reply_id );
    572 
    573                 // No replies, so look for topics and use last permalink
    574                 } else {
    575                         if ( $topic_id = bbp_get_forum_last_topic_id( $forum_id ) ) {
    576                                 $reply_url = bbp_get_topic_permalink( $topic_id );
    577 
    578                         // No topics either, so set $reply_url as empty
    579                         } else {
    580                                 $reply_url = '';
    581                         }
    582                 }
    583 
    584                 // Filter and return
    585                 return apply_filters( 'bbp_get_forum_last_reply_url', $reply_url );
    586         }
    587 
    588 /**
    589  * bbp_forum_freshness_link ()
    590  *
    591  * Output link to the most recent activity inside a forum, complete with
    592  * link attributes and content.
    593  *
    594  * @package bbPress
    595  * @subpackage Template Tags
    596  * @since bbPress (r2625)
    597  *
    598  * @param int $forum_id
    599  */
    600 function bbp_forum_freshness_link ( $forum_id = 0) {
    601         echo bbp_get_forum_freshness_link( $forum_id );
    602 }
    603         /**
    604          * bbp_get_forum_freshness_link ()
    605          *
    606          * Returns link to the most recent activity inside a forum, complete with
    607          * link attributes and content.
    608          *
    609          * @package bbPress
    610          * @subpackage Template Tags
    611          * @since bbPress (r2625)
    612          *
    613          * @param int $forum_id
    614          */
    615         function bbp_get_forum_freshness_link ( $forum_id = 0 ) {
    616                 $forum_id   = bbp_get_forum_id( $forum_id );
    617                 $link_url   = bbp_get_forum_last_reply_url( $forum_id );
    618                 $title      = bbp_get_forum_last_reply_title( $forum_id );
    619                 $time_since = bbp_get_forum_last_active( $forum_id );
    620 
    621                 if ( !empty( $time_since ) )
    622                         $anchor = '<a href="' . $link_url . '" title="' . esc_attr( $title ) . '">' . $time_since . '</a>';
    623                 else
    624                         $anchor = __( 'No Topics', 'bbpress' );
    625 
    626                 return apply_filters( 'bbp_get_forum_freshness_link', $anchor );
    627         }
    628 
    629 /**
    630  * bbp_get_forum_last_topic_author_id ()
    631  *
    632  * Return the author ID of the last topic of a forum
    633  *
    634  * @package bbPress
    635  * @subpackage Template Tags
    636  * @since bbPress (r2625)
    637  *
    638  * @param int $forum_id
    639  */
    640 function bbp_get_forum_last_topic_author_id ( $forum_id = 0 ) {
    641         $forum_id  = bbp_get_forum_id( $forum_id );
    642         $author_id = get_post_field( 'post_author', bbp_get_forum_last_topic_id( $forum_id ) );
    643         return apply_filters( 'bbp_get_forum_last_topic_author', $author_id );
    644 }
    645 
    646 /**
    647  * bbp_forum_last_topic_author_link ()
    648  *
    649  * Output link to author of last topic of forum
    650  *
    651  * @package bbPress
    652  * @subpackage Template Tags
    653  * @since bbPress (r2625)
    654  *
    655  * @param int $forum_id
    656  */
    657 function bbp_forum_last_topic_author_link ( $forum_id = 0 ) {
    658         echo bbp_get_forum_last_topic_author_link( $forum_id );
    659 }
    660         /**
    661          * bbp_get_forum_last_topic_author_link ()
    662          *
    663          * Return link to author of last topic of forum
    664          *
    665          * @package bbPress
    666          * @subpackage Template Tags
    667          * @since bbPress (r2625)
    668          *
    669          * @param int $forum_id
    670          * @return string
    671          */
    672         function bbp_get_forum_last_topic_author_link ( $forum_id = 0 ) {
    673                 $forum_id    = bbp_get_forum_id( $forum_id );
    674                 $author_id   = bbp_get_forum_last_topic_author_id( $forum_id );
    675                 $name        = get_the_author_meta( 'display_name', $author_id );
    676                 $author_link = '<a href="' . get_author_posts_url( $author_id ) . '" title="' . esc_attr( $name ) . '">' . $name . '</a>';
    677                 return apply_filters( 'bbp_get_forum_last_topic_author_link', $author_link );
    678         }
    679 
    680 /**
    681  * bbp_forum_last_reply_author_id ()
    682  *
    683  * Output author ID of last reply of forum
    684  *
    685  * @package bbPress
    686  * @subpackage Template Tags
    687  * @since bbPress (r2625)
    688  *
    689  * @param int $forum_id
    690  */
    691 function bbp_forum_last_reply_author_id ( $forum_id = 0 ) {
    692         echo bbp_get_forum_last_reply_author_id( $forum_id );
    693 }
    694         /**
    695          * bbp_get_forum_last_reply_author_id ()
    696          *
    697          * Return author ID of last reply of forum
    698          *
    699          * @package bbPress
    700          * @subpackage Template Tags
    701          * @since bbPress (r2625)
    702          *
    703          * @param int $forum_id
    704          */
    705         function bbp_get_forum_last_reply_author_id ( $forum_id = 0 ) {
    706                 $forum_id  = bbp_get_forum_id( $forum_id );
    707                 $author_id = get_post_field( 'post_author', bbp_get_forum_last_reply_id( $forum_id ) );
    708                 return apply_filters( 'bbp_get_forum_last_reply_author', $author_id );
    709         }
    710 
    711 /**
    712  * bbp_forum_last_reply_author_link ()
    713  *
    714  * Output link to author of last reply of forum
    715  *
    716  * @package bbPress
    717  * @subpackage Template Tags
    718  * @since bbPress (r2625)
    719  *
    720  * @param int $forum_id
    721  */
    722 function bbp_forum_last_reply_author_link ( $forum_id = 0 ) {
    723         echo bbp_get_forum_last_reply_author_link( $forum_id );
    724 }
    725         /**
    726          * bbp_get_forum_last_reply_author_link ()
    727          *
    728          * Return link to author of last reply of forum
    729          *
    730          * @package bbPress
    731          * @subpackage Template Tags
    732          * @since bbPress (r2625)
    733          *
    734          * @param int $forum_id
    735          * @return string
    736          */
    737         function bbp_get_forum_last_reply_author_link ( $forum_id = 0 ) {
    738                 $forum_id    = bbp_get_forum_id( $forum_id );
    739                 $author_id   = bbp_get_forum_last_reply_author_id( $forum_id );
    740                 $name        = get_the_author_meta( 'display_name', $author_id );
    741                 $author_link = '<a href="' . get_author_posts_url( $author_id ) . '" title="' . esc_attr( $name ) . '">' . $name . '</a>';
    742                 return apply_filters( 'bbp_get_forum_last_reply_author_link', $author_link );
    743         }
    744 
    745 /** FORUM COUNTS **************************************************************/
    746 
    747 /**
    748  * bbp_forum_subforum_count ()
    749  *
    750  * Output total sub-forum count of a forum
    751  *
    752  * @package bbPress
    753  * @subpackage Template Tags
    754  * @since bbPress (r2464)
    755  *
    756  * @uses bbp_get_forum_subforum_count()
    757  * @param int $forum_id optional Forum ID to check
    758  */
    759 function bbp_forum_subforum_count ( $forum_id = 0 ) {
    760         echo bbp_get_forum_subforum_count( $forum_id );
    761 }
    762         /**
    763          * bbp_get_forum_subforum_count ()
    764          *
    765          * Return total sub-forum count of a forum
    766          *
    767          * @package bbPress
    768          * @subpackage Template Tags
    769          * @since bbPress (r2464)
    770          *
    771          * @uses bbp_get_forum_id
    772          * @uses get_pages
    773          * @uses apply_filters
    774          *
    775          * @param int $forum_id optional Forum ID to check
    776          */
    777         function bbp_get_forum_subforum_count ( $forum_id = 0 ) {
    778                 $forum_id    = bbp_get_forum_id( $forum_id );
    779                 $forum_count = get_post_meta( $forum_id, '_bbp_forum_subforum_count', true );
    780 
    781                 if ( '' === $forum_count )
    782                         $forum_count = bbp_update_forum_subforum_count( $forum_id );
    783 
    784                 return apply_filters( 'bbp_get_forum_subforum_count', $forum_count );
    785         }
    786 
    787 /**
    788  * bbp_forum_topic_count ()
    789  *
    790  * Output total topic count of a forum
    791  *
    792  * @package bbPress
    793  * @subpackage Template Tags
    794  * @since bbPress (r2464)
    795  *
    796  * @uses bbp_get_forum_topic_count()
    797  * @param int $forum_id optional Forum ID to check
    798  */
    799 function bbp_forum_topic_count ( $forum_id = 0 ) {
    800         echo bbp_get_forum_topic_count( $forum_id );
    801 }
    802         /**
    803          * bbp_get_forum_topic_count ()
    804          *
    805          * Return total topic count of a forum
    806          *
    807          * @package bbPress
    808          * @subpackage Template Tags
    809          * @since bbPress (r2464)
    810          *
    811          * @todo stash and cache (see commented out code)
    812          *
    813          * @uses bbp_get_forum_id
    814          * @uses get_pages
    815          * @uses apply_filters
    816          *
    817          * @param int $forum_id optional Forum ID to check
    818          */
    819         function bbp_get_forum_topic_count ( $forum_id = 0 ) {
    820                 $forum_id = bbp_get_forum_id( $forum_id );
    821                 $topics   = get_post_meta( $forum_id, '_bbp_forum_topic_count', true );
    822 
    823                 if ( '' === $topics )
    824                         $topics = bbp_update_forum_topic_count( $forum_id );
    825 
    826                 return apply_filters( 'bbp_get_forum_topic_count', $topics );
    827         }
    828 
    829 /**
    830  * bbp_forum_reply_count ()
    831  *
    832  * Output total reply count of a forum
    833  *
    834  * @package bbPress
    835  * @subpackage Template Tags
    836  * @since bbPress (r2464)
    837  *
    838  * @uses bbp_get_forum_topic_reply_count()
    839  * @param int $forum_id optional
    840  */
    841 function bbp_forum_reply_count ( $forum_id = 0 ) {
    842         echo bbp_get_forum_reply_count( $forum_id );
    843 }
    844         /**
    845          * bbp_forum_reply_count ()
    846          *
    847          * Return total post count of a forum
    848          *
    849          * @package bbPress
    850          * @subpackage Template Tags
    851          * @since bbPress (r2464)
    852          *
    853          * @todo stash and cache (see commented out code)
    854          *
    855          * @uses bbp_get_forum_id()
    856          * @uses get_pages
    857          * @uses apply_filters
    858          *
    859          * @param int $forum_id optional
    860          */
    861         function bbp_get_forum_reply_count ( $forum_id = 0 ) {
    862                 $forum_id = bbp_get_forum_id( $forum_id );
    863                 $replies  = get_post_meta( $forum_id, '_bbp_forum_reply_count', true );
    864 
    865                 if ( '' === $replies )
    866                         $replies = bbp_update_forum_reply_count( $forum_id );
    867 
    868                 return apply_filters( 'bbp_get_forum_reply_count', (int)$replies );
    869         }
    870 
    871 /**
    872  * bbp_forum_voice_count ()
    873  *
    874  * Output total voice count of a forum
    875  *
    876  * @package bbPress
    877  * @subpackage Template Tags
    878  * @since bbPress (r2567)
    879  *
    880  * @uses bbp_get_forum_voice_count()
    881  * @uses apply_filters
    882  *
    883  * @param int $forum_id
    884  */
    885 function bbp_forum_voice_count ( $forum_id = 0 ) {
    886         echo bbp_get_forum_voice_count( $forum_id );
    887 }
    888         /**
    889          * bbp_get_forum_voice_count ()
    890          *
    891          * Return total voice count of a forum
    892          *
    893          * @package bbPress
    894          * @subpackage Template Tags
    895          * @since bbPress (r2567)
    896          *
    897          * @uses bbp_get_forum_id()
    898          * @uses apply_filters
    899          *
    900          * @param int $forum_id
    901          *
    902          * @return int Voice count of the forum
    903          */
    904         function bbp_get_forum_voice_count ( $forum_id = 0 ) {
    905                 $forum_id = bbp_get_forum_id( $forum_id );
    906                 $voices   = get_post_meta( $forum_id, '_bbp_forum_voice_count', true );
    907 
    908                 if ( '' === $voices )
    909                         $voices = bbp_update_forum_voice_count( $forum_id );
    910 
    911                 return apply_filters( 'bbp_get_forum_voice_count', (int)$voices, $forum_id );
    912         }
    913 
    914 /**
    915  * bbp_forum_status ()
    916  *
    917  * Output the status of the forum in the loop
    918  *
    919  * @package bbPress
    920  * @subpackage Template Tags
    921  * @since bbPress (r2667)
    922  * @param int $forum_id optional
    923  *
    924  * @uses bbp_get_forum_status()
    925  */
    926 function bbp_forum_status ( $forum_id = 0 ) {
    927         echo bbp_get_forum_status( $forum_id );
    928 }
    929         /**
    930          * bbp_get_forum_status ()
    931          *
    932          * Return the status of the forum in the loop
    933          *
    934          * @package bbPress
    935          * @subpackage Template Tags
    936          * @since bbPress (r2667)
    937          *
    938          * @uses apply_filters
    939          * @uses get_post_status()
    940          * @param int $forum_id optional
    941          *
    942          * @return string Status of forum
    943          */
    944         function bbp_get_forum_status ( $forum_id = 0 ) {
    945                 $forum_id = bbp_get_forum_id( $forum_id );
    946 
    947                 return apply_filters( 'bbp_get_forum_status', get_post_status( $forum_id ) );
    948         }
    949 
    950 /**
    951  * bbp_forum_class ()
    952  *
    953  * Output the row class of a forum
    954  *
    955  * @package bbPress
    956  * @subpackage Template Tags
    957  * @since bbPress (r2667)
    958  */
    959 function bbp_forum_class ( $forum_id = 0 ) {
    960         echo bbp_get_forum_class( $forum_id );
    961 }
    962         /**
    963          * bbp_get_forum_class ()
    964          *
    965          * Return the row class of a forum
    966          *
    967          * @package bbPress
    968          * @subpackage Template Tags
    969          * @since bbPress (r2667)
    970          *
    971          * @global WP_Query $bbp_forums_template
    972          * @param int $forum_id
    973          * @return string
    974          */
    975         function bbp_get_forum_class ( $forum_id = 0 ) {
    976                 global $bbp_forums_template;
    977 
    978                 $alternate = $bbp_forums_template->current_post % 2 ? '' : 'alternate';
    979                 $status    = 'status-'  . bbp_get_forum_status();
    980                 $post      = post_class( array( $alternate, $status ) );
    981 
    982                 return apply_filters( 'bbp_get_forum_class', $post );
    983         }
    984 
    985 /** Forum Updaters ************************************************************/
    986 
    987 /**
    988  * bbp_update_forum_last_topic_id ()
    989  *
    990  * Update the forum last topic id
    991  *
    992  * @package bbPress
    993  * @subpackage Template Tags
    994  * @since bbPress (r2625)
    995  *
    996  * @todo everything
    997  * @param int $forum_id
    998  */
    999 function bbp_update_forum_last_topic_id ( $forum_id = 0, $topic_id = 0 ) {
    1000         $forum_id = bbp_get_forum_id( $forum_id );
    1001         $topic_id = bbp_get_topic_id( $topic_id );
    1002 
    1003         // Update the last reply ID
    1004         if ( !empty( $topic_id ) ) {
    1005                 update_post_meta( $forum_id, '_bbp_forum_last_topic_id', $topic_id );
    1006                 return true;
    1007         }
    1008 
    1009         return false;
    1010 }
    1011 
    1012 /**
    1013  * bbp_update_forum_last_reply_id ()
    1014  *
    1015  * Update the forum last reply id
    1016  *
    1017  * @package bbPress
    1018  * @subpackage Template Tags
    1019  * @since bbPress (r2625)
    1020  *
    1021  * @todo everything
    1022  * @param int $forum_id
    1023  */
    1024 function bbp_update_forum_last_reply_id ( $forum_id = 0, $reply_id = 0 ) {
    1025         $forum_id = bbp_get_forum_id( $forum_id );
    1026         $reply_id = bbp_get_reply_id( $reply_id );
    1027 
    1028         // Update the last reply ID
    1029         if ( !empty( $reply_id ) ) {
    1030                 update_post_meta( $forum_id, '_bbp_forum_last_reply_id', $reply_id );
    1031                 return true;
    1032         }
    1033 
    1034         return false;
    1035 }
    1036 
    1037 /**
    1038  * bbp_update_forum_last_active ()
    1039  *
    1040  * Update the forums last active date/time (aka freshness)
    1041  *
    1042  * @package bbPress
    1043  * @subpackage Template Tags
    1044  * @since bbPress (r2680)
    1045  *
    1046  * @param int $forum_id optional
    1047  *
    1048  * @return string
    1049  */
    1050 function bbp_update_forum_last_active ( $forum_id = 0, $new_time = '' ) {
    1051         $forum_id = bbp_get_forum_id( $forum_id );
    1052 
    1053         // Check time and use current if empty
    1054         if ( empty( $new_time ) )
    1055                 $new_time = current_time( 'mysql' );
    1056 
    1057         // Update the last reply ID
    1058         if ( !empty( $forum_id ) ) {
    1059                 update_post_meta( $forum_id, '_bbp_forum_last_active', $new_time );
    1060                 return true;
    1061         }
    1062 
    1063         return false;
    1064 }
    1065 
    1066 /**
    1067  * bbp_update_forum_subforum_count ()
    1068  *
    1069  * Update the forum sub-forum count
    1070  *
    1071  * @package bbPress
    1072  * @subpackage Template Tags
    1073  * @since bbPress (r2625)
    1074  *
    1075  * @todo everything
    1076  * @param int $forum_id
    1077  */
    1078 function bbp_update_forum_subforum_count ( $forum_id = 0 ) {
    1079         $forum_id = bbp_get_forum_id( $forum_id );
    1080 }
    1081 
    1082 /**
    1083  * bbp_update_forum_topic_count ()
    1084  *
    1085  * Adjust the total topic count of a forum
    1086  *
    1087  * @package bbPress
    1088  * @subpackage Template Tags
    1089  * @since bbPress (r2464)
    1090  *
    1091  * @param int $forum_id optional
    1092  * @return int
    1093  */
    1094 function bbp_update_forum_topic_count ( $forum_id = 0 ) {
    1095         global $wpdb, $bbp;
    1096 
    1097         $forum_id = bbp_get_forum_id( $forum_id );
    1098 
    1099         // If it's a reply, then get the parent (topic id)
    1100         if ( $bbp->topic_id == get_post_field( 'post_type', $forum_id ) )
    1101                 $forum_id = get_post_field( 'post_parent', $forum_id );
    1102 
    1103         // Get topics count
    1104         $topics = count( $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->topic_id . "';", $forum_id ) ) );
    1105 
    1106         // Update the count
    1107         update_post_meta( $forum_id, '_bbp_forum_topic_count', (int)$topics );
    1108 
    1109         return apply_filters( 'bbp_update_forum_topic_count', (int)$topics );
    1110 }
    1111 
    1112 /**
    1113  * bbp_update_forum_reply_count ()
    1114  *
    1115  * Adjust the total post count of a forum
    1116  *
    1117  * @package bbPress
    1118  * @subpackage Template Tags
    1119  * @since bbPress (r2464)
    1120  *
    1121  * @uses bbp_get_forum_id(0
    1122  * @uses apply_filters
    1123  *
    1124  * @param int $forum_id optional
    1125  *
    1126  * @return int
    1127  */
    1128 function bbp_update_forum_reply_count ( $forum_id = 0 ) {
    1129         global $wpdb, $bbp;
    1130 
    1131         $forum_id = bbp_get_forum_id( $forum_id );
    1132 
    1133         // If it's a reply, then get the parent (topic id)
    1134         if ( $bbp->reply_id == get_post_field( 'post_type', $forum_id ) )
    1135                 $forum_id = get_post_field( 'post_parent', $forum_id );
    1136 
    1137         // There should always be at least 1 voice
    1138         $replies = count( $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->reply_id . "';", $forum_id ) ) );
    1139 
    1140         // Update the count
    1141         update_post_meta( $forum_id, '_bbp_forum_reply_count', (int)$replies );
    1142 
    1143         return apply_filters( 'bbp_update_forum_reply_count', (int)$replies );
    1144 }
    1145 
    1146 /**
    1147  * bbp_update_forum_voice_count ()
    1148  *
    1149  * Adjust the total voice count of a forum
    1150  *
    1151  * @package bbPress
    1152  * @subpackage Template Tags
    1153  * @since bbPress (r2567)
    1154  *
    1155  * @uses bbp_get_forum_id()
    1156  * @uses wpdb
    1157  * @uses apply_filters
    1158  *
    1159  * @todo cache
    1160  *
    1161  * @param int $forum_id optional Topic ID to update
    1162  *
    1163  * @return bool false on failure, voice count on success
    1164  */
    1165 function bbp_update_forum_voice_count ( $forum_id = 0 ) {
    1166         global $wpdb, $bbp;
    1167 
    1168         $forum_id = bbp_get_forum_id( $forum_id );
    1169 
    1170         // If it is not a forum or reply, then we don't need it
    1171         if ( !in_array( get_post_field( 'post_type', $forum_id ), array( $bbp->forum_id, $bbp->reply_id ) ) )
    1172                 return false;
    1173 
    1174         // If it's a reply, then get the parent (forum id)
    1175         if ( $bbp->reply_id == get_post_field( 'post_type', $forum_id ) )
    1176                 $forum_id = get_post_field( 'post_parent', $forum_id );
    1177 
    1178         // There should always be at least 1 voice
    1179         if ( !$voices = count( $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE ( post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->reply_id . "' ) OR ( ID = %d AND post_type = '" . $bbp->forum_id . "' );", $forum_id, $forum_id ) ) ) )
    1180                 $voices = 1;
    1181 
    1182         // Update the count
    1183         update_post_meta( $forum_id, '_bbp_forum_voice_count', (int)$voices );
    1184 
    1185         return apply_filters( 'bbp_update_forum_voice_count', (int)$voices );
    1186 }
    1187 
    1188 /** END - Forum Loop Functions ************************************************/
    1189 
    1190 /** START - Topic Loop Functions **********************************************/
    1191 
    1192 /**
    1193  * bbp_has_topics()
    1194  *
    1195  * The main topic loop. WordPress makes this easy for us
    1196  *
    1197  * @package bbPress
    1198  * @subpackage Template Tags
    1199  * @since bbPress (r2485)
    1200  *
    1201  * @global WP_Query $bbp_topics_template
    1202  * @param array $args Possible arguments to change returned topics
    1203  * @return object Multidimensional array of topic information
    1204  */
    1205 function bbp_has_topics ( $args = '' ) {
    1206         global $wp_rewrite, $bbp_topics_template, $bbp;
    1207 
    1208         $default = array (
    1209                 // Narrow query down to bbPress topics
    1210                 'post_type'        => $bbp->topic_id,
    1211 
    1212                 // Forum ID
    1213                 'post_parent'      => isset( $_REQUEST['forum_id'] ) ? $_REQUEST['forum_id'] : bbp_get_forum_id(),
    1214 
    1215                 // Make sure topic has some last activity time
    1216                 'meta_key'         => '_bbp_topic_last_active',
    1217 
    1218                 //'author', 'date', 'title', 'modified', 'parent', rand',
    1219                 'orderby'          => isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : 'meta_value',
    1220 
    1221                 // 'ASC', 'DESC'
    1222                 'order'            => isset( $_REQUEST['order'] ) ? $_REQUEST['order'] : 'DESC',
    1223 
    1224                 // @todo replace 15 with setting
    1225                 'posts_per_page'   => isset( $_REQUEST['posts'] ) ? $_REQUEST['posts'] : 15,
    1226 
    1227                 // Page Number
    1228                 'paged'            => bbp_get_paged(),
    1229 
    1230                 // Topic Search
    1231                 's'                => empty( $_REQUEST['ts'] ) ? '' : $_REQUEST['ts'],
    1232         );
    1233 
    1234         // Don't pass post_parent if forum_id is empty or 0
    1235         if ( empty( $default['post_parent'] ) ) {
    1236                 unset( $default['post_parent'] );
    1237                 $post_parent = get_the_ID();
    1238         }
    1239 
    1240         // Set up topic variables
    1241         $bbp_t = wp_parse_args( $args, $default );
    1242         $r     = extract( $bbp_t );
    1243 
    1244         // If we're viewing a tax/term, use the existing query; if not, run our own
    1245         if ( !is_tax() ) {
    1246                 $bbp_topics_template = new WP_Query( $bbp_t );
    1247         } else {
    1248                 global $wp_query;
    1249                 $bbp_topics_template = $wp_query;
    1250         }
    1251 
    1252         if ( -1 == $posts_per_page )
    1253                 $posts_per_page = $bbp_topics_template->post_count;
    1254 
    1255         // Add pagination values to query object
    1256         $bbp_topics_template->posts_per_page = $posts_per_page;
    1257         $bbp_topics_template->paged          = $paged;
    1258 
    1259         // Only add pagination if query returned results
    1260         if ( ( (int)$bbp_topics_template->post_count || (int)$bbp_topics_template->found_posts ) && (int)$bbp_topics_template->posts_per_page ) {
    1261 
    1262                 // If pretty permalinks are enabled, make our pagination pretty
    1263                 if ( $wp_rewrite->using_permalinks() )
    1264                         $base = user_trailingslashit( trailingslashit( get_permalink( $post_parent ) ) . 'page/%#%/' );
    1265                 else
    1266                         $base = add_query_arg( 'page', '%#%' );
    1267 
    1268                 // Pagination settings with filter
    1269                 $bbp_topic_pagination = apply_filters( 'bbp_topic_pagination', array (
    1270                         'base'      => $base,
    1271                         'format'    => '',
    1272                         'total'     => $posts_per_page == $bbp_topics_template->found_posts ? 1 : ceil( (int)$bbp_topics_template->found_posts / (int)$posts_per_page ),
    1273                         'current'   => (int)$bbp_topics_template->paged,
    1274                         'prev_text' => '&larr;',
    1275                         'next_text' => '&rarr;',
    1276                         'mid_size'  => 1
    1277                 ) );
    1278 
    1279                 // Add pagination to query object
    1280                 $bbp_topics_template->pagination_links = paginate_links ( $bbp_topic_pagination );
    1281 
    1282                 // Remove first page from pagination
    1283                 $bbp_topics_template->pagination_links = str_replace( 'page/1/\'', '\'', $bbp_topics_template->pagination_links );
    1284         }
    1285 
    1286         // Return object
    1287         return apply_filters( 'bbp_has_topics', $bbp_topics_template->have_posts(), $bbp_topics_template );
    1288 }
    1289 
    1290 /**
    1291  * bbp_topics()
    1292  *
    1293  * Whether there are more topics available in the loop
    1294  *
    1295  * @package bbPress
    1296  * @subpackage Template Tags
    1297  * @since bbPress (r2485)
    1298  *
    1299  * @global WP_Query $bbp_topics_template
    1300  * @return object Forum information
    1301  */
    1302 function bbp_topics () {
    1303         global $bbp_topics_template;
    1304         return $bbp_topics_template->have_posts();
    1305 }
    1306 
    1307 /**
    1308  * bbp_the_topic()
    1309  *
    1310  * Loads up the current topic in the loop
    1311  *
    1312  * @package bbPress
    1313  * @subpackage Template Tags
    1314  * @since bbPress (r2485)
    1315  *
    1316  * @global WP_Query $bbp_topics_template
    1317  * @return object Forum information
    1318  */
    1319 function bbp_the_topic () {
    1320         global $bbp_topics_template;
    1321         return $bbp_topics_template->the_post();
    1322 }
    1323 
    1324 /**
    1325  * bbp_topic_id()
    1326  *
    1327  * Output id from bbp_topic_id()
    1328  *
    1329  * @package bbPress
    1330  * @subpackage Template Tags
    1331  * @since bbPress (r2485)
    1332  *
    1333  * @uses bbp_get_topic_id()
    1334  */
    1335 function bbp_topic_id () {
    1336         echo bbp_get_topic_id();
    1337 }
    1338         /**
    1339          * bbp_get_topic_id()
    1340          *
    1341          * Return the topic ID
    1342          *
    1343          * @package bbPress
    1344          * @subpackage Template Tags
    1345          * @since bbPress (r2485)
    1346          *
    1347          * @global object $topics_template
    1348          * @return string Forum id
    1349          */
    1350         function bbp_get_topic_id ( $topic_id = 0 ) {
    1351                 global $bbp_topics_template, $wp_query, $bbp;
    1352 
    1353                 // Easy empty checking
    1354                 if ( !empty( $topic_id ) && is_numeric( $topic_id ) )
    1355                         $bbp_topic_id = $topic_id;
    1356 
    1357                 // Currently inside a topic loop
    1358                 elseif ( !empty( $bbp_topics_template->in_the_loop ) && isset( $bbp_topics_template->post->ID ) )
    1359                         $bbp_topic_id = $bbp_topics_template->post->ID;
    1360 
    1361                 // Currently viewing a topic
    1362                 elseif ( bbp_is_topic() && isset( $wp_query->post->ID ) )
    1363                         $bbp_topic_id = $wp_query->post->ID;
    1364 
    1365                 // Currently viewing a singular reply
    1366                 elseif ( bbp_is_reply() )
    1367                         $bbp_topic_id = bbp_get_reply_topic_id();
    1368 
    1369                 // Fallback
    1370                 else
    1371                         $bbp_topic_id = 0;
    1372 
    1373                 $bbp->current_topic_id = $bbp_topic_id;
    1374 
    1375                 return apply_filters( 'bbp_get_topic_id', (int)$bbp_topic_id );
    1376         }
    1377 
    1378 /**
    1379  * bbp_topic_permalink ()
    1380  *
    1381  * Output the link to the topic in the topic loop
    1382  *
    1383  * @package bbPress
    1384  * @subpackage Template Tags
    1385  * @since bbPress (r2485)
    1386  *
    1387  * @uses bbp_get_topic_permalink()
    1388  * @param int $topic_id optional
    1389  */
    1390 function bbp_topic_permalink ( $topic_id = 0 ) {
    1391         echo bbp_get_topic_permalink( $topic_id );
    1392 }
    1393         /**
    1394          * bbp_get_topic_permalink()
    1395          *
    1396          * Return the link to the topic in the loop
    1397          *
    1398          * @package bbPress
    1399          * @subpackage Template Tags
    1400          * @since bbPress (r2485)
    1401          *
    1402          * @uses apply_filters
    1403          * @uses get_permalink
    1404          * @param int $topic_id optional
    1405          *
    1406          * @return string Permanent link to topic
    1407          */
    1408         function bbp_get_topic_permalink ( $topic_id = 0 ) {
    1409                 $topic_id = bbp_get_topic_id( $topic_id );
    1410 
    1411                 return apply_filters( 'bbp_get_topic_permalink', get_permalink( $topic_id ) );
    1412         }
    1413 
    1414 /**
    1415  * bbp_topic_title ()
    1416  *
    1417  * Output the title of the topic in the loop
    1418  *
    1419  * @package bbPress
    1420  * @subpackage Template Tags
    1421  * @since bbPress (r2485)
    1422  * @param int $topic_id optional
    1423  *
    1424  * @uses bbp_get_topic_title()
    1425  */
    1426 function bbp_topic_title ( $topic_id = 0 ) {
    1427         echo bbp_get_topic_title( $topic_id );
    1428 }
    1429         /**
    1430          * bbp_get_topic_title ()
    1431          *
    1432          * Return the title of the topic in the loop
    1433          *
    1434          * @package bbPress
    1435          * @subpackage Template Tags
    1436          * @since bbPress (r2485)
    1437          *
    1438          * @uses apply_filters
    1439          * @uses get_the_title()
    1440          * @param int $topic_id optional
    1441          *
    1442          * @return string Title of topic
    1443          */
    1444         function bbp_get_topic_title ( $topic_id = 0 ) {
    1445                 $topic_id = bbp_get_topic_id( $topic_id );
    1446 
    1447                 return apply_filters( 'bbp_get_topic_title', get_the_title( $topic_id ) );
    1448         }
    1449 
    1450 /**
    1451  * bbp_topic_status ()
    1452  *
    1453  * Output the status of the topic in the loop
    1454  *
    1455  * @package bbPress
    1456  * @subpackage Template Tags
    1457  * @since bbPress (r2667)
    1458  * @param int $topic_id optional
    1459  *
    1460  * @uses bbp_get_topic_status()
    1461  */
    1462 function bbp_topic_status ( $topic_id = 0 ) {
    1463         echo bbp_get_topic_status( $topic_id );
    1464 }
    1465         /**
    1466          * bbp_get_topic_status ()
    1467          *
    1468          * Return the status of the topic in the loop
    1469          *
    1470          * @package bbPress
    1471          * @subpackage Template Tags
    1472          * @since bbPress (r2667)
    1473          *
    1474          * @todo custom topic ststuses
    1475          *
    1476          * @uses apply_filters
    1477          * @uses get_the_title()
    1478          * @param int $topic_id optional
    1479          *
    1480          * @return string Status of topic
    1481          */
    1482         function bbp_get_topic_status ( $topic_id = 0 ) {
    1483                 $topic_id = bbp_get_topic_id( $topic_id );
    1484 
    1485                 return apply_filters( 'bbp_get_topic_status', get_post_status( $topic_id ) );
    1486         }
    1487 
    1488 /**
    1489  * bbp_topic_author ()
    1490  *
    1491  * Output the author of the topic in the loop
    1492  *
    1493  * @package bbPress
    1494  * @subpackage Template Tags
    1495  * @since bbPress (r2590)
    1496  * @param int $topic_id optional
    1497  *
    1498  * @uses bbp_get_topic_author()
    1499  */
    1500 function bbp_topic_author ( $topic_id = 0 ) {
    1501         echo bbp_get_topic_author( $topic_id );
    1502 }
    1503         /**
    1504          * bbp_get_topic_author ()
    1505          *
    1506          * Return the author of the topic in the loop
    1507          *
    1508          * @package bbPress
    1509          * @subpackage Template Tags
    1510          * @since bbPress (r2590)
    1511          *
    1512          * @uses apply_filters
    1513          * @param int $topic_id optional
    1514          *
    1515          * @return string Author of topic
    1516          */
    1517         function bbp_get_topic_author ( $topic_id = 0 ) {
    1518                 $topic_id = bbp_get_topic_id( $topic_id );
    1519 
    1520                 if ( get_post_field( 'post_author', $topic_id ) )
    1521                         $author = get_the_author();
    1522                 else
    1523                         $author = get_post_meta( $topic_id, '_bbp_anonymous_name', true );
    1524 
    1525                 return apply_filters( 'bbp_get_topic_author', $author );
    1526         }
    1527 
    1528 /**
    1529  * bbp_topic_author_id ()
    1530  *
    1531  * Output the author ID of the topic in the loop
    1532  *
    1533  * @package bbPress
    1534  * @subpackage Template Tags
    1535  * @since bbPress (r2590)
    1536  * @param int $topic_id optional
    1537  *
    1538  * @uses bbp_get_topic_author()
    1539  */
    1540 function bbp_topic_author_id ( $topic_id = 0 ) {
    1541         echo bbp_get_topic_author_id( $topic_id );
    1542 }
    1543         /**
    1544          * bbp_get_topic_author_id ()
    1545          *
    1546          * Return the author ID of the topic in the loop
    1547          *
    1548          * @package bbPress
    1549          * @subpackage Template Tags
    1550          * @since bbPress (r2590)
    1551          *
    1552          * @uses apply_filters
    1553          * @param int $topic_id optional
    1554          *
    1555          * @return string Author of topic
    1556          */
    1557         function bbp_get_topic_author_id ( $topic_id = 0 ) {
    1558                 $topic_id = bbp_get_topic_id( $topic_id );
    1559 
    1560                 return apply_filters( 'bbp_get_topic_author_id', get_post_field( 'post_author', $topic_id ) );
    1561         }
    1562 
    1563 /**
    1564  * bbp_topic_author_display_name ()
    1565  *
    1566  * Output the author display_name of the topic in the loop
    1567  *
    1568  * @package bbPress
    1569  * @subpackage Template Tags
    1570  * @since bbPress (r2590)
    1571  * @param int $topic_id optional
    1572  *
    1573  * @uses bbp_get_topic_author()
    1574  */
    1575 function bbp_topic_author_display_name ( $topic_id = 0 ) {
    1576         echo bbp_get_topic_author_display_name( $topic_id );
    1577 }
    1578         /**
    1579          * bbp_get_topic_author_display_name ()
    1580          *
    1581          * Return the author display_name of the topic in the loop
    1582          *
    1583          * @package bbPress
    1584          * @subpackage Template Tags
    1585          * @since bbPress (r2485)
    1586          *
    1587          * @uses apply_filters
    1588          * @param int $topic_id optional
    1589          *
    1590          * @return string Author of topic
    1591          */
    1592         function bbp_get_topic_author_display_name ( $topic_id = 0 ) {
    1593                 $topic_id = bbp_get_topic_id( $topic_id );
    1594 
    1595                 // Check for anonymous user
    1596                 if ( $author_id = get_post_field( 'post_author', $topic_id ) )
    1597                         $author_name = get_the_author_meta( 'display_name', $author_id );
    1598                 else
    1599                         $author_name = get_post_meta( $topic_id, '_bbp_anonymous_name', true );
    1600 
    1601                 return apply_filters( 'bbp_get_topic_author_id', esc_attr( $author_name ) );
    1602         }
    1603 
    1604 /**
    1605  * bbp_topic_author_avatar ()
    1606  *
    1607  * Output the author avatar of the topic in the loop
    1608  *
    1609  * @package bbPress
    1610  * @subpackage Template Tags
    1611  * @since bbPress (r2590)
    1612  * @param int $topic_id optional
    1613  *
    1614  * @uses bbp_get_topic_author()
    1615  */
    1616 function bbp_topic_author_avatar ( $topic_id = 0, $size = 40 ) {
    1617         echo bbp_get_topic_author_avatar( $topic_id, $size );
    1618 }
    1619         /**
    1620          * bbp_get_topic_author_avatar ()
    1621          *
    1622          * Return the author avatar of the topic in the loop
    1623          *
    1624          * @package bbPress
    1625          * @subpackage Template Tags
    1626          * @since bbPress (r2590)
    1627          *
    1628          * @uses apply_filters
    1629          * @param int $topic_id optional
    1630          *
    1631          * @return string Author of topic
    1632          */
    1633         function bbp_get_topic_author_avatar ( $topic_id = 0, $size = 40 ) {
    1634                 $topic_id = bbp_get_topic_id( $topic_id );
    1635 
    1636                 return apply_filters( 'bbp_get_topic_author_avatar', get_avatar( get_post_field( 'post_author', $topic_id ), $size ) );
    1637         }
    1638 
    1639 /**
    1640  * bbp_topic_author_avatar ()
    1641  *
    1642  * Output the author avatar of the topic in the loop
    1643  *
    1644  * @package bbPress
    1645  * @subpackage Template Tags
    1646  * @since bbPress (r2590)
    1647  * @param int $topic_id optional
    1648  *
    1649  * @uses bbp_get_topic_author()
    1650  */
    1651 function bbp_topic_author_url ( $topic_id = 0 ) {
    1652         echo bbp_get_topic_author_url( $topic_id );
    1653 }
    1654         /**
    1655          * bbp_get_topic_author_url ()
    1656          *
    1657          * Return the author url of the topic in the loop
    1658          *
    1659          * @package bbPress
    1660          * @subpackage Template Tags
    1661          * @since bbPress (r2590)
    1662          *
    1663          * @uses apply_filters
    1664          * @param int $topic_id optional
    1665          *
    1666          * @return string Author URL of topic
    1667          */
    1668         function bbp_get_topic_author_url ( $topic_id = 0 ) {
    1669                 $topic_id = bbp_get_topic_id( $topic_id );
    1670 
    1671                 // Check for anonymous user
    1672                 if ( $author_id = get_post_field( 'post_author', $topic_id ) )
    1673                         $author_url = get_author_posts_url( $author_id );
    1674                 else
    1675                         $author_url = get_post_meta( $topic_id, '_bbp_anonymous_website', true );
    1676 
    1677                 return apply_filters( 'bbp_get_topic_author_url', $author_url );
    1678         }
    1679 
    1680 /**
    1681  * bbp_topic_forum_title ()
    1682  *
    1683  * Output the title of the forum a topic belongs to
    1684  *
    1685  * @package bbPress
    1686  * @subpackage Template Tags
    1687  * @since bbPress (r2485)
    1688  *
    1689  * @param int $topic_id optional
    1690  *
    1691  * @uses bbp_get_topic_forum_title()
    1692  */
    1693 function bbp_topic_forum_title ( $topic_id = 0 ) {
    1694         echo bbp_get_topic_forum_title( $topic_id );
    1695 }
    1696         /**
    1697          * bbp_get_topic_forum_title ()
    1698          *
    1699          * Return the title of the forum a topic belongs to
    1700          *
    1701          * @package bbPress
    1702          * @subpackage Template Tags
    1703          * @since bbPress (r2485)
    1704          *
    1705          * @param int $topic_id optional
    1706          *
    1707          * @return string
    1708          */
    1709         function bbp_get_topic_forum_title ( $topic_id = 0 ) {
    1710                 $topic_id = bbp_get_topic_id( $topic_id );
    1711                 $forum_id = bbp_get_topic_forum_id( $topic_id );
    1712 
    1713                 return apply_filters( 'bbp_get_topic_forum', bbp_get_forum_title( $forum_id ) );
    1714         }
    1715 
    1716 /**
    1717  * bbp_topic_forum_id ()
    1718  *
    1719  * Output the forum ID a topic belongs to
    1720  *
    1721  * @package bbPress
    1722  * @subpackage Template Tags
    1723  * @since bbPress (r2491)
    1724  *
    1725  * @param int $topic_id optional
    1726  *
    1727  * @uses bbp_get_topic_forum_id()
    1728  */
    1729 function bbp_topic_forum_id ( $topic_id = 0 ) {
    1730         echo bbp_get_topic_forum_id( $topic_id );
    1731 }
    1732         /**
    1733          * bbp_get_topic_forum_id ()
    1734          *
    1735          * Return the forum ID a topic belongs to
    1736          *
    1737          * @package bbPress
    1738          * @subpackage Template Tags
    1739          * @since bbPress (r2491)
    1740          *
    1741          * @param int $topic_id optional
    1742          *
    1743          * @return string
    1744          */
    1745         function bbp_get_topic_forum_id ( $topic_id = 0 ) {
    1746                 $topic_id = bbp_get_topic_id( $topic_id );
    1747                 $forum_id = get_post_field( 'post_parent', $topic_id );
    1748 
    1749                 return apply_filters( 'bbp_get_topic_forum_id', $forum_id, $topic_id );
    1750         }
    1751 
    1752 /**
    1753  * bbp_topic_last_active ()
    1754  *
    1755  * Output the topics last update date/time (aka freshness)
    1756  *
    1757  * @package bbPress
    1758  * @subpackage Template Tags
    1759  * @since bbPress (r2625)
    1760  *
    1761  *
    1762  * @param int $topic_id optional
    1763  *
    1764  * @uses bbp_get_topic_last_active()
    1765  */
    1766 function bbp_topic_last_active ( $topic_id = 0 ) {
    1767         echo bbp_get_topic_last_active( $topic_id );
    1768 }
    1769         /**
    1770          * bbp_get_topic_last_active ()
    1771          *
    1772          * Return the topics last update date/time (aka freshness)
    1773          *
    1774          * @package bbPress
    1775          * @subpackage Template Tags
    1776          * @since bbPress (r2625)
    1777          *
    1778          * @param int $topic_id optional
    1779          *
    1780          * @return string
    1781          */
    1782         function bbp_get_topic_last_active ( $topic_id = 0 ) {
    1783                 $topic_id = bbp_get_topic_id( $topic_id );
    1784 
    1785                 if ( !$last_active = get_post_meta( $topic_id, '_bbp_topic_last_active', true ) )
    1786                         $reply_id = bbp_get_topic_last_reply_id( $topic_id );
    1787 
    1788                 return apply_filters( 'bbp_get_topic_last_active', bbp_get_time_since( bbp_get_modified_time( $last_active ) ) );
    1789         }
    1790 
    1791 /** TOPIC LAST REPLY **********************************************************/
    1792 
    1793 /**
    1794  * bbp_topic_last_reply_id ()
    1795  *
    1796  * Output the id of the topics last reply
    1797  *
    1798  * @package bbPress
    1799  * @subpackage Template Tags
    1800  * @since bbPress (r2625)
    1801  *
    1802  * @param int $topic_id optional
    1803  *
    1804  * @uses bbp_get_topic_last_active()
    1805  */
    1806 function bbp_topic_last_reply_id ( $topic_id = 0 ) {
    1807         echo bbp_get_topic_last_reply_id( $topic_id );
    1808 }
    1809         /**
    1810          * bbp_get_topic_last_reply_id ()
    1811          *
    1812          * Return the topics last update date/time (aka freshness)
    1813          *
    1814          * @package bbPress
    1815          * @subpackage Template Tags
    1816          * @since bbPress (r2625)
    1817          *
    1818          * @param int $topic_id optional
    1819          *
    1820          * @return string
    1821          */
    1822         function bbp_get_topic_last_reply_id ( $topic_id = 0 ) {
    1823                 $topic_id = bbp_get_topic_id( $topic_id );
    1824                 $reply_id = get_post_meta( $topic_id, '_bbp_topic_last_reply_id', true );
    1825 
    1826                 return apply_filters( 'bbp_get_topic_last_reply_id', $reply_id );
    1827         }
    1828 
    1829 /**
    1830  * bbp_topic_last_reply_title ()
    1831  *
    1832  * Output the title of the last reply inside a topic
    1833  *
    1834  * @param int $topic_id
    1835  */
    1836 function bbp_topic_last_reply_title ( $topic_id = 0 ) {
    1837         echo bbp_get_topic_last_reply_title( $topic_id );
    1838 }
    1839         /**
    1840          * bbp_get_topic_last_reply_title ()
    1841          *
    1842          * Return the title of the last reply inside a topic
    1843          *
    1844          * @param int $topic_id
    1845          * @return string
    1846          */
    1847         function bbp_get_topic_last_reply_title( $topic_id = 0 ) {
    1848                 $topic_id = bbp_get_topic_id( $topic_id );
    1849                 return apply_filters( 'bbp_get_topic_last_topic_title', bbp_get_reply_title( bbp_get_topic_last_reply_id( $topic_id ) ) );
    1850         }
    1851 
    1852 /**
    1853  * bbp_topic_last_reply_permalink ()
    1854  *
    1855  * Output the link to the last reply in a topic
    1856  *
    1857  * @package bbPress
    1858  * @subpackage Template Tags
    1859  * @since bbPress (r2464)
    1860  *
    1861  * @param int $topic_id optional
    1862  * @uses bbp_get_topic_permalink()
    1863  */
    1864 function bbp_topic_last_reply_permalink ( $topic_id = 0 ) {
    1865         echo bbp_get_topic_last_reply_permalink( $topic_id );
    1866 }
    1867         /**
    1868          * bbp_get_topic_last_reply_permalink ()
    1869          *
    1870          * Return the link to the last reply in a topic
    1871          *
    1872          * @package bbPress
    1873          * @subpackage Template Tags
    1874          * @since bbPress (r2464)
    1875          *
    1876          * @param int $topic_id optional
    1877          * @uses apply_filters
    1878          * @uses get_permalink
    1879          * @return string Permanent link to topic
    1880          */
    1881         function bbp_get_topic_last_reply_permalink ( $topic_id = 0 ) {
    1882                 $topic_id = bbp_get_topic_id( $topic_id );
    1883                 return apply_filters( 'bbp_get_topic_last_reply_permalink', bbp_get_reply_permalink( bbp_get_topic_last_reply_id( $topic_id ) ) );
    1884         }
    1885 
    1886 /**
    1887  * bbp_topic_last_reply_url ()
    1888  *
    1889  * Output the link to the last reply in a topic
    1890  *
    1891  * @package bbPress
    1892  * @subpackage Template Tags
    1893  * @since bbPress (r2683)
    1894  *
    1895  * @param int $topic_id optional
    1896  * @uses bbp_get_topic_url()
    1897  */
    1898 function bbp_topic_last_reply_url ( $topic_id = 0 ) {
    1899         echo bbp_get_topic_last_reply_url( $topic_id );
    1900 }
    1901         /**
    1902          * bbp_get_topic_last_reply_url ()
    1903          *
    1904          * Return the link to the last reply in a topic
    1905          *
    1906          * @package bbPress
    1907          * @subpackage Template Tags
    1908          * @since bbPress (r2683)
    1909          *
    1910          * @param int $topic_id optional
    1911          * @uses apply_filters
    1912          * @uses get_url
    1913          * @return string Permanent link to topic
    1914          */
    1915         function bbp_get_topic_last_reply_url ( $topic_id = 0 ) {
    1916                 $topic_id = bbp_get_topic_id( $topic_id );
    1917                 $reply_id = bbp_get_topic_last_reply_id( $topic_id );
    1918 
    1919                 if ( !empty( $reply_id ) )
    1920                         $reply_url = bbp_get_reply_url( $reply_id );
    1921                 else
    1922                         $reply_url = bbp_get_topic_permalink( $topic_id );
    1923 
    1924                 return apply_filters( 'bbp_get_topic_last_reply_url', $reply_url );
    1925         }
    1926 
    1927 /**
    1928  * bbp_topic_freshness_link ()
    1929  *
    1930  * Output link to the most recent activity inside a topic, complete with
    1931  * link attributes and content.
    1932  *
    1933  * @package bbPress
    1934  * @subpackage Template Tags
    1935  * @since bbPress (r2625)
    1936  *
    1937  * @param int $topic_id
    1938  */
    1939 function bbp_topic_freshness_link ( $topic_id = 0) {
    1940         echo bbp_get_topic_freshness_link( $topic_id );
    1941 }
    1942         /**
    1943          * bbp_get_topic_freshness_link ()
    1944          *
    1945          * Returns link to the most recent activity inside a topic, complete with
    1946          * link attributes and content.
    1947          *
    1948          * @package bbPress
    1949          * @subpackage Template Tags
    1950          * @since bbPress (r2625)
    1951          *
    1952          * @param int $topic_id
    1953          */
    1954         function bbp_get_topic_freshness_link ( $topic_id = 0 ) {
    1955                 $topic_id   = bbp_get_topic_id( $topic_id );
    1956                 $link_url   = bbp_get_topic_last_reply_url( $topic_id );
    1957                 $title      = bbp_get_topic_last_reply_title( $topic_id );
    1958                 $time_since = bbp_get_topic_last_active( $topic_id );
    1959 
    1960                 if ( !empty( $time_since ) )
    1961                         $anchor = '<a href="' . $link_url . '" title="' . esc_attr( $title ) . '">' . $time_since . '</a>';
    1962                 else
    1963                         $anchor = __( 'No Replies', 'bbpress' );
    1964 
    1965                 return apply_filters( 'bbp_get_topic_freshness_link', $anchor );
    1966         }
    1967 
    1968 /**
    1969  * bbp_topic_reply_count ()
    1970  *
    1971  * Output total post count of a topic
    1972  *
    1973  * @package bbPress
    1974  * @subpackage Template Tags
    1975  * @since bbPress (r2485)
    1976  *
    1977  * @uses bbp_get_topic_reply_count()
    1978  * @param int $topic_id
    1979  */
    1980 function bbp_topic_reply_count ( $topic_id = 0 ) {
    1981         echo bbp_get_topic_reply_count( $topic_id );
    1982 }
    1983         /**
    1984          * bbp_get_topic_reply_count ()
    1985          *
    1986          * Return total post count of a topic
    1987          *
    1988          * @package bbPress
    1989          * @subpackage Template Tags
    1990          * @since bbPress (r2485)
    1991          *
    1992          * @uses bbp_get_topic_id()
    1993          * @uses get_pages
    1994          * @uses apply_filters
    1995          *
    1996          * @param int $topic_id
    1997          */
    1998         function bbp_get_topic_reply_count ( $topic_id = 0 ) {
    1999                 $topic_id = bbp_get_topic_id( $topic_id );
    2000                 $replies  = get_post_meta( $topic_id, '_bbp_topic_reply_count', true );
    2001 
    2002                 if ( '' === $replies )
    2003                         $replies = bbp_update_topic_reply_count( $topic_id );
    2004 
    2005                 return apply_filters( 'bbp_get_topic_reply_count', (int)$replies );
    2006         }
    2007 
    2008 /**
    2009  * bbp_topic_voice_count ()
    2010  *
    2011  * Output total voice count of a topic
    2012  *
    2013  * @package bbPress
    2014  * @subpackage Template Tags
    2015  * @since bbPress (r2567)
    2016  *
    2017  * @uses bbp_get_topic_voice_count()
    2018  * @uses apply_filters
    2019  *
    2020  * @param int $topic_id
    2021  */
    2022 function bbp_topic_voice_count ( $topic_id = 0 ) {
    2023         echo bbp_get_topic_voice_count( $topic_id );
    2024 }
    2025         /**
    2026          * bbp_get_topic_voice_count ()
    2027          *
    2028          * Return total voice count of a topic
    2029          *
    2030          * @package bbPress
    2031          * @subpackage Template Tags
    2032          * @since bbPress (r2567)
    2033          *
    2034          * @uses bbp_get_topic_id()
    2035          * @uses apply_filters
    2036          *
    2037          * @param int $topic_id
    2038          *
    2039          * @return int Voice count of the topic
    2040          */
    2041         function bbp_get_topic_voice_count ( $topic_id = 0 ) {
    2042                 $topic_id = bbp_get_topic_id( $topic_id );
    2043 
    2044                 // Look for existing count, and populate if does not exist
    2045                 if ( !$voices = get_post_meta( $topic_id, '_bbp_topic_voice_count', true ) )
    2046                         $voices = bbp_update_topic_voice_count( $topic_id );
    2047 
    2048                 return apply_filters( 'bbp_get_topic_voice_count', (int)$voices, $topic_id );
    2049         }
    2050 
    2051 /**
    2052  * bbp_topic_tag_list ( $topic_id = 0, $args = '' )
    2053  *
    2054  * Output a the tags of a topic
    2055  *
    2056  * @param int $topic_id
    2057  * @param array $args
    2058  */
    2059 function bbp_topic_tag_list ( $topic_id = 0, $args = '' ) {
    2060         echo bbp_get_topic_tag_list( $topic_id, $args );
    2061 }
    2062         /**
    2063          * bbp_get_topic_tag_list ( $topic_id = 0, $args = '' )
    2064          *
    2065          * Return the tags of a topic
    2066          *
    2067          * @param int $topic_id
    2068          * @param array $args
    2069          * @return string
    2070          */
    2071         function bbp_get_topic_tag_list ( $topic_id = 0, $args = '' ) {
    2072                 global $bbp;
    2073 
    2074                 $defaults = array(
    2075                         'before' => '<p>' . __( 'Tagged:', 'bbpress' ) . '&nbsp;',
    2076                         'sep'    => ', ',
    2077                         'after'  => '</p>'
    2078                 );
    2079 
    2080                 $r = wp_parse_args( $args, $defaults );
    2081                 extract( $r );
    2082 
    2083                 $topic_id = bbp_get_topic_id( $topic_id );
    2084 
    2085                 return get_the_term_list( $topic_id, $bbp->topic_tag_id, $before, $sep, $after );
    2086         }
    2087 
    2088 /**
    2089  * bbp_topic_admin_links()
    2090  *
    2091  * Output admin links for topic
    2092  *
    2093  * @param array $args
    2094  */
    2095 function bbp_topic_admin_links( $args = '' ) {
    2096         echo bbp_get_topic_admin_links( $args );
    2097 }
    2098         /**
    2099          * bbp_get_topic_admin_links()
    2100          *
    2101          * Return admin links for topic
    2102          *
    2103          * @param array $args
    2104          * @return string
    2105          */
    2106         function bbp_get_topic_admin_links( $args = '' ) {
    2107                 if ( !current_user_can( 'edit_others_topics' ) )
    2108                         return '&nbsp';
    2109 
    2110                 $defaults = array (
    2111                         'before' => '<span class="bbp-admin-links">',
    2112                         'after'  => '</span>',
    2113                         'sep'    => ' | ',
    2114                         'links'  => array (
    2115                                 'edit'   => __( 'Edit',   'bbpress' ), // bbp_get_topic_edit_link( $args )
    2116                                 'trash'  => __( 'Trash',  'bbpress' ), // bbp_get_topic_trash_link( $args ),
    2117                                 'close'  => __( 'Close',  'bbpress' ), // bbp_get_topic_close_link( $args ),
    2118                                 'sticky' => __( 'Sticky', 'bbpress' ), // bbp_get_topic_sticky_link( $args ),
    2119                                 'move'   => __( 'Move',   'bbpress' ), // bbp_get_topic_move_dropdown( $args )
    2120                         ),
    2121                 );
    2122 
    2123                 $r = wp_parse_args( $args, $defaults );
    2124                 extract( $r );
    2125 
    2126                 // Process the admin links
    2127                 $links = implode( $sep, $links );
    2128 
    2129                 return apply_filters( 'bbp_get_topic_admin_links', $before . $links . $after, $args );
    2130         }
    2131 
    2132 /**
    2133  * bbp_topic_class ()
    2134  *
    2135  * Output the row class of a topic
    2136  *
    2137  * @package bbPress
    2138  * @subpackage Template Tags
    2139  * @since bbPress (r2667)
    2140  */
    2141 function bbp_topic_class ( $topic_id = 0 ) {
    2142         echo bbp_get_topic_class( $topic_id );
    2143 }
    2144         /**
    2145          * bbp_get_topic_class ()
    2146          *
    2147          * Return the row class of a topic
    2148          *
    2149          * @package bbPress
    2150          * @subpackage Template Tags
    2151          * @since bbPress (r2667)
    2152          *
    2153          * @global WP_Query $bbp_topics_template
    2154          * @param int $topic_id
    2155          * @return string
    2156          */
    2157         function bbp_get_topic_class ( $topic_id = 0 ) {
    2158                 global $bbp_topics_template;
    2159 
    2160                 $alternate = $bbp_topics_template->current_post % 2 ? '' : 'alternate';
    2161                 $status    = 'status-'  . bbp_get_topic_status();
    2162                 $post      = post_class( array( $alternate, $status ) );
    2163 
    2164                 return apply_filters( 'bbp_get_topic_class', $post );
    2165         }
    2166 
    2167 /** Topic Updaters ************************************************************/
    2168 
    2169 /**
    2170  * bbp_update_topic_reply_count ()
    2171  *
    2172  * Adjust the total post count of a topic
    2173  *
    2174  * @package bbPress
    2175  * @subpackage Template Tags
    2176  * @since bbPress (r2467)
    2177  *
    2178  * @uses bbp_get_topic_id()
    2179  * @uses apply_filters
    2180  *
    2181  * @param int $topic_id optional Forum ID to update
    2182  *
    2183  * @return int
    2184  */
    2185 function bbp_update_topic_reply_count ( $topic_id = 0 ) {
    2186         global $wpdb, $bbp;
    2187 
    2188         $topic_id = bbp_get_topic_id( $topic_id );
    2189 
    2190         // If it's a reply, then get the parent (topic id)
    2191         if ( $bbp->reply_id == get_post_field( 'post_type', $topic_id ) )
    2192                 $topic_id = get_post_field( 'post_parent', $topic_id );
    2193 
    2194         // Get replies of topic
    2195         $replies = count( $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->reply_id . "';", $topic_id ) ) );
    2196 
    2197         // Update the count
    2198         update_post_meta( $topic_id, '_bbp_topic_reply_count', (int)$replies );
    2199 
    2200         return apply_filters( 'bbp_update_topic_reply_count', (int)$replies );
    2201 }
    2202 
    2203 /**
    2204  * bbp_update_topic_last_active ()
    2205  *
    2206  * Update the topics last active date/time (aka freshness)
    2207  *
    2208  * @package bbPress
    2209  * @subpackage Template Tags
    2210  * @since bbPress (r2680)
    2211  *
    2212  * @param int $topic_id optional
    2213  *
    2214  * @return string
    2215  */
    2216 function bbp_update_topic_last_active ( $topic_id = 0, $new_time = '' ) {
    2217         $topic_id = bbp_get_topic_id( $topic_id );
    2218 
    2219         // Check time and use current if empty
    2220         if ( empty( $new_time ) )
    2221                 $new_time = current_time( 'mysql' );
    2222 
    2223         // Update the last reply ID
    2224         if ( !empty( $topic_id ) ) {
    2225                 update_post_meta( $topic_id, '_bbp_topic_last_active', $new_time );
    2226                 return true;
    2227         }
    2228 
    2229         return false;
    2230 }
    2231 
    2232 /**
    2233  * bbp_update_topic_last_reply_id ()
    2234  *
    2235  * Update the topic with the most recent reply ID
    2236  *
    2237  * @package bbPress
    2238  * @subpackage Template Tags
    2239  * @since bbPress (r2625)
    2240  *
    2241  * @todo everything
    2242  * @param int $topic_id
    2243  */
    2244 function bbp_update_topic_last_reply_id ( $topic_id = 0, $reply_id = 0 ) {
    2245         $topic_id = bbp_get_topic_id( $topic_id );
    2246         $reply_id = bbp_get_reply_id( $reply_id );
    2247 
    2248         // Update the last reply ID
    2249         if ( !empty( $topic_id ) ) {
    2250                 update_post_meta( $topic_id, '_bbp_topic_last_reply_id', $reply_id );
    2251                 return true;
    2252         }
    2253 
    2254         return false;
    2255 }
    2256 
    2257 /**
    2258  * bbp_update_topic_voice_count ()
    2259  *
    2260  * Adjust the total voice count of a topic
    2261  *
    2262  * @package bbPress
    2263  * @subpackage Template Tags
    2264  * @since bbPress (r2567)
    2265  *
    2266  * @uses bbp_get_topic_id()
    2267  * @uses apply_filters
    2268  *
    2269  * @todo cache
    2270  *
    2271  * @param int $topic_id optional Topic ID to update
    2272  * @return bool false on failure, voice count on success
    2273  */
    2274 function bbp_update_topic_voice_count ( $topic_id = 0 ) {
    2275         global $wpdb, $bbp;
    2276 
    2277         $topic_id = bbp_get_topic_id( $topic_id );
    2278 
    2279         // If it is not a topic or reply, then we don't need it
    2280         if ( !in_array( get_post_field( 'post_type', $topic_id ), array( $bbp->topic_id, $bbp->reply_id ) ) )
    2281                 return false;
    2282 
    2283         // If it's a reply, then get the parent (topic id)
    2284         if ( $bbp->reply_id == get_post_field( 'post_type', $topic_id ) )
    2285                 $topic_id = get_post_field( 'post_parent', $topic_id );
    2286 
    2287         // There should always be at least 1 voice
    2288         if ( !$voices = count( $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_author FROM $wpdb->posts WHERE ( post_parent = %d AND post_status = 'publish' AND post_type = '" . $bbp->reply_id . "' ) OR ( ID = %d AND post_type = '" . $bbp->topic_id . "' );", $topic_id, $topic_id ) ) ) )
    2289                 $voices = 1;
    2290 
    2291         // Update the count
    2292         update_post_meta( $topic_id, '_bbp_topic_voice_count', (int)$voices );
    2293 
    2294         return apply_filters( 'bbp_update_topic_voice_count', (int)$voices );
    2295 }
    2296 
    2297 /** Topic Pagination **********************************************************/
    2298 
    2299 /**
    2300  * bbp_forum_pagination_count ()
    2301  *
    2302  * Output the pagination count
    2303  *
    2304  * @package bbPress
    2305  * @subpackage Template Tags
    2306  * @since bbPress (r2519)
    2307  *
    2308  * @global WP_Query $bbp_topics_template
    2309  */
    2310 function bbp_forum_pagination_count () {
    2311         echo bbp_get_forum_pagination_count();
    2312 }
    2313         /**
    2314          * bbp_get_forum_pagination_count ()
    2315          *
    2316          * Return the pagination count
    2317          *
    2318          * @package bbPress
    2319          * @subpackage Template Tags
    2320          * @since bbPress (r2519)
    2321          *
    2322          * @global WP_Query $bbp_topics_template
    2323          * @return string
    2324          */
    2325         function bbp_get_forum_pagination_count () {
    2326                 global $bbp_topics_template;
    2327 
    2328                 if ( !isset( $bbp_topics_template ) )
    2329                         return false;
    2330 
    2331                 // Set pagination values
    2332                 $start_num = intval( ( $bbp_topics_template->paged - 1 ) * $bbp_topics_template->posts_per_page ) + 1;
    2333                 $from_num  = bbp_number_format( $start_num );
    2334                 $to_num    = bbp_number_format( ( $start_num + ( $bbp_topics_template->posts_per_page - 1 ) > $bbp_topics_template->found_posts ) ? $bbp_topics_template->found_posts : $start_num + ( $bbp_topics_template->posts_per_page - 1 ) );
    2335                 $total     = bbp_number_format( !empty( $bbp_topics_template->found_posts ) ? $bbp_topics_template->found_posts : $bbp_topics_template->post_count );
    2336 
    2337                 // Set return string
    2338                 if ( $total > 1 && (int)$from_num == (int)$to_num )
    2339                         $retstr = sprintf( __( 'Viewing topic %1$s (of %2$s total)', 'bbpress' ), $from_num, $total );
    2340                 elseif ( $total > 1 && empty( $to_num ) )
    2341                         $retstr = sprintf( __( 'Viewing %1$s topics', 'bbpress' ), $total );
    2342                 elseif ( $total > 1 && (int)$from_num != (int)$to_num )
    2343                         $retstr = sprintf( __( 'Viewing topics %1$s through %2$s (of %3$s total)', 'bbpress' ), $from_num, $to_num, $total );
    2344                 else
    2345                         $retstr = sprintf( __( 'Viewing %1$s topic', 'bbpress' ), $total );
    2346 
    2347                 // Filter and return
    2348                 return apply_filters( 'bbp_get_topic_pagination_count', $retstr );
    2349         }
    2350 
    2351 /**
    2352  * bbp_forum_pagination_links ()
    2353  *
    2354  * Output pagination links
    2355  *
    2356  * @package bbPress
    2357  * @subpackage Template Tags
    2358  * @since bbPress (r2519)
    2359  */
    2360 function bbp_forum_pagination_links () {
    2361         echo bbp_get_forum_pagination_links();
    2362 }
    2363         /**
    2364          * bbp_get_forum_pagination_links ()
    2365          *
    2366          * Return pagination links
    2367          *
    2368          * @package bbPress
    2369          * @subpackage Template Tags
    2370          * @since bbPress (r2519)
    2371          *
    2372          * @global WP_Query $bbp_topics_template
    2373          * @return string
    2374          */
    2375         function bbp_get_forum_pagination_links () {
    2376                 global $bbp_topics_template;
    2377 
    2378                 if ( !isset( $bbp_topics_template ) )
    2379                         return false;
    2380 
    2381                 return apply_filters( 'bbp_get_topic_pagination_links', $bbp_topics_template->pagination_links );
    2382         }
    2383 
    2384 /** END - Topic Loop Functions ************************************************/
    2385 
    2386 /** START - Reply Loop Functions **********************************************/
    2387 
    2388 /**
    2389  * bbp_has_replies ( $args )
    2390  *
    2391  * The main reply loop. WordPress makes this easy for us
    2392  *
    2393  * @package bbPress
    2394  * @subpackage Template Tags
    2395  * @since bbPress (r2553)
    2396  *
    2397  * @global WP_Query $bbp_replies_template
    2398  * @param array $args Possible arguments to change returned replies
    2399  * @return object Multidimensional array of reply information
    2400  */
    2401 function bbp_has_replies ( $args = '' ) {
    2402         global $wp_rewrite, $bbp_replies_template, $bbp;
    2403 
    2404         $default = array(
    2405                 // Narrow query down to bbPress topics
    2406                 'post_type'        => $bbp->reply_id,
    2407 
    2408                 // Forum ID
    2409                 'post_parent'      => isset( $_REQUEST['topic_id'] ) ? $_REQUEST['topic_id'] : bbp_get_topic_id(),
    2410 
    2411                 //'author', 'date', 'title', 'modified', 'parent', rand',
    2412                 'orderby'          => isset( $_REQUEST['orderby'] ) ? $_REQUEST['orderby'] : 'date',
    2413 
    2414                 // 'ASC', 'DESC'
    2415                 'order'            => isset( $_REQUEST['order'] ) ? $_REQUEST['order'] : 'ASC',
    2416 
    2417                 // @todo replace 15 with setting
    2418                 'posts_per_page'   => isset( $_REQUEST['posts'] ) ? $_REQUEST['posts'] : 15,
    2419 
    2420                 // Page Number
    2421                 'paged'            => bbp_get_paged(),
    2422 
    2423                 // Reply Search
    2424                 's'                => empty( $_REQUEST['rs'] ) ? '' : $_REQUEST['rs'],
    2425         );
    2426 
    2427         // Set up topic variables
    2428         $bbp_r = wp_parse_args( $args, $default );
    2429         $r     = extract( $bbp_r );
    2430 
    2431         // Call the query
    2432         $bbp_replies_template = new WP_Query( $bbp_r );
    2433 
    2434         // Add pagination values to query object
    2435         $bbp_replies_template->posts_per_page = $posts_per_page;
    2436         $bbp_replies_template->paged = $paged;
    2437 
    2438         // Only add pagination if query returned results
    2439         if ( (int)$bbp_replies_template->found_posts && (int)$bbp_replies_template->posts_per_page ) {
    2440 
    2441                 // If pretty permalinks are enabled, make our pagination pretty
    2442                 if ( $wp_rewrite->using_permalinks() )
    2443                         $base = user_trailingslashit( trailingslashit( get_permalink( $post_parent ) ) . 'page/%#%/' );
    2444                 else
    2445                         $base = add_query_arg( 'page', '%#%' );
    2446 
    2447                 // Pagination settings with filter
    2448                 $bbp_replies_pagination = apply_filters( 'bbp_replies_pagination', array(
    2449                         'base'      => $base,
    2450                         'format'    => '',
    2451                         'total'     => ceil( (int)$bbp_replies_template->found_posts / (int)$posts_per_page ),
    2452                         'current'   => (int)$bbp_replies_template->paged,
    2453                         'prev_text' => '&larr;',
    2454                         'next_text' => '&rarr;',
    2455                         'mid_size'  => 1
    2456                 ) );
    2457 
    2458                 // Add pagination to query object
    2459                 $bbp_replies_template->pagination_links = paginate_links( $bbp_replies_pagination );
    2460 
    2461                 // Remove first page from pagination
    2462                 $bbp_replies_template->pagination_links = str_replace( 'page/1/\'', '\'', $bbp_replies_template->pagination_links );
    2463         }
    2464 
    2465         // Return object
    2466         return apply_filters( 'bbp_has_replies', $bbp_replies_template->have_posts(), $bbp_replies_template );
    2467 }
    2468 
    2469 /**
    2470  * bbp_replies ()
    2471  *
    2472  * Whether there are more replies available in the loop
    2473  *
    2474  * @package bbPress
    2475  * @subpackage Template Tags
    2476  * @since bbPress (r2553)
    2477  *
    2478  * @global WP_Query $bbp_replies_template
    2479  * @return object Replies information
    2480  */
    2481 function bbp_replies () {
    2482         global $bbp_replies_template;
    2483         return $bbp_replies_template->have_posts();
    2484 }
    2485 
    2486 /**
    2487  * bbp_the_reply ()
    2488  *
    2489  * Loads up the current reply in the loop
    2490  *
    2491  * @package bbPress
    2492  * @subpackage Template Tags
    2493  * @since bbPress (r2553)
    2494  *
    2495  * @global WP_Query $bbp_replies_template
    2496  * @return object Reply information
    2497  */
    2498 function bbp_the_reply () {
    2499         global $bbp_replies_template;
    2500         return $bbp_replies_template->the_post();
    2501 }
    2502 
    2503 /**
    2504  * bbp_reply_id ()
    2505  *
    2506  * Output id from bbp_get_reply_id()
    2507  *
    2508  * @package bbPress
    2509  * @subpackage Template Tags
    2510  * @since bbPress (r2553)
    2511  *
    2512  * @uses bbp_get_reply_id()
    2513  */
    2514 function bbp_reply_id () {
    2515         echo bbp_get_reply_id();
    2516 }
    2517         /**
    2518          * bbp_get_reply_id ()
    2519          *
    2520          * Return the id of the reply in a replies loop
    2521          *
    2522          * @package bbPress
    2523          * @subpackage Template Tags
    2524          * @since bbPress (r2553)
    2525          *
    2526          * @global object $bbp_replies_template
    2527          * @return int Reply id
    2528          */
    2529         function bbp_get_reply_id ( $reply_id = 0 ) {
    2530                 global $bbp_replies_template, $wp_query, $bbp;
    2531 
    2532                 // Easy empty checking
    2533                 if ( !empty( $reply_id ) && is_numeric( $reply_id ) )
    2534                         $bbp_reply_id = $reply_id;
    2535 
    2536                 // Currently viewing a reply
    2537                 elseif ( bbp_is_reply() && isset( $wp_query->post->ID ) )
    2538                         $bbp_reply_id = $wp_query->post->ID;
    2539 
    2540                 // Currently inside a replies loop
    2541                 elseif ( isset( $bbp_replies_template->post->ID ) )
    2542                         $bbp_reply_id = $bbp_replies_template->post->ID;
    2543 
    2544                 // Fallback
    2545                 else
    2546                         $bbp_reply_id = 0;
    2547 
    2548                 $bbp->current_reply_id = $bbp_reply_id;
    2549 
    2550                 return apply_filters( 'bbp_get_reply_id', (int)$bbp_reply_id );
    2551         }
    2552 
    2553 /**
    2554  * bbp_reply_permalink ()
    2555  *
    2556  * Output the link to the reply in the reply loop
    2557  *
    2558  * @package bbPress
    2559  * @subpackage Template Tags
    2560  * @since bbPress (r2553)
    2561  *
    2562  * @uses bbp_get_reply_permalink()
    2563  * @param int $reply_id optional
    2564  */
    2565 function bbp_reply_permalink ( $reply_id = 0 ) {
    2566         echo bbp_get_reply_permalink( $reply_id );
    2567 }
    2568         /**
    2569          * bbp_get_reply_permalink()
    2570          *
    2571          * Return the link to the reply in the loop
    2572          *
    2573          * @package bbPress
    2574          * @subpackage Template Tags
    2575          * @since bbPress (r2553)
    2576          *
    2577          * @uses apply_filters
    2578          * @uses get_permalink
    2579          * @param int $reply_id optional
    2580          *
    2581          * @return string Permanent link to reply
    2582          */
    2583         function bbp_get_reply_permalink ( $reply_id = 0 ) {
    2584                 $reply_id = bbp_get_reply_id( $reply_id );
    2585 
    2586                 return apply_filters( 'bbp_get_reply_permalink', get_permalink( $reply_id ), $reply_id );
    2587         }
    2588 /**
    2589  * bbp_reply_url ()
    2590  *
    2591  * Output the paginated url to the reply in the reply loop
    2592  *
    2593  * @package bbPress
    2594  * @subpackage Template Tags
    2595  * @since bbPress (r2679)
    2596  *
    2597  * @uses bbp_get_reply_url()
    2598  * @param int $reply_id optional
    2599  */
    2600 function bbp_reply_url ( $reply_id = 0 ) {
    2601         echo bbp_get_reply_url( $reply_id );
    2602 }
    2603         /**
    2604          * bbp_get_reply_url()
    2605          *
    2606          * Return the paginated url to the reply in the reply loop
    2607          *
    2608          * @package bbPress
    2609          * @subpackage Template Tags
    2610          * @since bbPress (r2679)
    2611          *
    2612          * @uses apply_filters
    2613          * @uses bbp_get_reply_id
    2614          * @uses bbp_get_reply_topic_id
    2615          * @uses bbp_get_topic_permalink
    2616          * @param int $reply_id optional
    2617          *
    2618          * @return string Link to reply relative to paginated topic
    2619          */
    2620         function bbp_get_reply_url ( $reply_id = 0 ) {
    2621                 global $bbp_replies_template;
    2622 
    2623                 // Set needed variables
    2624                 $reply_id  = bbp_get_reply_id( $reply_id );
    2625                 $topic_id  = bbp_get_reply_topic_id( $reply_id );
    2626                 $topic_url = bbp_get_topic_permalink( $topic_id );
    2627 
    2628                 // If $bbp_replies_template isn't set, we're handling a new reply redirect
    2629                 if ( !isset( $bbp_replies_template ) ) {
    2630 
    2631                         // Populate the replies global
    2632                         bbp_has_replies();
    2633 
    2634                         // Do some math and see if we need to bounce to the newest page
    2635                         $new_reply_location = ceil( $bbp_replies_template->found_posts / $bbp_replies_template->posts_per_page );
    2636                         if ( $bbp_replies_template->paged < (int) $new_reply_location )
    2637                                 $reply_page = $new_reply_location;
    2638                         else
    2639                                 $reply_page = $bbp_replies_template->paged;
    2640 
    2641                 // All good, so assign the reply_page to the current page
    2642                 } else {
    2643                         $reply_page = $bbp_replies_template->paged;
    2644                 }
    2645 
    2646                 // Don't include pagination if on first page
    2647                 if ( 1 >= $reply_page )
    2648                         $url = untrailingslashit( $topic_url ) . "/#reply-{$reply_id}";
    2649                 else
    2650                         $url = trailingslashit( $topic_url ) . "page/{$reply_page}/#reply-{$reply_id}";
    2651 
    2652                 return apply_filters( 'bbp_get_reply_url', $url, $reply_id );
    2653         }
    2654 
    2655 /**
    2656  * bbp_reply_title ()
    2657  *
    2658  * Output the title of the reply in the loop
    2659  *
    2660  * @package bbPress
    2661  * @subpackage Template Tags
    2662  * @since bbPress (r2553)
    2663  * @param int $reply_id optional
    2664  *
    2665  * @uses bbp_get_reply_title()
    2666  */
    2667 function bbp_reply_title ( $reply_id = 0 ) {
    2668         echo bbp_get_reply_title( $reply_id );
    2669 }
    2670 
    2671         /**
    2672          * bbp_get_reply_title ()
    2673          *
    2674          * Return the title of the reply in the loop
    2675          *
    2676          * @package bbPress
    2677          * @subpackage Template Tags
    2678          * @since bbPress (r2553)
    2679          *
    2680          * @uses apply_filters
    2681          * @uses get_the_title()
    2682          * @param int $reply_id optional
    2683          *
    2684          * @return string Title of reply
    2685          */
    2686         function bbp_get_reply_title ( $reply_id = 0 ) {
    2687                 return apply_filters( 'bbp_get_reply_title', get_the_title( $reply_id ), $reply_id );
    2688         }
    2689 
    2690 /**
    2691  * bbp_reply_content ()
    2692  *
    2693  * Output the content of the reply in the loop
    2694  *
    2695  * @package bbPress
    2696  * @subpackage Template Tags
    2697  * @since bbPress (r2553)
    2698  *
    2699  * @todo Have a parameter reply_id
    2700  *
    2701  * @uses bbp_get_reply_content()
    2702  */
    2703 function bbp_reply_content () {
    2704         echo bbp_get_reply_content();
    2705 }
    2706         /**
    2707          * bbp_get_reply_content ()
    2708          *
    2709          * Return the content of the reply in the loop
    2710          *
    2711          * @package bbPress
    2712          * @subpackage Template Tags
    2713          * @since bbPress (r2553)
    2714          *
    2715          * @uses apply_filters
    2716          * @uses get_the_content()
    2717          *
    2718          * @return string Content of the reply
    2719          */
    2720         function bbp_get_reply_content () {
    2721                 return apply_filters( 'bbp_get_reply_content', get_the_content() );
    2722         }
    2723 
    2724 /**
    2725  * bbp_reply_status ()
    2726  *
    2727  * Output the status of the reply in the loop
    2728  *
    2729  * @package bbPress
    2730  * @subpackage Template Tags
    2731  * @since bbPress (r2667)
    2732  * @param int $reply_id optional
    2733  *
    2734  * @uses bbp_get_reply_status()
    2735  */
    2736 function bbp_reply_status ( $reply_id = 0 ) {
    2737         echo bbp_get_reply_status( $reply_id );
    2738 }
    2739         /**
    2740          * bbp_get_reply_status ()
    2741          *
    2742          * Return the status of the reply in the loop
    2743          *
    2744          * @package bbPress
    2745          * @subpackage Template Tags
    2746          * @since bbPress (r2667)
    2747          *
    2748          * @todo custom topic ststuses
    2749          *
    2750          * @uses apply_filters
    2751          * @uses get_post_status()
    2752          * @param int $reply_id optional
    2753          *
    2754          * @return string Status of reply
    2755          */
    2756         function bbp_get_reply_status ( $reply_id = 0 ) {
    2757                 $reply_id = bbp_get_reply_id( $reply_id );
    2758 
    2759                 return apply_filters( 'bbp_get_reply_status', get_post_status( $reply_id ) );
    2760         }
    2761 
    2762 /**
    2763  * bbp_reply_author ()
    2764  *
    2765  * Output the author of the reply in the loop
    2766  *
    2767  * @package bbPress
    2768  * @subpackage Template Tags
    2769  * @since bbPress (r2667)
    2770  * @param int $reply_id optional
    2771  *
    2772  * @uses bbp_get_reply_author()
    2773  */
    2774 function bbp_reply_author ( $reply_id = 0 ) {
    2775         echo bbp_get_reply_author( $reply_id );
    2776 }
    2777         /**
    2778          * bbp_get_reply_author ()
    2779          *
    2780          * Return the author of the reply in the loop
    2781          *
    2782          * @package bbPress
    2783          * @subpackage Template Tags
    2784          * @since bbPress (r2667)
    2785          *
    2786          * @uses apply_filters
    2787          * @param int $reply_id optional
    2788          *
    2789          * @return string Author of reply
    2790          */
    2791         function bbp_get_reply_author ( $reply_id = 0 ) {
    2792                 $reply_id = bbp_get_reply_id( $reply_id );
    2793 
    2794                 if ( get_post_field( 'post_author', $reply_id ) )
    2795                         $author = get_the_author();
    2796                 else
    2797                         $author = get_post_meta( $topic_id, '_bbp_anonymous_name', true );
    2798 
    2799                 return apply_filters( 'bbp_get_reply_author', $author );
    2800         }
    2801 
    2802 /**
    2803  * bbp_reply_author_id ()
    2804  *
    2805  * Output the author ID of the reply in the loop
    2806  *
    2807  * @package bbPress
    2808  * @subpackage Template Tags
    2809  * @since bbPress (r2667)
    2810  * @param int $reply_id optional
    2811  *
    2812  * @uses bbp_get_reply_author()
    2813  */
    2814 function bbp_reply_author_id ( $reply_id = 0 ) {
    2815         echo bbp_get_reply_author_id( $reply_id );
    2816 }
    2817         /**
    2818          * bbp_get_reply_author_id ()
    2819          *
    2820          * Return the author ID of the reply in the loop
    2821          *
    2822          * @package bbPress
    2823          * @subpackage Template Tags
    2824          * @since bbPress (r2667)
    2825          *
    2826          * @uses apply_filters
    2827          * @param int $reply_id optional
    2828          *
    2829          * @return string Author of reply
    2830          */
    2831         function bbp_get_reply_author_id ( $reply_id = 0 ) {
    2832                 $reply_id = bbp_get_reply_id( $reply_id );
    2833 
    2834                 return apply_filters( 'bbp_get_reply_author_id', get_post_field( 'post_author', $reply_id ) );
    2835         }
    2836 
    2837 /**
    2838  * bbp_reply_author_display_name ()
    2839  *
    2840  * Output the author display_name of the reply in the loop
    2841  *
    2842  * @package bbPress
    2843  * @subpackage Template Tags
    2844  * @since bbPress (r2667)
    2845  * @param int $reply_id optional
    2846  *
    2847  * @uses bbp_get_reply_author()
    2848  */
    2849 function bbp_reply_author_display_name ( $reply_id = 0 ) {
    2850         echo bbp_get_reply_author_display_name( $reply_id );
    2851 }
    2852         /**
    2853          * bbp_get_reply_author_display_name ()
    2854          *
    2855          * Return the author display_name of the reply in the loop
    2856          *
    2857          * @package bbPress
    2858          * @subpackage Template Tags
    2859          * @since bbPress (r2667)
    2860          *
    2861          * @uses apply_filters
    2862          * @param int $reply_id optional
    2863          *
    2864          * @return string Author of reply
    2865          */
    2866         function bbp_get_reply_author_display_name ( $reply_id = 0 ) {
    2867                 $reply_id = bbp_get_reply_id( $reply_id );
    2868 
    2869                 // Check for anonymous user
    2870                 if ( $author_id = get_post_field( 'post_author', $reply_id ) )
    2871                         $author_name = get_the_author_meta( 'display_name', $author_id );
    2872                 else
    2873                         $author_name = get_post_meta( $reply_id, '_bbp_anonymous_name', true );
    2874 
    2875                 return apply_filters( 'bbp_get_reply_author_id', esc_attr( $author_name ) );
    2876         }
    2877 
    2878 /**
    2879  * bbp_reply_author_avatar ()
    2880  *
    2881  * Output the author avatar of the reply in the loop
    2882  *
    2883  * @package bbPress
    2884  * @subpackage Template Tags
    2885  * @since bbPress (r2667)
    2886  * @param int $reply_id optional
    2887  *
    2888  * @uses bbp_get_reply_author()
    2889  */
    2890 function bbp_reply_author_avatar ( $reply_id = 0, $size = 40 ) {
    2891         echo bbp_get_reply_author_avatar( $reply_id, $size );
    2892 }
    2893         /**
    2894          * bbp_get_reply_author_avatar ()
    2895          *
    2896          * Return the author avatar of the reply in the loop
    2897          *
    2898          * @package bbPress
    2899          * @subpackage Template Tags
    2900          * @since bbPress (r2667)
    2901          *
    2902          * @uses apply_filters
    2903          * @param int $reply_id optional
    2904          *
    2905          * @return string Author of reply
    2906          */
    2907         function bbp_get_reply_author_avatar ( $reply_id = 0, $size = 40 ) {
    2908                 $reply_id = bbp_get_reply_id( $reply_id );
    2909 
    2910                 return apply_filters( 'bbp_get_reply_author_avatar', get_avatar( get_post_field( 'post_author', $reply_id ), $size ) );
    2911         }
    2912 
    2913 /**
    2914  * bbp_reply_author_url ()
    2915  *
    2916  * Output the author url of the reply in the loop
    2917  *
    2918  * @package bbPress
    2919  * @subpackage Template Tags
    2920  * @since bbPress (r2667)
    2921  * @param int $reply_id optional
    2922  *
    2923  * @uses bbp_get_reply_author_url()
    2924  */
    2925 function bbp_reply_author_url ( $reply_id = 0 ) {
    2926         echo bbp_get_reply_author_url( $reply_id );
    2927 }
    2928         /**
    2929          * bbp_get_reply_author_url ()
    2930          *
    2931          * Return the author url of the reply in the loop
    2932          *
    2933          * @package bbPress
    2934          * @subpackage Template Tags
    2935          * @since bbPress (r2667)
    2936          *
    2937          * @uses apply_filters
    2938          * @param int $reply_id optional
    2939          *
    2940          * @return string Author URL of reply
    2941          */
    2942         function bbp_get_reply_author_url ( $reply_id = 0 ) {
    2943                 $reply_id = bbp_get_reply_id( $reply_id );
    2944 
    2945                 // Check for anonymous user
    2946                 if ( $author_id = get_post_field( 'post_author', $reply_id ) )
    2947                         $author_url = get_author_posts_url( $author_id );
    2948                 else
    2949                         $author_url = get_post_meta( $reply_id, '_bbp_anonymous_website', true );
    2950 
    2951                 return apply_filters( 'bbp_get_reply_author_url', $author_url );
    2952         }
    2953 
    2954 /**
    2955  * bbp_reply_topic_title ()
    2956  *
    2957  * Output the topic title a reply belongs to
    2958  *
    2959  * @package bbPress
    2960  * @subpackage Template Tags
    2961  * @since bbPress (r2553)
    2962  *
    2963  * @param int $reply_id optional
    2964  *
    2965  * @uses bbp_get_reply_topic()
    2966  */
    2967 function bbp_reply_topic_title ( $reply_id = 0 ) {
    2968         echo bbp_get_reply_topic_title( $reply_id );
    2969 }
    2970         /**
    2971          * bbp_get_reply_topic_title ()
    2972          *
    2973          * Return the topic title a reply belongs to
    2974          *
    2975          * @package bbPress
    2976          * @subpackage Template Tags
    2977          * @since bbPress (r2553)
    2978          *
    2979          * @param int $reply_id optional
    2980          *
    2981          * @uses bbp_get_reply_topic_id ()
    2982          * @uses bbp_topic_title ()
    2983          *
    2984          * @return string
    2985          */
    2986         function bbp_get_reply_topic_title ( $reply_id = 0 ) {
    2987                 $reply_id = bbp_get_reply_id( $reply_id );
    2988                 $topic_id = bbp_get_reply_topic_id( $reply_id );
    2989 
    2990                 return apply_filters( 'bbp_get_reply_topic_title', bbp_get_topic_title( $topic_id ), $reply_id, $topic_id );
    2991         }
    2992 
    2993 /**
    2994  * bbp_reply_topic_id ()
    2995  *
    2996  * Output the topic ID a reply belongs to
    2997  *
    2998  * @package bbPress
    2999  * @subpackage Template Tags
    3000  * @since bbPress (r2553)
    3001  *
    3002  * @param int $reply_id optional
    3003  *
    3004  * @uses bbp_get_reply_topic_id ()
    3005  */
    3006 function bbp_reply_topic_id ( $reply_id = 0 ) {
    3007         echo bbp_get_reply_topic_id( $reply_id );
    3008 }
    3009         /**
    3010          * bbp_get_reply_topic_id ()
    3011          *
    3012          * Return the topic ID a reply belongs to
    3013          *
    3014          * @package bbPress
    3015          * @subpackage Template Tags
    3016          * @since bbPress (r2553)
    3017          *
    3018          * @param int $reply_id optional
    3019          *
    3020          * @todo - Walk ancestors and look for topic post_type (for threaded replies)
    3021          *
    3022          * @return string
    3023          */
    3024         function bbp_get_reply_topic_id ( $reply_id = 0 ) {
    3025                 $reply_id = bbp_get_reply_id( $reply_id );
    3026                 $topic_id = get_post_field( 'post_parent', $reply_id );
    3027 
    3028                 return apply_filters( 'bbp_get_reply_topic_id', $topic_id, $reply_id );
    3029         }
    3030 
    3031 /**
    3032  * bbp_reply_forum_id ()
    3033  *
    3034  * Output the forum ID a reply belongs to
    3035  *
    3036  * @package bbPress
    3037  * @subpackage Template Tags
    3038  * @since bbPress (r2679)
    3039  *
    3040  * @param int $reply_id optional
    3041  *
    3042  * @uses bbp_get_reply_topic_id ()
    3043  */
    3044 function bbp_reply_forum_id ( $reply_id = 0 ) {
    3045         echo bbp_get_reply_forum_id( $reply_id );
    3046 }
    3047         /**
    3048          * bbp_get_reply_forum_id ()
    3049          *
    3050          * Return the forum ID a reply belongs to
    3051          *
    3052          * @package bbPress
    3053          * @subpackage Template Tags
    3054          * @since bbPress (r2679)
    3055          *
    3056          * @param int $reply_id optional
    3057          *
    3058          * @todo - Walk ancestors and look for forum post_type
    3059          *
    3060          * @return string
    3061          */
    3062         function bbp_get_reply_forum_id ( $reply_id = 0 ) {
    3063                 $reply_id = bbp_get_forum_id( $reply_id );
    3064                 $topic_id = get_post_field( 'post_parent', $reply_id );
    3065                 $forum_id = get_post_field( 'post_parent', $topic_id );
    3066 
    3067                 return apply_filters( 'bbp_get_reply_topic_id', $forum_id, $reply_id );
    3068         }
    3069 
    3070 /**
    3071  * bbp_reply_admin_links()
    3072  *
    3073  * Output admin links for reply
    3074  *
    3075  * @param array $args
    3076  */
    3077 function bbp_reply_admin_links( $args = '' ) {
    3078         echo bbp_get_reply_admin_links( $args );
    3079 }
    3080         /**
    3081          * bbp_get_reply_admin_links()
    3082          *
    3083          * Return admin links for reply
    3084          *
    3085          * @param array $args
    3086          * @return string
    3087          */
    3088         function bbp_get_reply_admin_links( $args = '' ) {
    3089                 if ( !current_user_can( 'edit_others_replies' ) )
    3090                         return;
    3091 
    3092                 $defaults = array (
    3093                         'before' => '<span class="bbp-admin-links">',
    3094                         'after'  => '</span>',
    3095                         'sep'    => ' | ',
    3096                         'links'  => array (
    3097                                 'edit'  => __( 'Edit', 'bbpress' ),  // bbp_get_reply_close_link( $args ),
    3098                                 'trash' => __( 'Trash', 'bbpress' ), // bbp_get_reply_delete_link( $args ),
    3099                         ),
    3100                 );
    3101 
    3102                 $r = wp_parse_args( $args, $defaults );
    3103                 extract( $r );
    3104 
    3105                 // Process the admin links
    3106                 $links = implode( $sep, $links );
    3107 
    3108                 return apply_filters( 'bbp_get_reply_admin_links', $before . $links . $after, $args );
    3109         }
    3110 
    3111 /**
    3112  * bbp_reply_class ()
    3113  *
    3114  * Output the row class of a reply
    3115  */
    3116 function bbp_reply_class ( $reply_id = 0 ) {
    3117         echo bbp_get_reply_class( $reply_id );
    3118 }
    3119         /**
    3120          * bbp_get_reply_class ()
    3121          *
    3122          * Return the row class of a reply
    3123          *
    3124          * @global WP_Query $bbp_replies_template
    3125          * @param int $reply_id
    3126          * @return string
    3127          */
    3128         function bbp_get_reply_class ( $reply_id = 0 ) {
    3129                 global $bbp_replies_template;
    3130 
    3131                 $alternate = $bbp_replies_template->current_post % 2 ? '' : 'alternate';
    3132                 $status    = 'status-'  . bbp_get_reply_status();
    3133                 $post      = post_class( array( $alternate, $status ) );
    3134 
    3135                 return apply_filters( 'bbp_reply_class', $post );
    3136         }
    3137 
    3138 /**
    3139  * bbp_topic_pagination_count ()
    3140  *
    3141  * Output the pagination count
    3142  *
    3143  * @package bbPress
    3144  * @subpackage Template Tags
    3145  * @since bbPress (r2519)
    3146  *
    3147  * @global WP_Query $bbp_topics_template
    3148  */
    3149 function bbp_topic_pagination_count () {
    3150         echo bbp_get_topic_pagination_count();
    3151 }
    3152         /**
    3153          * bbp_get_topic_pagination_count ()
    3154          *
    3155          * Return the pagination count
    3156          *
    3157          * @package bbPress
    3158          * @subpackage Template Tags
    3159          * @since bbPress (r2519)
    3160          *
    3161          * @global WP_Query $bbp_replies_template
    3162          * @return string
    3163          */
    3164         function bbp_get_topic_pagination_count () {
    3165                 global $bbp_replies_template;
    3166 
    3167                 // Set pagination values
    3168                 $start_num = intval( ( $bbp_replies_template->paged - 1 ) * $bbp_replies_template->posts_per_page ) + 1;
    3169                 $from_num  = bbp_number_format( $start_num );
    3170                 $to_num    = bbp_number_format( ( $start_num + ( $bbp_replies_template->posts_per_page - 1 ) > $bbp_replies_template->found_posts ) ? $bbp_replies_template->found_posts : $start_num + ( $bbp_replies_template->posts_per_page - 1 ) );
    3171                 $total     = bbp_number_format( $bbp_replies_template->found_posts );
    3172 
    3173                 // Set return string
    3174                 if ( $total > 1 && $from_num != $to_num )
    3175                         $retstr = sprintf( __( 'Viewing replies %1$s through %2$s (of %3$s total)', 'bbpress' ), $from_num, $to_num, $total );
    3176                 elseif ( $total > 1 && $from_num == $to_num )
    3177                         $retstr = sprintf( __( 'Viewing reply %1$s (of %2$s total)', 'bbpress' ), $from_num, $total );
    3178                 else
    3179                         $retstr = sprintf( __( 'Viewing %1$s reply', 'bbpress' ), $total );
    3180 
    3181                 // Filter and return
    3182                 return apply_filters( 'bbp_get_topic_pagination_count', $retstr );
    3183         }
    3184 
    3185 /**
    3186  * bbp_topic_pagination_links ()
    3187  *
    3188  * Output pagination links
    3189  *
    3190  * @package bbPress
    3191  * @subpackage Template Tags
    3192  * @since bbPress (r2519)
    3193  */
    3194 function bbp_topic_pagination_links () {
    3195         echo bbp_get_topic_pagination_links();
    3196 }
    3197         /**
    3198          * bbp_get_topic_pagination_links ()
    3199          *
    3200          * Return pagination links
    3201          *
    3202          * @package bbPress
    3203          * @subpackage Template Tags
    3204          * @since bbPress (r2519)
    3205          *
    3206          * @global WP_Query $bbp_replies_template
    3207          * @return string
    3208          */
    3209         function bbp_get_topic_pagination_links () {
    3210                 global $bbp_replies_template;
    3211 
    3212                 if ( !isset( $bbp_replies_template->pagination_links ) || empty( $bbp_replies_template->pagination_links ) )
    3213                         return false;
    3214                 else
    3215                         return apply_filters( 'bbp_get_topic_pagination_links', $bbp_replies_template->pagination_links );
    3216         }
    3217 
    3218 /** END reply Loop Functions **************************************************/
    3219 
    322035/** START is_ Functions *******************************************************/
    322136
    … …  
    3298113 * bbp_is_favorites ()
    3299114 *
    3300  * Check if current page is a bbPress user's favorites page (author page)
     115 * Check if current page is a bbPress user's favorites page (profile page)
    3301116 *
    3302117 * @since bbPress (r2652)
    3303118 *
    3304  * @return bool
    3305  */
    3306 function bbp_is_favorites () {
    3307         return (bool) is_author();
     119 * @param bool $query_name_check Optional. Check the query name (_bbp_query_name query var), if it is 'bbp_user_profile_favorites' or not. Defaults to true.
     120 * @return bool
     121 */
     122function bbp_is_favorites ( $query_name_check = true ) {
     123        if ( !bbp_is_user_profile_page() )
     124                return false;
     125
     126        if ( !empty( $query_name_check ) && 'bbp_user_profile_favorites' != get_query_var( '_bbp_query_name' ) )
     127                return false;
     128
     129        return true;
    3308130}
    3309131
    … …  
    3311133 * bbp_is_subscriptions ()
    3312134 *
    3313  * Check if current page is a bbPress user's subscriptions page (author page)
     135 * Check if current page is a bbPress user's subscriptions page (profile page)
    3314136 *
    3315137 * @since bbPress (r2652)
    3316138 *
    3317  * @return bool
    3318  */
    3319 function bbp_is_subscriptions () {
    3320         return (bool) is_author();
     139 * @param bool $query_name_check Optional. Check the query name (_bbp_query_name query var), if it is 'bbp_user_profile_subscriptions' or not. Defaults to true.
     140 * @return bool
     141 */
     142function bbp_is_subscriptions ( $query_name_check = true ) {
     143        if ( !bbp_is_user_profile_page() )
     144                return false;
     145
     146        if ( !empty( $query_name_check ) && 'bbp_user_profile_subscriptions' != get_query_var( '_bbp_query_name' ) )
     147                return false;
     148
     149        return true;
     150}
     151
     152/**
     153 * bbp_is_topics_created ()
     154 *
     155 * Check if current page shows the topics created by a bbPress user (profile page)
     156 *
     157 * @since bbPress (r2688)
     158 *
     159 * @param bool $query_name_check Optional. Check the query name (_bbp_query_name query var), if it is 'bbp_user_profile_subscriptions' or not. Defaults to true.
     160 * @return bool
     161 */
     162function bbp_is_topics_created ( $query_name_check = true ) {
     163        if ( !bbp_is_user_profile_page() )
     164                return false;
     165
     166        if ( !empty( $query_name_check ) && 'bbp_user_profile_topics_created' != get_query_var( '_bbp_query_name' ) )
     167                return false;
     168
     169        return true;
    3321170}
    3322171
    … …  
    3329178 * @return bool
    3330179 */
    3331 function bbp_is_user_home() {
    3332         global $current_user;
    3333 
    3334         $current_user = wp_get_current_user();
    3335 
    3336         if ( $current_user->ID == get_the_author_meta( 'ID' ) )
    3337                 $retval = true;
     180function bbp_is_user_home () {
     181        global $bbp;
     182
     183        if ( !isset( $bbp->displayed_user ) )
     184                $retval = false;
    3338185        else
    3339                 $retval = false;
    3340 
    3341         return apply_filters( 'bbp_is_user_home', $retval, $current_user );
    3342 }
    3343 
    3344 /**
    3345  * bbp_is_anonymous ()
    3346  *
    3347  * Return true if anonymous is allowed and user is not logged in.
    3348  * Return false if anonymous is not allowed or user is logged in
    3349  *
    3350  * @return bool
    3351  */
    3352 function bbp_is_anonymous () {
    3353         if ( !is_user_logged_in() && bbp_allow_anonymous() )
    3354                 $is_anonymous = true;
     186                $retval = $bbp->current_user->ID == $bbp->displayed_user->ID ? true : false;
     187
     188        return apply_filters( 'bbp_is_user_home', $retval );
     189}
     190
     191/**
     192 * bbp_is_user_profile_page ()
     193 *
     194 * Check if current page is a user profile page
     195 *
     196 * @since bbPress (r2688)
     197 *
     198 * @global object $wp_query
     199 * @return bool
     200 */
     201function bbp_is_user_profile_page () {
     202        global $wp_query;
     203
     204        if ( !empty( $wp_query->bbp_is_user_profile_page ) && $wp_query->bbp_is_user_profile_page == true )
     205                return true;
    3355206        else
    3356                 $is_anonymous = false;
    3357 
    3358         return apply_filters( 'bbp_is_anonymous', $is_anonymous );
     207                return false;
     208
     209        return apply_filters( 'bbp_is_user_profile_page', $retval, $wp_query );
     210}
     211
     212/**
     213 * bbp_is_user_profile_edit ()
     214 *
     215 * Check if current page is a user profile edit page
     216 *
     217 * @since bbPress (r2688)
     218 *
     219 * @global object $wp_query
     220 * @return bool
     221 */
     222function bbp_is_user_profile_edit () {
     223        global $wp_query;
     224
     225        if ( !empty( $wp_query->bbp_is_user_profile_edit ) && $wp_query->bbp_is_user_profile_edit == true )
     226                return true;
     227        else
     228                return false;
     229
     230        return apply_filters( 'bbp_is_user_profile_edit', $retval, $wp_query );
    3359231}
    3360232
    3361233/** END is_ Functions *********************************************************/
    3362 
    3363 /** START Favorites Functions *************************************************/
    3364 
    3365 /**
    3366  * bbp_favorites_permalink ()
    3367  *
    3368  * Output the link to the user's favorites page (author page)
    3369  *
    3370  * @package bbPress
    3371  * @subpackage Template Tags
    3372  * @since bbPress (r2652)
    3373  *
    3374  * @param int $user_id optional
    3375  * @uses bbp_get_favorites_permalink()
    3376  */
    3377 function bbp_favorites_permalink ( $user_id = 0 ) {
    3378         echo bbp_get_favorites_permalink( $user_id );
    3379 }
    3380         /**
    3381          * bbp_get_favorites_permalink ()
    3382          *
    3383          * Return the link to the user's favorites page (author page)
    3384          *
    3385          * @package bbPress
    3386          * @subpackage Template Tags
    3387          * @since bbPress (r2652)
    3388          *
    3389          * @param int $user_id optional
    3390          * @uses apply_filters
    3391          * @uses get_author_posts_url
    3392          * @return string Permanent link to topic
    3393          */
    3394         function bbp_get_favorites_permalink ( $user_id = 0 ) {
    3395                 return apply_filters( 'bbp_get_favorites_permalink', get_author_posts_url( $user_id ) );
    3396         }
    3397 
    3398 /**
    3399  * bbp_user_favorites_link ()
    3400  *
    3401  * Output the link to make a topic favorite/remove a topic from favorites
    3402  *
    3403  * @package bbPress
    3404  * @subpackage Template Tags
    3405  * @since bbPress (r2652)
    3406  *
    3407  * @param array $add optional
    3408  * @param array $rem optional
    3409  * @param int $user_id optional
    3410  *
    3411  * @uses bbp_get_user_favorites_link()
    3412  */
    3413 function bbp_user_favorites_link ( $add = array(), $rem = array(), $user_id = 0 ) {
    3414         echo bbp_get_user_favorites_link( $add, $rem, $user_id );
    3415 }
    3416         /**
    3417          * bbp_get_user_favorites_link ()
    3418          *
    3419          * Return the link to make a topic favorite/remove a topic from favorites
    3420          *
    3421          * @package bbPress
    3422          * @subpackage Template Tags
    3423          * @since bbPress (r2652)
    3424          *
    3425          * @param array $add optional
    3426          * @param array $rem optional
    3427          * @param int $user_id optional
    3428          *
    3429          * @uses apply_filters
    3430          * @return string Permanent link to topic
    3431          */
    3432         function bbp_get_user_favorites_link ( $add = array(), $rem = array(), $user_id = 0 ) {
    3433                 global $current_user;
    3434 
    3435                 $current_user = wp_get_current_user();
    3436 
    3437                 if ( empty( $user_id ) && !$user_id = $current_user->ID )
    3438                         return false;
    3439 
    3440                 if ( !current_user_can( 'edit_user', (int) $user_id ) )
    3441                         return false;
    3442 
    3443                 if ( !$topic_id = bbp_get_topic_id() )
    3444                         return false;
    3445 
    3446                 if ( empty( $add ) || !is_array( $add ) ) {
    3447                         $add = array(
    3448                                 'mid'  => __( 'Add this topic to your favorites', 'bbpress' ),
    3449                                 'post' => __( ' (%?%)', 'bbpress' )
    3450                         );
    3451                 }
    3452 
    3453                 if ( empty( $rem ) || !is_array( $rem ) ) {
    3454                         $rem = array(
    3455                                 'pre'  => __( 'This topic is one of your %favorites% [', 'bbpress' ),
    3456                                 'mid'  => __( '&times;', 'bbpress' ),
    3457                                 'post' => __( ']', 'bbpress' )
    3458                         );
    3459                 }
    3460 
    3461                 if ( $is_fav = bbp_is_user_favorite( $user_id, $topic_id ) ) {
    3462                         $url  = esc_url( bbp_get_favorites_permalink( $user_id ) );
    3463                         $rem  = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $rem );
    3464                         $favs = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
    3465                         $pre  = ( is_array( $rem ) && isset( $rem['pre']  ) ) ? $rem['pre']  : '';
    3466                         $mid  = ( is_array( $rem ) && isset( $rem['mid']  ) ) ? $rem['mid']  : ( is_string( $rem ) ? $rem : '' );
    3467                         $post = ( is_array( $rem ) && isset( $rem['post'] ) ) ? $rem['post'] : '';
    3468                 } else {
    3469                         $url  = esc_url( bbp_get_topic_permalink( $topic_id ) );
    3470                         $add  = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $add );
    3471                         $favs = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id );
    3472                         $pre  = ( is_array( $add ) && isset( $add['pre']  ) ) ? $add['pre']  : '';
    3473                         $mid  = ( is_array( $add ) && isset( $add['mid']  ) ) ? $add['mid']  : ( is_string( $add ) ? $add : '' );
    3474                         $post = ( is_array( $add ) && isset( $add['post'] ) ) ? $add['post'] : '';
    3475                 }
    3476 
    3477                 // Create the link based where the user is and if the topic is already the user's favorite
    3478                 $permalink = bbp_is_favorites() ? bbp_get_favorites_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
    3479                 $url       = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) );
    3480                 $is_fav    = $is_fav ? 'is-favorite' : '';
    3481                 $html      = '<span id="favorite-toggle"><span id="favorite-' . $topic_id . '" class="' . $is_fav . '">' . $pre . '<a href="' . $url . '" class="dim:favorite-toggle:favorite-' . $topic_id . ':is-favorite">' . $mid . '</a>' . $post . '</span></span>';
    3482 
    3483                 // Return the link
    3484                 return apply_filters( 'bbp_get_user_favorites_link', $html, $add, $rem, $user_id, $topic_id );
    3485         }
    3486 
    3487 /** END Favorites Functions ***************************************************/
    3488 
    3489 /** START Subscriptions Functions *********************************************/
    3490 
    3491 /**
    3492  * bbp_user_subscribe_link ()
    3493  *
    3494  * Output the link to subscribe/unsubscribe from a topic
    3495  *
    3496  * @package bbPress
    3497  * @subpackage Template Tags
    3498  * @since bbPress (r2668)
    3499  *
    3500  * @param mixed $args
    3501  *
    3502  * @uses bbp_get_user_subscribe_link()
    3503  */
    3504 function bbp_user_subscribe_link ( $args = '' ) {
    3505         echo bbp_get_user_subscribe_link( $args );
    3506 }
    3507         /**
    3508          * bbp_get_user_subscribe_link ()
    3509          *
    3510          * Return the link to subscribe/unsubscribe from a topic
    3511          *
    3512          * @package bbPress
    3513          * @subpackage Template Tags
    3514          * @since bbPress (r2668)
    3515          *
    3516          * @param mixed $args
    3517          *
    3518          * @uses apply_filters
    3519          * @return string Permanent link to topic
    3520          */
    3521         function bbp_get_user_subscribe_link ( $args = '' ) {
    3522                 if ( !bbp_is_subscriptions_active() )
    3523                         return;
    3524 
    3525                 $defaults = array (
    3526                         'subscribe'     => __( 'Subscribe',   'bbpress' ),
    3527                         'unsubscribe'   => __( 'Unsubscribe', 'bbpress' ),
    3528                         'user_id'       => 0,
    3529                         'topic_id'      => 0,
    3530                         'before'        => '&nbsp;|&nbsp;',
    3531                         'after'         => ''
    3532                 );
    3533 
    3534                 $args = wp_parse_args( $args, $defaults );
    3535                 extract( $args );
    3536 
    3537                 // Try to get a user_id from $current_user
    3538                 if ( empty( $user_id ) ) {
    3539                         global $current_user;
    3540                         $current_user = wp_get_current_user();
    3541 
    3542                         // Return if not logged in
    3543                         if ( !$user_id = $current_user->ID ) {
    3544                                 return false;
    3545                         }
    3546                 }
    3547 
    3548                 if ( !current_user_can( 'edit_user', (int) $user_id ) )
    3549                         return false;
    3550 
    3551                 if ( !$topic_id = bbp_get_topic_id( $topic_id ) )
    3552                         return false;
    3553 
    3554                 if ( $is_subscribed = bbp_is_user_subscribed( $user_id, $topic_id ) ) {
    3555                         $text = $unsubscribe;
    3556                         $query_args  = array( 'action' => 'bbp_unsubscribe', 'topic_id' => $topic_id );
    3557                 } else {
    3558                         $text = $subscribe;
    3559                         $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id );
    3560                 }
    3561 
    3562                 // Create the link based where the user is and if the user is subscribed already
    3563                 $permalink     = bbp_is_subscriptions() ? bbp_get_favorites_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
    3564                 $url           = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
    3565                 $is_subscribed = $is_subscribed ? 'is-subscribed' : '';
    3566                 $html          = '<span id="subscription-toggle">' . $before . '<span id="subscribe-' . $topic_id . '" class="' . $is_subscribed . '"><a href="' . $url . '" class="dim:subscription-toggle:subscribe-' . $topic_id . ':is-subscribed">' . $text . '</a></span>' . $after . '</span>';
    3567 
    3568                 // Return the link
    3569                 return apply_filters( 'bbp_get_user_subscribe_link', $html, $subscribe, $unsubscribe, $user_id, $topic_id );
    3570         }
    3571 
    3572 /** END Subscriptions Functions ***********************************************/
    3573 
    3574 /** START User Functions ******************************************************/
    3575 
    3576 /**
    3577  * bbp_current_user_id ()
    3578  *
    3579  * Output ID of current user
    3580  *
    3581  * @uses bbp_get_current_user_id()
    3582  */
    3583 function bbp_current_user_id () {
    3584         echo bbp_get_current_user_id();
    3585 }
    3586         /**
    3587          * bbp_get_current_user_id ()
    3588          *
    3589          * Return ID of current user
    3590          *
    3591          * @global object $current_user
    3592          * @global string $user_identity
    3593          * @return int
    3594          */
    3595         function bbp_get_current_user_id () {
    3596                 global $current_user;
    3597 
    3598                 if ( is_user_logged_in() )
    3599                         $current_user_id = $current_user->ID;
    3600                 else
    3601                         $current_user_id = 0;
    3602 
    3603                 return apply_filters( 'bbp_get_current_user_id', $current_user_id );
    3604         }
    3605 
    3606 /**
    3607  * bbp_current_user_name ()
    3608  *
    3609  * Output name of current user
    3610  *
    3611  * @uses bbp_get_current_user_name()
    3612  */
    3613 function bbp_current_user_name () {
    3614         echo bbp_get_current_user_name();
    3615 }
    3616         /**
    3617          * bbp_get_current_user_name ()
    3618          *
    3619          * Return name of current user
    3620          *
    3621          * @global object $current_user
    3622          * @global string $user_identity
    3623          * @return string
    3624          */
    3625         function bbp_get_current_user_name () {
    3626                 global $current_user, $user_identity;
    3627 
    3628                 if ( is_user_logged_in() )
    3629                         $current_user_name = $user_identity;
    3630                 else
    3631                         $current_user_name = __( 'Anonymous', 'bbpress' );
    3632 
    3633                 return apply_filters( 'bbp_get_current_user_name', $current_user_name );
    3634         }
    3635 
    3636 /**
    3637  * bbp_current_user_avatar ()
    3638  *
    3639  * Output avatar of current user
    3640  *
    3641  * @uses bbp_get_current_user_avatar()
    3642  */
    3643 function bbp_current_user_avatar ( $size = 40 ) {
    3644         echo bbp_get_current_user_avatar( $size );
    3645 }
    3646 
    3647         /**
    3648          * bbp_get_current_user_avatar ( $size = 40 )
    3649          *
    3650          * Return avatar of current user
    3651          *
    3652          * @global object $current_user
    3653          * @param int $size
    3654          * @return string
    3655          */
    3656         function bbp_get_current_user_avatar ( $size = 40 ) {
    3657                 global $current_user;
    3658 
    3659                 return apply_filters( 'bbp_get_current_user_avatar', get_avatar( bbp_get_current_user_id(), $size ) );
    3660         }
    3661 
    3662 /** END User Functions ********************************************************/
    3663234
    3664235/** START Form Functions ******************************************************/
    … …  
    3749320
    3750321/**
     322 * bbp_error_messages ()
     323 *
     324 * Display possible error messages inside a template file
     325 *
     326 * @global WP_Error $errors
     327 */
     328function bbp_error_messages () {
     329        global $errors;
     330
     331        if ( isset( $errors ) && is_wp_error( $errors ) ) : ?>
     332
     333                <div class="bp-messages error">
     334                        <p>
     335                                <?php echo implode( "</p>\n<p>", $errors->get_error_messages() ); ?>
     336                        </p>
     337                </div>
     338
     339<?php endif;
     340}
     341add_action( 'bbp_template_notices', 'bbp_error_messages' );
     342
     343/**
    3751344 * bbp_title_breadcrumb ( $sep )
    3752345 *
  • branches/plugin/bbp-includes/bbp-loader.php

    r2679 r2688  
    1313
    1414// Attach to WordPress actions
    15 add_action( 'plugins_loaded', 'bbp_loaded'                   , 10 );
    16 add_action( 'init',           'bbp_init'                     , 10 );
     15add_action( 'plugins_loaded',         'bbp_loaded'                   , 10 );
     16add_action( 'init',                   'bbp_init'                     , 10 );
     17add_action( 'generate_rewrite_rules', 'bbp_generate_rewrite_rules'   , 12 );
    1718
    1819// Attach to bbp_loaded.
    19 add_action( 'bbp_loaded',     'bbp_constants'                , 2  );
    20 add_action( 'bbp_loaded',     'bbp_boot_strap_globals'       , 4  );
    21 add_action( 'bbp_loaded',     'bbp_includes'                 , 6  );
    22 add_action( 'bbp_loaded',     'bbp_setup_globals'            , 8  );
    23 add_action( 'bbp_loaded',     'bbp_register_theme_directory' , 10 );
     20add_action( 'bbp_loaded',             'bbp_constants'                , 2  );
     21add_action( 'bbp_loaded',             'bbp_boot_strap_globals'       , 4  );
     22add_action( 'bbp_loaded',             'bbp_includes'                 , 6  );
     23add_action( 'bbp_loaded',             'bbp_setup_globals'            , 8  );
     24add_action( 'bbp_loaded',             'bbp_register_theme_directory' , 10 );
    2425
    2526// Attach to bbp_init.
    26 add_action( 'bbp_init',       'bbp_register_post_types'      , 4  );
    27 add_action( 'bbp_init',       'bbp_register_taxonomies'      , 6  );
    28 add_action( 'bbp_init',       'bbp_register_textdomain'      , 8  );
    29 add_action( 'bbp_init',       'bbp_ready'                    , 10 );
     27add_action( 'bbp_init',               'bbp_register_post_types'      , 4  );
     28add_action( 'bbp_init',               'bbp_register_taxonomies'      , 6  );
     29add_action( 'bbp_init',               'bbp_register_textdomain'      , 8  );
     30add_action( 'bbp_init',               'bbp_add_user_rewrite_tag'     , 10 );
     31add_action( 'bbp_init',               'bbp_ready'                    , 14 );
    3032
    3133/**
    … …  
    126128
    127129/**
     130 * bbp_add_user_rewrite_tag ()
     131 *
     132 * Add the %bbp_user% rewrite tag
     133 *
     134 * @since bbPress (r2688)
     135 */
     136function bbp_add_user_rewrite_tag () {
     137        do_action ( 'bbp_add_user_rewrite_tag' );
     138}
     139
     140/**
     141 * bbp_generate_rewrite_rules ()
     142 *
     143 * Generate rewrite rules, particularly for /profile/%bbp_user%/ pages
     144 *
     145 * @since bbPress (r2688)
     146 *
     147 * @param object $wp_rewrite
     148 */
     149function bbp_generate_rewrite_rules ( $wp_rewrite ) {
     150        do_action_ref_array( 'bbp_generate_rewrite_rules', array( &$wp_rewrite ) );
     151}
     152
     153/**
    128154 * bbp_ready ()
    129155 *
  • branches/plugin/bbp-includes/bbp-users.php

    r2686 r2688  
    22
    33/**
    4  * bbp_has_access()
    5  *
    6  * Make sure user can perform special tasks
    7  *
    8  * @package bbPress
    9  * @subpackage Functions
    10  * @since bbPress (r2464)
    11  *
    12  * @uses is_super_admin ()
    13  * @uses apply_filters
    14  *
    15  * @todo bbPress port of existing roles/caps
    16  * @return bool $has_access
    17  */
    18 function bbp_has_access () {
    19 
    20         if ( is_super_admin () )
    21                 $has_access = true;
     4 * bbp_is_anonymous ()
     5 *
     6 * Return true if anonymous is allowed and user is not logged in.
     7 * Return false if anonymous is not allowed or user is logged in
     8 *
     9 * @return bool
     10 */
     11function bbp_is_anonymous () {
     12        if ( !is_user_logged_in() && bbp_allow_anonymous() )
     13                $is_anonymous = true;
    2214        else
    23                 $has_access = false;
    24 
    25         return apply_filters( 'bbp_has_access', $has_access );
    26 }
     15                $is_anonymous = false;
     16
     17        return apply_filters( 'bbp_is_anonymous', $is_anonymous );
     18}
     19
     20/**
     21 * bbp_set_current_user ()
     22 *
     23 * Sets the current user in bbPress scope
     24 *
     25 * @global bbPress $bbp
     26 * @global WP_User $current_user
     27 * @return If already set
     28 */
     29function bbp_set_current_user () {
     30        global $bbp, $current_user;
     31
     32        // Don't set again
     33        if ( isset( $bbp->current_user ) )
     34                return;
     35
     36        // Load current user if somehow it hasn't been set yet
     37        if ( !isset( $current_user ) )
     38                wp_die( 'Loading the user too soon!' );
     39
     40        // Set bbPress current user to WordPress current user
     41        $bbp->current_user = $current_user;
     42}
     43add_action( 'bbp_init', 'bbp_set_current_user', 2 );
    2744
    2845/**
    … …  
    83100 */
    84101function bbp_get_user_favorites ( $user_id = 0 ) {
     102        // Default to the query var set before (if it is a profile page)
     103        if ( empty( $user_id ) && bbp_is_user_profile_page() )
     104                $user_id = get_query_var( 'bbp_user_id' );
     105
    85106        // Default to author
    86107        if ( empty( $user_id ) )
    87108                $user_id = get_the_author_meta( 'ID' );
    88109
    89         // If nothing passed and not an author page, return nothing
    90         if ( empty( $user_id ) )
    91                 return false;
    92 
    93         // Get users' favorites
    94         $favorites = bbp_get_user_favorites_topic_ids( $user_id );
     110        // If nothing passed and not an author/profile page, return nothing
     111        if ( empty( $user_id ) )
     112                return false;
    95113
    96114        // If user has favorites, load them
    97         if ( !empty( $favorites ) ) {
    98                 $query = bbp_has_topics( array( 'post__in' => $favorites, 'posts_per_page' => -1 ) );
     115        if ( $favorites = bbp_get_user_favorites_topic_ids( $user_id ) ) {
     116                $query = bbp_has_topics( array( 'post__in' => $favorites ) );
    99117                return apply_filters( 'bbp_get_user_favorites', $query, $user_id );
    100118        }
    … …  
    122140                $favorites = (array) explode( ',', $favorites );
    123141                $favorites = array_filter( $favorites );
    124                 $favorites = apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id );
    125 
    126                 if ( !empty( $favorites ) )
    127                         return $favorites;
    128 
    129                 return false;
     142
     143                return apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites, $user_id );
    130144        }
    131145
    … …  
    294308 */
    295309function bbp_get_user_subscriptions ( $user_id = 0 ) {
     310
     311        // Default to the displayed user
     312        if ( empty( $user_id ) && bbp_is_user_profile_page() )
     313                $user_id = bbp_get_displayed_user_id();
     314
    296315        // Default to author
    297316        if ( empty( $user_id ) )
    298317                $user_id = get_the_author_meta( 'ID' );
    299318
    300         // If nothing passed and not an author page, return nothing
    301         if ( empty( $user_id ) )
    302                 return false;
    303 
    304         // Get users' subscriptions
    305         $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id );
     319        // If nothing passed and not an author/profile page, return nothing
     320        if ( empty( $user_id ) )
     321                return false;
    306322
    307323        // If user has subscriptions, load them
    308         if ( !empty( $subscriptions ) ) {
    309                 $query = bbp_has_topics( array( 'post__in' => $subscriptions, 'posts_per_page' => -1 ) );
    310                 return apply_filters( 'bbp_get_user_subscriptions', $query );
     324        if ( $subscriptions = bbp_get_user_subscribed_topic_ids( $user_id ) ) {
     325                $query = bbp_has_topics( array( 'post__in' => $subscriptions ) );
     326                return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
    311327        }
    312328
    … …  
    333349                $subscriptions = (array) explode( ',', $subscriptions );
    334350                $subscriptions = array_filter( $subscriptions );
    335                 $subscriptions = apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions );
    336 
    337                 if ( !empty( $subscriptions ) )
    338                         return $subscriptions;
    339 
    340                 return false;
     351
     352                return apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions );
    341353        }
    342354
    … …  
    355367 */
    356368function bbp_is_user_subscribed ( $user_id = 0, $topic_id = 0 ) {
    357         global $post, $current_user;
    358 
    359         if ( empty( $user_id ) ) {
    360                 $current_user = wp_get_current_user();
    361                 $user_id      = $current_user->ID;
    362         }
     369        global $bbp, $post;
     370
     371        if ( empty( $user_id ) )
     372                $user_id = $bbp->current_user->ID;
    363373
    364374        if ( empty( $user_id ) )
    … …  
    368378
    369379        if ( !empty( $topic_id ) ) {
    370                 $post = get_post( $topic_id );
     380                $post     = get_post( $topic_id );
    371381                $topic_id = $post->ID;
    372382        } elseif ( !$topic_id = bbp_get_topic_id() ) {
    … …  
    475485 */
    476486function bbp_get_user_topics_started ( $user_id = 0 ) {
     487        // Default to the query var set before (if it is a profile page)
     488        if ( empty( $user_id ) && bbp_is_user_profile_page() )
     489                $user_id = get_query_var( 'bbp_user_id' );
     490
    477491        // Default to author
    478492        if ( empty( $user_id ) )
    479493                $user_id = get_the_author_meta( 'ID' );
    480494
    481         // If nothing passed and not an author page, return nothing
     495        // If nothing passed and not an author/profile page, return nothing
    482496        if ( empty( $user_id ) )
    483497                return false;
  • branches/plugin/bbp-themes/bbp-twentyten/css/bbpress.css

    r2679 r2688  
    155155        clear: left;
    156156}
     157
     158/* edit user */
     159div.bbp-edit-user fieldset {
     160        margin-top: 20px;
     161        padding: 20px 20px 0 20px;
     162}
     163        div.bbp-edit-user fieldset div {
     164                margin-bottom: 20px;
     165                float: left;
     166                width: 100%;
     167                clear: left;
     168        }
     169        div.bbp-edit-user fieldset textarea {
     170                line-height: 0;
     171        }
     172        div.bbp-edit-user fieldset select {
     173                margin-bottom: 0;
     174        }
     175        div.bbp-edit-user fieldset input,       
     176        div.bbp-edit-user fieldset textarea {
     177                margin-bottom: 0;
     178                width: 250px;
     179        }
     180        div.bbp-edit-user fieldset legend {
     181                display: none;
     182        }
     183        div.bbp-edit-user fieldset label {
     184                float: left;
     185                width: 150px;
     186                padding-right: 20px;
     187                text-align: right;
     188        }
     189        div.bbp-edit-user fieldset span.description {
     190                margin-left: 170px;
     191                font-size: 12px;
     192                font-style: italic;
     193                float: left;
     194                clear: left;
     195                width: 233px;
     196                padding: 5px 10px;
     197                border: #E6DB55 1px solid;
     198                background-color: #FFFBCC;
     199        }
     200
     201        div.bbp-edit-user fieldset fieldset {
     202                margin: 0;
     203                width: 260px;
     204                border: none;
     205                padding: 0;
     206        }
     207        div.bbp-edit-user fieldset fieldset span.description {
     208                margin-left: 0;
     209                margin-bottom: 20px;
     210        }
     211
     212/* notices */
     213div.bbp-template-notice {
     214        border: #bbddbb 1px solid;
     215        background-color: #ddffdd;
     216        padding: 10px;
     217        margin-top: 20px;
     218        }
     219        div.bbp-template-notice.important {
     220                border: #E6DB55 1px solid;
     221                background-color: #FFFBCC;
     222        }
     223        #main div.bbp-template-notice p {
     224                margin: 0;
     225        }
  • branches/plugin/bbp-themes/bbp-twentyten/functions.php

    r2679 r2688  
    9898
    9999/**
    100  * bbp_twentyten_enqueue_topic_script ()
    101  *
    102  * Enqueue the topic page Javascript file
    103  *
    104  * @package bbPress
    105  * @subpackage bbPress TwentyTen
    106  * @since bbPress (r2652)
    107  *
    108  * @return void
    109  */
    110 function bbp_twentyten_enqueue_topic_script () {
    111         if ( !bbp_is_topic() )
    112                 return;
    113 
    114         wp_enqueue_script( 'bbp_topic', get_stylesheet_directory_uri() . '/js/topic.js', array( 'wp-lists' ), '20101202' );
    115 }
    116 add_filter( 'wp_enqueue_scripts', 'bbp_twentyten_enqueue_topic_script' );
     100 * bbp_twentyten_enqueue_scripts ()
     101 *
     102 * Enqueue the required Javascript files
     103 *
     104 * @package bbPress
     105 * @subpackage bbPress TwentyTen
     106 * @since bbPress (r2652)
     107 *
     108 * @return void
     109 */
     110function bbp_twentyten_enqueue_scripts () {
     111        if ( bbp_is_topic() )
     112                wp_enqueue_script( 'bbp_topic', get_stylesheet_directory_uri() . '/js/topic.js', array( 'wp-lists' ), '20101202' );
     113
     114        if ( bbp_is_user_profile_edit() )
     115                wp_enqueue_script( 'user-profile' );
     116}
     117add_action( 'wp_enqueue_scripts', 'bbp_twentyten_enqueue_scripts' );
    117118
    118119/**
    … …  
    128129 */
    129130function bbp_twentyten_scripts () {
    130         if ( !bbp_is_topic() )
    131                 return; ?>
     131        if ( bbp_is_topic() ) : ?>
    132132
    133133        <script type='text/javascript'>
    … …  
    137137        </script>
    138138
    139 <?php
     139        <?php elseif ( bbp_is_user_profile_edit() ) : ?>
     140
     141        <script type="text/javascript" charset="utf-8">
     142                if ( window.location.hash == '#password' ) {
     143                        document.getElementById('pass1').focus();
     144                }
     145        </script>
     146       
     147        <?php
     148        endif;
    140149}
    141150add_filter( 'wp_head', 'bbp_twentyten_scripts', -1 );
  • branches/plugin/bbp-themes/bbp-twentyten/loop-bbp_forums.php

    r2679 r2688  
    4444                                </tr><!-- bbp-forum-<?php bbp_forum_id(); ?> -->
    4545
    46                         <?php endwhile; global $bbp_forums_template; unset( $bbp_forums_template ); ?>
     46                        <?php endwhile; ?>
    4747
    4848        </table>
  • branches/plugin/bbp-themes/bbp-twentyten/loop-bbp_topics.php

    r2679 r2688  
    77 */
    88
    9 if ( bbp_is_favorites() || bbp_has_topics() ) : ?>
     9if ( bbp_is_subscriptions() || bbp_is_favorites() || bbp_has_topics() ) : ?>
    1010
    1111        <?php get_template_part( 'pagination', 'bbp_topics' ); ?>
    … …  
    1818                                <th class="bbp-topic-voice-count"><?php _e( 'Voices', 'bbpress' ); ?></th>
    1919                                <th class="bbp-topic-freshness"><?php _e( 'Freshness', 'bbpress' ); ?></th>
    20                                 <?php if ( bbp_is_favorites() ) : ?><th class="bbp-topic-action"><?php _e( 'Favorite', 'bbpress' ); ?></th><?php endif; ?>
     20                                <?php if ( ( bbp_is_user_home() && ( bbp_is_favorites() || bbp_is_subscriptions() ) ) ) : ?><th class="bbp-topic-action"><?php _e( 'Remove', 'bbpress' ); ?></th><?php endif; ?>
    2121                        </tr>
    2222                </thead>
    2323
    2424                <tfoot>
    25                         <tr><td colspan="<?php echo bbp_is_favorites() ? '5' : '4'; ?>">&nbsp</td></tr>
     25                        <tr><td colspan="<?php echo ( bbp_is_user_home() && ( bbp_is_favorites() || bbp_is_subscriptions() ) ) ? '5' : '4'; ?>">&nbsp</td></tr>
    2626                </tfoot>
    2727
    … …  
    5151                                        <td class="bbp-topic-freshness"><?php bbp_topic_freshness_link(); ?></td>
    5252
    53                                         <?php if ( bbp_is_favorites() ) : ?>
     53                                        <?php if ( bbp_is_user_home() ) : ?>
    5454
    55                                                 <td class="bbp-topic-action">
     55                                                <?php if ( bbp_is_favorites() ) : ?>
    5656
    57                                                         <?php bbp_user_favorites_link( array( 'mid' => '+', 'post' => '' ), array( 'pre' => '', 'mid' => '&times;', 'post' => '' ) ); ?>
     57                                                        <td class="bbp-topic-action">
    5858
    59                                                 </td>
     59                                                                <?php bbp_user_favorites_link( array( 'mid' => '+', 'post' => '' ), array( 'pre' => '', 'mid' => '&times;', 'post' => '' ) ); ?>
     60
     61                                                        </td>
     62
     63                                                <?php elseif ( bbp_is_subscriptions() ) : ?>
     64
     65                                                        <td class="bbp-topic-action">
     66
     67                                                                <?php bbp_user_subscribe_link( array( 'before' => '', 'subscribe' => '+', 'unsubscribe' => '&times;' ) ); ?>
     68
     69                                                        </td>
     70
     71                                                <?php endif; ?>
    6072
    6173                                        <?php endif; ?>
  • branches/plugin/bbpress.php

    r2679 r2688  
    4040        var $reply_slug;
    4141        var $topic_tag_slug;
     42        var $user_slug;
    4243
    4344        // Absolute Paths
    … …  
    5455        var $current_topic_id;
    5556        var $current_reply_id;
     57
     58        // User objects
     59        var $current_user;
     60        var $displayed_user;
     61
     62        // Query objects
     63        var $forum_query;
     64        var $topic_query;
     65        var $reply_query;
    5666
    5767        /**
    … …  
    101111                $this->reply_slug     = apply_filters( 'bbp_reply_slug',     'reply'     );
    102112                $this->topic_tag_slug = apply_filters( 'bbp_topic_tag_slug', 'topic-tag' );
     113                $this->user_slug      = apply_filters( 'bbp_user_slug',      'user'   );
    103114        }
    104115
    … …  
    120131                require_once ( $this->plugin_dir . '/bbp-includes/bbp-widgets.php'   );
    121132                require_once ( $this->plugin_dir . '/bbp-includes/bbp-users.php'     );
    122                 require_once ( $this->plugin_dir . '/bbp-includes/bbp-template.php'  );
     133
     134                // Load template files
     135                require_once ( $this->plugin_dir . '/bbp-includes/bbp-general-template.php'  );
     136                require_once ( $this->plugin_dir . '/bbp-includes/bbp-forum-template.php'  );
     137                require_once ( $this->plugin_dir . '/bbp-includes/bbp-topic-template.php'  );
     138                require_once ( $this->plugin_dir . '/bbp-includes/bbp-reply-template.php'  );
     139                require_once ( $this->plugin_dir . '/bbp-includes/bbp-user-template.php'  );
    123140
    124141                // Quick admin check and load if needed
    … …  
    148165                // Load textdomain
    149166                add_action( 'bbp_load_textdomain',          array ( $this, 'register_textdomain'      ), 10, 2 );
     167
     168                // Add the %bbp_user% rewrite tag
     169                add_action( 'bbp_add_user_rewrite_tag',     array ( $this, 'add_user_rewrite_tag'     ), 10, 2 );
     170
     171                // Generate rewrite rules, particularly for /profile/%bbp_user%/ pages
     172                add_action( 'bbp_generate_rewrite_rules',   array ( $this, 'generate_rewrite_rules'   ), 10, 2 );
    150173        }
    151174
    … …  
    403426                );
    404427        }
     428
     429        /**
     430         * add_user_rewrite_tag ()
     431         *
     432         * Add the %bbp_user% rewrite tag
     433         *
     434         * @since bbPress (r2688)
     435         * @uses add_rewrite_tag
     436         */
     437        function add_user_rewrite_tag () {
     438                add_rewrite_tag( '%bbp_user%',         '([^/]+)'  );
     439                add_rewrite_tag( '%bbp_edit_profile%', '([1]{1})' );
     440        }
     441
     442        /**
     443         * generate_rewrite_rules ()
     444         *
     445         * Generate rewrite rules for /profile/%bbp_user%/ pages
     446         *
     447         * @since bbPress (r2688)
     448         *
     449         * @param object $wp_rewrite
     450         */
     451        function generate_rewrite_rules ( $wp_rewrite ) {
     452                $user_rules = array(
     453                        // @todo - feeds
     454                        //$this->user_slug . '/([^/]+)/(feed|rdf|rss|rss2|atom)/?$'      => 'index.php?bbp_user=' . $wp_rewrite->preg_index( 1 ) . '&feed='  . $wp_rewrite->preg_index( 2 ),
     455                        //$this->user_slug . '/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?bbp_user=' . $wp_rewrite->preg_index( 1 ) . '&feed='  . $wp_rewrite->preg_index( 2 ),
     456                        $this->user_slug . '/([^/]+)/edit/?$'                          => 'index.php?bbp_user=' . $wp_rewrite->preg_index( 1 ) . '&bbp_edit_profile=1',
     457                        $this->user_slug . '/([^/]+)/page/?([0-9]{1,})/?$'             => 'index.php?bbp_user=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
     458                        $this->user_slug . '/([^/]+)/?$'                               => 'index.php?bbp_user=' . $wp_rewrite->preg_index( 1 )
     459                );
     460
     461                $wp_rewrite->rules = array_merge( $user_rules, $wp_rewrite->rules );
     462        }
    405463}
    406464
Note: See TracChangeset for help on using the changeset viewer.