Skip to:
Content

bbPress.org


Ignore:
Timestamp:
06/14/2017 06:45:49 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Performance: Last pass at 2.6 performance tuning.

  • Keep a local cache of non-options that aren't in the database, to avoid multiple database misses for options we know aren't in the database after wp_load_alloptions() is called
  • Stop getting all favorite IDs and subscription IDs when checking if a user has favorited or subscribed to something, because these queries are expensive joins that are difficult to cache & invalidate
  • Consolidate forum/topic favorites & subscriptions logic back into central routers, to make it easier to handle taxonomy term subscriptions in the future, and remove nested filter calls that make the call-stack confusing
  • Informally deprecate some forum & topic specific fav/sub functions
  • Rename one of the engagements remove functions to better match existing naming pattern
  • Typo fixes & general code quality improvements
  • Bump slow tests threshold up to 500 for now, and we can bring back down later (my 12" MacBook runs this pretty slowly, so I'd like to play with this more)
  • Unit tests are all passing, and more quickly, maybe surprisingly

This should result in around 20 fewer database hits on non-persistent-cache sites, on average. When viewing single topics & replies, this will result in more than 25 fewer database hits.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/users/engagements.php

    r6534 r6544  
    5757
    5858/**
    59  * Remove all users from an object
     59 * Remove a user id from all objects
     60 *
     61 * @since 2.6.0 bbPress (r6109)
     62 *
     63 * @param int    $user_id   The user id
     64 * @param string $meta_key  The relationship key
     65 * @param string $meta_type The relationship type (usually 'post')
     66 *
     67 * @uses delete_metadata() To remove user from all objects
     68 *
     69 * @return bool Returns true on success, false on failure
     70 */
     71function bbp_remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
     72    $retval = delete_metadata( $meta_type, null, $meta_key, $user_id, true );
     73
     74    // Filter & return
     75    return (bool) apply_filters( 'bbp_remove_user_from_all_objects', (bool) $retval, $user_id, $meta_key, $meta_type );
     76}
     77
     78/**
     79 * Remove an object from all users
    6080 *
    6181 * @since 2.6.0 bbPress (r6109)
     
    7090 * @return bool Returns true on success, false on failure
    7191 */
    72 function bbp_remove_all_users_from_object( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
     92function bbp_remove_object_from_all_users( $object_id = 0, $meta_key = '', $meta_type = 'post' ) {
    7393    $retval = delete_metadata( $meta_type, $object_id, $meta_key, null, false );
    7494
    7595    // Filter & return
    76     return (bool) apply_filters( 'bbp_remove_all_users_from_object', (bool) $retval, $object_id, $meta_key, $meta_type );
    77 }
    78 
    79 /**
    80  * Remove a user id from all objects
    81  *
    82  * @since 2.6.0 bbPress (r6109)
    83  *
    84  * @param int    $user_id   The user id
    85  * @param string $meta_key  The relationship key
    86  * @param string $meta_type The relationship type (usually 'post')
    87  *
    88  * @uses delete_metadata() To remove user from all objects
    89  *
    90  * @return bool Returns true on success, false on failure
    91  */
    92 function bbp_remove_user_from_all_objects( $user_id = 0, $meta_key = '', $meta_type = 'post' ) {
    93     $retval = delete_metadata( $meta_type, null, $meta_key, $user_id, true );
    94 
    95     // Filter & return
    96     return (bool) apply_filters( 'bbp_remove_user_from_all_objects', (bool) $retval, $user_id, $meta_key, $meta_type );
     96    return (bool) apply_filters( 'bbp_remove_object_from_all_users', (bool) $retval, $object_id, $meta_key, $meta_type );
    9797}
    9898
     
    416416
    417417        // Delete all engagements
    418         bbp_remove_all_users_from_object( $topic_id, '_bbp_engagement' );
     418        bbp_remove_object_from_all_users( $topic_id, '_bbp_engagement' );
    419419
    420420        // Update the voice count for this topic id
     
    519519
    520520/**
    521  * Get a user's favorite topic ids
    522  *
    523  * @since 2.0.0 bbPress (r2652)
    524  *
    525  * @param int $user_id Optional. User id
    526  * @uses bbp_get_user_id() To get the user id
    527  * @uses bbp_get_topic_post_type() To get the topic post type
    528  * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
    529  *                        the favorites and user id
    530  * @return array|bool Results if user has favorites, otherwise null
    531  */
    532 function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
    533     $user_id   = bbp_get_user_id( $user_id );
    534     $favorites = new WP_Query( array(
    535         'fields'        => 'ids',
    536         'post_type'     => bbp_get_topic_post_type(),
    537         'nopaging'      => true,
    538         'no_found_rows' => true,
    539         'meta_query'    => array( array(
    540             'key'     => '_bbp_favorite',
    541             'value'   => $user_id,
    542             'compare' => 'NUMERIC'
    543         ) )
    544     ) );
    545 
    546     // Filter & return
    547     return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id );
    548 }
    549 
    550 /**
    551521 * Check if a topic is in user's favorites or not
    552522 *
     
    556526 * @param int $topic_id Optional. Topic id
    557527 * @uses bbp_get_user_id() To get the user id
    558  * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    559528 * @uses bbp_get_topic() To get the topic
    560529 * @uses bbp_get_topic_id() To get the topic id
     
    565534 */
    566535function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {
    567     $retval    = false;
    568     $user_id   = bbp_get_user_id( $user_id, true, true );
    569     $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    570 
    571     if ( ! empty( $favorites ) ) {
    572 
    573         // Checking a specific topic id
    574         if ( ! empty( $topic_id ) ) {
    575             $topic    = bbp_get_topic( $topic_id );
    576             $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    577 
    578         // Using the global topic id
    579         } elseif ( bbp_get_topic_id() ) {
    580             $topic_id = bbp_get_topic_id();
    581 
    582         // Use the current post id
    583         } elseif ( ! bbp_get_topic_id() ) {
    584             $topic_id = get_the_ID();
    585         }
    586 
    587         // Is topic_id in the user's favorites
    588         if ( ! empty( $topic_id ) ) {
    589             $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' );
    590         }
    591     }
    592 
    593     // Filter & return
    594     return (bool) apply_filters( 'bbp_is_user_favorite', $retval, $user_id, $topic_id, $favorites );
     536    $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' );
     537
     538    // Filter & return
     539    return (bool) apply_filters( 'bbp_is_user_favorite', $retval, $user_id, $topic_id );
    595540}
    596541
     
    612557 */
    613558function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) {
     559    $user_id  = bbp_get_user_id( $user_id, false, false );
     560    $topic_id = bbp_get_topic_id( $topic_id );
    614561
    615562    // Bail if not enough info
     
    646593 */
    647594function bbp_remove_user_favorite( $user_id, $topic_id ) {
     595    $user_id  = bbp_get_user_id( $user_id, false, false );
     596    $topic_id = bbp_get_topic_id( $topic_id );
    648597
    649598    // Bail if not enough info
     
    776725
    777726/**
    778  * Get the users who have subscribed to the forum
    779  *
    780  * @since 2.5.0 bbPress (r5156)
    781  *
    782  * @param int $forum_id Optional. forum id
    783  * @uses bbp_get_users_for_object() To get the forum subscribers
    784  * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
    785  * @return array|bool Results if the forum has any subscribers, otherwise false
    786  */
    787 function bbp_get_forum_subscribers( $forum_id = 0 ) {
    788     $forum_id = bbp_get_forum_id( $forum_id );
    789     $users    = bbp_get_users_for_object( $forum_id, '_bbp_subscription' );
    790 
    791     // Filter & return
    792     return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
    793 }
    794 
    795 /**
    796  * Get the users who have subscribed to the topic
    797  *
    798  * @since 2.0.0 bbPress (r2668)
    799  *
    800  * @param int $topic_id Optional. Topic id
    801  * @uses bbp_get_users_for_object() To get the topic subscribers
    802  * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
    803  * @return array|bool Results if the topic has any subscribers, otherwise false
    804  */
    805 function bbp_get_topic_subscribers( $topic_id = 0 ) {
    806     $topic_id = bbp_get_topic_id( $topic_id );
    807     $users    = bbp_get_users_for_object( $topic_id, '_bbp_subscription' );
    808 
    809     // Filter & return
    810     return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
    811 }
    812 
    813 /**
    814  * Get a user's subscribed topics
    815  *
    816  * @since 2.0.0 bbPress (r2668)
    817  *
    818  * @deprecated 2.5.0 bbPress (r5156)
    819  *
    820  * @param int $user_id Optional. User id
    821  * @uses bbp_get_user_topic_subscriptions() To get the user's subscriptions
    822  * @return array|bool Results if user has subscriptions, otherwise false
    823  */
    824 function bbp_get_user_subscriptions( $user_id = 0 ) {
    825     _deprecated_function( __FUNCTION__, 2.5, 'bbp_get_user_topic_subscriptions()' );
    826     $query = bbp_get_user_topic_subscriptions( $user_id );
    827 
    828     // Filter & return
    829     return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
     727 * Get the users who have subscribed
     728 *
     729 * @since 2.6.0 bbPress (r5156)
     730 *
     731 * @param int $object_id Optional. ID of forum, topic, or something else
     732 * @uses bbp_get_users_for_object() To get the subscribers
     733 * @uses apply_filters() Calls 'bbp_get_subscribers' with the subscribers
     734 * @return array|bool Results if subscribers, otherwise false
     735 */
     736function bbp_get_subscribers( $object_id = 0, $type = 'post' ) {
     737    $users = bbp_get_users_for_object( $object_id, '_bbp_subscription', $type );
     738
     739    // Filter & return
     740    return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $object_id, $type );
    830741}
    831742
     
    883794    return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id );
    884795}
     796
     797/**
     798 * Check if a topic or forum is in user's subscription list or not
     799 *
     800 * @since 2.5.0 bbPress (r5156)
     801 *
     802 * @param int $user_id Optional. User id
     803 * @param int $object_id Optional. Topic id
     804 * @uses get_post() To get the post object
     805 * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
     806 * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
     807 * @uses bbp_get_forum_post_type() To get the forum post type
     808 * @uses bbp_get_topic_post_type() To get the topic post type
     809 * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
     810 *                        forum/topic id and subscriptions
     811 * @return bool True if the forum or topic is in user's subscriptions, otherwise false
     812 */
     813function bbp_is_user_subscribed( $user_id = 0, $object_id = 0, $type = 'post' ) {
     814    $retval = bbp_is_object_of_user( $object_id, $user_id, '_bbp_subscription', $type );
     815
     816    // Filter & return
     817    return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $type );
     818}
     819
     820/**
     821 * Add a user subscription
     822 *
     823 * @since 2.5.0 bbPress (r5156)
     824 * @since 2.6.0 bbPress (r6544) Added $type parameter
     825 *
     826 * @param int    $user_id   Optional. User id
     827 * @param int    $object_id Optional. Topic id
     828 * @param string $type      Optional. Type of object being subscribed to
     829 *
     830 * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id
     831 * @return bool Always true
     832 */
     833function bbp_add_user_subscription( $user_id = 0, $object_id = 0, $type = 'post' ) {
     834
     835    // Bail if not enough info
     836    if ( empty( $user_id ) || empty( $object_id ) ) {
     837        return false;
     838    }
     839
     840    // Bail if already subscribed
     841    if ( bbp_is_user_subscribed( $user_id, $object_id, $type ) ) {
     842        return false;
     843    }
     844
     845    // Bail if add fails
     846    if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription', $type ) ) {
     847        return false;
     848    }
     849
     850    do_action( 'bbp_add_user_subscription', $user_id, $object_id, $type );
     851
     852    return true;
     853}
     854
     855/**
     856 * Remove a user subscription
     857 *
     858 * @since 2.5.0 bbPress (r5156)
     859 * @since 2.6.0 bbPress (r6544) Added $type parameter
     860 *
     861 * @param int    $user_id   Optional. User id
     862 * @param int    $object_id Optional. Topic id
     863 * @param string $type      Optional. Type of object being subscribed to
     864 *
     865 * @uses bbp_is_user_subscribed() To check if the user is already subscribed
     866 * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
     867 *                    topic id
     868 * @return bool True if the topic was removed from user's subscriptions,
     869 *               otherwise false
     870 */
     871function bbp_remove_user_subscription( $user_id = 0, $object_id = 0, $type = 'post' ) {
     872
     873    // Bail if not enough info
     874    if ( empty( $user_id ) || empty( $object_id ) ) {
     875        return false;
     876    }
     877
     878    // Bail if not subscribed
     879    if ( ! bbp_is_user_subscribed( $user_id, $object_id, $type ) ) {
     880        return false;
     881    }
     882
     883    // Bail if remove fails
     884    if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription', $type ) ) {
     885        return false;
     886    }
     887
     888    do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $type );
     889
     890    return true;
     891}
     892
     893/**
     894 * Handles the front end toggling of user subscriptions
     895 *
     896 * @since 2.0.0 bbPress (r2790)
     897 * @since 2.6.l bbPress (r6543)
     898 *
     899 * @param string $action The requested action to compare this function to
     900 * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
     901 * @uses bbp_get_user_id() To get the user id
     902 * @uses bbp_verify_nonce_request() To verify the nonce and check the request
     903 * @uses current_user_can() To check if the current user can edit the user
     904 * @uses bbPress:errors:add() To log the error messages
     905 * @uses bbp_is_user_subscribed() To check if the object is in user's
     906 *                                 subscriptions
     907 * @uses bbp_remove_user_subscription() To remove the user subscription
     908 * @uses bbp_add_user_subscription() To add the user subscription
     909 * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
     910 *                    object id and action
     911 * @uses bbp_is_subscription() To check if it's the subscription page
     912 * @uses bbp_redirect() To redirect to the url
     913 */
     914function bbp_subscriptions_handler( $action = '' ) {
     915
     916    // Default
     917    $success = false;
     918
     919    // Bail if subscriptions not active
     920    if ( ! bbp_is_subscriptions_active() ) {
     921        return $success;
     922    }
     923
     924    // Bail if no object ID is passed
     925    if ( empty( $_GET['object_id'] ) ) {
     926        return $success;
     927    }
     928
     929    // Setup possible get actions
     930    $possible_actions = array(
     931        'bbp_subscribe',
     932        'bbp_unsubscribe'
     933    );
     934
     935    // Bail if actions aren't meant for this function
     936    if ( ! in_array( $action, $possible_actions, true ) ) {
     937        return $success;
     938    }
     939
     940    // Get required data
     941    $user_id     = bbp_get_current_user_id();
     942    $object_id   = absint( $_GET['object_id'] );
     943    $object_type = ! empty( $_GET['object_type'] )
     944        ? sanitize_key( $_GET['object_type'] )
     945        : 'post';
     946
     947    // Check for empty topic
     948    if ( empty( $object_id ) ) {
     949        bbp_add_error( 'bbp_subscription_object_id', __( '<strong>ERROR</strong>: Not found. What are you subscribing/unsubscribing to?', 'bbpress' ) );
     950
     951    // Check nonce
     952    } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $object_id ) ) {
     953        bbp_add_error( 'bbp_subscription_object_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
     954
     955    // Check current user's ability to edit the user
     956    } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
     957        bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit subscriptions of that user.', 'bbpress' ) );
     958    }
     959
     960    // Bail if we have errors
     961    if ( bbp_has_errors() ) {
     962        return $success;
     963    }
     964
     965    /** No errors *************************************************************/
     966
     967    if ( 'bbp_unsubscribe' === $action ) {
     968        $success = bbp_remove_user_subscription( $user_id, $object_id, $object_type );
     969    } elseif ( 'bbp_subscribe' === $action ) {
     970        $success = bbp_add_user_subscription( $user_id, $object_id, $object_type );
     971    }
     972
     973    // Do additional subscriptions actions
     974    do_action( 'bbp_subscriptions_handler', $success, $user_id, $object_id, $action, $object_type );
     975
     976    // Success!
     977    if ( true === $success ) {
     978
     979        // Redirect back from whence we came
     980        if ( ! empty( $_REQUEST['redirect_to'] ) ) {
     981            $redirect = $_REQUEST['redirect_to']; // Validated later
     982        } elseif ( bbp_is_subscriptions() ) {
     983            $redirect = bbp_get_subscriptions_permalink( $user_id );
     984        } elseif ( bbp_is_single_user() ) {
     985            $redirect = bbp_get_user_profile_url();
     986        } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
     987            $redirect = bbp_get_topic_permalink( $object_id );
     988        } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
     989            $redirect = bbp_get_forum_permalink( $object_id );
     990        } elseif ( is_single() || is_page() ) {
     991            $redirect = get_permalink();
     992        } else {
     993            $redirect = get_permalink( $object_id );
     994        }
     995
     996        bbp_redirect( $redirect );
     997
     998    // Fail! Handle errors
     999    } elseif ( 'bbp_unsubscribe' === $action ) {
     1000        bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing.', 'bbpress' ) );
     1001    } elseif ( 'bbp_subscribe' === $action ) {
     1002        bbp_add_error( 'bbp_subscribe',   __( '<strong>ERROR</strong>: There was a problem subscribing.', 'bbpress' ) );
     1003    }
     1004
     1005    return (bool) $success;
     1006}
     1007
     1008/** Query Helpers *************************************************************/
     1009
     1010/**
     1011 * Get a user's favorite topic ids
     1012 *
     1013 * @since 2.0.0 bbPress (r2652)
     1014 *
     1015 * @param int $user_id Optional. User id
     1016 * @uses bbp_get_user_id() To get the user id
     1017 * @uses bbp_get_topic_post_type() To get the topic post type
     1018 * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
     1019 *                        the favorites and user id
     1020 * @return array|bool Results if user has favorites, otherwise null
     1021 */
     1022function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
     1023    $user_id   = bbp_get_user_id( $user_id );
     1024    $favorites = new WP_Query( array(
     1025        'fields'        => 'ids',
     1026        'post_type'     => bbp_get_topic_post_type(),
     1027        'nopaging'      => true,
     1028        'no_found_rows' => true,
     1029        'meta_query'    => array( array(
     1030            'key'     => '_bbp_favorite',
     1031            'value'   => $user_id,
     1032            'compare' => 'NUMERIC'
     1033        ) )
     1034    ) );
     1035
     1036    // Filter & return
     1037    return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id );
     1038}
     1039
    8851040
    8861041/**
     
    9151070
    9161071/**
    917  * Get a user's subscribed topics' ids
     1072 * Get a user's subscribed topic ids
    9181073 *
    9191074 * @since 2.0.0 bbPress (r2668)
     
    9441099}
    9451100
    946 /**
    947  * Check if a topic or forum is in user's subscription list or not
     1101/** Deprecated ****************************************************************/
     1102
     1103/**
     1104 * Get a user's subscribed topics
     1105 *
     1106 * @since 2.0.0 bbPress (r2668)
     1107 * @deprecated 2.5.0 bbPress (r5156)
     1108 *
     1109 * @param int $user_id Optional. User id
     1110 *
     1111 * @return array|bool Results if user has subscriptions, otherwise false
     1112 */
     1113function bbp_get_user_subscriptions( $user_id = 0 ) {
     1114    _deprecated_function( __FUNCTION__, '2.5', 'bbp_get_user_topic_subscriptions()' );
     1115    $query = bbp_get_user_topic_subscriptions( $user_id );
     1116
     1117    // Filter & return
     1118    return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
     1119}
     1120
     1121/**
     1122 * Get the users who have subscribed to the forum
    9481123 *
    9491124 * @since 2.5.0 bbPress (r5156)
    950  *
    951  * @param int $user_id Optional. User id
    952  * @param int $object_id Optional. Topic id
    953  * @uses get_post() To get the post object
    954  * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
    955  * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
    956  * @uses bbp_get_forum_post_type() To get the forum post type
    957  * @uses bbp_get_topic_post_type() To get the topic post type
    958  * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
    959  *                        forum/topic id and subscriptions
    960  * @return bool True if the forum or topic is in user's subscriptions, otherwise false
    961  */
    962 function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) {
    963 
    964     // Assume user is not subscribed
    965     $retval = false;
    966 
    967     // Setup ID's array
    968     $subscribed_ids = array();
    969 
    970     // User and object ID's are passed
    971     if ( ! empty( $user_id ) && ! empty( $object_id ) ) {
    972 
    973         // Get the post type
    974         $post_type = get_post_type( $object_id );
    975 
    976         // Post exists, so check the types
    977         if ( ! empty( $post_type ) ) {
    978 
    979             switch( $post_type ) {
    980 
    981                 // Forum
    982                 case bbp_get_forum_post_type() :
    983                     $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
    984                     $retval         = bbp_is_user_subscribed_to_forum( $user_id, $object_id, $subscribed_ids );
    985                     break;
    986 
    987                 // Topic (default)
    988                 case bbp_get_topic_post_type() :
    989                 default :
    990                     $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
    991                     $retval         = bbp_is_user_subscribed_to_topic( $user_id, $object_id, $subscribed_ids );
    992                     break;
    993             }
    994         }
    995     }
    996 
    997     // Filter & return
    998     return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscribed_ids );
     1125 * @deprecated 2.6.0 bbPress (r6543)
     1126 *
     1127 * @param int $forum_id Optional. forum id
     1128 *
     1129 * @return array|bool Results if the forum has any subscribers, otherwise false
     1130 */
     1131function bbp_get_forum_subscribers( $forum_id = 0 ) {
     1132    $users = bbp_get_users_for_object( $forum_id, '_bbp_subscription' );
     1133
     1134    // Filter & return
     1135    return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
     1136}
     1137
     1138/**
     1139 * Get the users who have subscribed to the topic
     1140 *
     1141 * @since 2.0.0 bbPress (r2668)
     1142 * @deprecated 2.6.0 bbPress (r6543)
     1143 *
     1144 * @param int $topic_id Optional. Topic id
     1145 *
     1146 * @return array|bool Results if the topic has any subscribers, otherwise false
     1147 */
     1148function bbp_get_topic_subscribers( $topic_id = 0 ) {
     1149    $users = bbp_get_users_for_object( $topic_id, '_bbp_subscription' );
     1150
     1151    // Filter & return
     1152    return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
    9991153}
    10001154
     
    10031157 *
    10041158 * @since 2.5.0 bbPress (r5156)
    1005  *
    1006  * @param int $user_id Optional. User id
    1007  * @param int $forum_id Optional. Topic id
    1008  * @param array $subscribed_ids Optional. Array of forum ID's to check
    1009  * @uses bbp_get_user_id() To get the user id
    1010  * @uses bbp_get_forum() To get the forum
    1011  * @uses bbp_get_forum_id() To get the forum id
    1012  * @uses bbp_is_object_of_user() To check if the user has a subscription
    1013  * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_forum' with the bool, user id,
    1014  *                        forum id and subsriptions
     1159 * @deprecated 2.6.0 bbPress (r6543)
     1160 *
     1161 * @param int $user_id  Optional. User id
     1162 * @param int $forum_id Optional. Forum id
     1163 *
    10151164 * @return bool True if the forum is in user's subscriptions, otherwise false
    10161165 */
    1017 function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0, $subscribed_ids = array() ) {
    1018 
    1019     // Assume user is not subscribed
    1020     $retval = false;
    1021 
    1022     // Validate user
    1023     $user_id = bbp_get_user_id( $user_id, true, true );
    1024     if ( ! empty( $user_id ) ) {
    1025 
    1026         // Get subscription ID's if none passed
    1027         if ( empty( $subscribed_ids ) ) {
    1028             $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
    1029         }
    1030 
    1031         // User has forum subscriptions
    1032         if ( ! empty( $subscribed_ids ) ) {
    1033 
    1034             // Checking a specific forum id
    1035             if ( ! empty( $forum_id ) ) {
    1036                 $forum    = bbp_get_forum( $forum_id );
    1037                 $forum_id = ! empty( $forum ) ? $forum->ID : 0;
    1038 
    1039             // Using the global forum id
    1040             } elseif ( bbp_get_forum_id() ) {
    1041                 $forum_id = bbp_get_forum_id();
    1042 
    1043             // Use the current post id
    1044             } elseif ( ! bbp_get_forum_id() ) {
    1045                 $forum_id = get_the_ID();
    1046             }
    1047 
    1048             // Is forum_id in the user's subscriptions
    1049             if ( ! empty( $forum_id ) ) {
    1050                 $retval = bbp_is_object_of_user( $forum_id, $user_id, '_bbp_subscription' );
    1051             }
    1052         }
    1053     }
    1054 
    1055     // Filter & return
    1056     return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', $retval, $user_id, $forum_id, $subscribed_ids );
     1166function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0 ) {
     1167    return bbp_is_user_subscribed( $user_id, $forum_id );
    10571168}
    10581169
     
    10611172 *
    10621173 * @since 2.5.0 bbPress (r5156)
     1174 * @deprecated 2.6.0 bbPress (r6543) Use bbp_is_user_subscribed()
    10631175 *
    10641176 * @param int $user_id Optional. User id
    10651177 * @param int $topic_id Optional. Topic id
    1066  * @param array $subscribed_ids Optional. Array of topic ID's to check
    1067  * @uses bbp_get_user_id() To get the user id
    1068  * @uses bbp_get_topic() To get the topic
    1069  * @uses bbp_get_topic_id() To get the topic id
    1070  * @uses bbp_is_object_of_user() To check if the user is subscribed
    1071  * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_topic' with the bool, user id,
    1072  *                        topic id and subsriptions
    10731178 * @return bool True if the topic is in user's subscriptions, otherwise false
    10741179 */
    1075 function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0, $subscribed_ids = array() ) {
    1076 
    1077     // Assume user is not subscribed
    1078     $retval = false;
    1079 
    1080     // Validate user
    1081     $user_id = bbp_get_user_id( $user_id, true, true );
    1082     if ( ! empty( $user_id ) ) {
    1083 
    1084         // Get subscription ID's if none passed
    1085         if ( empty( $subscribed_ids ) ) {
    1086             $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
    1087         }
    1088 
    1089         // User has topic subscriptions
    1090         if ( ! empty( $subscribed_ids ) ) {
    1091 
    1092             // Checking a specific topic id
    1093             if ( ! empty( $topic_id ) ) {
    1094                 $topic    = bbp_get_topic( $topic_id );
    1095                 $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    1096 
    1097             // Using the global topic id
    1098             } elseif ( bbp_get_topic_id() ) {
    1099                 $topic_id = bbp_get_topic_id();
    1100 
    1101             // Use the current post id
    1102             } elseif ( ! bbp_get_topic_id() ) {
    1103                 $topic_id = get_the_ID();
    1104             }
    1105 
    1106             // Is topic_id in the user's subscriptions
    1107             if ( ! empty( $topic_id ) ) {
    1108                 $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_subscription' );
    1109             }
    1110         }
    1111     }
    1112 
    1113     // Filter & return
    1114     return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', $retval, $user_id, $topic_id, $subscribed_ids );
    1115 }
    1116 
    1117 /**
    1118  * Add a user subscription
     1180function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0 ) {
     1181    return bbp_is_user_subscribed( $user_id, $topic_id );
     1182}
     1183
     1184/**
     1185 * Remove a forum from user's subscriptions
    11191186 *
    11201187 * @since 2.5.0 bbPress (r5156)
    1121  *
    1122  * @param int $user_id Optional. User id
    1123  * @param int $object_id Optional. Topic id
    1124  * @uses get_post() To get the post object
    1125  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id
     1188 * @deprecated 2.6.0 bbPress (r6543)
     1189 *
     1190 * @param int $user_id Optional. User id
     1191 * @param int $forum_id Optional. forum id
     1192 * @return bool True if the forum was removed from user's subscriptions,
     1193 *               otherwise false
     1194 */
     1195function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
     1196    return bbp_remove_user_subscription( $user_id, $forum_id );
     1197}
     1198
     1199/**
     1200 * Remove a topic from user's subscriptions
     1201 *
     1202 * @since 2.5.0 bbPress (r5156)
     1203 * @deprecated 2.6.0 bbPress (r6543)
     1204 *
     1205 * @param int $user_id Optional. User id
     1206 * @param int $topic_id Optional. Topic id
     1207 * @return bool True if the topic was removed from user's subscriptions,
     1208 *               otherwise false
     1209 */
     1210function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
     1211    return bbp_remove_user_subscription( $user_id, $topic_id );
     1212}
     1213
     1214/**
     1215 * Add a forum to user's subscriptions
     1216 *
     1217 * @since 2.5.0 bbPress (r5156)
     1218 * @deprecated 2.6.0 bbPress (r6543)
     1219 *
     1220 * @param int $user_id Optional. User id
     1221 * @param int $forum_id Optional. forum id
    11261222 * @return bool Always true
    11271223 */
    1128 function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
    1129 
    1130     // Bail if not enough info
    1131     if ( empty( $user_id ) || empty( $object_id ) ) {
    1132         return false;
    1133     }
    1134 
    1135     // Get the post type
    1136     $post_type = get_post_type( $object_id );
    1137     if ( empty( $post_type ) ) {
    1138         return false;
    1139     }
    1140 
    1141     // Bail if already subscribed
    1142     if ( bbp_is_user_subscribed( $user_id, $object_id ) ) {
    1143         return false;
    1144     }
    1145 
    1146     // Bail if add fails
    1147     if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription' ) ) {
    1148         return false;
    1149     }
    1150 
    1151     do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );
    1152 
    1153     return true;
    1154 }
    1155 
    1156 /**
    1157  * Add a forum to user's subscriptions
    1158  *
    1159  * @since 2.5.0 bbPress (r5156)
    1160  *
    1161  * @param int $user_id Optional. User id
    1162  * @param int $forum_id Optional. forum id
    1163  * @uses bbp_get_forum() To get the forum
    1164  * @uses bbp_add_user_subscription() To add the user subscription
    1165  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
    1166  * @return bool Always true
    1167  */
    11681224function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
    1169 
    1170     // Bail if not enough info
    1171     if ( empty( $user_id ) || empty( $forum_id ) ) {
    1172         return false;
    1173     }
    1174 
    1175     // Bail if no forum
    1176     $forum = bbp_get_forum( $forum_id );
    1177     if ( empty( $forum ) ) {
    1178         return false;
    1179     }
    1180 
    1181     // Bail if already subscribed
    1182     if ( bbp_is_user_subscribed( $user_id, $forum_id ) ) {
    1183         return false;
    1184     }
    1185 
    1186     // Bail if add fails
    1187     if ( ! bbp_add_user_subscription( $user_id, $forum_id ) ) {
    1188         return false;
    1189     }
    1190 
    1191     do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );
    1192 
    1193     return true;
     1225    return bbp_add_user_subscription( $user_id, $forum_id );
    11941226}
    11951227
     
    12031235 *
    12041236 * @since 2.0.0 bbPress (r2668)
     1237 * @deprecated 2.6.0 bbPress (r6543)
    12051238 *
    12061239 * @param int $user_id Optional. User id
    12071240 * @param int $topic_id Optional. Topic id
    1208  * @uses bbp_add_user_subscription() To add the subscription
    1209  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    12101241 * @return bool Always true
    12111242 */
    12121243function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    1213 
    1214     // Bail if not enough info
    1215     if ( empty( $user_id ) || empty( $topic_id ) ) {
    1216         return false;
    1217     }
    1218 
    1219     // Bail if already subscribed
    1220     if ( bbp_is_user_subscribed_to_topic( $user_id, $topic_id ) ) {
    1221         return false;
    1222     }
    1223 
    1224     // Bail if add fails
    1225     if ( ! bbp_add_user_subscription( $user_id, $topic_id ) ) {
    1226         return false;
    1227     }
    1228 
    1229     do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );
    1230 
    1231     return true;
    1232 }
    1233 
    1234 /**
    1235  * Remove a user subscription
    1236  *
    1237  * @since 2.0.0 bbPress (r2668)
    1238  *
    1239  * @param int $user_id Optional. User id
    1240  * @param int $object_id Optional. Topic id
    1241  * @uses get_post() To get the post object
    1242  * @uses bbp_is_user_subscribed() To check if the user is already subscribed
    1243  * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    1244  *                    topic id
    1245  * @return bool True if the topic was removed from user's subscriptions,
    1246  *               otherwise false
    1247  */
    1248 function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {
    1249 
    1250     // Bail if not enough info
    1251     if ( empty( $user_id ) || empty( $object_id ) ) {
    1252         return false;
    1253     }
    1254 
    1255     // Get post type
    1256     $post_type = get_post_type( $object_id );
    1257     if ( empty( $post_type ) ) {
    1258         return false;
    1259     }
    1260 
    1261     // Bail if not subscribed
    1262     if ( ! bbp_is_user_subscribed( $user_id, $object_id ) ) {
    1263         return false;
    1264     }
    1265 
    1266     // Bail if remove fails
    1267     if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription' ) ) {
    1268         return false;
    1269     }
    1270 
    1271     do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );
    1272 
    1273     return true;
    1274 }
    1275 
    1276 /**
    1277  * Remove a forum from user's subscriptions
     1244    return bbp_add_user_subscription( $user_id, $topic_id );
     1245}
     1246
     1247/**
     1248 * Handles the front end toggling of forum subscriptions
    12781249 *
    12791250 * @since 2.5.0 bbPress (r5156)
    1280  *
    1281  * @param int $user_id Optional. User id
    1282  * @param int $forum_id Optional. forum id
    1283  * @uses bbp_remove_user_subscription() To remove the subscription
    1284  * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    1285  *                    forum id
    1286  * @return bool True if the forum was removed from user's subscriptions,
    1287  *               otherwise false
    1288  */
    1289 function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
    1290 
    1291     // Bail if not enough info
    1292     if ( empty( $user_id ) || empty( $forum_id ) ) {
    1293         return false;
    1294     }
    1295 
    1296     // Bail if remove fails
    1297     if ( ! bbp_remove_user_subscription( $user_id, $forum_id ) ) {
    1298         return false;
    1299     }
    1300 
    1301     do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
    1302 
    1303     return true;
    1304 }
    1305 
    1306 /**
    1307  * Remove a topic from user's subscriptions
    1308  *
    1309  * @since 2.5.0 bbPress (r5156)
    1310  *
    1311  * @param int $user_id Optional. User id
    1312  * @param int $topic_id Optional. Topic id
    1313  * @uses bbp_remove_user_subscription() To remove the subscription
    1314  * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    1315  *                    topic id
    1316  * @return bool True if the topic was removed from user's subscriptions,
    1317  *               otherwise false
    1318  */
    1319 function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    1320 
    1321     // Bail if not enough info
    1322     if ( empty( $user_id ) || empty( $topic_id ) ) {
    1323         return false;
    1324     }
    1325 
    1326     // Bail if remove fails
    1327     if ( ! bbp_remove_user_subscription( $user_id, $topic_id ) ) {
    1328         return false;
    1329     }
    1330 
    1331     do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
    1332 
    1333     return true;
    1334 }
    1335 
    1336 /**
    1337  * Handles the front end subscribing and unsubscribing forums
    1338  *
    1339  * @since 2.5.0 bbPress (r5156)
    1340  *
    1341  * @param string $action The requested action to compare this function to
    1342  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    1343  * @uses bbp_get_user_id() To get the user id
    1344  * @uses bbp_verify_nonce_request() To verify the nonce and check the request
    1345  * @uses current_user_can() To check if the current user can edit the user
    1346  * @uses bbPress:errors:add() To log the error messages
    1347  * @uses bbp_is_user_subscribed() To check if the forum is in user's
    1348  *                                 subscriptions
    1349  * @uses bbp_remove_user_subscription() To remove the user subscription
    1350  * @uses bbp_add_user_subscription() To add the user subscription
    1351  * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
    1352  *                    forum id and action
    1353  * @uses bbp_is_subscription() To check if it's the subscription page
    1354  * @uses bbp_get_forum_permalink() To get the forum permalink
    1355  * @uses bbp_redirect() To redirect to the url
     1251 * @deprecated 2.6.0 bbPress (r6543)
    13561252 */
    13571253function bbp_forum_subscriptions_handler( $action = '' ) {
    1358 
    1359     // Default
    1360     $success = false;
    1361 
    1362     // Bail if subscriptions not active
    1363     if ( ! bbp_is_subscriptions_active() ) {
    1364         return $success;
    1365     }
    1366 
    1367     // Bail if no forum ID is passed
    1368     if ( empty( $_GET['forum_id'] ) ) {
    1369         return $success;
    1370     }
    1371 
    1372     // Setup possible get actions
    1373     $possible_actions = array(
    1374         'bbp_subscribe',
    1375         'bbp_unsubscribe',
    1376     );
    1377 
    1378     // Bail if actions aren't meant for this function
    1379     if ( ! in_array( $action, $possible_actions, true ) ) {
    1380         return $success;
    1381     }
    1382 
    1383     // Get required data
    1384     $forum_id = bbp_get_forum_id( $_GET['forum_id'] );
    1385     $user_id  = bbp_get_user_id( 0, true, true );
    1386 
    1387     // Check for empty forum
    1388     if ( empty( $forum_id ) ) {
    1389         bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found. Which forum are you subscribing/unsubscribing to?', 'bbpress' ) );
    1390 
    1391     // Check nonce
    1392     } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {
    1393         bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
    1394 
    1395     // Check current user's ability to edit the user
    1396     } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    1397         bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites of that user.', 'bbpress' ) );
    1398     }
    1399 
    1400     // Bail if we have errors
    1401     if ( bbp_has_errors() ) {
    1402         return $success;
    1403     }
    1404 
    1405     /** No errors *************************************************************/
    1406 
    1407     if ( 'bbp_unsubscribe' === $action ) {
    1408         $success = bbp_remove_user_subscription( $user_id, $forum_id );
    1409     } elseif ( 'bbp_subscribe' === $action ) {
    1410         $success = bbp_add_user_subscription( $user_id, $forum_id );
    1411     }
    1412 
    1413     // Do additional subscriptions actions
    1414     do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action );
    1415 
    1416     // Success!
    1417     if ( true === $success ) {
    1418 
    1419         // Redirect back from whence we came
    1420         if ( ! empty( $_REQUEST['redirect_to'] ) ) {
    1421             $redirect = $_REQUEST['redirect_to']; // Validated later
    1422         } elseif ( bbp_is_subscriptions() ) {
    1423             $redirect = bbp_get_subscriptions_permalink( $user_id );
    1424         } elseif ( bbp_is_single_user() ) {
    1425             $redirect = bbp_get_user_profile_url();
    1426         } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
    1427             $redirect = bbp_get_forum_permalink( $forum_id );
    1428         } elseif ( is_single() || is_page() ) {
    1429             $redirect = get_permalink();
    1430         } else {
    1431             $redirect = get_permalink( $forum_id );
    1432         }
    1433 
    1434         bbp_redirect( $redirect );
    1435 
    1436     // Fail! Handle errors
    1437     } elseif ( 'bbp_unsubscribe' === $action ) {
    1438         bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum.', 'bbpress' ) );
    1439     } elseif ( 'bbp_subscribe' === $action ) {
    1440         bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum.', 'bbpress' ) );
    1441     }
    1442 
    1443     return (bool) $success;
    1444 }
    1445 
    1446 /**
    1447  * Handles the front end subscribing and unsubscribing topics
    1448  *
    1449  * @since 2.0.0 bbPress (r2790)
    1450  *
    1451  * @param string $action The requested action to compare this function to
    1452  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    1453  * @uses bbp_get_user_id() To get the user id
    1454  * @uses bbp_verify_nonce_request() To verify the nonce and check the request
    1455  * @uses current_user_can() To check if the current user can edit the user
    1456  * @uses bbPress:errors:add() To log the error messages
    1457  * @uses bbp_is_user_subscribed() To check if the topic is in user's
    1458  *                                 subscriptions
    1459  * @uses bbp_remove_user_subscription() To remove the user subscription
    1460  * @uses bbp_add_user_subscription() To add the user subscription
    1461  * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
    1462  *                    topic id and action
    1463  * @uses bbp_is_subscription() To check if it's the subscription page
    1464  * @uses bbp_get_topic_permalink() To get the topic permalink
    1465  * @uses bbp_redirect() To redirect to the url
    1466  */
    1467 function bbp_subscriptions_handler( $action = '' ) {
    1468 
    1469     // Default
    1470     $success = false;
    1471 
    1472     // Bail if subscriptions not active
    1473     if ( ! bbp_is_subscriptions_active() ) {
    1474         return $success;
    1475     }
    1476 
    1477     // Bail if no topic ID is passed
    1478     if ( empty( $_GET['topic_id'] ) ) {
    1479         return $success;
    1480     }
    1481 
    1482     // Setup possible get actions
    1483     $possible_actions = array(
    1484         'bbp_subscribe',
    1485         'bbp_unsubscribe',
    1486     );
    1487 
    1488     // Bail if actions aren't meant for this function
    1489     if ( ! in_array( $action, $possible_actions, true ) ) {
    1490         return $success;
    1491     }
    1492 
    1493     // Get required data
    1494     $topic_id = bbp_get_topic_id( $_GET['topic_id'] );
    1495     $user_id  = bbp_get_user_id( 0, true, true );
    1496 
    1497     // Check for empty topic
    1498     if ( empty( $topic_id ) ) {
    1499         bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found. Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
    1500 
    1501     // Check nonce
    1502     } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $topic_id ) ) {
    1503         bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
    1504 
    1505     // Check current user's ability to edit the user
    1506     } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    1507         bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites of that user.', 'bbpress' ) );
    1508     }
    1509 
    1510     // Bail if we have errors
    1511     if ( bbp_has_errors() ) {
    1512         return $success;
    1513     }
    1514 
    1515     /** No errors *************************************************************/
    1516 
    1517     if ( 'bbp_unsubscribe' === $action ) {
    1518         $success = bbp_remove_user_subscription( $user_id, $topic_id );
    1519     } elseif ( 'bbp_subscribe' === $action ) {
    1520         $success = bbp_add_user_subscription( $user_id, $topic_id );
    1521     }
    1522 
    1523     // Do additional subscriptions actions
    1524     do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
    1525 
    1526     // Success!
    1527     if ( true === $success ) {
    1528 
    1529         // Redirect back from whence we came
    1530         if ( ! empty( $_REQUEST['redirect_to'] ) ) {
    1531             $redirect = $_REQUEST['redirect_to']; // Validated later
    1532         } elseif ( bbp_is_subscriptions() ) {
    1533             $redirect = bbp_get_subscriptions_permalink( $user_id );
    1534         } elseif ( bbp_is_single_user() ) {
    1535             $redirect = bbp_get_user_profile_url();
    1536         } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
    1537             $redirect = bbp_get_topic_permalink( $topic_id );
    1538         } elseif ( is_single() || is_page() ) {
    1539             $redirect = get_permalink();
    1540         } else {
    1541             $redirect = get_permalink( $topic_id );
    1542         }
    1543 
    1544         bbp_redirect( $redirect );
    1545 
    1546     // Fail! Handle errors
    1547     } elseif ( 'bbp_unsubscribe' === $action ) {
    1548         bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic.', 'bbpress' ) );
    1549     } elseif ( 'bbp_subscribe' === $action ) {
    1550         bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that topic.', 'bbpress' ) );
    1551     }
    1552 
    1553     return (bool) $success;
    1554 }
     1254    return bbp_subscriptions_handler( $action );
     1255}
Note: See TracChangeset for help on using the changeset viewer.