Ejemplo n.º 1
0
function mb_handler_topic_bookmark()
{
    /* Is this a topic bookmark request? */
    if (!mb_is_board_action('toggle_bookmark') || !isset($_GET['topic_id'])) {
        return;
    }
    /* Make sure the user is logged in and bookmarks are active. */
    if (!is_user_logged_in() || !mb_is_bookmarks_active()) {
        return;
    }
    /* Get the topic ID. */
    $topic_id = mb_get_topic_id($_GET['topic_id']);
    /* Verify nonce. */
    if (!isset($_GET['mb_nonce']) || !wp_verify_nonce($_GET['mb_nonce'], "bookmark_topic_{$topic_id}")) {
        return;
    }
    /* Get the current user ID. */
    $user_id = get_current_user_id();
    /* If the current user can't read the topic, make sure they are not bookmarked and bail. */
    if (!current_user_can('read_topic', $topic_id)) {
        mb_remove_user_topic_bookmark($user_id, $topic_id);
        return;
    }
    /* If the user is already bookmarked, unbookmark them. */
    if (mb_is_topic_user_bookmark($user_id, $topic_id)) {
        mb_remove_user_topic_bookmark($user_id, $topic_id);
        /* Else, bookmark them. */
    } else {
        mb_add_user_topic_bookmark($user_id, $topic_id);
    }
    /* Redirect the user. */
    wp_safe_redirect(esc_url(remove_query_arg(array('mb_action', 'topic_id', 'mb_nonce'))));
}
Ejemplo n.º 2
0
/**
 * Returns the topic un/bookmark link.
 *
 * @since  1.0.0
 * @access public
 * @param  int     $topic_id
 * @return string
 */
function mb_get_topic_bookmark_link($topic_id = 0)
{
    if (!mb_is_bookmarks_active()) {
        return '';
    }
    $topic_id = mb_get_topic_id($topic_id);
    $link = '';
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        $url = mb_get_topic_bookmark_url($topic_id);
        $text = mb_is_topic_user_bookmark($user_id, $topic_id) ? __('Unbookmark', 'message-board') : __('Bookmark', 'message-board');
        if (!empty($url)) {
            $link = sprintf('<a class="mb-bookmark-link" href="%s">%s</a>', $url, $text);
        }
    }
    return apply_filters('mb_get_topic_bookmark_link', $link, $topic_id);
}