function bb_delete_topic($topic_id, $new_status = 0)
{
    global $bbdb;
    $topic_id = (int) $topic_id;
    add_filter('get_topic_where', 'bb_no_where');
    if ($topic = get_topic($topic_id)) {
        $new_status = (int) $new_status;
        $old_status = (int) $topic->topic_status;
        if ($new_status == $old_status) {
            return;
        }
        $thread_args = array('per_page' => -1, 'order' => 'DESC');
        if (0 != $old_status && 0 == $new_status) {
            $thread_args['post_status'] = 'all';
        }
        $poster_ids = array();
        $posts = get_thread($topic_id, $thread_args);
        if ($posts && count($posts)) {
            foreach ($posts as $post) {
                _bb_delete_post($post->post_id, $new_status);
                $poster_ids[] = $post->poster_id;
            }
        }
        if (count($poster_ids)) {
            foreach (array_unique($poster_ids) as $id) {
                if ($user = bb_get_user($id)) {
                    $topics_replied_key = $bbdb->prefix . 'topics_replied';
                    bb_update_usermeta($user->ID, $topics_replied_key, $old_status ? $user->{$topics_replied_key} + 1 : $user->{$topics_replied_key} - 1);
                }
            }
        }
        if ($ids = $bbdb->get_col("SELECT user_id, meta_value FROM {$bbdb->usermeta} WHERE meta_key = 'favorites' and FIND_IN_SET('{$topic_id}', meta_value) > 0")) {
            foreach ($ids as $id) {
                bb_remove_user_favorite($id, $topic_id);
            }
        }
        switch ($new_status) {
            case 0:
                // Undeleting
                $bbdb->update($bbdb->topics, array('topic_status' => $new_status), compact('topic_id'));
                $topic_posts = (int) $bbdb->get_var($bbdb->prepare("SELECT COUNT(*) FROM {$bbdb->posts} WHERE topic_id = %d AND post_status = 0", $topic_id));
                $all_posts = (int) $bbdb->get_var($bbdb->prepare("SELECT COUNT(*) FROM {$bbdb->posts} WHERE topic_id = %d", $topic_id));
                bb_update_topicmeta($topic_id, 'deleted_posts', $all_posts - $topic_posts);
                $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET topics = topics + 1, posts = posts + %d WHERE forum_id = %d", $topic_posts, $topic->forum_id));
                $bbdb->update($bbdb->topics, compact('topic_posts'), compact('topic_id'));
                bb_topic_set_last_post($topic_id);
                bb_update_post_positions($topic_id);
                break;
            default:
                // Other statuses (like Delete and Bozo)
                bb_remove_topic_tags($topic_id);
                $bbdb->update($bbdb->topics, array('topic_status' => $new_status, 'tag_count' => 0), compact('topic_id'));
                $bbdb->query($bbdb->prepare("UPDATE {$bbdb->forums} SET topics = topics - 1, posts = posts - %d WHERE forum_id = %d", $topic->topic_posts, $topic->forum_id));
                break;
        }
        do_action('bb_delete_topic', $topic_id, $new_status, $old_status);
        wp_cache_delete($topic_id, 'bb_topic');
        wp_cache_delete($topic->topic_slug, 'bb_topic_slug');
        wp_cache_delete($topic_id, 'bb_thread');
        wp_cache_delete($topic->forum_id, 'bb_forum');
        wp_cache_flush('bb_forums');
        wp_cache_flush('bb_query');
        wp_cache_flush('bb_cache_posts_post_ids');
        return $topic_id;
    } else {
        return false;
    }
}
Ejemplo n.º 2
0
if (isset($_GET['fav']) && isset($_GET['topic_id'])) {
    bb_auth('logged_in');
    if (!bb_current_user_can('edit_favorites_of', $user_id)) {
        bb_die(__('You cannot edit those favorites. How did you get here?'));
    }
    $fav = (int) $_GET['fav'];
    $topic_id = (int) $_GET['topic_id'];
    bb_check_admin_referer('toggle-favorite_' . $topic_id);
    $topic = get_topic($topic_id);
    if (!$topic || 0 != $topic->topic_status) {
        exit;
    }
    if ($fav) {
        bb_add_user_favorite($user_id, $topic_id);
    } else {
        bb_remove_user_favorite($user_id, $topic_id);
    }
    $ref = wp_get_referer();
    if (false !== strpos($ref, bb_get_uri(null, null, BB_URI_CONTEXT_TEXT))) {
        bb_safe_redirect($ref);
    } else {
        wp_redirect(get_topic_link($topic_id));
    }
    exit;
}
if (!bb_is_profile()) {
    $sendto = get_profile_tab_link($user->ID, 'favorites');
    wp_redirect($sendto);
    exit;
}
if ($topics = get_user_favorites($user->ID, true)) {
Ejemplo n.º 3
0
     if (false !== bb_remove_topic_tag($tag_id, $user_id, $topic_id)) {
         die('1');
     }
     break;
 case 'dim-favorite':
     $user_id = bb_get_current_user_info('id');
     if (!($topic = get_topic($id))) {
         die('0');
     }
     if (!bb_current_user_can('edit_favorites_of', $user_id)) {
         die('-1');
     }
     bb_check_ajax_referer("toggle-favorite_{$topic->topic_id}");
     $is_fav = is_user_favorite($user_id, $topic->topic_id);
     if (1 == $is_fav) {
         if (bb_remove_user_favorite($user_id, $topic->topic_id)) {
             die('1');
         }
     } elseif (false === $is_fav) {
         if (bb_add_user_favorite($user_id, $topic->topic_id)) {
             die('1');
         }
     }
     break;
 case 'delete-post':
     // $id is post_id
     if (!bb_current_user_can('delete_post', $id)) {
         die('-1');
     }
     bb_check_ajax_referer("delete-post_{$id}");
     $status = (int) $_POST['status'];
Ejemplo n.º 4
0
                 }
             }
             unset($topic_ids);
         }
     }
     unset($terms, $term);
 }
 if (isset($_POST['clean-favorites']) && 1 == $_POST['clean-favorites']) {
     $favorites_key = $bbdb->prefix . 'favorites';
     if ($users = $bbdb->get_results("SELECT user_id AS id, meta_value AS favorites FROM {$bbdb->usermeta} WHERE meta_key = '" . $favorites_key . "'")) {
         $messages[] = __('Removed deleted topics from users\' favorites');
         $topics = $bbdb->get_col("SELECT topic_id FROM {$bbdb->topics} WHERE topic_status = '0'");
         foreach ($users as $user) {
             foreach (explode(',', $user->favorites) as $favorite) {
                 if (!in_array($favorite, $topics)) {
                     bb_remove_user_favorite($user->id, $favorite);
                 }
             }
         }
         unset($topics, $users, $user, $favorite);
     }
 }
 bb_recount_list();
 foreach ((array) $recount_list as $item) {
     if (isset($item[2]) && isset($_POST[$item[0]]) && 1 == $_POST[$item[0]] && is_callable($item[2])) {
         call_user_func($item[2]);
     }
 }
 if (count($messages)) {
     $messages = join('</p>' . "\n" . '<p>', $messages);
     bb_admin_notice($messages);