Skip to:
Content

bbPress.org


Ignore:
Timestamp:
11/20/2013 07:50:55 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Forum Subscriptions - Allow users to subscribe to new topics in specific forums.

  • Code largely lifted from existing Topics Subscriptions, and is based largely on forum-subscriptions.2.diff from mordauk, with edits for code consistency across bbPress components.
  • Refactor existing ambiguous function names into base functions for both forum and topic subscriptions.
  • Include new functions for getting and outputting subscriptions.
  • Modify user-subscriptions.php to show subscribed forums. This includes a modification to content-single-forum.php to include the "Unsubscribe" link if looking at a user profile page.
  • Modify templates/default/bbpress-functions.php to enqueue new JS file to handle forum subscription ajax.
  • Rename HTML element classes from bbp-topic-action to bbp-row-actions to better accommodate forum subscriptions (and any future actions.)
  • BuddyPress tested, JJJ approved.
  • See #2299. Props mordauk, netweb for the considerable effort.
  • More to do here, largely from forum-subscriptions.3.diff
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/templates/default/bbpress-functions.php

    r4944 r5156  
    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( 'bbp_ajax_favorite',     array( $this, 'ajax_favorite'         ) ); // Handles the ajax favorite/unfavorite
    92         add_action( 'bbp_ajax_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( 'bbp_ajax_favorite',           array( $this, 'ajax_favorite'           ) ); // Handles the topic ajax favorite/unfavorite
     92        add_action( 'bbp_ajax_subscription',       array( $this, 'ajax_subscription'       ) ); // Handles the topic ajax subscribe/unsubscribe
     93        add_action( 'bbp_ajax_forum_subscription', array( $this, 'ajax_forum_subscription' ) ); // Handles the forum ajax subscribe/unsubscribe
    9394
    9495        /** Template Wrappers *************************************************/
     
    169170     * @since bbPress (r3732)
    170171     *
     172     * @uses bbp_is_single_forum() To check if it's the forum page
    171173     * @uses bbp_is_single_topic() To check if it's the topic page
     174     * @uses bbp_thread_replies() To check if threaded replies are enabled
    172175     * @uses bbp_is_single_user_edit() To check if it's the profile edit page
    173176     * @uses wp_enqueue_script() To enqueue the scripts
     
    178181        if ( bbp_use_wp_editor() ) {
    179182            wp_enqueue_script( 'jquery' );
     183        }
     184
     185        // Forum-specific scripts
     186        if ( bbp_is_single_forum() ) {
     187
     188            // Forum subscribe/unsubscribe
     189            wp_enqueue_script( 'bbpress-forum', $this->url . 'js/forum.js', array( 'jquery' ), $this->version );
    180190        }
    181191
     
    286296     * @since bbPress (r3732)
    287297     *
     298     * @uses bbp_is_single_forum() To check if it's the forum page
    288299     * @uses bbp_is_single_topic() To check if it's the topic page
    289300     * @uses is_user_logged_in() To check if user is logged in
    290301     * @uses bbp_get_current_user_id() To get the current user id
     302     * @uses bbp_get_forum_id() To get the forum id
    291303     * @uses bbp_get_topic_id() To get the topic id
    292304     * @uses bbp_get_favorites_permalink() To get the favorites permalink
     
    299311    public function localize_topic_script() {
    300312
    301         // Bail if not viewing a single topic
    302         if ( !bbp_is_single_topic() )
    303             return;
    304 
    305         wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
    306             'bbp_ajaxurl'        => bbp_get_ajax_url(),
    307             'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
    308             'is_user_logged_in'  => is_user_logged_in(),
    309             'fav_nonce'          => wp_create_nonce( 'toggle-favorite_' .     get_the_ID() ),
    310             'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
    311         ) );
     313        // Single forum
     314        if ( bbp_is_single_forum() ) {
     315            wp_localize_script( 'bbpress-forum', 'bbpForumJS', array(
     316                'bbp_ajaxurl'        => bbp_get_ajax_url(),
     317                'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
     318                'is_user_logged_in'  => is_user_logged_in(),
     319                'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
     320            ) );
     321           
     322        // Single topic
     323        } elseif ( bbp_is_single_topic() ) {
     324            wp_localize_script( 'bbpress-topic', 'bbpTopicJS', array(
     325                'bbp_ajaxurl'        => bbp_get_ajax_url(),
     326                'generic_ajax_error' => __( 'Something went wrong. Refresh your browser and try again.', 'bbpress' ),
     327                'is_user_logged_in'  => is_user_logged_in(),
     328                'fav_nonce'          => wp_create_nonce( 'toggle-favorite_' .     get_the_ID() ),
     329                'subs_nonce'         => wp_create_nonce( 'toggle-subscription_' . get_the_ID() )
     330            ) );
     331        }
     332    }
     333
     334    /**
     335     * AJAX handler to Subscribe/Unsubscribe a user from a forum
     336     *
     337     * @since bbPress (r5155)
     338     *
     339     * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     340     * @uses bbp_is_user_logged_in() To check if user is logged in
     341     * @uses bbp_get_current_user_id() To get the current user id
     342     * @uses current_user_can() To check if the current user can edit the user
     343     * @uses bbp_get_forum() To get the forum
     344     * @uses wp_verify_nonce() To verify the nonce
     345     * @uses bbp_is_user_subscribed() To check if the forum is in user's subscriptions
     346     * @uses bbp_remove_user_subscriptions() To remove the forum from user's subscriptions
     347     * @uses bbp_add_user_subscriptions() To add the forum from user's subscriptions
     348     * @uses bbp_ajax_response() To return JSON
     349     */
     350    public function ajax_forum_subscription() {
     351
     352        // Bail if subscriptions are not active
     353        if ( ! bbp_is_subscriptions_active() ) {
     354            bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
     355        }
     356
     357        // Bail if user is not logged in
     358        if ( ! is_user_logged_in() ) {
     359            bbp_ajax_response( false, __( 'Please login to subscribe to this forum.', 'bbpress' ), 301 );
     360        }
     361
     362        // Get user and forum data
     363        $user_id = bbp_get_current_user_id();
     364        $id      = intval( $_POST['id'] );
     365
     366        // Bail if user cannot add favorites for this user
     367        if ( ! current_user_can( 'edit_user', $user_id ) ) {
     368            bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
     369        }
     370
     371        // Get the forum
     372        $forum = bbp_get_forum( $id );
     373
     374        // Bail if forum cannot be found
     375        if ( empty( $forum ) ) {
     376            bbp_ajax_response( false, __( 'The forum could not be found.', 'bbpress' ), 303 );
     377        }
     378
     379        // Bail if user did not take this action
     380        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $forum->ID ) ) {
     381            bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
     382        }
     383
     384        // Take action
     385        $status = bbp_is_user_subscribed( $user_id, $forum->ID ) ? bbp_remove_user_subscription( $user_id, $forum->ID ) : bbp_add_user_subscription( $user_id, $forum->ID );
     386
     387        // Bail if action failed
     388        if ( empty( $status ) ) {
     389            bbp_ajax_response( false, __( 'The request was unsuccessful. Please try again.', 'bbpress' ), 305 );
     390        }
     391
     392        // Put subscription attributes in convenient array
     393        $attrs = array(
     394            'forum_id' => $forum->ID,
     395            'user_id'  => $user_id
     396        );
     397
     398        // Action succeeded
     399        bbp_ajax_response( true, bbp_get_forum_subscription_link( $attrs, $user_id, false ), 200 );
    312400    }
    313401
     
    317405     * @since bbPress (r3732)
    318406     *
     407     * @uses bbp_is_favorites_active() To check if favorites are active
     408     * @uses bbp_is_user_logged_in() To check if user is logged in
    319409     * @uses bbp_get_current_user_id() To get the current user id
    320410     * @uses current_user_can() To check if the current user can edit the user
     
    334424
    335425        // Bail if user is not logged in
    336         if ( !is_user_logged_in() ) {
     426        if ( ! is_user_logged_in() ) {
    337427            bbp_ajax_response( false, __( 'Please login to make this topic a favorite.', 'bbpress' ), 301 );
    338428        }
     
    343433
    344434        // Bail if user cannot add favorites for this user
    345         if ( !current_user_can( 'edit_user', $user_id ) ) {
     435        if ( ! current_user_can( 'edit_user', $user_id ) ) {
    346436            bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
    347437        }
     
    356446
    357447        // Bail if user did not take this action
    358         if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) {
     448        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-favorite_' . $topic->ID ) ) {
    359449            bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
    360450        }
     
    384474     *
    385475     * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     476     * @uses bbp_is_user_logged_in() To check if user is logged in
    386477     * @uses bbp_get_current_user_id() To get the current user id
    387478     * @uses current_user_can() To check if the current user can edit the user
     
    396487
    397488        // Bail if subscriptions are not active
    398         if ( !bbp_is_subscriptions_active() ) {
     489        if ( ! bbp_is_subscriptions_active() ) {
    399490            bbp_ajax_response( false, __( 'Subscriptions are no longer active.', 'bbpress' ), 300 );
    400491        }
    401492
    402493        // Bail if user is not logged in
    403         if ( !is_user_logged_in() ) {
     494        if ( ! is_user_logged_in() ) {
    404495            bbp_ajax_response( false, __( 'Please login to subscribe to this topic.', 'bbpress' ), 301 );
    405496        }
     
    410501
    411502        // Bail if user cannot add favorites for this user
    412         if ( !current_user_can( 'edit_user', $user_id ) ) {
     503        if ( ! current_user_can( 'edit_user', $user_id ) ) {
    413504            bbp_ajax_response( false, __( 'You do not have permission to do this.', 'bbpress' ), 302 );
    414505        }
     
    423514
    424515        // Bail if user did not take this action
    425         if ( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) {
     516        if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'toggle-subscription_' . $topic->ID ) ) {
    426517            bbp_ajax_response( false, __( 'Are you sure you meant to do that?', 'bbpress' ), 304 );
    427518        }
Note: See TracChangeset for help on using the changeset viewer.