/** * Get the forum favorite link * * A custom wrapper for bbp_get_user_favorite_link() * * @since 2.5.0 bbPress (r5156) * * @uses bbp_parse_args() * @uses bbp_get_user_favorites_link() * @uses apply_filters() Calls 'bbp_get_topic_favorite_link' */ function bbp_get_topic_favorite_link($args = array()) { // No link $retval = false; // Parse the arguments $r = bbp_parse_args($args, array('user_id' => 0, 'topic_id' => 0, 'before' => '', 'after' => '', 'favorite' => esc_html__('Favorite', 'bbpress'), 'favorited' => esc_html__('Unfavorite', 'bbpress')), 'get_topic_favorite_link'); // Get the link $retval = bbp_get_user_favorites_link($r); return apply_filters('bbp_get_topic_favorite_link', $retval, $r, $args); }
?> <?php bbp_get_template_part('form', 'protected'); ?> <?php } else { ?> <?php bbp_topic_tag_list(); ?> <?php bbp_single_topic_description(array('before' => '<div class="bbp-template-notice info"><p class="bbp-forum-description">' . bbp_get_user_favorites_link() . bbp_get_topic_subscription_link())); ?> <?php if (bbp_show_lead_topic()) { ?> <?php bbp_get_template_part('content', 'single-topic-lead'); ?> <?php } ?> <?php
/** * Output the link to make a topic favorite/remove a topic from favorites * * @since 2.0.0 bbPress (r2652) * * @param array $args See {@link bbp_get_user_favorites_link()} * @param int $user_id Optional. User id * @param bool $wrap Optional. If you want to wrap the link in <span id="favorite-toggle">. * @uses bbp_get_user_favorites_link() To get the user favorites link */ function bbp_user_favorites_link($args = array(), $user_id = 0, $wrap = true) { echo bbp_get_user_favorites_link($args, $user_id, $wrap); }
/** * AJAX handler to add or remove a topic from a user's favorites * * @since bbPress (r3732) * * @uses bbp_is_favorites_active() To check if favorites are active * @uses bbp_is_user_logged_in() To check if user is logged in * @uses bbp_get_current_user_id() To get the current user id * @uses current_user_can() To check if the current user can edit the user * @uses bbp_get_topic() To get the topic * @uses wp_verify_nonce() To verify the nonce & check the referer * @uses bbp_is_user_favorite() To check if the topic is user's favorite * @uses bbp_remove_user_favorite() To remove the topic from user's favorites * @uses bbp_add_user_favorite() To add the topic from user's favorites * @uses bbp_ajax_response() To return JSON */ public function ajax_favorite() { // Bail if favorites are not active if (!bbp_is_favorites_active()) { bbp_ajax_response(false, __('Favorites are no longer active.', 'bbpress'), 300); } // Bail if user is not logged in if (!is_user_logged_in()) { bbp_ajax_response(false, __('Please login to make this topic a favorite.', 'bbpress'), 301); } // Get user and topic data $user_id = bbp_get_current_user_id(); $id = !empty($_POST['id']) ? intval($_POST['id']) : 0; // Bail if user cannot add favorites for this user if (!current_user_can('edit_user', $user_id)) { bbp_ajax_response(false, __('You do not have permission to do this.', 'bbpress'), 302); } // Get the topic $topic = bbp_get_topic($id); // Bail if topic cannot be found if (empty($topic)) { bbp_ajax_response(false, __('The topic could not be found.', 'bbpress'), 303); } // Bail if user did not take this action if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'toggle-favorite_' . $topic->ID)) { bbp_ajax_response(false, __('Are you sure you meant to do that?', 'bbpress'), 304); } // Take action $status = bbp_is_user_favorite($user_id, $topic->ID) ? bbp_remove_user_favorite($user_id, $topic->ID) : bbp_add_user_favorite($user_id, $topic->ID); // Bail if action failed if (empty($status)) { bbp_ajax_response(false, __('The request was unsuccessful. Please try again.', 'bbpress'), 305); } // Put subscription attributes in convenient array $attrs = array('topic_id' => $topic->ID, 'user_id' => $user_id); // Action succeeded bbp_ajax_response(true, bbp_get_user_favorites_link($attrs, $user_id, false), 200); }
/** * Output the link to make a topic favorite/remove a topic from favorites * * @since bbPress (r2652) * * @param array $add Optional. Add to favorites args * @param array $rem Optional. Remove from favorites args * @param int $user_id Optional. User id * @param int $topic_id Optional. Topic id * @uses bbp_get_user_favorites_link() To get the user favorites link */ function bbp_user_favorites_link($add = array(), $rem = array(), $user_id = 0, $topic_id = 0) { echo bbp_get_user_favorites_link($add, $rem, $user_id, $topic_id); }