Skip to:
Content

bbPress.org

Changeset 4615


Ignore:
Timestamp:
12/21/2012 08:12:29 AM (11 years ago)
Author:
johnjamesjacoby
Message:

Simplify favorites link logic and output, to match subscriptions link functionality. Fixes #2118.

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/users/template-tags.php

    r4614 r4615  
    619619 * @since bbPress (r2652)
    620620 *
    621  * @param array $add Optional. Add to favorites args
    622  * @param array $rem Optional. Remove from favorites args
     621 * @param mixed $args See {@link bbp_get_user_favorites_link()}
    623622 * @param int $user_id Optional. User id
    624  * @param int $topic_id Optional. Topic id
     623 * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">.
    625624 * @uses bbp_get_user_favorites_link() To get the user favorites link
    626625 */
    627 function bbp_user_favorites_link( $add = array(), $rem = array(), $user_id = 0, $topic_id = 0 ) {
    628     echo bbp_get_user_favorites_link( $add, $rem, $user_id, $topic_id );
     626function bbp_user_favorites_link( $args = array(), $user_id = 0, $wrap = true ) {
     627    echo bbp_get_user_favorites_link( $args, $user_id, $wrap );
    629628}
    630629    /**
     
    636635     * @since bbPress (r2652)
    637636     *
    638      * @param array $add Optional. Add to favorites args
    639      * @param array $rem Optional. Remove from favorites args
     637     * @param mixed $args This function supports these arguments:
     638     *  - subscribe: Favorite text
     639     *  - unsubscribe: Unfavorite text
     640     *  - user_id: User id
     641     *  - topic_id: Topic id
     642     *  - before: Before the link
     643     *  - after: After the link
    640644     * @param int $user_id Optional. User id
    641645     * @param int $topic_id Optional. Topic id
     
    652656     * @return string User favorites link
    653657     */
    654     function bbp_get_user_favorites_link( $add = array(), $rem = array(), $user_id = 0, $topic_id = 0, $wrap = true ) {
     658    function bbp_get_user_favorites_link( $args = '', $user_id = 0, $wrap = true ) {
    655659        if ( !bbp_is_favorites_active() )
    656660            return false;
    657661
     662        // Parse arguments against default values
     663        $r = bbp_parse_args( $args, array(
     664            'favorite'  => __( 'Favorite',  'bbpress' ),
     665            'favorited' => __( 'Favorited', 'bbpress' ),
     666            'user_id'   => 0,
     667            'topic_id'  => 0,
     668            'before'    => '',
     669            'after'     => ''
     670        ), 'get_user_favorites_link' );
     671
    658672        // Validate user and topic ID's
    659         $user_id  = bbp_get_user_id( $user_id, true, true );
    660         $topic_id = bbp_get_topic_id( $topic_id );
    661         if ( empty( $user_id ) || empty( $topic_id ) )
     673        $user_id  = bbp_get_user_id( $r['user_id'], true, true );
     674        $topic_id = bbp_get_topic_id( $r['topic_id'] );
     675        if ( empty( $user_id ) || empty( $topic_id ) ) {
    662676            return false;
    663 
    664         if ( !current_user_can( 'edit_user', (int) $user_id ) )
     677        }
     678
     679        // No link if you can't edit yourself
     680        if ( !current_user_can( 'edit_user', (int) $user_id ) ) {
    665681            return false;
    666 
    667         if ( empty( $add ) || !is_array( $add ) ) {
    668             $add = array(
    669                 'mid'  => __( 'Add this topic to your favorites', 'bbpress' ),
    670                 'post' => __( ' (%?%)', 'bbpress' )
    671             );
    672         }
    673 
    674         if ( empty( $rem ) || !is_array( $rem ) ) {
    675             $rem = array(
    676                 'pre'  => __( 'This topic is one of your %favorites% [', 'bbpress' ),
    677                 'mid'  => __( '&times;', 'bbpress' ),
    678                 'post' => __( ']', 'bbpress' )
    679             );
    680         }
    681 
     682        }
     683
     684        // Decine which link to show
    682685        $is_fav = bbp_is_user_favorite( $user_id, $topic_id );
    683686        if ( !empty( $is_fav ) ) {
    684             $url   = esc_url( bbp_get_favorites_permalink( $user_id ) );
    685             $rem   = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $rem );
    686             $favs  = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
    687             $pre   = ( is_array( $rem ) && isset( $rem['pre']  ) ) ? $rem['pre']  : '';
    688             $mid   = ( is_array( $rem ) && isset( $rem['mid']  ) ) ? $rem['mid']  : ( is_string( $rem ) ? $rem : '' );
    689             $_post = ( is_array( $rem ) && isset( $rem['post'] ) ) ? $rem['post'] : '';
     687            $text       = $r['favorited'];
     688            $query_args = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
    690689        } else {
    691             $url   = esc_url( bbp_get_favorites_permalink( $user_id ) );
    692             $add   = preg_replace( '|%(.+)%|', "<a href='$url'>$1</a>", $add );
    693             $favs  = array( 'action' => 'bbp_favorite_add', 'topic_id' => $topic_id );
    694             $pre   = ( is_array( $add ) && isset( $add['pre']  ) ) ? $add['pre']  : '';
    695             $mid   = ( is_array( $add ) && isset( $add['mid']  ) ) ? $add['mid']  : ( is_string( $add ) ? $add : '' );
    696             $_post = ( is_array( $add ) && isset( $add['post'] ) ) ? $add['post'] : '';
     690            $text       = $r['favorite'];
     691            $query_args = array( 'action' => 'bbp_favorite_add',    'topic_id' => $topic_id );
    697692        }
    698693
     
    707702        }
    708703
    709         $url  = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) );
    710         $fav  = $is_fav ? 'is-favorite' : '';
    711         $html = sprintf( '<span id="favorite-%d" class="%s">%s<a href="%s" class="favorite-toggle" data-topic="%d" >%s</a>%s</span>', $topic_id, $fav, $pre, $url, $topic_id, $mid, $_post );
     704        $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-favorite_' . $topic_id ) );
     705        $sub  = $is_fav ? ' class="is-favorite"' : '';
     706        $html = sprintf( '%s<span id="favorite-%d"  %s><a href="%s" class="favorite-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] );
    712707
    713708        // Initial output is wrapped in a span, ajax output is hooked to this
     
    717712
    718713        // Return the link
    719         return apply_filters( 'bbp_get_user_favorites_link', $html, $add, $rem, $user_id, $topic_id );
     714        return apply_filters( 'bbp_get_user_favorites_link', $html, $r, $user_id, $topic_id );
    720715    }
    721716
     
    787782 *
    788783 * @param mixed $args See {@link bbp_get_user_subscribe_link()}
     784 * @param int $user_id Optional. User id
     785 * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
    789786 * @uses bbp_get_user_subscribe_link() To get the subscribe link
    790787 */
    791 function bbp_user_subscribe_link( $args = '' ) {
    792     echo bbp_get_user_subscribe_link( $args );
     788function bbp_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) {
     789    echo bbp_get_user_subscribe_link( $args, $user_id, $wrap );
    793790}
    794791    /**
     
    805802     *  - after: After the link
    806803     * @param int $user_id Optional. User id
    807      * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">.
     804     * @param bool $wrap Optional. If you want to wrap the link in <span id="subscription-toggle">.
    808805     * @uses bbp_get_user_id() To get the user id
    809806     * @uses current_user_can() To check if the current user can edit user
     
    865862        $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
    866863        $sub  = $is_subscribed ? ' class="is_subscribed"' : '';
    867         $html = sprintf( '%s <span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>' . $r['after'] . '</span>', $r['before'], $topic_id, $sub, $url, $topic_id, $text );
     864        $html = sprintf( '%s<span id="subscribe-%d"  %s><a href="%s" class="subscription-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] );
    868865
    869866        // Initial output is wrapped in a span, ajax output is hooked to this
  • trunk/templates/default/bbpress-functions.php

    r4576 r4615  
    301301        }
    302302
     303        // Put subscription attributes in convenient array
     304        $attrs = array(
     305            'topic_id' => $topic->ID,
     306            'user_id'  => $user_id
     307        );
     308
    303309        // Action succeeded
    304         bbp_ajax_response( true, bbp_get_user_favorites_link( array(), array(), $user_id, $id, false ), 200 );
     310        bbp_ajax_response( true, bbp_get_user_favorites_link( $attrs, $user_id, false ), 200 );
    305311    }
    306312
  • trunk/templates/default/bbpress/loop-single-topic.php

    r4594 r4615  
    2222                    <?php do_action( 'bbp_theme_before_topic_favorites_action' ); ?>
    2323
    24                     <?php bbp_user_favorites_link( array( 'mid' => '+', 'post' => '' ), array( 'pre' => '', 'mid' => '&times;', 'post' => '' ) ); ?>
     24                    <?php bbp_user_favorites_link( array( 'before' => '', 'favorite' => '+', 'favorited' => '&times;' ) ); ?>
    2525
    2626                    <?php do_action( 'bbp_theme_after_topic_favorites_action' ); ?>
Note: See TracChangeset for help on using the changeset viewer.