Skip to:
Content

bbPress.org


Ignore:
Timestamp:
12/05/2012 09:02:01 PM (14 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.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.