Ticket #1905: 1905.diff
File 1905.diff, 20.6 KB (added by , 12 years ago) |
---|
-
templates/default/js/topic.js
1 bbpTopicJS = jQuery.extend( { 2 // User and Topic 3 currentUserId: '0', 4 topicId: '0', 1 jQuery( document ).ready( function ( $ ) { 5 2 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 ) { 16 4 17 // Subscriptions 18 subsLink: '', 19 subsActive: 0, 20 isSubscribed: 0, 21 subsSub: 'Subscribe', 22 subsUns: 'Unsubscribe' 23 }, bbpTopicJS ); 5 var $data = { 6 action:action, 7 id :topic_id, 8 nonce :nonce 9 }; 24 10 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' ); 43 } 44 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>" ); 11 $.post( bbpTopicJS.ajaxurl, $data, function ( response ) { 12 if ( response.success ) { 13 $( update_selector ).html( response.content ); 53 14 } else { 54 html = bbpTopicJS.favNo55 .replace( /%favLinkNo%/, aLink + bbpTopicJS.favLinkNo + "</a>" )56 .replace( /%favAdd%/, aDim + bbpTopicJS.favAdd + "</a>");15 if ( !response.content ) 16 response.content = ''; 17 alert( response.content + ' ' + bbpTopicJS.generic_ajax_error ); 57 18 } 58 favoritesToggleSpan.html( html ); 59 favoritesToggle.get(0).wpList.process( favoritesToggle ); 19 } ); 60 20 } 61 21 62 /** Subscriptions *********************************************************/ 22 $( 'span#favorite-toggle' ).on( 'click', 'span a.favorite-toggle', function ( e ) { 23 e.preventDefault(); 24 var topic_id = $( this ).attr( 'data-topic' ); 63 25 64 if ( 1 == bbpTopicJS.subsActive ) { 65 var subscriptionToggle = jQuery( '#subscription-toggle' ) 66 .addClass( 'list:subscription' ) 67 .wpList( { alt: '', dimAfter: subsLinkSetup } ); 26 bbp_ajax_call( 'dim-favorite', topic_id, bbpTopicJS.fav_nonce, 'span#favorite-toggle' ); 68 27 69 var subscriptionToggleSpan = subscriptionToggle.children( 'span' ) 70 [bbpTopicJS.isSubscribed ? 'addClass' : 'removeClass' ]( 'is-subscribed' ); 71 } 28 } ); 72 29 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'>"; 30 $( 'span#subscription-toggle' ).on( 'click', 'span a.subscription-toggle', function ( e ) { 31 e.preventDefault(); 32 var topic_id = $( this ).attr( 'data-topic' ); 77 33 78 if ( bbpTopicJS.isSubscribed ) { 79 html = aDim + bbpTopicJS.subsUns + '</a>'; 80 } else { 81 html = aDim + bbpTopicJS.subsSub + '</a>'; 82 } 34 bbp_ajax_call( 'dim-subscription', topic_id, bbpTopicJS.subs_nonce, 'span#subscription-toggle' ); 83 35 84 subscriptionToggleSpan.html( html );85 }86 36 } ); 37 38 } ); -
templates/default/js/topic.min.js
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)}});2 No newline at end of file 1 jQuery(document).ready(function($){function bbp_ajax_call(action,topic_id,nonce,update_selector){var $data={action:action,id:topic_id,nonce:nonce};$.post(bbpTopicJS.ajaxurl,$data,function(response){if(response.success){$(update_selector).html(response.content)}else{if(!response.content)response.content='';alert(response.content+' '+bbpTopicJS.generic_ajax_error)}})}$('span#favorite-toggle').on('click','span a.favorite-toggle',function(e){e.preventDefault();var topic_id=$(this).attr('data-topic');bbp_ajax_call('dim-favorite',topic_id,bbpTopicJS.fav_nonce,'span#favorite-toggle')});$('span#subscription-toggle').on('click','span a.subscription-toggle',function(e){e.preventDefault();var topic_id=$(this).attr('data-topic');bbp_ajax_call('dim-subscription',topic_id,bbpTopicJS.subs_nonce,'span#subscription-toggle')})}); 2 No newline at end of file -
templates/default/bbpress-functions.php
91 91 add_action( 'wp_ajax_dim-favorite', array( $this, 'ajax_favorite' ) ); // Handles the ajax favorite/unfavorite 92 92 add_action( 'wp_ajax_dim-subscription', array( $this, 'ajax_subscription' ) ); // Handles the ajax subscribe/unsubscribe 93 93 94 /* 95 * The subscription and favorite functionality is only available 96 * for logged in users, but if the user loses his session whithout 97 * refreshing the page, we want to show him a nice error message for 98 * the ajax call, instead of failing sillently. So we add the _nopriv_ 99 * ajax handlers, and deal with !is_user_logged_in in the ajax handler 100 */ 101 add_action( 'wp_ajax_nopriv_dim-favorite', array( $this, 'ajax_favorite' ) ); // Handles the ajax favorite/unfavorite 102 add_action( 'wp_ajax_nopriv_dim-subscription', array( $this, 'ajax_subscription' ) ); // Handles the ajax subscribe/unsubscribe 103 94 104 /** Template Wrappers *************************************************/ 95 105 96 106 add_action( 'bbp_before_main_content', array( $this, 'before_main_content' ) ); // Top wrapper HTML … … 174 184 */ 175 185 public function enqueue_scripts() { 176 186 177 if ( bbp_is_single_topic() ) 178 wp_enqueue_script( 'bbpress-topic', $this->url . 'js/topic.js', array( 'wp-lists' ), $this->version, true ); 187 if ( bbp_is_single_topic() ) { 188 wp_enqueue_script( 'bbpress-topic', $this->url . 'js/topic.js', array( 'jquery' ), $this->version, true ); 189 } 179 190 180 elseif ( bbp_is_single_user_edit() )191 if ( bbp_is_single_user_edit() ) 181 192 wp_enqueue_script( 'user-profile' ); 182 193 } 183 194 … … 256 267 if ( !bbp_is_single_topic() ) 257 268 return; 258 269 259 // Bail if user is not logged in 260 if ( !is_user_logged_in() ) 261 return; 270 $localizations = array( 'ajaxurl' => admin_url( 'admin-ajax.php', ( is_ssl() ? 'https' : 'http' ) ), 271 'generic_ajax_error' => __( 'Please refresh the page and try again.', 'bbpress' ), 272 'is_user_logged_in' => is_user_logged_in(), 273 'fav_nonce' => wp_create_nonce( 'toggle-favorite_' . get_the_ID() ), 274 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) ); 262 275 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'] = __( '×', 'bbpress' ); 280 $localizations['favAdd'] = __( 'Add this topic to your favorites', 'bbpress' ); 281 } else { 282 $localizations['favoritesActive'] = 0; 276 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', $localizations ); 283 277 } 284 278 285 // Subscriptions286 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 279 296 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', $localizations );297 }298 299 280 /** 300 * A dd or remove a topic from a user's favorites281 * AJAX handler to add or remove a topic from a user's favorites 301 282 * 302 283 * @since bbPress (r3732) 303 284 * 304 285 * @uses bbp_get_current_user_id() To get the current user id 305 286 * @uses current_user_can() To check if the current user can edit the user 306 287 * @uses bbp_get_topic() To get the topic 307 * @uses check_ajax_referer()To verify the nonce & check the referer288 * @uses wp_verify_nonce() To verify the nonce & check the referer 308 289 * @uses bbp_is_user_favorite() To check if the topic is user's favorite 309 290 * @uses bbp_remove_user_favorite() To remove the topic from user's favorites 310 291 * @uses bbp_add_user_favorite() To add the topic from user's favorites 292 * @uses bbp_ajax_response() To return JSON 311 293 */ 312 294 public function ajax_favorite() { 295 if ( !bbp_is_favorites_active() ) 296 bbp_ajax_response( false, __( "Favorites are not active.", 'bbpress' ), 300 ); 297 298 if ( !is_user_logged_in() ) 299 bbp_ajax_response( false, __( "You're not logged in.", 'bbpress' ), 301 ); 300 313 301 $user_id = bbp_get_current_user_id(); 314 302 $id = intval( $_POST['id'] ); 315 303 316 304 if ( !current_user_can( 'edit_user', $user_id ) ) 317 die( '-1');305 bbp_ajax_response( false, __( "You don't have permission to do this.", 'bbpress' ), 302); 318 306 319 307 $topic = bbp_get_topic( $id ); 320 308 321 309 if ( empty( $topic ) ) 322 die( '0');310 bbp_ajax_response( false, __( "Invalid topic ID.", 'bbpress' ), 303 ); 323 311 324 check_ajax_referer( 'toggle-favorite_' . $topic->ID ); 312 if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) 313 bbp_ajax_response( false, __( "Invalid nonce.", 'bbpress' ), 304 ); 325 314 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 } 315 $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 ); 335 316 336 die( '0' ); 317 if ( $status ) 318 bbp_ajax_response( true, bbp_get_user_favorites_link( array(), array(), $user_id, $id, false ), 200 ); 319 320 321 bbp_ajax_response( false, __( "Unexpected error.", 'bbpress' ), 305 ); 322 337 323 } 338 324 339 325 /** 340 * Subscribe/Unsubscribe a user from a topic326 * AJAX Hanlder to Subscribe/Unsubscribe a user from a topic 341 327 * 342 328 * @since bbPress (r3732) 343 329 * … … 345 331 * @uses bbp_get_current_user_id() To get the current user id 346 332 * @uses current_user_can() To check if the current user can edit the user 347 333 * @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 334 * @uses wp_verify_nonce() To verify the nonce 335 * @uses bbp_is_user_subscribed() To check if the topic is in user's subscriptions 336 * @uses bbp_remove_user_subscriptions() To remove the topic from user's subscriptions 353 337 * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions 338 * @uses bbp_ajax_response() To return JSON 354 339 */ 355 340 public function ajax_subscription() { 356 341 if ( !bbp_is_subscriptions_active() ) 357 return;342 bbp_ajax_response( false, __( "Subscriptions are not active.", 'bbpress' ), 300 ); 358 343 344 if ( !is_user_logged_in() ) 345 bbp_ajax_response( false, __( "You're not logged in.", 'bbpress' ), 301 ); 346 359 347 $user_id = bbp_get_current_user_id(); 360 348 $id = intval( $_POST['id'] ); 361 349 362 350 if ( !current_user_can( 'edit_user', $user_id ) ) 363 die( '-1');351 bbp_ajax_response( false, __( "You don't have permission to do this.", 'bbpress' ), 302 ); 364 352 365 353 $topic = bbp_get_topic( $id ); 366 354 367 355 if ( empty( $topic ) ) 368 die( '0');356 bbp_ajax_response( false, __( "Invalid topic ID.", 'bbpress' ), 303 ); 369 357 370 check_ajax_referer( 'toggle-subscription_' . $topic->ID ); 358 if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) 359 bbp_ajax_response( false, __( "Invalid nonce.", 'bbpress' ), 304 ); 371 360 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 } 361 $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 ); 381 362 382 die( '0' ); 363 if ( $status ) 364 bbp_ajax_response( true, bbp_get_user_subscribe_link( array( 'topic_id' => $topic->ID, 365 'user_id' => $user_id ), $user_id, false ), 200 ); 366 367 bbp_ajax_response( false, __( "Unexpected error.", 'bbpress' ), 305 ); 368 383 369 } 384 370 } 385 371 new BBP_Default(); -
includes/common/functions.php
1687 1687 1688 1688 $wp_query->set_404(); 1689 1689 } 1690 1691 1692 /** 1693 * Helper method to return JSON response for the ajax calls 1694 * 1695 * @param bool $success 1696 * @param string $content 1697 * @param array $aditional_data 1698 */ 1699 function bbp_ajax_response( $success = false, $content = '', $status = -1, $aditional_data = array() ) { 1700 1701 if ( $success && $status === -1 ) 1702 $status = 200; 1703 1704 $response = array( 'success' => $success, 'status'=> $status, 'content'=> $content ); 1705 1706 if ( !empty( $aditional_data ) ) 1707 $response = array_merge( $response, $aditional_data ); 1708 1709 header( 'Content-type: application/json' ); 1710 echo json_encode( $response ); 1711 die(); 1712 } 1713 No newline at end of file -
includes/users/template-tags.php
639 639 * @param array $rem Optional. Remove from favorites args 640 640 * @param int $user_id Optional. User id 641 641 * @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() 642 643 * @uses bbp_get_user_id() To get the user id 643 644 * @uses current_user_can() If the current user can edit the user 644 645 * @uses bbp_get_topic_id() To get the topic id … … 650 651 * html, add args, remove args, user & topic id 651 652 * @return string User favorites link 652 653 */ 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 ) { 654 655 if ( !bbp_is_favorites_active() ) 655 656 return false; 656 657 … … 709 710 710 711 $url = esc_url( wp_nonce_url( add_query_arg( $favs, $permalink ), 'toggle-favorite_' . $topic_id ) ); 711 712 $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>';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, $is_fav, $pre, $url, $topic_id, $mid, $_post ); 713 714 715 if ( $wrap ) 716 $html = '<span id="favorite-toggle">' . $html . '</span>'; 717 714 718 // Return the link 715 719 return apply_filters( 'bbp_get_user_favorites_link', $html, $add, $rem, $user_id, $topic_id ); 716 720 } … … 800 804 * - before: Before the link 801 805 * - after: After the link 802 806 * @param int $user_id Optional. User id 807 * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">. See ajax_favorite() 803 808 * @uses bbp_get_user_id() To get the user id 804 809 * @uses current_user_can() To check if the current user can edit user 805 810 * @uses bbp_get_topic_id() To get the topic id … … 811 816 * link, args, user id & topic id 812 817 * @return string Permanent link to topic 813 818 */ 814 function bbp_get_user_subscribe_link( $args = '', $user_id = 0 ) {819 function bbp_get_user_subscribe_link( $args = '', $user_id = 0, $wrap = true ) { 815 820 if ( !bbp_is_subscriptions_active() ) 816 821 return; 817 822 … … 861 866 862 867 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); 863 868 $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 $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, $subscribed, $url, $topic_id, $text ); 865 870 871 if ( $wrap ) 872 $html = '<span id="subscription-toggle">' . $html . '</span>'; 873 866 874 // Return the link 867 875 return apply_filters( 'bbp_get_user_subscribe_link', $html, $r, $user_id, $topic_id ); 868 876 }