bbp_favorites_handler( string $action = '' )
Handles the front end adding and removing of favorite topics
Description Description
Parameters Parameters
- $action
-
(Optional) The requested action to compare this function to
Default value: ''
Source Source
File: includes/users/engagements.php
function bbp_favorites_handler( $action = '' ) {
// Default
$success = false;
// Bail if favorites not active
if ( ! bbp_is_favorites_active() ) {
return $success;
}
// Bail if no topic ID is passed
if ( empty( $_GET['object_id'] ) ) {
return $success;
}
// Setup possible get actions
$possible_actions = array(
'bbp_favorite_add',
'bbp_favorite_remove',
);
// Bail if actions aren't meant for this function
if ( ! in_array( $action, $possible_actions, true ) ) {
return $success;
}
// What action is taking place?
$topic_id = bbp_get_topic_id( $_GET['object_id'] );
$user_id = bbp_get_user_id( 0, true, true );
// Check for empty topic
if ( empty( $topic_id ) ) {
bbp_add_error( 'bbp_favorite_topic_id', __( '<strong>ERROR</strong>: No topic was found. Which topic are you marking/unmarking as favorite?', 'bbpress' ) );
// Check nonce
} elseif ( ! bbp_verify_nonce_request( 'toggle-favorite_' . $topic_id ) ) {
bbp_add_error( 'bbp_favorite_nonce', __( '<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress' ) );
// Check current user's ability to edit the user
} elseif ( ! current_user_can( 'edit_user', $user_id ) ) {
bbp_add_error( 'bbp_favorite_permission', __( '<strong>ERROR</strong>: You do not have permission to edit favorites for that user.', 'bbpress' ) );
}
// Bail if errors
if ( bbp_has_errors() ) {
return $success;
}
/** No errors *************************************************************/
if ( 'bbp_favorite_remove' === $action ) {
$success = bbp_remove_user_favorite( $user_id, $topic_id );
} elseif ( 'bbp_favorite_add' === $action ) {
$success = bbp_add_user_favorite( $user_id, $topic_id );
}
// Do additional favorites actions
do_action( 'bbp_favorites_handler', $success, $user_id, $topic_id, $action );
// Success!
if ( true === $success ) {
// Redirect back from whence we came
if ( ! empty( $_REQUEST['redirect_to'] ) ) {
$redirect = $_REQUEST['redirect_to']; // Validated later
} elseif ( bbp_is_favorites() ) {
$redirect = bbp_get_favorites_permalink( $user_id, true );
} elseif ( bbp_is_single_user() ) {
$redirect = bbp_get_user_profile_url();
} elseif ( is_singular( bbp_get_topic_post_type() ) ) {
$redirect = bbp_get_topic_permalink( $topic_id );
} elseif ( is_single() || is_page() ) {
$redirect = get_permalink();
} else {
$redirect = get_permalink( $topic_id );
}
bbp_redirect( $redirect );
// Fail! Handle errors
} elseif ( 'bbp_favorite_remove' === $action ) {
bbp_add_error( 'bbp_favorite_remove', __( '<strong>ERROR</strong>: There was a problem removing that topic from favorites.', 'bbpress' ) );
} elseif ( 'bbp_favorite_add' === $action ) {
bbp_add_error( 'bbp_favorite_add', __( '<strong>ERROR</strong>: There was a problem favoriting that topic.', 'bbpress' ) );
}
return (bool) $success;
}