Skip to:
Content

bbPress.org

Changeset 2660


Ignore:
Timestamp:
11/29/2010 11:09:18 AM (14 years ago)
Author:
johnjamesjacoby
Message:

Make favorites work without needing AJAX. Add Topics Created to author page. Adjust some CSS. Rename favorite links to permalinks. Clean up author.php.

Location:
branches/plugin
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/plugin/bbp-includes/bbp-functions.php

    r2659 r2660  
    245245
    246246/**
     247 * bbp_favorites_handler ()
     248 *
     249 * Handles the front end adding and removing of favorite topics
     250 */
     251function bbp_favorites_handler () {
     252    global $bbp, $current_user;
     253
     254    // Only proceed if GET is a favorite action
     255    if ( 'GET' == $_SERVER['REQUEST_METHOD'] && !empty( $_GET['action'] ) && !empty( $_GET['topic_id'] ) ) {
     256
     257        // Load user info
     258        $current_user = wp_get_current_user();
     259        $user_id      = $current_user->ID;
     260
     261        // Check users ability to create new reply
     262        if ( !current_user_can( 'edit_user', $user_id ) )
     263            return false;
     264
     265        // What action is taking place?
     266        $action       = $_GET['action'];
     267
     268        // Load favorite info
     269        $topic_id     = intval( $_GET['topic_id'] );
     270        $is_favorite  = bbp_is_user_favorite( $user_id, $topic_id );
     271        $success      = false;
     272
     273        // Handle insertion into posts table
     274        if ( !empty( $topic_id ) && !empty( $user_id ) ) {
     275
     276            if ( $is_favorite && 'bbp_favorite_remove' == $action )
     277                $success = bbp_remove_user_favorite( $user_id, $topic_id );
     278            elseif ( !$is_favorite && 'bbp_favorite_add' == $action )
     279                $success = bbp_add_user_favorite( $user_id, $topic_id );
     280
     281            // Do additional favorites actions
     282            do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
     283
     284            // Check for missing reply_id or error
     285            if ( true == $success ) {
     286
     287                // Redirect back to new reply
     288                $redirect = bbp_is_favorites() ? bbp_get_favorites_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
     289                wp_redirect( $redirect );
     290
     291                // For good measure
     292                exit();
     293            }
     294        }
     295    }
     296}
     297add_action( 'template_redirect', 'bbp_favorites_handler' );
     298
     299/**
     300 * bbp_load_template( $files )
     301 *
     302 *
     303 * @param str $files
     304 * @return On failure
     305 */
     306function bbp_load_template( $files ) {
     307    if ( empty( $files ) )
     308        return;
     309
     310    // Force array
     311    if ( is_string( $files ) )
     312        $files = (array)$files;
     313
     314    // Exit if file is found
     315    if ( locate_template( $files, true ) )
     316        exit();
     317
     318    return;
     319}
     320
     321/**
    247322 * bbp_get_stickies()
    248323 *
  • branches/plugin/bbp-includes/bbp-template.php

    r2659 r2660  
    10571057    $bbp_topics_template = new WP_Query( $bbp_t );
    10581058
     1059    if ( -1 == $posts_per_page )
     1060        $posts_per_page = $bbp_topics_template->post_count;
     1061
    10591062    // Add pagination values to query object
    10601063    $bbp_topics_template->posts_per_page = $posts_per_page;
     
    10621065
    10631066    // Only add pagination if query returned results
    1064     if ( (int)$bbp_topics_template->found_posts && (int)$bbp_topics_template->posts_per_page ) {
     1067    if ( (int)$bbp_topics_template->post_count && (int)$bbp_topics_template->posts_per_page ) {
    10651068
    10661069        // If pretty permalinks are enabled, make our pagination pretty
     
    10741077            'base'      => $base,
    10751078            'format'    => '',
    1076             'total'     => ceil( (int)$bbp_topics_template->found_posts / (int)$posts_per_page ),
     1079            'total'     => $posts_per_page == $bbp_topics_template->post_count ? 1 : ceil( (int)$bbp_topics_template->found_posts / (int)$posts_per_page ),
    10771080            'current'   => (int)$bbp_topics_template->paged,
    10781081            'prev_text' => '←',
     
    19911994        $from_num  = bbp_number_format( $start_num );
    19921995        $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 ) );
    1993         $total     = bbp_number_format( $bbp_topics_template->found_posts );
     1996        $total     = bbp_number_format( $bbp_topics_template->post_count );
    19941997
    19951998        // Set return string
    1996         if ( $total > 1 && $from_num != $to_num )
     1999        if ( $total > 1 && (int)$from_num == (int)$to_num )
     2000            $retstr = sprintf( __( 'Viewing topic %1$s (of %2$s total)', 'bbpress' ), $from_num, $total );
     2001        elseif ( $total > 1 && empty( $to_num ) )
     2002            $retstr = sprintf( __( 'Viewing %1$s topics', 'bbpress' ), $total );
     2003        elseif ( $total > 1 && (int)$from_num != (int)$to_num )
    19972004            $retstr = sprintf( __( 'Viewing topics %1$s through %2$s (of %3$s total)', 'bbpress' ), $from_num, $to_num, $total );
    1998         elseif ( $total > 1 && $from_num == $to_num )
    1999             $retstr = sprintf( __( 'Viewing topic %1$s (of %2$s total)', 'bbpress' ), $from_num, $total );
    20002005        else
    20012006            $retstr = sprintf( __( 'Viewing %1$s topic', 'bbpress' ), $total );
     
    25612566}
    25622567
     2568/**
     2569 * bbp_is_user_home ()
     2570 *
     2571 * Check if current page is the currently logged in users author page
     2572 *
     2573 * @global object $current_user
     2574 * @return bool
     2575 */
     2576function bbp_is_user_home() {
     2577    global $current_user;
     2578
     2579    $current_user = wp_get_current_user();
     2580
     2581    if ( $current_user->ID == get_the_author_meta( 'ID' ) )
     2582        $retval = true;
     2583    else
     2584        $retval = false;
     2585
     2586    return apply_filters( 'bbp_is_user_home', $retval, $current_user );
     2587}
     2588
    25632589/** END is_ Functions *********************************************************/
    25642590
     
    25662592
    25672593/**
    2568  * bbp_favorites_link ()
     2594 * bbp_favorites_permalink ()
    25692595 *
    25702596 * Output the link to the user's favorites page (author page)
     
    25752601 *
    25762602 * @param int $user_id optional
    2577  * @uses bbp_get_favorites_link()
    2578  */
    2579 function bbp_favorites_link ( $user_id = 0 ) {
    2580     echo bbp_get_favorites_link( $user_id );
    2581 }
    2582     /**
    2583      * bbp_get_favorites_link ()
     2603 * @uses bbp_get_favorites_permalink()
     2604 */
     2605function bbp_favorites_permalink ( $user_id = 0 ) {
     2606    echo bbp_get_favorites_permalink( $user_id );
     2607}
     2608    /**
     2609     * bbp_get_favorites_permalink ()
    25842610     *
    25852611     * Return the link to the user's favorites page (author page)
     
    25942620     * @return string Permanent link to topic
    25952621     */
    2596     function bbp_get_favorites_link ( $user_id = 0 ) {
    2597         return apply_filters( 'bbp_get_favorites_link', get_author_posts_url( $user_id ) );
     2622    function bbp_get_favorites_permalink ( $user_id = 0 ) {
     2623        return apply_filters( 'bbp_get_favorites_permalink', get_author_posts_url( $user_id ) );
    25982624    }
    25992625
     
    26352661    function bbp_get_user_favorites_link ( $add = array(), $rem = array(), $user_id = 0 ) {
    26362662        global $current_user;
    2637         wp_get_current_user();
     2663
     2664        $current_user = wp_get_current_user();
    26382665
    26392666        if ( empty( $user_id ) && !$user_id = $current_user->ID )
     
    26512678                'post' => __( ' (%?%)', 'bbpress' )
    26522679            );
    2653             $url = esc_url( bbp_get_topic_permalink( $topic_id ) );
    26542680        }
    26552681
     
    26602686                'post' => __( ']', 'bbpress' )
    26612687            );
    2662             $url = esc_url( bbp_get_favorites_link( $user_id ) );
    26632688        }
    26642689
    26652690        if ( bbp_is_user_favorite( $user_id, $topic_id ) ) {
     2691            $url  = esc_url( bbp_get_topic_permalink( $topic_id ) );
    26662692            $rem  = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $rem );
    2667             $favs = array( 'fav' => '0', 'topic_id' => $topic_id );
     2693            $favs = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
    26682694            $pre  = ( is_array( $rem ) && isset( $rem['pre']  ) ) ? $rem['pre']  : '';
    26692695            $mid  = ( is_array( $rem ) && isset( $rem['mid']  ) ) ? $rem['mid']  : ( is_string( $rem ) ? $rem : '' );
    26702696            $post = ( is_array( $rem ) && isset( $rem['post'] ) ) ? $rem['post'] : '';
    26712697        } else {
     2698            $url  = esc_url( bbp_get_favorites_permalink( $user_id ) );
    26722699            $add  = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $add );
    2673             $favs = array( 'fav' => '1', 'topic_id' => $topic_id );
     2700            $favs = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id );
    26742701            $pre  = ( is_array( $add ) && isset( $add['pre']  ) ) ? $add['pre']  : '';
    26752702            $mid  = ( is_array( $add ) && isset( $add['mid']  ) ) ? $add['mid']  : ( is_string( $add ) ? $add : '' );
     
    26772704        }
    26782705
    2679         $url = esc_url( wp_nonce_url( add_query_arg( $favs, bbp_get_topic_permalink( $topic_id ) ), 'toggle-favorite_' . $topic_id ) );
     2706        $permalink = bbp_is_favorites() ? bbp_get_favorites_permalink( $user_id ) : bbp_get_topic_permalink( $topic_id );
     2707        $url       = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) );
    26802708
    26812709        return apply_filters( 'bbp_get_user_favorites_link', "<span id='favorite-toggle'><span id='favorite-$topic_id'>$pre<a href='$url' class='dim:favorite-toggle:favorite-$topic_id:is-favorite'>$mid</a>$post</span></span>" );
  • branches/plugin/bbp-includes/bbp-users.php

    r2659 r2660  
    8181 */
    8282function bbp_get_user_favorites ( $user_id = 0 ) {
    83     if ( empty( $user_id ) )
    84         return;
    85 
     83    // Default to author
     84    if ( empty( $user_id ) )
     85        $user_id = get_the_author_meta( 'ID' );
     86
     87    // If nothing passed and not an author page, return nothing
     88    if ( empty( $user_id ) )
     89        return false;
     90
     91    // Get users' favorites
    8692    $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    8793
     94    // If user has favorites, load them
    8895    if ( !empty( $favorites ) ) {
    89         $query = bbp_has_topics( array( 'post__in' => $favorites, 'per_page' => -1 ) );
     96        $query = bbp_has_topics( array( 'post__in' => $favorites, 'posts_per_page' => -1 ) );
    9097        return $query;
    9198    }
     
    111118
    112119    if ( empty( $user_id ) ) {
    113         wp_get_current_user();
    114         $user_id = $current_user->ID;
     120        $current_user = wp_get_current_user();
     121        $user_id      = $current_user->ID;
    115122    }
    116123
     
    213220/** END - Favorites ***********************************************************/
    214221
     222/**
     223 * bbp_get_user_topics_started ()
     224 *
     225 * Get the topics that a user created
     226 *
     227 * @package bbPress
     228 * @subpackage Users
     229 * @since bbPress (r2652)
     230 *
     231 * @param int $user_id User ID
     232 * @return array|bool Results if user has favorites, otherwise false
     233 */
     234function bbp_get_user_topics_started ( $user_id = 0 ) {
     235    // Default to author
     236    if ( empty( $user_id ) )
     237        $user_id = get_the_author_meta( 'ID' );
     238
     239    // If nothing passed and not an author page, return nothing
     240    if ( empty( $user_id ) )
     241        return false;
     242
     243    if ( $query = bbp_has_topics( array( 'author' => $user_id, 'posts_per_page' => -1 ) ) )
     244        return $query;
     245
     246    return false;
     247}
     248
    215249?>
  • branches/plugin/bbp-themes/bbp-twentyten/author.php

    r2652 r2660  
    11<?php
    22/**
    3  * The template for displaying Author Archive pages.
     3 * Template Name: bbPress - User Profile
    44 *
    5  * @package WordPress
    6  * @subpackage Twenty_Ten
    7  * @since Twenty Ten 1.0
     5 * @package bbPress
     6 * @subpackage Template
    87 */
     8?>
    99
    10 get_header(); ?>
     10<?php get_header(); ?>
    1111
    1212        <div id="container">
    1313            <div id="content" role="main">
    1414
    15 <?php
    16     /* Queue the first post, that way we know who
    17      * the author is when we try to get their name,
    18      * URL, description, avatar, etc.
    19      *
    20      * We reset this later so we can run the loop
    21      * properly with a call to rewind_posts().
    22      */
    23     if ( have_posts() )
    24         the_post();
    25 ?>
     15                <?php if ( have_posts() ) the_post(); ?>
    2616
    2717                <h1 class="page-title author"><?php printf( __( 'Author Archives: %s', 'twentyten' ), "<span class='vcard'><a class='url fn n' href='" . get_author_posts_url( get_the_author_meta( 'ID' ) ) . "' title='" . esc_attr( get_the_author() ) . "' rel='me'>" . get_the_author() . "</a></span>" ); ?></h1>
    2818
    29 <?php
    30 // If a user has filled out their description, show a bio on their entries.
    31 if ( get_the_author_meta( 'description' ) ) : ?>
     19                <?php if ( get_the_author_meta( 'description' ) ) : ?>
     20
    3221                    <div id="entry-author-info">
    3322                        <div id="author-avatar">
     23
    3424                            <?php echo get_avatar( get_the_author_meta( 'user_email' ), apply_filters( 'twentyten_author_bio_avatar_size', 60 ) ); ?>
     25
    3526                        </div><!-- #author-avatar -->
    3627                        <div id="author-description">
    3728                            <h2><?php printf( __( 'About %s', 'twentyten' ), get_the_author() ); ?></h2>
     29
    3830                            <?php the_author_meta( 'description' ); ?>
     31
    3932                        </div><!-- #author-description  -->
    4033                    </div><!-- #entry-author-info -->
    41 <?php endif; ?>
    4234
    43 <?php
    44     /* Since we called the_post() above, we need to
    45      * rewind the loop back to the beginning that way
    46      * we can run the loop properly, in full.
    47      */
    48     rewind_posts();
     35                <?php endif; ?>
    4936
    50     /* Run the loop for the author archive page to output the authors posts
    51      * If you want to overload this in a child theme then include a file
    52      * called loop-author.php and that will be used instead.
    53      */
    54     get_template_part( 'loop', 'author' );
    55 ?>
     37                <?php rewind_posts(); ?>
    5638
    57 <h2 id="fav-heading"><?php _e( 'Favorites', 'bbpress' ); ?></h2>
     39                <?php get_template_part( 'loop', 'author' ); ?>
    5840
    59 <?php
    60     /* @todo Add favorites feeds
    61         if ( current_user_can( 'edit_user', get_the_author_meta( 'ID' ) ) ) : ?>
    62     <p>
    63         <?php printf( __( 'You can also <a href="%1$s">manage your favorites</a> and subscribe to your favorites&#8217; <a href="%2$s"><abbr title="Really Simple Syndication">RSS</abbr> feed</a>.', 'bbpress' ), esc_attr( bbp_get_favorites_link() ), esc_attr( bbp_get_favorites_rss_link() ) ); ?>
    64     </p>
     41                <div id="bbp-author-favorites" class="bbp-author-favorites">
     42                    <hr />
     43                    <h1 class="entry-title"><?php _e( 'Favorite Topics', 'bbpress' ); ?></h1>
     44                    <div class="entry-content">
    6545
    66     <p><?php _e( 'Favorites allow members to create a custom <abbr title="Really Simple Syndication">RSS</abbr> feed which pulls recent replies to the topics they specify.', 'bbpress' ); ?></p>
     46                        <?php if ( bbp_is_user_home() ) : ?>
    6747
    68 <?php
    69         endif;
    70      */
    71 ?>
     48                            <p><?php _e( 'To add topics to your list of favorites, just click the "Add to Favorites" link found on that topic&#8217;s page.', 'bbpress' ); ?></p>
    7249
    73 <?php if ( current_user_can( 'edit_user', get_the_author_meta( 'ID' ) ) ) : ?>
     50                        <?php endif; ?>
    7451
    75     <p><?php _e( 'To add topics to your list of favorites, just click the "Add to Favorites" link found on that topic&#8217;s page.', 'bbpress' ); ?></p>
     52                        <?php if ( bbp_get_user_favorites() ) :
    7653
    77 <?php endif; ?>
     54                            get_template_part( 'loop', 'bbp_topics' );
    7855
    79 <?php
    80     // Get the user's favorite topics
    81     if ( bbp_get_user_favorites( get_the_author_meta( 'ID' ) ) ) :
     56                        else : ?>
    8257
    83         get_template_part( 'loop', 'bbp_topics' );
     58                            <p><?php bbp_is_user_home() ? _e( 'You currently have no favorite topics.', 'bbpress' ) : _e( 'This user has no favorite topics.', 'bbpress' ); ?></p>
    8459
    85     else :
     60                        <?php endif; ?>
    8661
    87         $current_user = wp_get_current_user();
     62                    </div>
     63                </div><!-- #bbp-author-favorites -->
    8864
    89         if ( get_the_author_meta( 'ID' ) == $current_user->ID ) : ?>
     65                <div id="bbp-author-topics-started" class="bbp-author-topics-started">
     66                    <hr />
     67                    <h1 class="entry-title"><?php _e( 'Topics Created', 'bbpress' ); ?></h1>
     68                    <div class="entry-content">
    9069
    91         <p><?php _e( 'You currently have no favorite topics.', 'bbpress' ); ?></p>
     70                        <?php if ( bbp_get_user_topics_started() ) :
    9271
    93 <?php else : ?>
     72                            get_template_part( 'loop', 'bbp_topics' );
    9473
    95         <p><?php _e( 'The user currently has no favorite topics.', 'bbpress' ); ?></p>
     74                        else : ?>
    9675
    97 <?php
    98         endif;
    99     endif;
    100 ?>
     76                            <p><?php bbp_is_user_home() ? _e( 'You have not created any topics.', 'bbpress' ) : _e( 'This user has not created any topics.', 'bbpress' ); ?></p>
     77
     78                        <?php endif; ?>
     79
     80                    </div>
     81                </div><!-- #bbp-new-topic -->
    10182
    10283            </div><!-- #content -->
  • branches/plugin/bbp-themes/bbp-twentyten/functions.php

    r2659 r2660  
    1515    global $current_user;
    1616
    17     wp_get_current_user();
    18 
    19     $user_id = $current_user->ID;
    20     $id      = intval( $_POST['id'] );
     17    $current_user = wp_get_current_user();
     18    $user_id      = $current_user->ID;
     19    $id           = intval( $_POST['id'] );
    2120
    2221    if ( !current_user_can( 'edit_user', $user_id ) )
     
    3736
    3837    die( '0' );
    39 
    4038}
    4139add_action( 'wp_ajax_dim-favorite', 'bbp_twentyten_dim_favorite' );
     
    110108        'currentUserId' => $user_id,
    111109        'topicId'       => bbp_get_topic_id(),
    112         'favoritesLink' => bbp_get_favorites_link( $user_id ),
     110        'favoritesLink' => bbp_get_favorites_permalink( $user_id ),
    113111        'isFav'         => (int) bbp_is_user_favorite( $user_id ),
    114112        'favLinkYes'    => __( 'favorites', 'bbpress' ),
     
    118116        'favDel'        => __( '&times;', 'bbpress' ),
    119117        'favAdd'        => __( 'Add this topic to your favorites', 'bbpress' )
    120     ));
     118    ) );
    121119}
    122120add_filter( 'wp_enqueue_scripts', 'bbp_twentyten_topic_script_localization' );
  • branches/plugin/bbp-themes/bbp-twentyten/loop-bbp_topics.php

    r2656 r2660  
    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; ?>
    2021            </tr>
    2122        </thead>
    2223
    2324        <tfoot>
    24             <tr><td colspan="4">&nbsp</td></tr>
     25            <tr><td colspan="<?php echo bbp_is_favorites() ? '5' : '4'; ?>">&nbsp</td></tr>
    2526        </tfoot>
    2627
     
    5051                    <td class="bbp-topic-freshness"><?php bbp_topic_freshness_link(); ?></td>
    5152
     53                    <?php if ( bbp_is_favorites() ) : ?>
     54
     55                        <td class="bbp-topic-action">
     56
     57                            <?php bbp_user_favorites_link( array( 'mid' => '+', 'post' => '' ), array( 'pre' => '', 'mid' => '&times;', 'post' => '' ) ); ?>
     58
     59                        </td>
     60
     61                    <?php endif; ?>
     62
    5263                </tr><!-- #topic-<?php bbp_topic_id(); ?> -->
    5364
  • branches/plugin/bbp-themes/bbp-twentyten/style.css

    r2659 r2660  
    13451345}
    13461346
    1347 .bbp-forum-topic-count, .bbp-forum-topic-replies, .bbp-topic-reply-count, .bbp-topic-voice-count {
     1347.bbp-forum-topic-count, .bbp-forum-topic-replies,
     1348.bbp-topic-reply-count, .bbp-topic-voice-count, .bbp-topic-action {
    13481349    width: 10%;
    13491350    text-align: center;
    13501351}
     1352
    13511353.bbp-topic-freshness, .bbp-forum-freshness {
    13521354    text-align: center;
Note: See TracChangeset for help on using the changeset viewer.