Skip to:
Content

bbPress.org

Ticket #3114: 3114.05.patch

File 3114.05.patch, 10.8 KB (added by johnjamesjacoby, 8 years ago)

What it could look like to move everything to data attributes

  • src/includes/users/template.php

     
    12021202                        $permalink = get_permalink();
    12031203                }
    12041204
    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;
    12081206
     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
    12091224                // Initial output is wrapped in a span, ajax output is hooked to this
    12101225                if ( ! empty( $wrap ) ) {
    12111226                        $html = '<span id="favorite-toggle">' . $html . '</span>';
     
    13541369
    13551370                // Bail if subscriptions are inactive
    13561371                if ( ! bbp_is_subscriptions_active() ) {
    1357                         return;
     1372                        return false;
    13581373                }
    13591374
    13601375                // Parse arguments against default values
     
    13861401                if ( empty( $topic_id ) && ! empty( $forum_id ) ) {
    13871402
    13881403                        // Decide which link to show
     1404                        $object_id     = $forum_id;
    13891405                        $is_subscribed = bbp_is_user_subscribed_to_forum( $user_id, $forum_id );
    13901406                        if ( ! empty( $is_subscribed ) ) {
    13911407                                $text       = $r['unsubscribe'];
     
    13951411                                $query_args = array( 'action' => 'bbp_subscribe',   'forum_id' => $forum_id );
    13961412                        }
    13971413
    1398                         // Custom redirect
    1399                         if ( ! empty( $r['redirect_to'] ) ) {
    1400                                 $query_args['redirect_to'] = urlencode( $r['redirect_to'] );
    1401                         }
    1402 
    14031414                        // Create the link based where the user is and if the user is
    14041415                        // subscribed already
    14051416                        if ( bbp_is_subscriptions() ) {
     
    14101421                                $permalink = get_permalink();
    14111422                        }
    14121423
    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 this
    1418                         if ( ! empty( $wrap ) ) {
    1419                                 $html = '<span id="subscription-toggle">' . $html . '</span>';
    1420                         }
    1421 
    14221424                } else {
    14231425
    14241426                        // Decide which link to show
     1427                        $object_id     = $topic_id;
    14251428                        $is_subscribed = bbp_is_user_subscribed_to_topic( $user_id, $topic_id );
    14261429                        if ( ! empty( $is_subscribed ) ) {
    14271430                                $text       = $r['unsubscribe'];
     
    14311434                                $query_args = array( 'action' => 'bbp_subscribe',   'topic_id' => $topic_id );
    14321435                        }
    14331436
    1434                         // Custom redirect
    1435                         if ( ! empty( $r['redirect_to'] ) ) {
    1436                                 $query_args['redirect_to'] = urlencode( $r['redirect_to'] );
    1437                         }
    1438 
    14391437                        // Create the link based where the user is and if the user is
    14401438                        // subscribed already
    14411439                        if ( bbp_is_subscriptions() ) {
     
    14451443                        } else {
    14461444                                $permalink = get_permalink();
    14471445                        }
     1446                }
    14481447
    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                }
    14521452
    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>';
    14571473                }
    14581474
    14591475                // Filter & return
  • src/templates/default/bbpress-functions.php

     
    221221         * These localizations require information that may not be loaded even by init.
    222222         *
    223223         * @since 2.1.0 bbPress (r3732)
     224         * @since 2.6.0 bbPress (r6540) Moved vars from here to data attributes
    224225         *
    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
    237227         */
    238228        public function localize_topic_script() {
    239229
    240230                // Single forum
    241231                if ( bbp_is_single_forum() ) {
    242232                        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' )
    248234                        ) );
    249235
    250236                // Single topic
    251237                } elseif ( bbp_is_single_topic() ) {
    252238                        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' )
    259240                        ) );
    260241                }
    261242        }
  • src/templates/default/js/forum.js

     
    11/* global bbpForumJS */
    22jQuery( document ).ready( function ( $ ) {
    33
    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 ) {
    126                        if ( response.success ) {
    137                                $( update_selector ).html( response.content );
    148                        } else {
     
    2115        }
    2216
    2317        $( '#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 
    2618                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                );
    2828        } );
    2929} );
  • src/templates/default/js/topic.js

     
    11/* global bbpTopicJS */
    22jQuery( document ).ready( function ( $ ) {
    33
    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 ) {
    126                        if ( response.success ) {
    137                                $( update_selector ).html( response.content );
    148                        } else {
    159                                if ( !response.content ) {
    16                                         response.content = bbpTopicJS.generic_ajax_error;
     10                                        response.content = bbpForumJS.generic_ajax_error;
    1711                                }
    1812                                window.alert( response.content );
    1913                        }
     
    2115        }
    2216
    2317        $( '#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 
    2618                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                );
    2828        } );
    2929
    3030        $( '#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 
    3331                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                );
    3541        } );
    3642
    3743        $( '.bbp-alert-outer' ).on( 'click', '.bbp-alert-close', function( e ) {