Skip to:
Content

bbPress.org

Changeset 6421


Ignore:
Timestamp:
05/22/2017 06:02:20 PM (8 years ago)
Author:
johnjamesjacoby
Message:

Users: move Engagements API into its own engagements.php file.

/users/functions.php is getting a bit gnarly.

Location:
trunk/src
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bbpress.php

    r6341 r6421  
    360360        // Users
    361361        require $this->includes_dir . 'users/capabilities.php';
     362        require $this->includes_dir . 'users/engagements.php';
    362363        require $this->includes_dir . 'users/functions.php';
    363364        require $this->includes_dir . 'users/template.php';
  • trunk/src/includes/users/functions.php

    r6415 r6421  
    273273
    274274    return (bool) apply_filters( 'bbp_is_object_of_user', (bool) $retval, $object_id, $user_id, $meta_key, $meta_type );
    275 }
    276 
    277 /** Engagements ***************************************************************/
    278 
    279 /**
    280  * Get the users who have engaged in a topic
    281  *
    282  * @since 2.6.0 bbPress (r6320)
    283  *
    284  * @param int $topic_id Optional. Topic id
    285  * @uses bbp_get_users_for_object() To get user ids who engaged
    286  * @uses apply_filters() Calls 'bbp_get_topic_engagements' with the users and
    287  *                        topic id
    288  * @return array|bool Results if the topic has any engagements, otherwise false
    289  */
    290 function bbp_get_topic_engagements( $topic_id = 0 ) {
    291     $topic_id = bbp_get_topic_id( $topic_id );
    292     $users    = bbp_get_users_for_object( $topic_id, '_bbp_engagement' );
    293 
    294     return (array) apply_filters( 'bbp_get_topic_engagements', $users, $topic_id );
    295 }
    296 
    297 /**
    298  * Get a user's topic engagements
    299  *
    300  * @since 2.6.0 bbPress (r6320)
    301  *
    302  * @param int $user_id Optional. User id
    303  * @uses bbp_has_topics() To get the topics
    304  * @uses apply_filters() Calls 'bbp_get_user_engagements' with the topic query and
    305  *                        user id
    306  * @return array|bool Results if user has engaged, otherwise false
    307  */
    308 function bbp_get_user_engagements( $user_id = 0 ) {
    309     $user_id     = bbp_get_user_id( $user_id );
    310     $engagements = bbp_has_topics( array(
    311         'meta_query' => array(
    312             array(
    313                 'key'     => '_bbp_engagement',
    314                 'value'   => $user_id,
    315                 'compare' => 'NUMERIC'
    316             )
    317         )
    318     ) );
    319 
    320     return apply_filters( 'bbp_get_user_engagements', $engagements, $user_id );
    321 }
    322 
    323 /**
    324  * Get a user's engaged topic ids
    325  *
    326  * @since 2.6.0 bbPress (r6320)
    327  *
    328  * @param int $user_id Optional. User id
    329  * @uses bbp_get_user_id() To get the user id
    330  * @uses bbp_get_topic_post_type() To get the topic post type
    331  * @uses apply_filters() Calls 'bbp_get_user_engaged_topic_ids' with
    332  *                        the engaged topics and user id
    333  * @return array|bool Results if user has engaged, otherwise null
    334  */
    335 function bbp_get_user_engaged_topic_ids( $user_id = 0 ) {
    336     $user_id     = bbp_get_user_id( $user_id );
    337     $engagements = new WP_Query( array(
    338         'fields'        => 'ids',
    339         'post_type'     => bbp_get_topic_post_type(),
    340         'nopaging'      => true,
    341         'no_found_rows' => true,
    342         'meta_query'    => array( array(
    343             'key'     => '_bbp_engagement',
    344             'value'   => $user_id,
    345             'compare' => 'NUMERIC'
    346         ) )
    347     ) );
    348 
    349     return (array) apply_filters( 'bbp_get_user_engaged_topic_ids', $engagements->posts, $user_id );
    350 }
    351 
    352 /**
    353  * Check if a user is engaged in a topic or not
    354  *
    355  * @since 2.6.0 bbPress (r6320)
    356  *
    357  * @param int $user_id Optional. User id
    358  * @param int $topic_id Optional. Topic id
    359  * @uses bbp_get_user_id() To get the user id
    360  * @uses bbp_get_topic_id() To get the topic id
    361  * @uses bbp_is_object_of_user() To check if the user has engaged
    362  * @uses apply_filters() Calls 'bbp_is_user_engaged' with the bool, user id,
    363  *                        topic id and engagements
    364  * @return bool True if the topic is in user's engagements, otherwise false
    365  */
    366 function bbp_is_user_engaged( $user_id = 0, $topic_id = 0 ) {
    367     $user_id  = bbp_get_user_id( $user_id, true, true );
    368     $topic_id = bbp_get_topic_id( $topic_id );
    369     $retval   = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_engagement' );
    370 
    371     return (bool) apply_filters( 'bbp_is_user_engaged', (bool) $retval, $user_id, $topic_id );
    372 }
    373 
    374 /**
    375  * Add a topic to user's engagements
    376  *
    377  * @since 2.6.0 bbPress (r6320)
    378  *
    379  * @param int $user_id Optional. User id
    380  * @param int $topic_id Optional. Topic id
    381  * @uses bbp_is_user_engaged() To check if the user is engaged in a topic
    382  * @uses do_action() Calls 'bbp_add_user_engagement' with the user id and topic id
    383  * @return bool Always true
    384  */
    385 function bbp_add_user_engagement( $user_id = 0, $topic_id = 0 ) {
    386 
    387     // Bail if not enough info
    388     if ( empty( $user_id ) || empty( $topic_id ) ) {
    389         return false;
    390     }
    391 
    392     // Bail if no topic
    393     $topic = bbp_get_topic( $topic_id );
    394     if ( empty( $topic ) ) {
    395         return false;
    396     }
    397 
    398     // Bail if already a engaged
    399     if ( bbp_is_user_engaged( $user_id, $topic_id ) ) {
    400         return false;
    401     }
    402 
    403     // Bail if add fails
    404     if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_engagement' ) ) {
    405         return false;
    406     }
    407 
    408     do_action( 'bbp_add_user_engagement', $user_id, $topic_id );
    409 
    410     return true;
    411 }
    412 
    413 /**
    414  * Remove a topic from user's engagements
    415  *
    416  * @since 2.6.0 bbPress (r6320)
    417  *
    418  * @param int $user_id Optional. User id
    419  * @param int $topic_id Optional. Topic id
    420  * @uses bbp_is_user_engaged() To check if the user is engaged in a topic
    421  * @uses do_action() Calls 'bbp_remove_user_engagement' with the user & topic id
    422  * @return bool True if the topic was removed from user's engagements, otherwise
    423  *               false
    424  */
    425 function bbp_remove_user_engagement( $user_id, $topic_id ) {
    426 
    427     // Bail if not enough info
    428     if ( empty( $user_id ) || empty( $topic_id ) ) {
    429         return false;
    430     }
    431 
    432     // Bail if not already engaged
    433     if ( ! bbp_is_user_engaged( $user_id, $topic_id ) ) {
    434         return false;
    435     }
    436 
    437     // Bail if remove fails
    438     if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_engagement' ) ) {
    439         return false;
    440     }
    441 
    442     do_action( 'bbp_remove_user_engagement', $user_id, $topic_id );
    443 
    444     return true;
    445 }
    446 
    447 /** Favorites *****************************************************************/
    448 
    449 /**
    450  * Get the users who have made the topic favorite
    451  *
    452  * @since 2.0.0 bbPress (r2658)
    453  *
    454  * @param int $topic_id Optional. Topic id
    455  * @uses bbp_get_users_for_object() To get user IDs who favorited
    456  * @uses apply_filters() Calls 'bbp_get_topic_favoriters' with the users and
    457  *                        topic id
    458  * @return array|bool Results if the topic has any favoriters, otherwise false
    459  */
    460 function bbp_get_topic_favoriters( $topic_id = 0 ) {
    461     $topic_id = bbp_get_topic_id( $topic_id );
    462     $users    = bbp_get_users_for_object( $topic_id, '_bbp_favorite' );
    463 
    464     return (array) apply_filters( 'bbp_get_topic_favoriters', $users, $topic_id );
    465 }
    466 
    467 /**
    468  * Get a user's favorite topics
    469  *
    470  * @since 2.0.0 bbPress (r2652)
    471  *
    472  * @param int $user_id Optional. User id
    473  * @uses bbp_has_topics() To get the topics
    474  * @uses apply_filters() Calls 'bbp_get_user_favorites' with the topic query and
    475  *                        user id
    476  * @return array|bool Results if user has favorites, otherwise false
    477  */
    478 function bbp_get_user_favorites( $user_id = 0 ) {
    479     $user_id = bbp_get_user_id( $user_id );
    480     $query   = bbp_has_topics( array(
    481         'meta_query' => array(
    482             array(
    483                 'key'     => '_bbp_favorite',
    484                 'value'   => $user_id,
    485                 'compare' => 'NUMERIC'
    486             )
    487         )
    488     ) );
    489 
    490     return apply_filters( 'bbp_get_user_favorites', $query, $user_id );
    491 }
    492 
    493 /**
    494  * Get a user's favorite topic ids
    495  *
    496  * @since 2.0.0 bbPress (r2652)
    497  *
    498  * @param int $user_id Optional. User id
    499  * @uses bbp_get_user_id() To get the user id
    500  * @uses bbp_get_topic_post_type() To get the topic post type
    501  * @uses apply_filters() Calls 'bbp_get_user_favorites_topic_ids' with
    502  *                        the favorites and user id
    503  * @return array|bool Results if user has favorites, otherwise null
    504  */
    505 function bbp_get_user_favorites_topic_ids( $user_id = 0 ) {
    506     $user_id   = bbp_get_user_id( $user_id );
    507     $favorites = new WP_Query( array(
    508         'fields'        => 'ids',
    509         'post_type'     => bbp_get_topic_post_type(),
    510         'nopaging'      => true,
    511         'no_found_rows' => true,
    512         'meta_query'    => array( array(
    513             'key'     => '_bbp_favorite',
    514             'value'   => $user_id,
    515             'compare' => 'NUMERIC'
    516         ) )
    517     ) );
    518 
    519     return (array) apply_filters( 'bbp_get_user_favorites_topic_ids', $favorites->posts, $user_id );
    520 }
    521 
    522 /**
    523  * Check if a topic is in user's favorites or not
    524  *
    525  * @since 2.0.0 bbPress (r2652)
    526  *
    527  * @param int $user_id Optional. User id
    528  * @param int $topic_id Optional. Topic id
    529  * @uses bbp_get_user_id() To get the user id
    530  * @uses bbp_get_user_favorites_topic_ids() To get the user favorites
    531  * @uses bbp_get_topic() To get the topic
    532  * @uses bbp_get_topic_id() To get the topic id
    533  * @uses bbp_is_object_of_user() To check if the user has a favorite
    534  * @uses apply_filters() Calls 'bbp_is_user_favorite' with the bool, user id,
    535  *                        topic id and favorites
    536  * @return bool True if the topic is in user's favorites, otherwise false
    537  */
    538 function bbp_is_user_favorite( $user_id = 0, $topic_id = 0 ) {
    539     $retval    = false;
    540     $user_id   = bbp_get_user_id( $user_id, true, true );
    541     $favorites = bbp_get_user_favorites_topic_ids( $user_id );
    542 
    543     if ( ! empty( $favorites ) ) {
    544 
    545         // Checking a specific topic id
    546         if ( ! empty( $topic_id ) ) {
    547             $topic    = bbp_get_topic( $topic_id );
    548             $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    549 
    550         // Using the global topic id
    551         } elseif ( bbp_get_topic_id() ) {
    552             $topic_id = bbp_get_topic_id();
    553 
    554         // Use the current post id
    555         } elseif ( ! bbp_get_topic_id() ) {
    556             $topic_id = get_the_ID();
    557         }
    558 
    559         // Is topic_id in the user's favorites
    560         if ( ! empty( $topic_id ) ) {
    561             $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_favorite' );
    562         }
    563     }
    564 
    565     return (bool) apply_filters( 'bbp_is_user_favorite', (bool) $retval, $user_id, $topic_id, $favorites );
    566 }
    567 
    568 /**
    569  * Add a topic to user's favorites
    570  *
    571  * @since 2.0.0 bbPress (r2652)
    572  *
    573  * @param int $user_id Optional. User id
    574  * @param int $topic_id Optional. Topic id
    575  * @uses bbp_is_user_favorite() To check if the topic is a user favorite
    576  * @uses do_action() Calls 'bbp_add_user_favorite' with the user id and topic id
    577  * @return bool Always true
    578  */
    579 function bbp_add_user_favorite( $user_id = 0, $topic_id = 0 ) {
    580 
    581     // Bail if not enough info
    582     if ( empty( $user_id ) || empty( $topic_id ) ) {
    583         return false;
    584     }
    585 
    586     // Bail if no topic
    587     $topic = bbp_get_topic( $topic_id );
    588     if ( empty( $topic ) ) {
    589         return false;
    590     }
    591 
    592     // Bail if already a favorite
    593     if ( bbp_is_user_favorite( $user_id, $topic_id ) ) {
    594         return false;
    595     }
    596 
    597     // Bail if add fails
    598     if ( ! bbp_add_user_to_object( $topic_id, $user_id, '_bbp_favorite' ) ) {
    599         return false;
    600     }
    601 
    602     do_action( 'bbp_add_user_favorite', $user_id, $topic_id );
    603 
    604     return true;
    605 }
    606 
    607 /**
    608  * Remove a topic from user's favorites
    609  *
    610  * @since 2.0.0 bbPress (r2652)
    611  *
    612  * @param int $user_id Optional. User id
    613  * @param int $topic_id Optional. Topic id
    614  * @uses bbp_is_user_favorite() To check if the topic is a user favorite
    615  * @uses do_action() Calls 'bbp_remove_user_favorite' with the user & topic id
    616  * @return bool True if the topic was removed from user's favorites, otherwise
    617  *               false
    618  */
    619 function bbp_remove_user_favorite( $user_id, $topic_id ) {
    620 
    621     // Bail if not enough info
    622     if ( empty( $user_id ) || empty( $topic_id ) ) {
    623         return false;
    624     }
    625 
    626     // Bail if not already a favorite
    627     if ( ! bbp_is_user_favorite( $user_id, $topic_id ) ) {
    628         return false;
    629     }
    630 
    631     // Bail if remove fails
    632     if ( ! bbp_remove_user_from_object( $topic_id, $user_id, '_bbp_favorite' ) ) {
    633         return false;
    634     }
    635 
    636     do_action( 'bbp_remove_user_favorite', $user_id, $topic_id );
    637 
    638     return true;
    639 }
    640 
    641 /**
    642  * Handles the front end adding and removing of favorite topics
    643  *
    644  * @param string $action The requested action to compare this function to
    645  * @uses bbp_get_user_id() To get the user id
    646  * @uses bbp_verify_nonce_request() To verify the nonce and check the request
    647  * @uses current_user_can() To check if the current user can edit the user
    648  * @uses bbPress:errors:add() To log the error messages
    649  * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
    650  * @uses bbp_remove_user_favorite() To remove the user favorite
    651  * @uses bbp_add_user_favorite() To add the user favorite
    652  * @uses do_action() Calls 'bbp_favorites_handler' with success, user id, topic
    653  *                    id and action
    654  * @uses bbp_is_favorites() To check if it's the favorites page
    655  * @uses bbp_get_favorites_link() To get the favorites page link
    656  * @uses bbp_get_topic_permalink() To get the topic permalink
    657  * @uses bbp_redirect() To redirect to the url
    658  */
    659 function bbp_favorites_handler( $action = '' ) {
    660 
    661     if ( ! bbp_is_favorites_active() ) {
    662         return false;
    663     }
    664 
    665     // Bail if no topic ID is passed
    666     if ( empty( $_GET['topic_id'] ) ) {
    667         return;
    668     }
    669 
    670     // Setup possible get actions
    671     $possible_actions = array(
    672         'bbp_favorite_add',
    673         'bbp_favorite_remove',
    674     );
    675 
    676     // Bail if actions aren't meant for this function
    677     if ( ! in_array( $action, $possible_actions, true ) ) {
    678         return;
    679     }
    680 
    681     // What action is taking place?
    682     $topic_id = intval( $_GET['topic_id'] );
    683     $user_id  = bbp_get_user_id( 0, true, true );
    684 
    685     // Check for empty topic
    686     if ( empty( $topic_id ) ) {
    687         bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found. Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
    688 
    689     // Check nonce
    690     } elseif ( ! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) {
    691         bbp_add_error( 'bbp_favorite_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
    692 
    693     // Check current user's ability to edit the user
    694     } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    695         bbp_add_error( 'bbp_favorite_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites for that user!.', 'bbpress' ) );
    696     }
    697 
    698     // Bail if errors
    699     if ( bbp_has_errors() ) {
    700         return;
    701     }
    702 
    703     /** No errors *************************************************************/
    704 
    705     $is_favorite = bbp_is_user_favorite( $user_id, $topic_id );
    706     $success     = false;
    707 
    708     if ( true === $is_favorite && 'bbp_favorite_remove' === $action ) {
    709         $success = bbp_remove_user_favorite( $user_id, $topic_id );
    710     } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) {
    711         $success = bbp_add_user_favorite( $user_id, $topic_id );
    712     }
    713 
    714     // Do additional favorites actions
    715     do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
    716 
    717     // Success!
    718     if ( true === $success ) {
    719 
    720         // Redirect back from whence we came
    721         if ( ! empty( $_REQUEST['redirect_to'] ) ) {
    722             $redirect = $_REQUEST['redirect_to']; // Validated later
    723         } elseif ( bbp_is_favorites() ) {
    724             $redirect = bbp_get_favorites_permalink( $user_id, true );
    725         } elseif ( bbp_is_single_user() ) {
    726             $redirect = bbp_get_user_profile_url();
    727         } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
    728             $redirect = bbp_get_topic_permalink( $topic_id );
    729         } elseif ( is_single() || is_page() ) {
    730             $redirect = get_permalink();
    731         } else {
    732             $redirect = get_permalink( $topic_id );
    733         }
    734 
    735         bbp_redirect( $redirect );
    736 
    737     // Fail! Handle errors
    738     } elseif ( true === $is_favorite && 'bbp_favorite_remove' === $action ) {
    739         bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites.', 'bbpress' ) );
    740     } elseif ( false === $is_favorite && 'bbp_favorite_add' === $action ) {
    741         bbp_add_error( 'bbp_favorite_add',    __( '<strong>ERROR</strong>: There was a problem favoriting that topic.', 'bbpress' ) );
    742     }
    743 }
    744 
    745 /** Subscriptions *************************************************************/
    746 
    747 /**
    748  * Get the users who have subscribed to the forum
    749  *
    750  * @since 2.5.0 bbPress (r5156)
    751  *
    752  * @param int $forum_id Optional. forum id
    753  * @uses bbp_get_users_for_object() To get the forum subscribers
    754  * @uses apply_filters() Calls 'bbp_get_forum_subscribers' with the subscribers
    755  * @return array|bool Results if the forum has any subscribers, otherwise false
    756  */
    757 function bbp_get_forum_subscribers( $forum_id = 0 ) {
    758     $forum_id = bbp_get_forum_id( $forum_id );
    759     $users    = bbp_get_users_for_object( $forum_id, '_bbp_subscription' );
    760 
    761     return (array) apply_filters( 'bbp_get_forum_subscribers', $users, $forum_id );
    762 }
    763 
    764 /**
    765  * Get the users who have subscribed to the topic
    766  *
    767  * @since 2.0.0 bbPress (r2668)
    768  *
    769  * @param int $topic_id Optional. Topic id
    770  * @uses bbp_get_users_for_object() To get the topic subscribers
    771  * @uses apply_filters() Calls 'bbp_get_topic_subscribers' with the subscribers
    772  * @return array|bool Results if the topic has any subscribers, otherwise false
    773  */
    774 function bbp_get_topic_subscribers( $topic_id = 0 ) {
    775     $topic_id = bbp_get_topic_id( $topic_id );
    776     $users    = bbp_get_users_for_object( $topic_id, '_bbp_subscription' );
    777 
    778     return (array) apply_filters( 'bbp_get_topic_subscribers', $users, $topic_id );
    779 }
    780 
    781 /**
    782  * Get a user's subscribed topics
    783  *
    784  * @since 2.0.0 bbPress (r2668)
    785  *
    786  * @deprecated 2.5.0 bbPress (r5156)
    787  *
    788  * @param int $user_id Optional. User id
    789  * @uses bbp_get_user_topic_subscriptions() To get the user's subscriptions
    790  * @return array|bool Results if user has subscriptions, otherwise false
    791  */
    792 function bbp_get_user_subscriptions( $user_id = 0 ) {
    793     _deprecated_function( __FUNCTION__, 2.5, 'bbp_get_user_topic_subscriptions()' );
    794     $query = bbp_get_user_topic_subscriptions( $user_id );
    795     return apply_filters( 'bbp_get_user_subscriptions', $query, $user_id );
    796 }
    797 
    798 /**
    799  * Get a user's subscribed topics
    800  *
    801  * @since 2.0.0 bbPress (r2668)
    802  *
    803  * @param int $user_id Optional. User id
    804  * @uses bbp_has_topics() To get the topics
    805  * @uses apply_filters() Calls 'bbp_get_user_subscriptions' with the topic query
    806  *                        and user id
    807  * @return array|bool Results if user has subscriptions, otherwise false
    808  */
    809 function bbp_get_user_topic_subscriptions( $user_id = 0 ) {
    810     $user_id = bbp_get_user_id( $user_id );
    811     $query   = bbp_has_topics( array(
    812         'meta_query' => array(
    813             array(
    814                 'key'     => '_bbp_subscription',
    815                 'value'   => $user_id,
    816                 'compare' => 'NUMERIC'
    817             )
    818         )
    819     ) );
    820 
    821     return apply_filters( 'bbp_get_user_topic_subscriptions', $query, $user_id );
    822 }
    823 
    824 /**
    825  * Get a user's subscribed forums
    826  *
    827  * @since 2.5.0 bbPress (r5156)
    828  *
    829  * @param int $user_id Optional. User id
    830  * @uses bbp_has_forums() To get the forums
    831  * @uses apply_filters() Calls 'bbp_get_user_forum_subscriptions' with the forum
    832  *                        query and user id
    833  * @return array|bool Results if user has subscriptions, otherwise false
    834  */
    835 function bbp_get_user_forum_subscriptions( $user_id = 0 ) {
    836     $user_id = bbp_get_user_id( $user_id );
    837     $query   = bbp_has_forums( array(
    838         'meta_query' => array(
    839             array(
    840                 'key'     => '_bbp_subscription',
    841                 'value'   => $user_id,
    842                 'compare' => 'NUMERIC'
    843             )
    844         )
    845     ) );
    846 
    847     return apply_filters( 'bbp_get_user_forum_subscriptions', $query, $user_id );
    848 }
    849 
    850 /**
    851  * Get a user's subscribed forum ids
    852  *
    853  * @since 2.5.0 bbPress (r5156)
    854  *
    855  * @param int $user_id Optional. User id
    856  * @uses bbp_get_user_id() To get the user id
    857  * @uses bbp_get_forum_post_type() To get the forum post type
    858  * @uses apply_filters() Calls 'bbp_get_user_subscribed_forum_ids' with
    859  *                        the subscriptions and user id
    860  * @return array|bool Results if user has subscriptions, otherwise null
    861  */
    862 function bbp_get_user_subscribed_forum_ids( $user_id = 0 ) {
    863     $user_id       = bbp_get_user_id( $user_id );
    864     $subscriptions = new WP_Query( array(
    865         'fields'        => 'ids',
    866         'post_type'     => bbp_get_forum_post_type(),
    867         'nopaging'      => true,
    868         'no_found_rows' => true,
    869         'meta_query'    => array( array(
    870             'key'     => '_bbp_subscription',
    871             'value'   => $user_id,
    872             'compare' => 'NUMERIC'
    873         ) )
    874     ) );
    875 
    876     return (array) apply_filters( 'bbp_get_user_subscribed_forum_ids', $subscriptions->posts, $user_id );
    877 }
    878 
    879 /**
    880  * Get a user's subscribed topics' ids
    881  *
    882  * @since 2.0.0 bbPress (r2668)
    883  *
    884  * @param int $user_id Optional. User id
    885  * @uses bbp_get_user_id() To get the user id
    886  * @uses bbp_get_topic_post_type() To get the topic post type
    887  * @uses apply_filters() Calls 'bbp_get_user_subscribed_topic_ids' with
    888  *                        the subscriptions and user id
    889  * @return array|bool Results if user has subscriptions, otherwise null
    890  */
    891 function bbp_get_user_subscribed_topic_ids( $user_id = 0 ) {
    892     $user_id       = bbp_get_user_id( $user_id );
    893     $subscriptions = new WP_Query( array(
    894         'fields'        => 'ids',
    895         'post_type'     => bbp_get_topic_post_type(),
    896         'nopaging'      => true,
    897         'no_found_rows' => true,
    898         'meta_query' => array( array(
    899             'key'     => '_bbp_subscription',
    900             'value'   => $user_id,
    901             'compare' => 'NUMERIC'
    902         ) )
    903     ) );
    904 
    905     return (array) apply_filters( 'bbp_get_user_subscribed_topic_ids', $subscriptions->posts, $user_id );
    906 }
    907 
    908 /**
    909  * Check if a topic or forum is in user's subscription list or not
    910  *
    911  * @since 2.5.0 bbPress (r5156)
    912  *
    913  * @param int $user_id Optional. User id
    914  * @param int $object_id Optional. Topic id
    915  * @uses get_post() To get the post object
    916  * @uses bbp_get_user_subscribed_forum_ids() To get the user's forum subscriptions
    917  * @uses bbp_get_user_subscribed_topic_ids() To get the user's topic subscriptions
    918  * @uses bbp_get_forum_post_type() To get the forum post type
    919  * @uses bbp_get_topic_post_type() To get the topic post type
    920  * @uses apply_filters() Calls 'bbp_is_user_subscribed' with the bool, user id,
    921  *                        forum/topic id and subsriptions
    922  * @return bool True if the forum or topic is in user's subscriptions, otherwise false
    923  */
    924 function bbp_is_user_subscribed( $user_id = 0, $object_id = 0 ) {
    925 
    926     // Assume user is not subscribed
    927     $retval = false;
    928 
    929     // Setup ID's array
    930     $subscribed_ids = array();
    931 
    932     // User and object ID's are passed
    933     if ( ! empty( $user_id ) && ! empty( $object_id ) ) {
    934 
    935         // Get the post type
    936         $post_type = get_post_type( $object_id );
    937 
    938         // Post exists, so check the types
    939         if ( ! empty( $post_type ) ) {
    940 
    941             switch( $post_type ) {
    942 
    943                 // Forum
    944                 case bbp_get_forum_post_type() :
    945                     $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
    946                     $retval         = bbp_is_user_subscribed_to_forum( $user_id, $object_id, $subscribed_ids );
    947                     break;
    948 
    949                 // Topic (default)
    950                 case bbp_get_topic_post_type() :
    951                 default :
    952                     $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
    953                     $retval         = bbp_is_user_subscribed_to_topic( $user_id, $object_id, $subscribed_ids );
    954                     break;
    955             }
    956         }
    957     }
    958 
    959     return (bool) apply_filters( 'bbp_is_user_subscribed', $retval, $user_id, $object_id, $subscribed_ids );
    960 }
    961 
    962 /**
    963  * Check if a forum is in user's subscription list or not
    964  *
    965  * @since 2.5.0 bbPress (r5156)
    966  *
    967  * @param int $user_id Optional. User id
    968  * @param int $forum_id Optional. Topic id
    969  * @param array $subscribed_ids Optional. Array of forum ID's to check
    970  * @uses bbp_get_user_id() To get the user id
    971  * @uses bbp_get_forum() To get the forum
    972  * @uses bbp_get_forum_id() To get the forum id
    973  * @uses bbp_is_object_of_user() To check if the user has a subscription
    974  * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_forum' with the bool, user id,
    975  *                        forum id and subsriptions
    976  * @return bool True if the forum is in user's subscriptions, otherwise false
    977  */
    978 function bbp_is_user_subscribed_to_forum( $user_id = 0, $forum_id = 0, $subscribed_ids = array() ) {
    979 
    980     // Assume user is not subscribed
    981     $retval = false;
    982 
    983     // Validate user
    984     $user_id = bbp_get_user_id( $user_id, true, true );
    985     if ( ! empty( $user_id ) ) {
    986 
    987         // Get subscription ID's if none passed
    988         if ( empty( $subscribed_ids ) ) {
    989             $subscribed_ids = bbp_get_user_subscribed_forum_ids( $user_id );
    990         }
    991 
    992         // User has forum subscriptions
    993         if ( ! empty( $subscribed_ids ) ) {
    994 
    995             // Checking a specific forum id
    996             if ( ! empty( $forum_id ) ) {
    997                 $forum    = bbp_get_forum( $forum_id );
    998                 $forum_id = ! empty( $forum ) ? $forum->ID : 0;
    999 
    1000             // Using the global forum id
    1001             } elseif ( bbp_get_forum_id() ) {
    1002                 $forum_id = bbp_get_forum_id();
    1003 
    1004             // Use the current post id
    1005             } elseif ( ! bbp_get_forum_id() ) {
    1006                 $forum_id = get_the_ID();
    1007             }
    1008 
    1009             // Is forum_id in the user's subscriptions
    1010             if ( ! empty( $forum_id ) ) {
    1011                 $retval = bbp_is_object_of_user( $forum_id, $user_id, '_bbp_subscription' );
    1012             }
    1013         }
    1014     }
    1015 
    1016     return (bool) apply_filters( 'bbp_is_user_subscribed_to_forum', (bool) $retval, $user_id, $forum_id, $subscribed_ids );
    1017 }
    1018 
    1019 /**
    1020  * Check if a topic is in user's subscription list or not
    1021  *
    1022  * @since 2.5.0 bbPress (r5156)
    1023  *
    1024  * @param int $user_id Optional. User id
    1025  * @param int $topic_id Optional. Topic id
    1026  * @param array $subscribed_ids Optional. Array of topic ID's to check
    1027  * @uses bbp_get_user_id() To get the user id
    1028  * @uses bbp_get_topic() To get the topic
    1029  * @uses bbp_get_topic_id() To get the topic id
    1030  * @uses bbp_is_object_of_user() To check if the user is subscribed
    1031  * @uses apply_filters() Calls 'bbp_is_user_subscribed_to_topic' with the bool, user id,
    1032  *                        topic id and subsriptions
    1033  * @return bool True if the topic is in user's subscriptions, otherwise false
    1034  */
    1035 function bbp_is_user_subscribed_to_topic( $user_id = 0, $topic_id = 0, $subscribed_ids = array() ) {
    1036 
    1037     // Assume user is not subscribed
    1038     $retval = false;
    1039 
    1040     // Validate user
    1041     $user_id = bbp_get_user_id( $user_id, true, true );
    1042     if ( ! empty( $user_id ) ) {
    1043 
    1044         // Get subscription ID's if none passed
    1045         if ( empty( $subscribed_ids ) ) {
    1046             $subscribed_ids = bbp_get_user_subscribed_topic_ids( $user_id );
    1047         }
    1048 
    1049         // User has topic subscriptions
    1050         if ( ! empty( $subscribed_ids ) ) {
    1051 
    1052             // Checking a specific topic id
    1053             if ( ! empty( $topic_id ) ) {
    1054                 $topic    = bbp_get_topic( $topic_id );
    1055                 $topic_id = ! empty( $topic ) ? $topic->ID : 0;
    1056 
    1057             // Using the global topic id
    1058             } elseif ( bbp_get_topic_id() ) {
    1059                 $topic_id = bbp_get_topic_id();
    1060 
    1061             // Use the current post id
    1062             } elseif ( ! bbp_get_topic_id() ) {
    1063                 $topic_id = get_the_ID();
    1064             }
    1065 
    1066             // Is topic_id in the user's subscriptions
    1067             if ( ! empty( $topic_id ) ) {
    1068                 $retval = bbp_is_object_of_user( $topic_id, $user_id, '_bbp_subscription' );
    1069             }
    1070         }
    1071     }
    1072 
    1073     return (bool) apply_filters( 'bbp_is_user_subscribed_to_topic', (bool) $retval, $user_id, $topic_id, $subscribed_ids );
    1074 }
    1075 
    1076 /**
    1077  * Add a user subscription
    1078  *
    1079  * @since 2.5.0 bbPress (r5156)
    1080  *
    1081  * @param int $user_id Optional. User id
    1082  * @param int $object_id Optional. Topic id
    1083  * @uses get_post() To get the post object
    1084  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & object id
    1085  * @return bool Always true
    1086  */
    1087 function bbp_add_user_subscription( $user_id = 0, $object_id = 0 ) {
    1088 
    1089     // Bail if not enough info
    1090     if ( empty( $user_id ) || empty( $object_id ) ) {
    1091         return false;
    1092     }
    1093 
    1094     // Get the post type
    1095     $post_type = get_post_type( $object_id );
    1096     if ( empty( $post_type ) ) {
    1097         return false;
    1098     }
    1099 
    1100     // Bail if already subscribed
    1101     if ( bbp_is_user_subscribed( $user_id, $object_id ) ) {
    1102         return false;
    1103     }
    1104 
    1105     // Bail if add fails
    1106     if ( ! bbp_add_user_to_object( $object_id, $user_id, '_bbp_subscription' ) ) {
    1107         return false;
    1108     }
    1109 
    1110     do_action( 'bbp_add_user_subscription', $user_id, $object_id, $post_type );
    1111 
    1112     return true;
    1113 }
    1114 
    1115 /**
    1116  * Add a forum to user's subscriptions
    1117  *
    1118  * @since 2.5.0 bbPress (r5156)
    1119  *
    1120  * @param int $user_id Optional. User id
    1121  * @param int $forum_id Optional. forum id
    1122  * @uses bbp_get_forum() To get the forum
    1123  * @uses bbp_add_user_subscription() To add the user subscription
    1124  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & forum id
    1125  * @return bool Always true
    1126  */
    1127 function bbp_add_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
    1128 
    1129     // Bail if not enough info
    1130     if ( empty( $user_id ) || empty( $forum_id ) ) {
    1131         return false;
    1132     }
    1133 
    1134     // Bail if no forum
    1135     $forum = bbp_get_forum( $forum_id );
    1136     if ( empty( $forum ) ) {
    1137         return false;
    1138     }
    1139 
    1140     // Bail if already subscribed
    1141     if ( bbp_is_user_subscribed( $user_id, $forum_id ) ) {
    1142         return false;
    1143     }
    1144 
    1145     // Bail if add fails
    1146     if ( ! bbp_add_user_subscription( $user_id, $forum_id ) ) {
    1147         return false;
    1148     }
    1149 
    1150     do_action( 'bbp_add_user_forum_subscription', $user_id, $forum_id );
    1151 
    1152     return true;
    1153 }
    1154 
    1155 /**
    1156  * Add a topic to user's subscriptions
    1157  *
    1158  * @since 2.0.0 bbPress (r2668)
    1159  *
    1160  * @param int $user_id Optional. User id
    1161  * @param int $topic_id Optional. Topic id
    1162  * @uses bbp_get_topic() To get the topic
    1163  * @uses bbp_add_user_subscription() To add the subscription
    1164  * @uses do_action() Calls 'bbp_add_user_subscription' with the user & topic id
    1165  * @return bool Always true
    1166  */
    1167 function bbp_add_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    1168 
    1169     // Bail if not enough info
    1170     if ( empty( $user_id ) || empty( $topic_id ) ) {
    1171         return false;
    1172     }
    1173 
    1174     // Bail if no topic
    1175     $topic = bbp_get_topic( $topic_id );
    1176     if ( empty( $topic ) ) {
    1177         return false;
    1178     }
    1179 
    1180     // Bail if already subscribed
    1181     if ( bbp_is_user_subscribed_to_topic( $user_id, $topic_id ) ) {
    1182         return false;
    1183     }
    1184 
    1185     // Bail if add fails
    1186     if ( ! bbp_add_user_subscription( $user_id, $topic_id ) ) {
    1187         return false;
    1188     }
    1189 
    1190     do_action( 'bbp_add_user_topic_subscription', $user_id, $topic_id );
    1191 
    1192     return true;
    1193 }
    1194 
    1195 /**
    1196  * Remove a user subscription
    1197  *
    1198  * @since 2.0.0 bbPress (r2668)
    1199  *
    1200  * @param int $user_id Optional. User id
    1201  * @param int $object_id Optional. Topic id
    1202  * @uses get_post() To get the post object
    1203  * @uses bbp_is_user_subscribed() To check if the user is already subscribed
    1204  * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    1205  *                    topic id
    1206  * @return bool True if the topic was removed from user's subscriptions,
    1207  *               otherwise false
    1208  */
    1209 function bbp_remove_user_subscription( $user_id = 0, $object_id = 0 ) {
    1210 
    1211     // Bail if not enough info
    1212     if ( empty( $user_id ) || empty( $object_id ) ) {
    1213         return false;
    1214     }
    1215 
    1216     // Get post type
    1217     $post_type = get_post_type( $object_id );
    1218     if ( empty( $post_type ) ) {
    1219         return false;
    1220     }
    1221 
    1222     // Bail if not subscribed
    1223     if ( ! bbp_is_user_subscribed( $user_id, $object_id ) ) {
    1224         return false;
    1225     }
    1226 
    1227     // Bail if remove fails
    1228     if ( ! bbp_remove_user_from_object( $object_id, $user_id, '_bbp_subscription' ) ) {
    1229         return false;
    1230     }
    1231 
    1232     do_action( 'bbp_remove_user_subscription', $user_id, $object_id, $post_type );
    1233 
    1234     return true;
    1235 }
    1236 
    1237 /**
    1238  * Remove a forum from user's subscriptions
    1239  *
    1240  * @since 2.5.0 bbPress (r5156)
    1241  *
    1242  * @param int $user_id Optional. User id
    1243  * @param int $forum_id Optional. forum id
    1244  * @uses bbp_remove_user_subscription() To remove the subscription
    1245  * @uses do_action() Calls 'bbp_remove_user_subscription' with the user id and
    1246  *                    forum id
    1247  * @return bool True if the forum was removed from user's subscriptions,
    1248  *               otherwise false
    1249  */
    1250 function bbp_remove_user_forum_subscription( $user_id = 0, $forum_id = 0 ) {
    1251 
    1252     // Bail if not enough info
    1253     if ( empty( $user_id ) || empty( $forum_id ) ) {
    1254         return false;
    1255     }
    1256 
    1257     // Bail if remove fails
    1258     if ( ! bbp_remove_user_subscription( $user_id, $forum_id ) ) {
    1259         return false;
    1260     }
    1261 
    1262     do_action( 'bbp_remove_user_forum_subscription', $user_id, $forum_id );
    1263 
    1264     return true;
    1265 }
    1266 
    1267 /**
    1268  * Remove a topic from user's subscriptions
    1269  *
    1270  * @since 2.5.0 bbPress (r5156)
    1271  *
    1272  * @param int $user_id Optional. User id
    1273  * @param int $topic_id Optional. Topic id
    1274  * @uses bbp_remove_user_subscription() To remove the subscription
    1275  * @uses do_action() Calls 'bbp_remove_user_topic_subscription' with the user id and
    1276  *                    topic id
    1277  * @return bool True if the topic was removed from user's subscriptions,
    1278  *               otherwise false
    1279  */
    1280 function bbp_remove_user_topic_subscription( $user_id = 0, $topic_id = 0 ) {
    1281 
    1282     // Bail if not enough info
    1283     if ( empty( $user_id ) || empty( $topic_id ) ) {
    1284         return false;
    1285     }
    1286 
    1287     // Bail if remove fails
    1288     if ( ! bbp_remove_user_subscription( $user_id, $topic_id ) ) {
    1289         return false;
    1290     }
    1291 
    1292     do_action( 'bbp_remove_user_topic_subscription', $user_id, $topic_id );
    1293 
    1294     return true;
    1295 }
    1296 
    1297 /**
    1298  * Handles the front end subscribing and unsubscribing forums
    1299  *
    1300  * @since 2.5.0 bbPress (r5156)
    1301  *
    1302  * @param string $action The requested action to compare this function to
    1303  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    1304  * @uses bbp_get_user_id() To get the user id
    1305  * @uses bbp_verify_nonce_request() To verify the nonce and check the request
    1306  * @uses current_user_can() To check if the current user can edit the user
    1307  * @uses bbPress:errors:add() To log the error messages
    1308  * @uses bbp_is_user_subscribed() To check if the forum is in user's
    1309  *                                 subscriptions
    1310  * @uses bbp_remove_user_subscription() To remove the user subscription
    1311  * @uses bbp_add_user_subscription() To add the user subscription
    1312  * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
    1313  *                    forum id and action
    1314  * @uses bbp_is_subscription() To check if it's the subscription page
    1315  * @uses bbp_get_forum_permalink() To get the forum permalink
    1316  * @uses bbp_redirect() To redirect to the url
    1317  */
    1318 function bbp_forum_subscriptions_handler( $action = '' ) {
    1319 
    1320     if ( ! bbp_is_subscriptions_active() ) {
    1321         return false;
    1322     }
    1323 
    1324     // Bail if no forum ID is passed
    1325     if ( empty( $_GET['forum_id'] ) ) {
    1326         return;
    1327     }
    1328 
    1329     // Setup possible get actions
    1330     $possible_actions = array(
    1331         'bbp_subscribe',
    1332         'bbp_unsubscribe',
    1333     );
    1334 
    1335     // Bail if actions aren't meant for this function
    1336     if ( ! in_array( $action, $possible_actions, true ) ) {
    1337         return;
    1338     }
    1339 
    1340     // Get required data
    1341     $user_id  = bbp_get_user_id( 0, true, true );
    1342     $forum_id = intval( $_GET['forum_id'] );
    1343 
    1344     // Check for empty forum
    1345     if ( empty( $forum_id ) ) {
    1346         bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: No forum was found. Which forum are you subscribing/unsubscribing to?', 'bbpress' ) );
    1347 
    1348     // Check nonce
    1349     } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $forum_id ) ) {
    1350         bbp_add_error( 'bbp_subscription_forum_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
    1351 
    1352     // Check current user's ability to edit the user
    1353     } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    1354         bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites of that user.', 'bbpress' ) );
    1355     }
    1356 
    1357     // Bail if we have errors
    1358     if ( bbp_has_errors() ) {
    1359         return;
    1360     }
    1361 
    1362     /** No errors *************************************************************/
    1363 
    1364     $is_subscription = bbp_is_user_subscribed( $user_id, $forum_id );
    1365     $success         = false;
    1366 
    1367     if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
    1368         $success = bbp_remove_user_subscription( $user_id, $forum_id );
    1369     } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
    1370         $success = bbp_add_user_subscription( $user_id, $forum_id );
    1371     }
    1372 
    1373     // Do additional subscriptions actions
    1374     do_action( 'bbp_subscriptions_handler', $success, $user_id, $forum_id, $action );
    1375 
    1376     // Success!
    1377     if ( true === $success ) {
    1378 
    1379         // Redirect back from whence we came
    1380         if ( ! empty( $_REQUEST['redirect_to'] ) ) {
    1381             $redirect = $_REQUEST['redirect_to']; // Validated later
    1382         } elseif ( bbp_is_subscriptions() ) {
    1383             $redirect = bbp_get_subscriptions_permalink( $user_id );
    1384         } elseif ( bbp_is_single_user() ) {
    1385             $redirect = bbp_get_user_profile_url();
    1386         } elseif ( is_singular( bbp_get_forum_post_type() ) ) {
    1387             $redirect = bbp_get_forum_permalink( $forum_id );
    1388         } elseif ( is_single() || is_page() ) {
    1389             $redirect = get_permalink();
    1390         } else {
    1391             $redirect = get_permalink( $forum_id );
    1392         }
    1393 
    1394         bbp_redirect( $redirect );
    1395 
    1396     // Fail! Handle errors
    1397     } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
    1398         bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that forum.', 'bbpress' ) );
    1399     } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
    1400         bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that forum.', 'bbpress' ) );
    1401     }
    1402 }
    1403 
    1404 /**
    1405  * Handles the front end subscribing and unsubscribing topics
    1406  *
    1407  * @since 2.0.0 bbPress (r2790)
    1408  *
    1409  * @param string $action The requested action to compare this function to
    1410  * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
    1411  * @uses bbp_get_user_id() To get the user id
    1412  * @uses bbp_verify_nonce_request() To verify the nonce and check the request
    1413  * @uses current_user_can() To check if the current user can edit the user
    1414  * @uses bbPress:errors:add() To log the error messages
    1415  * @uses bbp_is_user_subscribed() To check if the topic is in user's
    1416  *                                 subscriptions
    1417  * @uses bbp_remove_user_subscription() To remove the user subscription
    1418  * @uses bbp_add_user_subscription() To add the user subscription
    1419  * @uses do_action() Calls 'bbp_subscriptions_handler' with success, user id,
    1420  *                    topic id and action
    1421  * @uses bbp_is_subscription() To check if it's the subscription page
    1422  * @uses bbp_get_topic_permalink() To get the topic permalink
    1423  * @uses bbp_redirect() To redirect to the url
    1424  */
    1425 function bbp_subscriptions_handler( $action = '' ) {
    1426 
    1427     if ( ! bbp_is_subscriptions_active() ) {
    1428         return false;
    1429     }
    1430 
    1431     // Bail if no topic ID is passed
    1432     if ( empty( $_GET['topic_id'] ) ) {
    1433         return;
    1434     }
    1435 
    1436     // Setup possible get actions
    1437     $possible_actions = array(
    1438         'bbp_subscribe',
    1439         'bbp_unsubscribe',
    1440     );
    1441 
    1442     // Bail if actions aren't meant for this function
    1443     if ( ! in_array( $action, $possible_actions, true ) ) {
    1444         return;
    1445     }
    1446 
    1447     // Get required data
    1448     $user_id  = bbp_get_user_id( 0, true, true );
    1449     $topic_id = intval( $_GET['topic_id'] );
    1450 
    1451     // Check for empty topic
    1452     if ( empty( $topic_id ) ) {
    1453         bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: No topic was found. Which topic are you subscribing/unsubscribing to?', 'bbpress' ) );
    1454 
    1455     // Check nonce
    1456     } elseif ( ! bbp_verify_nonce_request( 'toggle-subscription_' . $topic_id ) ) {
    1457         bbp_add_error( 'bbp_subscription_topic_id', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
    1458 
    1459     // Check current user's ability to edit the user
    1460     } elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
    1461         bbp_add_error( 'bbp_subscription_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites of that user.', 'bbpress' ) );
    1462     }
    1463 
    1464     // Bail if we have errors
    1465     if ( bbp_has_errors() ) {
    1466         return;
    1467     }
    1468 
    1469     /** No errors *************************************************************/
    1470 
    1471     $is_subscription = bbp_is_user_subscribed( $user_id, $topic_id );
    1472     $success         = false;
    1473 
    1474     if ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
    1475         $success = bbp_remove_user_subscription( $user_id, $topic_id );
    1476     } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
    1477         $success = bbp_add_user_subscription( $user_id, $topic_id );
    1478     }
    1479 
    1480     // Do additional subscriptions actions
    1481     do_action( 'bbp_subscriptions_handler', $success, $user_id, $topic_id, $action );
    1482 
    1483     // Success!
    1484     if ( true === $success ) {
    1485 
    1486         // Redirect back from whence we came
    1487         if ( ! empty( $_REQUEST['redirect_to'] ) ) {
    1488             $redirect = $_REQUEST['redirect_to']; // Validated later
    1489         } elseif ( bbp_is_subscriptions() ) {
    1490             $redirect = bbp_get_subscriptions_permalink( $user_id );
    1491         } elseif ( bbp_is_single_user() ) {
    1492             $redirect = bbp_get_user_profile_url();
    1493         } elseif ( is_singular( bbp_get_topic_post_type() ) ) {
    1494             $redirect = bbp_get_topic_permalink( $topic_id );
    1495         } elseif ( is_single() || is_page() ) {
    1496             $redirect = get_permalink();
    1497         } else {
    1498             $redirect = get_permalink( $topic_id );
    1499         }
    1500 
    1501         bbp_redirect( $redirect );
    1502 
    1503     // Fail! Handle errors
    1504     } elseif ( true === $is_subscription && 'bbp_unsubscribe' === $action ) {
    1505         bbp_add_error( 'bbp_unsubscribe', __( '<strong>ERROR</strong>: There was a problem unsubscribing from that topic.', 'bbpress' ) );
    1506     } elseif ( false === $is_subscription && 'bbp_subscribe' === $action ) {
    1507         bbp_add_error( 'bbp_subscribe',    __( '<strong>ERROR</strong>: There was a problem subscribing to that topic.', 'bbpress' ) );
    1508     }
    1509275}
    1510276
Note: See TracChangeset for help on using the changeset viewer.