Skip to:
Content

bbPress.org

Changeset 4543


Ignore:
Timestamp:
12/05/2012 09:02:01 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Subscriptions/Favorites:

  • Improvements to AJAX handling.
  • Introduces bbp_ajax_response() function in common/functions.php, to handle JSON output.
  • Adds AJAX failure responses.
  • Props MZAWeb.
  • Fixes #1905.
Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/common/functions.php

    r4508 r4543  
    16881688    $wp_query->set_404();
    16891689}
     1690
     1691/**
     1692 * Helper method to return JSON response for the ajax calls
     1693 *
     1694 * @since bbPress (r4542)
     1695 *
     1696 * @param bool $success
     1697 * @param string $content
     1698 * @param array $extras
     1699 */
     1700function bbp_ajax_response( $success = false, $content = '', $status = -1, $extras = array() ) {
     1701
     1702    // Set status to 200 if setting response as successful
     1703    if ( ( true === $success ) && ( -1 === $status ) )
     1704        $status = 200;
     1705
     1706    // Setup the response array
     1707    $response = array(
     1708        'success' => $success,
     1709        'status'  => $status,
     1710        'content' => $content
     1711    );
     1712
     1713    // Merge extra response parameters in
     1714    if ( !empty( $extras ) && is_array( $extras ) ) {
     1715        $response = array_merge( $response, $extras );
     1716    }
     1717
     1718    // Send back the JSON
     1719    header( 'Content-type: application/json' );
     1720    echo json_encode( $response );
     1721    die();
     1722}
  • trunk/includes/users/template-tags.php

    r4542 r4543  
    640640     * @param int $user_id Optional. User id
    641641     * @param int $topic_id Optional. Topic id
     642     * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">. See ajax_favorite()
    642643     * @uses bbp_get_user_id() To get the user id
    643644     * @uses current_user_can() If the current user can edit the user
     
    651652     * @return string User favorites link
    652653     */
    653     function bbp_get_user_favorites_link( $add = array(), $rem = array(), $user_id = 0, $topic_id = 0 ) {
     654    function bbp_get_user_favorites_link( $add = array(), $rem = array(), $user_id = 0, $topic_id = 0, $wrap = true ) {
    654655        if ( !bbp_is_favorites_active() )
    655656            return false;
     
    704705        } elseif ( is_singular( bbp_get_reply_post_type() ) ) {
    705706            $permalink = bbp_get_topic_permalink( $topic_id );
    706         } elseif ( bbp_is_query_name( 'bbp_single_topic' ) ) {
     707        } else {
    707708            $permalink = get_permalink();
    708709        }
    709710
    710         $url    = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) );
    711         $is_fav = $is_fav ? 'is-favorite' : '';
    712         $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>';
     711        $url  = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) );
     712        $fav  = $is_fav ? 'is-favorite' : '';
     713        $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 );
     714
     715        // Initial output is wrapped in a span, ajax output is hooked to this
     716        if ( !empty( $wrap ) ) {
     717            $html = '<span id="favorite-toggle">' . $html . '</span>';
     718        }
    713719
    714720        // Return the link
     
    801807     *  - after: After the link
    802808     * @param int $user_id Optional. User id
     809     * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">.
    803810     * @uses bbp_get_user_id() To get the user id
    804811     * @uses current_user_can() To check if the current user can edit user
     
    812819     * @return string Permanent link to topic
    813820     */
    814     function bbp_get_user_subscribe_link( $args = '', $user_id = 0 ) {
     821    function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) {
    815822        if ( !bbp_is_subscriptions_active() )
    816823            return;
     
    856863        } elseif ( is_singular( bbp_get_reply_post_type() ) ) {
    857864            $permalink = bbp_get_topic_permalink( $topic_id );
    858         } elseif ( bbp_is_query_name( 'bbp_single_topic' ) ) {
     865        } else {
    859866            $permalink = get_permalink();
    860867        }
    861868
    862         $url        = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
    863         $subscribed = $is_subscribed ? ' class="is_subscribed"' : '';
    864         $html       = '<span id="subscription-toggle">' . $r['before'] . '<span id="subscribe-' . $topic_id . '"' . $subscribed . '><a href="' . $url . '" class="dim:subscription-toggle:subscribe-' . $topic_id . ':is-subscribed">' . $text . '</a></span>' . $r['after'] . '</span>';
     869        $url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) );
     870        $sub  = $is_subscribed ? ' class="is_subscribed"' : '';
     871        $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 );
     872
     873        // Initial output is wrapped in a span, ajax output is hooked to this
     874        if ( !empty( $wrap ) ) {
     875            $html = '<span id="subscription-toggle">' . $html . '</span>';
     876        }
    865877
    866878        // Return the link
  • trunk/templates/default/bbpress-functions.php

    r4513 r4543  
    8585        /** Scripts ***********************************************************/
    8686
    87         add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_styles'        ) ); // Enqueue theme CSS
    88         add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_scripts'       ) ); // Enqueue theme JS
    89         add_filter( 'bbp_enqueue_scripts',      array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization
    90         add_action( 'bbp_head',                 array( $this, 'head_scripts'          ) ); // Output some extra JS in the <head>
    91         add_action( 'wp_ajax_dim-favorite',     array( $this, 'ajax_favorite'         ) ); // Handles the ajax favorite/unfavorite
    92         add_action( 'wp_ajax_dim-subscription', array( $this, 'ajax_subscription'     ) ); // Handles the ajax subscribe/unsubscribe
     87        add_action( 'bbp_enqueue_scripts',             array( $this, 'enqueue_styles'        ) ); // Enqueue theme CSS
     88        add_action( 'bbp_enqueue_scripts',             array( $this, 'enqueue_scripts'       ) ); // Enqueue theme JS
     89        add_filter( 'bbp_enqueue_scripts',             array( $this, 'localize_topic_script' ) ); // Enqueue theme script localization
     90        add_action( 'bbp_head',                        array( $this, 'head_scripts'          ) ); // Output some extra JS in the <head>
     91        add_action( 'wp_ajax_dim-favorite',            array( $this, 'ajax_favorite'         ) ); // Handles the ajax favorite/unfavorite
     92        add_action( 'wp_ajax_dim-subscription',        array( $this, 'ajax_subscription'     ) ); // Handles the ajax subscribe/unsubscribe
     93        add_action( 'wp_ajax_nopriv_dim-favorite',     array( $this, 'ajax_favorite'         ) ); // Handles the ajax favorite/unfavorite
     94        add_action( 'wp_ajax_nopriv_dim-subscription', array( $this, 'ajax_subscription'     ) ); // Handles the ajax subscribe/unsubscribe
    9395
    9496        /** Template Wrappers *************************************************/
     
    175177    public function enqueue_scripts() {
    176178
    177         if ( bbp_is_single_topic() )
    178             wp_enqueue_script( 'bbpress-topic', $this->url . 'js/topic.js', array( 'wp-lists' ), $this->version, true );
    179 
    180         elseif ( bbp_is_single_user_edit() )
     179        // Topic favorite/subscribe
     180        if ( bbp_is_single_topic() ) {
     181            wp_enqueue_script( 'bbpress-topic', $this->url . 'js/topic.js', array( 'jquery' ), $this->version, true );
     182        }
     183
     184        // User Profile edit
     185        if ( bbp_is_single_user_edit() ) {
    181186            wp_enqueue_script( 'user-profile' );
     187        }
    182188    }
    183189
     
    203209            }
    204210            <?php endif; ?>
    205            
     211
    206212            <?php if ( bbp_use_wp_editor() ) : ?>
    207213            jQuery(document).ready( function() {
     
    257263            return;
    258264
    259         // Bail if user is not logged in
    260         if ( !is_user_logged_in() )
    261             return;
    262 
    263         $user_id = bbp_get_current_user_id();
    264 
    265         $localizations = array(
    266             'currentUserId' => $user_id,
    267             'topicId'       => bbp_get_topic_id(),
    268         );
    269 
    270         // Favorites
    271         if ( bbp_is_favorites_active() ) {
    272             $localizations['favoritesActive'] = 1;
    273             $localizations['favoritesLink']   = bbp_get_favorites_permalink( $user_id );
    274             $localizations['isFav']           = (int) bbp_is_user_favorite( $user_id );
    275             $localizations['favLinkYes']      = __( 'favorites',                                         'bbpress' );
    276             $localizations['favLinkNo']       = __( '?',                                                 'bbpress' );
    277             $localizations['favYes']          = __( 'This topic is one of your %favLinkYes% [%favDel%]', 'bbpress' );
    278             $localizations['favNo']           = __( '%favAdd% (%favLinkNo%)',                            'bbpress' );
    279             $localizations['favDel']          = __( '&times;',                                           'bbpress' );
    280             $localizations['favAdd']          = __( 'Add this topic to your favorites',                  'bbpress' );
    281         } else {
    282             $localizations['favoritesActive'] = 0;
    283         }
    284 
    285         // Subscriptions
    286         if ( bbp_is_subscriptions_active() ) {
    287             $localizations['subsActive']   = 1;
    288             $localizations['isSubscribed'] = (int) bbp_is_user_subscribed( $user_id );
    289             $localizations['subsSub']      = __( 'Subscribe',   'bbpress' );
    290             $localizations['subsUns']      = __( 'Unsubscribe', 'bbpress' );
    291             $localizations['subsLink']     = bbp_get_topic_permalink();
    292         } else {
    293             $localizations['subsActive'] = 0;
    294         }
    295 
    296         wp_localize_script( 'bbpress-topic', 'bbpTopicJS', $localizations );
    297     }
    298 
    299     /**
    300      * Add or remove a topic from a user's favorites
     265        wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
     266            'ajaxurl'            => admin_url( 'admin-ajax.php', ( is_ssl() ? 'https' : 'http' ) ),
     267            'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
     268            'is_user_logged_in'  => is_user_logged_in(),
     269            'fav_nonce'          => wp_create_nonce( 'toggle-favorite_' .     get_the_ID() ),
     270            'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
     271        ) );
     272    }
     273
     274    /**
     275     * AJAX handler to add or remove a topic from a user's favorites
    301276     *
    302277     * @since bbPress (r3732)
     
    305280     * @uses current_user_can() To check if the current user can edit the user
    306281     * @uses bbp_get_topic() To get the topic
    307      * @uses check_ajax_referer() To verify the nonce & check the referer
     282     * @uses wp_verify_nonce() To verify the nonce & check the referer
    308283     * @uses bbp_is_user_favorite() To check if the topic is user's favorite
    309284     * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
    310285     * @uses bbp_add_user_favorite() To add the topic from user's favorites
     286     * @uses bbp_ajax_response() To return JSON
    311287     */
    312288    public function ajax_favorite() {
     289
     290        // Bail if favorites are not active
     291        if ( ! bbp_is_favorites_active() ) {
     292            bbp_ajax_response( false, __( 'Favorites are no longer active.', 'bbpress' ), 300 );
     293        }
     294
     295        // Bail if user is not logged in
     296        if ( !is_user_logged_in() ) {
     297            bbp_ajax_response( false, __( 'Please login to make this topic a favorite.', 'bbpress' ), 301 );
     298        }
     299
     300        // Get user and topic data
    313301        $user_id = bbp_get_current_user_id();
    314         $id      = intval( $_POST['id'] );
    315 
    316         if ( !current_user_can( 'edit_user', $user_id ) )
    317             die( '-1' );
    318 
     302        $id      = !empty( $_POST['id'] ) ? intval( $_POST['id'] ) : 0;
     303
     304        // Bail if user cannot add favorites for this user
     305        if ( !current_user_can( 'edit_user', $user_id ) ) {
     306            bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
     307        }
     308
     309        // Get the topic
    319310        $topic = bbp_get_topic( $id );
    320311
    321         if ( empty( $topic ) )
    322             die( '0' );
    323 
    324         check_ajax_referer( 'toggle-favorite_' . $topic->ID );
    325 
    326         if ( bbp_is_user_favorite( $user_id, $topic->ID ) ) {
    327             if ( bbp_remove_user_favorite( $user_id, $topic->ID ) ) {
    328                 die( '1' );
    329             }
    330         } else {
    331             if ( bbp_add_user_favorite( $user_id, $topic->ID ) ) {
    332                 die( '1' );
    333             }
    334         }
    335 
    336         die( '0' );
    337     }
    338 
    339     /**
    340      * Subscribe/Unsubscribe a user from a topic
     312        // Bail if topic cannot be found
     313        if ( empty( $topic ) ) {
     314            bbp_ajax_response( false, __( 'The topic could not be found.', 'bbpress' ), 303 );
     315        }
     316
     317        // Bail if user did not take this action
     318        if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) {
     319            bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
     320        }
     321
     322        // Take action
     323        $status = bbp_is_user_favorite( $user_id, $topic->ID ) ? bbp_remove_user_favorite( $user_id, $topic->ID ) : bbp_add_user_favorite( $user_id, $topic->ID );
     324
     325        // Bail if action failed
     326        if ( empty( $status ) ) {
     327            bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
     328        }
     329
     330        // Action succeeded
     331        bbp_ajax_response( true, bbp_get_user_favorites_link( array(), array(), $user_id, $id, false ), 200 );
     332    }
     333
     334    /**
     335     * AJAX handler to Subscribe/Unsubscribe a user from a topic
    341336     *
    342337     * @since bbPress (r3732)
     
    346341     * @uses current_user_can() To check if the current user can edit the user
    347342     * @uses bbp_get_topic() To get the topic
    348      * @uses check_ajax_referer() To verify the nonce & check the referer
    349      * @uses bbp_is_user_subscribed() To check if the topic is in user's
    350      *                                 subscriptions
    351      * @uses bbp_remove_user_subscriptions() To remove the topic from user's
    352      *                                        subscriptions
     343     * @uses wp_verify_nonce() To verify the nonce
     344     * @uses bbp_is_user_subscribed() To check if the topic is in user's subscriptions
     345     * @uses bbp_remove_user_subscriptions() To remove the topic from user's subscriptions
    353346     * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions
     347     * @uses bbp_ajax_response() To return JSON
    354348     */
    355349    public function ajax_subscription() {
    356         if ( !bbp_is_subscriptions_active() )
    357             return;
    358 
     350
     351        // Bail if subscriptions are not active
     352        if ( !bbp_is_subscriptions_active() ) {
     353            bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
     354        }
     355
     356        // Bail if user is not logged in
     357        if ( !is_user_logged_in() ) {
     358            bbp_ajax_response( false, __( 'Please login to subscribe to this topic.', 'bbpress' ), 301 );
     359        }
     360
     361        // Get user and topic data
    359362        $user_id = bbp_get_current_user_id();
    360363        $id      = intval( $_POST['id'] );
    361364
    362         if ( !current_user_can( 'edit_user', $user_id ) )
    363             die( '-1' );
    364 
     365        // Bail if user cannot add favorites for this user
     366        if ( !current_user_can( 'edit_user', $user_id ) ) {
     367            bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
     368        }
     369
     370        // Get the topic
    365371        $topic = bbp_get_topic( $id );
    366372
    367         if ( empty( $topic ) )
    368             die( '0' );
    369 
    370         check_ajax_referer( 'toggle-subscription_' . $topic->ID );
    371 
    372         if ( bbp_is_user_subscribed( $user_id, $topic->ID ) ) {
    373             if ( bbp_remove_user_subscription( $user_id, $topic->ID ) ) {
    374                 die( '1' );
    375             }
    376         } else {
    377             if ( bbp_add_user_subscription( $user_id, $topic->ID ) ) {
    378                 die( '1' );
    379             }
    380         }
    381 
    382         die( '0' );
     373        // Bail if topic cannot be found
     374        if ( empty( $topic ) ) {
     375            bbp_ajax_response( false, __( 'The topic could not be found.', 'bbpress' ), 303 );
     376        }
     377
     378        // Bail if user did not take this action
     379        if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) {
     380            bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
     381        }
     382
     383        // Take action
     384        $status = bbp_is_user_subscribed( $user_id, $topic->ID ) ? bbp_remove_user_subscription( $user_id, $topic->ID ) : bbp_add_user_subscription( $user_id, $topic->ID );
     385
     386        // Bail if action failed
     387        if ( empty( $status ) ) {
     388            bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
     389        }
     390
     391        // Put subscription attributes in convenient array
     392        $attrs = array(
     393            'topic_id' => $topic->ID,
     394            'user_id'  => $user_id
     395        );
     396
     397        // Action succeeded
     398        bbp_ajax_response( true, bbp_get_user_subscribe_link( $attrs, $user_id, false ), 200 );
    383399    }
    384400}
  • trunk/templates/default/js/topic.js

    r4511 r4543  
    1 bbpTopicJS = jQuery.extend( {
    2     // User and Topic
    3     currentUserId: '0',
    4     topicId: '0',
     1jQuery( document ).ready( function ( $ ) {
    52
    6     // Favorites
    7     favoritesLink: '',
    8     favoritesActive: 0,
    9     isFav: 0,
    10     favLinkYes: 'favorites',
    11     favLinkNo: '?',
    12     favYes: 'This topic is one of your %favLinkYes% [%favDel%]',
    13     favNo: '%favAdd% (%favLinkNo%)',
    14     favDel: 'x',
    15     favAdd: 'Add this topic to your favorites',
     3    function bbp_ajax_call( action, topic_id, nonce, update_selector ) {
     4        var $data = {
     5            action : action,
     6            id     : topic_id,
     7            nonce  : nonce
     8        };
    169
    17     // Subscriptions
    18     subsLink: '',
    19     subsActive: 0,
    20     isSubscribed: 0,
    21     subsSub: 'Subscribe',
    22     subsUns: 'Unsubscribe'
    23 }, bbpTopicJS );
    24 
    25 // Topic Global
    26 bbpTopicJS.favoritesActive = parseInt( bbpTopicJS.favoritesActive );
    27 bbpTopicJS.isFav           = parseInt( bbpTopicJS.isFav );
    28 bbpTopicJS.subsActive      = parseInt( bbpTopicJS.subsActive );
    29 bbpTopicJS.isSubscribed    = parseInt( bbpTopicJS.isSubscribed );
    30 
    31 // Run it
    32 jQuery(document).ready( function() {
    33 
    34     /** Favorites *************************************************************/
    35 
    36     if ( 1 == bbpTopicJS.favoritesActive ) {
    37         var favoritesToggle = jQuery( '#favorite-toggle' )
    38             .addClass( 'list:favorite' )
    39             .wpList( { alt: '', dimAfter: favLinkSetup } );
    40 
    41         var favoritesToggleSpan = favoritesToggle.children( 'span' )
    42             [bbpTopicJS.isFav ? 'addClass' : 'removeClass' ]( 'is-favorite' );
     10        $.post( bbpTopicJS.ajaxurl, $data, function ( response ) {
     11            if ( response.success ) {
     12                $( update_selector ).html( response.content );
     13            } else {
     14                if ( !response.content ) {
     15                    response.content = bbpTopicJS.generic_ajax_error;
     16                }
     17                alert( response.content );
     18            }
     19        } );
    4320    }
    4421
    45     function favLinkSetup() {
    46         bbpTopicJS.isFav = favoritesToggleSpan.is( '.is-favorite' );
    47         var aLink = "<a href='" + bbpTopicJS.favoritesLink + "'>";
    48         var aDim  = "<a href='" + favoritesToggleSpan.find( 'a[class^="dim:"]' ).attr( 'href' ) + "' class='dim:favorite-toggle:" + favoritesToggleSpan.attr( 'id' ) + ":is-favorite'>";
    49         if ( bbpTopicJS.isFav ) {
    50             html = bbpTopicJS.favYes
    51                 .replace( /%favLinkYes%/, aLink + bbpTopicJS.favLinkYes + "</a>" )
    52                 .replace( /%favDel%/, aDim + bbpTopicJS.favDel + "</a>" );
    53         } else {
    54             html = bbpTopicJS.favNo
    55                 .replace( /%favLinkNo%/, aLink + bbpTopicJS.favLinkNo + "</a>" )
    56                 .replace( /%favAdd%/, aDim + bbpTopicJS.favAdd + "</a>" );
    57         }
    58         favoritesToggleSpan.html( html );
    59         favoritesToggle.get(0).wpList.process( favoritesToggle );
    60     }
     22    $( '#favorite-toggle' ).on( 'click', 'span a.favorite-toggle', function( e ) {
     23        e.preventDefault();
     24        bbp_ajax_call( 'dim-favorite', $( this ).attr( 'data-topic' ), bbpTopicJS.fav_nonce, '#favorite-toggle' );
     25    } );
    6126
    62     /** Subscriptions *********************************************************/
    63 
    64     if ( 1 == bbpTopicJS.subsActive ) {
    65         var subscriptionToggle = jQuery( '#subscription-toggle' )
    66             .addClass( 'list:subscription' )
    67             .wpList( { alt: '', dimAfter: subsLinkSetup } );
    68 
    69         var subscriptionToggleSpan = subscriptionToggle.children( 'span' )
    70             [bbpTopicJS.isSubscribed ? 'addClass' : 'removeClass' ]( 'is-subscribed' );
    71     }
    72 
    73     function subsLinkSetup() {
    74         bbpTopicJS.isSubscribed = subscriptionToggleSpan.is( '.is-subscribed' );
    75         var aLink = "<a href='" + bbpTopicJS.subsLink + "'>";
    76         var aDim  = "<a href='" + subscriptionToggleSpan.find( 'a[class^="dim:"]' ).attr( 'href' ) + "' class='dim:subscription-toggle:" + subscriptionToggleSpan.attr( 'id' ) + ":is-subscribed'>";
    77 
    78         if ( bbpTopicJS.isSubscribed ) {
    79             html = aDim + bbpTopicJS.subsUns + '</a>';
    80         } else {
    81             html = aDim + bbpTopicJS.subsSub + '</a>';
    82         }
    83 
    84         subscriptionToggleSpan.html( html );
    85     }
     27    $( '#subscription-toggle' ).on( 'click', 'span a.subscription-toggle', function( e ) {
     28        e.preventDefault();
     29        bbp_ajax_call( 'dim-subscription', $( this ).attr( 'data-topic' ), bbpTopicJS.subs_nonce, '#subscription-toggle' );
     30    } );
    8631} );
  • trunk/templates/default/js/topic.min.js

    r4514 r4543  
    1 bbpTopicJS=jQuery.extend({currentUserId:"0",topicId:"0",favoritesLink:"",favoritesActive:0,isFav:0,favLinkYes:"favorites",favLinkNo:"?",favYes:"This topic is one of your %favLinkYes% [%favDel%]",favNo:"%favAdd% (%favLinkNo%)",favDel:"x",favAdd:"Add this topic to your favorites",subsLink:"",subsActive:0,isSubscribed:0,subsSub:"Subscribe",subsUns:"Unsubscribe"},bbpTopicJS);bbpTopicJS.favoritesActive=parseInt(bbpTopicJS.favoritesActive);bbpTopicJS.isFav=parseInt(bbpTopicJS.isFav);bbpTopicJS.subsActive=parseInt(bbpTopicJS.subsActive);bbpTopicJS.isSubscribed=parseInt(bbpTopicJS.isSubscribed);jQuery(document).ready(function(){if(1==bbpTopicJS.favoritesActive){var e=jQuery("#favorite-toggle").addClass("list:favorite").wpList({alt:"",dimAfter:d});var f=e.children("span")[bbpTopicJS.isFav?"addClass":"removeClass"]("is-favorite")}function d(){bbpTopicJS.isFav=f.is(".is-favorite");var g="<a href='"+bbpTopicJS.favoritesLink+"'>";var h="<a href='"+f.find('a[class^="dim:"]').attr("href")+"' class='dim:favorite-toggle:"+f.attr("id")+":is-favorite'>";if(bbpTopicJS.isFav){html=bbpTopicJS.favYes.replace(/%favLinkYes%/,g+bbpTopicJS.favLinkYes+"</a>").replace(/%favDel%/,h+bbpTopicJS.favDel+"</a>")}else{html=bbpTopicJS.favNo.replace(/%favLinkNo%/,g+bbpTopicJS.favLinkNo+"</a>").replace(/%favAdd%/,h+bbpTopicJS.favAdd+"</a>")}f.html(html);e.get(0).wpList.process(e)}if(1==bbpTopicJS.subsActive){var b=jQuery("#subscription-toggle").addClass("list:subscription").wpList({alt:"",dimAfter:c});var a=b.children("span")[bbpTopicJS.isSubscribed?"addClass":"removeClass"]("is-subscribed")}function c(){bbpTopicJS.isSubscribed=a.is(".is-subscribed");var g="<a href='"+bbpTopicJS.subsLink+"'>";var h="<a href='"+a.find('a[class^="dim:"]').attr("href")+"' class='dim:subscription-toggle:"+a.attr("id")+":is-subscribed'>";if(bbpTopicJS.isSubscribed){html=h+bbpTopicJS.subsUns+"</a>"}else{html=h+bbpTopicJS.subsSub+"</a>"}a.html(html)}});
     1jQuery(document).ready(function(b){function a(g,d,f,c){var e={action:g,id:d,nonce:f};b.post(bbpTopicJS.ajaxurl,e,function(h){if(h.success){b(c).html(h.content)}else{if(!h.content){h.content=bbpTopicJS.generic_ajax_error}alert(h.content)}})}b("#favorite-toggle").on("click","span a.favorite-toggle",function(c){c.preventDefault();a("dim-favorite",b(this).attr("data-topic"),bbpTopicJS.fav_nonce,"#favorite-toggle")});b("#subscription-toggle").on("click","span a.subscription-toggle",function(c){c.preventDefault();a("dim-subscription",b(this).attr("data-topic"),bbpTopicJS.subs_nonce,"#subscription-toggle")})});
Note: See TracChangeset for help on using the changeset viewer.