Ticket #3114: 3114.05.patch
File 3114.05.patch, 10.8 KB (added by , 8 years ago) |
---|
-
src/includes/users/template.php
1202 1202 $permalink = get_permalink(); 1203 1203 } 1204 1204 1205 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-favorite_' . $topic_id ) ); 1206 $sub = $is_fav ? ' class="is-favorite"' : ''; 1207 $html = sprintf( '%s<span id="favorite-%d" %s><a href="%s" class="favorite-toggle" data-topic="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, wp_create_nonce( 'toggle-favorite_' . $topic_id ), $text, $r['after'] ); 1205 $object_id = $topic_id; 1208 1206 1207 // Nonce 1208 $query_args['_wpnonce'] = wp_create_nonce( 'toggle-favorite_' . $object_id ); 1209 1210 // URL 1211 $html = sprintf( 1212 '%s<span id="favorite-%d" %s><a href="%s" class="favorite-toggle" data-bbp-object-id="%d" data-bbp-nonce="%s" data-bbp-ajax-url="%s">%s</a></span>%s', 1213 $r['before'], 1214 $object_id, 1215 $is_fav ? ' class="is-favorite"' : '', 1216 esc_url( add_query_arg( $query_args, $permalink ) ), 1217 $object_id, 1218 $query_args['_wpnonce'], 1219 bbp_get_ajax_url(), 1220 $text, 1221 $r['after'] 1222 ); 1223 1209 1224 // Initial output is wrapped in a span, ajax output is hooked to this 1210 1225 if ( ! empty( $wrap ) ) { 1211 1226 $html = '<span id="favorite-toggle">' . $html . '</span>'; … … 1354 1369 1355 1370 // Bail if subscriptions are inactive 1356 1371 if ( ! bbp_is_subscriptions_active() ) { 1357 return ;1372 return false; 1358 1373 } 1359 1374 1360 1375 // Parse arguments against default values … … 1386 1401 if ( empty( $topic_id ) && ! empty( $forum_id ) ) { 1387 1402 1388 1403 // Decide which link to show 1404 $object_id = $forum_id; 1389 1405 $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id ); 1390 1406 if ( ! empty( $is_subscribed ) ) { 1391 1407 $text = $r['unsubscribe']; … … 1395 1411 $query_args = array( 'action' => 'bbp_subscribe', 'forum_id' => $forum_id ); 1396 1412 } 1397 1413 1398 // Custom redirect1399 if ( ! empty( $r['redirect_to'] ) ) {1400 $query_args['redirect_to'] = urlencode( $r['redirect_to'] );1401 }1402 1403 1414 // Create the link based where the user is and if the user is 1404 1415 // subscribed already 1405 1416 if ( bbp_is_subscriptions() ) { … … 1410 1421 $permalink = get_permalink(); 1411 1422 } 1412 1423 1413 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $forum_id ) );1414 $sub = $is_subscribed ? ' class="is-subscribed"' : '';1415 $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-forum="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $forum_id, $sub, $url, $forum_id, wp_create_nonce( 'toggle-subscription_' . $forum_id ), $text, $r['after'] );1416 1417 // Initial output is wrapped in a span, ajax output is hooked to this1418 if ( ! empty( $wrap ) ) {1419 $html = '<span id="subscription-toggle">' . $html . '</span>';1420 }1421 1422 1424 } else { 1423 1425 1424 1426 // Decide which link to show 1427 $object_id = $topic_id; 1425 1428 $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id ); 1426 1429 if ( ! empty( $is_subscribed ) ) { 1427 1430 $text = $r['unsubscribe']; … … 1431 1434 $query_args = array( 'action' => 'bbp_subscribe', 'topic_id' => $topic_id ); 1432 1435 } 1433 1436 1434 // Custom redirect1435 if ( ! empty( $r['redirect_to'] ) ) {1436 $query_args['redirect_to'] = urlencode( $r['redirect_to'] );1437 }1438 1439 1437 // Create the link based where the user is and if the user is 1440 1438 // subscribed already 1441 1439 if ( bbp_is_subscriptions() ) { … … 1445 1443 } else { 1446 1444 $permalink = get_permalink(); 1447 1445 } 1446 } 1448 1447 1449 $url = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-subscription_' . $topic_id ) ); 1450 $sub = $is_subscribed ? ' class="is-subscribed"' : ''; 1451 $html = sprintf( '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-topic="%d" data-bbp-nonce="%s">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, wp_create_nonce( 'toggle-subscription_' . $topic_id ), $text, $r['after'] ); 1448 // Custom redirect 1449 if ( ! empty( $r['redirect_to'] ) ) { 1450 $query_args['redirect_to'] = urlencode( $r['redirect_to'] ); 1451 } 1452 1452 1453 // Initial output is wrapped in a span, ajax output is hooked to this 1454 if ( ! empty( $wrap ) ) { 1455 $html = '<span id="subscription-toggle">' . $html . '</span>'; 1456 } 1453 // Nonce 1454 $query_args['_wpnonce'] = wp_create_nonce( 'toggle-subscription_' . $object_id ); 1455 1456 // URL 1457 $html = sprintf( 1458 '%s<span id="subscribe-%d" %s><a href="%s" class="subscription-toggle" data-bbp-object-id="%d" data-bbp-nonce="%s" data-bbp-ajax-url="%s">%s</a></span>%s', 1459 $r['before'], 1460 $object_id, 1461 $is_subscribed ? ' class="is-subscribed"' : '', 1462 esc_url( add_query_arg( $query_args, $permalink ) ), 1463 $object_id, 1464 $query_args['_wpnonce'], 1465 bbp_get_ajax_url(), 1466 $text, 1467 $r['after'] 1468 ); 1469 1470 // Initial output is wrapped in a span, ajax output is hooked to this 1471 if ( ! empty( $wrap ) ) { 1472 $html = '<span id="subscription-toggle">' . $html . '</span>'; 1457 1473 } 1458 1474 1459 1475 // Filter & return -
src/templates/default/bbpress-functions.php
221 221 * These localizations require information that may not be loaded even by init. 222 222 * 223 223 * @since 2.1.0 bbPress (r3732) 224 * @since 2.6.0 bbPress (r6540) Moved vars from here to data attributes 224 225 * 225 * @uses bbp_is_single_forum() To check if it's the forum page 226 * @uses bbp_is_single_topic() To check if it's the topic page 227 * @uses is_user_logged_in() To check if user is logged in 228 * @uses bbp_get_current_user_id() To get the current user id 229 * @uses bbp_get_forum_id() To get the forum id 230 * @uses bbp_get_topic_id() To get the topic id 231 * @uses bbp_get_favorites_permalink() To get the favorites permalink 232 * @uses bbp_is_user_favorite() To check if the topic is in user's favorites 233 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active 234 * @uses bbp_is_user_subscribed() To check if the user is subscribed to topic 235 * @uses bbp_get_topic_permalink() To get the topic permalink 236 * @uses wp_localize_script() To localize the script 226 * @uses wp_localize_script() To localize strings 237 227 */ 238 228 public function localize_topic_script() { 239 229 240 230 // Single forum 241 231 if ( bbp_is_single_forum() ) { 242 232 wp_localize_script( 'bbpress-forum', 'bbpForumJS', array( 243 'forum_id' => get_the_ID(), 244 'bbp_ajaxurl' => bbp_get_ajax_url(), 245 'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 246 'is_user_logged_in' => is_user_logged_in(), 247 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 233 'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ) 248 234 ) ); 249 235 250 236 // Single topic 251 237 } elseif ( bbp_is_single_topic() ) { 252 238 wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array( 253 'topic_id' => get_the_ID(), 254 'bbp_ajaxurl' => bbp_get_ajax_url(), 255 'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ), 256 'is_user_logged_in' => is_user_logged_in(), 257 'fav_nonce' => wp_create_nonce( 'toggle-favorite_' . get_the_ID() ), 258 'subs_nonce' => wp_create_nonce( 'toggle-subscription_' . get_the_ID() ) 239 'generic_ajax_error' => esc_html__( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ) 259 240 ) ); 260 241 } 261 242 } -
src/templates/default/js/forum.js
1 1 /* global bbpForumJS */ 2 2 jQuery( document ).ready( function ( $ ) { 3 3 4 function bbp_ajax_call( action, forum_id, nonce, update_selector ) { 5 var $data = { 6 action : action, 7 id : forum_id, 8 nonce : nonce 9 }; 10 11 $.post( bbpForumJS.bbp_ajaxurl, $data, function ( response ) { 4 function bbp_ajax_call( url, data, update_selector ) { 5 $.post( url, data, function ( response ) { 12 6 if ( response.success ) { 13 7 $( update_selector ).html( response.content ); 14 8 } else { … … 21 15 } 22 16 23 17 $( '#subscription-toggle' ).on( 'click', 'span a.subscription-toggle', function( e ) { 24 var nonce = ( bbpForumJS.forum_id === 0 ) ? $( this ).data( 'bbp-nonce' ) : bbpForumJS.subs_nonce;25 26 18 e.preventDefault(); 27 bbp_ajax_call( 'forum_subscription', $( this ).attr( 'data-forum' ), nonce, '#subscription-toggle' ); 19 bbp_ajax_call( 20 $( this ).data( 'bbp-ajax-url' ), 21 { 22 action : 'forum_subscription', 23 id : $( this ).data( 'bbp-object-id' ), 24 nonce : $( this ).data( 'bbp-nonce' ) 25 }, 26 '#subscription-toggle' 27 ); 28 28 } ); 29 29 } ); -
src/templates/default/js/topic.js
1 1 /* global bbpTopicJS */ 2 2 jQuery( document ).ready( function ( $ ) { 3 3 4 function bbp_ajax_call( action, topic_id, nonce, update_selector ) { 5 var $data = { 6 action : action, 7 id : topic_id, 8 nonce : nonce 9 }; 10 11 $.post( bbpTopicJS.bbp_ajaxurl, $data, function ( response ) { 4 function bbp_ajax_call( url, data, update_selector ) { 5 $.post( url, data, function ( response ) { 12 6 if ( response.success ) { 13 7 $( update_selector ).html( response.content ); 14 8 } else { 15 9 if ( !response.content ) { 16 response.content = bbp TopicJS.generic_ajax_error;10 response.content = bbpForumJS.generic_ajax_error; 17 11 } 18 12 window.alert( response.content ); 19 13 } … … 21 15 } 22 16 23 17 $( '#favorite-toggle' ).on( 'click', 'span a.favorite-toggle', function( e ) { 24 var nonce = ( bbpTopicJS.topic_id === 0 ) ? $( this ).data( 'bbp-nonce' ) : bbpTopicJS.fav_nonce;25 26 18 e.preventDefault(); 27 bbp_ajax_call( 'favorite', $( this ).attr( 'data-topic' ), nonce, '#favorite-toggle' ); 19 bbp_ajax_call( 20 $( this ).data( 'bbp-ajax-url' ), 21 { 22 action : 'favorite', 23 id : $( this ).data( 'bbp-object-id' ), 24 nonce : $( this ).data( 'bbp-nonce' ) 25 }, 26 '#favorite-toggle' 27 ); 28 28 } ); 29 29 30 30 $( '#subscription-toggle' ).on( 'click', 'span a.subscription-toggle', function( e ) { 31 var nonce = ( bbpTopicJS.topic_id === 0 ) ? $( this ).data( 'bbp-nonce' ) : bbpTopicJS.subs_nonce;32 33 31 e.preventDefault(); 34 bbp_ajax_call( 'subscription', $( this ).attr( 'data-topic' ), nonce, '#subscription-toggle' ); 32 bbp_ajax_call( 33 $( this ).data( 'bbp-ajax-url' ), 34 { 35 action : 'subscription', 36 id : $( this ).data( 'bbp-object-id' ), 37 nonce : $( this ).data( 'bbp-nonce' ) 38 }, 39 '#subscription-toggle' 40 ); 35 41 } ); 36 42 37 43 $( '.bbp-alert-outer' ).on( 'click', '.bbp-alert-close', function( e ) {